SDL 2 Lessons: Lesson 6 - Primitives

    Hello everyone, this is the sixth lesson on SDL 2 and it came out small, but it is worth going. All lessons are here .

    In this lesson we will draw drawing of primitives. Let's get started and welcome to the lesson

    Primitives


    First, let’s see what a primitive is. A graphic primitive is the simplest geometric object displayed on the display screen: a point, a line segment, a rectangle, an arc, a circle, etc. In SDL 2, we can only draw points, rectangles, and lines.

    Let's get down to code

    #include 
    #include 
    using namespace std;
    int SCREEN_WIDTH = 640;
    int SCREEN_HEIGHT = 480;
    SDL_Window *win = NULL;
    SDL_Renderer *ren = NULL;
    

    I will not describe the declaration of variables, let's move on.

    bool init() {
        bool ok = true;
        if (SDL_Init(SDL_INIT_VIDEO) != 0) {
            cout << "Can't init SDL: " << SDL_GetError() << endl;
        }
        win = SDL_CreateWindow("Примитивы", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if (win == NULL) {
            cout << "Can't create window: " << SDL_GetError() << endl;
            ok = false;
        }
        ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
        if (ren == NULL) {
            cout << "Can't create renderer: " << SDL_GetError() << endl;
            ok = false;
        }
        return ok;
    }
    

    In Init, initialize SDL 2, create a window and render here, too, do not linger. I do not write the Load

    function , since we do not need to load anything. In the Quit function, we simply remove the window, render and uninitialize SDL 2.



    void quit() {
        SDL_DestroyWindow(win);
        win = NULL;
        SDL_DestroyRenderer(ren);
        ren = NULL;
        SDL_Quit;
    }
    

    I’ll skip the call to the Init function and move on to the most interesting part. In order to draw a rectangle, we need to create an object of type SDL_Rect . Its coordinates and dimensions are needed for drawing.

    A little about SDL_Rect : this data type is an array of 4 numbers: x , y , w and h - in that order. That is, this code: SDL_Rect rect = {1, 1, 1, 1}; - completely correct.

    This completes the theory about SDL_Rect, let 's start writing code.

        SDL_SetRenderDrawColor(ren, 0x00, 0x00, 0x00, 0x00);
        SDL_RenderClear(ren);
        SDL_SetRenderDrawColor(ren, 0xFF, 0xFF, 0xFF, 0xFF);
        SDL_Rect rect1 = {10, 10, 50, 50};
        SDL_RenderFillRect(ren, &rect1);
    

    Here we were told the render to paint in black and fill them with the whole window. After that they said to paint in white. Next, we created a rectangle with coordinates (10; 10) and dimensions 50x50. The SDL_RenderFillRect function draws a rectangle.

    Drawing only the outline of the rectangle is not much different.

        SDL_Rect rect2 = {70, 10, 50, 50};
        SDL_RenderDrawRect(ren, &rect2);
    

    We created a rectangle and called the SDL_RenderDrawRect function . She only draws the outline of the rectangle on the screen.

    The next step is to draw a line.

        SDL_RenderDrawLine(ren, 10, 70, 640 - 10, 70);
    

    A rectangle is not needed to draw a line. The SDL_RenderDrawLine function accepts render values ​​and four coordinates. These are the coordinates of the start point and end point. I decided to draw a horizontal line indented from the edges by 10 pixels.

    Drawing points is almost no different from drawing lines. We call the SDL_RenderDrawPoint function and pass in the render and the coordinates of the point. But just to draw a point is not interesting, let's write for better , in which through 3 pixels we will draw points.

        for (int i = 10; i <= 640-10; i +=4 ) {
            SDL_RenderDrawPoint(ren, i, 90);
        }
    

    We got a horizontal dotted line of dots.

    On this drawing has stopped. It remains only to update the screen, set the pause time, exit and return 0 .

        SDL_RenderPresent(ren);
        SDL_Delay(5000);
        quit();
        return 0;
    }
    

    This concludes our lesson, here is the complete code:

    #include 
    #include 
    using namespace std;
    int SCREEN_WIDTH = 640;
    int SCREEN_HEIGHT = 480;
    SDL_Window *win = NULL;
    SDL_Renderer *ren = NULL;
    bool init() {
        bool ok = true;
        if (SDL_Init(SDL_INIT_VIDEO) != 0) {
            cout << "Can't init SDL: " << SDL_GetError() << endl;
        }
        win = SDL_CreateWindow("Примитивы", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if (win == NULL) {
            cout << "Can't create window: " << SDL_GetError() << endl;
            ok = false;
        }
        ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
        if (ren == NULL) {
            cout << "Can't create renderer: " << SDL_GetError() << endl;
            ok = false;
        }
        return ok;
    }
    void quit() {
        SDL_DestroyWindow(win);
        win = NULL;
        SDL_DestroyRenderer(ren);
        ren = NULL;
        SDL_Quit;
    }
    int main (int arhc, char ** argv) {
        if (!init()) {
            quit();
            system("pause");
            return 1;
        }
        SDL_SetRenderDrawColor(ren, 0x00, 0x00, 0x00, 0x00);
        SDL_RenderClear(ren);
        SDL_SetRenderDrawColor(ren, 0xFF, 0xFF, 0xFF, 0xFF);
        SDL_Rect rect1 = {10, 10, 50, 50};
        SDL_RenderFillRect(ren, &rect1);
        SDL_Rect rect2 = {70, 10, 50, 50};
        SDL_RenderDrawRect(ren, &rect2);
        SDL_RenderDrawLine(ren, 10, 70, 640 - 10, 70);
        for (int i = 10; i <= 640-10; i +=4 ) {
            SDL_RenderDrawPoint(ren, i, 90);
        }
        SDL_RenderPresent(ren);
        SDL_Delay(5000);
        quit();
        return 0;
    }
    

    And I say goodbye to you, bye everyone!

    << Previous lesson

    Also popular now: