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; }
结果:
相关文章推荐
- Qt5.5 SDL2.0 鼠标事件,键盘事件,BMP图片加载
- SDL历程--课程设计之图片加载
- 使用SDL加载多张图片(二)
- ubuntu SDL加载图片
- SDL2.0图片渲染封装
- SDL入门学习之三.加载BMP图片,实现动画
- SDL 加载显示JPEG图片
- 零基础学习SDL开发之在Android使用SDL2.0加载字体
- OpenGL es2.0 加载纹理图片和CubMap
- sdl2.0使用SDL_RenderCopyEx进行图片旋转
- SDL游戏之路(七)--屏幕自适应+图片加载+配置文件
- [原]零基础学习SDL开发之在Android使用SDL2.0加载字体
- [原]零基础学习SDL开发之在Android使用SDL2.0渲染PNG图片
- Beginning SDL 2.0(4) YUV加载及渲染
- 解决Scaleform中使用AS2.0的UILoader不加载图片的问题
- SDL学习笔记二(图片的加载和显示)
- SDL图片加载2
- 零基础学习SDL开发之在Android使用SDL2.0加载字体
- SDL第一个程序:加载一张图片
- Vue 2.0 使用图片懒加载插件