79 lines
2.4 KiB
C++
79 lines
2.4 KiB
C++
#include "screen.h"
|
|
#include "const.h"
|
|
|
|
// Constructor
|
|
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, int screenWidth, int screenHeight, bool integerScale)
|
|
{
|
|
// Inicializa variables
|
|
mWindow = window;
|
|
mRenderer = renderer;
|
|
|
|
mScreenWidth = screenWidth;
|
|
mScreenHeight = screenHeight;
|
|
mGameCanvasWidth = SCREEN_WIDTH;
|
|
mGameCanvasHeight = SCREEN_HEIGHT;
|
|
mGameCanvasPosX = (mScreenWidth - mGameCanvasWidth) / 2;
|
|
mGameCanvasPosY = (mScreenHeight - mGameCanvasHeight) / 2;
|
|
mIntegerScale = integerScale;
|
|
|
|
// Calcula el tamaño de la escala máxima
|
|
mScale = 0;
|
|
int width = mGameCanvasWidth;
|
|
int height = mGameCanvasHeight;
|
|
while (((width * (mScale + 1)) < mScreenWidth) && ((height * (mScale + 1)) < mScreenHeight))
|
|
{
|
|
mScale++;
|
|
}
|
|
|
|
width = width * mScale;
|
|
height = height * mScale;
|
|
mGameCanvasPosX = (mScreenWidth - width) / 2;
|
|
mGameCanvasPosY = (mScreenHeight - height) / 2;
|
|
|
|
mBorderColor = {0x27, 0x27, 0x36};
|
|
mBorderColor = {0x00, 0x00, 0x00};
|
|
|
|
// Crea la textura donde se dibujan los graficos del juego
|
|
mGameCanvas = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, mGameCanvasWidth, mGameCanvasHeight);
|
|
if (mGameCanvas == NULL)
|
|
printf("TitleSurface could not be created!\nSDL Error: %s\n", SDL_GetError());
|
|
}
|
|
|
|
// Destructor
|
|
Screen::~Screen()
|
|
{
|
|
mRenderer = nullptr;
|
|
}
|
|
|
|
// Limpia la pantalla
|
|
void Screen::clean(color_t color)
|
|
{
|
|
SDL_SetRenderDrawColor(mRenderer, color.r, color.g, color.b, 0xFF);
|
|
SDL_RenderClear(mRenderer);
|
|
}
|
|
|
|
// Prepara para empezar a dibujar en la textura de juego
|
|
void Screen::start()
|
|
{
|
|
SDL_SetRenderTarget(mRenderer, mGameCanvas);
|
|
}
|
|
|
|
// Vuelca el contenido del renderizador en pantalla
|
|
void Screen::blit()
|
|
{
|
|
// Vuelve a dejar el renderizador en modo normal
|
|
SDL_SetRenderTarget(mRenderer, NULL);
|
|
|
|
// Borra el contenido previo
|
|
SDL_SetRenderDrawColor(mRenderer, mBorderColor.r, mBorderColor.g, mBorderColor.b, 0xFF);
|
|
SDL_RenderClear(mRenderer);
|
|
|
|
// Rectangulo de destino donde se dibujarà la textura con el juego
|
|
SDL_Rect dest = {mGameCanvasPosX, mGameCanvasPosY, mGameCanvasWidth*mScale, mGameCanvasHeight*mScale};
|
|
|
|
// Copia la textura de juego en el renderizador en la posición adecuada
|
|
SDL_RenderCopy(mRenderer, mGameCanvas, NULL, &dest);
|
|
|
|
// Muestra por pantalla el renderizador
|
|
SDL_RenderPresent(mRenderer);
|
|
} |