📜 ⬆️ ⬇️

SDL 2 Tutorials: Lesson 1 - Hello, SDL 2

Hello! I decided to look at SDL 2, but I did not find anything sensible in Russian. I decided to write my own, feeding on inspiration from here .

I’ll skip the SDL 2 installation, but I’ll start right away with writing the programs, so:

Your first window


Let's start with connecting SDL 2.
')
#include <SDL2/SDL.h> 

Here we will declare several global variables.

 const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; 

Next, open the familiar main function to everyone.

 int main (int argc, char ** args) { if( SDL_Init( SDL_INIT_EVERYTHING ) != 0 ) { return 1; } SDL_Surface* screen_surface = NULL; SDL_Window* window = NULL; 

Let's take a look at the code. In the main command line parameters are accepted command line parameters, they must be taken. Next, we initialize SDL 2 with the SDL_Init () function. We passed SDL_INIT_EVERITHING into it, which means that we initialize all SDL modules (video, audio, etc.). There are a few more flags for SDL_Init:

Flags
SDL_INIT_TIMER - timer connection;
SDL_INIT_AUDIO - audio connection;
SDL_INIT_VIDEO - video connection, events are automatically connected;
SDL_INIT_JOYSTICK - connection of joystick control;
SDL_INIT_HAPTIC - tactile subsystem (I do not know what it is, to be honest);
SDL_INIT_GAMECONTROLER - connects the control, the joystick is automatically connected;
SDL_INIT_EVENTS - connects event handling;
SDL_INIT_EVERYTHING - connects everything listed above;
SDL_INIT_NOPARACHUTE - compatibility check.

Next, create a window with the function SDL_CreateWindow () . If something went wrong as intended, return 1.

  window = SDL_CreateWindow("Hello, SDL 2!",SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SDL_SCREEN_WIDTH, SDL_SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (window == NULL) { return 1; } 

The SDL_CreateWindow () function accepts values ​​for the window name, window position by OX, window position by OY, window width, window height, and flags. Now we will manage flag SDL_WINDOW_SHOWN .

There are other flags to create a window.

Flags
SDL_WINDOW_FULLSCREEN - opens a window in full screen mode, changes the resolution of the desktop;
SDL_WINDOW_FULLSCREEN_DESCKTOP - opens a window in full-screen mode with the resolution of the given desktop;
SDL_WINDOW_OPENGL - window with OpenGL support;
SDL_WINDOW_HIDDEN - the window is hidden;
SDL_WINDOW_BORDERLESS - window without frame;
SDL_WINDOW_RESIZABLE - you can change the size of the window;
SDL_WINDOW_MINIMIZED - the window is minimized;
SDL_WINDOW_MAXIMIZED - the window is maximized;

If we compile and run the program now, we will see a window, 640x480 in size, filled with white color, which closes after a moment. Wonderful! To admire this miracle a bit, we can write SDL_Delay (2000) . It will cause the program to hang for 2 seconds.

Just a window is, of course, good, but it would be even cooler to draw in it, agree? To do this, we will create a surface on which we will draw everything, because directly in the window we can draw something somehow ugly or something.

  screen_surface = SDL_GetWindowSurface(window); SDL_FillRect(screen_surface, NULL, SDL_MapRGB(screen_surface->format, 0, 255, 0)); SDL_UpdateWindowSurface(window); 

First we get the window surface with the function SDL_GetWindowSurface () . It is made in order that it was not necessary to specify the dimensions of the surface, its format, but to immediately get the finished surface. Also, this surface does not need to be drawn in the window - she will do everything herself.

Then we use SDL_FillRect () to draw our surface in green. We wrote SDL_MapRGB , thus telling the program that we would use RGB (Red Green Blue) to set the color.

And at the end we need to update the window with the function SDL_UpdateWindowSurface () so that it will display what we told him.

At this stage, at startup, the program will display a window filled with bright green color, which will not close for 2 seconds.

After the program is completed, we need to clear the RAM of unnecessary files, close the window, in general, to determine the correct completion of the program. Specially prepared functions will help us with it.

  SDL_DestroyWindow(window); SDL_Quit(); 

The SDL_DestroyWindow () function closes the window, and the SDL_Quit () function initializes all the modules that were previously connected with the SDL_Init () function.

Well, lesson number 1 came to an end, I hope everything was clear. This is the code I got at the end:

 #include <SDL2/SDL.h> const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; int main (int argc, char ** args) { if( SDL_Init( SDL_INIT_EVERYTHING ) != 0 ) { return 1; } SDL_Surface* screen_surface = NULL; SDL_Window* window = NULL; window = SDL_CreateWindow("Hello, SDL 2!",SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (window == NULL) { return 1; } screen_surface = SDL_GetWindowSurface(window); SDL_FillRect(screen_surface, NULL, SDL_MapRGB( screen_surface->format, 0, 255, 0)); SDL_UpdateWindowSurface(window); SDL_Delay(2000); SDL_DestroyWindow(window); SDL_Quit(); return 0; }; 

Good luck to all!

Source: https://habr.com/ru/post/453700/


All Articles