forked from jaildesigner-jailgames/jaildoctors_dilemma
Eliminado el fichero const.h
This commit is contained in:
@@ -1,63 +1,133 @@
|
||||
#include "screen.h"
|
||||
#include "const.h"
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
|
||||
// Constructor
|
||||
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer)
|
||||
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options)
|
||||
{
|
||||
// Inicializa variables
|
||||
mWindow = window;
|
||||
mRenderer = renderer;
|
||||
this->window = window;
|
||||
this->renderer = renderer;
|
||||
this->options = options;
|
||||
|
||||
mScreenWidth = SCREEN_WIDTH;
|
||||
mScreenHeight = SCREEN_HEIGHT;
|
||||
mGameCanvasWidth = GAMECANVAS_WIDTH;
|
||||
mGameCanvasHeight = GAMECANVAS_HEIGHT;
|
||||
mGameCanvasPosX = (SCREEN_WIDTH - GAMECANVAS_WIDTH) / 2;
|
||||
mGameCanvasPosY = (SCREEN_HEIGHT - GAMECANVAS_HEIGHT) / 2;
|
||||
gameCanvasWidth = SCREEN_WIDTH;
|
||||
gameCanvasHeight = SCREEN_HEIGHT;
|
||||
|
||||
mBorderColor = {0x27, 0x27, 0x36};
|
||||
// Establece el modo de video
|
||||
setVideoMode(options->fullScreenMode);
|
||||
|
||||
// Define el color del borde para el modo de pantalla completa
|
||||
borderColor = {0x27, 0x27, 0x36};
|
||||
borderColor = {0x00, 0x00, 0x00};
|
||||
|
||||
// Crea la textura donde se dibujan los graficos del juego
|
||||
mGameCanvas = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
|
||||
if (mGameCanvas == NULL)
|
||||
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()
|
||||
{
|
||||
mRenderer = nullptr;
|
||||
renderer = nullptr;
|
||||
}
|
||||
|
||||
// Limpia la pantalla
|
||||
void Screen::clean(color_t color)
|
||||
{
|
||||
SDL_SetRenderDrawColor(mRenderer, color.r, color.g, color.b, 0xFF);
|
||||
SDL_RenderClear(mRenderer);
|
||||
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(mRenderer, mGameCanvas);
|
||||
SDL_SetRenderTarget(renderer, gameCanvas);
|
||||
}
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
void Screen::blit()
|
||||
{
|
||||
// Vuelve a dejar el renderizador en modo normal
|
||||
SDL_SetRenderTarget(mRenderer, NULL);
|
||||
SDL_SetRenderTarget(renderer, 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, mGameCanvasHeight};
|
||||
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(mRenderer, mGameCanvas, NULL, &dest);
|
||||
SDL_RenderCopy(renderer, gameCanvas, NULL, &dest);
|
||||
|
||||
// Muestra por pantalla el renderizador
|
||||
SDL_RenderPresent(mRenderer);
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
// Establece el modo de video
|
||||
void Screen::setVideoMode(int fullScreenMode)
|
||||
{
|
||||
// Aplica el modo de video
|
||||
SDL_SetWindowFullscreen(window, fullScreenMode);
|
||||
|
||||
// Si está activo el modo ventana quita el borde
|
||||
if (fullScreenMode == 0)
|
||||
{
|
||||
screenWidth = gameCanvasWidth;
|
||||
screenHeight = gameCanvasHeight;
|
||||
dest = {0, 0, gameCanvasWidth, gameCanvasHeight};
|
||||
|
||||
// Modifica el tamaño del renderizador y de la ventana
|
||||
SDL_RenderSetLogicalSize(renderer, screenWidth, screenHeight);
|
||||
SDL_SetWindowSize(window, screenWidth * options->windowSize, screenHeight * options->windowSize);
|
||||
}
|
||||
|
||||
// Si está activo el modo de pantalla completa añade el borde
|
||||
if (fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
|
||||
{
|
||||
// Obten el alto y el ancho de la ventana
|
||||
SDL_GetWindowSize(window, &screenWidth, &screenHeight);
|
||||
|
||||
// Aplica el escalado al rectangulo donde se pinta la textura del juego
|
||||
if (options->integerScale)
|
||||
{
|
||||
// Calcula el tamaño de la escala máxima
|
||||
int scale = 0;
|
||||
while (((gameCanvasWidth * (scale + 1)) <= screenWidth) && ((gameCanvasHeight * (scale + 1)) <= screenHeight))
|
||||
{
|
||||
scale++;
|
||||
}
|
||||
|
||||
dest.w = gameCanvasWidth * scale;
|
||||
dest.h = gameCanvasHeight * scale;
|
||||
dest.x = (screenWidth - dest.w) / 2;
|
||||
dest.y = (screenHeight - dest.h) / 2;
|
||||
}
|
||||
else if (options->keepAspect)
|
||||
{
|
||||
float ratio = (float)gameCanvasWidth / (float)gameCanvasHeight;
|
||||
if ((screenWidth - gameCanvasWidth) >= (screenHeight - gameCanvasHeight))
|
||||
{
|
||||
dest.h = screenHeight;
|
||||
dest.w = (int)((screenHeight * ratio) + 0.5f);
|
||||
dest.x = (screenWidth - dest.w) / 2;
|
||||
dest.y = (screenHeight - dest.h) / 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
dest.w = screenWidth;
|
||||
dest.h = (int)((screenWidth / ratio) + 0.5f);
|
||||
dest.x = (screenWidth - dest.w) / 2;
|
||||
dest.y = (screenHeight - dest.h) / 2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dest.w = screenWidth;
|
||||
dest.h = screenHeight;
|
||||
dest.x = dest.y = 0;
|
||||
}
|
||||
|
||||
// Modifica el tamaño del renderizador
|
||||
SDL_RenderSetLogicalSize(renderer, screenWidth, screenHeight);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user