100 lines
1.9 KiB
C++
100 lines
1.9 KiB
C++
#include "intro.h"
|
|
|
|
// Constructor
|
|
Intro::Intro(SDL_Renderer *renderer, Screen *screen, Asset *asset)
|
|
{
|
|
// Copia los punteros
|
|
this->renderer = renderer;
|
|
this->screen = screen;
|
|
this->asset = asset;
|
|
|
|
// Reserva memoria para los punteros
|
|
eventHandler = new SDL_Event();
|
|
texture = new LTexture();
|
|
loadTextureFromFile(texture, asset->get("intro.png"), renderer);
|
|
sprite = new AnimatedSprite(texture, renderer, asset->get("intro.ani"));
|
|
|
|
// Inicializa variables
|
|
section = {SECTION_PROG_INTRO, 0};
|
|
ticks = 0;
|
|
ticksSpeed = 15;
|
|
}
|
|
|
|
// Destructor
|
|
Intro::~Intro()
|
|
{
|
|
renderer = nullptr;
|
|
screen = nullptr;
|
|
asset = nullptr;
|
|
|
|
delete eventHandler;
|
|
eventHandler = nullptr;
|
|
|
|
texture->unload();
|
|
delete texture;
|
|
texture = nullptr;
|
|
}
|
|
|
|
// Actualiza las variables
|
|
void Intro::update()
|
|
{
|
|
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
|
|
if (SDL_GetTicks() - ticks > ticksSpeed)
|
|
{
|
|
// Actualiza el contador de ticks
|
|
ticks = SDL_GetTicks();
|
|
|
|
// 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;
|
|
}
|
|
|
|
// Cualquier tecla pulsada
|
|
if ((eventHandler->type == SDL_KEYDOWN) || (eventHandler->type == SDL_JOYBUTTONDOWN))
|
|
{
|
|
section.name = SECTION_PROG_GAME;
|
|
section.subsection = 0;
|
|
}
|
|
}
|
|
sprite->animate();
|
|
|
|
if (sprite->animationIsCompleted())
|
|
{
|
|
section.name = SECTION_PROG_GAME;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Dibuja en pantalla
|
|
void Intro::render()
|
|
{
|
|
// Prepara para empezar a dibujar en la textura de juego
|
|
screen->start();
|
|
|
|
// Limpia la pantalla
|
|
screen->clean();
|
|
|
|
// Dibuja los objetos
|
|
sprite->render();
|
|
|
|
// Vuelca el contenido del renderizador en pantalla
|
|
screen->blit();
|
|
}
|
|
|
|
// Bucle principal
|
|
section_t Intro::run()
|
|
{
|
|
while (section.name == SECTION_PROG_INTRO)
|
|
{
|
|
update();
|
|
render();
|
|
}
|
|
|
|
return section;
|
|
}
|