Files
coffee_crisis/source/logo.cpp

159 lines
3.2 KiB
C++

#include "logo.h"
#define INIT_FADE 100
#define END_LOGO 200
// Constructor
Logo::Logo(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, section_t *section)
{
// Copia la dirección de los objetos
this->renderer = renderer;
this->screen = screen;
this->asset = asset;
this->input = input;
this->section = section;
// Reserva memoria para los punteros
eventHandler = new SDL_Event();
texture = new Texture(renderer, asset->get("logo.png"));
sprite = new Sprite(14, 75, 226, 44, texture, renderer);
// Inicializa variables
counter = 0;
section->name = SECTION_PROG_LOGO;
section->subsection = 0;
ticks = 0;
ticksSpeed = 15;
}
// Destructor
Logo::~Logo()
{
texture->unload();
delete texture;
delete sprite;
delete eventHandler;
}
// Comprueba si ha terminado el logo
void Logo::checkLogoEnd()
{
if (counter >= END_LOGO + 20)
{
section->name = SECTION_PROG_INTRO;
section->subsection = 0;
}
}
// Comprueba los eventos
void Logo::checkEvents()
{
// Comprueba los eventos que hay en la cola
while (SDL_PollEvent(eventHandler) != 0)
{
// Evento de salida de la aplicación
if (eventHandler->type == SDL_QUIT)
{
section->name = SECTION_PROG_QUIT;
break;
}
}
}
// Comprueba las entradas
void Logo::checkInput()
{
if (input->checkInput(input_exit, REPEAT_FALSE))
{
section->name = SECTION_PROG_QUIT;
}
else if (input->checkInput(input_window_fullscreen, REPEAT_FALSE))
{
screen->switchVideoMode();
}
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE))
{
screen->decWindowSize();
}
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE))
{
screen->incWindowSize();
}
else if (input->checkInput(input_pause, REPEAT_FALSE) || input->checkInput(input_accept, REPEAT_FALSE) || input->checkInput(input_fire_left, REPEAT_FALSE) || input->checkInput(input_fire_center, REPEAT_FALSE) || input->checkInput(input_fire_right, REPEAT_FALSE))
{
section->name = SECTION_PROG_TITLE;
section->subsection = SUBSECTION_TITLE_1;
}
}
// Dibuja el fade
void Logo::renderFade()
{
// Dibuja el fade
if (counter >= INIT_FADE)
{
const float step = (float)(counter - INIT_FADE) / (float)(END_LOGO - INIT_FADE);
const int alpha = std::min((int)(255 * step), 255);
SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, alpha);
SDL_RenderFillRect(renderer, nullptr);
}
}
// Actualiza las variables del objeto
void Logo::update()
{
checkInput();
if (SDL_GetTicks() - ticks > ticksSpeed)
{
// Actualiza el contador de ticks
ticks = SDL_GetTicks();
// Actualiza el contador
counter++;
// Comprueba si ha terminado el logo
checkLogoEnd();
// Actualiza las notificaciones
screen->updateNotifier();
}
}
// Dibuja el objeto en pantalla
void Logo::render()
{
// Prepara para empezar a dibujar en la textura de juego
screen->start();
// Limpia la pantalla
screen->clean({238, 238, 238});
// Dibuja los objetos
sprite->render();
// Dibuja el fade
renderFade();
// Vuelca el contenido del renderizador en pantalla
screen->blit();
}
// Bucle para el logo del juego
void Logo::run()
{
JA_StopMusic();
while (section->name == SECTION_PROG_LOGO)
{
update();
checkEvents();
render();
}
}