diff --git a/source/animated_sprite.cpp b/source/animated_sprite.cpp index 493fd13..d3618d3 100644 --- a/source/animated_sprite.cpp +++ b/source/animated_sprite.cpp @@ -7,11 +7,10 @@ #include "texture.h" // for Texture // Carga la animación desde un fichero -AnimatedFile loadAnimationFromFile(std::shared_ptr texture, std::string file_path) +std::vector loadAnimationFromFile(std::shared_ptr texture, std::string file_path) { // Inicializa variables - AnimatedFile af; - af.texture = texture; + std::vector animations; auto frames_per_row = 0; auto frame_width = 0; auto frame_height = 0; @@ -35,14 +34,14 @@ AnimatedFile loadAnimationFromFile(std::shared_ptr texture, std::string // Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación if (line == "[animation]") { - Animation buffer; - buffer.counter = 0; - buffer.current_frame = 0; - buffer.completed = false; - buffer.name.clear(); - buffer.speed = 5; - buffer.loop = 0; - buffer.frames.clear(); + Animation animation; + animation.counter = 0; + animation.current_frame = 0; + animation.completed = false; + animation.name.clear(); + animation.speed = 5; + animation.loop = 0; + animation.frames.clear(); do { @@ -56,17 +55,17 @@ AnimatedFile loadAnimationFromFile(std::shared_ptr texture, std::string { if (line.substr(0, pos) == "name") { - buffer.name = line.substr(pos + 1, line.length()); + animation.name = line.substr(pos + 1, line.length()); } else if (line.substr(0, pos) == "speed") { - buffer.speed = std::stoi(line.substr(pos + 1, line.length())); + animation.speed = std::stoi(line.substr(pos + 1, line.length())); } else if (line.substr(0, pos) == "loop") { - buffer.loop = std::stoi(line.substr(pos + 1, line.length())); + animation.loop = std::stoi(line.substr(pos + 1, line.length())); } else if (line.substr(0, pos) == "frames") @@ -81,7 +80,7 @@ AnimatedFile loadAnimationFromFile(std::shared_ptr texture, std::string const auto num_tile = std::stoi(tmp) > max_tiles ? 0 : std::stoi(tmp); rect.x = (num_tile % frames_per_row) * frame_width; rect.y = (num_tile / frames_per_row) * frame_height; - buffer.frames.push_back(rect); + animation.frames.push_back(rect); } } @@ -95,7 +94,7 @@ AnimatedFile loadAnimationFromFile(std::shared_ptr texture, std::string } while (line != "[/animation]"); // Añade la animación al vector de animaciones - af.animations.push_back(buffer); + animations.push_back(animation); } // En caso contrario se parsea el fichero para buscar las variables y los valores @@ -156,37 +155,35 @@ AnimatedFile loadAnimationFromFile(std::shared_ptr texture, std::string #endif } - return af; + return animations; } // Constructor -AnimatedSprite::AnimatedSprite(std::shared_ptr texture, const std::string &file, std::vector *buffer) +AnimatedSprite::AnimatedSprite(std::shared_ptr texture, const std::string &file_path) : MovingSprite(texture), current_animation_(0) { // Carga las animaciones - if (!file.empty()) + if (!file_path.empty()) { - AnimatedFile as = loadAnimationFromFile(texture, file); - - // Copia los datos de las animaciones - std::copy(as.animations.begin(), as.animations.end(), std::back_inserter(animations_)); + animations_ = loadAnimationFromFile(texture, file_path); } +} - else if (buffer) +AnimatedSprite::AnimatedSprite(std::shared_ptr texture, std::vector *animations) + : MovingSprite(texture), + current_animation_(0) +{ + if (animations) { - loadFromVector(buffer); + loadFromVector(animations); } } // Constructor -AnimatedSprite::AnimatedSprite(const AnimatedFile *animation) - : MovingSprite(animation->texture), - current_animation_(0) -{ - // Copia los datos de las animaciones - std::copy(animation->animations.begin(), animation->animations.end(), std::back_inserter(animations_)); -} +AnimatedSprite::AnimatedSprite(std::shared_ptr texture) + : MovingSprite(texture), + current_animation_(0) {} // Destructor AnimatedSprite::~AnimatedSprite() @@ -356,14 +353,14 @@ bool AnimatedSprite::loadFromVector(std::vector *source) // Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación if (line == "[animation]") { - Animation buffer; - buffer.counter = 0; - buffer.current_frame = 0; - buffer.completed = false; - buffer.name.clear(); - buffer.speed = 5; - buffer.loop = 0; - buffer.frames.clear(); + Animation animation; + animation.counter = 0; + animation.current_frame = 0; + animation.completed = false; + animation.name.clear(); + animation.speed = 5; + animation.loop = 0; + animation.frames.clear(); do { @@ -379,17 +376,17 @@ bool AnimatedSprite::loadFromVector(std::vector *source) { if (line.substr(0, pos) == "name") { - buffer.name = line.substr(pos + 1, line.length()); + animation.name = line.substr(pos + 1, line.length()); } else if (line.substr(0, pos) == "speed") { - buffer.speed = std::stoi(line.substr(pos + 1, line.length())); + animation.speed = std::stoi(line.substr(pos + 1, line.length())); } else if (line.substr(0, pos) == "loop") { - buffer.loop = std::stoi(line.substr(pos + 1, line.length())); + animation.loop = std::stoi(line.substr(pos + 1, line.length())); } else if (line.substr(0, pos) == "frames") @@ -404,7 +401,7 @@ bool AnimatedSprite::loadFromVector(std::vector *source) const int num_tile = std::stoi(tmp) > max_tiles ? 0 : std::stoi(tmp); rect.x = (num_tile % frames_per_row) * frame_width; rect.y = (num_tile / frames_per_row) * frame_height; - buffer.frames.push_back(rect); + animation.frames.push_back(rect); } } @@ -419,7 +416,7 @@ bool AnimatedSprite::loadFromVector(std::vector *source) } while (line != "[/animation]"); // Añade la animación al vector de animaciones - animations_.push_back(buffer); + animations_.push_back(animation); } // En caso contrario se parsea el fichero para buscar las variables y los valores diff --git a/source/animated_sprite.h b/source/animated_sprite.h index 5a4035a..f12a9b1 100644 --- a/source/animated_sprite.h +++ b/source/animated_sprite.h @@ -19,14 +19,8 @@ struct Animation int counter; // Contador para las animaciones }; -struct AnimatedFile -{ - std::vector animations; // Vector con las diferentes animaciones - std::shared_ptr texture; // Textura con los graficos para el sprite -}; - // Carga la animación desde un fichero -AnimatedFile loadAnimationFromFile(std::shared_ptr texture, std::string filePath); +std::vector loadAnimationFromFile(std::shared_ptr texture, std::string filePath); class AnimatedSprite : public MovingSprite { @@ -35,16 +29,20 @@ protected: std::vector animations_; // Vector con las diferentes animaciones int current_animation_; // Animacion activa + // Calcula el frame correspondiente a la animación actual + void animate(); + public: // Constructor - explicit AnimatedSprite(std::shared_ptr texture = nullptr, const std::string &file = std::string(), std::vector *buffer = nullptr); - explicit AnimatedSprite(const AnimatedFile *animation); + AnimatedSprite(std::shared_ptr texture, const std::string &file_path); + AnimatedSprite(std::shared_ptr texture, std::vector *animations); + explicit AnimatedSprite(std::shared_ptr texture); // Destructor virtual ~AnimatedSprite(); - // Calcula el frame correspondiente a la animación actual - void animate(); + // Actualiza las variables del objeto + void update() override; // Obtiene el número de frames de la animación actual int getNumFrames(); @@ -84,9 +82,6 @@ public: void setCurrentAnimation(const std::string &name = "default"); void setCurrentAnimation(int index = 0); - // Actualiza las variables del objeto - void update() override; - // OLD - Establece el rectangulo para un frame de una animación void setAnimationFrames(Uint8 index_animation, Uint8 index_frame, int x, int y, int w, int h); diff --git a/source/background.cpp b/source/background.cpp index 231b3b6..1a6e0a4 100644 --- a/source/background.cpp +++ b/source/background.cpp @@ -1,12 +1,12 @@ #include "background.h" -#include // for SDL_BLENDMODE_BLEND -#include // for SDL_PIXELFORMAT_RGBA8888 -#include // for clamp, max -#include "asset.h" // for Asset +#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 +#include "param.h" // for param +#include "sprite.h" // for Sprite +#include "texture.h" // for Texture // Constructor Background::Background(SDL_Renderer *renderer) @@ -19,55 +19,71 @@ Background::Background(SDL_Renderer *renderer) { // Inicializa variables - gradient_number_ = 0; - alpha_ = 0; - clouds_speed_ = 0; - transition_ = 0; - counter_ = 0; - - rect_ = {0, 0, gradients_texture_->getWidth() / 2, gradients_texture_->getHeight() / 2}; - src_rect_ = {0, 0, 320, 240}; - dst_rect_ = {0, 0, 320, 240}; - - base_ = rect_.h; - color_ = {param.background.attenuate_color.r, param.background.attenuate_color.g, param.background.attenuate_color.b}; - alpha_color_text_ = alpha_color_text_temp_ = param.background.attenuate_alpha; - - gradient_rect_[0] = {0, 0, rect_.w, rect_.h}; - gradient_rect_[1] = {rect_.w, 0, rect_.w, rect_.h}; - gradient_rect_[2] = {0, rect_.h, rect_.w, rect_.h}; - gradient_rect_[3] = {rect_.w, rect_.h, rect_.w, rect_.h}; - - const int top_clouds_texture_height = top_clouds_texture_->getHeight() / 4; - const int bottom_clouds_texture_height = bottom_clouds_texture_->getHeight() / 4; - for (int i = 0; i < 4; ++i) { - top_clouds_rect_[i] = {0, i * top_clouds_texture_height, top_clouds_texture_->getWidth(), top_clouds_texture_height}; - bottom_clouds_rect_[i] = {0, i * bottom_clouds_texture_height, bottom_clouds_texture_->getWidth(), bottom_clouds_texture_height}; + gradient_number_ = 0; + alpha_ = 0; + clouds_speed_ = 0; + transition_ = 0; + counter_ = 0; + + rect_ = {0, 0, gradients_texture_->getWidth() / 2, gradients_texture_->getHeight() / 2}; + src_rect_ = {0, 0, 320, 240}; + dst_rect_ = {0, 0, 320, 240}; + + base_ = rect_.h; + color_ = {param.background.attenuate_color.r, param.background.attenuate_color.g, param.background.attenuate_color.b}; + alpha_color_text_ = alpha_color_text_temp_ = param.background.attenuate_alpha; + + gradient_rect_[0] = {0, 0, rect_.w, rect_.h}; + gradient_rect_[1] = {rect_.w, 0, rect_.w, rect_.h}; + gradient_rect_[2] = {0, rect_.h, rect_.w, rect_.h}; + gradient_rect_[3] = {rect_.w, rect_.h, rect_.w, rect_.h}; + + const int top_clouds_texture_height = top_clouds_texture_->getHeight() / 4; + const int bottom_clouds_texture_height = bottom_clouds_texture_->getHeight() / 4; + for (int i = 0; i < 4; ++i) + { + top_clouds_rect_[i] = {0, i * top_clouds_texture_height, top_clouds_texture_->getWidth(), top_clouds_texture_height}; + bottom_clouds_rect_[i] = {0, i * bottom_clouds_texture_height, bottom_clouds_texture_->getWidth(), bottom_clouds_texture_height}; + } } // Crea los sprites - const int top_clouds_y = base_ - 165; - const int bottom_clouds_y = base_ - 101; - constexpr float top_clouds_speed = 0.1f; - constexpr float bottom_clouds_speed = 0.05f; - top_clouds_sprite_a_ = std::make_unique(0, top_clouds_y, rect_.w, top_clouds_texture_->getHeight(), -top_clouds_speed, 0.0f, 0.0f, 0.0f, top_clouds_texture_); - top_clouds_sprite_b_ = std::make_unique(rect_.w, top_clouds_y, rect_.w, top_clouds_texture_->getHeight(), -top_clouds_speed, 0.0f, 0.0f, 0.0f, top_clouds_texture_); + { + const int top_clouds_y = base_ - 165; + const int bottom_clouds_y = base_ - 101; - bottom_clouds_sprite_a_ = std::make_unique(0, bottom_clouds_y, rect_.w, bottom_clouds_texture_->getHeight(), -bottom_clouds_speed, 0.0f, 0.0f, 0.0f, bottom_clouds_texture_); - bottom_clouds_sprite_b_ = std::make_unique(rect_.w, bottom_clouds_y, rect_.w, bottom_clouds_texture_->getHeight(), -bottom_clouds_speed, 0.0f, 0.0f, 0.0f, bottom_clouds_texture_); + top_clouds_sprite_a_ = std::make_unique(top_clouds_texture_, (SDL_Rect){0, top_clouds_y, rect_.w, top_clouds_texture_->getHeight()}); + top_clouds_sprite_b_ = std::make_unique(top_clouds_texture_, (SDL_Rect){rect_.w, top_clouds_y, rect_.w, top_clouds_texture_->getHeight()}); - buildings_sprite_ = std::make_unique(0, 0, buildings_texture_->getWidth(), buildings_texture_->getHeight(), buildings_texture_); - gradient_sprite_ = std::make_unique(0, 0, rect_.w, rect_.h, gradients_texture_); - grass_sprite_ = std::make_unique(0, 0, grass_texture_->getWidth(), grass_texture_->getHeight() / 2, grass_texture_); + bottom_clouds_sprite_a_ = std::make_unique(bottom_clouds_texture_, (SDL_Rect){0, bottom_clouds_y, rect_.w, bottom_clouds_texture_->getHeight()}); + bottom_clouds_sprite_b_ = std::make_unique(bottom_clouds_texture_, (SDL_Rect){rect_.w, bottom_clouds_y, rect_.w, bottom_clouds_texture_->getHeight()}); + + buildings_sprite_ = std::make_unique(buildings_texture_, 0, 0, buildings_texture_->getWidth(), buildings_texture_->getHeight()); + gradient_sprite_ = std::make_unique(gradients_texture_, 0, 0, rect_.w, rect_.h); + grass_sprite_ = std::make_unique(grass_texture_, 0, 0, grass_texture_->getWidth(), grass_texture_->getHeight() / 2); + } // Inicializa objetos - top_clouds_sprite_a_->setSpriteClip(0, 0, top_clouds_texture_->getWidth(), top_clouds_texture_->getHeight()); - top_clouds_sprite_b_->setSpriteClip(0, 0, top_clouds_texture_->getWidth(), top_clouds_texture_->getHeight()); - bottom_clouds_sprite_a_->setSpriteClip(0, 0, bottom_clouds_texture_->getWidth(), bottom_clouds_texture_->getHeight()); - bottom_clouds_sprite_b_->setSpriteClip(0, 0, bottom_clouds_texture_->getWidth(), bottom_clouds_texture_->getHeight()); - buildings_sprite_->setPosY(base_ - buildings_sprite_->getHeight()); - grass_sprite_->setPosY(base_ - grass_sprite_->getHeight()); + { + constexpr float top_clouds_speed = 0.1f; + constexpr float bottom_clouds_speed = 0.05f; + + top_clouds_sprite_a_->setVelX(-top_clouds_speed); + top_clouds_sprite_a_->setSpriteClip(0, 0, top_clouds_texture_->getWidth(), top_clouds_texture_->getHeight()); + + top_clouds_sprite_b_->setVelX(-top_clouds_speed); + top_clouds_sprite_b_->setSpriteClip(0, 0, top_clouds_texture_->getWidth(), top_clouds_texture_->getHeight()); + + bottom_clouds_sprite_a_->setVelX(-bottom_clouds_speed); + bottom_clouds_sprite_a_->setSpriteClip(0, 0, bottom_clouds_texture_->getWidth(), bottom_clouds_texture_->getHeight()); + + bottom_clouds_sprite_b_->setVelX(-bottom_clouds_speed); + bottom_clouds_sprite_b_->setSpriteClip(0, 0, bottom_clouds_texture_->getWidth(), bottom_clouds_texture_->getHeight()); + + buildings_sprite_->setPosY(base_ - buildings_sprite_->getHeight()); + grass_sprite_->setPosY(base_ - grass_sprite_->getHeight()); + } // Crea la textura para componer el fondo canvas_ = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, rect_.w, rect_.h); @@ -297,10 +313,10 @@ void Background::updateClouds() bottom_clouds_sprite_b_->setVelX(clouds_speed_ / 2); // Mueve las nubes - top_clouds_sprite_a_->move(); - top_clouds_sprite_b_->move(); - bottom_clouds_sprite_a_->move(); - bottom_clouds_sprite_b_->move(); + top_clouds_sprite_a_->update(); + top_clouds_sprite_b_->update(); + bottom_clouds_sprite_a_->update(); + bottom_clouds_sprite_b_->update(); // Calcula el offset de las nubes if (top_clouds_sprite_a_->getPosX() < -top_clouds_sprite_a_->getWidth()) diff --git a/source/background.h b/source/background.h index 0d85efe..addf56c 100644 --- a/source/background.h +++ b/source/background.h @@ -1,9 +1,9 @@ #pragma once -#include // for SDL_Rect -#include // for SDL_Renderer, SDL_Texture -#include // for unique_ptr, shared_ptr -#include "utils.h" // for Color +#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; diff --git a/source/balloon.cpp b/source/balloon.cpp index f74914f..26c9813 100644 --- a/source/balloon.cpp +++ b/source/balloon.cpp @@ -1,6 +1,6 @@ #include "balloon.h" #include // for abs -#include "animated_sprite.h" // for AnimatedSprite +#include "animated_sprite.h" // for SpriteAnimated #include "moving_sprite.h" // for MovingSprite #include "param.h" // for param #include "sprite.h" // for Sprite @@ -8,7 +8,7 @@ // Constructor Balloon::Balloon(float x, float y, Uint8 kind, float vel_x, float speed, Uint16 creation_timer, std::shared_ptr texture, std::vector *animation) - : sprite_(std::make_unique(texture, "", animation)), + : sprite_(std::make_unique(texture, animation)), pos_x_(x), pos_y_(y), vel_x_(vel_x), @@ -217,7 +217,7 @@ Balloon::Balloon(float x, float y, Uint8 kind, float vel_x, float speed, Uint16 menace_ = 0; // Añade rotación al sprite_ - sprite_->setRotate(false); + sprite_->disableRotate(); sprite_->setRotateSpeed(0); vel_x_ > 0.0f ? sprite_->setRotateAmount(2.0) : sprite_->setRotateAmount(-2.0); @@ -306,7 +306,7 @@ void Balloon::render() if (kind_ == POWER_BALL && !isBeingCreated()) { - auto sp = std::make_unique(sprite_->getPos(), sprite_->getTexture()); + auto sp = std::make_unique(sprite_->getTexture(), sprite_->getPos()); sp->setSpriteClip(BALLOON_WIDTH_4, 0, BALLOON_WIDTH_4, BALLOON_WIDTH_4); sp->render(); } @@ -454,7 +454,7 @@ void Balloon::update() { if (enabled_) { - sprite_->MovingSprite::update(); + sprite_->update(); move(); updateAnimation(); updateColliders(); @@ -510,7 +510,7 @@ void Balloon::updateState() setInvulnerable(false); if (kind_ == POWER_BALL) { - sprite_->setRotate(true); + sprite_->enableRotate(); } } } @@ -520,7 +520,7 @@ void Balloon::updateState() // Si es una powerball deja de rodar if (kind_ == POWER_BALL) { - sprite_->setRotate(false); + sprite_->disableRotate(); } // Reduce el contador @@ -536,7 +536,7 @@ void Balloon::updateState() // Si es una powerball vuelve a rodar if (kind_ == POWER_BALL) { - sprite_->setRotate(true); + sprite_->enableRotate(); } } } @@ -569,7 +569,7 @@ void Balloon::updateAnimation() sprite_->setCurrentAnimation(normal_animation); } - sprite_->animate(); + sprite_->update(); } // Comprueba si el globo está habilitado diff --git a/source/balloon.h b/source/balloon.h index 15ad78f..1e0f34d 100644 --- a/source/balloon.h +++ b/source/balloon.h @@ -4,7 +4,7 @@ #include // for shared_ptr, unique_ptr #include // for string #include // for vector -#include "animated_sprite.h" // for AnimatedSprite +#include "animated_sprite.h" // for SpriteAnimated #include "utils.h" // for Circle class Texture; diff --git a/source/bullet.cpp b/source/bullet.cpp index 8bc9dcd..3afc428 100644 --- a/source/bullet.cpp +++ b/source/bullet.cpp @@ -12,7 +12,7 @@ constexpr int BULLET_VELX_RIGHT = 2; // Constructor Bullet::Bullet(int x, int y, BulletType kind, bool powered_up, int owner, SDL_Rect *play_area, std::shared_ptr texture) - : sprite_(std::make_unique(SDL_Rect{x, y, BULLET_WIDTH, BULLET_HEIGHT}, texture)), + : sprite_(std::make_unique(texture, SDL_Rect{x, y, BULLET_WIDTH, BULLET_HEIGHT})), pos_x_(x), pos_y_(y), width_(BULLET_WIDTH), diff --git a/source/director.cpp b/source/director.cpp index 9bcaf13..63a414f 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -55,6 +55,10 @@ Director::Director(int argc, const char *argv[]) section::name = section::Name::LOGO; #endif + // Deshabilita todos los std::cout + //std::ostream null_stream(nullptr); + //std::streambuf *orig_buf = std::cout.rdbuf(null_stream.rdbuf()); + // Comprueba los parametros del programa checkProgramArguments(argc, argv); diff --git a/source/explosions.cpp b/source/explosions.cpp index 0a662c2..0e2198c 100644 --- a/source/explosions.cpp +++ b/source/explosions.cpp @@ -1,6 +1,6 @@ #include "explosions.h" #include // for move -#include "animated_sprite.h" // for AnimatedSprite +#include "animated_sprite.h" // for SpriteAnimated class Texture; // lines 3-3 // Constructor @@ -51,7 +51,7 @@ void Explosions::addTexture(int size, std::shared_ptr texture, std::vec void Explosions::add(int x, int y, int size) { const int index = getIndexBySize(size); - auto sprite = std::make_unique(textures_[index].texture, "", textures_[index].animation); + auto sprite = std::make_unique(textures_[index].texture, textures_[index].animation); sprite->setPos(x, y); explosions_.push_back(std::move(sprite)); } diff --git a/source/explosions.h b/source/explosions.h index 66649c9..b5566cf 100644 --- a/source/explosions.h +++ b/source/explosions.h @@ -1,8 +1,8 @@ #pragma once -#include // for shared_ptr, unique_ptr -#include // for string -#include // for vector +#include // for shared_ptr, unique_ptr +#include // for string +#include // for vector class AnimatedSprite; class Texture; diff --git a/source/game.cpp b/source/game.cpp index f6243ba..98eb224 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -32,7 +32,7 @@ #include "scoreboard.h" // for Scoreboard, ScoreboardMode, SCOREB... #include "screen.h" // for Screen #include "section.h" // for Name, name, Options, options -#include "smart_sprite.h" // for SmartSprite +#include "smart_sprite.h" // for SpriteSmart #include "text.h" // for Text, TEXT_CENTER #include "texture.h" // for Texture struct JA_Music_t; // lines 35-35 @@ -1413,7 +1413,7 @@ void Game::freeItems() } } -// Crea un objeto SmartSprite para mostrar la puntuación al coger un objeto +// Crea un objeto SpriteSmart para mostrar la puntuación al coger un objeto void Game::createItemScoreSprite(int x, int y, std::shared_ptr texture) { smart_sprites_.emplace_back(std::make_unique(texture)); @@ -1432,7 +1432,7 @@ void Game::createItemScoreSprite(int x, int y, std::shared_ptr texture) } // Vacia el vector de smartsprites -void Game::freeSmartSprites() +void Game::freeSpriteSmarts() { if (!smart_sprites_.empty()) { @@ -1446,7 +1446,7 @@ void Game::freeSmartSprites() } } -// Crea un SmartSprite para arrojar el item café al recibir un impacto +// Crea un SpriteSmart para arrojar el item café al recibir un impacto void Game::throwCoffee(int x, int y) { smart_sprites_.emplace_back(std::make_unique(item_textures_[4])); @@ -1464,13 +1464,13 @@ void Game::throwCoffee(int x, int y) smart_sprites_.back()->setEnabled(true); smart_sprites_.back()->setFinishedCounter(1); smart_sprites_.back()->setSpriteClip(0, param.game.item_size, param.game.item_size, param.game.item_size); - smart_sprites_.back()->setRotate(true); + smart_sprites_.back()->enableRotate(); smart_sprites_.back()->setRotateSpeed(10); smart_sprites_.back()->setRotateAmount(90.0); } -// Actualiza los SmartSprites -void Game::updateSmartSprites() +// Actualiza los SpriteSmarts +void Game::updateSpriteSmarts() { for (auto &ss : smart_sprites_) { @@ -1478,8 +1478,8 @@ void Game::updateSmartSprites() } } -// Pinta los SmartSprites activos -void Game::renderSmartSprites() +// Pinta los SpriteSmarts activos +void Game::renderSpriteSmarts() { for (auto &ss : smart_sprites_) { @@ -1679,8 +1679,8 @@ void Game::update() // Actualiza el estado de muerte updateGameOver(); - // Actualiza los SmartSprites - updateSmartSprites(); + // Actualiza los SpriteSmarts + updateSpriteSmarts(); // Actualiza los contadores de estado y efectos updateTimeStoppedCounter(); @@ -1708,7 +1708,7 @@ void Game::update() freeBullets(); freeBalloons(); freeItems(); - freeSmartSprites(); + freeSpriteSmarts(); } // Comprueba si la música ha de estar sonando @@ -1757,7 +1757,7 @@ void Game::fillCanvas() // Dibuja los objetos background_->render(); renderItems(); - renderSmartSprites(); + renderSpriteSmarts(); explosions_->render(); renderBalloons(); renderBullets(); diff --git a/source/game.h b/source/game.h index 197ee4e..06723ef 100644 --- a/source/game.h +++ b/source/game.h @@ -133,8 +133,8 @@ private: std::vector> player2_textures_; // Vector con las texturas del jugador std::vector>> player_textures_; // Vector con todas las texturas de los jugadores; - std::vector> game_text_textures_; // Vector con las texturas para los sprites con textos - //std::vector> game_text_sprites_; // Sprite con el textos que aparecen al coger items + std::vector> game_text_textures_; // Vector con las texturas para los sprites con textos + // std::vector> game_text_sprites_; // Sprite con el textos que aparecen al coger items std::vector *> item_animations_; // Vector con las animaciones de los items std::vector *> player_animations_; // Vector con las animaciones del jugador @@ -321,20 +321,20 @@ private: // Vacia el vector de items void freeItems(); - // Crea un objeto SmartSprite + // Crea un objeto SpriteSmart void createItemScoreSprite(int x, int y, std::shared_ptr texture); // Vacia el vector de smartsprites - void freeSmartSprites(); + void freeSpriteSmarts(); - // Crea un SmartSprite para arrojar el item café al recibir un impacto + // Crea un SpriteSmart para arrojar el item café al recibir un impacto void throwCoffee(int x, int y); - // Actualiza los SmartSprites - void updateSmartSprites(); + // Actualiza los SpriteSmarts + void updateSpriteSmarts(); - // Pinta los SmartSprites activos - void renderSmartSprites(); + // Pinta los SpriteSmarts activos + void renderSpriteSmarts(); // Acciones a realizar cuando el jugador muere void killPlayer(std::shared_ptr &player); diff --git a/source/game_logo.cpp b/source/game_logo.cpp index d43240c..3af25e6 100644 --- a/source/game_logo.cpp +++ b/source/game_logo.cpp @@ -1,12 +1,12 @@ #include "game_logo.h" #include // for SDL_FLIP_HORIZONTAL #include // for max -#include "animated_sprite.h" // for AnimatedSprite +#include "animated_sprite.h" // for SpriteAnimated #include "asset.h" // for Asset #include "jail_audio.h" // for JA_DeleteSound, JA_LoadSound, JA_PlaySound #include "param.h" // for param #include "screen.h" // for Screen -#include "smart_sprite.h" // for SmartSprite +#include "smart_sprite.h" // for SpriteSmart #include "sprite.h" // for Sprite #include "texture.h" // for Texture #include "utils.h" // for Param, ParamGame, ParamTitle @@ -24,7 +24,7 @@ GameLogo::GameLogo(int x, int y) coffee_sprite_(std::make_unique(coffee_texture_)), crisis_sprite_(std::make_unique(crisis_texture_)), - arcade_edition_sprite_(std::make_unique((param.game.width - arcade_edition_texture_->getWidth()) / 2, param.title.arcade_edition_position, arcade_edition_texture_->getWidth(), arcade_edition_texture_->getHeight(), arcade_edition_texture_)), + arcade_edition_sprite_(std::make_unique(arcade_edition_texture_, (param.game.width - arcade_edition_texture_->getWidth()) / 2, param.title.arcade_edition_position, arcade_edition_texture_->getWidth(), arcade_edition_texture_->getHeight())), crash_sound_(JA_LoadSound(Asset::get()->get("title.wav").c_str())), diff --git a/source/instructions.cpp b/source/instructions.cpp index 1ee5ef2..bb88795 100644 --- a/source/instructions.cpp +++ b/source/instructions.cpp @@ -98,7 +98,7 @@ void Instructions::iniSprites() // Inicializa los sprites for (int i = 0; i < (int)item_textures_.size(); ++i) { - auto sprite = std::make_unique(0, 0, param.game.item_size, param.game.item_size, item_textures_[i]); + auto sprite = std::make_unique(item_textures_[i], 0, 0, param.game.item_size, param.game.item_size); sprite->setPos((SDL_Point){sprite_pos_.x, sprite_pos_.y + ((param.game.item_size + item_space_) * i)}); sprites_.push_back(std::move(sprite)); } diff --git a/source/intro.cpp b/source/intro.cpp index 52f553e..9f0a6c0 100644 --- a/source/intro.cpp +++ b/source/intro.cpp @@ -1,22 +1,22 @@ #include "intro.h" -#include // for SDL_PollEvent, SDL_Event, SDL_QUIT, SDL... -#include // for SDL_GetTicks -#include // for SDL_WINDOWEVENT_SIZE_CHANGED -#include // for move -#include "asset.h" // for Asset -#include "global_inputs.h" // for check -#include "input.h" // for Input -#include "jail_audio.h" // for JA_StopMusic, JA_PlayMusic -#include "lang.h" // for getText -#include "param.h" // for param -#include "screen.h" // for Screen -#include "section.h" // for Name, name, Options, options -#include "smart_sprite.h" // for SmartSprite -#include "text.h" // for Text -#include "texture.h" // for Texture -#include "utils.h" // for Param, ParamGame, Zone, BLOCK, Color -#include "writer.h" // for Writer -struct JA_Music_t; // lines 19-19 +#include // for SDL_PollEvent, SDL_Event, SDL_QUIT, SDL... +#include // for SDL_GetTicks +#include // for SDL_WINDOWEVENT_SIZE_CHANGED +#include // for move +#include "asset.h" // for Asset +#include "global_inputs.h" // for check +#include "input.h" // for Input +#include "jail_audio.h" // for JA_StopMusic, JA_PlayMusic +#include "lang.h" // for getText +#include "param.h" // for param +#include "screen.h" // for Screen +#include "section.h" // for Name, name, Options, options +#include "smart_sprite.h" // for SpriteSmart +#include "text.h" // for Text +#include "texture.h" // for Texture +#include "utils.h" // for Param, ParamGame, Zone, BLOCK, Color +#include "writer.h" // for Writer +struct JA_Music_t; // lines 19-19 // Constructor Intro::Intro(JA_Music_t *music) diff --git a/source/intro.h b/source/intro.h index f61e85c..9c4dbf3 100644 --- a/source/intro.h +++ b/source/intro.h @@ -1,13 +1,13 @@ #pragma once -#include // for Uint32, Uint8 -#include // for unique_ptr, shared_ptr -#include // for vector -#include "smart_sprite.h" // for SmartSprite -#include "writer.h" // for Writer +#include // for Uint32, Uint8 +#include // for unique_ptr, shared_ptr +#include // for vector +#include "smart_sprite.h" // for SpriteSmart +#include "writer.h" // for Writer class Text; class Texture; -struct JA_Music_t; // lines 11-11 +struct JA_Music_t; // lines 11-11 /* Esta clase gestiona un estado del programa. Se encarga de mostrar la secuencia diff --git a/source/item.cpp b/source/item.cpp index 0a8d98d..3c39418 100644 --- a/source/item.cpp +++ b/source/item.cpp @@ -1,12 +1,12 @@ #include "item.h" #include // for rand -#include "animated_sprite.h" // for AnimatedSprite +#include "animated_sprite.h" // for SpriteAnimated #include "param.h" // for param class Texture; // Constructor Item::Item(ItemType type, float x, float y, SDL_Rect *play_area, std::shared_ptr texture, std::vector *animation) - : sprite_(std::make_unique(texture, "", animation)), + : sprite_(std::make_unique(texture, animation)), accel_x_(0.0f), floor_collision_(false), type_(type), @@ -144,7 +144,7 @@ void Item::disable() void Item::update() { move(); - sprite_->animate(); + sprite_->update(); updateTimeToLive(); checkTimeToLive(); } diff --git a/source/item.h b/source/item.h index 7e63067..4248dcd 100644 --- a/source/item.h +++ b/source/item.h @@ -5,7 +5,7 @@ #include // for shared_ptr, unique_ptr #include // for string #include // for vector -#include "animated_sprite.h" // for AnimatedSprite +#include "animated_sprite.h" // for SpriteAnimated #include "utils.h" // for Circle class Texture; @@ -38,7 +38,7 @@ private: float accel_x_; // Aceleración en el eje X float accel_y_; // Aceleración en el eje Y bool floor_collision_; // Indica si el objeto colisiona con el suelo - ItemType type_; // Especifica el tipo de objeto que es + ItemType type_; // Especifica el tipo de objeto que es bool enabled_; // Especifica si el objeto está habilitado Circle collider_; // Circulo de colisión del objeto SDL_Rect *play_area_; // Rectangulo con la zona de juego diff --git a/source/logo.cpp b/source/logo.cpp index 810bc08..7421948 100644 --- a/source/logo.cpp +++ b/source/logo.cpp @@ -1,18 +1,18 @@ #include "logo.h" -#include // for SDL_PollEvent, SDL_Event, SDL_QUIT, SDL... -#include // for SDL_Renderer -#include // for SDL_GetTicks -#include // for SDL_WINDOWEVENT_SIZE_CHANGED -#include // for move -#include "asset.h" // for Asset -#include "global_inputs.h" // for check -#include "input.h" // for Input -#include "jail_audio.h" // for JA_StopMusic -#include "param.h" // for param -#include "screen.h" // for Screen -#include "section.h" // for Name, name, Options, options -#include "sprite.h" // for Sprite -#include "texture.h" // for Texture +#include // for SDL_PollEvent, SDL_Event, SDL_QUIT, SDL... +#include // for SDL_Renderer +#include // for SDL_GetTicks +#include // for SDL_WINDOWEVENT_SIZE_CHANGED +#include // for move +#include "asset.h" // for Asset +#include "global_inputs.h" // for check +#include "input.h" // for Input +#include "jail_audio.h" // for JA_StopMusic +#include "param.h" // for param +#include "screen.h" // for Screen +#include "section.h" // for Name, name, Options, options +#include "sprite.h" // for Sprite +#include "texture.h" // for Texture // Constructor Logo::Logo() @@ -23,7 +23,7 @@ Logo::Logo() // Reserva memoria para los punteros jail_texture_ = std::make_shared(renderer, Asset::get()->get("logo_jailgames.png")); since_texture_ = std::make_shared(renderer, Asset::get()->get("logo_since_1998.png")); - since_sprite_ = std::make_unique((param.game.width - since_texture_->getWidth()) / 2, 83 + jail_texture_->getHeight() + 5, since_texture_->getWidth(), since_texture_->getHeight(), since_texture_); + since_sprite_ = std::make_unique(since_texture_, (param.game.width - since_texture_->getWidth()) / 2, 83 + jail_texture_->getHeight() + 5, since_texture_->getWidth(), since_texture_->getHeight()); // Inicializa variables counter_ = 0; @@ -38,7 +38,7 @@ Logo::Logo() // Crea los sprites de cada linea for (int i = 0; i < jail_texture_->getHeight(); ++i) { - auto temp = std::make_unique(0, i, jail_texture_->getWidth(), 1, jail_texture_); + auto temp = std::make_unique(jail_texture_, 0, i, jail_texture_->getWidth(), 1); temp->setSpriteClip(0, i, jail_texture_->getWidth(), 1); const int posX = (i % 2 == 0) ? param.game.width + (i * 3) : -jail_texture_->getWidth() - (i * 3); temp->setPosX(posX); @@ -115,12 +115,12 @@ void Logo::updateJAILGAMES() { for (int i = 0; i < (int)jail_sprite_.size(); ++i) { - if (jail_sprite_[i]->getIntPosX() != dest_.x) + if (jail_sprite_[i]->getPosX() != dest_.x) { if (i % 2 == 0) { jail_sprite_[i]->incPosX(-SPEED); - if (jail_sprite_[i]->getIntPosX() < dest_.x) + if (jail_sprite_[i]->getPosX() < dest_.x) { jail_sprite_[i]->setPosX(dest_.x); } @@ -128,7 +128,7 @@ void Logo::updateJAILGAMES() else { jail_sprite_[i]->incPosX(SPEED); - if (jail_sprite_[i]->getIntPosX() > dest_.x) + if (jail_sprite_[i]->getPosX() > dest_.x) { jail_sprite_[i]->setPosX(dest_.x); } diff --git a/source/moving_sprite.cpp b/source/moving_sprite.cpp index eca665e..88ed2ba 100644 --- a/source/moving_sprite.cpp +++ b/source/moving_sprite.cpp @@ -2,34 +2,26 @@ #include "texture.h" // for Texture // Constructor -MovingSprite::MovingSprite(float x, float y, int w, int h, float vx, float vy, float ax, float ay, std::shared_ptr texture) - : Sprite((int)x, (int)y, w, h, texture), - x_(x), - y_(y), - vx_(vx), - vy_(vy), - ax_(ax), - ay_(ay), - zoom_w_(1), - zoom_h_(1), - counter_(0), - flip_(SDL_FLIP_NONE) -{ - // Establece los valores de rotacion - rotate_.enabled = false; - rotate_.speed = 0; - rotate_.angle = 0.0f; - rotate_.amount = 0.0f; - rotate_.center = nullptr; +MovingSprite::MovingSprite(std::shared_ptr texture, SDL_Rect pos, Rotate rotate, float zoom_w, float zoom_h, SDL_RendererFlip flip) + : Sprite(texture, pos), + rotate_(rotate), + zoom_w_(zoom_w), + zoom_h_(zoom_h), + flip_(flip) {} - sprite_clip_ = (SDL_Rect){0, 0, w, h}; -}; +MovingSprite::MovingSprite(std::shared_ptr texture, SDL_Rect pos) + : Sprite(texture, pos), + rotate_({false, 0, 0, 0.0f, 0.0f, nullptr}), + zoom_w_(1.0f), + zoom_h_(1.0f), + flip_(SDL_FLIP_NONE) {} MovingSprite::MovingSprite(std::shared_ptr texture) - : Sprite(texture) -{ - clear(); -}; + : Sprite(texture), + rotate_({false, 0, 0, 0.0f, 0.0f, nullptr}), + zoom_w_(1.0f), + zoom_h_(1.0f), + flip_(SDL_FLIP_NONE) {} // Reinicia todas las variables void MovingSprite::clear() @@ -43,16 +35,15 @@ void MovingSprite::clear() ax_ = 0.0f; // Aceleración en el eje X. Variación de la velocidad ay_ = 0.0f; // Aceleración en el eje Y. Variación de la velocidad - zoom_w_ = 1.0f; // Zoom aplicado a la anchura - zoom_h_ = 1.0f; // Zoom aplicado a la altura - rotate_.enabled = false; // Indica si ha de rotar + rotate_.counter = 0; // Contador rotate_.speed = 0; // Velocidad de giro rotate_.angle = 0.0f; // Angulo para dibujarlo rotate_.amount = 0.0f; // Cantidad de grados a girar en cada iteración rotate_.center = nullptr; // Centro de rotación - counter_ = 0; // Contador interno + zoom_w_ = 1.0f; // Zoom aplicado a la anchura + zoom_h_ = 1.0f; // Zoom aplicado a la altura flip_ = SDL_FLIP_NONE; // Establece como se ha de voltear el sprite @@ -70,10 +61,135 @@ void MovingSprite::move() vy_ += ay_; } +// Actualiza las variables internas del objeto +void MovingSprite::update() +{ + move(); + rotate(); +} + // Muestra el sprite por pantalla void MovingSprite::render() { - texture_->render((int)x_, (int)y_, &sprite_clip_, zoom_w_, zoom_h_, (double)rotate_.angle, rotate_.center, flip_); + texture_->render(pos_.x, pos_.y, &sprite_clip_, zoom_w_, zoom_h_, rotate_.angle, rotate_.center, flip_); +} + +// Obtiene el valor de la variable +float MovingSprite::getZoomW() const +{ + return zoom_w_; +} + +// Obtiene el valor de la variable +float MovingSprite::getZoomH() const +{ + return zoom_h_; +} + +// Obtiene el valor de la variable +double MovingSprite::getAngle() const +{ + return rotate_.angle; +} + +// Establece el valor de la variable +void MovingSprite::setZoomW(float value) +{ + zoom_w_ = value; +} + +// Establece el valor de la variable +void MovingSprite::setZoomH(float value) +{ + zoom_h_ = value; +} + +// Establece el valor de la variable +void MovingSprite::setAngle(double value) +{ + rotate_.angle = value; +} + +// Incrementa el valor del ángulo +void MovingSprite::updateAngle() +{ + rotate_.angle += rotate_.amount; +} + +// Obtiene el valor de la variable +bool MovingSprite::isRotating() const +{ + return rotate_.enabled; +} + +// Obtiene el valor de la variable +int MovingSprite::getRotateSpeed() const +{ + return rotate_.speed; +} + +// Establece la rotacion +void MovingSprite::rotate() +{ + if (rotate_.enabled) + { + ++rotate_.counter; + if (rotate_.counter % rotate_.speed == 0) + { + updateAngle(); + rotate_.counter = 0; + } + } +} + +// Establece el valor de la variable +void MovingSprite::enableRotate() +{ + rotate_.enabled = true; + rotate_.counter = 0; +} + +// Establece el valor de la variable +void MovingSprite::disableRotate() +{ + rotate_.enabled = false; + rotate_.counter = 0; +} + +// Establece el valor de la variable +void MovingSprite::setRotateSpeed(int value) +{ + rotate_.speed = std::max(1, value); +} + +// Establece el valor de la variable +void MovingSprite::setRotateAmount(double value) +{ + rotate_.amount = value; +} + +// Cambia el sentido de la rotación +void MovingSprite::switchRotate() +{ + rotate_.amount *= -1; +} + +// Establece el valor de la variable +void MovingSprite::setFlip(SDL_RendererFlip flip) +{ + flip_ = flip; +} + +// Gira el sprite horizontalmente +void MovingSprite::flip() +{ + flip_ = (flip_ == SDL_FLIP_HORIZONTAL) ? SDL_FLIP_NONE : SDL_FLIP_HORIZONTAL; +} + +// Obtiene el valor de la variable +SDL_RendererFlip MovingSprite::getFlip() +{ + return flip_; } // Obtiene el valor de la variable @@ -112,24 +228,6 @@ float MovingSprite::getAccelY() const return ay_; } -// Obtiene el valor de la variable -float MovingSprite::getZoomW() const -{ - return zoom_w_; -} - -// Obtiene el valor de la variable -float MovingSprite::getZoomH() const -{ - return zoom_h_; -} - -// Obtiene el valor de la variable -float MovingSprite::getAngle() const -{ - return rotate_.angle; -} - // Establece la posición y_ el tamaño del objeto void MovingSprite::setPos(SDL_Rect rect) { @@ -186,120 +284,3 @@ void MovingSprite::setAccelY(float value) { ay_ = value; } - -// Establece el valor de la variable -void MovingSprite::setZoomW(float value) -{ - zoom_w_ = value; -} - -// Establece el valor de la variable -void MovingSprite::setZoomH(float value) -{ - zoom_h_ = value; -} - -// Establece el valor de la variable -void MovingSprite::setAngle(double value) -{ - rotate_.angle = value; -} - -// Incrementa el valor de la variable -void MovingSprite::incAngle(double value) -{ - rotate_.angle += value; -} - -// Decrementa el valor de la variable -void MovingSprite::decAngle(double value) -{ - rotate_.angle -= value; -} - -// Obtiene el valor de la variable -bool MovingSprite::getRotate() const -{ - return rotate_.enabled; -} - -// Obtiene el valor de la variable -Uint16 MovingSprite::getRotateSpeed() const -{ - return rotate_.speed; -} - -// Establece la rotacion -void MovingSprite::rotate() -{ - if (rotate_.enabled) - { - if (counter_ % rotate_.speed == 0) - { - incAngle(rotate_.amount); - } - } -} - -// Establece el valor de la variable -void MovingSprite::setRotate(bool value) -{ - rotate_.enabled = value; -} - -// Establece el valor de la variable -void MovingSprite::setRotateSpeed(int value) -{ - rotate_.speed = (value < 1) ? 1 : value; -} - -// Establece el valor de la variable -void MovingSprite::setRotateAmount(double value) -{ - rotate_.amount = value; -} - -// Establece el valor de la variable -void MovingSprite::disableRotate() -{ - rotate_.enabled = false; - rotate_.angle = 0.0f; -} - -// Actualiza las variables internas del objeto -void MovingSprite::update() -{ - move(); - rotate(); - ++counter_ %= 60000; -} - -// Cambia el sentido de la rotación -void MovingSprite::switchRotate() -{ - rotate_.amount *= -1; -} - -// Establece el valor de la variable -void MovingSprite::setFlip(SDL_RendererFlip flip) -{ - flip_ = flip; -} - -// Gira el sprite horizontalmente -void MovingSprite::flip() -{ - flip_ = (flip_ == SDL_FLIP_HORIZONTAL) ? SDL_FLIP_NONE : SDL_FLIP_HORIZONTAL; -} - -// Obtiene el valor de la variable -SDL_RendererFlip MovingSprite::getFlip() -{ - return flip_; -} - -// Devuelve el rectangulo donde está el sprite -SDL_Rect MovingSprite::getPos() const -{ - return (SDL_Rect){(int)x_, (int)y_, pos_.w, pos_.h}; -} \ No newline at end of file diff --git a/source/moving_sprite.h b/source/moving_sprite.h index f8e32d6..9a2cc46 100644 --- a/source/moving_sprite.h +++ b/source/moving_sprite.h @@ -1,25 +1,27 @@ #pragma once -#include // for SDL_Rect, SDL_Point -#include // for SDL_RendererFlip -#include // for Uint16 -#include // for shared_ptr -#include "sprite.h" // for Sprite +#include // for SDL_Rect, SDL_Point +#include // for SDL_RendererFlip +#include // for Uint16 +#include // for shared_ptr +#include "sprite.h" // for Sprite class Texture; -// Clase MovingSprite. Añade posicion y velocidad en punto flotante +// Clase MovingSprite. Añade movimiento y efectos de rotación, zoom y flip al sprite class MovingSprite : public Sprite { -protected: +public: struct Rotate { bool enabled; // Indica si ha de rotar + int counter; // Contador int speed; // Velocidad de giro - float angle; // Angulo para dibujarlo - float amount; // Cantidad de grados a girar en cada iteración + double angle; // Angulo para dibujarlo + float amount; // Cantidad de grados a girar en cada iteración SDL_Point *center; // Centro de rotación }; +protected: float x_; // Posición en el eje X float y_; // Posición en el eje Y @@ -29,20 +31,13 @@ protected: float ax_; // Aceleración en el eje X. Variación de la velocidad float ay_; // Aceleración en el eje Y. Variación de la velocidad - float zoom_w_; // Zoom aplicado a la anchura - float zoom_h_; // Zoom aplicado a la altura - - int counter_; // Contador interno Rotate rotate_; // Variables usada para controlar la rotación del sprite + float zoom_w_; // Zoom aplicado a la anchura + float zoom_h_; // Zoom aplicado a la altura SDL_RendererFlip flip_; // Indica como se voltea el sprite -public: - // Constructor - explicit MovingSprite(float x = 0, float y = 0, int w = 0, int h = 0, float velx = 0, float vely = 0, float accelx = 0, float accely = 0, std::shared_ptr texture = nullptr); - explicit MovingSprite(std::shared_ptr texture = nullptr); - - // Destructor - virtual ~MovingSprite() = default; + // Incrementa el valor del ángulo + void updateAngle(); // Mueve el sprite void move(); @@ -50,6 +45,15 @@ public: // Rota el sprite void rotate(); +public: + // Constructor + MovingSprite(std::shared_ptr texture, SDL_Rect pos, MovingSprite::Rotate rotate, float zoom_w, float zoom_h, SDL_RendererFlip flip); + MovingSprite(std::shared_ptr texture, SDL_Rect pos); + explicit MovingSprite(std::shared_ptr texture); + + // Destructor + ~MovingSprite() = default; + // Actualiza las variables internas del objeto virtual void update(); @@ -59,45 +63,28 @@ public: // Muestra el sprite por pantalla void render() override; - // Obten el valor de la variable + // Obtiene la variable float getPosX() const; float getPosY() const; - - // Obten el valor de la variable float getVelX() const; float getVelY() const; - - // Obten el valor de la variable float getAccelX() const; float getAccelY() const; + // Establece la variable + void setVelX(float value); + void setVelY(float value); + void setAccelX(float value); + void setAccelY(float value); + // Obten el valor de la variable float getZoomW() const; float getZoomH() const; // Obten el valor de la variable - float getAngle() const; - bool getRotate() const; - Uint16 getRotateSpeed() const; - - // Establece la posición del objeto - void setPos(SDL_Rect rect) override; - void setPos(float x, float y); - - // Devuelve el rectangulo donde está el sprite - SDL_Rect getPos() const override; - - // Establece el valor de la variable - void setPosX(float value); - void setPosY(float value); - - // Establece el valor de la variable - void setVelX(float value); - void setVelY(float value); - - // Establece el valor de la variable - void setAccelX(float value); - void setAccelY(float value); + bool isRotating() const; + double getAngle() const; + int getRotateSpeed() const; // Establece el valor de la variable void setZoomW(float value); @@ -105,17 +92,15 @@ public: // Establece el valor de la variable void setAngle(double vaue); - void incAngle(double value); - void decAngle(double value); + + // Activa o desactiva el efecto derotación + void enableRotate(); + void disableRotate(); // Establece el valor de la variable - void setRotate(bool value); void setRotateSpeed(int value); void setRotateAmount(double value); - // Quita el efecto de rotación y deja el sprite en su angulo inicial. - void disableRotate(); - // Cambia el sentido de la rotación void switchRotate(); @@ -128,5 +113,15 @@ public: // Obtiene el valor de la variable SDL_RendererFlip getFlip(); + // Establece la posición y_ el tamaño del objeto + void setPos(SDL_Rect rect); + // Establece el valor de las variables + void setPos(float x, float y); + + // Establece el valor de la variable + void setPosX(float value); + + // Establece el valor de la variable + void setPosY(float value); }; \ No newline at end of file diff --git a/source/notifier.cpp b/source/notifier.cpp index 4e50aa1..263c3b8 100644 --- a/source/notifier.cpp +++ b/source/notifier.cpp @@ -277,7 +277,7 @@ void Notifier::showText(std::string text1, std::string text2, int icon, std::str // Dibuja el icono de la notificación if (has_icons_ && icon >= 0 && num_texts == 2) { - auto sp = std::make_unique((SDL_Rect){0, 0, icon_size, icon_size}, icon_texture_); + auto sp = std::make_unique(icon_texture_, (SDL_Rect){0, 0, icon_size, icon_size}); sp->setPos({padding_in_h, padding_in_v, icon_size, icon_size}); sp->setSpriteClip({icon_size * (icon % 10), icon_size * (icon / 10), icon_size, icon_size}); sp->render(); @@ -299,7 +299,7 @@ void Notifier::showText(std::string text1, std::string text2, int icon, std::str SDL_SetRenderTarget(renderer_, nullptr); // Crea el sprite de la notificación - n.sprite = std::make_shared(n.rect, n.texture); + n.sprite = std::make_shared(n.texture, n.rect); // Deja la notificación invisible n.texture->setAlpha(0); diff --git a/source/on_screen_help.cpp b/source/on_screen_help.cpp index fdde3ea..cb8c88a 100644 --- a/source/on_screen_help.cpp +++ b/source/on_screen_help.cpp @@ -101,7 +101,7 @@ void OnScreenHelp::fillTexture() auto controllersTexture = std::make_shared(Screen::get()->getRenderer(), Asset::get()->get("controllers.png")); // Crea el sprite para dibujar los gráficos - auto sprite = std::make_unique((SDL_Rect){0, 0, 16, 16}, controllersTexture); + auto sprite = std::make_unique(controllersTexture, (SDL_Rect){0, 0, 16, 16}); // Borra la textura SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 0); diff --git a/source/player.cpp b/source/player.cpp index 6cc8a5b..4216bb3 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -3,7 +3,7 @@ #include // for SDL_GetTicks #include // for rand #include // for max, min -#include "animated_sprite.h" // for AnimatedSprite +#include "animated_sprite.h" // for SpriteAnimated #include "input.h" // for inputs_e #include "param.h" // for param #include "texture.h" // for Texture @@ -12,8 +12,8 @@ // Constructor Player::Player(int id, float x, int y, bool demo, SDL_Rect *play_area, std::vector> texture, std::vector *> animations) - : player_sprite_(std::make_unique(texture[0], "", animations[0])), - power_sprite_(std::make_unique(texture[1], "", animations[1])), + : player_sprite_(std::make_unique(texture[0], animations[0])), + power_sprite_(std::make_unique(texture[1], animations[1])), enter_name_(std::make_unique()), play_area_(play_area), id_(id), @@ -279,10 +279,10 @@ void Player::setAnimation() } // Actualiza las animaciones de los sprites - player_sprite_->animate(); + player_sprite_->update(); // powerSprite->setFlip(flip_walk); - power_sprite_->animate(); + power_sprite_->update(); } // Obtiene el valor de la variable diff --git a/source/player.h b/source/player.h index c9176e4..e7202b8 100644 --- a/source/player.h +++ b/source/player.h @@ -5,7 +5,8 @@ #include // for unique_ptr, shared_ptr #include // for string #include // for vector -#include "animated_sprite.h" // for AnimatedSprite +#include "animated_sprite.h" // for SpriteAnimated +#include "smart_sprite.h" // for SpriteAnimated #include "enter_name.h" // for EnterName #include "utils.h" // for Circle class Texture; @@ -42,10 +43,10 @@ class Player { private: // Objetos y punteros - std::unique_ptr player_sprite_; // Sprite para dibujar el jugador - std::unique_ptr power_sprite_; // Sprite para dibujar el aura del jugador con el poder a tope - std::unique_ptr enter_name_; // Clase utilizada para introducir el nombre - SDL_Rect *play_area_; // Rectangulo con la zona de juego + std::unique_ptr player_sprite_; // Sprite para dibujar el jugador + std::unique_ptr power_sprite_; // Sprite para dibujar el aura del jugador con el poder a tope + std::unique_ptr enter_name_; // Clase utilizada para introducir el nombre + SDL_Rect *play_area_; // Rectangulo con la zona de juego // Variables int id_; // Numero de identificación para el jugador diff --git a/source/smart_sprite.cpp b/source/smart_sprite.cpp index 041c648..34cce0d 100644 --- a/source/smart_sprite.cpp +++ b/source/smart_sprite.cpp @@ -1,5 +1,4 @@ #include "smart_sprite.h" -#include "moving_sprite.h" // for MovingSprite class Texture; // Constructor @@ -14,8 +13,7 @@ void SmartSprite::init() { finished_counter_ = 0; on_destination_ = false; - dest_x_ = 0; - dest_y_ = 0; + dest_x_ = dest_y_ = 0; finished_ = false; enabled_ = false; } diff --git a/source/smart_sprite.h b/source/smart_sprite.h index a16669c..67abad4 100644 --- a/source/smart_sprite.h +++ b/source/smart_sprite.h @@ -1,10 +1,10 @@ #pragma once -#include // for shared_ptr -#include "animated_sprite.h" // for AnimatedSprite +#include // for shared_ptr +#include "animated_sprite.h" // for SpriteAnimated class Texture; -// Clase SmartSprite +// Clase SpriteSmart class SmartSprite : public AnimatedSprite { private: @@ -16,12 +16,12 @@ private: bool finished_; // Indica si ya ha terminado bool enabled_; // Indica si el objeto está habilitado - // Comprueba el movimiento - void checkMove(); - // Comprueba si ha terminado void checkFinished(); + // Comprueba el movimiento + void checkMove(); + public: // Constructor explicit SmartSprite(std::shared_ptr texture); diff --git a/source/sprite.cpp b/source/sprite.cpp index d7ba738..199aac5 100644 --- a/source/sprite.cpp +++ b/source/sprite.cpp @@ -1,12 +1,12 @@ #include "sprite.h" // Constructor -Sprite::Sprite(int x, int y, int w, int h, std::shared_ptr texture) +Sprite::Sprite(std::shared_ptr texture, int x, int y, int w, int h) : texture_(texture), pos_((SDL_Rect){x, y, w, h}), sprite_clip_((SDL_Rect){0, 0, pos_.w, pos_.h}) {} -Sprite::Sprite(SDL_Rect rect, std::shared_ptr texture) +Sprite::Sprite(std::shared_ptr texture, SDL_Rect rect) : texture_(texture), pos_(rect), sprite_clip_((SDL_Rect){0, 0, pos_.w, pos_.h}) {} @@ -23,13 +23,13 @@ void Sprite::render() } // Obten el valor de la variable -int Sprite::getIntPosX() const +int Sprite::getPosX() const { return pos_.x; } // Obten el valor de la variable -int Sprite::getIntPosY() const +int Sprite::getPosY() const { return pos_.y; } diff --git a/source/sprite.h b/source/sprite.h index 476925a..baaec09 100644 --- a/source/sprite.h +++ b/source/sprite.h @@ -15,25 +15,25 @@ protected: public: // Constructor - explicit Sprite(int x = 0, int y = 0, int w = 0, int h = 0, std::shared_ptr texture = nullptr); - explicit Sprite(SDL_Rect rect, std::shared_ptr texture = nullptr); - explicit Sprite(std::shared_ptr texture = nullptr); + Sprite(std::shared_ptr, int x, int y, int w, int h); + Sprite(std::shared_ptr, SDL_Rect rect); + explicit Sprite(std::shared_ptr); // Destructor - virtual ~Sprite() = default; + ~Sprite() = default; // Muestra el sprite por pantalla virtual void render(); // Obten el valor de la variable - int getIntPosX() const; - int getIntPosY() const; + int getPosX() const; + int getPosY() const; int getWidth() const; int getHeight() const; // Devuelve el rectangulo donde está el sprite - virtual SDL_Rect getPos() const; - + SDL_Rect getPos() const; + // Establece el valor de la variable void setPosX(int x); void setPosY(int y); @@ -43,7 +43,7 @@ public: // Establece la posición del objeto void setPos(int x, int y); void setPos(SDL_Point p); - virtual void setPos(SDL_Rect r); + void setPos(SDL_Rect r); // Incrementa el valor de la variable void incPosX(int value); diff --git a/source/text.cpp b/source/text.cpp index 55e9e32..5dccc64 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -99,7 +99,7 @@ Text::Text(const std::string &bitmap_file, const std::string &text_file, SDL_Ren // Crea los objetos texture_ = std::make_shared(renderer, bitmap_file); - sprite_ = std::make_unique((SDL_Rect){0, 0, box_width_, box_height_}, texture_); + sprite_ = std::make_unique(texture_, (SDL_Rect){0, 0, box_width_, box_height_}); // Inicializa variables fixed_width_ = false; @@ -122,7 +122,7 @@ Text::Text(const std::string &text_file, std::shared_ptr texture) } // Crea los objetos - sprite_ = std::make_unique((SDL_Rect){0, 0, box_width_, box_height_}, texture); + sprite_ = std::make_unique(texture, (SDL_Rect){0, 0, box_width_, box_height_}); // Inicializa variables fixed_width_ = false; @@ -142,7 +142,7 @@ Text::Text(TextFile *text_file, std::shared_ptr texture) } // Crea los objetos - sprite_ = std::make_unique((SDL_Rect){0, 0, box_width_, box_height_}, texture); + sprite_ = std::make_unique(texture, (SDL_Rect){0, 0, box_width_, box_height_}); // Inicializa variables fixed_width_ = false; diff --git a/source/tiled_bg.cpp b/source/tiled_bg.cpp index cc733db..1b1089f 100644 --- a/source/tiled_bg.cpp +++ b/source/tiled_bg.cpp @@ -60,7 +60,7 @@ void Tiledbg::fillTexture() { // Crea los objetos para pintar en la textura de fondo auto bg_tile_texture = std::make_shared(renderer_, texture_path_); - auto tile = std::make_unique((SDL_Rect){0, 0, tile_width_, tile_height_}, bg_tile_texture); + auto tile = std::make_unique(bg_tile_texture, (SDL_Rect){0, 0, tile_width_, tile_height_}); // Prepara para dibujar sobre la textura auto temp = SDL_GetRenderTarget(renderer_); diff --git a/source/title.cpp b/source/title.cpp index f068889..17bac15 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -39,7 +39,7 @@ Title::Title(JA_Music_t *music) text2_ = std::make_unique(Asset::get()->get("8bithud.png"), Asset::get()->get("8bithud.txt"), renderer); mini_logo_texture_ = std::make_shared(renderer, Asset::get()->get("logo_jailgames_mini.png")); - mini_logo_sprite_ = std::make_unique(param.game.game_area.center_x - mini_logo_texture_->getWidth() / 2, 0, mini_logo_texture_->getWidth(), mini_logo_texture_->getHeight(), mini_logo_texture_); + mini_logo_sprite_ = std::make_unique(mini_logo_texture_, param.game.game_area.center_x - mini_logo_texture_->getWidth() / 2, 0, mini_logo_texture_->getWidth(), mini_logo_texture_->getHeight()); tiled_bg_ = std::make_unique(Asset::get()->get("title_bg_tile.png"), (SDL_Rect){0, 0, param.game.width, param.game.height}, TILED_MODE_RANDOM);