Hello! This is my second lesson on SDL 2. I still take the information
from here .
So, I welcome you to the lesson.
Main not rubber
In the last lesson, I arranged everything beautifully in the
Main () function, but for large programs this is not good. That is why the opportunity to write functions appeared. Now we will use it.
')
Let's start writing code with connecting the SDL and declaring several global variables.
#include <SDL2/SDL.h> const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; SDL_Window *win = NULL; SDL_Surface *scr = NULL; SDL_Surface *smile = NULL;
Here we are outside the
Main () function declared window objects (
win ), screen surfaces (
scr ) and the image of this
emoticon (
smile ).
Let's write a function to initialize SDL, create a window and drawing surface. It looks like this:
Init () int init() { if (SDL_Init(SDL_INIT_VIDEO) != 0) { return 1; } win = SDL_CreateWindow("Main ", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (win == NULL) { return 1; } scr = SDL_GetWindowSurface(win); return 0; }
This code should be familiar to you from the previous lesson. Here we initialized the video system, created a window with the name “Main not rubber”, unspecified coordinates and with previously specified dimensions, and also obtained the window surface, saving it in
scr . On error, this function returns 1, otherwise 0.
Now you can write a function to download the necessary media files. Here she is:
Load () int load() { smile = SDL_LoadBMP("smile.bmp"); if (smile == NULL) { return 1; } return 0; }
This feature is extremely simple. We load the .bmp image using the
SDL_LoadBMP function. This function takes the path to the image file (full or relative), and returns the object of the
SDL_Surface class. You should also check the correctness of the execution: if there were problems with loading (file not found, broken file), then the
SDL_LoadBMP function will return
NULL .
Also write a program to complete the application.
Quit () void quit() { SDL_FreeSurface(smile); smile = NULL; SDL_DestroyWindow(win); SDL_Quit(); }
Here is a new feature
SDL_FreeSurface , which simply cleans the surface. After assigning the variable
smile value
NULL . All other functions were discussed in the previous lesson.
This ended the writing of the functions and you can proceed to
Main () .
Main () int main (int argc, char ** args) { if (init() == 1) { return 1; } if (load() == 1) { return 1; } SDL_BlitSurface(smile, NULL, scr, NULL); SDL_UpdateWindowSurface(win); SDL_Delay(2000); quit(); return 0; };
Here we first call the function
init () , written earlier, then the
load () function, which we also wrote earlier. Next we display
smile in
scr , using the
SDL_BlitSurface function. This function accepts a surface for drawing, a rectangle that should be cut from this surface (it is necessary for animation), if you don’t need to cut anything, the
NULL is transferred, the surface onto which the rectangle is drawn, the coordinates of which are used for drawing. If the coordinates are zero, we pass
NULL .
It's time to talk about the coordinates. The SDL uses a different coordinate system, not the one everyone is used to. Here the point with coordinates (0, 0) is in the upper left corner. This is important to consider when displaying to avoid unpleasant situations.
Well, it was fascinating, we continue. Next, we update the window with the familiar function, make the program freeze for 2 seconds, call the
quit () function written by us and end the program. At the exit, we have a program that displays a cool smile for 2 seconds. All code looks like this:
#include <SDL2/SDL.h> const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; SDL_Window *win = NULL; SDL_Surface *scr = NULL; SDL_Surface *smile = NULL; int init() { if (SDL_Init(SDL_INIT_VIDEO) != 0) { return 1; } win = SDL_CreateWindow("Main ", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (win == NULL) { return 1; } scr = SDL_GetWindowSurface(win); return 0; } int load() { smile = SDL_LoadBMP("smile.bmp"); if (smile == NULL) { return 1; } return 0; } void quit() { SDL_FreeSurface(smile); SDL_DestroyWindow(win); SDL_Quit(); } int main (int argc, char ** args) { if (init() == 1) { return 1; } if (load() == 1) { return 1; } SDL_BlitSurface(smile, NULL, scr, NULL); SDL_UpdateWindowSurface(win); SDL_Delay(2000); quit(); return 0; };
I hope everything was clear. Good luck to all!