示例代码
#include <SDL2/SDL.h>
#include <cstdio>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main(int argc, char const *argv[])
{
SDL_Window *window = NULL;
// 用来保存窗口 surface
SDL_Surface *screenSurface = NULL;
// SDL 初始化, 传入的 flag 为我们所用到的功能, 可通过或运算实现多个功能
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
else
{
// 创建窗口, SDL_Window_Showed 确保窗口在创建时显示
window = SDL_CreateWindow("SDL 教程", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if(window == NULL)
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
}
else
{
// 获取窗口当前的 surface 句柄
screenSurface = SDL_GetWindowSurface( window );
// 通过 surface 改变窗口属性
SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0x00, 0xE0, 0xFF ) );
SDL_UpdateWindowSurface( window );
SDL_Delay( 2000 );
}
}
SDL_DestroyWindow( window );
SDL_Quit();
return 0;
}

网友评论