英文版作者代码:https://github.com/Twinklebear/TwinklebearDev-Lessons
中文版翻译:http://adolfans.github.io/sdltutorialcn/blog/2013/01/26/lesson-1-hello-world/
觉得中文版翻译讲得有些繁琐,就按照自己的理解,整理如下:
代码:
#include <iostream> #include <SDL.h> int main(int, char**) { if (SDL_Init(SDL_INIT_VIDEO)) { std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl; return 1; } SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN); if (win == nullptr) { std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl; return 1; } SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (ren == nullptr) { SDL_DestroyWindow(win); std::cout << "SDL_CreateRender Error: " << SDL_GetError() << std::endl; SDL_Quit(); return 1; } std::string imagePath = "hello.bmp"; SDL_Surface *bmp = SDL_LoadBMP(imagePath.c_str()); if (bmp == nullptr) { SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl; SDL_Quit(); return 1; } SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp); SDL_FreeSurface(bmp); if (tex == nullptr) { SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl; SDL_Quit(); return 1; } for (int i = 0; i < 3; ++i) { SDL_RenderClear(ren); SDL_RenderCopy(ren, tex, NULL, NULL); SDL_RenderPresent(ren); SDL_Delay(1000); } SDL_DestroyTexture(tex); SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); SDL_Quit(); return 0; }
初始化:
SDL_Init(): 初始化SDL。
SDL_CreateWindow(): 创建窗口(Window)。
SDL_CreateRenderer(): 基于窗口创建渲染器(Render)。
SDL_CreateTexture(): 创建纹理(Texture)。
循环渲染数据:
SDL_UpdateTexture(): 设置纹理的数据。
SDL_RenderCopy(): 纹理复制给渲染器。
SDL_RenderPresent(): 显示。
具体参考:http://blog.csdn.net/leixiaohua1020/article/details/40680907