168 lines
3.7 KiB
C++
168 lines
3.7 KiB
C++
#include "logo.h"
|
|
#include <SDL2/SDL_timer.h> // for SDL_GetTicks
|
|
#include <algorithm> // for min
|
|
#include <string> // for basic_string
|
|
#include "asset.h" // for Asset
|
|
#include "const.h" // for bgColor, SECTION_PROG_LOGO, SECTION_PROG...
|
|
#include "input.h" // for Input, REPEAT_FALSE, inputs_e
|
|
#include "jail_audio.h" // for JA_StopMusic
|
|
#include "screen.h" // for Screen
|
|
#include "sprite.h" // for Sprite
|
|
#include "texture.h" // for Texture
|
|
#include "utils.h" // for section_t, color_t
|
|
|
|
// Valores de inicialización y fin
|
|
constexpr int INIT_FADE = 100;
|
|
constexpr int 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();
|
|
}
|
|
}
|
|
|
|
// 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();
|
|
}
|
|
}
|