Files
coffee_crisis/source/logo.cpp

169 lines
3.1 KiB
C++

#include "logo.h"
#define INIT_FADE 100
#define END_LOGO 200
// Constructor
Logo::Logo(SDL_Renderer *renderer, Screen *screen, Asset *asset)
{
// Copia la dirección de los objetos
this->renderer = renderer;
this->screen = screen;
this->asset = asset;
// 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 = PROG_SECTION_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 = PROG_SECTION_INTRO;
section.subsection = 0;
}
}
// Comprueba los eventos
void Logo::checkEventHandler()
{
// 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 = PROG_SECTION_QUIT;
break;
}
// Cualquier tecla pulsada
if ((eventHandler->type == SDL_KEYDOWN) || (eventHandler->type == SDL_JOYBUTTONDOWN))
{
switch (eventHandler->key.keysym.scancode)
{
case SDL_SCANCODE_F:
screen->switchVideoMode();
texture->reLoad();
break;
case SDL_SCANCODE_F1:
screen->setWindowSize(1);
texture->reLoad();
break;
case SDL_SCANCODE_F2:
screen->setWindowSize(2);
texture->reLoad();
break;
case SDL_SCANCODE_F3:
screen->setWindowSize(3);
texture->reLoad();
break;
case SDL_SCANCODE_F4:
screen->setWindowSize(4);
texture->reLoad();
break;
case SDL_SCANCODE_F5:
screen->showNotification("Conectado a Jailers.net");
break;
default:
section.name = PROG_SECTION_TITLE;
section.subsection = TITLE_SECTION_1;
break;
}
}
}
}
// 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()
{
checkEventHandler();
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
section_t Logo::run()
{
JA_StopMusic();
while (section.name == PROG_SECTION_LOGO)
{
update();
render();
}
return section;
}