59 lines
1.7 KiB
C++
59 lines
1.7 KiB
C++
#include "screen.h"
|
|
|
|
// Constructor
|
|
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, int scr_w, int scr_h, int gc_w, int gc_h)
|
|
{
|
|
// Inicializa variables
|
|
this->window = window;
|
|
this->renderer = renderer;
|
|
|
|
screenWidth = scr_w;
|
|
screenHeight = scr_h;
|
|
gameCanvasWidth = gc_w;
|
|
gameCanvasHeight = gc_h;
|
|
gameCanvasPosX = (scr_w - gc_w) / 2;
|
|
gameCanvasPosY = (scr_h - gc_h) / 2;
|
|
dest = {gameCanvasPosX, gameCanvasPosY, gameCanvasWidth, gameCanvasHeight};
|
|
borderColor = {0x27, 0x27, 0x36};
|
|
|
|
// Crea la textura donde se dibujan los graficos del juego
|
|
gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth, gameCanvasHeight);
|
|
if (gameCanvas == NULL)
|
|
printf("TitleSurface could not be created!\nSDL Error: %s\n", SDL_GetError());
|
|
}
|
|
|
|
// Destructor
|
|
Screen::~Screen()
|
|
{
|
|
renderer = nullptr;
|
|
}
|
|
|
|
// Limpia la pantalla
|
|
void Screen::clean(color_t color)
|
|
{
|
|
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 0xFF);
|
|
SDL_RenderClear(renderer);
|
|
}
|
|
|
|
// Prepara para empezar a dibujar en la textura de juego
|
|
void Screen::start()
|
|
{
|
|
SDL_SetRenderTarget(renderer, gameCanvas);
|
|
}
|
|
|
|
// Vuelca el contenido del renderizador en pantalla
|
|
void Screen::blit()
|
|
{
|
|
// Vuelve a dejar el renderizador en modo normal
|
|
SDL_SetRenderTarget(renderer, NULL);
|
|
|
|
// Borra el contenido previo
|
|
SDL_SetRenderDrawColor(renderer, borderColor.r, borderColor.g, borderColor.b, 0xFF);
|
|
SDL_RenderClear(renderer);
|
|
|
|
// Copia la textura de juego en el renderizador en la posición adecuada
|
|
SDL_RenderCopy(renderer, gameCanvas, NULL, &dest);
|
|
|
|
// Muestra por pantalla el renderizador
|
|
SDL_RenderPresent(renderer);
|
|
} |