diff --git a/source/credits.cpp b/source/credits.cpp index 95820c1..df573c0 100644 --- a/source/credits.cpp +++ b/source/credits.cpp @@ -1,55 +1,38 @@ #include "credits.h" -#include // for SDL_BLENDMODE_BLEND -#include // for SDL_GetError -#include // for SDL_PollEvent, SDL_Event -#include // for SDL_PIXELFORMAT_RGBA8888 -#include // for SDL_Rect -#include // for SDL_GetTicks -#include // for min -#include // for basic_ostream, operator<<, cout, endl -#include "animated_sprite.h" // for AnimatedSprite -#include "defines.h" // for GAMECANVAS_HEIGHT, GAMECANVAS_WIDTH -#include "global_events.h" // for check -#include "global_inputs.h" // for check -#include "options.h" // for Options, options, OptionsVideo, Sect... -#include "resource.h" // for Resource -#include "screen.h" // for Screen -#include "text.h" // for Text, TEXT_CENTER, TEXT_COLOR +#include // for SDL_BLENDMODE_BLEND +#include // for SDL_GetError +#include // for SDL_PollEvent, SDL_Event +#include // for SDL_PIXELFORMAT_RGBA8888 +#include // for SDL_Rect +#include // for SDL_GetTicks +#include // for min +#include // for basic_ostream, operator<<, cout, endl +#include "animated_sprite.h" // for AnimatedSprite +#include "defines.h" // for GAMECANVAS_HEIGHT, GAMECANVAS_WIDTH +#include "global_events.h" // for check +#include "global_inputs.h" // for check +#include "options.h" // for Options, options, OptionsVideo, Sect... +#include "resource.h" // for Resource +#include "screen.h" // for Screen +#include "text.h" // for Text, TEXT_CENTER, TEXT_COLOR // Constructor Credits::Credits() + : shining_sprite_(std::make_shared(Resource::get()->getTexture("shine.png"), Resource::get()->getAnimations("shine.ani"))) { - // Reserva memoria para los punteros - sprite_ = std::make_shared(Resource::get()->getTexture("shine.png"), Resource::get()->getAnimations("shine.ani")); - // Inicializa variables options.section.section = Section::CREDITS; options.section.subsection = Subsection::NONE; - sprite_->setPosition({194, 174, 8, 8}); + shining_sprite_->setPosition({194, 174, 8, 8}); // Cambia el color del borde Screen::get()->setBorderColor(stringToColor(options.video.palette, "black")); // Crea la textura para el texto que se escribe en pantalla - text_texture_ = SDL_CreateTexture(Screen::get()->getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT); - if (text_texture_ == nullptr) - { - if (options.console) - { - std::cout << "Error: textTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl; - } - } - SDL_SetTextureBlendMode(text_texture_, SDL_BLENDMODE_BLEND); + text_texture_ = createTexture(Screen::get()->getRenderer(), options.game.width, options.game.height); // Crea la textura para cubrir el rexto - cover_texture_ = SDL_CreateTexture(Screen::get()->getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT); - if (cover_texture_ == nullptr) - { - if (options.console) - { - std::cout << "Error: textTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl; - } - } + cover_texture_ = createTexture(Screen::get()->getRenderer(), options.game.width, options.game.height); SDL_SetTextureBlendMode(cover_texture_, SDL_BLENDMODE_BLEND); // Escribe el texto en la textura @@ -190,7 +173,7 @@ void Credits::fillTexture() text->writeColored(POS_X, 176, "}", stringToColor(options.video.palette, "bright_red")); // Recoloca el sprite del brillo - sprite_->setPosX(POS_X + 2); + shining_sprite_->setPosX(POS_X + 2); SDL_SetRenderTarget(Screen::get()->getRenderer(), nullptr); @@ -268,7 +251,7 @@ void Credits::update() // Actualiza el sprite con el brillo if (counter_ > 770) { - sprite_->update(); + shining_sprite_->update(); } } } @@ -294,7 +277,7 @@ void Credits::render() SDL_RenderCopy(Screen::get()->getRenderer(), cover_texture_, &srcRect, &dstRect); // Dibuja el sprite con el brillo - sprite_->render(); + shining_sprite_->render(); } // Vuelca el contenido del renderizador en pantalla diff --git a/source/credits.h b/source/credits.h index 45fd76a..b92b2f0 100644 --- a/source/credits.h +++ b/source/credits.h @@ -1,12 +1,12 @@ #pragma once -#include // for SDL_Texture -#include // for Uint32 -#include // for shared_ptr -#include // for string -#include // for vector -#include "utils.h" // for Color -class AnimatedSprite; // lines 9-9 +#include // for SDL_Texture +#include // for Uint32 +#include // for shared_ptr +#include // for string +#include // for vector +#include "utils.h" // for Color +class AnimatedSprite; // lines 9-9 class Credits { @@ -18,9 +18,9 @@ private: }; // Objetos y punteros - SDL_Texture *text_texture_; // Textura para dibujar el texto - SDL_Texture *cover_texture_; // Textura para cubrir el texto - std::shared_ptr sprite_; // Sprite para el brillo del corazón + SDL_Texture *text_texture_; // Textura para dibujar el texto + SDL_Texture *cover_texture_; // Textura para cubrir el texto + std::shared_ptr shining_sprite_; // Sprite para el brillo del corazón // Variables int counter_ = 0; // Contador diff --git a/source/ending.cpp b/source/ending.cpp index aeb9418..b89e193 100644 --- a/source/ending.cpp +++ b/source/ending.cpp @@ -1,23 +1,23 @@ #include "ending.h" -#include // for SDL_BLENDMODE_BLEND -#include // for SDL_GetError -#include // for SDL_PollEvent, SDL_Event -#include // for SDL_PIXELFORMAT_RGBA8888 -#include // for SDL_Rect -#include // for SDL_GetTicks -#include // for min -#include // for basic_ostream, operator<<, cout, endl -#include "defines.h" // for GAMECANVAS_HEIGHT, GAMECANVAS_WIDTH -#include "global_events.h" // for check -#include "global_inputs.h" // for check -#include "jail_audio.h" // for JA_SetVolume, JA_PlayMusic, JA_StopM... -#include "options.h" // for Options, options, OptionsVideo, Sect... -#include "resource.h" // for Resource -#include "screen.h" // for Screen -#include "sprite.h" // for Sprite -#include "text.h" // for Text, TEXT_STROKE -#include "texture.h" // for Texture -#include "utils.h" // for Color, stringToColor, Palette +#include // for SDL_BLENDMODE_BLEND +#include // for SDL_GetError +#include // for SDL_PollEvent, SDL_Event +#include // for SDL_PIXELFORMAT_RGBA8888 +#include // for SDL_Rect +#include // for SDL_GetTicks +#include // for min +#include // for basic_ostream, operator<<, cout, endl +#include "defines.h" // for GAMECANVAS_HEIGHT, options.game.width +#include "global_events.h" // for check +#include "global_inputs.h" // for check +#include "jail_audio.h" // for JA_SetVolume, JA_PlayMusic, JA_StopM... +#include "options.h" // for Options, options, OptionsVideo, Sect... +#include "resource.h" // for Resource +#include "screen.h" // for Screen +#include "sprite.h" // for Sprite +#include "text.h" // for Text, TEXT_STROKE +#include "texture.h" // for Texture +#include "utils.h" // for Color, stringToColor, Palette // Constructor Ending::Ending() @@ -42,15 +42,8 @@ Ending::Ending() // Cambia el color del borde Screen::get()->setBorderColor(stringToColor(options.video.palette, "black")); - // Crea la textura para cubrir el rexto - cover_texture_ = SDL_CreateTexture(Screen::get()->getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT + 8); - if (cover_texture_ == nullptr) - { - if (options.console) - { - std::cout << "Error: canvasTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl; - } - } + // Crea la textura para cubrir el texto + cover_texture_ = createTexture(Screen::get()->getRenderer(), options.game.width, options.game.height + 8); SDL_SetTextureBlendMode(cover_texture_, SDL_BLENDMODE_BLEND); // Rellena la textura para la cortinilla @@ -60,7 +53,6 @@ Ending::Ending() // Destructor Ending::~Ending() { - // Libera la memoria de los objetos SDL_DestroyTexture(cover_texture_); } @@ -99,19 +91,19 @@ void Ending::render() Screen::get()->start(); // Limpia la pantalla - Screen::get()->clean(stringToColor(options.video.palette, "black")); + Screen::get()->clean(stringToColor(options.video.palette, "yellow")); // Dibuja las imagenes de la escena - sprite_pics_[current_scene_].sprite->render(); - sprite_pics_[current_scene_].cover_sprite->render(); + sprite_pics_.at(current_scene_).image_sprite->render(); + sprite_pics_.at(current_scene_).cover_sprite->render(); // Dibuja los textos de la escena - for (auto ti : scenes_[current_scene_].text_index) + for (const auto &ti : scenes_.at(current_scene_).text_index) { if (counter_ > ti.trigger) { - sprite_texts_[ti.index].sprite->render(); - sprite_texts_[ti.index].cover_sprite->render(); + sprite_texts_.at(ti.index).image_sprite->render(); + sprite_texts_.at(ti.index).cover_sprite->render(); } } @@ -176,26 +168,26 @@ void Ending::iniTexts() // Crea los sprites sprite_texts_.clear(); - for (const auto &t : texts) + for (const auto &txt : texts) { auto text = Resource::get()->getText("smb2"); - const int WIDTH = text->lenght(t.caption, 1) + 2 + 2; + const int WIDTH = text->lenght(txt.caption, 1) + 2 + 2; const int HEIGHT = text->getCharacterSize() + 2 + 2; Color color = stringToColor(options.video.palette, "black"); - + EndingTexture st; - - // Crea la texture - st.texture = std::make_shared(Screen::get()->getRenderer()); - st.texture->createBlank(WIDTH, HEIGHT); - st.texture->setAsRenderTarget(Screen::get()->getRenderer()); - st.texture->setBlendMode(SDL_BLENDMODE_BLEND); - text->writeDX(TEXT_STROKE, 2, 2, t.caption, 1, color, 2, color); + + // Crea la textura + st.image_texture = std::make_shared(Screen::get()->getRenderer()); + st.image_texture->createBlank(WIDTH, HEIGHT); + st.image_texture->setAsRenderTarget(Screen::get()->getRenderer()); + st.image_texture->setBlendMode(SDL_BLENDMODE_BLEND); + text->writeDX(TEXT_STROKE, 2, 2, txt.caption, 1, color, 2, color); // Crea el sprite - st.sprite = std::make_shared(st.texture, 0, 0, st.texture->getWidth(), st.texture->getHeight()); - st.sprite->setPosition((GAMECANVAS_WIDTH - st.texture->getWidth()) / 2, t.pos); + st.image_sprite = std::make_shared(st.image_texture, 0, 0, st.image_texture->getWidth(), st.image_texture->getHeight()); + st.image_sprite->setPosition((options.game.width - st.image_texture->getWidth()) / 2, txt.pos); // Crea la coverTexture st.cover_texture = std::make_shared(Screen::get()->getRenderer()); @@ -207,7 +199,7 @@ void Ending::iniTexts() SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 0); SDL_RenderClear(Screen::get()->getRenderer()); - // Los primeros 8 pixels crea una malla + // Crea una malla de 8 pixels de alto color = stringToColor(options.video.palette, "black"); SDL_SetRenderDrawColor(Screen::get()->getRenderer(), color.r, color.g, color.b, 0xFF); for (int i = 0; i < WIDTH; i += 2) @@ -229,12 +221,12 @@ void Ending::iniTexts() // Crea el sprite st.cover_sprite = std::make_shared(st.cover_texture, 0, 0, st.cover_texture->getWidth(), st.cover_texture->getHeight() - 8); - st.cover_sprite->setPosition((GAMECANVAS_WIDTH - st.cover_texture->getWidth()) / 2, t.pos); - st.cover_sprite->setClip(0, 8, -1, -1); + st.cover_sprite->setPosition((options.game.width - st.cover_texture->getWidth()) / 2, txt.pos); + st.cover_sprite->setClip(0, 8, st.cover_texture->getWidth(), st.cover_texture->getHeight()); // Inicializa variables - st.clip_desp = 8; - st.clip_height = HEIGHT; + st.cover_clip_desp = 8; + st.cover_clip_height = HEIGHT; sprite_texts_.push_back(st); } @@ -266,22 +258,22 @@ void Ending::iniPics() // Crea los sprites sprite_pics_.clear(); - for (const auto &p : pics) + for (const auto &pic : pics) { EndingTexture sp; // Crea la texture - sp.texture = Resource::get()->getTexture(p.caption); - const int width = sp.texture->getWidth(); - const int height = sp.texture->getHeight(); + sp.image_texture = Resource::get()->getTexture(pic.caption); + const int WIDTH = sp.image_texture->getWidth(); + const int HEIGHT = sp.image_texture->getHeight(); // Crea el sprite - sp.sprite = std::make_shared(sp.texture, 0, 0, width, height); - sp.sprite->setPosition((GAMECANVAS_WIDTH - width) / 2, p.pos); + sp.image_sprite = std::make_shared(sp.image_texture, 0, 0, WIDTH, HEIGHT); + sp.image_sprite->setPosition((options.game.width - WIDTH) / 2, pic.pos); // Crea la coverTexture sp.cover_texture = std::make_shared(Screen::get()->getRenderer()); - sp.cover_texture->createBlank(width, height + 8); + sp.cover_texture->createBlank(WIDTH, HEIGHT + 8); sp.cover_texture->setAsRenderTarget(Screen::get()->getRenderer()); sp.cover_texture->setBlendMode(SDL_BLENDMODE_BLEND); @@ -289,10 +281,10 @@ void Ending::iniPics() SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 0); SDL_RenderClear(Screen::get()->getRenderer()); - // Los primeros 8 pixels crea una malla + // Crea una malla en los primeros 8 pixels Color c = stringToColor(options.video.palette, "black"); SDL_SetRenderDrawColor(Screen::get()->getRenderer(), c.r, c.g, c.b, 0xFF); - for (int i = 0; i < width; i += 2) + for (int i = 0; i < WIDTH; i += 2) { SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 0); SDL_RenderDrawPoint(Screen::get()->getRenderer(), i, 2); @@ -304,19 +296,19 @@ void Ending::iniPics() } // El resto se rellena de color sólido - SDL_Rect rect = {0, 8, width, height}; + SDL_Rect rect = {0, 8, WIDTH, HEIGHT}; c = stringToColor(options.video.palette, "black"); SDL_SetRenderDrawColor(Screen::get()->getRenderer(), c.r, c.g, c.b, 0xFF); SDL_RenderFillRect(Screen::get()->getRenderer(), &rect); // Crea el sprite sp.cover_sprite = std::make_shared(sp.cover_texture, 0, 0, sp.cover_texture->getWidth(), sp.cover_texture->getHeight() - 8); - sp.cover_sprite->setPosition((GAMECANVAS_WIDTH - sp.cover_texture->getWidth()) / 2, p.pos); - sp.cover_sprite->setClip(0, 8, -1, -1); + sp.cover_sprite->setPosition((options.game.width - sp.cover_texture->getWidth()) / 2, pic.pos); + sp.cover_sprite->setClip(0, 8, sp.cover_texture->getWidth(), sp.cover_texture->getHeight()); // Inicializa variables - sp.clip_desp = 8; - sp.clip_height = height; + sp.cover_clip_desp = 8; + sp.cover_clip_height = HEIGHT; sprite_pics_.push_back(sp); } @@ -327,7 +319,7 @@ void Ending::iniScenes() { // Variable para los tiempos int trigger; - const int lapse = 80; + constexpr int LAPSE = 80; // Crea el contenedor SceneData sc; @@ -340,13 +332,13 @@ void Ending::iniScenes() sc.picture_index = 0; sc.text_index.clear(); trigger = 85 * 2; - trigger += lapse; + trigger += LAPSE; sc.text_index.push_back({0, trigger}); - trigger += lapse; + trigger += LAPSE; sc.text_index.push_back({1, trigger}); - trigger += lapse * 3; + trigger += LAPSE * 3; sc.text_index.push_back({2, trigger}); - trigger += lapse; + trigger += LAPSE; sc.text_index.push_back({3, trigger}); scenes_.push_back(sc); @@ -355,17 +347,17 @@ void Ending::iniScenes() sc.picture_index = 1; sc.text_index.clear(); trigger = 140 * 2; - trigger += lapse; + trigger += LAPSE; sc.text_index.push_back({4, trigger}); - trigger += lapse; + trigger += LAPSE; sc.text_index.push_back({5, trigger}); - trigger += lapse; + trigger += LAPSE; sc.text_index.push_back({6, trigger}); - trigger += lapse * 3; + trigger += LAPSE * 3; sc.text_index.push_back({7, trigger}); - trigger += lapse; + trigger += LAPSE; sc.text_index.push_back({8, trigger}); - trigger += lapse * 3; + trigger += LAPSE * 3; sc.text_index.push_back({9, trigger}); scenes_.push_back(sc); @@ -374,9 +366,9 @@ void Ending::iniScenes() sc.picture_index = 2; sc.text_index.clear(); trigger = 148 / 2; - trigger += lapse; + trigger += LAPSE; sc.text_index.push_back({10, trigger}); - trigger += lapse; + trigger += LAPSE; sc.text_index.push_back({11, trigger}); scenes_.push_back(sc); @@ -385,9 +377,9 @@ void Ending::iniScenes() sc.picture_index = 3; sc.text_index.clear(); trigger = 87 / 2; - trigger += lapse; + trigger += LAPSE; sc.text_index.push_back({12, trigger}); - trigger += lapse / 2; + trigger += LAPSE / 2; sc.text_index.push_back({13, trigger}); scenes_.push_back(sc); @@ -396,11 +388,11 @@ void Ending::iniScenes() sc.picture_index = 4; sc.text_index.clear(); trigger = 91 * 2; - trigger += lapse; + trigger += LAPSE; sc.text_index.push_back({14, trigger}); - trigger += lapse * 2; + trigger += LAPSE * 2; sc.text_index.push_back({15, trigger}); - trigger += lapse * 3; + trigger += LAPSE * 3; sc.text_index.push_back({16, trigger}); scenes_.push_back(sc); } @@ -442,44 +434,45 @@ void Ending::updateCounters() // Actualiza las cortinillas de los elementos void Ending::updateSpriteCovers() -{ // Actualiza la cortinilla de los textos +{ + // Actualiza la cortinilla de los textos if (counter_ % 4 == 0) { - for (auto ti : scenes_[current_scene_].text_index) + for (auto ti : scenes_.at(current_scene_).text_index) { if (counter_ > ti.trigger) { - if (sprite_texts_[ti.index].clip_desp > 0) + if (sprite_texts_.at(ti.index).cover_clip_desp > 0) { - sprite_texts_[ti.index].clip_desp -= 2; + sprite_texts_.at(ti.index).cover_clip_desp -= 2; } - else if (sprite_texts_[ti.index].clip_height > 0) + else if (sprite_texts_.at(ti.index).cover_clip_height > 0) { - sprite_texts_[ti.index].clip_height -= 2; - sprite_texts_[ti.index].cover_sprite->setY(sprite_texts_[ti.index].cover_sprite->getY() + 2); + sprite_texts_.at(ti.index).cover_clip_height -= 2; + sprite_texts_.at(ti.index).cover_sprite->setY(sprite_texts_.at(ti.index).cover_sprite->getY() + 2); } - sprite_texts_[ti.index].cover_sprite->setClip(0, sprite_texts_[ti.index].clip_desp, sprite_texts_[ti.index].cover_sprite->getWidth(), sprite_texts_[ti.index].clip_height); + sprite_texts_.at(ti.index).cover_sprite->setClip(0, sprite_texts_.at(ti.index).cover_clip_desp, sprite_texts_.at(ti.index).cover_sprite->getWidth(), sprite_texts_.at(ti.index).cover_clip_height); } } } - // Actualiza la cortinilla de las imagenes + // Actualiza la cortinilla de las imágenes if (counter_ % 2 == 0) { - if (sprite_pics_[current_scene_].clip_desp > 0) + if (sprite_pics_.at(current_scene_).cover_clip_desp > 0) { - sprite_pics_[current_scene_].clip_desp -= 2; + sprite_pics_.at(current_scene_).cover_clip_desp -= 2; } - else if (sprite_pics_[current_scene_].clip_height > 0) + else if (sprite_pics_.at(current_scene_).cover_clip_height > 0) { - sprite_pics_[current_scene_].clip_height -= 2; - if (sprite_pics_[current_scene_].clip_height < 0) + sprite_pics_.at(current_scene_).cover_clip_height -= 2; + if (sprite_pics_.at(current_scene_).cover_clip_height < 0) { - sprite_pics_[current_scene_].clip_height = 0; + sprite_pics_.at(current_scene_).cover_clip_height = 0; } - sprite_pics_[current_scene_].cover_sprite->setY(sprite_pics_[current_scene_].cover_sprite->getY() + 2); + sprite_pics_.at(current_scene_).cover_sprite->setY(sprite_pics_.at(current_scene_).cover_sprite->getY() + 2); } - sprite_pics_[current_scene_].cover_sprite->setClip(0, sprite_pics_[current_scene_].clip_desp, sprite_pics_[current_scene_].cover_sprite->getWidth(), sprite_pics_[current_scene_].clip_height); + sprite_pics_.at(current_scene_).cover_sprite->setClip(0, sprite_pics_.at(current_scene_).cover_clip_desp, sprite_pics_.at(current_scene_).cover_sprite->getWidth(), sprite_pics_.at(current_scene_).cover_clip_height); } } @@ -536,10 +529,11 @@ void Ending::fillCoverTexture() void Ending::renderCoverTexture() { if (cover_counter_ > 0) - { // Dibuja la textura que cubre el texto - const int offset = std::min(cover_counter_, 100); - SDL_Rect srcRect = {0, 200 - (cover_counter_ * 2), 256, offset * 2}; - SDL_Rect dstRect = {0, 0, 256, offset * 2}; + { + // Dibuja la textura que cubre el texto + const int OFFSET = std::min(cover_counter_, 100); + SDL_Rect srcRect = {0, 200 - (cover_counter_ * 2), 256, OFFSET * 2}; + SDL_Rect dstRect = {0, 0, 256, OFFSET * 2}; SDL_RenderCopy(Screen::get()->getRenderer(), cover_texture_, &srcRect, &dstRect); } } diff --git a/source/ending.h b/source/ending.h index 65c4a5b..c1c0124 100644 --- a/source/ending.h +++ b/source/ending.h @@ -1,12 +1,12 @@ #pragma once -#include // for SDL_Texture -#include // for Uint32 -#include // for shared_ptr -#include // for string -#include // for vector -class Sprite; // lines 12-12 -class Texture; // lines 14-14 +#include // for SDL_Texture +#include // for Uint32 +#include // for shared_ptr +#include // for string +#include // for vector +class Sprite; // lines 12-12 +class Texture; // lines 14-14 class Ending { @@ -14,12 +14,12 @@ private: // Estructuras struct EndingTexture // Estructura con dos texturas y sprites, uno para mostrar y el otro hace de cortinilla { - std::shared_ptr texture; // Textura a mostrar - std::shared_ptr sprite; // Sprite para mostrar la textura + std::shared_ptr image_texture; // Textura a mostrar + std::shared_ptr image_sprite; // Sprite para mostrar la textura std::shared_ptr cover_texture; // Textura que cubre a la otra textura std::shared_ptr cover_sprite; // Sprite para mostrar la textura que cubre a la otra textura - int clip_desp; // Desplazamiento del spriteClip de la textura de cobertura - int clip_height; // Altura del spriteClip de la textura de cobertura + int cover_clip_desp; // Desplazamiento del spriteClip de la textura de cobertura + int cover_clip_height; // Altura del spriteClip de la textura de cobertura }; struct TextAndPosition // Estructura con un texto y su posición en el eje Y @@ -37,8 +37,8 @@ private: struct SceneData // Estructura para crear cada una de las escenas del final { std::vector text_index; // Indices del vector de textos a mostrar y su disparador - int picture_index; // Indice del vector de imagenes a mostrar - int counter_end; // Valor del contador en el que finaliza la escena + int picture_index; // Indice del vector de imagenes a mostrar + int counter_end; // Valor del contador en el que finaliza la escena }; // Objetos y punteros diff --git a/source/ending2.cpp b/source/ending2.cpp index 9b5ed68..aa58066 100644 --- a/source/ending2.cpp +++ b/source/ending2.cpp @@ -6,7 +6,7 @@ #include // for SDL_GetTicks #include // for max, min, replace #include "animated_sprite.h" // for AnimatedSprite -#include "defines.h" // for GAMECANVAS_HEIGHT, GAMECANVAS_CENTER_X +#include "defines.h" // for options.game.height, GAMECANVAS_CENTER_X #include "global_events.h" // for check #include "global_inputs.h" // for check #include "jail_audio.h" // for JA_SetVolume, JA_PlayMusic, JA_StopM... @@ -17,6 +17,7 @@ #include "text.h" // for Text #include "texture.h" // for Texture #include "utils.h" // for Color, stringToColor +#include // Constructor Ending2::Ending2() @@ -279,9 +280,9 @@ void Ending2::loadSprites() sprite_max_height_ = 0; // Carga los sprites - for (const auto &sl : sprite_list_) + for (const auto &file : sprite_list_) { - sprites_.emplace_back(std::make_shared(Resource::get()->getTexture(sl + ".png"), Resource::get()->getAnimations(sl + ".ani"))); + sprites_.emplace_back(std::make_shared(Resource::get()->getTexture(file + ".png"), Resource::get()->getAnimations(file + ".ani"))); sprite_max_width_ = std::max(sprites_.back()->getWidth(), sprite_max_width_); sprite_max_height_ = std::max(sprites_.back()->getHeight(), sprite_max_height_); } @@ -327,9 +328,9 @@ void Ending2::renderSprites() const Color color = stringToColor(options.video.palette, "red"); for (auto sprite : sprites_) { - const bool a = sprite->getRect().y + sprite->getRect().h > 0; - const bool b = sprite->getRect().y < GAMECANVAS_HEIGHT; - if (a && b) + const bool A = sprite->getRect().y + sprite->getRect().h > 0; + const bool B = sprite->getRect().y < options.game.height; + if (A && B) { sprite->getTexture()->setColor(color.r, color.g, color.b); sprite->render(); @@ -348,9 +349,9 @@ void Ending2::renderSpriteTexts() const Color color = stringToColor(options.video.palette, "white"); for (auto sprite : sprite_texts_) { - const bool a = sprite->getRect().y + sprite->getRect().h > 0; - const bool b = sprite->getRect().y < GAMECANVAS_HEIGHT; - if (a && b) + const bool A = sprite->getRect().y + sprite->getRect().h > 0; + const bool B = sprite->getRect().y < options.game.height; + if (A && B) { sprite->getTexture()->setColor(color.r, color.g, color.b); sprite->render(); @@ -363,9 +364,9 @@ void Ending2::renderTexts() { for (auto sprite : texts_) { - const bool a = sprite->getRect().y + sprite->getRect().h > 0; - const bool b = sprite->getRect().y < GAMECANVAS_HEIGHT; - if (a && b) + const bool A = sprite->getRect().y + sprite->getRect().h > 0; + const bool B = sprite->getRect().y < options.game.height; + if (A && B) { sprite->render(); } @@ -375,25 +376,23 @@ void Ending2::renderTexts() // Coloca los sprites en su sito void Ending2::placeSprites() { - for (int i = 0; i < (int)sprites_.size(); ++i) + for (int i = 0; i < static_cast(sprites_.size()); ++i) { const int X = i % 2 == 0 ? FIRST_COL_ : SECOND_COL_; - const int Y = (i / 1) * (sprite_max_height_ + DIST_SPRITE_TEXT_ + Resource::get()->getText("smb2")->getCharacterSize() + DIST_SPRITE_SPRITE_) + GAMECANVAS_HEIGHT + 40; + const int Y = (i / 1) * (sprite_max_height_ + DIST_SPRITE_TEXT_ + Resource::get()->getText("smb2")->getCharacterSize() + DIST_SPRITE_SPRITE_) + options.game.height + 40; const int W = sprites_.at(i)->getWidth(); const int H = sprites_.at(i)->getHeight(); const int DX = -(W / 2); const int DY = sprite_max_height_ - H; - sprites_.at(i)->setPosition({X + DX, Y + DY, W, H}); + sprites_.at(i)->setPos({X + DX, Y + DY, W, H}); sprites_.at(i)->setVelY(SPRITE_DESP_SPEED_); } - // Recoloca el último sprite, que es el del jugador - const int W = sprites_.back()->getWidth(); - const int X = GAMECANVAS_CENTER_X - (W / 2); + // Recoloca el sprite del jugador, que es el último de la lista + const int X = (options.game.width - sprites_.back()->getWidth()) / 2; const int Y = sprites_.back()->getPosY() + sprite_max_height_ * 2; - sprites_.back()->setPosX(X); - sprites_.back()->setPosY(Y); + sprites_.back()->setPos(X, Y); sprites_.back()->setCurrentAnimation("walk"); } @@ -456,7 +455,7 @@ void Ending2::createTexts() const int h = text->getCharacterSize(); const int x = GAMECANVAS_CENTER_X; const int dx = -(w / 2); - const int y = GAMECANVAS_HEIGHT + (text->getCharacterSize() * (i * 2)); + const int y = options.game.height + (text->getCharacterSize() * (i * 2)); // Crea la textura auto texture = std::make_shared(Screen::get()->getRenderer()); @@ -473,7 +472,7 @@ void Ending2::createTexts() // Crea los últimos textos // El primer texto va a continuación del ultimo spriteText - const int start = sprite_texts_.back()->getPosY() + text->getCharacterSize() * 15; + const int START = sprite_texts_.back()->getPosY() + text->getCharacterSize() * 15; list.clear(); list.push_back("THANK YOU"); list.push_back("FOR PLAYING!"); @@ -486,7 +485,7 @@ void Ending2::createTexts() const int h = text->getCharacterSize(); const int x = GAMECANVAS_CENTER_X; const int dx = -(w / 2); - const int y = start + (text->getCharacterSize() * (i * 2)); + const int y = START + (text->getCharacterSize() * (i * 2)); // Crea la textura auto texture = std::make_shared(Screen::get()->getRenderer()); diff --git a/source/game.cpp b/source/game.cpp index 691d75d..614d2e3 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -663,20 +663,11 @@ void Game::initPlayer(const PlayerSpawn &spawn_point, std::shared_ptr room void Game::createRoomNameTexture() { auto text = Resource::get()->getText("smb2"); - room_name_texture_ = SDL_CreateTexture(Screen::get()->getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, text->getCharacterSize() * 2); - if (room_name_texture_ == nullptr) - { - if (options.console) - { - std::cout << "Error: room_name_texture_ could not be created!\nSDL Error: " << SDL_GetError() << std::endl; - } - } - - // Establece el blend mode de la textura + room_name_texture_ = createTexture(Screen::get()->getRenderer(), options.game.width, text->getCharacterSize() * 2); SDL_SetTextureBlendMode(room_name_texture_, SDL_BLENDMODE_BLEND); // Establece el destino de la textura - room_name_rect_ = {0, PLAY_AREA_HEIGHT, GAMECANVAS_WIDTH, text->getCharacterSize() * 2}; + room_name_rect_ = {0, PLAY_AREA_HEIGHT, options.game.width, text->getCharacterSize() * 2}; } // DEMO MODE: Inicializa las variables para el modo demo diff --git a/source/loading_screen.cpp b/source/loading_screen.cpp index 03e0a4f..b80f956 100644 --- a/source/loading_screen.cpp +++ b/source/loading_screen.cpp @@ -33,14 +33,7 @@ LoadingScreen::LoadingScreen() mono_loading_screen_sprite_ = std::make_shared(mono_loading_screen_texture_, 0, 0, mono_loading_screen_texture_->getWidth(), mono_loading_screen_texture_->getHeight()); color_loading_screen_sprite_ = std::make_shared(color_loading_screen_texture_, 0, 0, color_loading_screen_texture_->getWidth(), color_loading_screen_texture_->getHeight()); - texture_ = SDL_CreateTexture(Screen::get()->getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, options.game.width, options.game.height); - if (texture_ == nullptr) - { - if (options.console) - { - std::cout << "LoadingScreen::texture_ could not be created!\nSDL Error: " << SDL_GetError() << std::endl; - } - } + texture_ = createTexture(Screen::get()->getRenderer(), options.game.width, options.game.height); clearTexture(); // Inicializa variables diff --git a/source/moving_sprite.cpp b/source/moving_sprite.cpp index 5becf1b..508bc7e 100644 --- a/source/moving_sprite.cpp +++ b/source/moving_sprite.cpp @@ -1,5 +1,5 @@ #include "moving_sprite.h" -#include "texture.h" // for Texture +#include "texture.h" // for Texture // Constructor MovingSprite::MovingSprite(std::shared_ptr texture, SDL_Rect pos, Rotate rotate, float zoom_w, float zoom_h, SDL_RendererFlip flip) @@ -9,7 +9,7 @@ MovingSprite::MovingSprite(std::shared_ptr texture, SDL_Rect pos, Rotat rotate_(rotate), zoom_w_(zoom_w), zoom_h_(zoom_h), - flip_(flip) {} + flip_(flip) { Sprite::pos_ = pos; } MovingSprite::MovingSprite(std::shared_ptr texture, SDL_Rect pos) : Sprite(texture, pos), @@ -18,7 +18,7 @@ MovingSprite::MovingSprite(std::shared_ptr texture, SDL_Rect pos) rotate_(Rotate()), zoom_w_(1.0f), zoom_h_(1.0f), - flip_(SDL_FLIP_NONE) {} + flip_(SDL_FLIP_NONE) { Sprite::pos_ = pos; } MovingSprite::MovingSprite(std::shared_ptr texture) : Sprite(texture), diff --git a/source/options.cpp b/source/options.cpp index 665ebf4..084a207 100644 --- a/source/options.cpp +++ b/source/options.cpp @@ -21,7 +21,7 @@ void initOptions() options = Options(); #ifdef DEBUG - options.section = SectionState(Section::DEMO, Subsection::NONE); + options.section = SectionState(Section::ENDING2, Subsection::NONE); options.console = true; #else options.section = SectionState(Section::LOGO, Subsection::LOGO_TO_INTRO); diff --git a/source/room.cpp b/source/room.cpp index 532820b..cce3b37 100644 --- a/source/room.cpp +++ b/source/room.cpp @@ -415,15 +415,7 @@ Room::Room(const std::string &room_path, std::shared_ptr data) setAnimatedTiles(); // Crea la textura para el mapa de tiles de la habitación - map_texture_ = SDL_CreateTexture(Screen::get()->getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT); - if (map_texture_ == nullptr) - { - if (options.console) - { - std::cout << "Error: mapTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl; - } - } - SDL_SetTextureBlendMode(map_texture_, SDL_BLENDMODE_BLEND); + map_texture_ = createTexture(Screen::get()->getRenderer(), PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT); // Pinta el mapa de la habitación en la textura fillMapTexture(); diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index d2e2571..ce32a7a 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -27,14 +27,7 @@ Scoreboard::Scoreboard(std::shared_ptr data) player_sprite_ = std::make_shared(player_texture, player_animations); player_sprite_->setCurrentAnimation("walk_menu"); - texture_ = SDL_CreateTexture(Screen::get()->getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, TEXTURE_WIDTH_, TEXTURE_HEIGHT_); - if (texture_ == nullptr) - { - if (options.console) - { - std::cout << "Scorebard::texture_ could not be created!\nSDL Error: " << SDL_GetError() << std::endl; - } - } + texture_ = createTexture(Screen::get()->getRenderer(), TEXTURE_WIDTH_, TEXTURE_HEIGHT_); texture_dest_ = {0, options.game.height - TEXTURE_HEIGHT_, TEXTURE_WIDTH_, TEXTURE_HEIGHT_}; // Inicializa las variables diff --git a/source/screen.cpp b/source/screen.cpp index 1551562..16e1fed 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -52,35 +52,13 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) SDL_RenderSetIntegerScale(renderer_, options.video.integer_scale ? SDL_TRUE : SDL_FALSE); // Crea la textura donde se vuelcan las surfaces - surface_texture_ = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, options.game.width, options.game.height); - if (surface_texture_ == nullptr) - { - if (options.console) - { - std::cout << "surface_texture_ could not be created!\nSDL Error: " << SDL_GetError() << std::endl; - } - } - //SDL_SetTextureBlendMode(surface_texture_, SDL_BLENDMODE_BLEND); + surface_texture_ = createTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, options.game.width, options.game.height); // Crea la textura donde se dibujan los graficos del juego - game_texture_ = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, options.game.width, options.game.height); - if (game_texture_ == nullptr) - { - if (options.console) - { - std::cout << "game_texture_ could not be created!\nSDL Error: " << SDL_GetError() << std::endl; - } - } + game_texture_ = createTexture(renderer, options.game.width, options.game.height); // Crea la textura donde se dibuja el borde que rodea el area de juego - border_texture_ = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, options.game.width + options.video.border.width * 2, options.game.height + options.video.border.height * 2); - if (border_texture_ == nullptr) - { - if (options.console) - { - std::cout << "border_texture_ could not be created!\nSDL Error: " << SDL_GetError() << std::endl; - } - } + border_texture_ = createTexture(renderer,options.game.width + options.video.border.width * 2, options.game.height + options.video.border.height * 2); setBorderColor(border_color_); // Crea la surface donde se pinta el juego diff --git a/source/title.cpp b/source/title.cpp index 2bac751..1c3cb80 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -27,14 +27,7 @@ Title::Title() sprite_(std::make_shared(texture_, 0, 0, texture_->getWidth(), texture_->getHeight())) { // Crea la textura para los graficos que aparecen en el fondo de la pantalla de titulo - bg_texture_ = SDL_CreateTexture(Screen::get()->getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT); - if (bg_texture_ == nullptr) - { - if (options.console) - { - std::cout << "Error: bgTexture could not be created!\nSDL Error: " << SDL_GetError() << std::endl; - } - } + bg_texture_ = createTexture(Screen::get()->getRenderer(), options.game.width, options.game.height); SDL_SetTextureBlendMode(bg_texture_, SDL_BLENDMODE_BLEND); // Carga la surface con los gráficos de la pantalla de carga diff --git a/source/utils.cpp b/source/utils.cpp index ef5f40b..07dae7d 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -1,16 +1,16 @@ #include "utils.h" -#include // for abs -#include // for find, transform -#include // for tolower -#include // for round, abs -#include // for exception -#include // for path -#include // for basic_ostream, cout, basic_ios, ios, endl -#include // for basic_string, string, char_traits, allocator -#include // for unordered_map, operator==, _Node_const_iter... -#include // for pair -#include "jail_audio.h" // for JA_GetMusicState, JA_Music_state, JA_PlayMusic -#include "resource.h" // for Resource +#include // for abs +#include // for find, transform +#include // for tolower +#include // for round, abs +#include // for exception +#include // for path +#include // for basic_ostream, cout, basic_ios, ios, endl +#include // for basic_string, string, char_traits, allocator +#include // for unordered_map, operator==, _Node_const_iter... +#include // for pair +#include "jail_audio.h" // for JA_GetMusicState, JA_Music_state, JA_PlayMusic +#include "resource.h" // for Resource // Calcula el cuadrado de la distancia entre dos puntos double distanceSquared(int x1, int y1, int x2, int y2) @@ -551,3 +551,21 @@ void fillTextureWithColor(SDL_Renderer *renderer, SDL_Texture *texture, Uint8 r, // Restaurar el render target previo SDL_SetRenderTarget(renderer, previous_target); } + +// Crea un SDL_Texture +SDL_Texture *createTexture(SDL_Renderer *renderer, int width, int height, Uint32 format, int access, bool logError) +{ + // Crear la textura + SDL_Texture *texture = SDL_CreateTexture(renderer, format, access, width, height); + if (texture == nullptr) + { + // Registrar el error si está habilitado + if (logError) + { + std::cerr << "Error: Texture could not be created!\nSDL Error: " << SDL_GetError() << std::endl; + } + return nullptr; + } + + return texture; +} diff --git a/source/utils.h b/source/utils.h index 5af037c..24aa67d 100644 --- a/source/utils.h +++ b/source/utils.h @@ -133,4 +133,7 @@ bool stringInVector(const std::vector &vec, const std::string &str) void playMusic(const std::string &music_path); // Rellena una textura de un color -void fillTextureWithColor(SDL_Renderer *renderer, SDL_Texture *texture, Uint8 r, Uint8 g, Uint8 b, Uint8 a); \ No newline at end of file +void fillTextureWithColor(SDL_Renderer *renderer, SDL_Texture *texture, Uint8 r, Uint8 g, Uint8 b, Uint8 a); + +// Crea un SDL_Texture +SDL_Texture *createTexture(SDL_Renderer *renderer, int width, int height, Uint32 format = SDL_PIXELFORMAT_RGBA8888, int access = SDL_TEXTUREACCESS_TARGET, bool logError = false); \ No newline at end of file