sdl programming
Post on 30-Dec-2015
100 Views
Preview:
DESCRIPTION
TRANSCRIPT
2
Initialization The first thing you must do is initialize SDL
int SDL_Init( SDL_INIT_EVERYTHING ) This will return -1 if it fails Various flags can be OR’ed together to enable various
features In this case, we enable all features
3
Surfaces A surface is an area where you can draw The screen is one such surface You can create other surfaces to hold images
or to draw on You can copy one surface to another Surfaces have properties
Height Width Number of bits per pixel
4
Creating the Screen After initializing SDL, you create the screen
SDL_Surface *screen; screen = SDL_SetVideoMode(
winWidth, winHeight, screenDepth, SDL_HWSURFACE | SDL_DOUBLEBUF);
The screen depth is usually 32 If the screen cannot be created, NULL will be
returned When a surface is no longer needed it should
be freed SDL_FreeSurface(surface);
5
Setting a Window Title You can set the title in the top of the window
SDL_WM_SetCaption( “Title", NULL );
The title will be displayed in the window decoration area, usually at the top of the window
6
SDL Coordinates
(0, 0)(100, 0)
(0, 100) (100, 100)
•The origin of a window is the top-left corner•Y coordinates increase as we go down
7
Loading Images SDL can load images in the BMP format There are extensions which can load other
formats These extensions must be downloaded and
installed into your SDL directories Loaded images are stored on surfaces Remember, the screen itself is a surface You can then copy them from one surface to
another to make them appear on the screen
8
Loading Images To load an image
SDL_Surface *image = SDL_LoadBMP(“file.bmp”); This will either load the image or return NULL if it
cannot be loaded
The trouble is that the image might not have the same format as the screen you want to display it on
You can convert it to the correct format as follows SDL_Surface *displayImage =
SDL_DisplayFormat(image);
9
Loading Images These steps are done often enough to warrant
creating a function or method for loading an image
SDL_Surface* loadImage(const char* fileName) {SDL_Surface *tmp = NULL, *image = NULL;tmp = SDL_LoadBMP(fileName);if(tmp) {
image = SDL_DisplayFormat(tmp);SDL_FreeSurface(tmp);
}return image;
}
10
Rendering Images When you load an image
It is stored on a surface This does not make the image visible
The image must be copied to the screen to be visible
We move an image using bit blitting Bit block image transfer This is done with the function SDL_BlitSurface
11
SDL_BlitSurface int SDL_BlitSurface(
SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Src – source surface Srcrect – rectangle delimiting areas to copy or NULL
for entire image Dst – the destination surface Dstrect – position to place on destination surface. If
NULL image will be placed at top-left corner Returns 0 on success
12
SDL_Rect This is a common structure used to indicate
the size and position of rectangular areas
typedef struct{ Sint16 x, y; Uint16 w, h;
} SDL_Rect;
13
Event Handling Many events happen while a program is
running Key presses Mouse movement Window resizing Window closing
Games are interested in these events and need to be able to find out when they occur
This can be done using SDL_PollEvent(& event)
14
SDL_Event
typedef union{ Uint8 type; SDL_ActiveEvent active; SDL_KeyboardEvent key; SDL_MouseMotionEvent motion;
SDL_MouseButtonEvent button; SDL_JoyAxisEvent jaxis; SDL_JoyBallEvent jball; SDL_JoyHatEvent jhat; SDL_JoyButtonEvent jbutton; SDL_ResizeEvent resize; SDL_ExposeEvent expose; SDL_QuitEvent quit; SDL_UserEvent user; SDL_SysWMEvent syswm; } SDL_Event;
• This is used for all event types• Note that it is a union• The type field indicates which member of the union is to be used
15
SDL Event Types & StructuresEvent type Event Structure
SDL_ACTIVEEVENT SDL_ActiveEvent
SDL_KEYDOWN/UP SDL_KeyboardEvent
SDL_MOUSEMOTION SDL_MouseMotionEvent
SDL_MOUSEBUTTONDOWN/UP SDL_MouseButtonEvent
SDL_JOYAXISMOTION SDL_JoyAxisEvent
SDL_JOYBALLMOTION SDL_JoyBallEvent
SDL_JOYHATMOTION SDL_JoyHatEvent
SDL_JOYBUTTONDOWN/UP SDL_JoyButtonEvent
SDL_VIDEORESIZE SDL_ResizeEvent
SDL_VIDEOEXPOSE SDL_ExposeEvent
SDL_QUIT SDL_QuitEvent
SDL_USEREVENT SDL_UserEvent
SDL_SYSWMEVENT SDL_SysWMEvent
16
SDL_KeyboardEvent
typedef struct{ Uint8 type; Uint8 state; SDL_keysym keysym; } SDL_KeyboardEvent;
Field Values Description
type SDL_KEYDOWNSDL_KEYUP
Type of key event
State SDL_KEYPRESSEDSDL_KEYRELEASED
Same as type
keysym Structure The key pressed
17
SDL_keysym
typedef struct{ Uint8 scancode; SDLKey sym; SDLMod mod; Uint16 unicode; } SDL_keysym;
Field Values Description
scancode Hardware scan code
sym See table SDL virtual key code
mod Key modifiers
unicode Translated character
18
SDL Key SymbolsSDL key ASCII Name
SDLK_BACKSPACE '\b' backspac
e
SDLK_TAB '\t' tab
SDLK_CLEAR clear
SDLK_RETURN '\r' return
SDLK_PAUSE pause
SDLK_ESCAPE '^[' escape
SDLK_SPACE ' ' space
SDLK_EXCLAIM '!' exclamation
SDLK_QUOTEDBL '"' double
quote
SDLK_HASH '#' hash
SDLK_DOLLAR '$' dollar
SDLK_AMPERSAND '&' ampersa
nd
19
SDL Key SymbolsSDL key ASCII Name
SDLK_QUOTE '\'' single quote
SDLK_LEFTPAREN '(' left
parenthesis
SDLK_RIGHTPAREN ')' right
parenthesis
SDLK_ASTERISK '*' asterisk
SDLK_PLUS '+' plus sign
SDLK_COMMA ',' comma
SDLK_MINUS '-' minus sign
SDLK_PERIOD '.' period / full stop
SDLK_SLASH '/' forward slash
SDLK_COLON ':' colon
SDLK_SEMICOLON ';' semicolon
SDLK_LESS '<' less-than sign
20
SDL Key SymbolsSDL key ASCI
IName
SDLK_EQUALS '=' equals sign
SDLK_GREATER '>' greater-than sign
SDLK_QUESTION '?' question mark
SDLK_AT '@' at
SDLK_LEFTBRACKET '[' left bracket
SDLK_BACKSLASH '\\' backslash
SDLK_RIGHTBRACKET ']' right bracket
SDLK_CARET '^' caret
SDLK_UNDERSCORE '_' underscore
SDLK_DELETE '^?' delete
SDLK_UP up arrow
SDLK_DOWN down arrow
21
SDL Key SymbolsSDL key ASCII Name
SDLK_RIGHT right arrow
SDLK_LEFT left arrow
SDLK_INSERT insert
SDLK_HOME home
SDLK_END end
SDLK_PAGEUP page up
SDLK_PAGEDOWN page down
SDLK_F1 ... F1
SDLK_F15 F15
SDLK_PRINT print-screen
SDLK_SYSREQ SysRq
SDLK_BREAK break
22
SDL Key Symbols
SDL key ASCII Name
SDLK_0 '0' 0
SDLK_1 '1' 1
SDLK_2 '2' 2
SDLK_3 '3' 3
SDLK_4 '4' 4
SDLK_5 '5' 5
SDLK_6 '6' 6
SDLK_7 '7' 7
SDLK_8 '8' 8
SDLK_9 '9' 9
23
SDL Key SymbolsSDL key ASCII
SDLK_a 'a'
SDLK_b 'b'
SDLK_c 'c'
SDLK_d 'd'
SDLK_e 'e'
SDLK_f 'f'
SDLK_g 'g'
SDLK_h 'h'
SDLK_i 'i'
SDLK_j 'j'
SDLK_k 'k'
SDLK_l 'l'
SDLK_m 'm'
SDL key ASCII
SDLK_n 'n'
SDLK_o 'o'
SDLK_p 'p'
SDLK_q 'q'
SDLK_r 'r'
SDLK_s 's'
SDLK_t 't'
SDLK_u 'u'
SDLK_v 'v'
SDLK_w 'w'
SDLK_x 'x'
SDLK_y 'y'
SDLK_z 'z'
24
Event Processing Loop
while(SDL_PollEvent(&event)){ switch(event.type){ case SDL_KEYDOWN: if(event.key.keysym.sym==SDLK_LEFT) move_left(); break; . . . } }Poll for events and process each of
the events
25
The Game Loop
bool quit = false;SDL_Event event;
while( quit == false ) { if( SDL_PollEvent( &event ) ) { if( event.type == SDL_QUIT ) { quit = true; } if( SDL_Flip( screen ) == -1 ) {
//return 1; } }}
This processes eventsand keeps the windowon the screen untilthe X in the top rightof the window is clicked.
26
Drawing Text SDL does not support TTF fonts as distributed You can download the extension from
http://www.libsdl.org/projects/SDL_ttf/ Get the file
SDL_ttf-devel-2.0.10-VC.zip Open the zip file and
Copy the file in include to the include directory for your SDL
Copy the files in lib to the lib directory for your SDL
You will need to copy the new DLLs to any project that wants to use text
27
Preparing to Use Text Set the text color
SDL_Color textColor;textColor.r = textColor.g = textColor.b = 255;
Initialize the TTF extensionif( TTF_Init() == -1 ) { return false; }
Load the fontif( NULL == (font = TTF_OpenFont( "lazy.ttf", 28 )) )
{ return false; }
28
Rendering the Text text is rendered to a newly created surface You then copy this surface onto the surface
where the text should appear
textSurface = TTF_RenderText_Solid( font, "A-Maze-ing", textColor );
This surface is then blitted onto the destination surface
29
Colors Colors are specified as
RGB with each value from 0 – 255 There are two different structures
An unsigned 32 bit int An SDL_Color structure
SDL_Color has members r, g, b
To create the 32 bit intunsigned int color = SD_MapRGB(screen->format,
255, 255, 255);
top related