Intro

How Chapter 1 works?

  1. Init SDL with SDL_Init().
  2. Create window with SDL_CreateWindow().
  3. Get surface with SDL_GetWindowSurface().
    • do something with surface. (like SDL_FillRect()).
  4. Update surface with SDL_UpdateWindowSurface().
    • you can delay loop( for window lasting) with SDL_Delay().
  5. If you done all your jobs with SDL, Destroy window with SDL_DestroyWindow().
  6. Don’t forget calling SDL_Quit() for terminating all jobs!

SDL.h

  • main header file for sdl.
  • includes video, (will be updated)
  • when you setup SDL.h and other libraries, check
    • include (-I)
    • library (-L)
    • link (-l)
    • dll (windows)

main

  • main function type is fixed for cross-platform.
    1
    
    int main( int argc, char* args[] );
    

Function

SDL_Init()

1
int SDL_Init( Uint32 flags );
  • param
    • flags : subsystem initialization flags
  • return
    • 0 on success
    • negative integer as error code on failure

you need to call this function to use SDL functions.
input flags you’ll use as parameter.

SDL_GetError()

1
const char* SDL_GetError( void );
  • param
    • void
  • return
    • character array on message with information about error
    • empty array on no error happen

this function will return character array that represents latest error.

SDL_CreateWindow()

1
SDL_Window* SDL_CreateWindow( const char* title, int x, int y, int w, int h, Uint32 flags );
  • param
    • title : the title of the window. un UTF-8 encoding
    • x : the x position of the window. SDL_WINDOWPOS_CENTERED or SDL_WINDOWPOS_UNDEFINED
    • y : the y position of the window. SDL_WINDOWPOS_CENTERED or SDL_WINDOWPOS_UNDEFINED
    • w : the width of the window. in screen coordinates
    • h : the height of the window. in screen coordinates
    • flags : 0, or one or more SDL_WindowFlags OR’d together (?)
  • return
    • window object’s address on success
    • NULL on failure

every SDL_Video works on SDL_Window object.
this function will create and return abstract window that compatible with OS.

SDL_GetWindowSurface()

1
SDL_Surface* SDL_GetWindowSurface( SDL_Window* window );
  • param
    • window : the window to query
  • return
    • surface object that asociated with the window’s address on success
    • NULL on failure

after SDL_Window created, you can read and write SDL_Surface from/to SDL_Window.
this function will get address of SDL_Surface that asociated with window.

SDL_FillRect()

1
int SDL_FillRect( SDL_Surface* dst, const SDL_Rect* rect, Uint32 color );
  • param
    • dst : the SDL_Surface structure that is the drawing target
    • rect : the SDL_Rect structure representing the rectangle to fill, or NULL to fill the entire surface
    • color : the color to fill with (SDL_MapRGB() or SDL_MapRGBA())
  • return
    • 0 on success
    • negative integer as error code on failure

this function is one kind of draw function.
SDL_Rect for area, SDL_Surface as target, and SDL_MapRGB(A) will choose color.

SDL_MapRGB()

1
Uint32 SDL_MapRGB( const SDL_PixelFormat* format, Uint8 r, Uint8 g, Uint8 b );
  • param
    • format : an SDL_PixelFormat structure describing the pixel format
    • r : the red component of the pixel in the range 0~255
    • g : the green component of the pixel in the range 0~255
    • b : the blue component of the pixel range 0~255
  • return
    • pixel value on everytime

simple but cross-platform color returning function.

SDL_UpdateWindowSurface()

1
int SDL_UpdateWindowSurface( SDL_Window* window );
  • param
    • window : the window to update
  • return
    • 0 on success
    • negative integer as error code on failure

SDL_Window object will reload SDL_Surface asociated with it without changing it.

SDL_Delay()

1
void SDL_Delay( Uint32 ms );
  • param
    • ms : the number of milliseconds to delay
  • return
    • void

simple cross-platform sleep function.

SDL_DestroyWindow()

1
void SDL_DestroyWindow( SDL_Window* window );
  • param
    • window : the window to destroy
  • return
    • void

call this function when you’re done with that SDL_Window object.

SDL_Quit()

1
void SDL_Quit( void );
  • param
    • void
  • return
    • void

this function called after all SDL jobs are done.
SDL recommend to call this function essentially at end of program.


Struct

SDL_Window

SDL_Surface


Term

SDL_Init()

  • Uint32 flags
    • SDL_INIT_TIMER: timer subsystem
    • SDL_INIT_AUDIO: audio subsystem
    • SDL_INIT_VIDEO: video subsystem; automatically initializes the events subsystem
    • SDL_INIT_JOYSTICK: joystick subsystem; automatically initializes the events subsystem
    • SDL_INIT_HAPTIC: haptic (force feedback) subsystem
    • SDL_INIT_GAMECONTROLLER: controller subsystem; automatically initializes the joystick subsystem
    • SDL_INIT_EVENTS: events subsystem
    • SDL_INIT_EVERYTHING: all of the above subsystems
    • SDL_INIT_NOPARACHUTE: compatibility; this flag is ignored

SDL_CreateWindow()

  • int x,y
    • SDL_WINDOWPOS_CENTERED
    • SDL_WINDOWPOS_UNDEFINED
  • Uint32 flag
    • SDL_WINDOW_SHOWN : shown window
    • SDL_WINDOW_FULLSCREEN: fullscreen window
    • SDL_WINDOW_FULLSCREEN_DESKTOP: fullscreen window at desktop resolution
    • SDL_WINDOW_OPENGL: window usable with an OpenGL context
    • SDL_WINDOW_VULKAN: window usable with a Vulkan instance
    • SDL_WINDOW_METAL: window usable with a Metal instance
    • SDL_WINDOW_HIDDEN: window is not visible
    • SDL_WINDOW_BORDERLESS: no window decoration
    • SDL_WINDOW_RESIZABLE: window can be resized
    • SDL_WINDOW_MINIMIZED: window is minimized
    • SDL_WINDOW_MAXIMIZED: window is maximized
    • SDL_WINDOW_INPUT_GRABBED: window has grabbed input focus
    • SDL_WINDOW_ALLOW_HIGHDPI: window should be created in high-DPI mode if supported (>= SDL 2.0.1)