diff --git a/source/core/input/global_inputs.cpp b/source/core/input/global_inputs.cpp index b148cf8..2e1f8d0 100644 --- a/source/core/input/global_inputs.cpp +++ b/source/core/input/global_inputs.cpp @@ -25,7 +25,7 @@ namespace GlobalInputs { return true; } // F5/F6 només actuen quan el post-procesado està actiu. - if (Screen::get()->isShaderEnabled()) { + if (Screen::isShaderEnabled()) { if (Input::get()->checkInput(TOGGLE_SHADER_TYPE, REPEAT_FALSE)) { Screen::get()->toggleActiveShader(); return true; diff --git a/source/core/input/input.cpp b/source/core/input/input.cpp index ecacab5..1a046c1 100644 --- a/source/core/input/input.cpp +++ b/source/core/input/input.cpp @@ -2,7 +2,7 @@ #include -#include // for any_of +#include // for ranges::any_of #include // for basic_ostream, operator<<, cout, basi... #include @@ -164,7 +164,7 @@ auto Input::checkAnyInput(int device, int index) -> bool { if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY) { const bool *key_states = SDL_GetKeyboardState(nullptr); - if (std::any_of(key_bindings_.begin(), key_bindings_.end(), + if (std::ranges::any_of(key_bindings_, [key_states](const auto &key_binding) { return key_states[key_binding.scancode]; })) { return true; } diff --git a/source/core/locale/lang.cpp b/source/core/locale/lang.cpp index bf24adc..85a3fa5 100644 --- a/source/core/locale/lang.cpp +++ b/source/core/locale/lang.cpp @@ -51,8 +51,8 @@ auto Lang::setLang(Code lang) -> bool { break; } - for (auto &mTextString : text_strings_) { - mTextString = ""; + for (auto &text_string : text_strings_) { + text_string = ""; } // Lee el fichero via ResourceHelper (pack o filesystem) @@ -74,9 +74,9 @@ auto Lang::setLang(Code lang) -> bool { line.pop_back(); } // Almacena solo las lineas que no empiezan por # o no esten vacias - const bool test1 = line.substr(0, 1) != "#"; - const bool test2 = !line.empty(); - if (test1 && test2) { + const bool NOT_COMMENT = line.substr(0, 1) != "#"; + const bool NOT_EMPTY = !line.empty(); + if (NOT_COMMENT && NOT_EMPTY) { if (index >= MAX_TEXT_STRINGS) { break; } diff --git a/source/core/rendering/animatedsprite.cpp b/source/core/rendering/animatedsprite.cpp index 63177ad..43e0496 100644 --- a/source/core/rendering/animatedsprite.cpp +++ b/source/core/rendering/animatedsprite.cpp @@ -1,95 +1,120 @@ #include "core/rendering/animatedsprite.h" -#include // for basic_ostream, operator<<, basic_istream, basic... #include // for cout #include // for basic_stringstream #include "core/rendering/texture.h" // for Texture #include "core/resources/resource_helper.h" // for loadFile (pack + filesystem fallback) +namespace { + +// Normalitza CRLF: fitxers .ani amb terminadors de Windows fan que +// line == "[animation]" no faci match i el parser entri en bucle +// infinit / no carregui cap animació. +void stripCr(std::string &s) { + if (!s.empty() && s.back() == '\r') { + s.pop_back(); + } +} + +void parseFramesList(const std::string &value, Animation &buffer, int frame_width, int frame_height, int frames_per_row, int max_tiles) { + std::stringstream ss(value); + std::string tmp; + SDL_Rect rect = {0, 0, frame_width, frame_height}; + while (getline(ss, tmp, ',')) { + 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); + } +} + +void parseAnimationField(const std::string &line, int pos, Animation &buffer, int frame_width, int frame_height, int frames_per_row, int max_tiles, const std::string &filename) { + const std::string KEY = line.substr(0, pos); + const std::string VALUE = line.substr(pos + 1, line.length()); + if (KEY == "name") { + buffer.name = VALUE; + } else if (KEY == "speed") { + buffer.speed = std::stoi(VALUE); + } else if (KEY == "loop") { + buffer.loop = std::stoi(VALUE); + } else if (KEY == "frames") { + parseFramesList(VALUE, buffer, frame_width, frame_height, frames_per_row, max_tiles); + } else { + std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << KEY.c_str() << "\"" << '\n'; + } +} + +auto parseAnimationBlock(std::istream &file, int frame_width, int frame_height, int frames_per_row, int max_tiles, const std::string &filename) -> Animation { + Animation buffer; + buffer.speed = 0; + buffer.loop = -1; + buffer.counter = 0; + buffer.current_frame = 0; + buffer.completed = false; + + std::string line; + do { + if (!std::getline(file, line)) { + break; + } + stripCr(line); + int pos = line.find('='); + if (pos != (int)std::string::npos) { + parseAnimationField(line, pos, buffer, frame_width, frame_height, frames_per_row, max_tiles, filename); + } + } while (line != "[/animation]"); + + return buffer; +} + +void parseGlobalField(const std::string &line, int pos, int &frames_per_row, int &frame_width, int &frame_height, int &max_tiles, const Texture *texture, const std::string &filename) { + const std::string KEY = line.substr(0, pos); + const std::string VALUE = line.substr(pos + 1, line.length()); + if (KEY == "framesPerRow") { + frames_per_row = std::stoi(VALUE); + } else if (KEY == "frameWidth") { + frame_width = std::stoi(VALUE); + } else if (KEY == "frameHeight") { + frame_height = std::stoi(VALUE); + } else { + std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << KEY.c_str() << "\"" << '\n'; + } + + if (frames_per_row == 0 && frame_width > 0) { + frames_per_row = texture->getWidth() / frame_width; + } + if (max_tiles == 0 && frame_width > 0 && frame_height > 0) { + const int W = texture->getWidth() / frame_width; + const int H = texture->getHeight() / frame_height; + max_tiles = W * H; + } +} + +} // namespace + // Parser compartido: lee un istream con el formato .ani static auto parseAnimationStream(std::istream &file, Texture *texture, const std::string &filename, bool verbose) -> AnimatedSpriteData { AnimatedSpriteData as; as.texture = texture; - int framesPerRow = 0; - int frameWidth = 0; - int frameHeight = 0; - int maxTiles = 0; + int frames_per_row = 0; + int frame_width = 0; + int frame_height = 0; + int max_tiles = 0; std::string line; if (verbose) { std::cout << "Animation loaded: " << filename << '\n'; } - // Normalitza CRLF: fitxers .ani amb terminadors de Windows fan que - // line == "[animation]" no faci match i el parser entri en bucle - // infinit / no carregui cap animació. - auto strip_cr = [](std::string &s) { - if (!s.empty() && s.back() == '\r') { - s.pop_back(); - } - }; while (std::getline(file, line)) { - strip_cr(line); + stripCr(line); if (line == "[animation]") { - Animation buffer; - buffer.speed = 0; - buffer.loop = -1; - buffer.counter = 0; - buffer.current_frame = 0; - buffer.completed = false; - - do { - if (!std::getline(file, line)) { - break; - } - strip_cr(line); - int pos = line.find('='); - if (pos != (int)std::string::npos) { - if (line.substr(0, pos) == "name") { - buffer.name = line.substr(pos + 1, line.length()); - } else if (line.substr(0, pos) == "speed") { - buffer.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())); - } else if (line.substr(0, pos) == "frames") { - std::stringstream ss(line.substr(pos + 1, line.length())); - std::string tmp; - SDL_Rect rect = {0, 0, frameWidth, frameHeight}; - while (getline(ss, tmp, ',')) { - const int numTile = std::stoi(tmp) > maxTiles ? 0 : std::stoi(tmp); - rect.x = (numTile % framesPerRow) * frameWidth; - rect.y = (numTile / framesPerRow) * frameHeight; - buffer.frames.push_back(rect); - } - } else { - std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << '\n'; - } - } - } while (line != "[/animation]"); - - as.animations.push_back(buffer); + as.animations.push_back(parseAnimationBlock(file, frame_width, frame_height, frames_per_row, max_tiles, filename)); } else { int pos = line.find('='); if (pos != (int)std::string::npos) { - if (line.substr(0, pos) == "framesPerRow") { - framesPerRow = std::stoi(line.substr(pos + 1, line.length())); - } else if (line.substr(0, pos) == "frameWidth") { - frameWidth = std::stoi(line.substr(pos + 1, line.length())); - } else if (line.substr(0, pos) == "frameHeight") { - frameHeight = std::stoi(line.substr(pos + 1, line.length())); - } else { - std::cout << "Warning: file " << filename.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << '\n'; - } - - if (framesPerRow == 0 && frameWidth > 0) { - framesPerRow = texture->getWidth() / frameWidth; - } - if (maxTiles == 0 && frameWidth > 0 && frameHeight > 0) { - const int w = texture->getWidth() / frameWidth; - const int h = texture->getHeight() / frameHeight; - maxTiles = w * h; - } + parseGlobalField(line, pos, frames_per_row, frame_width, frame_height, max_tiles, texture, filename); } } } @@ -99,17 +124,17 @@ static auto parseAnimationStream(std::istream &file, Texture *texture, const std // Carga la animación desde un fichero (vía ResourceHelper: pack si està inicialitzat, filesystem si no) auto loadAnimationFromFile(Texture *texture, const std::string &file_path, bool verbose) -> AnimatedSpriteData { - const std::string filename = file_path.substr(file_path.find_last_of("\\/") + 1); + const std::string FILE_NAME = file_path.substr(file_path.find_last_of("\\/") + 1); auto bytes = ResourceHelper::loadFile(file_path); if (bytes.empty()) { if (verbose) { - std::cout << "Warning: Unable to open " << filename.c_str() << " file" << '\n'; + std::cout << "Warning: Unable to open " << FILE_NAME.c_str() << " file" << '\n'; } AnimatedSpriteData as; as.texture = texture; return as; } - return loadAnimationFromMemory(texture, bytes, filename, verbose); + return loadAnimationFromMemory(texture, bytes, FILE_NAME, verbose); } // Carga la animación desde bytes en memoria @@ -125,7 +150,7 @@ auto loadAnimationFromMemory(Texture *texture, const std::vector &bytes } // Constructor -AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, const std::string &file, std::vector *buffer) +AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, const std::string &file, const std::vector *buffer) : current_animation_(0) { // Copia los punteros setTexture(texture); @@ -279,132 +304,29 @@ auto AnimatedSprite::getAnimationClip(int index_a, Uint8 index_f) -> SDL_Rect { return animation_[index_a].frames[index_f]; } -// Carga la animación desde un vector -auto AnimatedSprite::loadFromVector(std::vector *source) -> bool { - // Inicializa variables - int framesPerRow = 0; - int frameWidth = 0; - int frameHeight = 0; - int maxTiles = 0; +// Carga la animación desde un vector (reutiliza parseAnimationStream via stringstream) +auto AnimatedSprite::loadFromVector(const std::vector *source) -> bool { + std::stringstream ss; + for (const auto &line : *source) { + ss << line << '\n'; + } + AnimatedSpriteData as = parseAnimationStream(ss, texture_, "", false); + animation_.insert(animation_.end(), as.animations.begin(), as.animations.end()); - // Indicador de éxito en el proceso - bool success = true; - std::string line; - - // Recorre todo el vector - int index = 0; - while (index < (int)source->size()) { - // Lee desde el vector - line = source->at(index); - - // Si la linea contiene el texto [animation_] se realiza el proceso de carga de una animación - if (line == "[animation]") { - Animation buffer; - buffer.speed = 0; - buffer.loop = -1; - buffer.counter = 0; - buffer.current_frame = 0; - buffer.completed = false; - - do { - // Aumenta el indice para leer la siguiente linea - index++; - line = source->at(index); - - // Encuentra la posición del caracter '=' - int pos = line.find('='); - - // Procesa las dos subcadenas - if (pos != (int)std::string::npos) { - if (line.substr(0, pos) == "name") { - buffer.name = line.substr(pos + 1, line.length()); - } - - else if (line.substr(0, pos) == "speed") { - buffer.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())); - } - - else if (line.substr(0, pos) == "frames") { - // Se introducen los valores separados por comas en un vector - std::stringstream ss(line.substr(pos + 1, line.length())); - std::string tmp; - SDL_Rect rect = {0, 0, frameWidth, frameHeight}; - while (getline(ss, tmp, ',')) { - // Comprueba que el tile no sea mayor que el maximo indice permitido - const int numTile = std::stoi(tmp) > maxTiles ? 0 : std::stoi(tmp); - rect.x = (numTile % framesPerRow) * frameWidth; - rect.y = (numTile / framesPerRow) * frameHeight; - buffer.frames.push_back(rect); - } - } - - else { - std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << '\n'; - success = false; - } - } - } while (line != "[/animation]"); - - // Añade la animación al vector de animaciones - animation_.push_back(buffer); - } - - // En caso contrario se parsea el fichero para buscar las variables y los valores - else { - // Encuentra la posición del caracter '=' - int pos = line.find('='); - - // Procesa las dos subcadenas - if (pos != (int)std::string::npos) { - if (line.substr(0, pos) == "framesPerRow") { - framesPerRow = std::stoi(line.substr(pos + 1, line.length())); - } - - else if (line.substr(0, pos) == "frameWidth") { - frameWidth = std::stoi(line.substr(pos + 1, line.length())); - } - - else if (line.substr(0, pos) == "frameHeight") { - frameHeight = std::stoi(line.substr(pos + 1, line.length())); - } - - else { - std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << '\n'; - success = false; - } - - // Normaliza valores - if (framesPerRow == 0 && frameWidth > 0) { - framesPerRow = texture_->getWidth() / frameWidth; - } - - if (maxTiles == 0 && frameWidth > 0 && frameHeight > 0) { - const int w = texture_->getWidth() / frameWidth; - const int h = texture_->getHeight() / frameHeight; - maxTiles = w * h; - } - } - } - - // Una vez procesada la linea, aumenta el indice para pasar a la siguiente - index++; + // El primer frame lleva frame_width/frame_height en .w/.h — los usamos como rect por defecto + if (!as.animations.empty() && !as.animations.front().frames.empty()) { + const auto &first = as.animations.front().frames.front(); + setRect({0, 0, first.w, first.h}); } - // Pone un valor por defecto - setRect({0, 0, frameWidth, frameHeight}); - - return success; + return true; } // Establece la animacion actual void AnimatedSprite::setCurrentAnimation(const std::string &name) { - const int newAnimation = getIndex(name); - if (current_animation_ != newAnimation) { - current_animation_ = newAnimation; + const int NEW_ANIMATION = getIndex(name); + if (current_animation_ != NEW_ANIMATION) { + current_animation_ = NEW_ANIMATION; animation_[current_animation_].current_frame = 0; animation_[current_animation_].counter = 0; animation_[current_animation_].completed = false; @@ -413,9 +335,9 @@ void AnimatedSprite::setCurrentAnimation(const std::string &name) { // Establece la animacion actual void AnimatedSprite::setCurrentAnimation(int index) { - const int newAnimation = index; - if (current_animation_ != newAnimation) { - current_animation_ = newAnimation; + const int NEW_ANIMATION = index; + if (current_animation_ != NEW_ANIMATION) { + current_animation_ = NEW_ANIMATION; animation_[current_animation_].current_frame = 0; animation_[current_animation_].counter = 0; animation_[current_animation_].completed = false; diff --git a/source/core/rendering/animatedsprite.h b/source/core/rendering/animatedsprite.h index 3dd7847..1f445b6 100644 --- a/source/core/rendering/animatedsprite.h +++ b/source/core/rendering/animatedsprite.h @@ -32,7 +32,7 @@ auto loadAnimationFromMemory(Texture *texture, const std::vector &bytes class AnimatedSprite : public MovingSprite { public: - explicit AnimatedSprite(Texture *texture = nullptr, SDL_Renderer *renderer = nullptr, const std::string &file = "", std::vector *buffer = nullptr); // Constructor + explicit AnimatedSprite(Texture *texture = nullptr, SDL_Renderer *renderer = nullptr, const std::string &file = "", const std::vector *buffer = nullptr); // Constructor AnimatedSprite(SDL_Renderer *renderer, AnimatedSpriteData *data); ~AnimatedSprite() override; // Destructor @@ -57,7 +57,7 @@ class AnimatedSprite : public MovingSprite { auto getAnimationClip(int index_a = 0, Uint8 index_f = 0) -> SDL_Rect; auto getIndex(const std::string &name) -> int; // Obtiene el indice de la animación a partir del nombre - auto loadFromVector(std::vector *source) -> bool; // Carga la animación desde un vector + auto loadFromVector(const std::vector *source) -> bool; // Carga la animación desde un vector void setCurrentAnimation(const std::string &name = "default"); // Establece la animacion actual void setCurrentAnimation(int index = 0); diff --git a/source/core/rendering/fade.cpp b/source/core/rendering/fade.cpp index 26318f3..5198baf 100644 --- a/source/core/rendering/fade.cpp +++ b/source/core/rendering/fade.cpp @@ -46,93 +46,15 @@ void Fade::init(Uint8 r, Uint8 g, Uint8 b) { void Fade::render() { if (enabled_ && !finished_) { switch (fade_type_) { - case FADE_FULLSCREEN: { - if (!fullscreen_done_) { - SDL_FRect fRect1 = {0, 0, (float)GAMECANVAS_WIDTH, (float)GAMECANVAS_HEIGHT}; - - int alpha = counter_ * 4; - if (alpha >= 255) { - fullscreen_done_ = true; - - // Deja todos los buffers del mismo color - SDL_SetRenderTarget(renderer_, backbuffer_); - SDL_SetRenderDrawColor(renderer_, r_, g_, b_, 255); - SDL_RenderClear(renderer_); - - SDL_SetRenderTarget(renderer_, nullptr); - SDL_SetRenderDrawColor(renderer_, r_, g_, b_, 255); - SDL_RenderClear(renderer_); - - finished_ = true; - } else { - // Dibujamos sobre el renderizador - SDL_SetRenderTarget(renderer_, nullptr); - - // Copia el backbuffer con la imagen que había al renderizador - SDL_RenderTexture(renderer_, backbuffer_, nullptr, nullptr); - - SDL_SetRenderDrawColor(renderer_, r_, g_, b_, alpha); - SDL_RenderFillRect(renderer_, &fRect1); - } - } + case FADE_FULLSCREEN: + renderFadeFullscreen(); break; - } - - case FADE_CENTER: { - SDL_FRect fR1 = {0, 0, (float)GAMECANVAS_WIDTH, 0}; - SDL_FRect fR2 = {0, 0, (float)GAMECANVAS_WIDTH, 0}; - - SDL_SetRenderDrawColor(renderer_, r_, g_, b_, 64); - - for (int i = 0; i < counter_; i++) { - fR1.h = fR2.h = (float)(i * 4); - fR2.y = (float)(GAMECANVAS_HEIGHT - (i * 4)); - - SDL_RenderFillRect(renderer_, &fR1); - SDL_RenderFillRect(renderer_, &fR2); - } - - if ((counter_ * 4) > GAMECANVAS_HEIGHT) { - finished_ = true; - } + case FADE_CENTER: + renderFadeCenter(); break; - } - - case FADE_RANDOM_SQUARE: { - Uint32 now = SDL_GetTicks(); - if (squares_drawn_ < 50 && now - last_square_ticks_ >= 100) { - last_square_ticks_ = now; - - SDL_FRect fRs = {0, 0, 32, 32}; - - // Crea un color al azar - Uint8 r = 255 * (rand() % 2); - Uint8 g = 255 * (rand() % 2); - Uint8 b = 255 * (rand() % 2); - SDL_SetRenderDrawColor(renderer_, r, g, b, 64); - - // Dibujamos sobre el backbuffer - SDL_SetRenderTarget(renderer_, backbuffer_); - - fRs.x = (float)(rand() % (GAMECANVAS_WIDTH - 32)); - fRs.y = (float)(rand() % (GAMECANVAS_HEIGHT - 32)); - SDL_RenderFillRect(renderer_, &fRs); - - // Volvemos a usar el renderizador de forma normal - SDL_SetRenderTarget(renderer_, nullptr); - - squares_drawn_++; - } - - // Copiamos el backbuffer al renderizador - SDL_RenderTexture(renderer_, backbuffer_, nullptr, nullptr); - - if (squares_drawn_ >= 50) { - finished_ = true; - } + case FADE_RANDOM_SQUARE: + renderFadeRandomSquare(); break; - } - default: break; } @@ -144,6 +66,95 @@ void Fade::render() { } } +// Helper de render: tipo FADE_FULLSCREEN +void Fade::renderFadeFullscreen() { + if (fullscreen_done_) { + return; + } + + const int ALPHA = counter_ * 4; + if (ALPHA >= 255) { + fullscreen_done_ = true; + + // Deja todos los buffers del mismo color + SDL_SetRenderTarget(renderer_, backbuffer_); + SDL_SetRenderDrawColor(renderer_, r_, g_, b_, 255); + SDL_RenderClear(renderer_); + + SDL_SetRenderTarget(renderer_, nullptr); + SDL_SetRenderDrawColor(renderer_, r_, g_, b_, 255); + SDL_RenderClear(renderer_); + + finished_ = true; + return; + } + + // Dibujamos sobre el renderizador + SDL_SetRenderTarget(renderer_, nullptr); + + // Copia el backbuffer con la imagen que había al renderizador + SDL_RenderTexture(renderer_, backbuffer_, nullptr, nullptr); + + SDL_FRect f_rect1 = {0, 0, (float)GAMECANVAS_WIDTH, (float)GAMECANVAS_HEIGHT}; + SDL_SetRenderDrawColor(renderer_, r_, g_, b_, ALPHA); + SDL_RenderFillRect(renderer_, &f_rect1); +} + +// Helper de render: tipo FADE_CENTER +void Fade::renderFadeCenter() { + SDL_FRect f_r1 = {0, 0, (float)GAMECANVAS_WIDTH, 0}; + SDL_FRect f_r2 = {0, 0, (float)GAMECANVAS_WIDTH, 0}; + + SDL_SetRenderDrawColor(renderer_, r_, g_, b_, 64); + + for (int i = 0; i < counter_; i++) { + f_r1.h = f_r2.h = (float)(i * 4); + f_r2.y = (float)(GAMECANVAS_HEIGHT - (i * 4)); + + SDL_RenderFillRect(renderer_, &f_r1); + SDL_RenderFillRect(renderer_, &f_r2); + } + + if ((counter_ * 4) > GAMECANVAS_HEIGHT) { + finished_ = true; + } +} + +// Helper de render: tipo FADE_RANDOM_SQUARE +void Fade::renderFadeRandomSquare() { + const Uint32 NOW = SDL_GetTicks(); + if (squares_drawn_ < 50 && NOW - last_square_ticks_ >= 100) { + last_square_ticks_ = NOW; + + SDL_FRect f_rs = {0, 0, 32, 32}; + + // Crea un color al azar + const Uint8 R = 255 * (rand() % 2); + const Uint8 G = 255 * (rand() % 2); + const Uint8 B = 255 * (rand() % 2); + SDL_SetRenderDrawColor(renderer_, R, G, B, 64); + + // Dibujamos sobre el backbuffer + SDL_SetRenderTarget(renderer_, backbuffer_); + + f_rs.x = (float)(rand() % (GAMECANVAS_WIDTH - 32)); + f_rs.y = (float)(rand() % (GAMECANVAS_HEIGHT - 32)); + SDL_RenderFillRect(renderer_, &f_rs); + + // Volvemos a usar el renderizador de forma normal + SDL_SetRenderTarget(renderer_, nullptr); + + squares_drawn_++; + } + + // Copiamos el backbuffer al renderizador + SDL_RenderTexture(renderer_, backbuffer_, nullptr, nullptr); + + if (squares_drawn_ >= 50) { + finished_ = true; + } +} + // Actualiza las variables internas void Fade::update() { if (enabled_) { diff --git a/source/core/rendering/fade.h b/source/core/rendering/fade.h index 8db7554..df1c0b7 100644 --- a/source/core/rendering/fade.h +++ b/source/core/rendering/fade.h @@ -24,6 +24,10 @@ class Fade { void setFadeType(Uint8 fade_type); // Establece el tipo de fade private: + void renderFadeFullscreen(); // Helper de render: tipo FADE_FULLSCREEN + void renderFadeCenter(); // Helper de render: tipo FADE_CENTER + void renderFadeRandomSquare(); // Helper de render: tipo FADE_RANDOM_SQUARE + SDL_Renderer *renderer_ = nullptr; // El renderizador de la ventana SDL_Texture *backbuffer_ = nullptr; // Textura para usar como backbuffer Uint8 fade_type_ = FADE_FULLSCREEN; // Tipo de fade a realizar diff --git a/source/core/resources/asset.cpp b/source/core/resources/asset.cpp index a656587..31fd105 100644 --- a/source/core/resources/asset.cpp +++ b/source/core/resources/asset.cpp @@ -11,8 +11,8 @@ Asset *Asset::instance = nullptr; // Singleton API -void Asset::init(const std::string &executablePath) { - Asset::instance = new Asset(executablePath); +void Asset::init(const std::string &executable_path) { + Asset::instance = new Asset(executable_path); } void Asset::destroy() { @@ -25,8 +25,8 @@ auto Asset::get() -> Asset * { } // Constructor -Asset::Asset(const std::string &executablePath) - : executable_path_(executablePath.substr(0, executablePath.find_last_of("\\/"))) { +Asset::Asset(const std::string &executable_path) + : executable_path_(executable_path.substr(0, executable_path.find_last_of("\\/"))) { } // Añade un elemento a la lista @@ -37,17 +37,17 @@ void Asset::add(const std::string &file, Type type, bool required, bool absolute temp.required = required; file_list_.push_back(temp); - const std::string filename = file.substr(file.find_last_of("\\/") + 1); - longest_name_ = SDL_max(longest_name_, filename.size()); + const std::string FILE_NAME = file.substr(file.find_last_of("\\/") + 1); + longest_name_ = SDL_max(longest_name_, FILE_NAME.size()); } // Devuelve el fichero de un elemento de la lista a partir de una cadena auto Asset::get(const std::string &text) -> std::string { for (const auto &f : file_list_) { - const size_t lastIndex = f.file.find_last_of('/') + 1; - const std::string file = f.file.substr(lastIndex); + const size_t LAST_INDEX = f.file.find_last_of('/') + 1; + const std::string FILE_NAME = f.file.substr(LAST_INDEX); - if (file == text) { + if (FILE_NAME == text) { return f.file; } } @@ -116,7 +116,7 @@ auto Asset::checkFile(const std::string &path) const -> bool { std::string result = "ERROR"; // Comprueba si existe el fichero (pack o filesystem) - const std::string filename = path.substr(path.find_last_of("\\/") + 1); + const std::string FILE_NAME = path.substr(path.find_last_of("\\/") + 1); if (ResourceHelper::shouldUseResourcePack(path)) { auto bytes = ResourceHelper::loadFile(path); if (!bytes.empty()) { @@ -137,7 +137,7 @@ auto Asset::checkFile(const std::string &path) const -> bool { std::cout << "Checking file: "; std::cout.width(longest_name_ + 2); std::cout.fill('.'); - std::cout << filename + " "; + std::cout << FILE_NAME + " "; std::cout << " [" + result + "]" << '\n'; } diff --git a/source/game/defaults.hpp b/source/game/defaults.hpp index 0cad336..8931a76 100644 --- a/source/game/defaults.hpp +++ b/source/game/defaults.hpp @@ -105,6 +105,6 @@ constexpr int SUBSECTION_TITLE_INSTRUCTIONS = 6; constexpr int NO_KIND = 0; // Colores -const Color bgColor = {0x27, 0x27, 0x36}; -const Color noColor = {0xFF, 0xFF, 0xFF}; -const Color shdwTxtColor = {0x43, 0x43, 0x4F}; +const Color BG_COLOR = {0x27, 0x27, 0x36}; +const Color NO_COLOR = {0xFF, 0xFF, 0xFF}; +const Color SHADOW_COLOR = {0x43, 0x43, 0x4F}; diff --git a/source/game/game.cpp b/source/game/game.cpp index 2ede589..cc14c69 100644 --- a/source/game/game.cpp +++ b/source/game/game.cpp @@ -2400,7 +2400,7 @@ void Game::render() { Screen::get()->start(); // Limpia la pantalla - Screen::get()->clean(bgColor); + Screen::get()->clean(BG_COLOR); // Dibuja los objetos renderBackground(); @@ -2595,7 +2595,7 @@ void Game::renderMessages() { // Time Stopped if (time_stopped_) { if ((time_stopped_counter_ > 100) || (time_stopped_counter_ % 10 > 4)) { - text_nokia2_->writeDX(TXT_CENTER, PLAY_AREA_CENTER_X, PLAY_AREA_FIRST_QUARTER_Y, Lang::get()->getText(36) + std::to_string(time_stopped_counter_ / 10), -1, noColor, 1, shdwTxtColor); + text_nokia2_->writeDX(TXT_CENTER, PLAY_AREA_CENTER_X, PLAY_AREA_FIRST_QUARTER_Y, Lang::get()->getText(36) + std::to_string(time_stopped_counter_ / 10), -1, NO_COLOR, 1, SHADOW_COLOR); } if (time_stopped_counter_ > 100) { @@ -2612,7 +2612,7 @@ void Game::renderMessages() { // D E M O if (demo_.enabled) { if (demo_.counter % 30 > 14) { - text_nokia_big2_->writeDX(TXT_CENTER, PLAY_AREA_CENTER_X, PLAY_AREA_FIRST_QUARTER_Y, Lang::get()->getText(37), 0, noColor, 2, shdwTxtColor); + text_nokia_big2_->writeDX(TXT_CENTER, PLAY_AREA_CENTER_X, PLAY_AREA_FIRST_QUARTER_Y, Lang::get()->getText(37), 0, NO_COLOR, 2, SHADOW_COLOR); } } @@ -2628,11 +2628,11 @@ void Game::renderMessages() { } if (!game_completed_) { // Escribe el numero de fases restantes - text_nokia_big2_->writeDX(TXT_CENTER, PLAY_AREA_CENTER_X, stage_bitmap_path_[stage_bitmap_counter_], stage_text, -2, noColor, 2, shdwTxtColor); + text_nokia_big2_->writeDX(TXT_CENTER, PLAY_AREA_CENTER_X, stage_bitmap_path_[stage_bitmap_counter_], stage_text, -2, NO_COLOR, 2, SHADOW_COLOR); } else { // Escribe el texto de juego completado stage_text = Lang::get()->getText(50); - text_nokia_big2_->writeDX(TXT_CENTER, PLAY_AREA_CENTER_X, stage_bitmap_path_[stage_bitmap_counter_], stage_text, -2, noColor, 1, shdwTxtColor); - text_nokia2_->writeDX(TXT_CENTER, PLAY_AREA_CENTER_X, stage_bitmap_path_[stage_bitmap_counter_] + text_nokia_big2_->getCharacterSize() + 2, Lang::get()->getText(76), -1, noColor, 1, shdwTxtColor); + text_nokia_big2_->writeDX(TXT_CENTER, PLAY_AREA_CENTER_X, stage_bitmap_path_[stage_bitmap_counter_], stage_text, -2, NO_COLOR, 1, SHADOW_COLOR); + text_nokia2_->writeDX(TXT_CENTER, PLAY_AREA_CENTER_X, stage_bitmap_path_[stage_bitmap_counter_] + text_nokia_big2_->getCharacterSize() + 2, Lang::get()->getText(76), -1, NO_COLOR, 1, SHADOW_COLOR); } } } @@ -2858,7 +2858,7 @@ void Game::renderPausedGame() { Screen::get()->start(); // Limpia la pantalla - Screen::get()->clean(bgColor); + Screen::get()->clean(BG_COLOR); // Pinta el escenario { @@ -2974,7 +2974,7 @@ void Game::renderGameOverScreen() { Screen::get()->start(); // Limpia la pantalla - Screen::get()->clean(bgColor); + Screen::get()->clean(BG_COLOR); // Dibujo if (!game_completed_) { // Dibujo de haber perdido la partida diff --git a/source/game/scenes/instructions.cpp b/source/game/scenes/instructions.cpp index ed97795..96dc66c 100644 --- a/source/game/scenes/instructions.cpp +++ b/source/game/scenes/instructions.cpp @@ -15,7 +15,7 @@ #include "core/rendering/text.h" // for Text, TXT_CENTER, TXT_COLOR, TXT_SHADOW #include "core/rendering/texture.h" // for Texture #include "core/resources/resource.h" -#include "game/defaults.hpp" // for shdwTxtColor, GAMECANVAS_CENTER_X, GAME... +#include "game/defaults.hpp" // for GAMECANVAS_CENTER_X, GAME... #include "utils/utils.h" // for Color, Section // Constructor @@ -113,25 +113,25 @@ void Instructions::render() { // Pinta en el backbuffer el texto y los sprites SDL_SetRenderTarget(renderer_, backbuffer_); - SDL_SetRenderDrawColor(renderer_, bgColor.r, bgColor.g, bgColor.b, 255); + SDL_SetRenderDrawColor(renderer_, BG_COLOR.r, BG_COLOR.g, BG_COLOR.b, 255); SDL_RenderClear(renderer_); // Escribe el texto - text_->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, 8, Lang::get()->getText(11), 1, ORANGE_COLOR, 1, shdwTxtColor); - text_->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, 24, Lang::get()->getText(12), 1, noColor, 1, shdwTxtColor); - text_->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, 34, Lang::get()->getText(13), 1, noColor, 1, shdwTxtColor); - text_->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, 48, Lang::get()->getText(14), 1, noColor, 1, shdwTxtColor); - text_->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, 58, Lang::get()->getText(15), 1, noColor, 1, shdwTxtColor); - text_->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, 75, Lang::get()->getText(16), 1, ORANGE_COLOR, 1, shdwTxtColor); + text_->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, 8, Lang::get()->getText(11), 1, ORANGE_COLOR, 1, SHADOW_COLOR); + text_->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, 24, Lang::get()->getText(12), 1, NO_COLOR, 1, SHADOW_COLOR); + text_->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, 34, Lang::get()->getText(13), 1, NO_COLOR, 1, SHADOW_COLOR); + text_->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, 48, Lang::get()->getText(14), 1, NO_COLOR, 1, SHADOW_COLOR); + text_->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, 58, Lang::get()->getText(15), 1, NO_COLOR, 1, SHADOW_COLOR); + text_->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, 75, Lang::get()->getText(16), 1, ORANGE_COLOR, 1, SHADOW_COLOR); - text_->writeShadowed(84, 92, Lang::get()->getText(17), shdwTxtColor); - text_->writeShadowed(84, 108, Lang::get()->getText(18), shdwTxtColor); - text_->writeShadowed(84, 124, Lang::get()->getText(19), shdwTxtColor); - text_->writeShadowed(84, 140, Lang::get()->getText(20), shdwTxtColor); - text_->writeShadowed(84, 156, Lang::get()->getText(21), shdwTxtColor); + text_->writeShadowed(84, 92, Lang::get()->getText(17), SHADOW_COLOR); + text_->writeShadowed(84, 108, Lang::get()->getText(18), SHADOW_COLOR); + text_->writeShadowed(84, 124, Lang::get()->getText(19), SHADOW_COLOR); + text_->writeShadowed(84, 140, Lang::get()->getText(20), SHADOW_COLOR); + text_->writeShadowed(84, 156, Lang::get()->getText(21), SHADOW_COLOR); if ((mode_ == Mode::MANUAL) && (counter_ % 50 > 14)) { - text_->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, GAMECANVAS_HEIGHT - 12, Lang::get()->getText(22), 1, ORANGE_COLOR, 1, shdwTxtColor); + text_->writeDX(TXT_CENTER | TXT_COLOR | TXT_SHADOW, GAMECANVAS_CENTER_X, GAMECANVAS_HEIGHT - 12, Lang::get()->getText(22), 1, ORANGE_COLOR, 1, SHADOW_COLOR); } // Disquito @@ -176,7 +176,7 @@ void Instructions::render() { Screen::get()->start(); // Limpia la pantalla - Screen::get()->clean(bgColor); + Screen::get()->clean(BG_COLOR); // Establece la ventana del backbuffer if (mode_ == Mode::AUTO) { diff --git a/source/game/scenes/intro.cpp b/source/game/scenes/intro.cpp index 0959259..c37d84e 100644 --- a/source/game/scenes/intro.cpp +++ b/source/game/scenes/intro.cpp @@ -364,7 +364,7 @@ void Intro::render() { Screen::get()->start(); // Limpia la pantalla - Screen::get()->clean(bgColor); + Screen::get()->clean(BG_COLOR); // Dibuja los objetos for (auto *bitmap : bitmaps_) { diff --git a/source/game/scenes/logo.cpp b/source/game/scenes/logo.cpp index 6a6666a..d35b8cc 100644 --- a/source/game/scenes/logo.cpp +++ b/source/game/scenes/logo.cpp @@ -10,8 +10,6 @@ #include "core/input/input.h" // for Input, REPEAT_FALSE, InputAction #include "core/rendering/screen.h" // for Screen #include "core/rendering/sprite.h" // for Sprite -#include "core/rendering/texture.h" // for Texture -#include "core/resources/asset.h" // for Asset #include "core/resources/resource.h" #include "game/defaults.hpp" // for bgColor, SECTION_PROG_LOGO, SECTION_PROG... #include "utils/utils.h" // for Section, Color @@ -76,9 +74,9 @@ void Logo::checkInput() { void Logo::renderFade() { // Dibuja el fade if (counter_ >= INIT_FADE) { - const float step = (float)(counter_ - INIT_FADE) / (float)(END_LOGO - INIT_FADE); - const int alpha = std::min((int)(255 * step), 255); - SDL_SetRenderDrawColor(renderer_, bgColor.r, bgColor.g, bgColor.b, alpha); + const float STEP = (float)(counter_ - INIT_FADE) / (float)(END_LOGO - INIT_FADE); + const int ALPHA = std::min((int)(255 * STEP), 255); + SDL_SetRenderDrawColor(renderer_, BG_COLOR.r, BG_COLOR.g, BG_COLOR.b, ALPHA); SDL_RenderFillRect(renderer_, nullptr); } } diff --git a/source/game/scenes/title.cpp b/source/game/scenes/title.cpp index a188181..102290f 100644 --- a/source/game/scenes/title.cpp +++ b/source/game/scenes/title.cpp @@ -522,7 +522,7 @@ void Title::render() { Screen::get()->start(); // Limpia la pantalla - Screen::get()->clean(bgColor); + Screen::get()->clean(BG_COLOR); // Dibuja el tileado de fondo { @@ -547,7 +547,7 @@ void Title::render() { Screen::get()->start(); // Limpia la pantalla - Screen::get()->clean(bgColor); + Screen::get()->clean(BG_COLOR); // Dibuja el tileado de fondo { @@ -574,7 +574,7 @@ void Title::render() { Screen::get()->start(); // Limpia la pantalla - Screen::get()->clean(bgColor); + Screen::get()->clean(BG_COLOR); // Dibuja el tileado de fondo { @@ -592,7 +592,7 @@ void Title::render() { crisis_bitmap_->render(); // Texto con el copyright y versión - text2_->writeDX(TXT_CENTER | TXT_SHADOW, GAMECANVAS_CENTER_X, GAMECANVAS_HEIGHT - (BLOCK * 2), COPYRIGHT, 1, noColor, 1, shdwTxtColor); + text2_->writeDX(TXT_CENTER | TXT_SHADOW, GAMECANVAS_CENTER_X, GAMECANVAS_HEIGHT - (BLOCK * 2), COPYRIGHT, 1, NO_COLOR, 1, SHADOW_COLOR); } if (menu_visible_) { @@ -604,7 +604,7 @@ void Title::render() { // PRESS ANY KEY! if ((counter_ % 50 > 14) && (!menu_visible_)) { - text1_->writeDX(TXT_CENTER | TXT_SHADOW, GAMECANVAS_CENTER_X, PLAY_AREA_THIRD_QUARTER_Y + BLOCK, Lang::get()->getText(23), 1, noColor, 1, shdwTxtColor); + text1_->writeDX(TXT_CENTER | TXT_SHADOW, GAMECANVAS_CENTER_X, PLAY_AREA_THIRD_QUARTER_Y + BLOCK, Lang::get()->getText(23), 1, NO_COLOR, 1, SHADOW_COLOR); } // Fade