SDL basics

SDLÀ» ÃʱâÈ­ÇÏ°í À̹ÌÁö¸¦ ¶ç¿öº»´Ù.

http://lazyfoo.net/tutorials/SDL/03_event_driven_programming/index.php

À§ÀÇ ¸µÅ©¸¦ Á¤¸®Çß´Ù.

½ÇÇà È­¸éÀº ´ÙÀ½°ú °°´Ù.



Áß¿ä ½ÇÇà ÄÚµå´Â ´ÙÀ½°ú °°Àº È帧ÀÌ´Ù.

init
loadMedia
while
SDL_PollEvent
SDL_BlitSurface
SDL_UpdateWindowSurface
close

Çì´õ ÆÄÀÏ°ú Àü¿ªº¯¼ö¸¦ ¼±¾ðÇÑ´Ù.

/*This source code copyrighted by Lazy Foo' Productions (2004-2020)
and may not be redistributed without written permission.*/

//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>

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

//Starts up SDL and creates window
bool init();

//Loads media
bool loadMedia();

//Frees media and shuts down SDL
void close();

//The window we'll be rendering to
SDL_Window* gWindow = NULL;
    
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;

//The image we will load and show on the screen
SDL_Surface* gXOut = NULL;

init : SDL ÃʱâÈ­ÇÏ°í À©µµ¿ì¸¦ ¸¸µç´Ù.
    - SDL_Init
    - SDL_CreateWindow
   - SDL_GetWindowSurface : ·ÎµùÇÑ À̹ÌÁö¸¦ ±×·ÁÁÖ±â À§ÇØ À©µµ¿ì Ç¥¸éÀ» ¾ò¾î¿Â´Ù.

bool init()
{
    //Initialization flag
    bool success = true;

    //Initialize SDL
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
        success = false;
    }
    else
    {
        //Create window
        gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
        if( gWindow == NULL )
        {
            printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
            success = false;
        }
        else
        {
            //Get window surface
            gScreenSurface = SDL_GetWindowSurface( gWindow );
        }
    }

    return success;
}

loadMedia : À̹ÌÁö¸¦ ·ÎµùÇÑ´Ù.
          - SDL_LoadBMP : À̹ÌÁö¸¦ ·ÎµùÇÏ°í Ç¥¸éÀ̹ÌÁö¸¦ gXOut¿¡ ÀúÀåÇÑ´Ù.

bool loadMedia()
{
    //Loading success flag
    bool success = true;

    //Load splash image
    gXOut = SDL_LoadBMP( "x.bmp" );
    if( gXOut == NULL )
    {
        printf( "Unable to load image %s! SDL Error: %s\n", "03_event_driven_programming/x.bmp", SDL_GetError() );
        success = false;
    }

    return success;
}

¾îÇø®ÄÉÀÌ¼Ç ·çÇÁ
     - quitÀÌ trueÀ̸é while ·çÇÁ¸¦ ºüÁ® ³ª°£´Ù.
     - SDL_PollEvent´Â ¸¶¿ì½º, Å°º¸µå, Á¶À̽ºÆ½ À̺¥Æ®¸¦ ÀÔ·Â ¹Þ´Â´Ù.
       À̺¥Æ®Å¥°¡ ºñ¾î ÀÖÀ¸¸é 0À» ¹ÝȯÇÑ´Ù. 
     - SDL_BlitSurface : ¼Ò½º Ç¥¸é¿¡¼­ ¸ñÀûÁö Ç¥¸éÀ¸·Î º¹»ç¸¦ ÇÑ´Ù.
                               ¿©±â¼­´Â À̹ÌÁö Ç¥¸éÀ» À©µµ¿ì Ç¥¸éÀ¸·Î º¹»çÇÑ´Ù.
     - SDL_UpdateWindowSurface : Ç¥¸é¿¡ ±×¸°°ÍÀ» È­¸é¿¡ ¾÷µ¥ÀÌÆ® ÇØÁØ´Ù.

            //Main loop flag
            bool quit = false;

            //Event handler
            SDL_Event e;

            //While application is running
            while( !quit )
            {
                //Handle events on queue
                while( SDL_PollEvent( &e ) != 0 )
                {
                    //User requests quit
                    if( e.type == SDL_QUIT )
                    {
                        quit = true;
                    }
                }

                //Apply the image
                SDL_BlitSurface( gXOut, NULL, gScreenSurface, NULL );
           
                //Update the surface
                SDL_UpdateWindowSurface( gWindow );
            }

Á¾·á ó¸®
       - SDL_FreeSurface : À̹ÌÁö Ç¥¸é gXOut ¸Þ¸ð¸®¸¦ ÇØÁ¦ ÇÑ´Ù.
       - SDL_DestroyWindow : À©µµ¿ì Ç¥¸éÀ» ÇØÁ¦ ÇÑ´Ù.
       - SDL_Quit : SDL ÇÏÀ§ ½Ã½ºÅÛÀ» Á¾·á ÇÑ´Ù.

void close()
{
    //Deallocate surface
    SDL_FreeSurface( gXOut );
    gXOut = NULL;

    //Destroy window
    SDL_DestroyWindow( gWindow );
    gWindow = NULL;

    //Quit SDL subsystems
    SDL_Quit();
}

ÄÚµå  : main.cpp