Completado el logo y la intro
This commit is contained in:
212
source/logo.cpp
212
source/logo.cpp
@@ -1,151 +1,137 @@
|
||||
#include "logo.h"
|
||||
#ifdef __MIPSEL__
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
#endif
|
||||
|
||||
#define INIT_FADE 100
|
||||
#define END_LOGO 200
|
||||
|
||||
// Constructor
|
||||
Logo::Logo(SDL_Renderer *renderer, Screen *screen, std::string *fileList)
|
||||
Logo::Logo(SDL_Renderer *renderer, Screen *screen, Asset *asset)
|
||||
{
|
||||
// Copia la dirección de los objetos
|
||||
mRenderer = renderer;
|
||||
mScreen = screen;
|
||||
mFileList = fileList;
|
||||
this->renderer = renderer;
|
||||
this->screen = screen;
|
||||
this->asset = asset;
|
||||
|
||||
// Reserva memoria para los punteros
|
||||
mEventHandler = new SDL_Event();
|
||||
mTexture = new LTexture();
|
||||
mSprite = new Sprite();
|
||||
eventHandler = new SDL_Event();
|
||||
texture = new LTexture();
|
||||
loadTextureFromFile(texture, asset->get("logo.png"), renderer);
|
||||
sprite = new Sprite(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, texture, renderer);
|
||||
|
||||
// Crea un backbuffer para el renderizador
|
||||
mBackbuffer = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
if (mBackbuffer == NULL)
|
||||
backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
if (backbuffer == NULL)
|
||||
printf("Backbuffer could not be created!\nSDL Error: %s\n", SDL_GetError());
|
||||
|
||||
// Inicializa variables
|
||||
counter = 0;
|
||||
section.name = SECTION_PROG_LOGO;
|
||||
section.subsection = 0;
|
||||
ticks = 0;
|
||||
ticksSpeed = 15;
|
||||
initFade = 100;
|
||||
endLogo = 200;
|
||||
postLogo = 20;
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Logo::~Logo()
|
||||
{
|
||||
mRenderer = nullptr;
|
||||
mScreen = nullptr;
|
||||
mFileList = nullptr;
|
||||
renderer = nullptr;
|
||||
screen = nullptr;
|
||||
asset = nullptr;
|
||||
|
||||
mTexture->unload();
|
||||
delete mTexture;
|
||||
mTexture = nullptr;
|
||||
texture->unload();
|
||||
delete texture;
|
||||
texture = nullptr;
|
||||
|
||||
delete mSprite;
|
||||
mSprite = nullptr;
|
||||
delete sprite;
|
||||
sprite = nullptr;
|
||||
|
||||
delete mEventHandler;
|
||||
mEventHandler = nullptr;
|
||||
delete eventHandler;
|
||||
eventHandler = nullptr;
|
||||
|
||||
SDL_DestroyTexture(mBackbuffer);
|
||||
mBackbuffer = nullptr;
|
||||
SDL_DestroyTexture(backbuffer);
|
||||
backbuffer = nullptr;
|
||||
}
|
||||
|
||||
// Inicializa las variables necesarias para la sección 'Logo'
|
||||
void Logo::init()
|
||||
// Actualiza las variables
|
||||
void Logo::update()
|
||||
{
|
||||
// Carga los recursos
|
||||
loadMedia();
|
||||
// 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();
|
||||
|
||||
// Inicializa variables
|
||||
mCounter = 0;
|
||||
mSection.name = PROG_SECTION_LOGO;
|
||||
mSection.subsection = 0;
|
||||
mSprite->init(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, mTexture, mRenderer);
|
||||
mTicks = 0;
|
||||
mTicksSpeed = 15;
|
||||
// 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_TITLE;
|
||||
section.subsection = 0;
|
||||
}
|
||||
}
|
||||
|
||||
counter++;
|
||||
|
||||
// Comprueba si ha terminado el logo
|
||||
if (counter == endLogo + postLogo)
|
||||
{
|
||||
section.name = SECTION_PROG_INTRO;
|
||||
section.subsection = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Carga los recursos necesarios para la sección 'Logo'
|
||||
bool Logo::loadMedia()
|
||||
// Dibuja en pantalla
|
||||
void Logo::render()
|
||||
{
|
||||
bool success = true;
|
||||
|
||||
success &= loadTextureFromFile(mTexture, mFileList[35], mRenderer);
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
screen->start();
|
||||
|
||||
return success;
|
||||
// Limpia la pantalla
|
||||
screen->clean();
|
||||
|
||||
// Dibuja los objetos
|
||||
sprite->render();
|
||||
|
||||
// Dibuja el fade
|
||||
if (counter >= initFade)
|
||||
{
|
||||
const SDL_Rect rect = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
|
||||
const color_t color = {0xFF, 0xFF, 0xFF};
|
||||
const int fadeLenght = endLogo - initFade;
|
||||
|
||||
int alpha = (255 * (counter - initFade)) / fadeLenght;
|
||||
if (alpha < 256)
|
||||
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, alpha);
|
||||
else
|
||||
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 255);
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
}
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
screen->blit();
|
||||
}
|
||||
|
||||
// Bucle para el logo del juego
|
||||
section_t Logo::run()
|
||||
{
|
||||
init();
|
||||
const SDL_Rect rect = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
|
||||
const int fadeLenght = END_LOGO - INIT_FADE;
|
||||
// Detiene la música
|
||||
JA_StopMusic();
|
||||
|
||||
while (mSection.name == PROG_SECTION_LOGO)
|
||||
while (section.name == SECTION_PROG_LOGO)
|
||||
{
|
||||
// Comprueba los eventos que hay en la cola
|
||||
while (SDL_PollEvent(mEventHandler) != 0)
|
||||
{
|
||||
// Evento de salida de la aplicación
|
||||
if (mEventHandler->type == SDL_QUIT)
|
||||
{
|
||||
mSection.name = PROG_SECTION_QUIT;
|
||||
break;
|
||||
}
|
||||
|
||||
// Cualquier tecla pulsada
|
||||
if ((mEventHandler->type == SDL_KEYDOWN) || (mEventHandler->type == SDL_JOYBUTTONDOWN))
|
||||
{
|
||||
mSection.name = PROG_SECTION_TITLE;
|
||||
mSection.subsection = TITLE_SECTION_1;
|
||||
}
|
||||
}
|
||||
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
mScreen->start();
|
||||
|
||||
// Limpia la pantalla
|
||||
mScreen->clean(bgColor);
|
||||
|
||||
// Dibuja los objetos
|
||||
mSprite->render();
|
||||
|
||||
// Dibuja el fade
|
||||
if (mCounter >= INIT_FADE)
|
||||
{
|
||||
Uint16 alpha = (255 * (mCounter - INIT_FADE)) / fadeLenght;
|
||||
if (alpha < 256)
|
||||
SDL_SetRenderDrawColor(mRenderer, bgColor.r, bgColor.g, bgColor.b, alpha);
|
||||
else
|
||||
SDL_SetRenderDrawColor(mRenderer, bgColor.r, bgColor.g, bgColor.b, 255);
|
||||
SDL_RenderFillRect(mRenderer, &rect);
|
||||
}
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
mScreen->blit();
|
||||
|
||||
// Comprueba si ha terminado el logo
|
||||
if (SDL_GetTicks() - mTicks > mTicksSpeed)
|
||||
{
|
||||
// Actualiza el contador de ticks
|
||||
mTicks = SDL_GetTicks();
|
||||
|
||||
if (mCounter == 0)
|
||||
{
|
||||
if (JA_GetMusicState() == JA_MUSIC_PLAYING)
|
||||
JA_StopMusic();
|
||||
}
|
||||
|
||||
if (mCounter == END_LOGO + 20)
|
||||
{
|
||||
mCounter = 0;
|
||||
mSection.name = PROG_SECTION_INTRO;
|
||||
mSection.subsection = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
mCounter++;
|
||||
}
|
||||
}
|
||||
update();
|
||||
render();
|
||||
}
|
||||
|
||||
return mSection;
|
||||
return section;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user