From b61677bda9278060e6d9e333fe51059a971e61da Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Tue, 23 Aug 2022 12:35:56 +0200 Subject: [PATCH] =?UTF-8?q?Empezada=20la=20secci=C3=B3n=20del=20titulo=20d?= =?UTF-8?q?el=20juego?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/game.cpp | 3 +- source/game.h | 8 ++-- source/intro.cpp | 8 +++- source/prog.cpp | 14 ++++++- source/prog.h | 3 ++ source/text.h | 2 +- source/title.cpp | 104 +++++++++++++++++++++++++++++++++++++++++++++++ source/title.h | 49 ++++++++++++++++++++++ 8 files changed, 180 insertions(+), 11 deletions(-) create mode 100644 source/title.cpp create mode 100644 source/title.h diff --git a/source/game.cpp b/source/game.cpp index be7375f..0da74b3 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -1,9 +1,8 @@ #include "game.h" // Constructor -Game::Game(SDL_Renderer *renderer, Asset *asset, Screen *screen, Input *input) +Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input) { - this->renderer = renderer; this->asset = asset; this->screen = screen; diff --git a/source/game.h b/source/game.h index 14a25a9..6042765 100644 --- a/source/game.h +++ b/source/game.h @@ -19,14 +19,14 @@ private: Screen *screen; // Objeto encargado de dibujar en pantalla Input *input; // Objeto Input para gestionar las entradas SDL_Event *eventHandler; // Manejador de eventos + JA_Music music; // Contiene la musica que se reproduce durante el juego Text *debugText; // Objeto para escribir texto con información de debug + Map *map; // Objeto encargado de gestionar el mapeado del juego + Player *player; // Objeto para gestionar el jugador section_t section; // Seccion actual dentro del programa int ticks; // Contador de ticks para ajustar la velocidad del programa int ticksSpeed; // Velocidad a la que se repiten los bucles del programa - Map *map; // Objeto encargado de gestionar el mapeado del juego - Player *player; // Objeto para gestionar el jugador bool debug; // Indica si esta activo el modo de depuración - JA_Music music; // Contiene la musica que se reproduce durante el juego // Actualiza el juego, las variables, comprueba la entrada, etc. void update(); @@ -51,7 +51,7 @@ private: public: // Constructor - Game(SDL_Renderer *renderer, Asset *asset, Screen *screen, Input *input); + Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input); // Destructor ~Game(); diff --git a/source/intro.cpp b/source/intro.cpp index d31e292..30a91ba 100644 --- a/source/intro.cpp +++ b/source/intro.cpp @@ -33,6 +33,9 @@ Intro::~Intro() texture->unload(); delete texture; texture = nullptr; + + delete sprite; + sprite = nullptr; } // Actualiza las variables @@ -57,15 +60,16 @@ void Intro::update() // Cualquier tecla pulsada if ((eventHandler->type == SDL_KEYDOWN) || (eventHandler->type == SDL_JOYBUTTONDOWN)) { - section.name = SECTION_PROG_GAME; + section.name = SECTION_PROG_TITLE; section.subsection = 0; } } sprite->animate(); + // Comprueba si ha terminado la animación de la intro if (sprite->animationIsCompleted()) { - section.name = SECTION_PROG_GAME; + section.name = SECTION_PROG_TITLE; } } } diff --git a/source/prog.cpp b/source/prog.cpp index de07aaa..9e0a565 100644 --- a/source/prog.cpp +++ b/source/prog.cpp @@ -204,6 +204,7 @@ void Prog::setSection(section_t section) this->section = section; } +// Ejecuta la seccion de juego con el logo void Prog::runLogo() { logo = new Logo(renderer, screen, asset); @@ -219,9 +220,18 @@ void Prog::runIntro() delete intro; } +// Ejecuta la seccion de juego con el titulo y los menus +void Prog::runTitle() +{ + title = new Title(renderer, screen, asset, input); + setSection(title->run()); + delete title; +} + +// Ejecuta la seccion de juego donde se juega void Prog::runGame() { - game = new Game(renderer, asset, screen, input); + game = new Game(renderer, screen, asset, input); setSection(game->run()); delete game; } @@ -240,7 +250,7 @@ void Prog::run() runIntro(); break; case SECTION_PROG_TITLE: - // runTitle(); + runTitle(); break; case SECTION_PROG_GAME: runGame(); diff --git a/source/prog.h b/source/prog.h index 5fcf9d4..828ac74 100644 --- a/source/prog.h +++ b/source/prog.h @@ -9,6 +9,8 @@ #include "screen.h" #include "logo.h" #include "intro.h" +#include "title.h" +#include "prog.h" #ifndef PROG_H #define PROG_H @@ -28,6 +30,7 @@ private: Game *game; // Objeto para la sección del juego Intro *intro; // Objeto encargado de gestionar la intro del juego Logo *logo; // Objeto encargado de gestionar el logo del juego + Title *title; // Objeto encargado de gestionar el titulo del juego, con el menu principal section_t section; // Sección y subsección actual del programa; struct options_t *options; // Contiene las opciones del programa diff --git a/source/text.h b/source/text.h index 94e9275..e388276 100644 --- a/source/text.h +++ b/source/text.h @@ -51,7 +51,7 @@ public: // Escribe el texto con sombra void writeShadowed(int x, int y, std::string text, color_t color, Uint8 shadowDistance = 1, int kerning = 1, int lenght = -1); - // Escribe el texto centrado en un punto x y con kerning + // Escribe el texto centrado en un punto x void writeCentered(int x, int y, std::string text, int kerning = 1, int lenght = -1); // Escribe texto con extras diff --git a/source/title.cpp b/source/title.cpp new file mode 100644 index 0000000..cf59154 --- /dev/null +++ b/source/title.cpp @@ -0,0 +1,104 @@ +#include "title.h" + +// Constructor +Title::Title(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input) +{ + // Copia los punteros + this->renderer = renderer; + this->screen = screen; + this->asset = asset; + this->input = input; + + // 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")); + sprite->setCurrentAnimation("menu"); + text = new Text(asset->get("debug.png"), asset->get("debug.txt"), renderer); + + // Inicializa variables + section = {SECTION_PROG_TITLE, 0}; + ticks = 0; + ticksSpeed = 15; +} + +// Destructor +Title::~Title() +{ + renderer = nullptr; + screen = nullptr; + asset = nullptr; + + delete eventHandler; + eventHandler = nullptr; + + texture->unload(); + delete texture; + texture = nullptr; + + delete sprite; + sprite = nullptr; + + delete text; + text = nullptr; +} + +// Actualiza las variables +void Title::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(); + } +} + +// Dibuja en pantalla +void Title::render() +{ + // Prepara para empezar a dibujar en la textura de juego + screen->start(); + + // Limpia la pantalla + screen->clean(); + + // Dibuja los objetos + sprite->render(); + text->writeCentered(160, 200, "@2016,2022 JailDesigner (v0.6)", -1); + + // Vuelca el contenido del renderizador en pantalla + screen->blit(); +} + +// Bucle principal +section_t Title::run() +{ + while (section.name == SECTION_PROG_TITLE) + { + update(); + render(); + } + + return section; +} diff --git a/source/title.h b/source/title.h new file mode 100644 index 0000000..80fda34 --- /dev/null +++ b/source/title.h @@ -0,0 +1,49 @@ +#pragma once + +#include +#include "const.h" +#include "asset.h" +#include "utils.h" +#include "input.h" +#include "screen.h" +#include "text.h" +#include "animatedsprite.h" +#include "jail_audio.h" + +#ifndef TITLE_H +#define TITLE_H + +// Clase Intro +class Title +{ +private: + SDL_Renderer *renderer; // El renderizador de la ventana + Screen *screen; // Objeto encargado de dibujar en pantalla + LTexture *texture; // Textura con los graficos + SDL_Event *eventHandler; // Manejador de eventos + Asset *asset; // Objeto con los ficheros de recurso + Input *input; // Objeto para gestionar las entradas + Text *text; // Objeto para escribir texto en pantalla + AnimatedSprite *sprite; // Sprite para dibujar los graficos de la intro + section_t section; // Estado del bucle principal para saber si continua o se sale + int ticks; // Contador de ticks para ajustar la velocidad del programa + int ticksSpeed; // Velocidad a la que se repiten los bucles del programa + + // Actualiza las variables + void update(); + + // Dibuja en pantalla + void render(); + +public: + // Constructor + Title(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input); + + // Destructor + ~Title(); + + // Bucle principal + section_t run(); +}; + +#endif