158 lines
3.0 KiB
C++
158 lines
3.0 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 LTexture(renderer,asset->get("logo.png");
|
|
sprite = new Sprite(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, texture, renderer);
|
|
|
|
// Crea un backbuffer para el renderizador
|
|
backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT);
|
|
if (backbuffer == nullptr)
|
|
{
|
|
printf("Backbuffer could not be created!\nSDL Error: %s\n", SDL_GetError());
|
|
}
|
|
|
|
// Inicializa variables
|
|
counter = 0;
|
|
section.name = PROG_SECTION_LOGO;
|
|
section.subsection = 0;
|
|
ticks = 0;
|
|
ticksSpeed = 15;
|
|
}
|
|
|
|
// Destructor
|
|
Logo::~Logo()
|
|
{
|
|
SDL_DestroyTexture(backbuffer);
|
|
|
|
texture->unload();
|
|
delete texture;
|
|
|
|
delete sprite;
|
|
delete eventHandler;
|
|
}
|
|
|
|
// Comprueba si ha terminado el logo
|
|
void Logo::checkLogoEnd()
|
|
{
|
|
if (counter == 0)
|
|
{
|
|
if (JA_GetMusicState() == JA_MUSIC_PLAYING)
|
|
{
|
|
JA_StopMusic();
|
|
}
|
|
}
|
|
|
|
if (counter == END_LOGO + 20)
|
|
{
|
|
counter = 0;
|
|
section.name = PROG_SECTION_INTRO;
|
|
section.subsection = 0;
|
|
}
|
|
else
|
|
{
|
|
counter++;
|
|
}
|
|
}
|
|
|
|
// 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))
|
|
{
|
|
section.name = PROG_SECTION_TITLE;
|
|
section.subsection = TITLE_SECTION_1;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Dibuja el fade
|
|
void Logo::renderFade()
|
|
{
|
|
const SDL_Rect rect = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
|
|
const int fadeLenght = END_LOGO - INIT_FADE;
|
|
|
|
// Dibuja el fade
|
|
if (counter >= INIT_FADE)
|
|
{
|
|
const Uint16 alpha = (255 * (counter - INIT_FADE)) / fadeLenght;
|
|
if (alpha < 256)
|
|
{
|
|
SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, alpha);
|
|
}
|
|
else
|
|
{
|
|
SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, 255);
|
|
}
|
|
SDL_RenderFillRect(renderer, &rect);
|
|
}
|
|
}
|
|
|
|
// Actualiza las variables del objeto
|
|
void Logo::update()
|
|
{
|
|
checkEventHandler();
|
|
|
|
if (SDL_GetTicks() - ticks > ticksSpeed)
|
|
{
|
|
// Actualiza el contador de ticks
|
|
ticks = SDL_GetTicks();
|
|
|
|
// 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(bgColor);
|
|
|
|
// 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()
|
|
{
|
|
while (section.name == PROG_SECTION_LOGO)
|
|
{
|
|
update();
|
|
render();
|
|
}
|
|
|
|
return section;
|
|
}
|