From 8a4d2a541da7c080514063b313b4cf0c05f54b17 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sun, 25 Sep 2022 13:00:39 +0200 Subject: [PATCH] Animatedsprite ya no permite indices fuera de rango --- source/animatedsprite.cpp | 27 ++++++++++++++++++--------- source/credits.cpp | 4 ++-- source/game.cpp | 39 ++++++++++++++++++--------------------- source/input.h | 4 ++-- source/intro.cpp | 4 ++-- source/intro.h | 4 ++-- source/item.cpp | 2 +- source/item.h | 4 ++-- source/item_tracker.cpp | 4 ++-- source/logo.cpp | 2 +- source/logo.h | 4 ++-- source/player.cpp | 4 ++-- source/room.cpp | 24 ++++++++++++------------ source/scoreboard.cpp | 2 +- source/screen.cpp | 6 +++--- source/text.cpp | 8 +++++--- source/title.cpp | 4 ++-- todo.txt | 4 ++-- 18 files changed, 79 insertions(+), 71 deletions(-) diff --git a/source/animatedsprite.cpp b/source/animatedsprite.cpp index e3e3e66..47407b6 100644 --- a/source/animatedsprite.cpp +++ b/source/animatedsprite.cpp @@ -32,7 +32,7 @@ int AnimatedSprite::getIndex(std::string name) for (auto a : animation) { - ++index; + index++; if (a.name == name) { return index; @@ -77,7 +77,7 @@ void AnimatedSprite::animate() setSpriteClip(animation[currentAnimation].frames[animation[currentAnimation].currentFrame]); // Incrementa el contador de la animacion - ++animation[currentAnimation].counter; + animation[currentAnimation].counter++; } } @@ -139,6 +139,7 @@ bool AnimatedSprite::load(std::string filePath) int framesPerRow = 0; int frameWidth = 0; int frameHeight = 0; + int maxTiles = 0; // Indicador de éxito en la carga bool success = true; @@ -192,7 +193,8 @@ bool AnimatedSprite::load(std::string filePath) SDL_Rect rect = {0, 0, frameWidth, frameHeight}; while (getline(ss, tmp, ',')) { - int numTile = std::stoi(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); @@ -227,12 +229,6 @@ bool AnimatedSprite::load(std::string filePath) else if (line.substr(0, pos) == "frameWidth") { frameWidth = std::stoi(line.substr(pos + 1, line.length())); - - // Normaliza valores - if (framesPerRow == 0) - { - framesPerRow = texture->getWidth() / frameWidth; - } } else if (line.substr(0, pos) == "frameHeight") @@ -245,6 +241,19 @@ bool AnimatedSprite::load(std::string filePath) printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str()); 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; + } } } } diff --git a/source/credits.cpp b/source/credits.cpp index 304af80..e1b1321 100644 --- a/source/credits.cpp +++ b/source/credits.cpp @@ -115,7 +115,7 @@ void Credits::fillTexture() for (auto t:texts) { text->write(0, i * size, t); - ++i; + i++; } SDL_SetRenderTarget(renderer, nullptr); @@ -134,7 +134,7 @@ void Credits::update() checkEventHandler(); // Incrementa el contador - ++counter; + counter++; // Comprueba si ha terminado la sección if (counter > 1000) diff --git a/source/game.cpp b/source/game.cpp index 1752a0a..4dd8aec 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -18,17 +18,17 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, D // **** // this->debug->setEnabled(true); - //currentRoom = "11.room"; - //spawnPoint = {2 * 8, 5 * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL}; + // currentRoom = "11.room"; + // spawnPoint = {2 * 8, 5 * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL}; - //currentRoom = "06.room"; - //spawnPoint = {14 * 8, 9 * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL}; + // currentRoom = "06.room"; + // spawnPoint = {14 * 8, 9 * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL}; - //currentRoom = "60.room"; - //const int x = 16; - //const int y = 13; - //spawnPoint = {x * 8, y * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL}; - // **** + // currentRoom = "60.room"; + // const int x = 16; + // const int y = 13; + // spawnPoint = {x * 8, y * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL}; + // **** // Crea los objetos scoreboard = new ScoreBoard(renderer, asset, &board); @@ -320,7 +320,7 @@ bool Game::changeRoom(std::string file) board.color = (c.r + c.g + c.b == 0) ? stringToColor("white") : c; // Si el color es negrom cambialo a blanco if (roomTracker->addRoom(file)) { // Incrementa el contador de habitaciones visitadas - ++board.rooms; + board.rooms++; } // Pasa la nueva habitación al jugador @@ -422,7 +422,7 @@ void Game::reLoadTextures() void Game::setBlackScreen() { blackScreen = true; - //screen->setspectrumFade(); + // screen->setspectrumFade(); } // Actualiza las variables relativas a la pantalla en negro @@ -430,18 +430,15 @@ void Game::updateBlackScreen() { if (blackScreen) { - //if (screen->spectrumFadeEnded()) + blackScreenCounter++; + if (blackScreenCounter > 10) { - ++blackScreenCounter; - if (blackScreenCounter > 10) - { - blackScreen = false; - blackScreenCounter = 0; + blackScreen = false; + blackScreenCounter = 0; - player->resume(); - room->resume(); - screen->setBorderColor(room->getBorderColor()); - } + player->resume(); + room->resume(); + screen->setBorderColor(room->getBorderColor()); } } } diff --git a/source/input.h b/source/input.h index cf3a834..d656ddf 100644 --- a/source/input.h +++ b/source/input.h @@ -41,15 +41,15 @@ private: Uint8 scancode; // Scancode asociado bool active; // Indica si está activo }; - 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 gameControllerBindings[17]; // Vector con las teclas asociadas a los inputs predefinidos + keyBindings_t keyBindings[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 connectedControllers; // Vector con todos los mandos conectados std::vector controllerNames; // Vector con los nombres de los mandos int numGamepads; // Numero de mandos conectados diff --git a/source/intro.cpp b/source/intro.cpp index 3216f37..ea42601 100644 --- a/source/intro.cpp +++ b/source/intro.cpp @@ -101,11 +101,11 @@ void Intro::updateCounter() { JA_PlayMusic(loadingSound2); } - ++counter; + counter++; } else { - ++preCounter; + preCounter++; } } diff --git a/source/intro.h b/source/intro.h index b2732e6..0e571c3 100644 --- a/source/intro.h +++ b/source/intro.h @@ -30,8 +30,8 @@ private: int preCounter; // Contador previo para realizar una pausa inicial int counter; // Contador section_t section; // Estado del bucle principal para saber si continua o se sale - Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa - Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa + Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa + Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa int loadCounter; // Contador para controlar las cargas bool load1, load2; JA_Music loadingSound1, loadingSound2, loadingSound3; diff --git a/source/item.cpp b/source/item.cpp index ca17acc..e18999a 100644 --- a/source/item.cpp +++ b/source/item.cpp @@ -58,7 +58,7 @@ void Item::render() // Actualiza las variables del objeto void Item::update() { - ++counter; + counter++; } // Obtiene el rectangulo de colision del objeto diff --git a/source/item.h b/source/item.h index 518a523..a44035a 100644 --- a/source/item.h +++ b/source/item.h @@ -23,8 +23,8 @@ struct item_t int y; // Posicion del item en pantalla int tile; // Numero de tile dentro de la textura int counter; // Contador inicial. Es el que lo hace cambiar de color - color_t color1; - color_t color2; + color_t color1; // Uno de los dos colores que se utiliza para el item + color_t color2; // Uno de los dos colores que se utiliza para el item }; // Clase Item diff --git a/source/item_tracker.cpp b/source/item_tracker.cpp index 91b0fe3..42afddc 100644 --- a/source/item_tracker.cpp +++ b/source/item_tracker.cpp @@ -65,7 +65,7 @@ int ItemTracker::findByName(std::string name) { return i; } - ++i; + i++; } return -1; @@ -82,7 +82,7 @@ int ItemTracker::findByPos(int index, SDL_Point pos) { return i; } - ++i; + i++; } return -1; diff --git a/source/logo.cpp b/source/logo.cpp index 9d79c4e..9aaaeb7 100644 --- a/source/logo.cpp +++ b/source/logo.cpp @@ -227,7 +227,7 @@ void Logo::update() checkEventHandler(); // Incrementa el contador - ++counter; + counter++; // Gestiona el logo de JAILGAME updateJAILGAMES(); diff --git a/source/logo.h b/source/logo.h index 2aa0311..faca962 100644 --- a/source/logo.h +++ b/source/logo.h @@ -27,8 +27,8 @@ private: std::vector color; // Vector con los colores para el fade int counter; // Contador section_t section; // Estado del bucle principal para saber si continua o se sale - Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa - Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa + Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa + Uint32 ticksSpeed; // Velocidad a la que se repiten los bucles del programa int initFade; // Tiempo del contador cuando inicia el fade a negro int endLogo; // Tiempo del contador para terminar el logo int postLogo; // Tiempo que dura el logo con el fade al maximo diff --git a/source/player.cpp b/source/player.cpp index 033f07b..1aee173 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -232,7 +232,7 @@ void Player::checkState() { vx = 0.0f; vy = maxVY; - ++fallCounter; + fallCounter++; playFallSound(); } @@ -249,7 +249,7 @@ void Player::checkState() else if (state == s_jumping) { - ++jumpCounter; + jumpCounter++; playJumpSound(); } } diff --git a/source/room.cpp b/source/room.cpp index 750a687..6697ea6 100644 --- a/source/room.cpp +++ b/source/room.cpp @@ -865,19 +865,19 @@ void Room::setBottomSurfaces() line.x1 = (tile[i] % mapWidth) * tileSize; line.y = ((tile[i] / mapWidth) * tileSize) + tileSize - 1; lastOne = i; - ++i; + i++; while (tile[i] == tile[i - 1] + 1) { lastOne = i; - ++i; + i++; } line.x2 = ((tile[lastOne] % mapWidth) * tileSize) + tileSize - 1; bottomSurfaces.push_back(line); if (tile[i] == -1) { // Si el siguiente elemento es un separador, hay que saltarlo - ++i; + i++; } } } @@ -912,19 +912,19 @@ void Room::setTopSurfaces() line.x1 = (tile[i] % mapWidth) * tileSize; line.y = (tile[i] / mapWidth) * tileSize; lastOne = i; - ++i; + i++; while (tile[i] == tile[i - 1] + 1) { lastOne = i; - ++i; + i++; } line.x2 = ((tile[lastOne] % mapWidth) * tileSize) + tileSize - 1; topSurfaces.push_back(line); if (tile[i] == -1) { // Si el siguiente elemento es un separador, hay que saltarlo - ++i; + i++; } } } @@ -959,11 +959,11 @@ void Room::setLeftSurfaces() line.y1 = ((tile[i] / mapWidth) * tileSize); while (tile[i] + mapWidth == tile[i + 1]) { - ++i; + i++; } line.y2 = ((tile[i] / mapWidth) * tileSize) + tileSize - 1; leftSurfaces.push_back(line); - ++i; + i++; } } @@ -997,11 +997,11 @@ void Room::setRightSurfaces() line.y1 = ((tile[i] / mapWidth) * tileSize); while (tile[i] + mapWidth == tile[i + 1]) { - ++i; + i++; } line.y2 = ((tile[i] / mapWidth) * tileSize) + tileSize - 1; rightSurfaces.push_back(line); - ++i; + i++; } } @@ -1037,7 +1037,7 @@ void Room::setLeftSlopes() lastOneFound = lookingFor; lookingFor += mapWidth + 1; found.erase(found.begin() + i); - --i; + i--; } } line.x2 = ((lastOneFound % mapWidth) * tileSize) + tileSize - 1; @@ -1078,7 +1078,7 @@ void Room::setRightSlopes() lastOneFound = lookingFor; lookingFor += mapWidth - 1; found.erase(found.begin() + i); - --i; + i--; } } line.x2 = (lastOneFound % mapWidth) * tileSize; diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index f70d5b9..29565a9 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -129,7 +129,7 @@ void ScoreBoard::render() // Actualiza las variables del objeto void ScoreBoard::update() { - ++counter; + counter++; sprite->update(); if (!paused) diff --git a/source/screen.cpp b/source/screen.cpp index ee946b5..03592d7 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -115,7 +115,7 @@ void Screen::setVideoMode(int fullScreenMode) int scale = 0; while (((gameCanvasWidth * (scale + 1)) <= screenWidth) && ((gameCanvasHeight * (scale + 1)) <= screenHeight)) { - ++scale; + scale++; } dest.w = gameCanvasWidth * scale; @@ -259,7 +259,7 @@ void Screen::updateFade() return; } - ++fadeCounter; + fadeCounter++; if (fadeCounter > fadeLenght) { iniFade(); @@ -325,7 +325,7 @@ void Screen::updateSpectrumFade() return; } - ++spectrumFadeCounter; + spectrumFadeCounter++; if (spectrumFadeCounter > spectrumFadeLenght) { iniSpectrumFade(); diff --git a/source/text.cpp b/source/text.cpp index d7ed473..7bcd656 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -26,7 +26,7 @@ Text::~Text() void Text::init() { // Inicializa a cero el vector con las coordenadas - for (int i = 0; i < 128; i++) + for (int i = 0; i < 128; ++i) { offset[i].x = 0; offset[i].y = 0; @@ -44,7 +44,7 @@ void Text::init() sprite->setSpriteClip(0, 0, sprite->getWidth(), sprite->getHeight()); // Establece las coordenadas para cada caracter ascii de la cadena y su ancho - for (int i = 32; i < 128; i++) + for (int i = 32; i < 128; ++i) { offset[i].x = ((i - 32) % 15) * boxWidth; offset[i].y = ((i - 32) / 15) * boxHeight; @@ -131,7 +131,7 @@ int Text::lenght(std::string text, int kerning) { int shift = 0; - for (int i = 0; i < (int)text.length(); i++) + for (int i = 0; i < (int)text.length(); ++i) shift += (offset[int(text[i])].w + kerning); // Descuenta el kerning del último caracter @@ -164,7 +164,9 @@ void Text::initOffsetFromFile() { // Almacena solo las lineas impares if (line_read % 2 == 1) + { offset[index++].w = std::stoi(buffer); + } // Limpia el buffer buffer.clear(); diff --git a/source/title.cpp b/source/title.cpp index dd22d41..d13015f 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -22,7 +22,7 @@ Title::Title(SDL_Renderer *renderer, Screen *screen, Asset *asset) ticks = 0; ticksSpeed = 15; longText = "HEY JAILERS!! IT'S 2022 AND WE'RE STILL ROCKING LIKE IT'S 1998!!! HAVE YOU HEARD IT? JAILGAMES ARE BACK!! YEEESSS BACK!! MORE THAN 10 TITLES ON JAILDOC'S KITCHEN!! THATS A LOOOOOOT OF JAILGAMES, BUT WHICH ONE WILL STRIKE FIRST? THERE IS ALSO A NEW DEVICE TO COME P.A.C.O. THAT WILL BLOW YOUR MIND WITH JAILGAMES ON THE GO. BUT WAIT! WHAT'S THAT BEAUTY I'M SEEING RIGHT OVER THERE?? OOOH THAT TINY MINIASCII IS PURE LOVE!! I WANT TO LICK EVERY BYTE OF IT!! OH SHIT! AND DON'T FORGET TO BRING BACK THOSE OLD AND FAT MS-DOS JAILGAMES TO GITHUB TO KEEP THEM ALIVE!! WHAT WILL BE THE NEXT JAILDOC RELEASE? WHAT WILL BE THE NEXT PROJECT TO COME ALIVE?? OH BABY WE DON'T KNOW BUT HERE YOU CAN FIND THE ANSWER, YOU JUST HAVE TO COMPLETE JAILDOCTOR'S DILEMMA ... COULD YOU?"; - for (int i = 0; i < (int)longText.length(); i++) + for (int i = 0; i < (int)longText.length(); ++i) { letter_t l; l.letter = longText.substr(i, 1); @@ -108,7 +108,7 @@ void Title::checkEventHandler() // Actualiza la marquesina void Title::updateMarquee() { - for (int i = 0; i < (int)letters.size(); i++) + for (int i = 0; i < (int)letters.size(); ++i) { if (letters[i].enabled) { diff --git a/todo.txt b/todo.txt index cf972ba..c87b3c3 100644 --- a/todo.txt +++ b/todo.txt @@ -9,7 +9,7 @@ x (A) Decidir un diseño para qué sucede en caso de morir: Recordar el punto po x (A) Crear tiles que maten {cm:2022-08-29} x (A) Modificar el salto para que coincida con el del JSW, no ha de colisionar lateralmente (A) Crear tiles que arrastren, tipo cinta transportadora -(A) Tiles animados +x (A) Tiles animados x (A) Tile que maten (o enemigos?) x (A) Cuando mueres, pantalla negra entre vida y vida x (A) Morir al caer de alto @@ -44,7 +44,7 @@ x (A) La pantalla de titulo no tiene menu, solo un PRESS ENTER TO PLAY x (A) Poner el mapa/jugador en pausa x (A) El color del borde se pierde al morir por la pantalla negra -(B) El fichero ani ha de calcular cuantos frames hay a partir del tamaño y ver que no hay ningun indice incorrecto +x (B) El fichero ani ha de calcular cuantos frames hay a partir del tamaño y ver que no hay ningun indice incorrecto x (B) Así como no necesitar lo de frames per row (B) Le ha de pasar el w y h al sprite (B) Lo enemigos han de coger el ancho y alto del fichero ani (y si no hay?)