From f1bf353cd34bd38558814538324f7dd7a6b754e1 Mon Sep 17 00:00:00 2001 From: Strangerke Date: Tue, 23 Jul 2019 14:45:49 +0200 Subject: HDB: Reduce some more variable scopes --- engines/hdb/ai-lists.cpp | 34 ++++++---------- engines/hdb/map.cpp | 35 +++++++---------- engines/hdb/menu.cpp | 100 +++++++++++++++++++---------------------------- engines/hdb/window.cpp | 17 ++++---- 4 files changed, 75 insertions(+), 111 deletions(-) diff --git a/engines/hdb/ai-lists.cpp b/engines/hdb/ai-lists.cpp index 0c6c17d3d6..b2f3a6949e 100644 --- a/engines/hdb/ai-lists.cpp +++ b/engines/hdb/ai-lists.cpp @@ -61,16 +61,13 @@ void AI::addAnimateTarget(int x, int y, int start, int end, AnimSpeed speed, boo // Set Info if this is not an inMap animation at->inMap = inMap; if (!inMap) { - char name[32]; - uint32 size; - for (int i = start; i <= end; i++) { if (i < 10) snprintf(name, 32, "%s0%d", tileName, i + 1); else snprintf(name, 32, "%s%d", tileName, i + 1); - size = g_hdb->_fileMan->getLength(name, TYPE_TILE32); + uint32 size = g_hdb->_fileMan->getLength(name, TYPE_TILE32); at->gfxList[i] = g_hdb->_gfx->getTileGfx(name, size); } } @@ -84,14 +81,11 @@ void AI::addAnimateTarget(int x, int y, int start, int end, AnimSpeed speed, boo Called every frame */ void AI::animateTargets() { - AnimTarget *at; int mx, my; - int layer; - g_hdb->_map->getMapXY(&mx, &my); for (uint i = 0; i < _animTargets.size(); i++) { - at = _animTargets[i]; + AnimTarget *at = _animTargets[i]; debug(9, "AnimTarget #%i: at: at->x: %d, at->y: %d, at->start: %d, at->end: %d, at->vel: %d", i, at->x, at->y, at->start, at->end, at->vel); // Draw Non-map stuff every frame @@ -105,7 +99,7 @@ void AI::animateTargets() { if (at->inMap) { // Animate Map Tiles - layer = 0; // BG layer + int layer = 0; // BG layer if (!(at->start == g_hdb->_map->getMapBGTileIndex(at->x, at->y))) layer = 1; @@ -162,10 +156,6 @@ void AI::addBridgeExtend(int x, int y, int bridgeType) { } void AI::animateBridges() { - int tileIndex, xv, yv; - uint32 flags; - bool done; - // out quick! if (!_numBridges) return; @@ -175,8 +165,9 @@ void AI::animateBridges() { continue; _bridges[i].delay = 5; - done = false; - xv = yv = 0; + bool done = false; + int xv = 0; + int yv = 0; switch (_bridges[i].dir) { case DIR_UP: @@ -220,17 +211,19 @@ void AI::animateBridges() { } // is this bridge done extending one chunk? - if (done == true) { + if (done) { if (g_hdb->_map->onScreen(_bridges[i].x, _bridges[i].y)) g_hdb->_sound->playSound(SND_BRIDGE_EXTEND); _bridges[i].anim = 0; _bridges[i].x += xv; _bridges[i].y += yv; - tileIndex = g_hdb->_map->getMapFGTileIndex(_bridges[i].x, _bridges[i].y); - flags = g_hdb->_map->getMapBGTileFlags(_bridges[i].x, _bridges[i].y); + int tileIndex = g_hdb->_map->getMapFGTileIndex(_bridges[i].x, _bridges[i].y); + uint32 flags = g_hdb->_map->getMapBGTileFlags(_bridges[i].x, _bridges[i].y); if (!flags || (flags & kFlagMetal) || tileIndex >= 0 || (flags & kFlagSolid)) { if (g_hdb->_map->onScreen(_bridges[i].x, _bridges[i].y)) g_hdb->_sound->playSound(SND_BRIDGE_END); + // TODO: CHECKME - Using i as an index looks very wrong as the for statement uses j. + // This results in copying multiple times the same data for (int j = 0; j < _numBridges - 1; j++) memcpy(&_bridges[i], &_bridges[i + 1], sizeof(Bridge)); _numBridges--; @@ -363,9 +356,6 @@ HereT *AI::findHere(int x, int y) { } void AI::addToAutoList(int x, int y, const char *luaFuncInit, const char *luaFuncUse) { - - const char *get; - for (int i = 0; i < kMaxAutoActions; i++) { if (!_autoActions[i].x) { _autoActions[i].x = x; @@ -378,7 +368,7 @@ void AI::addToAutoList(int x, int y, const char *luaFuncInit, const char *luaFun if (_autoActions[i].luaFuncInit[0]) { g_hdb->_lua->callFunction(_autoActions[i].luaFuncInit, 2); - get = g_hdb->_lua->getStringOffStack(); + const char *get = g_hdb->_lua->getStringOffStack(); if (!get) return; strcpy(&_autoActions[i].entityName[0], get); diff --git a/engines/hdb/map.cpp b/engines/hdb/map.cpp index 822980140d..4467a9f122 100644 --- a/engines/hdb/map.cpp +++ b/engines/hdb/map.cpp @@ -209,13 +209,13 @@ void Map::loadSaveFile(Common::InSaveFile *in) { int Map::loadTiles() { - int tile, temp; + int temp; int skyIndex = 0; // Load all tiles for (uint j = 0; j < _height; j++) { for (uint i = 0; i < _width; i++) { - tile = _background[j * _width + i]; + int tile = _background[j * _width + i]; if ((temp = g_hdb->_gfx->isSky(tile)) && !skyIndex) { skyIndex = temp; } @@ -607,7 +607,6 @@ bool Map::load(Common::SeekableReadStream *stream) { // Scan all icons and init all Entities g_hdb->setupProgressBar(_iconNum); for (int i = 0; i < _iconNum; i++) { - g_hdb->makeProgress(); // Don't spawn Action Mode Entities in Puzzle Mode @@ -811,13 +810,8 @@ bool Map::load(Common::SeekableReadStream *stream) { } void Map::draw() { - if (!_mapLoaded) { + if (!_mapLoaded) return; - } - - int matrixY; - int screenX, screenY; - int maxTileX, maxTileY; // Calculate Tile Offsets and Panning Offsets _mapTileX = _mapX / kTileWidth; @@ -825,8 +819,8 @@ void Map::draw() { _mapTileXOff = -(_mapX % kTileWidth); _mapTileYOff = -(_mapY % kTileHeight); - matrixY = _mapTileY * _width; - screenY = _mapTileYOff; + int matrixY = _mapTileY * _width; + int screenY = _mapTileYOff; /* Note from Original Source: @@ -838,8 +832,8 @@ void Map::draw() { when we're at the very bottom of the map. */ - maxTileX = (_mapTileXOff >= -8) ? kScreenXTiles - 1 : kScreenXTiles; - maxTileY = (!_mapTileYOff) ? kScreenYTiles - 1 : kScreenYTiles; + int maxTileX = (_mapTileXOff >= -8) ? kScreenXTiles - 1 : kScreenXTiles; + int maxTileY = (!_mapTileYOff) ? kScreenYTiles - 1 : kScreenYTiles; if (matrixY + (maxTileY - 1)*_width > _height * _width) { return; @@ -848,7 +842,7 @@ void Map::draw() { _numForegrounds = _numGratings = 0; for (int j = 0; j < maxTileY; j++) { - screenX = _mapTileXOff; + int screenX = _mapTileXOff; for (int i = 0; i < maxTileX; i++) { // Draw Background Tile @@ -900,33 +894,33 @@ void Map::draw() { // Animate FAST Map Tiles if (!(_animCycle % kAnimFastFrames)) { - for (Common::Array::iterator it = _listBGAnimFast.begin(); it != _listBGAnimFast.end(); it++) { + for (Common::Array::iterator it = _listBGAnimFast.begin(); it != _listBGAnimFast.end(); ++it) { _background[(*it)] = g_hdb->_gfx->animateTile(_background[(*it)]); } - for (Common::Array::iterator it = _listFGAnimFast.begin(); it != _listFGAnimFast.end(); it++) { + for (Common::Array::iterator it = _listFGAnimFast.begin(); it != _listFGAnimFast.end(); ++it) { _foreground[(*it)] = g_hdb->_gfx->animateTile(_foreground[(*it)]); } } // Animate MEDIUM Map Tiles if (!(_animCycle % kAnimMediumFrames)) { - for (Common::Array::iterator it = _listBGAnimMedium.begin(); it != _listBGAnimMedium.end(); it++) { + for (Common::Array::iterator it = _listBGAnimMedium.begin(); it != _listBGAnimMedium.end(); ++it) { _background[(*it)] = g_hdb->_gfx->animateTile(_background[(*it)]); } - for (Common::Array::iterator it = _listFGAnimMedium.begin(); it != _listFGAnimMedium.end(); it++) { + for (Common::Array::iterator it = _listFGAnimMedium.begin(); it != _listFGAnimMedium.end(); ++it) { _foreground[(*it)] = g_hdb->_gfx->animateTile(_foreground[(*it)]); } } // Animate SLOW Map Tiles if (!(_animCycle % kAnimSlowFrames)) { - for (Common::Array::iterator it = _listBGAnimSlow.begin(); it != _listBGAnimSlow.end(); it++) { + for (Common::Array::iterator it = _listBGAnimSlow.begin(); it != _listBGAnimSlow.end(); ++it) { _background[(*it)] = g_hdb->_gfx->animateTile(_background[(*it)]); } - for (Common::Array::iterator it = _listFGAnimSlow.begin(); it != _listFGAnimSlow.end(); it++) { + for (Common::Array::iterator it = _listFGAnimSlow.begin(); it != _listFGAnimSlow.end(); ++it) { _foreground[(*it)] = g_hdb->_gfx->animateTile(_foreground[(*it)]); } } @@ -1009,6 +1003,7 @@ void Map::addBGTileAnimation(int x, int y) { Tile *tile = g_hdb->_gfx->getTile(_background[i]); if (!tile) return; + uint32 flags = tile->_flags; // BACKGROUND diff --git a/engines/hdb/menu.cpp b/engines/hdb/menu.cpp index fbaa389119..f97cacfff4 100644 --- a/engines/hdb/menu.cpp +++ b/engines/hdb/menu.cpp @@ -248,12 +248,10 @@ static const char nebulaNames[kNebulaCount][32] = { void Menu::startMenu() { - int i; - // stuff that gets loaded-in at Title Screen if (!_titleLogo) { _titleLogo = g_hdb->_gfx->loadPic(TITLELOGO); - for (i = 0; i < kNebulaCount; i++) + for (int i = 0; i < kNebulaCount; i++) _nebulaGfx[i] = g_hdb->_gfx->loadPic(nebulaNames[i]); _rocketMain = g_hdb->_gfx->loadPic(MENU_ROCKETSHIP1); @@ -308,7 +306,7 @@ void Menu::startMenu() { // if we're popping back into menu, don't init this if (!_fStars[0].y) { - for (i = 0; i < kMaxStars; i++) { + for (int i = 0; i < kMaxStars; i++) { _fStars[i].y = -30; _fStars[i].x = g_hdb->_rnd->getRandomNumber(kScreenWidth - 1); _fStars[i].speed = g_hdb->_rnd->getRandomNumber(4) + 1; @@ -565,7 +563,6 @@ void Menu::drawMenu() { //------------------------------------------------------------------- // DRAW GAMEFILES MENU //------------------------------------------------------------------- - int i; g_hdb->_gfx->draw3DStars(); // @@ -587,8 +584,8 @@ void Menu::drawMenu() { drawRocketAndSelections(); } else { - static int anim = 0; - static uint32 anim_time = 0; + static int anim = 0; + static uint32 anim_time = 0; drawNebula(); _titleLogo->drawMasked(centerPic(_titleLogo), _rocketY + kMTitleY); @@ -607,9 +604,8 @@ void Menu::drawMenu() { anim = 0; } - for (i = 0; i < kNumSaveSlots; i++) { - char buff[16]; - int seconds = _saveGames[i].seconds; + for (int i = 0; i < kNumSaveSlots; i++) { + int seconds = _saveGames[i].seconds; _slotGfx->drawMasked(kSaveSlotX - 8, i * 32 + (kSaveSlotY - 4)); if (seconds || _saveGames[i].mapName[0]) { @@ -619,6 +615,7 @@ void Menu::drawMenu() { g_hdb->_gfx->drawText(_saveGames[i].mapName); g_hdb->_gfx->setCursor(kSaveSlotX + 180, i * 32 + kSaveSlotY); + char buff[16]; sprintf(buff, "%02d:%02d", seconds / 3600, (seconds / 60) % 60); g_hdb->_gfx->drawText(buff); } @@ -628,9 +625,6 @@ void Menu::drawMenu() { //------------------------------------------------------------------- // DRAW WARP MENU //------------------------------------------------------------------- - int i; - char string[32]; - g_hdb->_gfx->draw3DStars(); drawNebula(); drawWarpScreen(); @@ -638,17 +632,18 @@ void Menu::drawMenu() { _titleLogo->drawMasked(centerPic(_titleLogo), _rocketY + kMTitleY); _menuBackoutGfx->drawMasked(kWarpBackoutX, kWarpBackoutY); - for (i = 0; i < 10; i++) { + char string[32]; + for (int i = 0; i < 10; i++) { sprintf(string, "Map %2d", i); g_hdb->_gfx->setCursor(kWarpX + 4, i * 16 + kWarpY); g_hdb->_gfx->drawText(string); } - for (i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { sprintf(string, "Map %d", i + 10); g_hdb->_gfx->setCursor(kWarpX + 80, i * 16 + kWarpY); g_hdb->_gfx->drawText(string); } - for (i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { sprintf(string, "Map %d", i + 20); g_hdb->_gfx->setCursor(kWarpX + 160, i * 16 + kWarpY); g_hdb->_gfx->drawText(string); @@ -674,8 +669,6 @@ void Menu::drawMenu() { } void Menu::freeMenu() { - int i; - // title sequence stuff if (_titleScreen) delete _titleScreen; @@ -722,11 +715,12 @@ void Menu::freeMenu() { delete _hdbLogoScreen; _hdbLogoScreen = NULL; - if (_nebulaGfx[0]) - for (i = 0; i < kNebulaCount; i++) { + if (_nebulaGfx[0]) { + for (int i = 0; i < kNebulaCount; i++) { delete _nebulaGfx[i]; _nebulaGfx[i] = NULL; } + } if (_sliderLeft) delete _sliderLeft; @@ -804,8 +798,6 @@ void Menu::freeMenu() { } bool Menu::startTitle() { - int i; - readConfig(); _titleScreen = g_hdb->_gfx->loadPic(MONKEYLOGOSCREEN); @@ -819,7 +811,7 @@ bool Menu::startTitle() { _rocketEx2 = g_hdb->_gfx->loadPic(MENU_EXHAUST2); _titleLogo = g_hdb->_gfx->loadPic(TITLELOGO); - for (i = 0; i < kNebulaCount; i++) + for (int i = 0; i < kNebulaCount; i++) _nebulaGfx[i] = g_hdb->_gfx->loadPic(nebulaNames[i]); _titleCycle = 1; // 1 = Waiting for OOH OOH @@ -1172,8 +1164,6 @@ void Menu::processInput(int x, int y) { //------------------------------------------------------------------- // OPTIONS INPUT //------------------------------------------------------------------- - int offset, oldVol; - int xit = getMenuKey(); // @@ -1184,10 +1174,11 @@ void Menu::processInput(int x, int y) { return; } + int offset; // Slider 1 if (x >= 0 && x <= kOptionsX + 200 && y >= kOptionsY + 20 && y <= kOptionsY + 36) { - oldVol = g_hdb->_sound->getMusicVolume(); + int oldVol = g_hdb->_sound->getMusicVolume(); if (x < kOptionsX) { if (oldVol) { g_hdb->_sound->stopMusic(); @@ -1243,8 +1234,8 @@ void Menu::processInput(int x, int y) { g_hdb->_sound->playSound(SND_MENU_BACKOUT); } - int i; - for (i = 0; i < kNumSaveSlots; i++) + int i = 0; + for (; i < kNumSaveSlots; i++) if (y >= (i * 32 + kSaveSlotY - 4) && y <= (i * 32 + kSaveSlotY + 24)) break; if (i >= kNumSaveSlots) @@ -1253,33 +1244,29 @@ void Menu::processInput(int x, int y) { _saveSlot = i; // LOAD GAME! - { - // clicked on empty slot? - if (!_saveGames[i].seconds && !_saveGames[i].mapName[0]) { - g_hdb->_sound->playSound(SND_MENU_BACKOUT); - return; - } + // clicked on empty slot? + if (!_saveGames[i].seconds && !_saveGames[i].mapName[0]) { + g_hdb->_sound->playSound(SND_MENU_BACKOUT); + return; + } - g_hdb->_sound->playSound(SND_MENU_ACCEPT); - if (g_hdb->loadGameState(_saveSlot).getCode() == Common::kNoError) { - _gamefilesActive = false; + g_hdb->_sound->playSound(SND_MENU_ACCEPT); + if (g_hdb->loadGameState(_saveSlot).getCode() == Common::kNoError) { + _gamefilesActive = false; - freeMenu(); - g_hdb->setGameState(GAME_PLAY); - // if we're at the very start of the level, re-init the level - if (!_saveGames[i].seconds) { - g_hdb->_lua->callFunction("level_loaded", 0); // call "level_loaded" Lua function, if it exists - if (!g_hdb->_ai->cinematicsActive()) - g_hdb->_gfx->turnOffFade(); - } + freeMenu(); + g_hdb->setGameState(GAME_PLAY); + // if we're at the very start of the level, re-init the level + if (!_saveGames[i].seconds) { + g_hdb->_lua->callFunction("level_loaded", 0); // call "level_loaded" Lua function, if it exists + if (!g_hdb->_ai->cinematicsActive()) + g_hdb->_gfx->turnOffFade(); } } } else if (_warpActive) { //------------------------------------------------------------------- // WARP INPUT //------------------------------------------------------------------- - int map; - int xit = getMenuKey(); if ((y >= kMenuExitY && x < kMenuExitXLeft) || xit) { @@ -1288,7 +1275,7 @@ void Menu::processInput(int x, int y) { g_hdb->_sound->playSound(SND_MENU_BACKOUT); _clickDelay = 10; } else if (y >= kWarpY && y < kWarpY + 160) { - char string[16]; + int map; if (x > kWarpX + 160) map = 20; @@ -1306,6 +1293,7 @@ void Menu::processInput(int x, int y) { g_hdb->_gfx->updateVideo(); _warpActive = 0; + char string[16]; if (map < 10) sprintf(string, "MAP0%d", map); else @@ -1363,9 +1351,7 @@ void Menu::drawNebula() { // // draw the falling stars // - int i; - - for (i = 0; i < kMaxStars; i++) { + for (int i = 0; i < kMaxStars; i++) { _fStars[i].y += _fStars[i].speed; if (_fStars[i].y > kScreenHeight) { _fStars[i].y = (g_hdb->_rnd->getRandomNumber(29) + 30) * -1; @@ -1421,14 +1407,12 @@ void Menu::drawRocketAndSelections() { } void Menu::drawSlider(int x, int y, int offset) { - int i, x1; - - x1 = x; + int x1 = x; _sliderLeft->drawMasked(x, y); x += _sliderLeft->_width; - for (i = 0; i < 12; i++) { + for (int i = 0; i < 12; i++) { _sliderMid->draw(x, y); x += _sliderMid->_width; } @@ -1438,14 +1422,12 @@ void Menu::drawSlider(int x, int y, int offset) { } void Menu::drawToggle(int x, int y, bool flag) { - int i, x1; - - x1 = x; + int x1 = x; _gCheckLeft->drawMasked(x, y); x += _gCheckLeft->_width; - for (i = 0; i < 12; i++) { + for (int i = 0; i < 12; i++) { _gCheckEmpty->draw(x, y); x += _gCheckEmpty->_width; } diff --git a/engines/hdb/window.cpp b/engines/hdb/window.cpp index 343f3ebd0b..ae28df7079 100644 --- a/engines/hdb/window.cpp +++ b/engines/hdb/window.cpp @@ -1065,11 +1065,9 @@ void Window::checkInvSelect(int x, int y) { } void Window::openDeliveries(bool animate) { - DlvEnt *d; - // Load Gfx for (int i = 0; i < g_hdb->_ai->getDeliveriesAmount(); i++) { - d = g_hdb->_ai->getDeliveryItem(i); + DlvEnt *d = g_hdb->_ai->getDeliveryItem(i); if (d->itemGfxName[0]) d->itemGfx = g_hdb->_gfx->loadTile(d->itemGfxName); if (d->destGfxName[0]) @@ -1092,8 +1090,6 @@ void Window::openDeliveries(bool animate) { } void Window::drawDeliveries() { - int baseX, drawX, drawY; - DlvEnt *d; //static uint32 timer = g_hdb->getTimeSlice() + 300; //unused int crazySounds[kNumCrazy] = { @@ -1139,17 +1135,18 @@ void Window::drawDeliveries() { if (_infobarDimmed > 1) return; - baseX = drawX = _dlvsInfo.x; - drawY = _dlvsInfo.y; + int baseX = _dlvsInfo.x; + int drawX = _dlvsInfo.x; + int drawY = _dlvsInfo.y; if (_dlvsInfo.selected >= g_hdb->_ai->getDeliveriesAmount()) _dlvsInfo.selected = g_hdb->_ai->getDeliveriesAmount() - 1; // Draw Delivery Items - int inv; - for (inv = 0; inv < g_hdb->_ai->getDeliveriesAmount(); inv++) { + int inv = 0; + for (; inv < g_hdb->_ai->getDeliveriesAmount(); inv++) { int centerX = baseX + (kScreenWidth - baseX) / 2; - d = g_hdb->_ai->getDeliveryItem(inv); + DlvEnt *d = g_hdb->_ai->getDeliveryItem(inv); if (_dlvsInfo.animate && inv == g_hdb->_ai->getDeliveriesAmount() - 1) { if (_dlvsInfo.go1) { if (_dlvsInfo.delay1 < g_hdb->getTimeSlice()) { -- cgit v1.2.3