285 lines
8.3 KiB
C++
285 lines
8.3 KiB
C++
#include "background.h"
|
|
|
|
// Constructor
|
|
Background::Background(SDL_Renderer *renderer, Screen *screen, Asset *asset, param_t *param)
|
|
{
|
|
// Copia los punteros
|
|
this->renderer = renderer;
|
|
this->screen = screen;
|
|
this->asset = asset;
|
|
this->param = param;
|
|
|
|
// Inicializa variables
|
|
gradientNumber = 0;
|
|
alpha = 0;
|
|
cloudsSpeed = 0;
|
|
transition = 0;
|
|
counter = 0;
|
|
rect = {0, 0, param->gameWidth, param->gameHeight};
|
|
srcRect = {playArea.x, rect.h - playArea.h, playArea.w, playArea.h};
|
|
dstRect = {0, 0, playArea.w, playArea.h};
|
|
base = rect.h;
|
|
color = {param->backgroundAttenuateColor.r, param->backgroundAttenuateColor.g, param->backgroundAttenuateColor.b};
|
|
alphaColorText = alphaColorTextTemp = param->backgroundAttenuateAlpha;
|
|
|
|
gradientRect[0] = {0, 0, rect.w, rect.h};
|
|
gradientRect[1] = {rect.w, 0, rect.w, rect.h};
|
|
gradientRect[2] = {0, rect.h, rect.w, rect.h};
|
|
gradientRect[3] = {rect.w, rect.h, rect.w, rect.h};
|
|
|
|
// Carga las texturas
|
|
buildingsTexture = new Texture(renderer, asset->get("game_buildings.png"));
|
|
clouds1Texture = new Texture(renderer, asset->get("game_clouds1.png"));
|
|
clouds2Texture = new Texture(renderer, asset->get("game_clouds2.png"));
|
|
grassTexture = new Texture(renderer, asset->get("game_grass.png"));
|
|
gradientsTexture = new Texture(renderer, asset->get("game_sky_colors.png"));
|
|
|
|
// Crea los sprites
|
|
const int clouds1y = base - 165;
|
|
const int clouds2y = base - 101;
|
|
const float clouds1speed = 0.1f;
|
|
const float clouds2speed = 0.05f;
|
|
clouds1A = new MovingSprite(0, clouds1y, rect.w, clouds1Texture->getHeight(), -clouds1speed, 0.0f, 0.0f, 0.0f, clouds1Texture);
|
|
clouds1B = new MovingSprite(rect.w, clouds1y, rect.w, clouds1Texture->getHeight(), -clouds1speed, 0.0f, 0.0f, 0.0f, clouds1Texture);
|
|
|
|
clouds2A = new MovingSprite(0, clouds2y, rect.w, clouds2Texture->getHeight(), -clouds2speed, 0.0f, 0.0f, 0.0f, clouds2Texture);
|
|
clouds2B = new MovingSprite(rect.w, clouds2y, rect.w, clouds2Texture->getHeight(), -clouds2speed, 0.0f, 0.0f, 0.0f, clouds2Texture);
|
|
|
|
buildingsSprite = new Sprite(0, 0, buildingsTexture->getWidth(), buildingsTexture->getHeight(), buildingsTexture);
|
|
gradientSprite = new Sprite(0, 0, rect.w, rect.h, gradientsTexture);
|
|
grassSprite = new Sprite(0, 0, grassTexture->getWidth(), grassTexture->getHeight() / 2, grassTexture);
|
|
|
|
// Inicializa objetos
|
|
clouds1A->setSpriteClip(0, 0, clouds1Texture->getWidth(), clouds1Texture->getHeight());
|
|
clouds1B->setSpriteClip(0, 0, clouds1Texture->getWidth(), clouds1Texture->getHeight());
|
|
clouds2A->setSpriteClip(0, 0, clouds2Texture->getWidth(), clouds2Texture->getHeight());
|
|
clouds2B->setSpriteClip(0, 0, clouds2Texture->getWidth(), clouds2Texture->getHeight());
|
|
buildingsSprite->setPosY(base - buildingsSprite->getHeight());
|
|
grassSprite->setPosY(base - grassSprite->getHeight());
|
|
|
|
// Crea la textura para componer el fondo
|
|
canvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, rect.w, rect.h);
|
|
SDL_SetTextureBlendMode(canvas, SDL_BLENDMODE_BLEND);
|
|
|
|
// Crea la textura para atenuar el fondo
|
|
colorTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, rect.w, rect.h);
|
|
SDL_SetTextureBlendMode(colorTexture, SDL_BLENDMODE_BLEND);
|
|
setColor(color);
|
|
SDL_SetTextureAlphaMod(colorTexture, alphaColorText);
|
|
}
|
|
|
|
// Destructor
|
|
Background::~Background()
|
|
{
|
|
delete buildingsTexture;
|
|
delete clouds1Texture;
|
|
delete clouds2Texture;
|
|
delete grassTexture;
|
|
delete gradientsTexture;
|
|
|
|
delete clouds1A;
|
|
delete clouds1B;
|
|
delete clouds2A;
|
|
delete clouds2B;
|
|
delete buildingsSprite;
|
|
delete gradientSprite;
|
|
delete grassSprite;
|
|
SDL_DestroyTexture(canvas);
|
|
SDL_DestroyTexture(colorTexture);
|
|
}
|
|
|
|
// Actualiza la lógica del objeto
|
|
void Background::update()
|
|
{
|
|
// Actualiza el valor de alpha
|
|
updateAlphaColorText();
|
|
|
|
// Actualiza las nubes
|
|
updateCLouds();
|
|
|
|
// Calcula el frame de la hierba
|
|
grassSprite->setSpriteClip(0, (10 * (counter / 20 % 2)), 320, 10);
|
|
|
|
// Calcula el valor de alpha
|
|
alpha = std::max((255 - (int)(255 * transition)), 0);
|
|
|
|
// Incrementa el contador
|
|
counter++;
|
|
|
|
fillCanvas();
|
|
}
|
|
|
|
// Compone todos los elementos del fondo en la textura
|
|
void Background::fillCanvas()
|
|
{
|
|
// Cambia el destino del renderizador
|
|
SDL_Texture *temp = SDL_GetRenderTarget(renderer);
|
|
SDL_SetRenderTarget(renderer, canvas);
|
|
|
|
// Dibuja el gradiente 2
|
|
gradientSprite->setSpriteClip(gradientRect[(gradientNumber + 1) % 4]);
|
|
gradientsTexture->setAlpha(255);
|
|
gradientSprite->render();
|
|
|
|
// Dibuja el gradiente 1 con una opacidad cada vez menor
|
|
gradientSprite->setSpriteClip(gradientRect[gradientNumber]);
|
|
gradientsTexture->setAlpha(alpha);
|
|
gradientSprite->render();
|
|
|
|
// Dibuja las nubes
|
|
clouds1A->render();
|
|
clouds1B->render();
|
|
clouds2A->render();
|
|
clouds2B->render();
|
|
|
|
// Dibuja los edificios
|
|
buildingsSprite->render();
|
|
|
|
// Dibuja la hierba
|
|
grassSprite->render();
|
|
|
|
// Deja el renderizador apuntando donde estaba
|
|
SDL_SetRenderTarget(renderer, temp);
|
|
}
|
|
|
|
// Dibuja el objeto
|
|
void Background::render()
|
|
{
|
|
// Fondo
|
|
SDL_RenderCopy(renderer, canvas, &srcRect, &dstRect);
|
|
|
|
// Atenuación
|
|
SDL_RenderCopy(renderer, colorTexture, &srcRect, &dstRect);
|
|
}
|
|
|
|
// Vuelve a cargar las texturas
|
|
void Background::reloadTextures()
|
|
{
|
|
buildingsTexture->reLoad();
|
|
clouds1Texture->reLoad();
|
|
clouds2Texture->reLoad();
|
|
grassTexture->reLoad();
|
|
gradientsTexture->reLoad();
|
|
}
|
|
|
|
// Ajusta el valor de la variable
|
|
void Background::setCloudsSpeed(float value)
|
|
{
|
|
cloudsSpeed = value;
|
|
}
|
|
|
|
// Ajusta el valor de la variable
|
|
void Background::setGradientNumber(int value)
|
|
{
|
|
gradientNumber = value % 4;
|
|
}
|
|
|
|
// Ajusta el valor de la variable
|
|
void Background::setTransition(float value)
|
|
{
|
|
value = std::min(value, 1.0f);
|
|
value = std::max(value, 0.0f);
|
|
transition = value;
|
|
}
|
|
|
|
// Establece la posición del objeto
|
|
void Background::setPos(SDL_Rect rect)
|
|
{
|
|
dstRect = rect;
|
|
|
|
// Si cambian las medidas del destino, hay que cambiar las del origen para evitar deformar la imagen
|
|
srcRect.w = rect.w;
|
|
srcRect.h = rect.h;
|
|
}
|
|
|
|
// Ajusta el valor de la variable
|
|
void Background::setSrcRect(SDL_Rect value)
|
|
{
|
|
srcRect = value;
|
|
}
|
|
|
|
// Ajusta el valor de la variable
|
|
void Background::setDstRect(SDL_Rect value)
|
|
{
|
|
dstRect = value;
|
|
}
|
|
|
|
// Establece el color de atenuación
|
|
void Background::setColor(color_t color)
|
|
{
|
|
this->color = color;
|
|
|
|
// Colorea la textura
|
|
SDL_Texture *temp = SDL_GetRenderTarget(renderer);
|
|
SDL_SetRenderTarget(renderer, colorTexture);
|
|
|
|
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 255);
|
|
SDL_RenderClear(renderer);
|
|
|
|
SDL_SetRenderTarget(renderer, temp);
|
|
}
|
|
|
|
// Establece la transparencia de la atenuación
|
|
void Background::setAlpha(int alpha)
|
|
{
|
|
// Evita que se asignen valores fuera de rango
|
|
alpha = std::min(alpha, 255);
|
|
alpha = std::max(alpha, 0);
|
|
|
|
// Guarda el valor actual
|
|
alphaColorTextTemp = alphaColorText;
|
|
|
|
// Establece el nuevo valor
|
|
alphaColorText = alpha;
|
|
}
|
|
|
|
// Actualiza el valor de alpha
|
|
void Background::updateAlphaColorText()
|
|
{
|
|
if (alphaColorText == alphaColorTextTemp)
|
|
{
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
alphaColorText > alphaColorTextTemp ? alphaColorTextTemp++ : alphaColorTextTemp--;
|
|
SDL_SetTextureAlphaMod(colorTexture, alphaColorTextTemp);
|
|
}
|
|
}
|
|
|
|
// Actualiza las nubes
|
|
void Background::updateCLouds()
|
|
{
|
|
// Aplica la velocidad calculada a las nubes
|
|
clouds1A->setVelX(cloudsSpeed);
|
|
clouds1B->setVelX(cloudsSpeed);
|
|
clouds2A->setVelX(cloudsSpeed / 2);
|
|
clouds2B->setVelX(cloudsSpeed / 2);
|
|
|
|
// Mueve las nubes
|
|
clouds1A->move();
|
|
clouds1B->move();
|
|
clouds2A->move();
|
|
clouds2B->move();
|
|
|
|
// Calcula el offset de las nubes
|
|
if (clouds1A->getPosX() < -clouds1A->getWidth())
|
|
{
|
|
clouds1A->setPosX(clouds1A->getWidth());
|
|
}
|
|
|
|
if (clouds1B->getPosX() < -clouds1B->getWidth())
|
|
{
|
|
clouds1B->setPosX(clouds1B->getWidth());
|
|
}
|
|
|
|
if (clouds2A->getPosX() < -clouds2A->getWidth())
|
|
{
|
|
clouds2A->setPosX(clouds2A->getWidth());
|
|
}
|
|
|
|
if (clouds2B->getPosX() < -clouds2B->getWidth())
|
|
{
|
|
clouds2B->setPosX(clouds2B->getWidth());
|
|
}
|
|
} |