Lessons on SDL 2: Lesson 1 - Hello, SDL 2

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

I will skip the installation of SDL 2, and I will start right away by writing programs, so:

Your first window




Let's start by connecting SDL 2.

#include<SDL2/SDL.h>

Here we will declare several global variables.

constint SCREEN_WIDTH = 640;
constint SCREEN_HEIGHT = 480;

Next, open the familiar main function to everyone.

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

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

Flags
SDL_INIT_TIMER — подключение таймера;
SDL_INIT_AUDIO — подключение аудио;
SDL_INIT_VIDEO — подключение видео, автоматически подключаются события;
SDL_INIT_JOYSTICK — подключение управления джойстиком;
SDL_INIT_HAPTIC — тактильная подсистема (не знаю что это, если честно);
SDL_INIT_GAMECONTROLER — подключает управление, автоматически подключается джойстик;
SDL_INIT_EVENTS — подключает обработку событий;
SDL_INIT_EVERYTHING — подключает всё, выше перечисленное;
SDL_INIT_NOPARACHUTE — проверка на совместимость.

Next, create a window with the SDL_CreateWindow () function . 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) {
        return1;
    }

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

There are other flags for creating a window.

Flags
SDL_WINDOW_FULLSCREEN — открывает окно в полноэкранном режиме, изменяет разрешение рабочего стола;
SDL_WINDOW_FULLSCREEN_DESKTOP — открывает окно в полноэкранном режиме с разрешение данного рабочего стола;
SDL_WINDOW_OPENGL — окно с поддержкой OpenGL;
SDL_WINDOW_HIDDEN — окно скрыто;
SDL_WINDOW_BORDERLESS — окно без рамки;
SDL_WINDOW_RESIZABLE — можно изменять размер окна;
SDL_WINDOW_MINIMIZED — окно свернуто;
SDL_WINDOW_MAXIMIZED — окно развернуто;

If we compile and run the program now, we will see a 640x480 window, filled with white, which closes after a moment. Wonderful! To enjoy this miracle a bit, we can write SDL_Delay (2000) . It will make the program freeze for 2 seconds.

Just a window is, of course, good, but it would be even cooler to draw in it, okay? To do this, create a surface on which we will draw everything, because directly in the window, drawing is 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 SDL_GetWindowSurface () function . It was made so that it was not necessary to indicate the dimensions of the surface, its format, but immediately get the finished surface. Also, this surface does not need to be drawn in the window - it will do everything itself.

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

And in the end, we should update the window with the SDL_UpdateWindowSurface () function so that it displays what we told him.

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

After completing the program, we need to clear the RAM from unnecessary files, close the window, in general, determine the correct termination of the program. Specially prepared functions will help us with this.

    SDL_DestroyWindow(window);
    SDL_Quit();

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

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

#include<SDL2/SDL.h>constint SCREEN_WIDTH = 640;
constint SCREEN_HEIGHT = 480;
intmain(int argc, char ** args){
    if( SDL_Init( SDL_INIT_EVERYTHING ) != 0 )
    {
        return1;
    }
    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) {
        return1;
    }
    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();
    return0;
};

Good luck to all!

Next lesson >>

Also popular now: