diff --git a/Makefile b/Makefile index 22d5d65..fcd73fd 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ executable = coffee_crisis windows: @echo off if not exist bin\ (mkdir bin) - g++ -std=c++11 -Wall -O2 source/*.cpp -lmingw32 -lSDL2main -lSDL2 -ffunction-sections -fdata-sections -Wl,--gc-sections -static-libstdc++ -o bin/$(executable).exe + g++ -std=c++11 -Wall -O2 source/*.cpp -lmingw32 -lSDL2main -lSDL2 -ffunction-sections -fdata-sections -Wl,--gc-sections -static-libstdc++ -Wl,-subsystem,windows -o bin/$(executable).exe strip -s -R .comment -R .gnu.version bin/$(executable).exe --strip-unneeded macos: mkdir -p bin diff --git a/source/asset.cpp b/source/asset.cpp index 894a899..f00fb2f 100644 --- a/source/asset.cpp +++ b/source/asset.cpp @@ -4,7 +4,7 @@ Asset::Asset(std::string path) { executablePath = path; - longest_name = 0; + longestName = 0; } // Destructor @@ -13,7 +13,7 @@ Asset::~Asset() } // Añade un elemento a la lista -void Asset::add(std::string file, enum assetType_e type, bool required) +void Asset::add(std::string file, enum assetType type, bool required) { item_t temp; temp.file = executablePath + file; @@ -22,7 +22,7 @@ void Asset::add(std::string file, enum assetType_e type, bool required) fileList.push_back(temp); const std::string filename = file.substr(file.find_last_of("\\/") + 1); - longest_name = SDL_max(longest_name, filename.size()); + longestName = SDL_max(longestName, filename.size()); } // Devuelve el fichero de un elemento de la lista a partir de una cadena @@ -106,7 +106,7 @@ bool Asset::checkFile(std::string path) SDL_RWclose(file); } - const std::string s = "Checking file %-" + std::to_string(longest_name) + "s [" + result + "]\n"; + const std::string s = "Checking file %-" + std::to_string(longestName) + "s [" + result + "]\n"; printf(s.c_str(), filename.c_str()); return success; diff --git a/source/asset.h b/source/asset.h index 309cab6..afcabff 100644 --- a/source/asset.h +++ b/source/asset.h @@ -7,7 +7,7 @@ #ifndef ASSET_H #define ASSET_H -enum assetType_e +enum assetType { t_bitmap, t_music, @@ -29,11 +29,11 @@ private: struct item_t { std::string file; // Ruta del fichero desde la raiz del directorio - enum assetType_e type; // Indica el tipo de recurso + enum assetType type; // Indica el tipo de recurso bool required; // Indica si es un fichero que debe de existir }; - int longest_name; // Contiene la longitud del nombre de fichero mas largo + int longestName; // Contiene la longitud del nombre de fichero mas largo std::vector fileList; std::string executablePath; @@ -52,7 +52,7 @@ public: ~Asset(); // Añade un elemento a la lista - void add(std::string file, enum assetType_e type, bool required = true); + void add(std::string file, enum assetType type, bool required = true); // Devuelve un elemento de la lista a partir de una cadena std::string get(std::string text); diff --git a/source/input.cpp b/source/input.cpp index cc0d469..a4210c9 100644 --- a/source/input.cpp +++ b/source/input.cpp @@ -97,7 +97,7 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index) { if (repeat) { - if (SDL_GameControllerGetButton(connectedControllers.at(index), gameControllerBindings[input].button) != 0) + if (SDL_GameControllerGetButton(connectedControllers.at(index), gameControllerBindings.at(input).button) != 0) { successGameController = true; } @@ -108,11 +108,11 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index) } else { - if (!gameControllerBindings[input].active) + if (!gameControllerBindings.at(input).active) { - if (SDL_GameControllerGetButton(connectedControllers.at(index), gameControllerBindings[input].button) != 0) + if (SDL_GameControllerGetButton(connectedControllers.at(index), gameControllerBindings.at(input).button) != 0) { - gameControllerBindings[input].active = true; + gameControllerBindings.at(input).active = true; successGameController = true; } else @@ -122,9 +122,9 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index) } else { - if (SDL_GameControllerGetButton(connectedControllers.at(index), gameControllerBindings[input].button) == 0) + if (SDL_GameControllerGetButton(connectedControllers.at(index), gameControllerBindings.at(input).button) == 0) { - gameControllerBindings[input].active = false; + gameControllerBindings.at(input).active = false; successGameController = false; } else @@ -138,6 +138,44 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index) return (successKeyboard || successGameController); } +// Comprueba si hay almenos un input activo +bool Input::checkAnyInput(int device, int index) +{ + if (device == INPUT_USE_ANY) + { + index = 0; + } + + if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY) + { + const Uint8 *mKeystates = SDL_GetKeyboardState(nullptr); + + for (int i = 0; i < (int)keyBindings.size(); ++i) + { + if (mKeystates[keyBindings.at(i).scancode] != 0) + { + return true; + } + } + } + + if (gameControllerFound()) + { + if (device == INPUT_USE_GAMECONTROLLER || device == INPUT_USE_ANY) + { + for (int i = 0; i < (int)gameControllerBindings.size(); ++i) + { + if (SDL_GameControllerGetButton(connectedControllers.at(index), gameControllerBindings.at(i).button) != 0) + { + return true; + } + } + } + } + + return false; +} + // Comprueba si hay un mando conectado bool Input::discoverGameController() { diff --git a/source/input.h b/source/input.h index 62c7865..a2eab87 100644 --- a/source/input.h +++ b/source/input.h @@ -74,6 +74,9 @@ public: // Comprueba si un input esta activo bool checkInput(Uint8 input, bool repeat, int device = INPUT_USE_ANY, int index = 0); + // Comprueba si hay almenos un input activo + bool checkAnyInput(int device, int index); + // Comprueba si hay algun mando conectado bool gameControllerFound(); diff --git a/source/menu.cpp b/source/menu.cpp index 8c0f904..2d7771e 100644 --- a/source/menu.cpp +++ b/source/menu.cpp @@ -171,9 +171,6 @@ bool Menu::load(std::string file_path) success = false; } - // Reorganiza el menu con los valores recien cargados - // reorganize(); - return success; } @@ -211,6 +208,7 @@ bool Menu::setItem(item_t *item, std::string var, std::string value) else if ((var == "") || (var == "[/item]")) { } + else { success = false; @@ -355,11 +353,6 @@ bool Menu::setVars(std::string var, std::string value) return success; } -// Inicializa las variables -void Menu::init() -{ -} - // Carga los ficheros de audio void Menu::loadAudioFile(std::string file, int sound) { @@ -412,7 +405,8 @@ void Menu::updateSelector() selector.moving = false; } } - if (selector.despY < 0) // Va hacia arriba + + else if (selector.despY < 0) // Va hacia arriba { if (selector.y < selector.targetY) // Ha llegado al destino { @@ -440,7 +434,8 @@ void Menu::updateSelector() selector.resizing = false; } } - if (selector.incH < 0) // Decrece + + else if (selector.incH < 0) // Decrece { if (selector.h < selector.targetH) // Ha llegado al destino { @@ -463,7 +458,7 @@ void Menu::setSelectorPos(int index) if (index < (int)item.size()) { selector.index = index; - selector.rect.y = selector.y = selector.originY = selector.targetY = item[selector.index].rect.y; + selector.rect.y = selector.y = selector.originY = selector.targetY = item.at(selector.index).rect.y; selector.rect.w = rectBG.rect.w; selector.rect.x = rectBG.rect.x; selector.originH = selector.targetH = selector.rect.h = getSelectorHeight(selector.index); @@ -491,13 +486,13 @@ void Menu::reset() { itemSelected = MENU_NO_OPTION; selector.index = 0; - selector.originY = selector.targetY = selector.y = item[0].rect.y; - selector.originH = selector.targetH = item[0].rect.h; + selector.originY = selector.targetY = selector.y = item.at(0).rect.y; + selector.originH = selector.targetH = item.at(0).rect.h; selector.moving = false; selector.resizing = false; // Si el primer elemento no es seleccionable, incrementa el selector - if (!item[selector.index].selectable) + if (!item.at(selector.index).selectable) { increaseSelectorIndex(); setSelectorPos(selector.index); @@ -529,18 +524,18 @@ void Menu::reorganize() bool Menu::increaseSelectorIndex() { // Obten las coordenadas del elemento actual - selector.y = selector.originY = item[selector.index].rect.y; + selector.y = selector.originY = item.at(selector.index).rect.y; selector.h = selector.originH = getSelectorHeight(selector.index); // Calcula cual es el siguiente elemento ++selector.index %= item.size(); - while (!item[selector.index].selectable) + while (!item.at(selector.index).selectable) { ++selector.index %= item.size(); } // Establece las coordenadas y altura de destino - selector.targetY = item[selector.index].rect.y; + selector.targetY = item.at(selector.index).rect.y; selector.despY = (selector.targetY - selector.originY) / selector.numJumps; selector.targetH = getSelectorHeight(selector.index); @@ -559,7 +554,7 @@ bool Menu::increaseSelectorIndex() bool Menu::decreaseSelectorIndex() { // Obten las coordenadas del elemento actual - selector.y = selector.originY = item[selector.index].rect.y; + selector.y = selector.originY = item.at(selector.index).rect.y; selector.h = selector.originH = getSelectorHeight(selector.index); // Calcula cual es el siguiente elemento @@ -572,7 +567,7 @@ bool Menu::decreaseSelectorIndex() selector.index--; } - while (!item[selector.index].selectable) + while (!item.at(selector.index).selectable) { if (selector.index == 0) { @@ -585,7 +580,7 @@ bool Menu::decreaseSelectorIndex() } // Establece las coordenadas y altura de destino - selector.targetY = item[selector.index].rect.y; + selector.targetY = item.at(selector.index).rect.y; selector.despY = (selector.targetY - selector.originY) / selector.numJumps; selector.targetH = getSelectorHeight(selector.index); @@ -635,28 +630,29 @@ void Menu::render() if (i == selector.index) { const color_t color = {selector.itemColor.r, selector.itemColor.g, selector.itemColor.b}; - text->writeColored(item[i].rect.x, item[i].rect.y, item[i].label, color); + text->writeColored(item.at(i).rect.x, item.at(i).rect.y, item.at(i).label, color); } - else if (item[i].selectable) + else if (item.at(i).selectable) { - text->write(item[i].rect.x, item[i].rect.y, item[i].label); + text->write(item.at(i).rect.x, item.at(i).rect.y, item.at(i).label); } - else if (item[i].greyed) + else if (item.at(i).greyed) { - text->writeColored(item[i].rect.x, item[i].rect.y, item[i].label, colorGreyed); + text->writeColored(item.at(i).rect.x, item.at(i).rect.y, item.at(i).label, colorGreyed); } + else { // No seleccionable - if ((item[i].linkedUp) && (i == selector.index + 1)) + if ((item.at(i).linkedUp) && (i == selector.index + 1)) { const color_t color = {selector.itemColor.r, selector.itemColor.g, selector.itemColor.b}; - text->writeColored(item[i].rect.x, item[i].rect.y, item[i].label, color); + text->writeColored(item.at(i).rect.x, item.at(i).rect.y, item.at(i).label, color); } else // No enlazado con el de arriba { - text->write(item[i].rect.x, item[i].rect.y, item[i].label); + text->write(item.at(i).rect.x, item.at(i).rect.y, item.at(i).label); } } } @@ -918,42 +914,42 @@ int Menu::findHeight() // Recoloca los elementos del menu en el eje Y void Menu::replaceElementsOnY() { - item[0].rect.y = y; + item.at(0).rect.y = y; for (int i = 1; i < (int)item.size(); i++) { - item[i].rect.y = item[i - 1].rect.y + item[i - 1].rect.h + item[i - 1].hPaddingDown; + item.at(i).rect.y = item.at(i - 1).rect.y + item.at(i - 1).rect.h + item.at(i - 1).hPaddingDown; } } // Establece el estado seleccionable de un item void Menu::setSelectable(int index, bool value) { - item[index].selectable = value; + item.at(index).selectable = value; } // Establece el estado agrisado de un item void Menu::setGreyed(int index, bool value) { - item[index].greyed = value; + item.at(index).greyed = value; } // Establece el estado de enlace de un item void Menu::setLinkedDown(int index, bool value) { - item[index].linkedDown = value; + item.at(index).linkedDown = value; } // Calcula la altura del selector int Menu::getSelectorHeight(int value) { - if (item[value].linkedDown) + if (item.at(value).linkedDown) { - return item[value].rect.h + item[value].hPaddingDown + item[value + 1].rect.h; + return item.at(value).rect.h + item.at(value).hPaddingDown + item.at(value + 1).rect.h; } else { - return item[value].rect.h; + return item.at(value).rect.h; } } diff --git a/source/menu.h b/source/menu.h index d67fa6b..fba00be 100644 --- a/source/menu.h +++ b/source/menu.h @@ -108,9 +108,6 @@ private: // Asigna variables a partir de dos cadenas bool setItem(item_t *item, std::string var, std::string value); - // Inicializa las variables - void init(); - // Actualiza el menu para recolocarlo correctamente y establecer el tamaño void reorganize(); diff --git a/source/movingsprite.cpp b/source/movingsprite.cpp index 254f3ae..f3efc9a 100644 --- a/source/movingsprite.cpp +++ b/source/movingsprite.cpp @@ -160,11 +160,13 @@ double MovingSprite::getAngle() return angle; } -// Establece la posición del objeto -void MovingSprite::setPos(SDL_Rect rect) +// Establece la posición y el tamaño del objeto +void MovingSprite::setRect(SDL_Rect rect) { x = (float)rect.x; y = (float)rect.y; + w = rect.w; + h = rect.h; } // Establece el valor de la variable @@ -333,15 +335,6 @@ SDL_Rect MovingSprite::getRect() return rect; } -// Establece los valores de posición y tamaño del sprite -void MovingSprite::setRect(SDL_Rect rect) -{ - x = (float)rect.x; - y = (float)rect.y; - w = rect.w; - h = rect.h; -} - // Deshace el último movimiento void MovingSprite::undoMove() { diff --git a/source/movingsprite.h b/source/movingsprite.h index d94f8ed..9f305b3 100644 --- a/source/movingsprite.h +++ b/source/movingsprite.h @@ -88,8 +88,8 @@ public: // Obtiene el valor de la variable Uint16 getRotateSpeed(); - // Establece la posición del objeto - void setPos(SDL_Rect rect); + // Establece la posición y el tamaño del objeto + void setRect(SDL_Rect rect); // Establece el valor de la variable void setPosX(float value); @@ -151,9 +151,6 @@ public: // Devuelve el rectangulo donde está el sprite SDL_Rect getRect(); - // Establece los valores de posición y tamaño del sprite - void setRect(SDL_Rect rect); - // Deshace el último movimiento void undoMove(); diff --git a/source/screen.cpp b/source/screen.cpp index aa9bad0..6308cf4 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -21,7 +21,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options, i // Crea la textura donde se dibujan los graficos del juego gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth, gameCanvasHeight); - if (gameCanvas == NULL) + if (gameCanvas == nullptr) { printf("TitleSurface could not be created!\nSDL Error: %s\n", SDL_GetError()); } @@ -61,14 +61,14 @@ void Screen::start() void Screen::blit() { // Vuelve a dejar el renderizador en modo normal - SDL_SetRenderTarget(renderer, NULL); + SDL_SetRenderTarget(renderer, nullptr); // Borra el contenido previo SDL_SetRenderDrawColor(renderer, borderColor.r, borderColor.g, borderColor.b, 0xFF); SDL_RenderClear(renderer); // Copia la textura de juego en el renderizador en la posición adecuada - SDL_RenderCopy(renderer, gameCanvas, NULL, &dest); + SDL_RenderCopy(renderer, gameCanvas, nullptr, &dest); // Muestra por pantalla el renderizador SDL_RenderPresent(renderer); diff --git a/source/text.cpp b/source/text.cpp index b7c6eb7..d11381f 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -143,7 +143,9 @@ void Text::initOffsetFromFile(std::string file) { // Almacena solo las lineas impares if (line_read % 2 == 1) + { offset[index++].w = std::stoi(buffer); + } // Limpia el buffer buffer.clear();