📜 ⬆️ ⬇️

SDL 2 Tutorial: Lesson 6 - Primitives

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

In this lesson we will analyze the drawing of primitives. Let's start and welcome to the lesson

Primitives


First, we analyze what a primitive is. The graphic primitive is the simplest geometric object displayed on the display screen: a point, a straight 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 the code

#include <SDL2/SDL.h> #include <iostream> using namespace std; int SCREEN_WIDTH = 640; int SCREEN_HEIGHT = 480; SDL_Window *win = NULL; SDL_Renderer *ren = NULL; 

I will not describe the variable declaration, we go further.

 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 , we initialize SDL 2, we create a window, and the render here is also not delayed.

I don't write the Load function, since we don't need to load anything.

In the Quit function, we simply remove the window, render and de-initialize SDL 2.

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

I will skip the call to the Init function, let's move on to the most interesting. To draw a rectangle, we need to create an object of type SDL_Rect . Its coordinates and dimensions are needed for drawing.

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

This completes the theory about SDL_Rect, let 's start writing the 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 told Render to paint in black and fill the entire window with it. After that they said to paint in white. Next, create a rectangle with coordinates (10; 10) and dimensions 50x50. The SDL_RenderFillRect function draws a rectangle.

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

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

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

The next step is to draw the line.

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

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

Drawing points is almost the same as drawing lines. We call the SDL_RenderDrawPoint function and, more specifically , we render the render and coordinates of the point. But just draw a point is not interesting, let's write for , in which in 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.

At this drawing sank. It remains only to update the screen, put a pause time, exit and return 0 .

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

This concludes our lesson, here’s the full code:

 #include <SDL2/SDL.h> #include <iostream> 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, all the while!

<< Previous lesson

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


All Articles