diff --git a/data/map/01.map b/data/map/01.map index 9e7cb74..9930ac6 100644 --- a/data/map/01.map +++ b/data/map/01.map @@ -33,8 +33,8 @@ width=16 height=16 x=20 y=8 -vx=0 -vy=0.7 +vx=0.7 +vy=0 x1=5 y1=8 x2=33 diff --git a/data/map/01.tmx b/data/map/01.tmx index d1af713..5aa40cf 100644 --- a/data/map/01.tmx +++ b/data/map/01.tmx @@ -22,8 +22,8 @@ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,239,240,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,69,70,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,257,258,257,258,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,101,102,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,289,290,289,290,0,0,0,0,577,578,579,580,581,582,0,0,0,0,0,0,0,0,327,328,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,189,190,0,0,0,0,0,0,0,0,0,0,359,360,69,70,0,0,0,93,94,0,0,0,0,0,0, +0,0,0,289,290,289,290,0,0,0,0,577,578,579,580,581,582,0,0,0,0,0,0,0,0,583,584,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,189,190,0,0,0,0,0,0,0,0,0,0,615,616,69,70,0,0,0,93,94,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,189,190,0,0,0,0,0,0,0,0,0,0,189,190,101,102,0,0,0,125,126,0,0,0,0,0,0, 263,264,0,0,0,0,0,193,194,0,0,0,0,189,190,0,0,0,0,0,0,0,281,282,277,278,279,274,280,0,0,0,157,158,0,0,0,0,0,0, 295,296,0,0,0,0,0,225,226,0,0,0,0,189,190,0,0,281,275,275,275,275,313,314,309,310,311,312,304,0,0,0,189,190,0,0,0,0,0,0, diff --git a/source/enemy.cpp b/source/enemy.cpp index 7d5ae41..8c450b2 100644 --- a/source/enemy.cpp +++ b/source/enemy.cpp @@ -60,12 +60,14 @@ void Enemy::update() // Comprueba si ha llegado al limite del recorrido para darse media vuelta void Enemy::checkPath() { + // Comprueba los límites horizontales if (sprite->getPosX() > p2.x || sprite->getPosX() < p1.x) { sprite->setVelX(sprite->getVelX() * (-1)); sprite->flip(); } + // Comprueba los límites verticales if (sprite->getPosY() > p2.y || sprite->getPosY() < p1.y) { sprite->setVelY(sprite->getVelY() * (-1)); diff --git a/source/input.cpp b/source/input.cpp index 9ebb704..46ca2ba 100644 --- a/source/input.cpp +++ b/source/input.cpp @@ -5,16 +5,16 @@ Input::Input(std::string file) { // Fichero gamecontrollerdb.txt - mDBpath = file; + dbPath = file; // Inicializa las variables for (int i = 0; i < 17; i++) { - mKeyBindings[i].scancode = 0; - mKeyBindings[i].active = false; + keyBindings[i].scancode = 0; + keyBindings[i].active = false; - mGameControllerBindings[i].button = SDL_CONTROLLER_BUTTON_INVALID; - mGameControllerBindings[i].active = false; + gameControllerBindings[i].button = SDL_CONTROLLER_BUTTON_INVALID; + gameControllerBindings[i].active = false; } discoverGameController(); @@ -23,20 +23,20 @@ Input::Input(std::string file) // Destructor Input::~Input() { - for (int i = 0; i < mNumGamepads; i++) - mConnectedControllers[i] = nullptr; + for (int i = 0; i < numGamepads; i++) + connectedControllers[i] = nullptr; } // Asigna uno de los posibles inputs a una tecla del teclado void Input::bindKey(Uint8 input, SDL_Scancode code) { - mKeyBindings[input].scancode = code; + keyBindings[input].scancode = code; } // Asigna uno de los posibles inputs a un botón del mando void Input::bindGameControllerButton(Uint8 input, SDL_GameControllerButton button) { - mGameControllerBindings[input].button = button; + gameControllerBindings[input].button = button; } // Comprueba si un input esta activo @@ -54,18 +54,18 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index) if (repeat) { - if (mKeystates[mKeyBindings[input].scancode] != 0) + if (mKeystates[keyBindings[input].scancode] != 0) successKeyboard = true; else successKeyboard = false; } else { - if (!mKeyBindings[input].active) + if (!keyBindings[input].active) { - if (mKeystates[mKeyBindings[input].scancode] != 0) + if (mKeystates[keyBindings[input].scancode] != 0) { - mKeyBindings[input].active = true; + keyBindings[input].active = true; successKeyboard = true; } else @@ -75,9 +75,9 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index) } else { - if (mKeystates[mKeyBindings[input].scancode] == 0) + if (mKeystates[keyBindings[input].scancode] == 0) { - mKeyBindings[input].active = false; + keyBindings[input].active = false; successKeyboard = false; } else @@ -93,18 +93,18 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index) { if (repeat) { - if (SDL_GameControllerGetButton(mConnectedControllers[index], mGameControllerBindings[input].button) != 0) + if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) != 0) successGameController = true; else successGameController = false; } else { - if (!mGameControllerBindings[input].active) + if (!gameControllerBindings[input].active) { - if (SDL_GameControllerGetButton(mConnectedControllers[index], mGameControllerBindings[input].button) != 0) + if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) != 0) { - mGameControllerBindings[input].active = true; + gameControllerBindings[input].active = true; successGameController = true; } else @@ -114,9 +114,9 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index) } else { - if (SDL_GameControllerGetButton(mConnectedControllers[index], mGameControllerBindings[input].button) == 0) + if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) == 0) { - mGameControllerBindings[input].active = false; + gameControllerBindings[input].active = false; successGameController = false; } else @@ -138,43 +138,43 @@ bool Input::discoverGameController() if (SDL_WasInit(SDL_INIT_GAMECONTROLLER) != 1) SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER); - if (SDL_GameControllerAddMappingsFromFile(mDBpath.c_str()) < 0) - printf("Error, could not load %s file: %s\n", mDBpath.c_str(), SDL_GetError()); + if (SDL_GameControllerAddMappingsFromFile(dbPath.c_str()) < 0) + printf("Error, could not load %s file: %s\n", dbPath.c_str(), SDL_GetError()); int nJoysticks = SDL_NumJoysticks(); - mNumGamepads = 0; + numGamepads = 0; // Cuenta el numero de mandos for (int i = 0; i < nJoysticks; i++) if (SDL_IsGameController(i)) - mNumGamepads++; + numGamepads++; printf("\nChecking for game controllers...\n"); - printf("%i joysticks found, %i are gamepads\n", nJoysticks, mNumGamepads); + printf("%i joysticks found, %i are gamepads\n", nJoysticks, numGamepads); - if (mNumGamepads > 0) + if (numGamepads > 0) { found = true; - for (int i = 0; i < mNumGamepads; i++) + for (int i = 0; i < numGamepads; i++) { // Abre el mando y lo añade a la lista SDL_GameController *pad = SDL_GameControllerOpen(i); if (SDL_GameControllerGetAttached(pad) == 1) { - mConnectedControllers.push_back(pad); + connectedControllers.push_back(pad); std::string separator(" #"); std::string name = SDL_GameControllerNameForIndex(i); name.resize(25); name = name + separator + std::to_string(i); std::cout << name << std::endl; - mControllerNames.push_back(name); + controllerNames.push_back(name); } else std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl; } - //mGameController = mConnectedControllers[0]; + //mGameController = connectedControllers[0]; SDL_GameControllerEventState(SDL_ENABLE); } @@ -184,7 +184,7 @@ bool Input::discoverGameController() // Comprueba si hay algun mando conectado bool Input::gameControllerFound() { - if (mNumGamepads > 0) + if (numGamepads > 0) return true; else return false; @@ -193,8 +193,8 @@ bool Input::gameControllerFound() // Obten el nombre de un mando de juego std::string Input::getControllerName(int index) { - if (mNumGamepads > 0) - return mControllerNames[index]; + if (numGamepads > 0) + return controllerNames[index]; else return ""; } @@ -202,5 +202,5 @@ std::string Input::getControllerName(int index) // Obten el numero de mandos conectados int Input::getNumControllers() { - return mNumGamepads; + return numGamepads; } \ No newline at end of file diff --git a/source/input.h b/source/input.h index 7344003..9cc7af8 100644 --- a/source/input.h +++ b/source/input.h @@ -41,19 +41,19 @@ private: Uint8 scancode; // Scancode asociado bool active; // Indica si está activo }; - keyBindings_t mKeyBindings[17]; // Vector con las teclas asociadas a los inputs predefinidos + keyBindings_t keyBindings[17]; // Vector con las teclas asociadas a los inputs predefinidos struct GameControllerBindings_t { SDL_GameControllerButton button; // GameControllerButton asociado bool active; // Indica si está activo }; - GameControllerBindings_t mGameControllerBindings[17]; // Vector con las teclas asociadas a los inputs predefinidos + GameControllerBindings_t gameControllerBindings[17]; // Vector con las teclas asociadas a los inputs predefinidos - std::vector mConnectedControllers; // Vector con todos los mandos conectados - std::vector mControllerNames; // Vector con los nombres de los mandos - int mNumGamepads; // Numero de mandos conectados - std::string mDBpath; // Ruta al archivo gamecontrollerdb.txt + std::vector connectedControllers; // Vector con todos los mandos conectados + std::vector controllerNames; // Vector con los nombres de los mandos + int numGamepads; // Numero de mandos conectados + std::string dbPath; // Ruta al archivo gamecontrollerdb.txt // Comprueba si hay un mando conectado bool discoverGameController(); diff --git a/source/menu.cpp b/source/menu.cpp index 2dbc492..02bcd57 100644 --- a/source/menu.cpp +++ b/source/menu.cpp @@ -2,20 +2,20 @@ #include "menu.h" // Constructor -Menu::Menu(SDL_Renderer *renderer, Text *text, Input *input, std::string *fileList) +Menu::Menu(SDL_Renderer *renderer, Text *text, Input *input, Asset *asset) { - mRenderer = renderer; - mText = text; - mInput = input; - mFileList = fileList; + this->renderer = renderer; + this->text = text; + this->input = input; + this->asset = asset; } Menu::~Menu() { - mRenderer = nullptr; - mText = nullptr; - mInput = nullptr; - mFileList = nullptr; + renderer = nullptr; + text = nullptr; + input = nullptr; + asset = nullptr; } // Inicializador @@ -24,56 +24,51 @@ void Menu::init(std::string name, int x, int y, int backgroundType) loadMedia(); // Inicia variables - mName = name; - mSelector.index = 0; - mTotalItems = 0; - mItemSelected = MENU_NO_OPTION; - mPosX = x; - mPosY = y; - mRectBG.rect.x = 0; - mRectBG.rect.y = 0; - mRectBG.rect.w = 0; - mRectBG.rect.h = 0; - mRectBG.r = 0; - mRectBG.g = 0; - mRectBG.b = 0; - mBackgroundType = backgroundType; - mIsCenteredOnX = false; - mIsCenteredOnY = false; - mAreElementsCenteredOnX = false; - mCenterX = 0; - mCenterY = 0; - mWidestItem = 0; - mColorGreyed = {128, 128, 128}; + this->name = name; + selector.index = 0; + totalItems = 0; + itemSelected = MENU_NO_OPTION; + this->x = x; + this->y = y; + rectBG.rect = {0,0,0,0}; + rectBG.color = {0,0,0}; + rectBG.a = 0; + this->backgroundType = backgroundType; + isCenteredOnX = false; + isCenteredOnY = false; + areElementsCenteredOnX = false; + centerX = 0; + centerY = 0; + widestItem = 0; + colorGreyed = {128, 128, 128}; // Selector - mSelector.originY = 0; - mSelector.targetY = 0; - mSelector.despY = 0; - mSelector.originH = 0; - mSelector.targetH = 0; - mSelector.incH = 0; - mSelector.y = 0.0f; - mSelector.h = 0.0f; - mSelector.numJumps = 8; - mSelector.moving = false; - mSelector.resizing = false; - mSelector.rect = {0, 0, 0, 0}; - mSelector.r = 0; - mSelector.g = 0; - mSelector.b = 0; - mSelector.a = 255; + selector.originY = 0; + selector.targetY = 0; + selector.despY = 0; + selector.originH = 0; + selector.targetH = 0; + selector.incH = 0; + selector.y = 0.0f; + selector.h = 0.0f; + selector.numJumps = 8; + selector.moving = false; + selector.resizing = false; + selector.rect = {0, 0, 0, 0}; + selector.color = {0,0,0}; + selector.itemColor = {0,0,0}; + selector.a = 255; // Elementos del menu for (int i = 0; i < MENU_MAX_ITEMS; i++) { - mItem[i].label = ""; - mItem[i].rect = {0, 0, 0, 0}; - mItem[i].hPaddingDown = 0; - mItem[i].selectable = false; - mItem[i].greyed = false; - mItem[i].linkedDown = false; - mItem[i].linkedUp = false; + item[i].label = ""; + item[i].rect = {0, 0, 0, 0}; + item[i].hPaddingDown = 0; + item[i].selectable = false; + item[i].greyed = false; + item[i].linkedDown = false; + item[i].linkedUp = false; } } @@ -84,9 +79,9 @@ bool Menu::loadMedia() bool success = true; // Sonidos - mSoundMove = JA_LoadSound(mFileList[17].c_str()); - mSoundAccept = JA_LoadSound(mFileList[18].c_str()); - mSoundCancel = JA_LoadSound(mFileList[16].c_str()); + soundMove = JA_LoadSound(asset->get("").c_str()); + soundAccept = JA_LoadSound(asset->get("").c_str()); + soundCancel = JA_LoadSound(asset->get("").c_str()); return success; } @@ -94,102 +89,102 @@ bool Menu::loadMedia() // Obtiene el nombre del menu std::string Menu::getName() { - return mName; + return name; } // Obtiene el valor de la variable -Uint8 Menu::getItemSelected() +int Menu::getItemSelected() { // Al llamar a esta funcion, se obtiene el valor y se borra - const int temp = mItemSelected; - mItemSelected = MENU_NO_OPTION; + const int temp = itemSelected; + itemSelected = MENU_NO_OPTION; return temp; } // Actualiza la posicion y el estado del selector void Menu::updateSelector() { - if (mSelector.moving) + if (selector.moving) { // Calcula el desplazamiento en Y - mSelector.y += mSelector.despY; - if (mSelector.despY > 0) // Va hacia abajo + selector.y += selector.despY; + if (selector.despY > 0) // Va hacia abajo { - if (mSelector.y > mSelector.targetY) // Ha llegado al destino + if (selector.y > selector.targetY) // Ha llegado al destino { - mSelector.originY = mSelector.y = mSelector.targetY; - mSelector.moving = false; + selector.originY = selector.y = selector.targetY; + selector.moving = false; } } - if (mSelector.despY < 0) // Va hacia arriba + if (selector.despY < 0) // Va hacia arriba { - if (mSelector.y < mSelector.targetY) // Ha llegado al destino + if (selector.y < selector.targetY) // Ha llegado al destino { - mSelector.originY = mSelector.y = mSelector.targetY; - mSelector.moving = false; + selector.originY = selector.y = selector.targetY; + selector.moving = false; } } - mSelector.rect.y = int(mSelector.y); + selector.rect.y = int(selector.y); } else { - mSelector.rect.y = int(mSelector.y); + selector.rect.y = int(selector.y); } - if (mSelector.resizing) + if (selector.resizing) { // Calcula el incremento en H - mSelector.h += mSelector.incH; - if (mSelector.incH > 0) // Crece + selector.h += selector.incH; + if (selector.incH > 0) // Crece { - if (mSelector.h > mSelector.targetH) // Ha llegado al destino + if (selector.h > selector.targetH) // Ha llegado al destino { - //mSelector.originH = mSelector.targetH = mSelector.rect.h = getSelectorHeight(mSelector.index); - mSelector.originH = mSelector.h = mSelector.targetH; - mSelector.resizing = false; + //selector.originH = selector.targetH = selector.rect.h = getSelectorHeight(selector.index); + selector.originH = selector.h = selector.targetH; + selector.resizing = false; } } - if (mSelector.incH < 0) // Decrece + if (selector.incH < 0) // Decrece { - if (mSelector.h < mSelector.targetH) // Ha llegado al destino + if (selector.h < selector.targetH) // Ha llegado al destino { - //mSelector.originH = mSelector.targetH = mSelector.rect.h = getSelectorHeight(mSelector.index); - mSelector.originH = mSelector.h = mSelector.targetH; - mSelector.resizing = false; + //selector.originH = selector.targetH = selector.rect.h = getSelectorHeight(selector.index); + selector.originH = selector.h = selector.targetH; + selector.resizing = false; } } - mSelector.rect.h = int(mSelector.h); + selector.rect.h = int(selector.h); } else { - mSelector.rect.h = getSelectorHeight(mSelector.index); + selector.rect.h = getSelectorHeight(selector.index); } } // Coloca el selector en una posición específica -void Menu::setSelectorPos(Uint8 index) +void Menu::setSelectorPos(int index) { - if (index < mTotalItems) + if (index < totalItems) { - mSelector.index = index; - mSelector.rect.y = mSelector.y = mSelector.originY = mSelector.targetY = mItem[mSelector.index].rect.y; - mSelector.rect.w = mRectBG.rect.w; - mSelector.rect.x = mRectBG.rect.x; - mSelector.originH = mSelector.targetH = mSelector.rect.h = getSelectorHeight(mSelector.index); - mSelector.moving = false; - mSelector.resizing = false; + selector.index = index; + selector.rect.y = selector.y = selector.originY = selector.targetY = item[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); + selector.moving = false; + selector.resizing = false; } } // Obtiene la anchura del elemento más ancho del menu -Uint16 Menu::getWidestItem() +int Menu::getWidestItem() { - Uint16 result = 0; + int result = 0; // Obtenemos la anchura del item mas ancho - for (int i = 0; i < mTotalItems; i++) - if (mItem[i].rect.w > result) - result = mItem[i].rect.w; + for (int i = 0; i < totalItems; i++) + if (item[i].rect.w > result) + result = item[i].rect.w; return result; } @@ -197,23 +192,23 @@ Uint16 Menu::getWidestItem() // Deja el menu apuntando al primer elemento void Menu::reset() { - mItemSelected = MENU_NO_OPTION; - mSelector.index = 0; - mSelector.originY = mSelector.targetY = mSelector.y = mItem[0].rect.y; - mSelector.originH = mSelector.targetH = mItem[0].rect.h; - mSelector.moving = false; - mSelector.resizing = false; + 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.moving = false; + selector.resizing = false; } // Actualiza el menu para recolocarlo correctamente y establecer el tamaño void Menu::reorganize() { setRectSize(); - if (mIsCenteredOnX) - centerMenuOnX(mCenterX); - if (mIsCenteredOnY) - centerMenuOnY(mCenterY); - if (mAreElementsCenteredOnX) + if (isCenteredOnX) + centerMenuOnX(centerX); + if (isCenteredOnY) + centerMenuOnY(centerY); + if (areElementsCenteredOnX) centerMenuElementsOnX(); } @@ -223,28 +218,28 @@ bool Menu::increaseSelectorIndex() bool success = false; // Obten las coordenadas del elemento actual - mSelector.y = mSelector.originY = mItem[mSelector.index].rect.y; - mSelector.h = mSelector.originH = getSelectorHeight(mSelector.index); + selector.y = selector.originY = item[selector.index].rect.y; + selector.h = selector.originH = getSelectorHeight(selector.index); // Calcula cual es el siguiente elemento - ++mSelector.index %= mTotalItems; - while (!mItem[mSelector.index].selectable) - //mSelector.index++; - ++mSelector.index %= mTotalItems; + ++selector.index %= totalItems; + while (!item[selector.index].selectable) + //selector.index++; + ++selector.index %= totalItems; success = true; // Establece las coordenadas y altura de destino if (success) { - mSelector.targetY = mItem[mSelector.index].rect.y; - mSelector.despY = (mSelector.targetY - mSelector.originY) / mSelector.numJumps; + selector.targetY = item[selector.index].rect.y; + selector.despY = (selector.targetY - selector.originY) / selector.numJumps; - mSelector.targetH = getSelectorHeight(mSelector.index); - mSelector.incH = (mSelector.targetH - mSelector.originH) / mSelector.numJumps; + selector.targetH = getSelectorHeight(selector.index); + selector.incH = (selector.targetH - selector.originH) / selector.numJumps; - mSelector.moving = true; - if (mSelector.incH != 0) - mSelector.resizing = true; + selector.moving = true; + if (selector.incH != 0) + selector.resizing = true; } return success; @@ -256,35 +251,35 @@ bool Menu::decreaseSelectorIndex() bool success = false; // Obten las coordenadas del elemento actual - mSelector.y = mSelector.originY = mItem[mSelector.index].rect.y; - mSelector.h = mSelector.originH = getSelectorHeight(mSelector.index); + selector.y = selector.originY = item[selector.index].rect.y; + selector.h = selector.originH = getSelectorHeight(selector.index); // Calcula cual es el siguiente elemento - if (mSelector.index == 0) - mSelector.index = mTotalItems; + if (selector.index == 0) + selector.index = totalItems; else - mSelector.index--; - while (!mItem[mSelector.index].selectable) + selector.index--; + while (!item[selector.index].selectable) { - if (mSelector.index == 0) - mSelector.index = mTotalItems; + if (selector.index == 0) + selector.index = totalItems; else - mSelector.index--; + selector.index--; } success = true; // Establece las coordenadas y altura de destino if (success) { - mSelector.targetY = mItem[mSelector.index].rect.y; - mSelector.despY = (mSelector.targetY - mSelector.originY) / mSelector.numJumps; + selector.targetY = item[selector.index].rect.y; + selector.despY = (selector.targetY - selector.originY) / selector.numJumps; - mSelector.targetH = getSelectorHeight(mSelector.index); - mSelector.incH = (mSelector.targetH - mSelector.originH) / mSelector.numJumps; + selector.targetH = getSelectorHeight(selector.index); + selector.incH = (selector.targetH - selector.originH) / selector.numJumps; - mSelector.moving = true; - if (mSelector.incH != 0) - mSelector.resizing = true; + selector.moving = true; + if (selector.incH != 0) + selector.resizing = true; } return success; @@ -300,52 +295,52 @@ void Menu::update() void Menu::render() { // Rendereritza el fondo del menu - if (mBackgroundType == MENU_BACKGROUND_SOLID) + if (backgroundType == MENU_BACKGROUND_SOLID) { - SDL_SetRenderDrawColor(mRenderer, mRectBG.r, mRectBG.g, mRectBG.b, mRectBG.a); - SDL_RenderFillRect(mRenderer, &mRectBG.rect); + SDL_SetRenderDrawColor(renderer, rectBG.color.r, rectBG.color.g, rectBG.color.b, rectBG.a); + SDL_RenderFillRect(renderer, &rectBG.rect); } // Renderiza el rectangulo del selector - SDL_Rect temp = mSelector.rect; + SDL_Rect temp = selector.rect; temp.y--; temp.h++; - SDL_SetRenderDrawColor(mRenderer, mSelector.r, mSelector.g, mSelector.b, mSelector.a); - SDL_RenderFillRect(mRenderer, &temp); + SDL_SetRenderDrawColor(renderer, selector.color.r, selector.color.g, selector.color.b, selector.a); + SDL_RenderFillRect(renderer, &temp); // Renderiza el borde del fondo - if (mBackgroundType == MENU_BACKGROUND_SOLID) + if (backgroundType == MENU_BACKGROUND_SOLID) { - SDL_SetRenderDrawColor(mRenderer, mRectBG.r, mRectBG.g, mRectBG.b, 255); - SDL_RenderDrawRect(mRenderer, &mRectBG.rect); + SDL_SetRenderDrawColor(renderer, rectBG.color.r, rectBG.color.g, rectBG.color.b, 255); + SDL_RenderDrawRect(renderer, &rectBG.rect); } // Renderitza el texto - for (int i = 0; i < mTotalItems; i++) + for (int i = 0; i < totalItems; i++) { - if (i == mSelector.index) + if (i == selector.index) { - const color_t color = {mSelector.itemR, mSelector.itemG, mSelector.itemB}; - mText->writeColored(mItem[i].rect.x, mItem[i].rect.y, mItem[i].label, color); + 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); } - else if (mItem[i].selectable) + else if (item[i].selectable) { - mText->write(mItem[i].rect.x, mItem[i].rect.y, mItem[i].label); + text->write(item[i].rect.x, item[i].rect.y, item[i].label); } - else if (mItem[i].greyed) + else if (item[i].greyed) { - mText->writeColored(mItem[i].rect.x, mItem[i].rect.y, mItem[i].label, mColorGreyed); + text->writeColored(item[i].rect.x, item[i].rect.y, item[i].label, colorGreyed); } else // No seleccionable { - if ((mItem[i].linkedUp) && (i == mSelector.index + 1)) + if ((item[i].linkedUp) && (i == selector.index + 1)) { - const color_t color = {mSelector.itemR, mSelector.itemG, mSelector.itemB}; - mText->writeColored(mItem[i].rect.x, mItem[i].rect.y, mItem[i].label, color); + 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); } else // No enlazado con el de arriba { - mText->write(mItem[i].rect.x, mItem[i].rect.y, mItem[i].label); + text->write(item[i].rect.x, item[i].rect.y, item[i].label); } } } @@ -354,65 +349,59 @@ void Menu::render() // Establece el rectangulo de fondo del menu y el selector void Menu::setRectSize() { - mRectBG.rect.w = findWidth() + mText->getCharacterWidth(); - mRectBG.rect.h = findHeight() + mText->getCharacterWidth(); + rectBG.rect.w = findWidth() + text->getCharacterWidth(); + rectBG.rect.h = findHeight() + text->getCharacterWidth(); // La posición X es la del menú menos medio caracter - mRectBG.rect.x = mPosX - (mText->getCharacterWidth() / 2); + rectBG.rect.x = x - (text->getCharacterWidth() / 2); // La posición Y es la del menu menos la altura de medio caracter - mRectBG.rect.y = mPosY - (mText->getCharacterWidth() / 2); + rectBG.rect.y = y - (text->getCharacterWidth() / 2); // Establecemos los valores del rectangulo del selector a partir de los valores del rectangulo de fondo - setSelectorPos(mSelector.index); + setSelectorPos(selector.index); } // Establece el valor de la variable void Menu::setTotalItems(int num) { - mTotalItems = num; - if (mTotalItems > MENU_MAX_ITEMS) - mTotalItems = MENU_MAX_ITEMS; + totalItems = num; + if (totalItems > MENU_MAX_ITEMS) + totalItems = MENU_MAX_ITEMS; } // Establece el color del rectangulo de fondo -void Menu::setBackgroundColor(int r, int g, int b, int alpha) +void Menu::setBackgroundColor(color_t color, int alpha) { - mRectBG.r = r; - mRectBG.g = g; - mRectBG.b = b; - mRectBG.a = alpha; + rectBG.color = color; + rectBG.a = alpha; } // Establece el color del rectangulo del selector -void Menu::setSelectorColor(int r, int g, int b, int alpha) +void Menu::setSelectorColor(color_t color, int alpha) { - mSelector.r = r; - mSelector.g = g; - mSelector.b = b; - mSelector.a = alpha; + selector.color = color; + selector.a = alpha; } // Establece el color del texto del selector -void Menu::setSelectorTextColor(int r, int g, int b) +void Menu::setSelectorTextColor(color_t color) { - mSelector.itemR = r; - mSelector.itemG = g; - mSelector.itemB = b; + selector.itemColor = color; } // Centra el menu respecto un punto en el eje X void Menu::centerMenuOnX(int value) { - mIsCenteredOnX = true; - mCenterX = value; + isCenteredOnX = true; + centerX = value; // Establece la nueva posición centrada en funcion del elemento más ancho - mPosX = (value) - (findWidth() / 2); + x = (value) - (findWidth() / 2); // Reposiciona los elementos del menu for (int i = 0; i < MENU_MAX_ITEMS; i++) - mItem[i].rect.x = mPosX; + item[i].rect.x = x; // Recalcula el rectangulo de fondo setRectSize(); @@ -421,11 +410,11 @@ void Menu::centerMenuOnX(int value) // Centra el menu respecto un punto en el eje Y void Menu::centerMenuOnY(int value) { - mIsCenteredOnY = true; - mCenterY = value; + isCenteredOnY = true; + centerY = value; // Establece la nueva posición centrada en funcion del elemento más ancho - mPosY = (value) - (findHeight() / 2); + y = (value) - (findHeight() / 2); // Reposiciona los elementos del menu replaceElementsOnY(); @@ -437,126 +426,126 @@ void Menu::centerMenuOnY(int value) // Centra los elementos del menu en el eje X void Menu::centerMenuElementsOnX() { - mAreElementsCenteredOnX = true; + areElementsCenteredOnX = true; - for (int i = 0; i < mTotalItems; i++) - mItem[i].rect.x = (mCenterX - (mItem[i].rect.w / 2)); + for (int i = 0; i < totalItems; i++) + item[i].rect.x = (centerX - (item[i].rect.w / 2)); } // Añade un item al menu -void Menu::addItem(std::string text, Uint8 hPaddingDown, bool selectable, bool greyed, bool linkedDown) +void Menu::addItem(std::string text, int hPaddingDown, bool selectable, bool greyed, bool linkedDown) { // Si es el primer item coge la posición en el eje Y del propio menu - if (mTotalItems == 0) - mItem[mTotalItems].rect.y = mPosY; + if (totalItems == 0) + item[totalItems].rect.y = y; else // En caso contrario, coge la posición en el eje Y a partir del elemento anterior - mItem[mTotalItems].rect.y = mItem[mTotalItems - 1].rect.y + mItem[mTotalItems - 1].rect.h + mItem[mTotalItems - 1].hPaddingDown; + item[totalItems].rect.y = item[totalItems - 1].rect.y + item[totalItems - 1].rect.h + item[totalItems - 1].hPaddingDown; - setItemCaption(mTotalItems, text); - mItem[mTotalItems].rect.x = mPosX; - mItem[mTotalItems].hPaddingDown = hPaddingDown; - mItem[mTotalItems].selectable = selectable; - mItem[mTotalItems].greyed = greyed; - mItem[mTotalItems].linkedDown = linkedDown; - if (mTotalItems > 0) - if (mItem[mTotalItems - 1].linkedDown) - mItem[mTotalItems].linkedUp = true; + setItemCaption(totalItems, text); + item[totalItems].rect.x = x; + item[totalItems].hPaddingDown = hPaddingDown; + item[totalItems].selectable = selectable; + item[totalItems].greyed = greyed; + item[totalItems].linkedDown = linkedDown; + if (totalItems > 0) + if (item[totalItems - 1].linkedDown) + item[totalItems].linkedUp = true; - setTotalItems(mTotalItems + 1); - mCenterX = mPosX + (findWidth() / 2); + setTotalItems(totalItems + 1); + centerX = x + (findWidth() / 2); reorganize(); } // Cambia el texto de un item -void Menu::setItemCaption(Uint8 index, std::string text) +void Menu::setItemCaption(int index, std::string text) { - mItem[index].label = text; - mItem[index].rect.w = mText->lenght(mItem[index].label); - mItem[index].rect.h = mText->getCharacterWidth(); + item[index].label = text; + item[index].rect.w = this->text->lenght(item[index].label); + item[index].rect.h = this->text->getCharacterWidth(); reorganize(); } // Establece el indice del itemm que se usará por defecto al cancelar el menu -void Menu::setDefaultActionWhenCancel(Uint8 item) +void Menu::setDefaultActionWhenCancel(int item) { - mDefaultActionWhenCancel = item; + defaultActionWhenCancel = item; } // Gestiona la entrada de teclado y mando durante el menu void Menu::checkInput() { - if (mInput->checkInput(INPUT_UP, REPEAT_FALSE)) + if (input->checkInput(INPUT_UP, REPEAT_FALSE)) if (decreaseSelectorIndex()) - JA_PlaySound(mSoundMove); + JA_PlaySound(soundMove); - if (mInput->checkInput(INPUT_DOWN, REPEAT_FALSE)) + if (input->checkInput(INPUT_DOWN, REPEAT_FALSE)) if (increaseSelectorIndex()) - JA_PlaySound(mSoundMove); + JA_PlaySound(soundMove); - if (mInput->checkInput(INPUT_ACCEPT, REPEAT_FALSE)) + if (input->checkInput(INPUT_ACCEPT, REPEAT_FALSE)) { - mItemSelected = mSelector.index; - JA_PlaySound(mSoundAccept); + itemSelected = selector.index; + JA_PlaySound(soundAccept); } - if (mInput->checkInput(INPUT_CANCEL, REPEAT_FALSE)) + if (input->checkInput(INPUT_CANCEL, REPEAT_FALSE)) { - mItemSelected = mDefaultActionWhenCancel; - JA_PlaySound(mSoundCancel); + itemSelected = defaultActionWhenCancel; + JA_PlaySound(soundCancel); } } // Calcula el ancho del menu -Uint16 Menu::findWidth() +int Menu::findWidth() { return getWidestItem(); } // Calcula el alto del menu -Uint16 Menu::findHeight() +int Menu::findHeight() { - Uint16 height = 0; + int height = 0; // Obtenemos la altura de la suma de alturas de los items - for (int i = 0; i < mTotalItems; i++) - height += mItem[i].rect.h + mItem[i].hPaddingDown; + for (int i = 0; i < totalItems; i++) + height += item[i].rect.h + item[i].hPaddingDown; - return height - mItem[mTotalItems - 1].hPaddingDown; + return height - item[totalItems - 1].hPaddingDown; } // Recoloca los elementos del menu en el eje Y void Menu::replaceElementsOnY() { - mItem[0].rect.y = mPosY; + item[0].rect.y = y; - for (int i = 1; i < mTotalItems; i++) - mItem[i].rect.y = mItem[i - 1].rect.y + mItem[i - 1].rect.h + mItem[i - 1].hPaddingDown; + for (int i = 1; i < totalItems; i++) + item[i].rect.y = item[i - 1].rect.y + item[i - 1].rect.h + item[i - 1].hPaddingDown; } // Establece el estado seleccionable de un item -void Menu::setSelectable(Uint8 index, bool value) +void Menu::setSelectable(int index, bool value) { - mItem[index].selectable = value; + item[index].selectable = value; } // Establece el estado agrisado de un item -void Menu::setGreyed(Uint8 index, bool value) +void Menu::setGreyed(int index, bool value) { - mItem[index].greyed = value; + item[index].greyed = value; } // Establece el estado de enlace de un item -void Menu::setLinkedDown(Uint8 index, bool value) +void Menu::setLinkedDown(int index, bool value) { - mItem[index].linkedDown = value; + item[index].linkedDown = value; } // Calcula la altura del selector int Menu::getSelectorHeight(int value) { - if (mItem[value].linkedDown) - return mItem[value].rect.h + mItem[value].hPaddingDown + mItem[value + 1].rect.h; + if (item[value].linkedDown) + return item[value].rect.h + item[value].hPaddingDown + item[value + 1].rect.h; else - return mItem[value].rect.h; + return item[value].rect.h; } \ No newline at end of file diff --git a/source/menu.h b/source/menu.h index 6f9e229..8b4fea1 100644 --- a/source/menu.h +++ b/source/menu.h @@ -4,6 +4,8 @@ #include "sprite.h" #include "text.h" #include "input.h" +#include "asset.h" +#include "utils.h" #include "jail_audio.h" #ifndef MENU_H @@ -23,76 +25,70 @@ class Menu { private: - std::string mName; // Nombre del menu - int mPosX; // Posición en el eje X de la primera letra del primer elemento - int mPosY; // Posición en el eje Y de la primera letra del primer elemento - Uint16 mHeight; // Altura del menu - Uint16 mWidth; // Anchura del menu - Uint8 mTotalItems; // Numero total de items del menu - int mItemSelected; // Índice del item del menu que ha sido seleccionado - Uint8 mDefaultActionWhenCancel; // Indice del item del menu que se selecciona cuando se cancela el menu - Uint8 mBackgroundType; // Tipo de fondo para el menu - bool mIsCenteredOnX; // Variable para saber si el menu debe estar centrado respecto a un punto en el eje X - bool mIsCenteredOnY; // Variable para saber si el menu debe estar centrado respecto a un punto en el eje Y - int mCenterX; // Centro del menu en el eje X - int mCenterY; // Centro del menu en el eje Y - bool mAreElementsCenteredOnX; // Variable para saber si los elementos van centrados en el eje X - Uint16 mWidestItem; // Anchura del elemento más ancho - JA_Sound mSoundAccept; // Sonido al aceptar o elegir una opción del menu - JA_Sound mSoundCancel; // Sonido al cancelar el menu - JA_Sound mSoundMove; // Sonido al mover el selector - SDL_Renderer *mRenderer; // Puntero al renderizador de la ventana - std::string *mFileList; // Lista de ficheros - Text *mText; // Texto para poder escribir los items del menu - Input *mInput; // Gestor de eventos de entrada de teclado o gamepad - color_t mColorGreyed; // Color para los elementos agrisados + std::string name; // Nombre del menu + int x; // Posición en el eje X de la primera letra del primer elemento + int y; // Posición en el eje Y de la primera letra del primer elemento + int h; // Altura del menu + int w; // Anchura del menu + int totalItems; // Numero total de items del menu + int itemSelected; // Índice del item del menu que ha sido seleccionado + int defaultActionWhenCancel; // Indice del item del menu que se selecciona cuando se cancela el menu + int backgroundType; // Tipo de fondo para el menu + int centerX; // Centro del menu en el eje X + int centerY; // Centro del menu en el eje Y + bool isCenteredOnX; // Variable para saber si el menu debe estar centrado respecto a un punto en el eje X + bool isCenteredOnY; // Variable para saber si el menu debe estar centrado respecto a un punto en el eje Y + bool areElementsCenteredOnX; // Variable para saber si los elementos van centrados en el eje X + int widestItem; // Anchura del elemento más ancho + JA_Sound soundAccept; // Sonido al aceptar o elegir una opción del menu + JA_Sound soundCancel; // Sonido al cancelar el menu + JA_Sound soundMove; // Sonido al mover el selector + SDL_Renderer *renderer; // Puntero al renderizador de la ventana + Asset *asset; // Objeto encargado de gestionar los ficheros de recursos + Text *text; // Texto para poder escribir los items del menu + Input *input; // Gestor de eventos de entrada de teclado o gamepad + color_t colorGreyed; // Color para los elementos agrisados - struct rectangle + struct rectangle_t { SDL_Rect rect; // Rectangulo - Uint8 r; // Rojo - Uint8 g; // Verde - Uint8 b; // Azul - Uint8 a; // Transparencia + color_t color; // Color + int a; // Transparencia }; - rectangle mRectBG; // Rectangulo de fondo del menu + rectangle_t rectBG; // Rectangulo de fondo del menu - struct item + struct item_t { - std::string label; // Texto - SDL_Rect rect; // Rectangulo que delimita el elemento - Uint8 hPaddingDown; // Espaciado bajo el elemento - bool selectable; // Indica si se puede seleccionar - bool greyed; // Indica si ha de aparecer con otro color mas oscuro - bool linkedDown; // Indica si el elemento actual y el siguiente se tratan como uno solo. Afecta al selector - bool linkedUp; // Indica si el elemento actual y el anterior se tratan como uno solo. Afecta al selector + std::string label; // Texto + SDL_Rect rect; // Rectangulo que delimita el elemento + int hPaddingDown; // Espaciado bajo el elemento + bool selectable; // Indica si se puede seleccionar + bool greyed; // Indica si ha de aparecer con otro color mas oscuro + bool linkedDown; // Indica si el elemento actual y el siguiente se tratan como uno solo. Afecta al selector + bool linkedUp; // Indica si el elemento actual y el anterior se tratan como uno solo. Afecta al selector }; - item mItem[MENU_MAX_ITEMS]; // Estructura para cada elemento del menu + item_t item[MENU_MAX_ITEMS]; // Estructura para cada elemento del menu - struct selector + struct selector_t { - float originY; // Coordenada de origen - float targetY; // Coordenada de destino - float despY; // Cantidad de pixeles que se desplaza el selector en cada salto: (target - origin) / numJumps - bool moving; // Indica si el selector está avanzando hacia el destino - float originH; // Altura de origen - float targetH; // Altura de destino - float incH; // Cantidad de pixels que debe incrementar o decrementar el selector en cada salto - bool resizing; // Indica si el selector está cambiando de tamaño - float y; // Coordenada actual, usado para el desplazamiento - float h; // Altura actual, usado para el cambio de tamaño - Uint8 numJumps; // Numero de pasos preestablecido para llegar al destino - Uint8 index; // Elemento del menu que tiene el foco - SDL_Rect rect; // Rectangulo del selector - Uint8 r; // Cantidad de color rojo para el rectangulo del selector - Uint8 g; // Cantidad de color verde para el rectangulo del selector - Uint8 b; // Cantidad de color azul para el rectangulo del selector - Uint8 a; // Cantidad de transparencia para el rectangulo del selector - Uint8 itemR; // Cantidad de color rojo para el texto del elemento seleccionado - Uint8 itemG; // Cantidad de color verde para el texto del elemento seleccionado - Uint8 itemB; // Cantidad de color azul para el texto del elemento seleccionado + float originY; // Coordenada de origen + float targetY; // Coordenada de destino + float despY; // Cantidad de pixeles que se desplaza el selector en cada salto: (target - origin) / numJumps + bool moving; // Indica si el selector está avanzando hacia el destino + float originH; // Altura de origen + float targetH; // Altura de destino + float incH; // Cantidad de pixels que debe incrementar o decrementar el selector en cada salto + bool resizing; // Indica si el selector está cambiando de tamaño + float y; // Coordenada actual, usado para el desplazamiento + float h; // Altura actual, usado para el cambio de tamaño + int numJumps; // Numero de pasos preestablecido para llegar al destino + int index; // Elemento del menu que tiene el foco + SDL_Rect rect; // Rectangulo del selector + color_t color; // Color del selector + color_t itemColor; // Color del item + int a; // Cantidad de transparencia para el rectangulo del selector }; - selector mSelector; // Variables para pintar el selector del menu + selector_t selector; // Variables para pintar el selector del menu // Carga los recursos necesarios para la sección 'Title' bool loadMedia(); @@ -116,16 +112,16 @@ private: void updateSelector(); // Obtiene la anchura del elemento más ancho del menu - Uint16 getWidestItem(); + int getWidestItem(); // Gestiona la entrada de teclado y mando durante el menu void checkMenuInput(Menu *menu); // Calcula el ancho del menu - Uint16 findWidth(); + int findWidth(); // Calcula el alto del menu - Uint16 findHeight(); + int findHeight(); // Recoloca los elementos del menu en el eje Y void replaceElementsOnY(); @@ -135,7 +131,7 @@ private: public: // Constructor - Menu(SDL_Renderer *renderer, Text *text, Input *input, std::string *fileList); + Menu(SDL_Renderer *renderer, Text *text, Input *input, Asset *asset); // Destructor ~Menu(); @@ -147,7 +143,7 @@ public: std::string getName(); // Obtiene el valor de la variable - Uint8 getItemSelected(); + int getItemSelected(); // Deja el menu apuntando al primer elemento void reset(); @@ -162,13 +158,13 @@ public: void render(); // Establece el color del rectangulo de fondo - void setBackgroundColor(int r, int g, int b, int alpha); + void setBackgroundColor(color_t color, int alpha); // Establece el color del rectangulo del selector - void setSelectorColor(int r, int g, int b, int alpha); + void setSelectorColor(color_t color, int alpha); // Establece el color del texto del selector - void setSelectorTextColor(int r, int g, int b); + void setSelectorTextColor(color_t color); // Centra el menu respecto a un punto en el eje X void centerMenuOnX(int value); @@ -180,25 +176,25 @@ public: void centerMenuElementsOnX(); // Añade un item al menu - void addItem(std::string text, Uint8 hPaddingDown = 1, bool selectable = true, bool greyed = false, bool linkedDown = false); + void addItem(std::string text, int hPaddingDown = 1, bool selectable = true, bool greyed = false, bool linkedDown = false); // Cambia el texto de un item - void setItemCaption(Uint8 index, std::string text); + void setItemCaption(int index, std::string text); // Establece el indice del item que se usará por defecto al cancelar el menu - void setDefaultActionWhenCancel(Uint8 item); + void setDefaultActionWhenCancel(int item); // Coloca el selector en una posición específica - void setSelectorPos(Uint8 index); + void setSelectorPos(int index); // Establece el estado seleccionable de un item - void setSelectable(Uint8 index, bool value); + void setSelectable(int index, bool value); // Establece el estado agrisado de un item - void setGreyed(Uint8 index, bool value); + void setGreyed(int index, bool value); // Establece el estado de enlace de un item - void setLinkedDown(Uint8 index, bool value); + void setLinkedDown(int index, bool value); }; #endif