您的位置:首页 > 其它

SDL2.0的加载图片贴图

2016-08-09 11:42 155 查看
加载图片贴图,采用了SDL_Window、SDL_Renderer、SDL_Texture和SDL_Image库

实例:

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <SDL2\SDL.h>
#include <SDL2\SDL_image.h>
#include <SDL2\ex\SDL_rectex.h>

SDL_Window *sdlWindow = NULL;
SDL_Renderer *sdlRender = NULL;
SDL_Texture *sdlTexture = NULL;
SDL_Rect srcRect;
SDL_Rect dstRect;
int w = 500;
int h = 500;

bool InitView(int width, int height, const char *iconName)
{
//初始化窗体
SDL_Init(SDL_INIT_VIDEO);

sdlWindow = SDL_CreateWindow(
"The First SDL Program",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height,
SDL_WINDOW_RESIZABLE);
if (sdlWindow == NULL) return false;

//加载窗体图标
SDL_Surface *iconSurface = IMG_Load(iconName);
if (iconSurface == NULL) return false;

SDL_SetWindowIcon(sdlWindow, iconSurface);

return true;
}

bool InitDraw(const char *imgName)
{
//加载渲染器
sdlRender = SDL_CreateRenderer(sdlWindow, -1, 0);
if (sdlRender == NULL) return false;
SDL_SetRenderDrawColor(sdlRender, 255, 255, 255, 255);

//加载绘画图片
SDL_Surface *sdlSurface = IMG_Load(imgName);
if (sdlSurface == NULL) return false;

//加载绘画纹理
sdlTexture = SDL_CreateTextureFromSurface(sdlRender, sdlSurface);
if (sdlTexture == NULL) return false;

SDL_FreeSurface(sdlSurface);
return true;
}

void UpdateDraw()
{
SDL_RenderClear(sdlRender);

//分X宫格
const int count = 9;
const int sqrtCount = (int)sqrt((double)count);
for (int i = 0; i < sqrtCount; i++)    {
srcRect = SDL_RectMake(0, 0, (w-sqrtCount)/sqrtCount, (h-sqrtCount)/sqrtCount);
for (int j = 0; j < sqrtCount; j++) {
srcRect.x = srcRect.w*j+(j?1*j:0);
srcRect.y = srcRect.h*i+(i?1*i:0);

//SDL_RectPrint("srcRect", srcRect);
SDL_RectCopy(&srcRect, &dstRect);
//SDL_RectPrint("dstRect", dstRect);
SDL_RenderCopy(sdlRender, sdlTexture, &srcRect, &dstRect);
}
}

SDL_RenderPresent(sdlRender);
}

void Quit(int code)
{
const char *errMsg = SDL_GetError();
if (errMsg && strlen(errMsg)) {
SDL_Log("Error : %s\n", errMsg);
}

//销毁窗口、渲染器、纹理
if (sdlWindow) SDL_DestroyWindow(sdlWindow);
if (sdlRender) SDL_DestroyRenderer(sdlRender);
if (sdlTexture) SDL_DestroyTexture(sdlTexture);
SDL_Quit();
exit(code);
}

void HandleKeyEvent(const SDL_Keysym* keysym)
{
int key = keysym->sym;
switch(key)
{
case SDLK_ESCAPE:
Quit(0);
break;
case SDLK_SPACE:
break;
case SDLK_UP:
case SDLK_DOWN:
case SDLK_LEFT:
case SDLK_RIGHT:
int x, y;
SDL_GetWindowPosition(sdlWindow, &x, &y);
x = (key == SDLK_LEFT ? x-2 : (key == SDLK_RIGHT ? x+2 : x));
y = (key == SDLK_UP ? y-2 : (key == SDLK_DOWN ? y+2 : y));
SDL_SetWindowPosition(sdlWindow, x, y);
SDL_Log("x=%d, y=%d\n", x, y);
break;
case SDLK_KP_PLUS:
case SDLK_KP_MINUS:
w = (key == SDLK_KP_PLUS ? w+2 : w-2);
h = (key == SDLK_KP_PLUS ? h+2 : h-2);
SDL_SetWindowSize(sdlWindow, w, h);
SDL_Log("w=%d, h=%d\n", w, h);
break;
default:
break;
}
}

void HandleEvents()
{
//Our SDL event placeholder.
SDL_Event event;
//Grab all the events off the queue.
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_KEYDOWN:
//Handle key Event
HandleKeyEvent(&event.key.keysym);
break;
case SDL_QUIT:
//Handle quit requests (like Ctrl-c).
Quit(0);
break;
}
}
}

int main(int argc, char* argv[])
{
printf("可以通过↑↓←→+ -按键控制移动和大小\n");
if (InitView(w, h, "yp.ico") == false) {
SDL_Log("sdlWindow is null @_@\n");
Quit(0);
return -1;
}

char *imgName = "gril.jpg";
if (InitDraw(imgName) == false) {
SDL_Log("Init Fail @_@\n");
Quit(0);
return -1;
}

//配置客户区大小
SDL_QueryTexture(sdlTexture,NULL, NULL, &w, &h);
SDL_SetWindowSize(sdlWindow, w, h);
SDL_Log("w=%d, h=%d\n", w, h);

while (1) {
HandleEvents();
UpdateDraw();
}

SDL_DestroyWindow(sdlWindow);
SDL_Quit();
return 0;
}


结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: