diff --git a/source/animated_sprite.cpp b/source/animated_sprite.cpp index 773ffc0..493fd13 100644 --- a/source/animated_sprite.cpp +++ b/source/animated_sprite.cpp @@ -1,6 +1,8 @@ #include "animated_sprite.h" +#include // for copy #include // for basic_ostream, operator<<, basic_istream, basic... #include // for cout +#include // for back_insert_iterator, back_inserter #include // for basic_stringstream #include "texture.h" // for Texture diff --git a/source/animated_sprite.h b/source/animated_sprite.h index 603a822..607a380 100644 --- a/source/animated_sprite.h +++ b/source/animated_sprite.h @@ -2,11 +2,11 @@ #include // for SDL_Rect #include // for Uint8 -#include // for string, basic_string +#include // for shared_ptr +#include // for string #include // for vector #include "moving_sprite.h" // for MovingSprite -#include "texture.h" -#include +class Texture; struct Animation { @@ -37,7 +37,7 @@ protected: public: // Constructor - explicit AnimatedSprite(std::shared_ptr texture = nullptr, const std::string &file = std::string(), std::vector* buffer = nullptr); + explicit AnimatedSprite(std::shared_ptr texture = nullptr, const std::string &file = std::string(), std::vector *buffer = nullptr); explicit AnimatedSprite(const AnimatedFile *animation); // Destructor diff --git a/source/background.cpp b/source/background.cpp index 1c4f2fe..231b3b6 100644 --- a/source/background.cpp +++ b/source/background.cpp @@ -1,10 +1,12 @@ #include "background.h" -#include // for SDL_BLENDMODE_BLEND -#include // for SDL_PIXELFORMAT_RGBA8888 -#include // for max, min -#include // for basic_string -#include "asset.h" // for Asset -#include "param.h" // for param +#include // for SDL_BLENDMODE_BLEND +#include // for SDL_PIXELFORMAT_RGBA8888 +#include // for clamp, max +#include "asset.h" // for Asset +#include "moving_sprite.h" // for MovingSprite +#include "param.h" // for param +#include "sprite.h" // for Sprite +#include "texture.h" // for Texture // Constructor Background::Background(SDL_Renderer *renderer) diff --git a/source/background.h b/source/background.h index 1c943de..0d85efe 100644 --- a/source/background.h +++ b/source/background.h @@ -1,12 +1,12 @@ #pragma once -#include // for SDL_Rect -#include // for SDL_Renderer, SDL_Texture -#include "utils.h" // for Color -#include "moving_sprite.h" -#include "sprite.h" -#include "texture.h" -#include +#include // for SDL_Rect +#include // for SDL_Renderer, SDL_Texture +#include // for unique_ptr, shared_ptr +#include "utils.h" // for Color +class MovingSprite; +class Sprite; +class Texture; /* Esta clase es la encargada de dibujar el fondo que aparece durante la sección diff --git a/source/balloon_formations.cpp b/source/balloon_formations.cpp index 0c22096..08fc9cc 100644 --- a/source/balloon_formations.cpp +++ b/source/balloon_formations.cpp @@ -428,7 +428,6 @@ void BalloonFormations::initBalloonFormations() // #24 - Treinta enemigos BALLOON1. Del centro hacia los extremos. Juntos. Simetricos j = 24; balloon_formation_[j].number_of_balloons = 30; - inc_x = 0; inc_time = 5; for (int i = 0; i < balloon_formation_[j].number_of_balloons; i++) { diff --git a/source/bullet.cpp b/source/bullet.cpp index a759576..f2b1d47 100644 --- a/source/bullet.cpp +++ b/source/bullet.cpp @@ -3,7 +3,6 @@ #include "sprite.h" // for Sprite #include // for std::unique_ptr -// Constantes evaluables en tiempo de compilación constexpr int BULLET_WIDTH = 12; constexpr int BULLET_HEIGHT = 12; constexpr int BULLET_VELY = -3; @@ -23,10 +22,9 @@ Bullet::Bullet(int x, int y, BulletType kind, bool powered_up, int owner, SDL_Re owner_(owner), play_area_(play_area) { - vel_x_ = (kind_ == BulletType::LEFT) ? BULLET_VELX_LEFT - : (kind_ == BulletType::RIGHT) ? BULLET_VELX_RIGHT - : 0; - + vel_x_ = (kind_ == BulletType::LEFT) ? BULLET_VELX_LEFT : (kind_ == BulletType::RIGHT) ? BULLET_VELX_RIGHT + : 0; + auto sprite_offset = powered_up ? 3 : 0; auto kind_index = static_cast(kind); sprite_->setSpriteClip((kind_index + sprite_offset) * width_, 0, sprite_->getWidth(), sprite_->getHeight()); @@ -67,12 +65,12 @@ BulletMoveStatus Bullet::move() bool Bullet::isEnabled() const { - return kind_ != BulletType::NULL_TYPE; + return kind_ != BulletType::NONE; } void Bullet::disable() { - kind_ = BulletType::NULL_TYPE; + kind_ = BulletType::NONE; } int Bullet::getPosX() const diff --git a/source/bullet.h b/source/bullet.h index c71fced..13d960d 100644 --- a/source/bullet.h +++ b/source/bullet.h @@ -13,7 +13,7 @@ enum class BulletType UP, LEFT, RIGHT, - NULL_TYPE + NONE }; // Enumeración para los resultados del movimiento de la bala diff --git a/source/director.cpp b/source/director.cpp index 32dd598..4fb96de 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -41,7 +41,7 @@ #endif // Constructor -Director::Director(int argc, char *argv[]) +Director::Director(int argc, const char *argv[]) { #ifdef RECORDING section::name = section::Name::GAME; @@ -160,8 +160,8 @@ void Director::initInput() Input::get()->bindKey(InputType::RESET, SDL_SCANCODE_F10); // Asigna botones a inputs - const int numGamePads = Input::get()->getNumControllers(); - for (int i = 0; i < numGamePads; ++i) + const int num_gamepads = Input::get()->getNumControllers(); + for (int i = 0; i < num_gamepads; ++i) { // Mando - Movimiento del jugador Input::get()->bindGameControllerButton(i, InputType::UP, SDL_CONTROLLER_BUTTON_DPAD_UP); @@ -188,7 +188,7 @@ void Director::initInput() } // Mapea las asignaciones a los botones desde el archivo de configuración, si se da el caso - for (int i = 0; i < numGamePads; ++i) + for (int i = 0; i < num_gamepads; ++i) for (int index = 0; index < (int)options.controller.size(); ++index) if (Input::get()->getControllerName(i) == options.controller[index].name) { @@ -200,7 +200,7 @@ void Director::initInput() } // Asigna botones a inputs desde otros inputs - for (int i = 0; i < numGamePads; ++i) + for (int i = 0; i < num_gamepads; ++i) { Input::get()->bindGameControllerButton(i, InputType::EXIT, InputType::START); Input::get()->bindGameControllerButton(i, InputType::RESET, InputType::FIRE_CENTER); @@ -213,7 +213,7 @@ void Director::initInput() } // Guarda las asignaciones de botones en las opciones - for (int i = 0; i < numGamePads; ++i) + for (int i = 0; i < num_gamepads; ++i) { options.controller[i].name = Input::get()->getControllerName(i); for (int j = 0; j < (int)options.controller[i].inputs.size(); ++j) @@ -484,7 +484,7 @@ void Director::loadParams(const std::string &file_path) } // Comprueba los parametros del programa -void Director::checkProgramArguments(int argc, char *argv[]) +void Director::checkProgramArguments(int argc, const char *argv[]) { // Establece la ruta del programa executable_path_ = argv[0]; @@ -492,7 +492,7 @@ void Director::checkProgramArguments(int argc, char *argv[]) // Valores por defecto param_file_argument_.clear(); - // Comprueba el resto de parametros + // Comprueba el resto de parámetros for (int i = 1; i < argc; ++i) { if (strcmp(argv[i], "--320x240") == 0) @@ -566,17 +566,15 @@ void Director::createSystemFolder(const std::string &folder) void Director::loadSounds() { // Obtiene la lista con las rutas a los ficheros de sonidos - std::vector list = Asset::get()->getListByType(AssetType::SOUND); + auto list = Asset::get()->getListByType(AssetType::SOUND); sounds_.clear(); for (const auto &l : list) { - const size_t lastIndex = l.find_last_of("/") + 1; - const std::string name = l.substr(lastIndex, std::string::npos); - SoundFile temp; - temp.name = name; // Añade el nombre del fichero - temp.file = JA_LoadSound(l.c_str()); // Carga el fichero de audio - sounds_.push_back(temp); + auto last_index = l.find_last_of('/') + 1; + auto name = l.substr(last_index); + + sounds_.emplace_back(SoundFile{name, JA_LoadSound(l.c_str())}); } } @@ -584,78 +582,69 @@ void Director::loadSounds() void Director::loadMusics() { // Obtiene la lista con las rutas a los ficheros musicales - std::vector list = Asset::get()->getListByType(AssetType::MUSIC); + auto list = Asset::get()->getListByType(AssetType::MUSIC); musics_.clear(); for (const auto &l : list) { - const size_t lastIndex = l.find_last_of("/") + 1; - const std::string name = l.substr(lastIndex, std::string::npos); - MusicFile temp; - temp.name = name; // Añade el nombre del fichero - temp.file = JA_LoadMusic(l.c_str()); // Carga el fichero de audio - musics_.push_back(temp); + auto last_index = l.find_last_of('/') + 1; + auto name = l.substr(last_index); + + musics_.emplace_back(MusicFile{name, JA_LoadMusic(l.c_str())}); } } // Ejecuta la sección con el logo void Director::runLogo() { - auto logo = new Logo(); + auto logo = std::make_unique(); logo->run(); - delete logo; } // Ejecuta la sección con la secuencia de introducción void Director::runIntro() { - auto intro = new Intro(getMusic(musics_, "intro.ogg")); + auto intro = std::make_unique(getMusic(musics_, "intro.ogg")); intro->run(); - delete intro; } -// Ejecuta la sección con el titulo del juego +// Ejecuta la sección con el título del juego void Director::runTitle() { - auto title = new Title(getMusic(musics_, "title.ogg")); + auto title = std::make_unique(getMusic(musics_, "title.ogg")); title->run(); - delete title; } // Ejecuta la sección donde se juega al juego void Director::runGame() { - const auto playerID = section::options == section::Options::GAME_PLAY_1P ? 1 : 2; - constexpr auto currentStage = 0; - auto game = new Game(playerID, currentStage, GAME_MODE_DEMO_OFF, getMusic(musics_, "playing.ogg")); + const auto player_id = section::options == section::Options::GAME_PLAY_1P ? 1 : 2; + constexpr auto current_stage = 0; + auto game = std::make_unique<Game>(player_id, current_stage, GAME_MODE_DEMO_OFF, getMusic(musics_, "playing.ogg")); game->run(); - delete game; } // Ejecuta la sección donde se muestran las instrucciones void Director::runInstructions() { - auto instructions = new Instructions(getMusic(musics_, "title.ogg")); + auto instructions = std::make_unique<Instructions>(getMusic(musics_, "title.ogg")); instructions->run(); - delete instructions; } // Ejecuta la sección donde se muestra la tabla de puntuaciones void Director::runHiScoreTable() { - auto hiScoreTable = new HiScoreTable(getMusic(musics_, "title.ogg")); - hiScoreTable->run(); - delete hiScoreTable; + auto hi_score_table = std::make_unique<HiScoreTable>(getMusic(musics_, "title.ogg")); + hi_score_table->run(); } // Ejecuta el juego en modo demo void Director::runDemoGame() { - const auto playerID = (rand() % 2) + 1; - constexpr auto currentStage = 0; - auto game = new Game(playerID, currentStage, GAME_MODE_DEMO_ON, nullptr); + const auto player_id = (rand() % 2) + 1; + constexpr auto current_stage = 0; + auto game = std::make_unique<Game>(player_id, current_stage, GAME_MODE_DEMO_ON, nullptr); game->run(); - delete game; } int Director::run() @@ -702,8 +691,8 @@ int Director::run() } } - const int returnCode = section::options == section::Options::QUIT_NORMAL ? 0 : 1; - return returnCode; + const int return_code = section::options == section::Options::QUIT_NORMAL ? 0 : 1; + return return_code; } // Obtiene una fichero a partir de un lang::Code diff --git a/source/director.h b/source/director.h index 775ec4b..79afd2d 100644 --- a/source/director.h +++ b/source/director.h @@ -46,7 +46,7 @@ private: void loadMusics(); // Comprueba los parametros del programa - void checkProgramArguments(int argc, char *argv[]); + void checkProgramArguments(int argc, const char *argv[]); // Crea la carpeta del sistema donde guardar datos void createSystemFolder(const std::string &folder); @@ -77,7 +77,7 @@ private: public: // Constructor - Director(int argc, char *argv[]); + Director(int argc, const char *argv[]); // Destructor ~Director(); diff --git a/source/logo.cpp b/source/logo.cpp index 2c11575..4c24398 100644 --- a/source/logo.cpp +++ b/source/logo.cpp @@ -114,12 +114,12 @@ void Logo::updateJAILGAMES() { for (int i = 0; i < (int)jail_sprite_.size(); ++i) { - if (jail_sprite_[i]->getPosX() != dest_.x) + if (jail_sprite_[i]->getIntPosX() != dest_.x) { if (i % 2 == 0) { jail_sprite_[i]->incPosX(-SPEED); - if (jail_sprite_[i]->getPosX() < dest_.x) + if (jail_sprite_[i]->getIntPosX() < dest_.x) { jail_sprite_[i]->setPosX(dest_.x); } @@ -127,7 +127,7 @@ void Logo::updateJAILGAMES() else { jail_sprite_[i]->incPosX(SPEED); - if (jail_sprite_[i]->getPosX() > dest_.x) + if (jail_sprite_[i]->getIntPosX() > dest_.x) { jail_sprite_[i]->setPosX(dest_.x); } diff --git a/source/main.cpp b/source/main.cpp index d075286..137b7e0 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -7,25 +7,23 @@ Actualizando a la versión "Arcade Edition" en 08/05/2024 */ -#include <iostream> // for basic_ostream, char_traits, operator<<, cout -#include <string> // for basic_string, operator<<, string -#include "director.h" // for Director +#include <iostream> // for basic_ostream, char_traits, operator<<, cout +#include <string> // for basic_string, operator<<, string +#include <memory> +#include "director.h" // for Director int main(int argc, char *argv[]) { - std::cout << "Game start" << std::endl; + std::cout << "Game start" << std::endl; - // Crea el objeto Director - Director *director = new Director(argc, argv); + // Crea el objeto Director + auto director = std::make_unique<Director>(argc, const_cast<const char **>(argv)); - // Bucle principal - const auto exit = director->run(); + // Bucle principal + const auto exit = director->run(); - // Destruye el objeto Director - delete director; + const auto endType = exit == 0 ? "keyboard" : "controller"; + std::cout << "\nGame end with " << endType << std::endl; - const auto endType = exit == 0 ? "keyboard" : "controller"; - std::cout << "\nGame end with " << endType << std::endl; - - return exit; + return exit; } diff --git a/source/param.cpp b/source/param.cpp index 59c6165..e4a9aa0 100644 --- a/source/param.cpp +++ b/source/param.cpp @@ -195,7 +195,7 @@ void loadParamsFromFile(const std::string &file_path) auto comment_pos = line.find('#'); if (comment_pos != std::string::npos) { - line = line.substr(0, comment_pos); + line.resize(comment_pos); } // Usa un stream para separar palabras diff --git a/source/scoreboard.h b/source/scoreboard.h index 1500388..f4c948d 100644 --- a/source/scoreboard.h +++ b/source/scoreboard.h @@ -103,7 +103,7 @@ private: // [SINGLETON] Ahora el constructor y el destructor son privados // Constructor - Scoreboard(SDL_Renderer *renderer); + explicit Scoreboard(SDL_Renderer *renderer); // Destructor ~Scoreboard(); diff --git a/source/sprite.cpp b/source/sprite.cpp index 60b26d7..92cb7e5 100644 --- a/source/sprite.cpp +++ b/source/sprite.cpp @@ -21,13 +21,13 @@ void Sprite::render() } // Obten el valor de la variable -int Sprite::getPosX() const +int Sprite::getIntPosX() const { return pos_.x; } // Obten el valor de la variable -int Sprite::getPosY() const +int Sprite::getIntPosY() const { return pos_.y; } diff --git a/source/sprite.h b/source/sprite.h index cc2fffd..bd5300a 100644 --- a/source/sprite.h +++ b/source/sprite.h @@ -26,8 +26,8 @@ public: virtual void render(); // Obten el valor de la variable - int getPosX() const; - int getPosY() const; + int getIntPosX() const; + int getIntPosY() const; int getWidth() const; int getHeight() const; diff --git a/source/text.cpp b/source/text.cpp index af93bcf..de36ea8 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -81,7 +81,7 @@ TextFile LoadTextFile(std::string file_path) } // Constructor -Text::Text(std::string bitmap_file, std::string text_file, SDL_Renderer *renderer) +Text::Text(const std::string &bitmap_file, const std::string &text_file, SDL_Renderer *renderer) { // Carga los offsets desde el fichero auto tf = LoadTextFile(text_file); @@ -105,7 +105,7 @@ Text::Text(std::string bitmap_file, std::string text_file, SDL_Renderer *rendere } // Constructor -Text::Text(std::string text_file, std::shared_ptr<Texture> texture) +Text::Text(const std::string &text_file, std::shared_ptr<Texture> texture) { // Carga los offsets desde el fichero auto tf = LoadTextFile(text_file); diff --git a/source/text.h b/source/text.h index ae68315..d87cf74 100644 --- a/source/text.h +++ b/source/text.h @@ -44,8 +44,8 @@ private: public: // Constructor - Text(std::string bitmap_file, std::string text_file, SDL_Renderer *renderer); - Text(std::string text_file, std::shared_ptr<Texture> texture); + Text(const std::string &bitmap_file, const std::string &text_file, SDL_Renderer *renderer); + Text(const std::string &text_file, std::shared_ptr<Texture> texture); Text(TextFile *text_file, std::shared_ptr<Texture> texture); // Destructor diff --git a/source/texture.cpp b/source/texture.cpp index a661bc5..50b941e 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -56,13 +56,13 @@ Texture::~Texture() // Carga una imagen desde un fichero bool Texture::loadFromFile(const std::string &path) { - const std::string file_name = path.substr(path.find_last_of("\\/") + 1); int req_format = STBI_rgb_alpha; int width, height, orig_format; unsigned char *data = stbi_load(path.c_str(), &width, &height, &orig_format, req_format); if (!data) { #ifdef VERBOSE + const std::string file_name = path.substr(path.find_last_of("\\/") + 1); std::cout << "Loading image failed: " << stbi_failure_reason() << std::endl; #endif exit(1); @@ -70,6 +70,7 @@ bool Texture::loadFromFile(const std::string &path) else { #ifdef VERBOSE + const std::string file_name = path.substr(path.find_last_of("\\/") + 1); std::cout << "Image loaded: " << file_name << std::endl; #endif } @@ -96,7 +97,7 @@ bool Texture::loadFromFile(const std::string &path) SDL_Texture *newTexture = nullptr; // Carga la imagen desde una ruta específica - auto loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom(static_cast<void*>(data), width, height, depth, pitch, pixel_format); + auto loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom(static_cast<void *>(data), width, height, depth, pitch, pixel_format); if (loadedSurface == nullptr) { #ifdef VERBOSE @@ -137,9 +138,9 @@ bool Texture::createBlank(int width, int height, SDL_PixelFormatEnum format, SDL texture_ = SDL_CreateTexture(renderer_, format, access, width, height); if (!texture_) { - #ifdef VERBOSE +#ifdef VERBOSE std::cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << std::endl; - #endif +#endif } else { @@ -312,7 +313,7 @@ void Texture::flipSurface() // Vuelca los datos Uint32 *pixels; int pitch; - SDL_LockTexture(texture_, nullptr, reinterpret_cast<void**>(&pixels), &pitch); + SDL_LockTexture(texture_, nullptr, reinterpret_cast<void **>(&pixels), &pitch); for (int i = 0; i < width_ * height_; ++i) { pixels[i] = palettes_[paletteIndex_][surface_->data[i]]; @@ -344,7 +345,7 @@ std::vector<Uint32> Texture::loadPal(const std::string &file_name) fread(buffer, size, 1, f); fclose(f); - auto pal = LoadPalette(buffer); + const auto pal = LoadPalette(buffer); if (!pal) { return palette; @@ -361,9 +362,9 @@ std::vector<Uint32> Texture::loadPal(const std::string &file_name) } // Añade una paleta a la lista -void Texture::addPalette(std::string path) +void Texture::addPalette(const std::string &path) { - palettes_.push_back(loadPal(path.c_str())); + palettes_.push_back(loadPal(path)); setPaletteColor((int)palettes_.size() - 1, 0, 0x00000000); } diff --git a/source/texture.h b/source/texture.h index 361bcee..990294e 100644 --- a/source/texture.h +++ b/source/texture.h @@ -91,7 +91,7 @@ public: SDL_Texture *getSDLTexture(); // Añade una paleta a la lista - void addPalette(std::string path); + void addPalette(const std::string &path); // Establece un color de la paleta void setPaletteColor(int palette, int index, Uint32 color); diff --git a/source/writer.cpp b/source/writer.cpp index 682762e..c798b74 100644 --- a/source/writer.cpp +++ b/source/writer.cpp @@ -79,7 +79,7 @@ void Writer::setKerning(int value) } // Establece el valor de la variable -void Writer::setCaption(std::string text) +void Writer::setCaption(const std::string &text) { caption_ = text; lenght_ = text.length(); diff --git a/source/writer.h b/source/writer.h index ba03ce4..5f4e556 100644 --- a/source/writer.h +++ b/source/writer.h @@ -48,7 +48,7 @@ public: void setKerning(int value); // Establece el valor de la variable - void setCaption(std::string text); + void setCaption(const std::string &text); // Establece el valor de la variable void setSpeed(int value);