Files
coffee_crisis/source/logo.cpp
Sergio Valor 06d4712493 migrat a SDL3 Callback API (SDL_AppInit/Iterate/Event/Quit) (milestone 3)
- main.cpp reescrit amb SDL_MAIN_USE_CALLBACKS
- Director convertit a màquina d'estats amb iterate() i handleEvent()
- Seccions (Logo, Intro, Title, Game) amb iterate() i handleEvent()
- Events SDL enrutats via SDL_AppEvent → Director → secció activa
- Eliminat SDL_PollEvent de iterate(), events via handleEvent()
- Transicions entre seccions gestionades per handleSectionTransition()
- Instructions i Game (demo) delegats frame a frame des de Title

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 19:32:31 +02:00

163 lines
4.1 KiB
C++

#include "logo.h"
#include <SDL3/SDL.h>
#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.hpp" // 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;
JA_StopMusic();
}
// 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_EVENT_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() {
JA_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) {
iterate();
}
}
// Ejecuta un frame
void Logo::iterate() {
update();
render();
}
// Procesa un evento individual
void Logo::handleEvent(SDL_Event *event) {
// SDL_EVENT_QUIT ya lo maneja Director
}