Translated from French to English - www.onlinedoctranslator.
com
                                                                  Introduction to SDL
                               Initializing the SDL
SDL is made up of subsystems:video, audio, timing… You can initialize some
components by specifying their names or initialize all the components using the
“EVERYTHING” flag
- - > Initializationis ensured thanks to the functionSDL_Initwhich returns 0 if the
initialization was successful.
                                          before retrieving the number        before playing
Example :            before displaying
                                         of seconds since initialization         a sound
                       a picture
                                                     of SDL
SDL_Quit(): allows you to release resources used by already initialized SDL
subsystems.
                              Creating a window
SDL_SetVideoMode(width, height, number of displayable colors, flags);
allows you to create a window whose width, height and resolution (in pixels) are changed to
settings.
                                                              Flags
Example :
SDL_SetVideoMode (400,400,32, SDL_HWSURFACE | SDL_DOUBLEBUF);
                                     Resolution Examples
                 SDL_SetVideoMode flags
SDL_SWSURFACE:Saving data in RAM
SDL_HWSURFACE:Saving data in the graphics card's video memory (VRAM)
SDL_RESIZABLE:Enlarge/shrink a window
SDL_DOUBLEBUf:technique allowing smooth movement of objects
                        Show an image
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;
❖ Loading an image into a surface
 image=IMG_Load(filename); // for any type of images image=
 SDL_LoadBMP(filename);// only for bitmap images
                  Scale / Mark in an SDL Surface
Whether the surface is a screen or an image an SDL mark is represented as follows:
                  (0,0)
                                                             X
                    Y
                          The SDL_Rect type
The typeSDL_Rectdefines a rectangular area. It is used by SDL_BlitSurface()
to define paste regions.
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)
                                                                                            The destination location
srcrect: the value of this field is:
- NULL: if you want to display the entire image
- address of an SDL_Rect type variable: if you want to display part of the image. dstrect:the
value of this field is equal to the address of a pos position declared and initialized previously.
❖ 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
                         position of the image
                         relative to the screen
                         what part of the image
                         should be displayed
           image.h                                image.c       9
Example: display            e of
Neymar
                   main.c
                                   10
Example: display the background image and the second image of
Neymar
                         position of the image
                         relative to the screen
                         what part of the image
                         should be displayed
           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();
2. Loading a font and initializing the size
   TTF_Font *font =TTF_OpenFont(font file, Size);
  Example :
   font = TTF_OpenFont("times.ttf", 65);
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
● If you want to use
  example ofThis site .
                                     Show text
3. Writing methods:
    TTF_RenderText_Solid;        if you have text that changes often.
    TTF_RenderText_Shaded;if your text doesn't change very often and you want to blister your text on a plain background.
    TTF_RenderText_Blended;if your text doesn't change very often but you want to blister on a non-solid
    background (like an image).
Example of writing text in Blended:
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 );
4. Bliter the surface:SDL_BlitSurface (text, NULL, screen, &positionScreen);
5. Close the font:TTF_CloseFont(font);
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
intMix_OpenAudio(intfrequency, Uint16format, intchannels, intchunk size);
                                                               Number of sound channels
2. Loading music:
Mix_Music *music;
music =Mix_LoadMUS("music.mp3");
3. Play the music
                                                   Number of repetitions (-1 infinity)
intMix_PlayMusic(Mix_Music *music, int loops);
4. Liberation of music
Mix_FreeMusic(music);
                                                                                                  19
           Play continuous sound
Example:
                                   20
                        Play a short sound
1. Loading sound effects:
Mix_Chunk*her;
sound=Mix_LoadWAV( "effect.wav" );
2. Play sound effectThe channelThe Mix_Chunk to play
Mix_PlayChannel(-1, sound, 0);
                                        Number of sound repetitions
3. Freeing sound effects
Mix_FreeChunk( her);
                                                                      21
                      Events in SDL
An event:a “signal” sent by a device to your application.
  • Press a key on the keyboard
  • Mouse click
  • Move the mouse
  • Minimize/close window
                                                            22
                         Events in SDL
To read an event you must:
1. declare a variable of type SDL_Event
2. Read the event with one of these functions:
● SDL_Pollevent: reads the event without blocking the game loop
● SDL_waitEvent: remains waiting until listening to an event
       Note: if you have automatic processing that does not depend on the user
       such as background animation, enemy movement, etc.,
       you better use the first function: SDL_PollEvent
                                                                                 23
                       Events in SDL
➢ Consider the following code to read an event:
                                 SDL_Event event;
              SDL_PollEvent(&event); // to call in the game loop
➢ To detect a keyboard key press, simply compare the event type with:
                            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
➢ To detect a mouse movement, simply compare the event type with:
                               if(event.type==SDL_MOUSEMOTION)
➢ To detect a mouse click, simply compare the event type with:
          if(event.type==SDL_MOUSEBUTTONUP) //or SDL_MOUSEBUTTONDOWN
➢ To know where the mouse movement/click took place, you must test if the position of the mouse
   mouse is above the desired surface with coordinate (X,Y,W,H):
                     if(event.motion.x> =X && event.motion.x<=X+W &&
                         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
Implementation of a game menu according to specifications.
 Noticed : When compiling code that uses SDL library
functions, you must add the following options in the
linking step:
- lSDL
- lSDL_ttf           Example :
- lSDL_image
- lSDL_mixer
                               References
● SDL documentation:https://www.libsdl.org , consulted on
  01/26/2022