neteja cppcheck: inicialitza Menu::h_, renomena macro PAUSE a DEBUG_PAUSE, const*
This commit is contained in:
+1
-1
@@ -129,7 +129,7 @@ target_include_directories(${PROJECT_NAME} PRIVATE ${DIR_SOURCES})
|
||||
|
||||
# Añadir definiciones de compilación dependiendo del tipo de build
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE
|
||||
$<$<CONFIG:DEBUG>:DEBUG PAUSE>
|
||||
$<$<CONFIG:DEBUG>:DEBUG DEBUG_PAUSE>
|
||||
$<$<CONFIG:RELEASE>:RELEASE_BUILD>
|
||||
)
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include <cmath> // Para std::lround
|
||||
#include <cstdint> // Para int8_t, uint8_t
|
||||
#include <string> // Para string
|
||||
#include <utility> // Para move
|
||||
|
||||
namespace Ja {
|
||||
struct Music;
|
||||
|
||||
@@ -164,10 +164,9 @@ auto Input::checkAnyInput(int device, int index) -> bool {
|
||||
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY) {
|
||||
const bool *key_states = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
for (auto &key_binding : key_bindings_) {
|
||||
if (key_states[key_binding.scancode]) {
|
||||
return true;
|
||||
}
|
||||
if (std::any_of(key_bindings_.begin(), key_bindings_.end(),
|
||||
[key_states](const auto &key_binding) { return key_states[key_binding.scancode]; })) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -159,7 +159,7 @@ void Texture::setAlpha(Uint8 alpha) {
|
||||
}
|
||||
|
||||
// Renderiza la textura en un punto específico
|
||||
void Texture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, float zoom_w, float zoom_h, double angle, SDL_Point *center, SDL_FlipMode flip) {
|
||||
void Texture::render(SDL_Renderer *renderer, int x, int y, const SDL_Rect *clip, float zoom_w, float zoom_h, double angle, const SDL_Point *center, SDL_FlipMode flip) {
|
||||
// Establece el destino de renderizado en la pantalla
|
||||
SDL_FRect render_quad = {(float)x, (float)y, (float)width_, (float)height_};
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ class Texture {
|
||||
void setBlendMode(SDL_BlendMode blending); // Establece el blending
|
||||
void setAlpha(Uint8 alpha); // Establece el alpha para la modulación
|
||||
|
||||
void render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip = nullptr, float zoom_w = 1, float zoom_h = 1, double angle = 0.0, SDL_Point *center = nullptr, SDL_FlipMode flip = SDL_FLIP_NONE); // Renderiza la textura en un punto específico
|
||||
void render(SDL_Renderer *renderer, int x, int y, const SDL_Rect *clip = nullptr, float zoom_w = 1, float zoom_h = 1, double angle = 0.0, const SDL_Point *center = nullptr, SDL_FlipMode flip = SDL_FLIP_NONE); // Renderiza la textura en un punto específico
|
||||
void setAsRenderTarget(SDL_Renderer *renderer); // Establece la textura como objetivo de renderizado
|
||||
|
||||
[[nodiscard]] auto getWidth() const -> int; // Obtiene el ancho de la imagen
|
||||
|
||||
+13
-15
@@ -63,8 +63,8 @@ Game::Game(int num_players, int current_stage, SDL_Renderer *renderer, bool demo
|
||||
// Pasa variables
|
||||
this->demo_.enabled = demo;
|
||||
this->num_players_ = num_players;
|
||||
#ifdef PAUSE
|
||||
this->currentStage = 3;
|
||||
#ifdef DEBUG_PAUSE
|
||||
this->current_stage_ = 3;
|
||||
#else
|
||||
this->current_stage_ = current_stage;
|
||||
#endif
|
||||
@@ -106,7 +106,7 @@ Game::Game(int num_players, int current_stage, SDL_Renderer *renderer, bool demo
|
||||
|
||||
// Inicializa las variables necesarias para la sección 'Game'
|
||||
init();
|
||||
#ifdef PAUSE
|
||||
#ifdef DEBUG_PAUSE
|
||||
pause = false;
|
||||
#endif
|
||||
}
|
||||
@@ -270,10 +270,8 @@ void Game::init() {
|
||||
balloons_popped_ += stage_[i].power_to_complete;
|
||||
}
|
||||
|
||||
total_power_to_complete_game_ = 0;
|
||||
for (auto &i : stage_) {
|
||||
total_power_to_complete_game_ += i.power_to_complete;
|
||||
}
|
||||
total_power_to_complete_game_ = std::accumulate(std::begin(stage_), std::end(stage_), 0,
|
||||
[](int acc, const auto &s) { return acc + s.power_to_complete; });
|
||||
|
||||
// Modo demo
|
||||
demo_.recording = false;
|
||||
@@ -1321,7 +1319,7 @@ void Game::setHiScore(Uint32 score) {
|
||||
// Actualiza el valor de hiScore en caso necesario
|
||||
void Game::updateHiScore() {
|
||||
// Si la puntuación actual es mayor que la máxima puntuación
|
||||
for (auto *player : players_) {
|
||||
for (const auto *player : players_) {
|
||||
if (player->getScore() > hi_score_) {
|
||||
// Actualiza la máxima puntuación
|
||||
hi_score_ = player->getScore();
|
||||
@@ -1511,7 +1509,7 @@ void Game::updateStage() {
|
||||
void Game::updateDeath() {
|
||||
// Comprueba si todos los jugadores estan muertos
|
||||
bool all_dead = true;
|
||||
for (auto *player : players_) {
|
||||
for (const auto *player : players_) {
|
||||
all_dead &= (!player->isAlive());
|
||||
}
|
||||
|
||||
@@ -2185,7 +2183,7 @@ void Game::updateDeathSequence() {
|
||||
|
||||
// Calcula y establece el valor de amenaza en funcion de los globos activos
|
||||
void Game::evaluateAndSetMenace() {
|
||||
menace_current_ = std::accumulate(balloons_.begin(), balloons_.end(), Uint8(0), [](Uint8 acc, Balloon *b) { return b->isEnabled() ? acc + b->getMenace() : acc; });
|
||||
menace_current_ = std::accumulate(balloons_.begin(), balloons_.end(), Uint8(0), [](Uint8 acc, const Balloon *b) { return b->isEnabled() ? acc + b->getMenace() : acc; });
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
@@ -2732,7 +2730,7 @@ void Game::iterate() {
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef PAUSE
|
||||
#ifdef DEBUG_PAUSE
|
||||
if (!pause)
|
||||
update();
|
||||
#else
|
||||
@@ -2761,7 +2759,7 @@ void Game::handleEvent(const SDL_Event *event) {
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef PAUSE
|
||||
#ifdef DEBUG_PAUSE
|
||||
if (event->type == SDL_EVENT_KEY_DOWN) {
|
||||
if (event->key.scancode == SDL_SCANCODE_P) {
|
||||
pause = !pause;
|
||||
@@ -3044,7 +3042,7 @@ auto Game::canPowerBallBeCreated() -> bool {
|
||||
|
||||
// Calcula el poder actual de los globos en pantalla
|
||||
auto Game::calculateScreenPower() -> int {
|
||||
return std::accumulate(balloons_.begin(), balloons_.end(), 0, [](int acc, Balloon *b) { return b->isEnabled() ? acc + b->getPower() : acc; });
|
||||
return std::accumulate(balloons_.begin(), balloons_.end(), 0, [](int acc, const Balloon *b) { return b->isEnabled() ? acc + b->getPower() : acc; });
|
||||
}
|
||||
|
||||
// Inicializa las variables que contienen puntos de ruta para mover objetos
|
||||
@@ -3121,7 +3119,7 @@ void Game::updateGameCompleted() {
|
||||
void Game::updateHelper() {
|
||||
// Solo ofrece ayuda cuando la amenaza es elevada
|
||||
if (menace_current_ > 15) {
|
||||
for (auto *player : players_) {
|
||||
for (const auto *player : players_) {
|
||||
helper_.need_coffee = player->getCoffees() == 0;
|
||||
|
||||
helper_.need_coffee_machine = !player->isPowerUp();
|
||||
@@ -3135,7 +3133,7 @@ void Game::updateHelper() {
|
||||
// Comprueba si todos los jugadores han muerto
|
||||
auto Game::allPlayersAreDead() -> bool {
|
||||
bool success = true;
|
||||
for (auto *player : players_) {
|
||||
for (const auto *player : players_) {
|
||||
success &= (!player->isAlive());
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -384,7 +384,7 @@ class Game {
|
||||
bool pause_initialized_; // Indica si la pausa ha sido inicializada
|
||||
bool game_over_initialized_; // Indica si el game over ha sido inicializado
|
||||
int game_over_post_fade_; // Opción a realizar cuando termina el fundido del game over
|
||||
#ifdef PAUSE
|
||||
#ifdef DEBUG_PAUSE
|
||||
bool pause;
|
||||
#endif
|
||||
};
|
||||
|
||||
+121
-156
@@ -1,7 +1,6 @@
|
||||
#include "game/ui/menu.h"
|
||||
|
||||
#include <algorithm> // for max, min
|
||||
#include <fstream> // for char_traits, basic_ifstream, basic_istream
|
||||
#include <numeric> // for accumulate
|
||||
#include <sstream> // for basic_stringstream
|
||||
|
||||
@@ -30,6 +29,7 @@ Menu::Menu(SDL_Renderer *renderer, const std::string &file)
|
||||
x_ = 0;
|
||||
y_ = 0;
|
||||
w_ = 0;
|
||||
h_ = 0;
|
||||
rect_bg_.rect = {.x = 0, .y = 0, .w = 0, .h = 0};
|
||||
rect_bg_.color = {0, 0, 0};
|
||||
rect_bg_.a = 0;
|
||||
@@ -90,7 +90,7 @@ Menu::~Menu() {
|
||||
// Parser compartido (recibe cualquier istream)
|
||||
auto Menu::parseFromStream(std::istream &file, const std::string &filename) -> bool {
|
||||
bool success = true;
|
||||
bool textAllocated = false;
|
||||
bool text_allocated = false;
|
||||
std::string line;
|
||||
(void)filename;
|
||||
|
||||
@@ -107,14 +107,14 @@ auto Menu::parseFromStream(std::istream &file, const std::string &filename) -> b
|
||||
while (std::getline(file, line)) {
|
||||
strip_cr(line);
|
||||
if (line == "[item]") {
|
||||
Item newItem;
|
||||
newItem.label = "";
|
||||
newItem.h_padding_down = 1;
|
||||
newItem.selectable = true;
|
||||
newItem.greyed = false;
|
||||
newItem.linked_down = false;
|
||||
newItem.visible = true;
|
||||
newItem.line = false;
|
||||
Item new_item;
|
||||
new_item.label = "";
|
||||
new_item.h_padding_down = 1;
|
||||
new_item.selectable = true;
|
||||
new_item.greyed = false;
|
||||
new_item.linked_down = false;
|
||||
new_item.visible = true;
|
||||
new_item.line = false;
|
||||
|
||||
do {
|
||||
if (!std::getline(file, line)) {
|
||||
@@ -122,12 +122,12 @@ auto Menu::parseFromStream(std::istream &file, const std::string &filename) -> b
|
||||
}
|
||||
strip_cr(line);
|
||||
int pos = line.find('=');
|
||||
if (!setItem(&newItem, line.substr(0, pos), line.substr(pos + 1, line.length()))) {
|
||||
if (!setItem(&new_item, line.substr(0, pos), line.substr(pos + 1, line.length()))) {
|
||||
success = false;
|
||||
}
|
||||
} while (line != "[/item]");
|
||||
|
||||
addItem(newItem);
|
||||
addItem(new_item);
|
||||
} else {
|
||||
int pos = line.find('=');
|
||||
if (!setVars(line.substr(0, pos), line.substr(pos + 1, line.length()))) {
|
||||
@@ -136,11 +136,11 @@ auto Menu::parseFromStream(std::istream &file, const std::string &filename) -> b
|
||||
|
||||
// Crea el objeto text tan pronto como se pueda. Necesario para añadir items.
|
||||
// Carga via ResourceHelper para que funcione tanto con pack como con filesystem.
|
||||
if (!font_png_.empty() && !font_txt_.empty() && !textAllocated) {
|
||||
auto pngBytes = ResourceHelper::loadFile(Asset::get()->get(font_png_));
|
||||
auto txtBytes = ResourceHelper::loadFile(Asset::get()->get(font_txt_));
|
||||
text_ = new Text(pngBytes, txtBytes, renderer_);
|
||||
textAllocated = true;
|
||||
if (!font_png_.empty() && !font_txt_.empty() && !text_allocated) {
|
||||
auto png_bytes = ResourceHelper::loadFile(Asset::get()->get(font_png_));
|
||||
auto txt_bytes = ResourceHelper::loadFile(Asset::get()->get(font_txt_));
|
||||
text_ = new Text(png_bytes, txt_bytes, renderer_);
|
||||
text_allocated = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -149,22 +149,22 @@ auto Menu::parseFromStream(std::istream &file, const std::string &filename) -> b
|
||||
|
||||
// Carga la configuración del menu (vía ResourceHelper: pack si està inicialitzat, filesystem si no)
|
||||
auto Menu::load(const std::string &file_path) -> bool {
|
||||
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()) {
|
||||
return false;
|
||||
}
|
||||
return loadFromBytes(bytes, filename);
|
||||
return loadFromBytes(bytes, FILE_NAME);
|
||||
}
|
||||
|
||||
// Carga el menu desde bytes en memoria
|
||||
auto Menu::loadFromBytes(const std::vector<uint8_t> &bytes, const std::string &nameForLogs) -> bool {
|
||||
auto Menu::loadFromBytes(const std::vector<uint8_t> &bytes, const std::string &name_for_logs) -> bool {
|
||||
if (bytes.empty()) {
|
||||
return false;
|
||||
}
|
||||
std::string content(reinterpret_cast<const char *>(bytes.data()), bytes.size());
|
||||
std::stringstream ss(content);
|
||||
bool ok = parseFromStream(ss, nameForLogs);
|
||||
bool ok = parseFromStream(ss, name_for_logs);
|
||||
setSelectorItemColors();
|
||||
reset();
|
||||
return ok;
|
||||
@@ -213,128 +213,93 @@ auto Menu::setItem(Item *item, const std::string &var, const std::string &value)
|
||||
return success;
|
||||
}
|
||||
|
||||
// Helper de setVars: variables de tipo sonido
|
||||
auto Menu::trySetSoundVar(const std::string &var, const std::string &value) -> bool {
|
||||
Ja::Sound **target = nullptr;
|
||||
if (var == "sound_cancel") {
|
||||
target = &sound_cancel_;
|
||||
} else if (var == "sound_accept") {
|
||||
target = &sound_accept_;
|
||||
} else if (var == "sound_move") {
|
||||
target = &sound_move_;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
auto bytes = ResourceHelper::loadFile(Asset::get()->get(value));
|
||||
if (!bytes.empty()) {
|
||||
*target = Ja::loadSound(bytes.data(), (uint32_t)bytes.size());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Helper de setVars: variables de tipo color
|
||||
auto Menu::trySetColorVar(const std::string &var, const std::string &value) -> bool {
|
||||
Color *color_target = nullptr;
|
||||
int *alpha_target = nullptr;
|
||||
if (var == "backgroundColor") {
|
||||
color_target = &rect_bg_.color;
|
||||
alpha_target = &rect_bg_.a;
|
||||
} else if (var == "selector_color") {
|
||||
color_target = &selector_.color;
|
||||
alpha_target = &selector_.a;
|
||||
} else if (var == "selector_text_color") {
|
||||
color_target = &selector_.item_color;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
std::stringstream ss(value);
|
||||
std::string tmp;
|
||||
getline(ss, tmp, ',');
|
||||
color_target->r = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
color_target->g = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
color_target->b = std::stoi(tmp);
|
||||
if (alpha_target != nullptr) {
|
||||
getline(ss, tmp, ',');
|
||||
*alpha_target = std::stoi(tmp);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Asigna variables a partir de dos cadenas
|
||||
auto Menu::setVars(const std::string &var, const std::string &value) -> bool {
|
||||
// Indicador de éxito en la asignación
|
||||
bool success = true;
|
||||
if (trySetSoundVar(var, value)) {
|
||||
return true;
|
||||
}
|
||||
if (trySetColorVar(var, value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (var == "font_png") {
|
||||
font_png_ = value;
|
||||
}
|
||||
|
||||
else if (var == "font_txt") {
|
||||
} else if (var == "font_txt") {
|
||||
font_txt_ = value;
|
||||
}
|
||||
|
||||
else if (var == "sound_cancel") {
|
||||
auto bytes = ResourceHelper::loadFile(Asset::get()->get(value));
|
||||
if (!bytes.empty()) {
|
||||
sound_cancel_ = Ja::loadSound(bytes.data(), (uint32_t)bytes.size());
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "sound_accept") {
|
||||
auto bytes = ResourceHelper::loadFile(Asset::get()->get(value));
|
||||
if (!bytes.empty()) {
|
||||
sound_accept_ = Ja::loadSound(bytes.data(), (uint32_t)bytes.size());
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "sound_move") {
|
||||
auto bytes = ResourceHelper::loadFile(Asset::get()->get(value));
|
||||
if (!bytes.empty()) {
|
||||
sound_move_ = Ja::loadSound(bytes.data(), (uint32_t)bytes.size());
|
||||
}
|
||||
}
|
||||
|
||||
else if (var == "name") {
|
||||
} else if (var == "name") {
|
||||
name_ = value;
|
||||
}
|
||||
|
||||
else if (var == "x") {
|
||||
} else if (var == "x") {
|
||||
x_ = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "centerX") {
|
||||
center_x_ = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "centerY") {
|
||||
center_y_ = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "y") {
|
||||
} else if (var == "y") {
|
||||
y_ = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "backgroundType") {
|
||||
} else if (var == "centerX") {
|
||||
center_x_ = std::stoi(value);
|
||||
} else if (var == "centerY") {
|
||||
center_y_ = std::stoi(value);
|
||||
} else if (var == "backgroundType") {
|
||||
background_type_ = std::stoi(value);
|
||||
}
|
||||
|
||||
else if (var == "backgroundColor") {
|
||||
// Se introducen los valores separados por comas en un vector
|
||||
std::stringstream ss(value);
|
||||
std::string tmp;
|
||||
getline(ss, tmp, ',');
|
||||
rect_bg_.color.r = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
rect_bg_.color.g = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
rect_bg_.color.b = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
rect_bg_.a = std::stoi(tmp);
|
||||
}
|
||||
|
||||
else if (var == "selector_color") {
|
||||
// Se introducen los valores separados por comas en un vector
|
||||
std::stringstream ss(value);
|
||||
std::string tmp;
|
||||
getline(ss, tmp, ',');
|
||||
selector_.color.r = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
selector_.color.g = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
selector_.color.b = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
selector_.a = std::stoi(tmp);
|
||||
}
|
||||
|
||||
else if (var == "selector_text_color") {
|
||||
// Se introducen los valores separados por comas en un vector
|
||||
std::stringstream ss(value);
|
||||
std::string tmp;
|
||||
getline(ss, tmp, ',');
|
||||
selector_.item_color.r = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
selector_.item_color.g = std::stoi(tmp);
|
||||
getline(ss, tmp, ',');
|
||||
selector_.item_color.b = std::stoi(tmp);
|
||||
}
|
||||
|
||||
else if (var == "areElementsCenteredOnX") {
|
||||
are_elements_centered_on_x_ = value == "true";
|
||||
}
|
||||
|
||||
else if (var == "isCenteredOnX") {
|
||||
is_centered_on_x_ = value == "true";
|
||||
}
|
||||
|
||||
else if (var == "isCenteredOnY") {
|
||||
is_centered_on_y_ = value == "true";
|
||||
}
|
||||
|
||||
else if (var == "defaultActionWhenCancel") {
|
||||
} else if (var == "areElementsCenteredOnX") {
|
||||
are_elements_centered_on_x_ = (value == "true");
|
||||
} else if (var == "isCenteredOnX") {
|
||||
is_centered_on_x_ = (value == "true");
|
||||
} else if (var == "isCenteredOnY") {
|
||||
is_centered_on_y_ = (value == "true");
|
||||
} else if (var == "defaultActionWhenCancel") {
|
||||
default_action_when_cancel_ = std::stoi(value);
|
||||
} else if (var.empty()) {
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
else if (var.empty()) {
|
||||
}
|
||||
|
||||
else {
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Carga los ficheros de audio
|
||||
@@ -365,9 +330,9 @@ auto Menu::getName() const -> const std::string & {
|
||||
// Obtiene el valor de la variable
|
||||
auto Menu::getItemSelected() -> int {
|
||||
// Al llamar a esta funcion, se obtiene el valor y se borra
|
||||
const int temp = item_selected_;
|
||||
const int TEMP = item_selected_;
|
||||
item_selected_ = MENU_NO_OPTION;
|
||||
return temp;
|
||||
return TEMP;
|
||||
}
|
||||
|
||||
// Actualiza la posicion y el estado del selector
|
||||
@@ -563,21 +528,21 @@ void Menu::update() {
|
||||
void Menu::render() {
|
||||
// Rendereritza el fondo del menu
|
||||
if (background_type_ == MENU_BACKGROUND_SOLID) {
|
||||
SDL_FRect fBG = {(float)rect_bg_.rect.x, (float)rect_bg_.rect.y, (float)rect_bg_.rect.w, (float)rect_bg_.rect.h};
|
||||
SDL_FRect f_bg = {(float)rect_bg_.rect.x, (float)rect_bg_.rect.y, (float)rect_bg_.rect.w, (float)rect_bg_.rect.h};
|
||||
SDL_SetRenderDrawColor(renderer_, rect_bg_.color.r, rect_bg_.color.g, rect_bg_.color.b, rect_bg_.a);
|
||||
SDL_RenderFillRect(renderer_, &fBG);
|
||||
SDL_RenderFillRect(renderer_, &f_bg);
|
||||
}
|
||||
|
||||
// Renderiza el rectangulo del selector
|
||||
const SDL_FRect fTemp = {(float)selector_.rect.x, (float)(selector_.rect.y - 1), (float)selector_.rect.w, (float)(selector_.rect.h + 1)};
|
||||
const SDL_FRect F_TEMP = {(float)selector_.rect.x, (float)(selector_.rect.y - 1), (float)selector_.rect.w, (float)(selector_.rect.h + 1)};
|
||||
SDL_SetRenderDrawColor(renderer_, selector_.color.r, selector_.color.g, selector_.color.b, selector_.a);
|
||||
SDL_RenderFillRect(renderer_, &fTemp);
|
||||
SDL_RenderFillRect(renderer_, &F_TEMP);
|
||||
|
||||
// Renderiza el borde del fondo
|
||||
if (background_type_ == MENU_BACKGROUND_SOLID) {
|
||||
SDL_FRect fBGBorder = {(float)rect_bg_.rect.x, (float)rect_bg_.rect.y, (float)rect_bg_.rect.w, (float)rect_bg_.rect.h};
|
||||
SDL_FRect f_bg_border = {(float)rect_bg_.rect.x, (float)rect_bg_.rect.y, (float)rect_bg_.rect.w, (float)rect_bg_.rect.h};
|
||||
SDL_SetRenderDrawColor(renderer_, rect_bg_.color.r, rect_bg_.color.g, rect_bg_.color.b, 255);
|
||||
SDL_RenderRect(renderer_, &fBGBorder);
|
||||
SDL_RenderRect(renderer_, &f_bg_border);
|
||||
}
|
||||
|
||||
// Crea una linea por si hay que dibujarla entre los items
|
||||
@@ -601,13 +566,13 @@ void Menu::render() {
|
||||
}
|
||||
|
||||
else if (i == selector_.index) { // A continuación si tiene el indice
|
||||
const Color color = {selector_.item_color.r, selector_.item_color.g, selector_.item_color.b};
|
||||
text_->writeColored(items_[i].rect.x, items_[i].rect.y, items_[i].label, color);
|
||||
const Color COLOR = {selector_.item_color.r, selector_.item_color.g, selector_.item_color.b};
|
||||
text_->writeColored(items_[i].rect.x, items_[i].rect.y, items_[i].label, COLOR);
|
||||
}
|
||||
|
||||
else if (i == selector_.previous_index) { // O si lo ha tenido
|
||||
const Color color = {selector_.previous_item_color.r, selector_.previous_item_color.g, selector_.previous_item_color.b};
|
||||
text_->writeColored(items_[i].rect.x, items_[i].rect.y, items_[i].label, color);
|
||||
const Color COLOR = {selector_.previous_item_color.r, selector_.previous_item_color.g, selector_.previous_item_color.b};
|
||||
text_->writeColored(items_[i].rect.x, items_[i].rect.y, items_[i].label, COLOR);
|
||||
}
|
||||
|
||||
else if (items_[i].selectable) { // O si simplemente es un elemento normal
|
||||
@@ -616,8 +581,8 @@ void Menu::render() {
|
||||
|
||||
else { // Si no es seleccionable
|
||||
if ((items_[i].linked_up) && (i == selector_.index + 1)) { // Si el elemento está enlazado con el elemento superior se pinta del color del selector
|
||||
const Color color = {selector_.item_color.r, selector_.item_color.g, selector_.item_color.b};
|
||||
text_->writeColored(items_[i].rect.x, items_[i].rect.y, items_[i].label, color);
|
||||
const Color COLOR = {selector_.item_color.r, selector_.item_color.g, selector_.item_color.b};
|
||||
text_->writeColored(items_[i].rect.x, items_[i].rect.y, items_[i].label, COLOR);
|
||||
} else { // Si no está enlazado con el elemento superior se pinta con el color normal
|
||||
text_->write(items_[i].rect.x, items_[i].rect.y, items_[i].label);
|
||||
}
|
||||
@@ -811,9 +776,9 @@ auto Menu::findWidth() -> int {
|
||||
|
||||
// Calcula el alto del menu
|
||||
auto Menu::findHeight() -> int {
|
||||
const int height = std::accumulate(items_.begin(), items_.end(), 0, [](int acc, const Item &i) { return acc + i.rect.h + i.h_padding_down; });
|
||||
const int HEIGHT = std::accumulate(items_.begin(), items_.end(), 0, [](int acc, const Item &i) { return acc + i.rect.h + i.h_padding_down; });
|
||||
|
||||
return height - items_.back().h_padding_down;
|
||||
return HEIGHT - items_.back().h_padding_down;
|
||||
}
|
||||
|
||||
// Recoloca los elementos del menu en el eje Y
|
||||
@@ -878,17 +843,17 @@ void Menu::setText(const std::string &font_png, const std::string &font_txt) {
|
||||
|
||||
// Calcula los colores del selector para el degradado
|
||||
void Menu::setSelectorItemColors() {
|
||||
const Color colorFrom = {255, 255, 255};
|
||||
const Color colorTo = selector_.item_color;
|
||||
const Color COLOR_FROM = {255, 255, 255};
|
||||
const Color COLOR_TO = selector_.item_color;
|
||||
|
||||
for (int i = 0; i < selector_.num_jumps; ++i) {
|
||||
const float step = ((float)i / (selector_.num_jumps - 1));
|
||||
const int r = colorFrom.r + ((colorTo.r - colorFrom.r) * step);
|
||||
const int g = colorFrom.g + ((colorTo.g - colorFrom.g) * step);
|
||||
const int b = colorFrom.b + ((colorTo.b - colorFrom.b) * step);
|
||||
selector_.jump_item_colors[i].r = r;
|
||||
selector_.jump_item_colors[i].g = g;
|
||||
selector_.jump_item_colors[i].b = b;
|
||||
const float STEP = ((float)i / (selector_.num_jumps - 1));
|
||||
const int R = COLOR_FROM.r + ((COLOR_TO.r - COLOR_FROM.r) * STEP);
|
||||
const int G = COLOR_FROM.g + ((COLOR_TO.g - COLOR_FROM.g) * STEP);
|
||||
const int B = COLOR_FROM.b + ((COLOR_TO.b - COLOR_FROM.b) * STEP);
|
||||
selector_.jump_item_colors[i].r = R;
|
||||
selector_.jump_item_colors[i].g = G;
|
||||
selector_.jump_item_colors[i].b = B;
|
||||
}
|
||||
|
||||
selector_.item_color_index = 0;
|
||||
|
||||
@@ -109,6 +109,8 @@ class Menu {
|
||||
auto load(const std::string &file_path) -> bool; // Carga la configuración del menu desde un archivo de texto
|
||||
auto parseFromStream(std::istream &file, const std::string &filename) -> bool; // Parser compartido (recibe cualquier istream)
|
||||
auto setVars(const std::string &var, const std::string &value) -> bool; // Asigna variables a partir de dos cadenas
|
||||
auto trySetSoundVar(const std::string &var, const std::string &value) -> bool; // Helper de setVars: variables de tipo sonido
|
||||
auto trySetColorVar(const std::string &var, const std::string &value) -> bool; // Helper de setVars: variables de tipo color
|
||||
static auto setItem(Item *item, const std::string &var, const std::string &value) -> bool; // Asigna variables a partir de dos cadenas
|
||||
|
||||
void reorganize(); // Actualiza el menu para recolocarlo correctamente y establecer el tamaño
|
||||
|
||||
Reference in New Issue
Block a user