se va la luz
This commit is contained in:
@@ -1,133 +1,363 @@
|
||||
#include "screen.h"
|
||||
#include "const.h"
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
|
||||
// Constructor
|
||||
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options)
|
||||
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options, int gameInternalResX, int gameInternalResY)
|
||||
{
|
||||
// Inicializa variables
|
||||
mWindow = window;
|
||||
mRenderer = renderer;
|
||||
mOptions = options;
|
||||
this->window = window;
|
||||
this->renderer = renderer;
|
||||
this->options = options;
|
||||
|
||||
mGameCanvasWidth = SCREEN_WIDTH;
|
||||
mGameCanvasHeight = SCREEN_HEIGHT;
|
||||
gameCanvasWidth = gameInternalResX;
|
||||
gameCanvasHeight = gameInternalResY;
|
||||
|
||||
// Establece el modo de video
|
||||
setVideoMode(mOptions->fullScreenMode);
|
||||
iniFade();
|
||||
iniSpectrumFade();
|
||||
|
||||
// Define el color del borde para el modo de pantalla completa
|
||||
mBorderColor = {0x27, 0x27, 0x36};
|
||||
mBorderColor = {0x00, 0x00, 0x00};
|
||||
borderColor = {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 == nullptr)
|
||||
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());
|
||||
|
||||
// Establece el modo de video
|
||||
setVideoMode(options->fullScreenMode);
|
||||
|
||||
// Calcula los anclajes
|
||||
anchor.left = 0;
|
||||
anchor.right = gameCanvasWidth;
|
||||
anchor.center = gameCanvasWidth / 2;
|
||||
anchor.top = 0;
|
||||
anchor.bottom = gameCanvasHeight;
|
||||
anchor.middle = gameCanvasHeight / 2;
|
||||
}
|
||||
|
||||
// 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, nullptr);
|
||||
SDL_SetRenderTarget(renderer, NULL);
|
||||
|
||||
// Borra el contenido previo
|
||||
SDL_SetRenderDrawColor(mRenderer, mBorderColor.r, mBorderColor.g, mBorderColor.b, 0xFF);
|
||||
SDL_RenderClear(mRenderer);
|
||||
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, nullptr, &mDest);
|
||||
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(mWindow, fullScreenMode);
|
||||
SDL_SetWindowFullscreen(window, fullScreenMode);
|
||||
|
||||
// Si está activo el modo ventana quita el borde
|
||||
if (fullScreenMode == 0)
|
||||
{
|
||||
mScreenWidth = mGameCanvasWidth;
|
||||
mScreenHeight = mGameCanvasHeight;
|
||||
mDest = {0, 0, mGameCanvasWidth, mGameCanvasHeight};
|
||||
if (options->borderEnabled)
|
||||
{
|
||||
const int incWidth = gameCanvasWidth * options->borderSize;
|
||||
const int incHeight = gameCanvasHeight * options->borderSize;
|
||||
screenWidth = gameCanvasWidth + incWidth;
|
||||
screenHeight = gameCanvasHeight + incHeight;
|
||||
dest = {0 + (incWidth / 2), 0 + (incHeight / 2), gameCanvasWidth, gameCanvasHeight};
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
screenWidth = gameCanvasWidth;
|
||||
screenHeight = gameCanvasHeight;
|
||||
dest = {0, 0, gameCanvasWidth, gameCanvasHeight};
|
||||
}
|
||||
|
||||
// Modifica el tamaño del renderizador y de la ventana
|
||||
SDL_RenderSetLogicalSize(mRenderer, mScreenWidth, mScreenHeight);
|
||||
SDL_SetWindowSize(mWindow, mScreenWidth * mOptions->windowSize, mScreenHeight * mOptions->windowSize);
|
||||
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)
|
||||
else if (fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
|
||||
{
|
||||
// Obten el alto y el ancho de la ventana
|
||||
SDL_GetWindowSize(mWindow, &mScreenWidth, &mScreenHeight);
|
||||
SDL_GetWindowSize(window, &screenWidth, &screenHeight);
|
||||
|
||||
// Aplica el escalado al rectangulo donde se pinta la textura del juego
|
||||
if (mOptions->integerScale)
|
||||
if (options->integerScale)
|
||||
{
|
||||
// Calcula el tamaño de la escala máxima
|
||||
int scale = 0;
|
||||
while (((mGameCanvasWidth * (scale + 1)) <= mScreenWidth) && ((mGameCanvasHeight * (scale + 1)) <= mScreenHeight))
|
||||
while (((gameCanvasWidth * (scale + 1)) <= screenWidth) && ((gameCanvasHeight * (scale + 1)) <= screenHeight))
|
||||
{
|
||||
scale++;
|
||||
}
|
||||
|
||||
mDest.w = mGameCanvasWidth * scale;
|
||||
mDest.h = mGameCanvasHeight * scale;
|
||||
mDest.x = (mScreenWidth - mDest.w) / 2;
|
||||
mDest.y = (mScreenHeight - mDest.h) / 2;
|
||||
dest.w = gameCanvasWidth * scale;
|
||||
dest.h = gameCanvasHeight * scale;
|
||||
dest.x = (screenWidth - dest.w) / 2;
|
||||
dest.y = (screenHeight - dest.h) / 2;
|
||||
}
|
||||
else if (mOptions->keepAspect)
|
||||
else if (options->keepAspect)
|
||||
{
|
||||
float ratio = (float)mGameCanvasWidth / (float)mGameCanvasHeight;
|
||||
if ((mScreenWidth - mGameCanvasWidth) >= (mScreenHeight - mGameCanvasHeight))
|
||||
float ratio = (float)gameCanvasWidth / (float)gameCanvasHeight;
|
||||
if ((screenWidth - gameCanvasWidth) >= (screenHeight - gameCanvasHeight))
|
||||
{
|
||||
mDest.h = mScreenHeight;
|
||||
mDest.w = (int)((mScreenHeight * ratio) + 0.5f);
|
||||
mDest.x = (mScreenWidth - mDest.w) / 2;
|
||||
mDest.y = (mScreenHeight - mDest.h) / 2;
|
||||
dest.h = screenHeight;
|
||||
dest.w = (int)((screenHeight * ratio) + 0.5f);
|
||||
dest.x = (screenWidth - dest.w) / 2;
|
||||
dest.y = (screenHeight - dest.h) / 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
mDest.w = mScreenWidth;
|
||||
mDest.h = (int)((mScreenWidth / ratio) + 0.5f);
|
||||
mDest.x = (mScreenWidth - mDest.w) / 2;
|
||||
mDest.y = (mScreenHeight - mDest.h) / 2;
|
||||
dest.w = screenWidth;
|
||||
dest.h = (int)((screenWidth / ratio) + 0.5f);
|
||||
dest.x = (screenWidth - dest.w) / 2;
|
||||
dest.y = (screenHeight - dest.h) / 2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mDest.w = mScreenWidth;
|
||||
mDest.h = mScreenHeight;
|
||||
mDest.x = mDest.y = 0;
|
||||
dest.w = screenWidth;
|
||||
dest.h = screenHeight;
|
||||
dest.x = dest.y = 0;
|
||||
}
|
||||
|
||||
// Modifica el tamaño del renderizador
|
||||
SDL_RenderSetLogicalSize(mRenderer, mScreenWidth, mScreenHeight);
|
||||
SDL_RenderSetLogicalSize(renderer, screenWidth, screenHeight);
|
||||
}
|
||||
|
||||
// Actualiza el valor de la variable
|
||||
options->fullScreenMode = fullScreenMode;
|
||||
}
|
||||
|
||||
// Camibia entre pantalla completa y ventana
|
||||
void Screen::switchVideoMode()
|
||||
{
|
||||
if (options->fullScreenMode == 0)
|
||||
{
|
||||
options->fullScreenMode = SDL_WINDOW_FULLSCREEN_DESKTOP;
|
||||
}
|
||||
else
|
||||
{
|
||||
options->fullScreenMode = 0;
|
||||
}
|
||||
|
||||
setVideoMode(options->fullScreenMode);
|
||||
}
|
||||
|
||||
// Cambia el tamaño de la ventana
|
||||
void Screen::setWindowSize(int size)
|
||||
{
|
||||
options->windowSize = size;
|
||||
setVideoMode(0);
|
||||
}
|
||||
|
||||
// Cambia el color del borde
|
||||
void Screen::setBorderColor(color_t color)
|
||||
{
|
||||
borderColor = color;
|
||||
}
|
||||
|
||||
// Cambia el tipo de mezcla
|
||||
void Screen::setBlendMode(SDL_BlendMode blendMode)
|
||||
{
|
||||
SDL_SetRenderDrawBlendMode(renderer, blendMode);
|
||||
}
|
||||
|
||||
// Establece el tamaño del borde
|
||||
void Screen::setBorderSize(float s)
|
||||
{
|
||||
options->borderSize = s;
|
||||
}
|
||||
|
||||
// Establece si se ha de ver el borde en el modo ventana
|
||||
void Screen::setBorderEnabled(bool value)
|
||||
{
|
||||
options->borderEnabled = value;
|
||||
}
|
||||
|
||||
// Cambia entre borde visible y no visible
|
||||
void Screen::switchBorder()
|
||||
{
|
||||
options->borderEnabled = !options->borderEnabled;
|
||||
setVideoMode(0);
|
||||
}
|
||||
|
||||
// Activa el fade
|
||||
void Screen::setFade()
|
||||
{
|
||||
fade = true;
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado el fade
|
||||
bool Screen::fadeEnded()
|
||||
{
|
||||
if (fade || fadeCounter > 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Activa el spectrum fade
|
||||
void Screen::setspectrumFade()
|
||||
{
|
||||
spectrumFade = true;
|
||||
}
|
||||
|
||||
// Comprueba si ha terminado el spectrum fade
|
||||
bool Screen::spectrumFadeEnded()
|
||||
{
|
||||
if (spectrumFade || spectrumFadeCounter > 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Inicializa las variables para el fade
|
||||
void Screen::iniFade()
|
||||
{
|
||||
fade = false;
|
||||
fadeCounter = 0;
|
||||
fadeLenght = 200;
|
||||
}
|
||||
|
||||
// Actualiza el fade
|
||||
void Screen::updateFade()
|
||||
{
|
||||
if (!fade)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
fadeCounter++;
|
||||
if (fadeCounter > fadeLenght)
|
||||
{
|
||||
iniFade();
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja el fade
|
||||
void Screen::renderFade()
|
||||
{
|
||||
if (!fade)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const SDL_Rect rect = {0, 0, gameCanvasWidth, gameCanvasHeight};
|
||||
color_t color = {0, 0, 0};
|
||||
const float step = (float)fadeCounter / (float)fadeLenght;
|
||||
const int alpha = 0 + (255 - 0) * step;
|
||||
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, alpha);
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
}
|
||||
|
||||
// Inicializa las variables para el fade spectrum
|
||||
void Screen::iniSpectrumFade()
|
||||
{
|
||||
spectrumFade = false;
|
||||
spectrumFadeCounter = 0;
|
||||
spectrumFadeLenght = 50;
|
||||
|
||||
spectrumColor.clear();
|
||||
|
||||
color_t c;
|
||||
c = stringToColor("black");
|
||||
spectrumColor.push_back(c);
|
||||
|
||||
c = stringToColor("blue");
|
||||
spectrumColor.push_back(c);
|
||||
|
||||
c = stringToColor("red");
|
||||
spectrumColor.push_back(c);
|
||||
|
||||
c = stringToColor("magenta");
|
||||
spectrumColor.push_back(c);
|
||||
|
||||
c = stringToColor("green");
|
||||
spectrumColor.push_back(c);
|
||||
|
||||
c = stringToColor("cyan");
|
||||
spectrumColor.push_back(c);
|
||||
|
||||
c = stringToColor("yellow");
|
||||
spectrumColor.push_back(c);
|
||||
|
||||
c = stringToColor("bright_white");
|
||||
spectrumColor.push_back(c);
|
||||
}
|
||||
|
||||
// Actualiza el spectrum fade
|
||||
void Screen::updateSpectrumFade()
|
||||
{
|
||||
if (!spectrumFade)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
spectrumFadeCounter++;
|
||||
if (spectrumFadeCounter > spectrumFadeLenght)
|
||||
{
|
||||
iniSpectrumFade();
|
||||
SDL_SetTextureColorMod(gameCanvas, 255, 255, 255);
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja el spectrum fade
|
||||
void Screen::renderSpectrumFade()
|
||||
{
|
||||
if (!spectrumFade)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const float step = (float)spectrumFadeCounter / (float)spectrumFadeLenght;
|
||||
const int max = spectrumColor.size() - 1;
|
||||
const int index = max + (0 - max) * step;
|
||||
const color_t c = spectrumColor[index];
|
||||
SDL_SetTextureColorMod(gameCanvas, c.r, c.g, c.b);
|
||||
}
|
||||
|
||||
// Actualiza los efectos
|
||||
void Screen::updateFX()
|
||||
{
|
||||
updateFade();
|
||||
updateSpectrumFade();
|
||||
}
|
||||
|
||||
// Dibuja los efectos
|
||||
void Screen::renderFX()
|
||||
{
|
||||
renderFade();
|
||||
renderSpectrumFade();
|
||||
}
|
||||
Reference in New Issue
Block a user