SDL 2 Lessons: Lesson 2 - Main Non-Rubber
Hello! This is my second SDL 2 lesson. I still get information from here .
So, welcome to the lesson
In the last lesson, I nicely arranged everything 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.
We'll start writing code by connecting an SDL and declaring several global variables.
Here, outside the Main () function, we declared window objects ( win ), screen surfaces ( scr ), and the image of this emoticon ( smile ).
Let's write a function to initialize the SDL, create a window and surface for drawing. It looks like this:
This code should be familiar to you from the previous lesson. Here we initialized the video system, created a window with the name “Main is not rubber”, with undefined coordinates and with the previously set dimensions, and also got the surface of the window, saving it in scr . On error, this function will return 1, otherwise 0.
It is now possible to write a function to load the necessary media files. Here she is:
This function 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 an object of the SDL_Surface class . You should also check the correctness of execution: if there were problems with the download (file not found, broken file), then the SDL_LoadBMP function will return NULL .
We also write a program to complete the application.
A new SDL_FreeSurface function has appeared here , which simply cleans the surface. After we set the variable smile to NULL . All other functions were discussed in the previous lesson.
That’s where the writing of the functions is over and we can proceed to Main () .
Here we first call the init () function , written earlier, then the load () function , which we also wrote before. Next, display smile in scr , using the SDL_BlitSurface function . This function takes a surface for drawing, a rectangle that should be cut from this surface (this is necessary for animation), if nothing needs to be cut , NULL is transmitted , the surface on which the rectangle is drawn, whose coordinates are used for drawing. If the coordinates are zero, we pass NULL .
It's time to talk about the coordinates. 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, that was fun, we continue. Next, we update the window with an already familiar function, make the program freeze for 2 seconds, call the quit () function that we wrote, and end the program. At the output, we have a program that displays a cool smile for 2 seconds. The whole code looks like this:
I hope everything was clear. Good luck to all!
<< Previous lesson || Next lesson >>
So, welcome to the lesson
Main not rubber
In the last lesson, I nicely arranged everything 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.
We'll start writing code by connecting an SDL and declaring several global variables.
#include
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
SDL_Window *win = NULL;
SDL_Surface *scr = NULL;
SDL_Surface *smile = NULL;
Here, outside the Main () function, we declared window objects ( win ), screen surfaces ( scr ), and the image of this emoticon ( smile ).
Let's write a function to initialize the SDL, create a window and surface for drawing. 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 is not rubber”, with undefined coordinates and with the previously set dimensions, and also got the surface of the window, saving it in scr . On error, this function will return 1, otherwise 0.
It is now possible to write a function to load the necessary media files. Here she is:
Load ()
int load() {
smile = SDL_LoadBMP("smile.bmp");
if (smile == NULL) {
return 1;
}
return 0;
}
This function 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 an object of the SDL_Surface class . You should also check the correctness of execution: if there were problems with the download (file not found, broken file), then the SDL_LoadBMP function will return NULL .
We also write a program to complete the application.
Quit ()
void quit() {
SDL_FreeSurface(smile);
smile = NULL;
SDL_DestroyWindow(win);
SDL_Quit();
}
A new SDL_FreeSurface function has appeared here , which simply cleans the surface. After we set the variable smile to NULL . All other functions were discussed in the previous lesson.
That’s where the writing of the functions is over and we 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 init () function , written earlier, then the load () function , which we also wrote before. Next, display smile in scr , using the SDL_BlitSurface function . This function takes a surface for drawing, a rectangle that should be cut from this surface (this is necessary for animation), if nothing needs to be cut , NULL is transmitted , the surface on which the rectangle is drawn, whose coordinates are used for drawing. If the coordinates are zero, we pass NULL .
It's time to talk about the coordinates. 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, that was fun, we continue. Next, we update the window with an already familiar function, make the program freeze for 2 seconds, call the quit () function that we wrote, and end the program. At the output, we have a program that displays a cool smile for 2 seconds. The whole code looks like this:
#include
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!
<< Previous lesson || Next lesson >>