Download presentation
Presentation is loading. Please wait.
Published byHeather Chapman Modified over 9 years ago
1
SDL Programming Introduction
2
Initialization 2 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 3 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 4 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 5 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 6 (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 The origin of a window is the top-left corner Y coordinates increase as we go down
7
Loading Images 7 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 8 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 9 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 10 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 11 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 12 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 13 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 14 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 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 & Structures 15 Event typeEvent Structure SDL_ACTIVEEVENTSDL_ActiveEvent SDL_KEYDOWN/UPSDL_KeyboardEvent SDL_MOUSEMOTIONSDL_MouseMotionEvent SDL_MOUSEBUTTONDOWN/UPSDL_MouseButtonEvent SDL_JOYAXISMOTIONSDL_JoyAxisEvent SDL_JOYBALLMOTIONSDL_JoyBallEvent SDL_JOYHATMOTIONSDL_JoyHatEvent SDL_JOYBUTTONDOWN/UPSDL_JoyButtonEvent SDL_VIDEORESIZESDL_ResizeEvent SDL_VIDEOEXPOSESDL_ExposeEvent SDL_QUITSDL_QuitEvent SDL_USEREVENTSDL_UserEvent SDL_SYSWMEVENTSDL_SysWMEvent
16
SDL_KeyboardEvent 16 typedef struct{ Uint8 type; Uint8 state; SDL_keysym keysym; } SDL_KeyboardEvent; typedef struct{ Uint8 type; Uint8 state; SDL_keysym keysym; } SDL_KeyboardEvent; FieldValuesDescription typeSDL_KEYDOWN SDL_KEYUP Type of key event StateSDL_KEYPRESSED SDL_KEYRELEASED Same as type keysymStructureThe key pressed
17
SDL_keysym 17 typedef struct{ Uint8 scancode; SDLKey sym; SDLMod mod; Uint16 unicode; } SDL_keysym; FieldValuesDescription scancodeHardware scan code symSee tableSDL virtual key code modKey modifiers unicodeTranslated character
18
SDL Key Symbols 18 SDL keyASCIIName SDLK_BACKSPACE'\b'backspace SDLK_TAB'\t'tab SDLK_CLEARclear SDLK_RETURN'\r'return SDLK_PAUSEpause SDLK_ESCAPE'^['escape SDLK_SPACE' space SDLK_EXCLAIM'!'exclamation SDLK_QUOTEDBL'"' double quote SDLK_HASH'#'hash SDLK_DOLLAR'$'dollar SDLK_AMPERSAND'&'ampersand
19
SDL Key Symbols 19 SDL keyASCIIName SDLK_QUOTE'\''single quote SDLK_LEFTPAREN'('left parenthesis SDLK_RIGHTPARE N ')'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 Symbols 20 SDL keyASCI I Name SDLK_EQUALS'='equals sign SDLK_GREATER'>'greater-than sign SDLK_QUESTION'?'question mark SDLK_AT'@'at SDLK_LEFTBRACKET'['left bracket SDLK_BACKSLASH'\\'backslash SDLK_RIGHTBRACKE T ']'right bracket SDLK_CARET'^'caret SDLK_UNDERSCORE'_'underscore SDLK_DELETE'^?'delete SDLK_UPup arrow SDLK_DOWNdown arrow
21
SDL Key Symbols 21 SDL keyASCIIName SDLK_RIGHTright arrow SDLK_LEFTleft arrow SDLK_INSERTinsert SDLK_HOMEhome SDLK_ENDend SDLK_PAGEUPpage up SDLK_PAGEDOWNpage down SDLK_F1...F1 SDLK_F15F15 SDLK_PRINTprint-screen SDLK_SYSREQSysRq SDLK_BREAKbreak
22
SDL Key Symbols 22 SDL keyASCIIName 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 Symbols 23 SDL keyASCII 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 keyASCII 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 24 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 25 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; } 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 events and keeps the window on the screen until the X in the top right of the window is clicked.
26
Drawing Text 26 SDL does not support TTF fonts as distributed You can download the extension from http://www.libsdl.org/projects/SDL_ttf/ http://www.libsdl.org/projects/SDL_ttf/ Get the file SDL_ttf-devel-2.0.10-VC.zip 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 27 Set the text color SDL_Color textColor; textColor.r = textColor.g = textColor.b = 255; Initialize the TTF extension if( TTF_Init() == -1 ) { return false; } Load the font if( NULL == (font = TTF_OpenFont( "lazy.ttf", 28 )) ) { return false; }
28
Rendering the Text 28 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 29 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 int unsigned int color = SD_MapRGB(screen->format, 255, 255, 255);
30
Drawing Lines & Rectangles 30 int SDL_DrawLine(SDL_Surface* dst, int x1, int y1, int x2, int y2, Uint32 color) int SDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color) Both functions return 0 on success
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.