From cdf0b0a69446c26156db11d6b4350d23288ee27d Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sun, 2 Mar 2025 10:41:38 +0100 Subject: [PATCH] demo.cpp a fer la mar --- source/demo.cpp | 195 -------------------------------------------- source/demo.h | 60 -------------- source/director.cpp | 65 +++++++-------- source/game.cpp | 122 ++++++++++++++++++--------- source/game.h | 58 ++++++++++--- source/options.cpp | 2 +- 6 files changed, 158 insertions(+), 344 deletions(-) delete mode 100644 source/demo.cpp delete mode 100644 source/demo.h diff --git a/source/demo.cpp b/source/demo.cpp deleted file mode 100644 index 70f5a2d8..00000000 --- a/source/demo.cpp +++ /dev/null @@ -1,195 +0,0 @@ -#include "demo.h" -#include // for SDL_PollEvent, SDL_Event -#include // for SDL_Rect -#include // for SDL_RenderFillRect, SDL_SetRenderDrawColor -#include // for SDL_GetTicks -#include "asset.h" // for Asset -#include "defines.h" // for BLOCK, PLAY_AREA_WIDTH, GAMECANVAS_CENT... -#include "global_events.h" // for check -#include "global_inputs.h" // for check -#include "item_tracker.h" // for ItemTracker -#include "options.h" // for Options, options, OptionsVideo, Section... -#include "resource.h" // for Resource -#include "room.h" // for Room -#include "scoreboard.h" // for ScoreboardData, Scoreboard -#include "screen.h" // for Screen -#include "text.h" // for TEXT_CENTER, TEXT_COLOR, Text -#include "utils.h" // for Color, stringToColor, colorAreEqual - -// Constructor -Demo::Demo() - : board_(std::make_shared()) -{ - // Inicia algunas variables - board_->ini_clock = SDL_GetTicks(); - rooms_.push_back("04.room"); - rooms_.push_back("54.room"); - rooms_.push_back("20.room"); - rooms_.push_back("09.room"); - rooms_.push_back("05.room"); - rooms_.push_back("11.room"); - rooms_.push_back("31.room"); - rooms_.push_back("44.room"); - - room_index_ = 0; - current_room_ = rooms_[room_index_]; - - // Crea los objetos - ItemTracker::init(); - scoreboard_ = std::make_shared(board_); - room_ = std::make_shared(current_room_, board_); - - // Inicializa el resto de variables - counter_ = 0; - room_time_ = 400; - ticks_ = 0; - board_->lives = 9; - board_->items = 0; - board_->rooms = 1; - board_->jail_is_open = false; - board_->music = true; - setScoreBoardColor(); - - options.section.section = Section::DEMO; - options.section.subsection = Subsection::NONE; -} - -// Destructor -Demo::~Demo() -{ - ItemTracker::destroy(); -} - -// Comprueba los eventos de la cola -void Demo::checkEvents() -{ - SDL_Event event; - while (SDL_PollEvent(&event)) - { - globalEvents::check(event); - } -} - -// Comprueba las entradas -void Demo::checkInput() -{ - globalInputs::check(); -} - -// Bucle para el juego -void Demo::run() -{ - while (options.section.section == Section::DEMO) - { - update(); - checkEvents(); - render(); - } -} - -// Actualiza el juego, las variables, comprueba la entrada, etc. -void Demo::update() -{ - // Comprueba que la diferencia de ticks sea mayor a la velocidad del juego - if (SDL_GetTicks() - ticks_ > GAME_SPEED) - { - // Actualiza el contador de ticks - ticks_ = SDL_GetTicks(); - - // Comprueba las entradas - checkInput(); - - // Actualiza los objetos - room_->update(); - scoreboard_->update(); - checkRoomChange(); - - Screen::get()->update(); - } -} - -// Pinta los objetos en pantalla -void Demo::render() -{ - // Prepara para dibujar el frame - Screen::get()->start(); - - // Dibuja los elementos del juego en orden - room_->renderMap(); - room_->renderEnemies(); - room_->renderItems(); - renderRoomName(); - scoreboard_->render(); - - // Actualiza la pantalla - Screen::get()->render(); -} - -// Escribe el nombre de la pantalla -void Demo::renderRoomName() -{ - // Texto en el centro de la pantalla - SDL_Rect rect = {0, 16 * BLOCK, PLAY_AREA_WIDTH, BLOCK * 2}; - Color color = stringToColor(options.video.palette, "white"); - SDL_SetRenderDrawColor(Screen::get()->getRenderer(), color.r, color.g, color.b, 0xFF); - SDL_RenderFillRect(Screen::get()->getRenderer(), &rect); - - Resource::get()->getText("smb2")->writeDX(TEXT_CENTER | TEXT_COLOR, GAMECANVAS_CENTER_X, 16 * 8 + 4, room_->getName(), 1, room_->getBGColor()); -} - -// Cambia de habitación -bool Demo::changeRoom(const std::string &room_path) -{ - // En las habitaciones los limites tienen la cadena del fichero o un 0 en caso de no limitar con nada - if (room_path != "0") - { - // Verifica que exista el fichero que se va a cargar - if (Asset::get()->get(room_path) != "") - { - // Crea un objeto habitación a partir del fichero - room_ = std::make_shared(room_path, board_); - - // Pone el color del marcador en función del color del borde de la habitación - setScoreBoardColor(); - - return true; - } - } - - return false; -} - -// Comprueba si se ha de cambiar de habitación -void Demo::checkRoomChange() -{ - counter_++; - if (counter_ == room_time_) - { - counter_ = 0; - room_index_++; - if (room_index_ == (int)rooms_.size()) - { - options.section.section = Section::LOGO; - options.section.subsection = Subsection::LOGO_TO_TITLE; - } - else - { - changeRoom(rooms_[room_index_]); - } - } -} - -// Pone el color del marcador en función del color del borde de la habitación -void Demo::setScoreBoardColor() -{ - // Obtiene el color del borde - const Color color = room_->getBorderColor(); - - // Si el color es negro lo cambia a blanco - const Color black_color = stringToColor(options.video.palette, "black"); - board_->color = colorAreEqual(color, black_color) ? stringToColor(options.video.palette, "white") : color; - - // Si el color es negro brillante lo cambia a blanco - const Color bright_blac_color = stringToColor(options.video.palette, "bright_black"); - board_->color = colorAreEqual(color, bright_blac_color) ? stringToColor(options.video.palette, "white") : color; -} \ No newline at end of file diff --git a/source/demo.h b/source/demo.h deleted file mode 100644 index 5531ac5f..00000000 --- a/source/demo.h +++ /dev/null @@ -1,60 +0,0 @@ -#pragma once - -#include // for Uint32 -#include // for shared_ptr -#include // for string -#include // for vector -class Room; // lines 14-14 -class Scoreboard; -struct ScoreboardData; - -class Demo -{ -private: - // Objetos y punteros - std::shared_ptr room_; // Objeto encargado de gestionar cada habitación del juego - std::shared_ptr scoreboard_; // Objeto encargado de gestionar el marcador - - // Variables - Uint32 ticks_; // Contador de ticks para ajustar la velocidad del programa - std::string current_room_; // Fichero de la habitación actual - std::shared_ptr board_; // Estructura con los datos del marcador - int counter_; // Contador para el modo demo - int room_time_; // Tiempo que se muestra cada habitacion - int room_index_; // Indice para el vector de habitaciones - std::vector rooms_; // Listado con los mapas de la demo - - // Actualiza el juego, las variables, comprueba la entrada, etc. - void update(); - - // Pinta los objetos en pantalla - void render(); - - // Comprueba los eventos de la cola - void checkEvents(); - - // Comprueba las entradas - void checkInput(); - - // Escribe el nombre de la pantalla - void renderRoomName(); - - // Cambia de habitación - bool changeRoom(const std::string &file); - - // Comprueba si se ha de cambiar de habitación - void checkRoomChange(); - - // Pone el color del marcador en función del color del borde de la habitación - void setScoreBoardColor(); - -public: - // Constructor - Demo(); - - // Destructor - ~Demo(); - - // Bucle para el juego - void run(); -}; diff --git a/source/director.cpp b/source/director.cpp index 75ae0bfa..8f1d3cbb 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -22,7 +22,6 @@ #include "credits.h" // for Credits #include "debug.h" // for Debug #include "defines.h" // for WINDOW_CAPTION, borderColor -#include "demo.h" // for Demo #include "ending.h" // for Ending #include "ending2.h" // for Ending2 #include "game.h" // for Game @@ -110,41 +109,37 @@ Director::~Director() // Comprueba los parametros del programa std::string Director::checkProgramArguments(int argc, const char *argv[]) { - // Comprueba los parametros - for (int i = 1; i < argc; ++i) - { - if (strcmp(argv[i], "--console") == 0) - { - options.console = true; - } + // Iterar sobre los argumentos del programa + for (int i = 1; i < argc; ++i) + { + std::string argument(argv[i]); - else if (strcmp(argv[i], "--infiniteLives") == 0) - { - options.cheats.infinite_lives = Cheat::CheatState::ENABLED; - } + if (argument == "--console") + { + options.console = true; + } + else if (argument == "--infiniteLives") + { + options.cheats.infinite_lives = Cheat::CheatState::ENABLED; + } + else if (argument == "--invincible") + { + options.cheats.invincible = Cheat::CheatState::ENABLED; + } + else if (argument == "--jailEnabled") + { + options.cheats.jail_is_open = Cheat::CheatState::ENABLED; + } + else if (argument == "--altSkin") + { + options.cheats.alternate_skin = Cheat::CheatState::ENABLED; + } + } - else if (strcmp(argv[i], "--invincible") == 0) - { - options.cheats.invincible = Cheat::CheatState::ENABLED; - ; - } - - else if (strcmp(argv[i], "--jailEnabled") == 0) - { - options.cheats.jail_is_open = Cheat::CheatState::ENABLED; - ; - } - - else if (strcmp(argv[i], "--altSkin") == 0) - { - options.cheats.alternate_skin = Cheat::CheatState::ENABLED; - ; - } - } - - return argv[0]; + return argv[0]; } + // Crea la carpeta del sistema donde guardar datos void Director::createSystemFolder(const std::string &folder) { @@ -774,8 +769,8 @@ void Director::runCredits() // Ejecuta la seccion de la demo, donde se ven pantallas del juego void Director::runDemo() { - auto demo = std::make_unique(); - demo->run(); + auto game = std::make_unique(GameMode::DEMO); + game->run(); } // Ejecuta la seccion del final del juego @@ -803,7 +798,7 @@ void Director::runGameOver() void Director::runGame() { JA_StopMusic(); - auto game = std::make_unique(); + auto game = std::make_unique(GameMode::GAME); game->run(); } diff --git a/source/game.cpp b/source/game.cpp index 74a8f0f1..691d75dd 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -1,37 +1,38 @@ #include "game.h" -#include // for SDL_BLENDMODE_BLEND -#include // for SDL_GetError -#include // for SDL_PIXELFORMAT_RGBA8888 -#include // for SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_... -#include // for SDL_GetTicks -#include // for basic_ostream, operator<<, cout, endl -#include // for vector -#include "asset.h" // for Asset -#include "cheevos.h" // for Cheevos -#include "debug.h" // for Debug -#include "defines.h" // for BLOCK, PLAY_AREA_HEIGHT, GAMECANVAS_... -#include "global_events.h" // for check -#include "global_inputs.h" // for check -#include "input.h" // for Input, InputAction, REPEAT_FALSE -#include "item_tracker.h" // for ItemTracker -#include "jail_audio.h" // for JA_PauseMusic, JA_PlaySound, JA_Resu... -#include "notifier.h" // for Notifier, NotificationText -#include "options.h" // for Options, options, Cheat, OptionsVideo -#include "resource.h" // for ResourceRoom, Resource -#include "room.h" // for Room, RoomData -#include "room_tracker.h" // for RoomTracker -#include "scoreboard.h" // for ScoreboardData, Scoreboard -#include "screen.h" // for Screen -#include "stats.h" // for Stats -#include "text.h" // for Text, TEXT_CENTER, TEXT_COLOR -#include "utils.h" // for Color, stringToColor, colorAreEqual +#include // for SDL_BLENDMODE_BLEND +#include // for SDL_GetError +#include // for SDL_PIXELFORMAT_RGBA8888 +#include // for SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_... +#include // for SDL_GetTicks +#include // for basic_ostream, operator<<, cout, endl +#include // for vector +#include "asset.h" // for Asset +#include "cheevos.h" // for Cheevos +#include "debug.h" // for Debug +#include "defines.h" // for BLOCK, PLAY_AREA_HEIGHT, GAMECANVAS_... +#include "global_events.h" // for check +#include "global_inputs.h" // for check +#include "input.h" // for Input, InputAction, REPEAT_FALSE +#include "item_tracker.h" // for ItemTracker +#include "jail_audio.h" // for JA_PauseMusic, JA_PlaySound, JA_Resu... +#include "notifier.h" // for Notifier, NotificationText +#include "options.h" // for Options, options, Cheat, OptionsVideo +#include "resource.h" // for ResourceRoom, Resource +#include "room.h" // for Room, RoomData +#include "room_tracker.h" // for RoomTracker +#include "scoreboard.h" // for ScoreboardData, Scoreboard +#include "screen.h" // for Screen +#include "stats.h" // for Stats +#include "text.h" // for Text, TEXT_CENTER, TEXT_COLOR +#include "utils.h" // for Color, stringToColor, colorAreEqual // Constructor -Game::Game() +Game::Game(GameMode mode) : board_(std::make_shared(0, 9, 0, true, Color(0, 0, 0), SDL_GetTicks(), options.cheats.jail_is_open == Cheat::CheatState::ENABLED)), scoreboard_(std::make_shared(board_)), room_tracker_(std::make_shared()), stats_(std::make_shared(Asset::get()->get("stats.csv"), Asset::get()->get("stats_buffer.csv"))), + mode_(mode), #ifdef DEBUG current_room_("03.room"), spawn_point_(PlayerSpawn(25 * BLOCK, 13 * BLOCK, 0, 0, 0, PlayerState::STANDING, SDL_FLIP_HORIZONTAL)) @@ -46,6 +47,7 @@ Game::Game() // Crea objetos e inicializa variables ItemTracker::init(); + DEMO_init(); room_ = std::make_shared(current_room_, board_); initPlayer(spawn_point_, room_); initStats(); @@ -57,7 +59,7 @@ Game::Game() Cheevos::get()->enable(!options.cheats.enabled()); // Deshabilita los logros si hay trucos activados Cheevos::get()->clearUnobtainableState(); - options.section.section = Section::GAME; + options.section.section = (mode_ == GameMode::GAME) ? Section::GAME : Section::DEMO; options.section.subsection = Subsection::NONE; } @@ -108,7 +110,7 @@ void Game::run() JA_PauseMusic(); } - while (options.section.section == Section::GAME) + while (options.section.section == Section::GAME || options.section.section == Section::DEMO) { update(); checkEvents(); @@ -136,15 +138,19 @@ void Game::update() // Actualiza los objetos room_->update(); - player_->update(); - checkPlayerOnBorder(); - checkPlayerAndItems(); - checkPlayerAndEnemies(); - checkIfPlayerIsAlive(); - checkGameOver(); - checkEndGame(); - checkRestoringJail(); - checkSomeCheevos(); + if (mode_ == GameMode::GAME) + { + player_->update(); + checkPlayerOnBorder(); + checkPlayerAndItems(); + checkPlayerAndEnemies(); + checkIfPlayerIsAlive(); + checkGameOver(); + checkEndGame(); + checkRestoringJail(); + checkSomeCheevos(); + } + DEMO_checkRoomChange(); scoreboard_->update(); updateBlackScreen(); @@ -162,13 +168,15 @@ void Game::render() { // Prepara para dibujar el frame Screen::get()->start(); - // test_surface_->render(0, 0, 10, 10, 64, 64); // Dibuja los elementos del juego en orden room_->renderMap(); room_->renderEnemies(); room_->renderItems(); - player_->render(); + if (mode_ == GameMode::GAME) + { + player_->render(); + } renderRoomName(); scoreboard_->render(); renderBlackScreen(); @@ -669,4 +677,38 @@ void Game::createRoomNameTexture() // Establece el destino de la textura room_name_rect_ = {0, PLAY_AREA_HEIGHT, GAMECANVAS_WIDTH, text->getCharacterSize() * 2}; +} + +// DEMO MODE: Inicializa las variables para el modo demo +void Game::DEMO_init() +{ + if (mode_ == GameMode::DEMO) + { + demo_ = DemoData(0, 400, 0, {"04.room", "54.room", "20.room", "09.room", "05.room", "11.room", "31.room", "44.room"}); + + current_room_ = demo_.rooms.front(); + } +} + +// DEMO MODE: Comprueba si se ha de cambiar de habitación +void Game::DEMO_checkRoomChange() +{ + if (mode_ == GameMode::DEMO) + { + demo_.counter++; + if (demo_.counter == demo_.room_time) + { + demo_.counter = 0; + demo_.room_index++; + if (demo_.room_index == (int)demo_.rooms.size()) + { + options.section.section = Section::LOGO; + options.section.subsection = Subsection::LOGO_TO_TITLE; + } + else + { + changeRoom(demo_.rooms[demo_.room_index]); + } + } + } } \ No newline at end of file diff --git a/source/game.h b/source/game.h index cfb92a13..d1007b7b 100644 --- a/source/game.h +++ b/source/game.h @@ -1,21 +1,45 @@ #pragma once -#include // for SDL_Event -#include // for SDL_Rect -#include // for SDL_Texture -#include // for Uint32 -#include // for shared_ptr -#include // for string -#include "player.h" // for PlayerSpawn -class Room; // lines 17-17 -class RoomTracker; // lines 18-18 -class Scoreboard; -class Stats; // lines 20-20 -struct ScoreboardData; +#include // for SDL_Event +#include // for SDL_Rect +#include // for SDL_Texture +#include // for Uint32 +#include // for shared_ptr +#include // for string +#include // for vector +#include "player.h" // for PlayerSpawn +class Room; // lines 10-10 +class RoomTracker; // lines 11-11 +class Scoreboard; // lines 12-12 +class Stats; // lines 13-13 +struct ScoreboardData; // lines 14-14 + +enum class GameMode +{ + DEMO, + GAME +}; class Game { private: + // Estructuras + struct DemoData + { + int counter; // Contador para el modo demo + int room_time; // Tiempo que se muestra cada habitación + int room_index; // Índice para el vector de habitaciones + std::vector rooms; // Listado con los mapas de la demo + + // Constructor por defecto + DemoData() + : counter(0), room_time(0), room_index(0), rooms({}) {} + + // Constructor parametrizado + DemoData(int counter, int room_time, int room_index, const std::vector &rooms) + : counter(counter), room_time(room_time), room_index(room_index), rooms(rooms) {} + }; + // Objetos y punteros std::shared_ptr board_; // Estructura con los datos del marcador std::shared_ptr scoreboard_; // Objeto encargado de gestionar el marcador @@ -26,6 +50,8 @@ private: SDL_Texture *room_name_texture_; // Textura para escribir el nombre de la habitación // Variables + GameMode mode_; // Modo del juego + DemoData demo_; // Variables para el modo demo Uint32 ticks_ = 0; // Contador de ticks para ajustar la velocidad del programa std::string current_room_; // Fichero de la habitación actual PlayerSpawn spawn_point_; // Lugar de la habitación donde aparece el jugador @@ -124,9 +150,15 @@ private: // Crea la textura para poner el nombre de la habitación void createRoomNameTexture(); + // DEMO MODE: Inicializa las variables para el modo demo + void DEMO_init(); + + // DEMO MODE: Comprueba si se ha de cambiar de habitación + void DEMO_checkRoomChange(); + public: // Constructor - Game(); + Game(GameMode mode); // Destructor ~Game(); diff --git a/source/options.cpp b/source/options.cpp index f8a5f785..665ebf4a 100644 --- a/source/options.cpp +++ b/source/options.cpp @@ -21,7 +21,7 @@ void initOptions() options = Options(); #ifdef DEBUG - options.section = SectionState(Section::TITLE, Subsection::NONE); + options.section = SectionState(Section::DEMO, Subsection::NONE); options.console = true; #else options.section = SectionState(Section::LOGO, Subsection::LOGO_TO_INTRO);