加载图片贴图,采用了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 = ;
int h = ; 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, -, );
if (sdlRender == NULL) return false;
SDL_SetRenderDrawColor(sdlRender, , , , ); //加载绘画图片
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 = ;
const int sqrtCount = (int)sqrt((double)count);
for (int i = ; i < sqrtCount; i++) {
srcRect = SDL_RectMake(, , (w-sqrtCount)/sqrtCount, (h-sqrtCount)/sqrtCount);
for (int j = ; j < sqrtCount; j++) {
srcRect.x = srcRect.w*j+(j?*j:);
srcRect.y = srcRect.h*i+(i?*i:); //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();
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- : (key == SDLK_RIGHT ? x+ : x));
y = (key == SDLK_UP ? y- : (key == SDLK_DOWN ? y+ : 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+ : w-);
h = (key == SDLK_KP_PLUS ? h+ : h-);
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();
break;
}
}
} int main(int argc, char* argv[])
{
printf("可以通过↑↓←→+ -按键控制移动和大小\n");
if (InitView(w, h, "yp.ico") == false) {
SDL_Log("sdlWindow is null @_@\n");
Quit();
return -;
} char *imgName = "gril.jpg";
if (InitDraw(imgName) == false) {
SDL_Log("Init Fail @_@\n");
Quit();
return -;
} //配置客户区大小
SDL_QueryTexture(sdlTexture,NULL, NULL, &w, &h);
SDL_SetWindowSize(sdlWindow, w, h);
SDL_Log("w=%d, h=%d\n", w, h); while () {
HandleEvents();
UpdateDraw();
} SDL_DestroyWindow(sdlWindow);
SDL_Quit();
return ;
}

结果:

SDL2.0的加载图片贴图-LMLPHP

05-02 19:35