Trabajando en la reescritura del código
This commit is contained in:
226
source/logo.cpp
226
source/logo.cpp
@@ -4,146 +4,154 @@
|
||||
#define END_LOGO 200
|
||||
|
||||
// Constructor
|
||||
Logo::Logo(SDL_Renderer *renderer, Screen *screen, Asset *mAsset)
|
||||
Logo::Logo(SDL_Renderer *renderer, Screen *screen, Asset *asset)
|
||||
{
|
||||
// Copia la dirección de los objetos
|
||||
mRenderer = renderer;
|
||||
mScreen = screen;
|
||||
this->mAsset = mAsset;
|
||||
this->renderer = renderer;
|
||||
this->screen = screen;
|
||||
this->asset = asset;
|
||||
|
||||
// Reserva memoria para los punteros
|
||||
mEventHandler = new SDL_Event();
|
||||
mTexture = new LTexture(mRenderer);
|
||||
mSprite = new Sprite();
|
||||
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
|
||||
mBackbuffer = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
if (mBackbuffer == nullptr)
|
||||
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()
|
||||
{
|
||||
mRenderer = nullptr;
|
||||
mScreen = nullptr;
|
||||
mAsset = nullptr;
|
||||
SDL_DestroyTexture(backbuffer);
|
||||
|
||||
mTexture->unload();
|
||||
delete mTexture;
|
||||
mTexture = nullptr;
|
||||
texture->unload();
|
||||
delete texture;
|
||||
|
||||
delete mSprite;
|
||||
mSprite = nullptr;
|
||||
|
||||
delete mEventHandler;
|
||||
mEventHandler = nullptr;
|
||||
|
||||
SDL_DestroyTexture(mBackbuffer);
|
||||
mBackbuffer = nullptr;
|
||||
delete sprite;
|
||||
delete eventHandler;
|
||||
}
|
||||
|
||||
// Inicializa las variables necesarias para la sección 'Logo'
|
||||
void Logo::init()
|
||||
// Comprueba si ha terminado el logo
|
||||
void Logo::checkLogoEnd()
|
||||
{
|
||||
// Carga los recursos
|
||||
loadMedia();
|
||||
if (counter == 0)
|
||||
{
|
||||
if (JA_GetMusicState() == JA_MUSIC_PLAYING)
|
||||
{
|
||||
JA_StopMusic();
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
if (counter == END_LOGO + 20)
|
||||
{
|
||||
counter = 0;
|
||||
section.name = PROG_SECTION_INTRO;
|
||||
section.subsection = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
||||
// Carga los recursos necesarios para la sección 'Logo'
|
||||
bool Logo::loadMedia()
|
||||
// Comprueba los eventos
|
||||
void Logo::checkEventHandler()
|
||||
{
|
||||
bool success = true;
|
||||
// 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;
|
||||
}
|
||||
|
||||
success &= loadTextureFromFile(mTexture, mAsset->get("logo.png"), mRenderer);
|
||||
// Cualquier tecla pulsada
|
||||
if ((eventHandler->type == SDL_KEYDOWN) || (eventHandler->type == SDL_JOYBUTTONDOWN))
|
||||
{
|
||||
section.name = PROG_SECTION_TITLE;
|
||||
section.subsection = TITLE_SECTION_1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return success;
|
||||
// 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()
|
||||
{
|
||||
init();
|
||||
const SDL_Rect rect = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
|
||||
const int fadeLenght = END_LOGO - INIT_FADE;
|
||||
|
||||
while (mSection.name == PROG_SECTION_LOGO)
|
||||
while (section.name == PROG_SECTION_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