tutorial 1

声明:个人学习记录,参考需谨慎。
原网址:http://lazyfoo.net/tutorials/SDL/01_hello_SDL/index2.php
/This source code copyrighted by Lazy Foo' Productions (2004-2020)
and may not be redistributed without written permission.
/

//Using SDL and standard IO

include

include

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

int main( int argc, char* args[] )
{
//The window we'll be rendering to
SDL_Window* window = NULL;

//The surface contained by the window
SDL_Surface* screenSurface = NULL;

//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
    printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
else
{
    //Create window
    window = SDL_CreateWindow( "SDL Tutorial", 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
    {
        //Get window surface
        screenSurface = SDL_GetWindowSurface( window );

        //Fill the surface white
        SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) );

        //Update the surface
        SDL_UpdateWindowSurface( window );

        //Wait two seconds
        SDL_Delay( 2000 );
    }
}

//Destroy window
SDL_DestroyWindow( window );

//Quit SDL subsystems
SDL_Quit();

return 0;

}

总结:
SDL使用框架
1.SDL_Window 声明用于显示内容的窗口变量
2.SDL_Surface 表示显示在窗口中的2D图形的变量
3.SDL_Init 初始化SDL库
4.SDL_CreateWindow 实际创建窗口 (SDL_WINDOW_SHOWN:窗口创建时就显示)
5.SDL_GetWindowSurface 获取窗口对应surface,将需要显示的内容放到surface里 (这里使用SDL_FillRect将surface填充为白色, surfae中有内容,并不代表你能在屏幕上看到这些内容,如果需要看到surface中的内容,需要更新窗口。且SDL surface是使用CPU进行渲染)
6.SDL_UpdateWindowSurface 更新窗口,使屏幕显示surface中的内容。

你可能感兴趣的:(tutorial 1)