参照国内最容易搜到的中文教程,一些讲解就在注释里了
#include "SDL.h" #include <iostream> #include <Windows.h> int main(int argc,char**argv) { // 初始化SDL的“一切”,不成功main返回-1,本例均如此 if (SDL_Init(SDL_INIT_EVERYTHING) == -1) { std::cout << SDL_GetError() << std::endl; return 1; } // 创建窗口指针 SDL_Window *win = nullptr; win = SDL_CreateWindow("First SDL Program!!",100,100,800,600,SDL_WINDOW_SHOWN); if (win == nullptr) { std::cout << SDL_GetError() << std::endl; return 1; } // 创建SDL Renderer(渲染器)指针 SDL_Renderer *ren = nullptr; // 参数分别为:指定窗口,自动选择显卡驱动,使用图形加速|以显示器频率同步 ren = SDL_CreateRenderer(win,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (ren == nullptr) { std::cout << SDL_GetError() << std::endl; return 1; } // 创建SDL_Surface,用于载入图像 SDL_Surface *bmp = nullptr; bmp = SDL_LoadBMP("image.bmp"); if (bmp == nullptr) { std::cout << SDL_GetError() << std::endl; return -1; } // 创建SDL_Texture(材质) SDL_Texture *tex = nullptr; tex = SDL_CreateTextureFromSurface(ren,bmp); SDL_FreeSurface(bmp); // 清空屏幕,绘图,刷新屏幕 SDL_RenderClear(ren); SDL_RenderCopy(ren,tex,NULL,NULL); SDL_RenderPresent(ren); // 延迟4秒后关闭窗口 SDL_Delay(4000); // 释放内存 SDL_DestroyTexture(tex); SDL_DestroyRenderer(ren); SDL_DestroyWindow(win); // 退出 SDL_Quit(); return 0; }
补充:
窗口标题无法显示中文。