Tutoriales de SDL 2: Lección 7 - Viewport

Hola a todos! ¡Estoy de vuelta con una nueva lección para ti! Y si de repente te cansas de esperar una lección mía, siempre puedes encontrar estas lecciones en inglés aquí .

Bueno, comencemos la séptima lección con el título

Viewport


Viewport se traduce del inglés como un lugar de visualización, o en nuestro caso es un lugar de representación. La esencia es esta: tomamos y limitamos la superficie de dibujo no a toda la ventana, sino a alguna parte de ella. Esto puede ser útil, por ejemplo, para dibujar un minimapa en un juego.

Según la teoría, tengo todo, pasamos a la práctica.

#include <SDL2/SDL.h> #include <SDL2/SDL_image.h> #include <iostream> using namespace std; const int SCREEN_SIZE[2] = {640, 480}; SDL_Window *window = NULL; SDL_Renderer *ren = NULL; SDL_Texture *flower = NULL; 

Para empezar, como siempre declarando variables.

Dibujaremos esta flor.

imagen

init
 bool init() { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) { cout << "Can't init SDL: " << SDL_GetError() << endl; return false; } int flags = IMG_INIT_PNG; if (!(IMG_Init(flags)&flags)) { cout << "Can't init IMG: " << IMG_GetError() << endl; return false; } window = SDL_CreateWindow("VIEWPORT", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_SIZE[0], SCREEN_SIZE[1], SDL_WINDOW_SHOWN); if (window == NULL) { cout << "Can't create window: " << SDL_GetError() <<endl; return false; } ren = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (ren == NULL) { cout << "Can't create renderer: " << SDL_GetError() <<endl; return false; } return true; } 


cargar
 bool load() { SDL_Surface *temp_surf = NULL; temp_surf = IMG_Load("flower.png"); if (temp_surf == NULL) { cout << "Can't load image: " << IMG_GetError() << endl; return false; } flower = SDL_CreateTextureFromSurface(ren, temp_surf); if (flower == NULL) { cout << "Can't create texture from surface: " << SDL_GetError() << endl; SDL_FreeSurface(temp_surf); temp_surf = NULL; return false; } return true; } 


renunciar
 void quit() { SDL_DestroyWindow(window); window = NULL; SDL_DestroyRenderer(ren); ren = NULL; SDL_DestroyTexture(flower); flower = NULL; SDL_Quit(); IMG_Quit(); } 


No describiré las funciones de inicialización, carga de imágenes y inicialización, ya que son casi idénticas a las anteriores.

Y ahora para la parte divertida:

 int main() { if (!init()) { quit(); return 1; } if (!load()) { quit(); return 1; } SDL_Rect top_vp = {0, 0, SCREEN_SIZE[0], SCREEN_SIZE[1] / 2}; SDL_Rect bottom_vp = {0, SCREEN_SIZE[1] / 2, SCREEN_SIZE[0], SCREEN_SIZE[1] / 2}; 

Aquí conectamos un SDL , subimos una imagen y creamos dos rectángulos que están uno debajo del otro y ocupan todo el ancho de la ventana. Estos son nuestros viewport s.

  SDL_RenderSetViewport(ren, &top_vp); SDL_RenderCopy(ren, flower, NULL, NULL); SDL_RenderSetViewport(ren, &bottom_vp); SDL_RenderCopy(ren, flower, NULL, NULL); 

A continuación, ya estamos comenzando a dibujar. En primer lugar, debemos establecer el lugar del dibujo. Esto se realiza mediante la función SDL_RenderSetViewport , que toma dos parámetros: un render y un rectángulo en el que nos gustaría dibujar. Después de configurar el área, dibuje nuestra flor con el conocido método SDL_RenderCopy . Después de eso, repita lo mismo para el segundo rectángulo.

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

Luego actualizamos la ventana, esperamos 2 segundos y finalizamos el programa.

Después de compilar y ejecutar este programa, veremos dos flores aplanadas. Eso es todo lo que quería mostrar hoy.

Bueno, aquí hay una pequeña lección, si tiene alguna pregunta, me complacerá explicarlo, pero tengo todo. Adios

<< Lección anterior

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


All Articles