美文网首页
SDL 教程 01 : 创建SDL窗口

SDL 教程 01 : 创建SDL窗口

作者: wjundong | 来源:发表于2020-02-18 23:19 被阅读0次

示例代码

#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;
}
运行结果.png

相关文章

网友评论

      本文标题:SDL 教程 01 : 创建SDL窗口

      本文链接:https://www.haomeiwen.com/subject/teqofhtx.html