reestructuració

This commit is contained in:
2026-04-17 17:15:38 +02:00
parent 55caef3210
commit 5fec0110b3
66 changed files with 221 additions and 217 deletions

View File

@@ -0,0 +1,283 @@
#include "game/scenes/instructions.h"
#include <SDL3/SDL.h>
#include <algorithm> // for max
#include <iostream> // for char_traits, basic_ostream, operator<<
#include <string> // for basic_string
#include "core/audio/jail_audio.hpp" // for JA_StopMusic
#include "core/input/input.h" // for Input, REPEAT_FALSE, inputs_e
#include "core/locale/lang.h" // for Lang
#include "core/rendering/screen.h" // for Screen
#include "core/rendering/sprite.h" // for Sprite
#include "core/rendering/text.h" // for Text, TXT_CENTER, TXT_COLOR, TXT_SHADOW
#include "core/rendering/texture.h" // for Texture
#include "core/resources/asset.h" // for Asset
#include "core/resources/resource.h"
#include "game/defaults.hpp" // for shdwTxtColor, GAMECANVAS_CENTER_X, GAME...
#include "utils/utils.h" // for color_t, section_t
// Constructor
Instructions::Instructions(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, Lang *lang, section_t *section) {
// Copia los punteros
this->renderer = renderer;
this->screen = screen;
this->asset = asset;
this->input = input;
this->lang = lang;
this->section = section;
// Texturas (handles compartidos de Resource)
Resource *R = Resource::get();
itemTextures.push_back(R->getTexture("item_points1_disk.png"));
itemTextures.push_back(R->getTexture("item_points2_gavina.png"));
itemTextures.push_back(R->getTexture("item_points3_pacmar.png"));
itemTextures.push_back(R->getTexture("item_clock.png"));
itemTextures.push_back(R->getTexture("item_coffee.png"));
itemTextures.push_back(R->getTexture("item_coffee_machine.png"));
eventHandler = new SDL_Event();
sprite = new Sprite(0, 0, 16, 16, itemTextures[0], renderer);
text = R->getText("smb2");
// Crea un backbuffer para el renderizador
backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
if (backbuffer != nullptr) {
SDL_SetTextureScaleMode(backbuffer, Texture::currentScaleMode);
} else {
std::cout << "Error: textTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
}
// Inicializa variables
ticks = 0;
ticksSpeed = 15;
manualQuit = false;
counter = 0;
counterEnd = 600;
finished = false;
quitRequested = false;
}
// Destructor
Instructions::~Instructions() {
// itemTextures y text son propiedad de Resource — no liberar.
itemTextures.clear();
delete sprite;
delete eventHandler;
SDL_DestroyTexture(backbuffer);
}
// Actualiza las variables
void Instructions::update() {
// Bombea el stream de música: si no se llama, el buffer se vacía y la
// música se corta hasta que volvamos a una escena que sí lo haga.
JA_Update();
// Comprueba las entradas
checkInput();
// Actualiza las variables
if (SDL_GetTicks() - ticks > ticksSpeed) {
// Actualiza el contador de ticks
ticks = SDL_GetTicks();
if (mode == m_auto) { // Modo automático
counter++;
if (counter == counterEnd) {
finished = true;
}
} else { // Modo manual
++counter %= 60000;
if (manualQuit) {
finished = true;
}
}
}
}
// Pinta en pantalla
void Instructions::render() {
// Pinta en pantalla
SDL_Rect window = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT};
SDL_Rect srcRect = {0, 0, 16, 16};
const color_t orangeColor = {0xFF, 0x7A, 0x00};
const SDL_Rect destRect1 = {60, 88 + (16 * 0), 16, 16}; // Disquito
const SDL_Rect destRect2 = {60, 88 + (16 * 1), 16, 16}; // Gavineixon
const SDL_Rect destRect3 = {60, 88 + (16 * 2), 16, 16}; // Pacmar
const SDL_Rect destRect4 = {60, 88 + (16 * 3), 16, 16}; // Time Stopper
const SDL_Rect destRect5 = {60, 88 + (16 * 4), 16, 16}; // Coffee
// Pinta en el backbuffer el texto y los sprites
SDL_SetRenderTarget(renderer, backbuffer);
SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, 255);
SDL_RenderClear(renderer);
// Escribe el texto
text->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, 8, lang->getText(11), 1, orangeColor, 1, shdwTxtColor);
text->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, 24, lang->getText(12), 1, noColor, 1, shdwTxtColor);
text->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, 34, lang->getText(13), 1, noColor, 1, shdwTxtColor);
text->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, 48, lang->getText(14), 1, noColor, 1, shdwTxtColor);
text->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, 58, lang->getText(15), 1, noColor, 1, shdwTxtColor);
text->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, 75, lang->getText(16), 1, orangeColor, 1, shdwTxtColor);
text->writeShadowed(84, 92, lang->getText(17), shdwTxtColor);
text->writeShadowed(84, 108, lang->getText(18), shdwTxtColor);
text->writeShadowed(84, 124, lang->getText(19), shdwTxtColor);
text->writeShadowed(84, 140, lang->getText(20), shdwTxtColor);
text->writeShadowed(84, 156, lang->getText(21), shdwTxtColor);
if ((mode == m_manual) && (counter % 50 > 14)) {
text->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, GAMECANVAS_HEIGHT - 12, lang->getText(22), 1, orangeColor, 1, shdwTxtColor);
}
// Disquito
sprite->setTexture(itemTextures[0]);
sprite->setPos(destRect1);
srcRect.y = 16 * (((counter + 12) / 36) % 2);
sprite->setSpriteClip(srcRect);
sprite->render();
// Gavineixon
sprite->setTexture(itemTextures[1]);
sprite->setPos(destRect2);
srcRect.y = 16 * (((counter + 9) / 36) % 2);
sprite->setSpriteClip(srcRect);
sprite->render();
// Pacmar
sprite->setTexture(itemTextures[2]);
sprite->setPos(destRect3);
srcRect.y = 16 * (((counter + 6) / 36) % 2);
sprite->setSpriteClip(srcRect);
sprite->render();
// Time Stopper
sprite->setTexture(itemTextures[3]);
sprite->setPos(destRect4);
srcRect.y = 16 * (((counter + 3) / 36) % 2);
sprite->setSpriteClip(srcRect);
sprite->render();
// Coffee
sprite->setTexture(itemTextures[4]);
sprite->setPos(destRect5);
srcRect.y = 16 * (((counter + 0) / 36) % 2);
sprite->setSpriteClip(srcRect);
sprite->render();
// Cambia el destino de renderizado
SDL_SetRenderTarget(renderer, nullptr);
// Prepara para empezar a dibujar en la textura de juego
screen->start();
// Limpia la pantalla
screen->clean(bgColor);
// Establece la ventana del backbuffer
if (mode == m_auto) {
window.y = std::max(8, GAMECANVAS_HEIGHT - counter + 100);
} else {
window.y = 0;
}
// Copia el backbuffer al renderizador
SDL_FRect fWindow = {(float)window.x, (float)window.y, (float)window.w, (float)window.h};
SDL_RenderTexture(renderer, backbuffer, nullptr, &fWindow);
// Vuelca el contenido del renderizador en pantalla
screen->blit();
}
// Comprueba los eventos
void Instructions::checkEvents() {
#ifndef __EMSCRIPTEN__
// 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) {
quitRequested = true;
finished = true;
break;
}
}
#endif
}
// Comprueba las entradas
void Instructions::checkInput() {
#ifndef __EMSCRIPTEN__
if (input->checkInput(input_exit, REPEAT_FALSE)) {
quitRequested = true;
finished = true;
} else
#endif
if (input->checkInput(input_window_fullscreen, REPEAT_FALSE)) {
screen->toggleVideoMode();
}
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE)) {
screen->decWindowZoom();
}
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE)) {
screen->incWindowZoom();
}
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)) {
if (mode == m_auto) {
finished = true;
} else {
if (counter > 30) {
manualQuit = true;
}
}
}
}
// Bucle para la pantalla de instrucciones (compatibilidad)
void Instructions::run(mode_e mode) {
start(mode);
while (!finished) {
update();
checkEvents();
render();
}
// Aplica los cambios de sección según el resultado
if (quitRequested) {
section->name = SECTION_PROG_QUIT;
} else {
section->name = SECTION_PROG_TITLE;
section->subsection = (mode == m_auto) ? SUBSECTION_TITLE_1 : SUBSECTION_TITLE_3;
}
}
// Inicia las instrucciones (sin bucle)
void Instructions::start(mode_e mode) {
this->mode = mode;
finished = false;
quitRequested = false;
manualQuit = false;
counter = 0;
ticks = 0;
}
// Indica si las instrucciones han terminado
bool Instructions::hasFinished() const {
return finished;
}
// Indica si se ha solicitado salir de la aplicación
bool Instructions::isQuitRequested() const {
return quitRequested;
}

View File

@@ -0,0 +1,76 @@
#pragma once
#include <SDL3/SDL.h>
#include <vector> // for vector
class Asset;
class Input;
class Lang;
class Screen;
class Sprite;
class Text;
class Texture;
struct section_t;
enum mode_e {
m_manual,
m_auto
};
// Clase Instructions
class Instructions {
private:
// Objetos y punteros
SDL_Renderer *renderer; // El renderizador de la ventana
Screen *screen; // Objeto encargado de dibujar en pantalla
std::vector<Texture *> itemTextures; // Vector con las texturas de los items
SDL_Event *eventHandler; // Manejador de eventos
SDL_Texture *backbuffer; // Textura para usar como backbuffer
Sprite *sprite; // Sprite con la textura de las instrucciones
Asset *asset; // Objeto que gestiona todos los ficheros de recursos
Input *input; // Objeto pata gestionar la entrada
Lang *lang; // Objeto para gestionar los textos en diferentes idiomas
Text *text; // Objeto para escribir texto
section_t *section; // Estado del bucle principal para saber si continua o se sale
// Variables
Uint16 counter; // Contador
Uint16 counterEnd; // Valor final para el contador
Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa
Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa
bool manualQuit; // Indica si se quiere salir del modo manual
mode_e mode; // Modo en el que se van a ejecutar las instrucciones
bool finished; // Indica si las instrucciones han terminado
bool quitRequested; // Indica si se ha solicitado salir de la aplicación
// Comprueba las entradas
void checkInput();
public:
// Constructor
Instructions(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, Lang *lang, section_t *section);
// Destructor
~Instructions();
// Bucle principal
void run(mode_e mode);
// Inicia las instrucciones (sin bucle)
void start(mode_e mode);
// Actualiza las variables
void update();
// Pinta en pantalla
void render();
// Comprueba los eventos
void checkEvents();
// Indica si las instrucciones han terminado
bool hasFinished() const;
// Indica si se ha solicitado salir de la aplicación
bool isQuitRequested() const;
};

View File

@@ -0,0 +1,416 @@
#include "game/scenes/intro.h"
#include <SDL3/SDL.h>
#include <string> // for basic_string
#include "core/audio/jail_audio.hpp" // for JA_StopMusic, JA_DeleteMusic, JA_LoadMusic
#include "core/input/input.h" // for Input, REPEAT_FALSE, inputs_e
#include "core/locale/lang.h" // for Lang
#include "core/rendering/screen.h" // for Screen
#include "core/rendering/smartsprite.h" // for SmartSprite
#include "core/rendering/text.h" // for Text
#include "core/rendering/texture.h" // for Texture
#include "core/rendering/writer.h" // for Writer
#include "core/resources/asset.h" // for Asset
#include "core/resources/resource.h"
#include "game/defaults.hpp" // for GAMECANVAS_CENTER_X, GAMECANVAS_FIRST_QU...
#include "utils/utils.h" // for section_t, color_t
// Constructor
Intro::Intro(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, Lang *lang, section_t *section) {
// Copia los punteros
this->renderer = renderer;
this->screen = screen;
this->lang = lang;
this->asset = asset;
this->input = input;
this->section = section;
// Reserva memoria para los objetos
eventHandler = new SDL_Event();
texture = Resource::get()->getTexture("intro.png");
text = Resource::get()->getText("nokia");
music = Resource::get()->getMusic("intro.ogg");
// Inicializa variables
section->name = SECTION_PROG_INTRO;
section->subsection = 0;
ticks = 0;
ticksSpeed = 15;
scene = 1;
// Inicializa los bitmaps de la intro
const int totalBitmaps = 6;
for (int i = 0; i < totalBitmaps; ++i) {
SmartSprite *ss = new SmartSprite(texture, renderer);
ss->setWidth(128);
ss->setHeight(96);
ss->setEnabledCounter(20);
ss->setDestX(GAMECANVAS_CENTER_X - 64);
ss->setDestY(GAMECANVAS_FIRST_QUARTER_Y - 24);
bitmaps.push_back(ss);
}
bitmaps[0]->setPosX(-128);
bitmaps[0]->setPosY(GAMECANVAS_FIRST_QUARTER_Y - 24);
bitmaps[0]->setVelX(0.0f);
bitmaps[0]->setVelY(0.0f);
bitmaps[0]->setAccelX(0.6f);
bitmaps[0]->setAccelY(0.0f);
bitmaps[0]->setSpriteClip(0, 0, 128, 96);
bitmaps[1]->setPosX(GAMECANVAS_WIDTH);
bitmaps[1]->setPosY(GAMECANVAS_FIRST_QUARTER_Y - 24);
bitmaps[1]->setVelX(-1.0f);
bitmaps[1]->setVelY(0.0f);
bitmaps[1]->setAccelX(-0.3f);
bitmaps[1]->setAccelY(0.0f);
bitmaps[1]->setSpriteClip(128, 0, 128, 96);
bitmaps[2]->setPosX(GAMECANVAS_CENTER_X - 64);
bitmaps[2]->setPosY(-96);
bitmaps[2]->setVelX(0.0f);
bitmaps[2]->setVelY(3.0f);
bitmaps[2]->setAccelX(0.1f);
bitmaps[2]->setAccelY(0.3f);
bitmaps[2]->setSpriteClip(0, 96, 128, 96);
bitmaps[2]->setEnabledCounter(250);
bitmaps[3]->setPosX(GAMECANVAS_CENTER_X - 64);
bitmaps[3]->setPosY(GAMECANVAS_HEIGHT);
bitmaps[3]->setVelX(0.0f);
bitmaps[3]->setVelY(-0.7f);
bitmaps[3]->setAccelX(0.0f);
bitmaps[3]->setAccelY(0.0f);
bitmaps[3]->setSpriteClip(128, 96, 128, 96);
bitmaps[4]->setPosX(GAMECANVAS_CENTER_X - 64);
bitmaps[4]->setPosY(-96);
bitmaps[4]->setVelX(0.0f);
bitmaps[4]->setVelY(3.0f);
bitmaps[4]->setAccelX(0.1f);
bitmaps[4]->setAccelY(0.3f);
bitmaps[4]->setSpriteClip(0, 192, 128, 96);
bitmaps[5]->setPosX(GAMECANVAS_WIDTH);
bitmaps[5]->setPosY(GAMECANVAS_FIRST_QUARTER_Y - 24);
bitmaps[5]->setVelX(-0.7f);
bitmaps[5]->setVelY(0.0f);
bitmaps[5]->setAccelX(0.0f);
bitmaps[5]->setAccelY(0.0f);
bitmaps[5]->setSpriteClip(128, 192, 128, 96);
// Inicializa los textos de la intro
const int totalTexts = 9;
for (int i = 0; i < totalTexts; ++i) {
Writer *w = new Writer(text);
w->setPosX(BLOCK * 0);
w->setPosY(GAMECANVAS_HEIGHT - (BLOCK * 6));
w->setKerning(-1);
w->setEnabled(false);
w->setEnabledCounter(180);
texts.push_back(w);
}
// Un dia qualsevol de l'any 2000
texts[0]->setCaption(lang->getText(27));
texts[0]->setSpeed(8);
// Tot esta tranquil a la UPV
texts[1]->setCaption(lang->getText(28));
texts[1]->setSpeed(8);
// Fins que un desaprensiu...
texts[2]->setCaption(lang->getText(29));
texts[2]->setSpeed(12);
// HEY! ME ANE A FERME UN CORTAET...
texts[3]->setCaption(lang->getText(30));
texts[3]->setSpeed(8);
// UAAAAAAAAAAAAA!!!
texts[4]->setCaption(lang->getText(31));
texts[4]->setSpeed(1);
// Espera un moment...
texts[5]->setCaption(lang->getText(32));
texts[5]->setSpeed(16);
// Si resulta que no tinc solt!
texts[6]->setCaption(lang->getText(33));
texts[6]->setSpeed(2);
// MERDA DE MAQUINA!
texts[7]->setCaption(lang->getText(34));
texts[7]->setSpeed(3);
// Blop... blop... blop...
texts[8]->setCaption(lang->getText(35));
texts[8]->setSpeed(16);
for (auto text : texts) {
text->center(GAMECANVAS_CENTER_X);
}
JA_PlayMusic(music, 0);
}
// Destructor
Intro::~Intro() {
delete eventHandler;
// texture, text, music son propiedad de Resource — no liberar aquí.
for (auto bitmap : bitmaps) {
delete bitmap;
}
for (auto t : texts) {
delete t;
}
}
// Carga los recursos (ya no carga nada, se mantiene por si hay callers)
bool Intro::loadMedia() {
return true;
}
// Comprueba los eventos
void Intro::checkEvents() {
#ifndef __EMSCRIPTEN__
// 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;
}
}
#endif
}
// Comprueba las entradas
void Intro::checkInput() {
#ifndef __EMSCRIPTEN__
if (input->checkInput(input_exit, REPEAT_FALSE)) {
section->name = SECTION_PROG_QUIT;
} else
#endif
if (input->checkInput(input_window_fullscreen, REPEAT_FALSE)) {
screen->toggleVideoMode();
}
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE)) {
screen->decWindowZoom();
}
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE)) {
screen->incWindowZoom();
}
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)) {
JA_StopMusic();
section->name = SECTION_PROG_TITLE;
section->subsection = SUBSECTION_TITLE_1;
}
}
// Actualiza las escenas de la intro
void Intro::updateScenes() {
switch (scene) {
case 1:
// Primera imagen - UPV
if (!bitmaps[0]->hasFinished()) {
bitmaps[0]->setEnabled(true);
}
// Primer texto de la primera imagen
if (bitmaps[0]->hasFinished() && !texts[0]->hasFinished()) {
texts[0]->setEnabled(true);
}
// Segundo texto de la primera imagen
if (texts[0]->hasFinished() && !texts[1]->hasFinished()) {
texts[0]->setEnabled(false);
texts[1]->setEnabled(true);
}
// Tercer texto de la primera imagen
if (texts[1]->hasFinished() && !texts[2]->hasFinished()) {
texts[1]->setEnabled(false);
texts[2]->setEnabled(true);
}
// Fin de la primera escena
if (texts[2]->hasFinished()) {
bitmaps[0]->setEnabled(false);
texts[2]->setEnabled(false);
scene++;
}
break;
case 2:
// Segunda imagen - Máquina
if (!bitmaps[1]->hasFinished()) {
bitmaps[1]->setEnabled(true);
}
// Primer texto de la segunda imagen
if (bitmaps[1]->hasFinished() && !texts[3]->hasFinished()) {
texts[3]->setEnabled(true);
}
// Fin de la segunda escena
if (texts[3]->hasFinished()) {
bitmaps[1]->setEnabled(false);
texts[3]->setEnabled(false);
scene++;
}
break;
case 3:
// Tercera imagen junto con primer texto - GRITO
if (!bitmaps[2]->hasFinished() && !texts[4]->hasFinished()) {
bitmaps[2]->setEnabled(true);
texts[4]->setEnabled(true);
}
// Fin de la tercera escena
if (bitmaps[2]->hasFinished() && texts[4]->hasFinished()) {
bitmaps[2]->setEnabled(false);
texts[4]->setEnabled(false);
scene++;
}
break;
case 4:
// Cuarta imagen junto con primer texto - Reflexión
if (!bitmaps[3]->hasFinished() && !texts[5]->hasFinished()) {
bitmaps[3]->setEnabled(true);
texts[5]->setEnabled(true);
}
// Segundo texto de la cuarta imagen
if (texts[5]->hasFinished() && !texts[6]->hasFinished()) {
texts[5]->setEnabled(false);
texts[6]->setEnabled(true);
}
// Fin de la cuarta escena
if (bitmaps[3]->hasFinished() && texts[6]->hasFinished()) {
bitmaps[3]->setEnabled(false);
texts[6]->setEnabled(false);
scene++;
}
break;
case 5:
// Quinta imagen - Patada
if (!bitmaps[4]->hasFinished()) {
bitmaps[4]->setEnabled(true);
}
// Primer texto de la quinta imagen
if (bitmaps[4]->hasFinished() && !texts[7]->hasFinished()) {
texts[7]->setEnabled(true);
}
// Fin de la quinta escena
if (bitmaps[4]->hasFinished() && texts[7]->hasFinished()) {
bitmaps[4]->setEnabled(false);
texts[7]->setEnabled(false);
scene++;
}
break;
case 6:
// Sexta imagen junto con texto - Globos de café
if (!bitmaps[5]->hasFinished() && !texts[8]->hasFinished()) {
bitmaps[5]->setEnabled(true);
texts[8]->setEnabled(true);
}
// Acaba el último texto
if (bitmaps[5]->hasFinished() && texts[8]->hasFinished()) {
bitmaps[5]->setEnabled(false);
texts[8]->setEnabled(false);
JA_StopMusic();
section->name = SECTION_PROG_TITLE;
section->subsection = SUBSECTION_TITLE_1;
}
break;
default:
break;
}
}
// Actualiza las variables del objeto
void Intro::update() {
JA_Update();
checkInput();
if (SDL_GetTicks() - ticks > ticksSpeed) {
// Actualiza el contador de ticks
ticks = SDL_GetTicks();
// Actualiza los objetos
for (auto bitmap : bitmaps) {
bitmap->update();
}
for (auto text : texts) {
text->update();
}
// Actualiza las escenas de la intro
updateScenes();
}
}
// Dibuja el objeto en pantalla
void Intro::render() {
// Prepara para empezar a dibujar en la textura de juego
screen->start();
// Limpia la pantalla
screen->clean(bgColor);
// Dibuja los objetos
for (auto bitmap : bitmaps) {
bitmap->render();
}
for (auto text : texts) {
text->render();
}
// Vuelca el contenido del renderizador en pantalla
screen->blit();
}
// Bucle principal
void Intro::run() {
JA_PlayMusic(music, 0);
while (section->name == SECTION_PROG_INTRO) {
iterate();
}
}
// Ejecuta un frame
void Intro::iterate() {
update();
render();
}
// Procesa un evento individual
void Intro::handleEvent(SDL_Event *event) {
// SDL_EVENT_QUIT ya lo maneja Director
}

View File

@@ -0,0 +1,72 @@
#pragma once
#include <SDL3/SDL.h>
#include <vector> // for vector
class Asset;
class Input;
class Lang;
class Screen;
class SmartSprite;
class Text;
class Texture;
class Writer;
struct JA_Music_t;
struct section_t;
// Clase Intro
class Intro {
private:
// Objetos y punteros
SDL_Renderer *renderer; // El renderizador de la ventana
Screen *screen; // Objeto encargado de dibujar en pantalla
Texture *texture; // Textura con los graficos
SDL_Event *eventHandler; // Manejador de eventos
Asset *asset; // Objeto que gestiona todos los ficheros de recursos
Lang *lang; // Objeto para gestionar los textos en diferentes idiomas
Input *input; // Objeto pata gestionar la entrada
std::vector<SmartSprite *> bitmaps; // Vector con los sprites inteligentes para los dibujos de la intro
std::vector<Writer *> texts; // Textos de la intro
Text *text; // Textos de la intro
section_t *section; // Estado del bucle principal para saber si continua o se sale
// Variables
Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa
Uint8 ticksSpeed; // Velocidad a la que se repiten los bucles del programa
JA_Music_t *music; // Musica para la intro
int scene; // Indica que escena está activa
// Actualiza las variables del objeto
void update();
// Dibuja el objeto en pantalla
void render();
// Carga los recursos
bool loadMedia();
// Comprueba los eventos
void checkEvents();
// Comprueba las entradas
void checkInput();
// Actualiza las escenas de la intro
void updateScenes();
public:
// Constructor
Intro(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, Lang *lang, section_t *section);
// Destructor
~Intro();
// Bucle principal
void run();
// Ejecuta un frame
void iterate();
// Procesa un evento
void handleEvent(SDL_Event *event);
};

164
source/game/scenes/logo.cpp Normal file
View File

@@ -0,0 +1,164 @@
#include "game/scenes/logo.h"
#include <SDL3/SDL.h>
#include <algorithm> // for min
#include <string> // for basic_string
#include "core/audio/jail_audio.hpp" // for JA_StopMusic
#include "core/input/input.h" // for Input, REPEAT_FALSE, inputs_e
#include "core/rendering/screen.h" // for Screen
#include "core/rendering/sprite.h" // for Sprite
#include "core/rendering/texture.h" // for Texture
#include "core/resources/asset.h" // for Asset
#include "core/resources/resource.h"
#include "game/defaults.hpp" // for bgColor, SECTION_PROG_LOGO, SECTION_PROG...
#include "utils/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 = Resource::get()->getTexture("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 es propiedad de Resource — no liberar aquí.
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() {
#ifndef __EMSCRIPTEN__
// 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;
}
}
#endif
}
// Comprueba las entradas
void Logo::checkInput() {
#ifndef __EMSCRIPTEN__
if (input->checkInput(input_exit, REPEAT_FALSE)) {
section->name = SECTION_PROG_QUIT;
} else
#endif
if (input->checkInput(input_window_fullscreen, REPEAT_FALSE)) {
screen->toggleVideoMode();
}
else if (input->checkInput(input_window_dec_size, REPEAT_FALSE)) {
screen->decWindowZoom();
}
else if (input->checkInput(input_window_inc_size, REPEAT_FALSE)) {
screen->incWindowZoom();
}
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
}

62
source/game/scenes/logo.h Normal file
View File

@@ -0,0 +1,62 @@
#pragma once
#include <SDL3/SDL.h>
class Asset;
class Input;
class Screen;
class Sprite;
class Texture;
struct section_t;
// Clase Logo
class Logo {
private:
// Objetos y punteros
SDL_Renderer *renderer; // El renderizador de la ventana
Screen *screen; // Objeto encargado de dibujar en pantalla
Asset *asset; // Objeto que gestiona todos los ficheros de recursos
Input *input; // Objeto pata gestionar la entrada
Texture *texture; // Textura con los graficos
SDL_Event *eventHandler; // Manejador de eventos
Sprite *sprite; // Sprite con la textura del logo
section_t *section; // Estado del bucle principal para saber si continua o se sale
// Variables
Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa
Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa
int counter; // Contador
// Actualiza las variables del objeto
void update();
// Dibuja el objeto en pantalla
void render();
// Comprueba si ha terminado el logo
void checkLogoEnd();
// Comprueba los eventos
void checkEvents();
// Comprueba las entradas
void checkInput();
// Dibuja el fade
void renderFade();
public:
// Constructor
Logo(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, section_t *section);
// Destructor
~Logo();
// Bucle principal
void run();
// Ejecuta un frame
void iterate();
// Procesa un evento
void handleEvent(SDL_Event *event);
};

1129
source/game/scenes/title.cpp Normal file

File diff suppressed because it is too large Load Diff

165
source/game/scenes/title.h Normal file
View File

@@ -0,0 +1,165 @@
#pragma once
#include <SDL3/SDL.h>
#include <vector> // for vector
#include "game/scenes/instructions.h" // for mode_e
#include "utils/utils.h" // for input_t, options_t, section_t
class AnimatedSprite;
class Asset;
class Fade;
class Game;
class Input;
class Lang;
class Menu;
class Screen;
class SmartSprite;
class Sprite;
class Text;
class Texture;
struct JA_Music_t;
struct JA_Sound_t;
// Textos
constexpr const char *TEXT_COPYRIGHT = "@2020 JailDesigner (v2.3.4)";
// Contadores
constexpr int TITLE_COUNTER = 800;
// Cantidad de eventos de la pantalla de título
constexpr int TITLE_TOTAL_EVENTS = 2;
class Title {
private:
struct menu_t {
Menu *title; // Menu de la pantalla de título
Menu *options; // Menú de la pantalla de opciones
Menu *playerSelect; // Menu para elegir jugador
Menu *active; // Menu activo (de momento para la pantalla del titulo)
bool keyPressed; // Variable para evitar la repetición de teclas en los menus
};
// Objetos y punteros
SDL_Renderer *renderer; // El renderizador de la ventana
Screen *screen; // Objeto encargado de dibujar en pantalla
Asset *asset; // Objeto que gestiona todos los ficheros de recursos
Input *input; // Objeto para leer las entradas de teclado o mando
Lang *lang; // Objeto para gestionar los textos en diferentes idiomas
Instructions *instructions; // Objeto para la sección de las instrucciones
Game *demoGame; // Objeto para lanzar la demo del juego
SDL_Event *eventHandler; // Manejador de eventos
section_t *section; // Indicador para el bucle del titulo
Texture *dustTexture; // Textura con los graficos del polvo
Texture *coffeeTexture; // Textura con los graficos de la palabra coffee
Texture *crisisTexture; // Textura con los graficos de la plabra crisis
Texture *gradientTexture; // Textura con los graficos para el degradado del fondo del titulo
SDL_Rect backgroundWindow; // Ventana visible para la textura de fondo del titulo
SDL_Texture *background; // Textura dibujar el fondo del titulo
AnimatedSprite *dustBitmapL; // Sprite con la el polvo que aparece al colisionar el texto de la pantalla de titulo
AnimatedSprite *dustBitmapR; // Sprite con la el polvo que aparece al colisionar el texto de la pantalla de titulo
SmartSprite *coffeeBitmap; // Sprite con la palabra COFFEE para la pantalla de titulo
SmartSprite *crisisBitmap; // Sprite con la palabra CRISIS para la pantalla de titulo
Sprite *gradient; // Sprite para dibujar el degradado del titulo
Text *text1; // Objeto de texto para poder escribir textos en pantalla
Text *text2; // Objeto de texto para poder escribir textos en pantalla
Fade *fade; // Objeto para realizar fundidos en pantalla
// Variable
JA_Music_t *titleMusic; // Musica para el titulo
JA_Sound_t *crashSound; // Sonido con el impacto del título
int backgroundCounter; // Temporizador para el fondo de tiles de la pantalla de titulo
int counter; // Temporizador para la pantalla de titulo
Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa
Uint8 backgroundMode; // Variable para almacenar el tipo de efecto que hará el fondo de la pantalla de titulo
float sin[360]; // Vector con los valores del seno precalculados
bool menuVisible; // Indicador para saber si se muestra el menu del titulo o la frase intermitente
bool demo; // Indica si el modo demo estará activo
section_t nextSection; // Indica cual es la siguiente sección a cargar cuando termine el contador del titulo
Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa
Uint8 postFade; // Opción a realizar cuando termina el fundido
menu_t menu; // Variable con todos los objetos menus y sus variables
struct options_t *options; // Variable con todas las variables de las opciones del programa
options_t optionsPrevious; // Variable de respaldo para las opciones
std::vector<input_t> availableInputDevices; // Vector con todos los metodos de control disponibles
std::vector<int> deviceIndex; // Indice para el jugador [i] del vector de dispositivos de entrada disponibles
// Variables para la vibración del título (SUBSECTION_TITLE_2)
int vibrationStep; // Paso actual de la vibración
int vibrationCoffeeBaseX; // Posición X base del bitmap Coffee
int vibrationCrisisBaseX; // Posición X base del bitmap Crisis
bool vibrationInitialized; // Indica si se han capturado las posiciones base
// Variables para sub-estados delegados (instrucciones y demo)
bool instructionsActive; // Indica si las instrucciones están activas
bool demoGameActive; // Indica si el juego demo está activo
mode_e instructionsMode; // Modo de las instrucciones activas
bool demoThenInstructions; // Indica si tras la demo hay que mostrar instrucciones
// Inicializa los valores
void init();
// Actualiza las variables del objeto
void update();
// Dibuja el objeto en pantalla
void render();
// Comprueba los eventos
void checkEvents();
// Comprueba las entradas
void checkInput();
// Actualiza el tileado de fondo
void updateBG();
// Cambia el valor de la variable de modo de pantalla completa
void switchFullScreenModeVar();
// Actualiza los elementos de los menus
void updateMenuLabels();
// Aplica las opciones de menu seleccionadas
void applyOptions();
// Ejecuta la parte donde se muestran las instrucciones
void runInstructions(mode_e mode);
// Ejecuta el juego en modo demo
void runDemoGame();
// Modifica las opciones para los controles de los jugadores
bool updatePlayerInputs(int numPlayer);
// Crea el mosaico de fondo del titulo
void createTiledBackground();
// Comprueba cuantos mandos hay conectados para gestionar el menu de opciones
void checkInputDevices();
// Recarga las texturas
void reLoadTextures();
public:
// Constructor
Title(SDL_Renderer *renderer, Screen *screen, Input *input, Asset *asset, options_t *options, Lang *lang, section_t *section);
// Destructor
~Title();
// Bucle para el titulo del juego
void run();
// Ejecuta un frame
void iterate();
// Procesa un evento
void handleEvent(SDL_Event *event);
};