SDL 2 Lecciones: Lección 6 - Primitivas

Hola a todos, esta es la sexta lección sobre SDL 2 y salió pequeña, pero vale la pena ir. Todas las lecciones están aquí .

En esta lección dibujaremos un dibujo de primitivas. Comencemos y bienvenidos a la lección.

Primitivas


Primero, veamos qué es un primitivo. Una primitiva gráfica es el objeto geométrico más simple que se muestra en la pantalla: un punto, un segmento de línea, un rectángulo, un arco, un círculo, etc. En SDL 2, solo podemos dibujar puntos, rectángulos y líneas.

Bajemos al código

#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; 

No describiré la declaración de variables, sigamos adelante.

 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; } 

En Init, inicialice SDL 2, cree una ventana y renderice aquí, también, no se demore.

No escribo la función Cargar , ya que no necesitamos cargar nada.

En la función Salir , simplemente eliminamos la ventana, renderizamos y desinicializamos SDL 2.

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

Saltaré la llamada a la función Init y pasaré a la parte más interesante. Para dibujar un rectángulo, necesitamos crear un objeto de tipo SDL_Rect . Sus coordenadas y dimensiones son necesarias para dibujar.

Un poco sobre SDL_Rect : este tipo de datos es una matriz de 4 números: x , y , w y h , en ese orden. Es decir, este código: SDL_Rect rect = {1, 1, 1, 1}; - Completamente correcto.

Esto completa la teoría sobre SDL_Rect, comencemos a escribir código.

  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); 

Aquí nos dijeron que el renderizado pintara en negro y los llenara con toda la ventana. Después de eso dijeron que pintar en blanco. Luego, creamos un rectángulo con coordenadas (10; 10) y dimensiones 50x50. La función SDL_RenderFillRect dibuja un rectángulo.

Dibujar solo el contorno del rectángulo no es muy diferente.

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

Creamos un rectángulo y llamamos a la función SDL_RenderDrawRect . Ella solo dibuja el contorno del rectángulo en la pantalla.

El siguiente paso es dibujar una línea.

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

No se necesita un rectángulo para dibujar una línea. La función SDL_RenderDrawLine acepta valores de representación y cuatro coordenadas. Estas son las coordenadas del punto de inicio y el punto final. Decidí dibujar una línea horizontal con sangría de los bordes en 10 píxeles.

Los puntos de dibujo casi no son diferentes de las líneas de dibujo. Llamamos a la función SDL_RenderDrawPoint y pasamos el render y las coordenadas del punto. Pero solo para dibujar un punto no es interesante, escribamos para mejor, en el que a través de 3 píxeles dibujaremos puntos.

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

Tenemos una línea punteada horizontal de puntos.

En este dibujo se ha detenido. Solo resta actualizar la pantalla, establecer el tiempo de pausa, salir y devolver 0 .

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

Esto concluye nuestra lección, aquí está el código completo:

 #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; } 

Y les digo adiós, ¡adiós a todos!

<< Lección anterior || Próxima lección >>

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


All Articles