SDL Initiation
SDL Initiation
com
Introduction to SDL
Initializing the SDL
Example :
SDL_SetVideoMode (400,400,32, SDL_HWSURFACE | SDL_DOUBLEBUF);
Resolution Examples
SDL_SetVideoMode flags
SDL_RESIZABLE:Enlarge/shrink a window
Ifyou manipulate .bmp images, you can make do with the bibSDL.h Otherwise,
you must include the library:SDL_image.h
# include <SDL/SDL_image.h>
❖ Declaration of a surface ²²
SDL_Surface *image;
(0,0)
X
Y
The SDL_Rect type
This is a predefined structure in the SDL.h library. This structure is made up of the
following fields:
- x: the abscissa
- y: the ordinate
- w: width
- h: height
Show an image
❖ To paste an image on a screen we use the function:
The source surface to copy The region The destination surface: Screen
source
SDL_BlitSurface(SDL_Surface* src, const SDL_Rect* srcrect, SDL_Surface* dst, SDL_Rect* dstrect)
❖ Screen update:
Screen refresh involves refreshing the screen after making multiple pastes
SDL_Flip(SDL_Surface *screen);
Example: display the background image and the second image of
Neymar
image.h image.c 9
Example: display e of
Neymar
main.c
10
Example: display the background image and the second image of
Neymar
image.h image.c 11
Example: display the background image and the second image of
Neymar
main.c
12
Show text
A text is a surface designed from a character string, a color, a font and a size.
To create a text, you must include the library<SDL/SDL_ttf.h>and follow the following
steps:
1.Initializing SDL_ttf:TTF_Init();
1. color initialization
SDL_Color textColor=(R,G,B); the value of R, J, B must be between 0 and
255 For more information on color codes consultthis palette .
Display text: example of color codes
Display text: Fonts
● On Ubuntu, you already have several fonts (files with .ttf extension)
under this directory: /usr/share/fonts/truetype
TTF_RenderText_Blended;if your text doesn't change very often but you want to blister on a non-solid
background (like an image).
SDL_Surface *text = NULL; Loading the font The message to be displayed Thisorol of the message
SDL_Color black color={0, 0, 0};
text=TTF_RenderText_Blended(police , " Message " , black color );
6. Stopping SDL_ttf:TTF_Quit();
Example: display the text Neymar in yellow at pos(300, 50)
text.h text.c 17
Example: display 00, 50)
main.c
18
Play continuous sound
SDL_mixer:
•Sound mixing library.
•Allows audio management.
1. Initialize SDL_mixer audio functions:
output lozn
Efcohrm frequencyanattildlo'éncnhaagnet(iH) Bytes/sample
4. Liberation of music
Mix_FreeMusic(music);
19
Play continuous sound
Example:
20
Play a short sound
21
Events in SDL
22
Events in SDL
23
Events in SDL
if(event.type==SDL_KEYDOWN)
➢ To know which key was pressed, you must make this comparison:
if(event.key.keysym.sym == key_name)
Example :if(event.key.keysym.sym ==SDLK_ESCAPE)
24
SDL KEY CODE
Events in SDL
if(event.type==SDL_MOUSEMOTION)
event.motion.y>=Y&& event.motion.y<=Y+H)
26
Events in SDL
Example: Declare an SDL_Event type variable
Reading events
Returns 1 if there is an event
0 otherwise
27
Work to do
- lSDL_ttf Example :
- lSDL_image
- lSDL_mixer
References