Acabat amb cppcheck

Arreglades les herencies de les classes Sprite
This commit is contained in:
2024-10-13 21:00:33 +02:00
parent 809c10048e
commit 7c876e1d4d
21 changed files with 99 additions and 110 deletions

View File

@@ -1,6 +1,8 @@
#include "animated_sprite.h"
#include <algorithm> // for copy
#include <fstream> // for basic_ostream, operator<<, basic_istream, basic...
#include <iostream> // for cout
#include <iterator> // for back_insert_iterator, back_inserter
#include <sstream> // for basic_stringstream
#include "texture.h" // for Texture

View File

@@ -2,11 +2,11 @@
#include <SDL2/SDL_rect.h> // for SDL_Rect
#include <SDL2/SDL_stdinc.h> // for Uint8
#include <string> // for string, basic_string
#include <memory> // for shared_ptr
#include <string> // for string
#include <vector> // for vector
#include "moving_sprite.h" // for MovingSprite
#include "texture.h"
#include <memory>
class Texture;
struct Animation
{
@@ -37,7 +37,7 @@ protected:
public:
// Constructor
explicit AnimatedSprite(std::shared_ptr<Texture> texture = nullptr, const std::string &file = std::string(), std::vector<std::string>* buffer = nullptr);
explicit AnimatedSprite(std::shared_ptr<Texture> texture = nullptr, const std::string &file = std::string(), std::vector<std::string> *buffer = nullptr);
explicit AnimatedSprite(const AnimatedFile *animation);
// Destructor

View File

@@ -1,10 +1,12 @@
#include "background.h"
#include <SDL2/SDL_blendmode.h> // for SDL_BLENDMODE_BLEND
#include <SDL2/SDL_pixels.h> // for SDL_PIXELFORMAT_RGBA8888
#include <algorithm> // for max, min
#include <string> // for basic_string
#include "asset.h" // for Asset
#include "param.h" // for param
#include <SDL2/SDL_blendmode.h> // for SDL_BLENDMODE_BLEND
#include <SDL2/SDL_pixels.h> // for SDL_PIXELFORMAT_RGBA8888
#include <algorithm> // 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)

View File

@@ -1,12 +1,12 @@
#pragma once
#include <SDL2/SDL_rect.h> // for SDL_Rect
#include <SDL2/SDL_render.h> // for SDL_Renderer, SDL_Texture
#include "utils.h" // for Color
#include "moving_sprite.h"
#include "sprite.h"
#include "texture.h"
#include <memory>
#include <SDL2/SDL_rect.h> // for SDL_Rect
#include <SDL2/SDL_render.h> // for SDL_Renderer, SDL_Texture
#include <memory> // 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

View File

@@ -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++)
{

View File

@@ -3,7 +3,6 @@
#include "sprite.h" // for Sprite
#include <memory> // 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,9 +22,8 @@ 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<int>(kind);
@@ -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

View File

@@ -13,7 +13,7 @@ enum class BulletType
UP,
LEFT,
RIGHT,
NULL_TYPE
NONE
};
// Enumeración para los resultados del movimiento de la bala

View File

@@ -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<std::string> 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<std::string> 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>();
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<Intro>(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<Title>(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

View File

@@ -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();

View File

@@ -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);
}

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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();

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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);

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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);

View File

@@ -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();

View File

@@ -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);