From 30287c91d23a72f42e8f9c3f0a5b172dff95a5a0 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Tue, 5 Dec 2023 22:12:43 -0600 Subject: [PATCH] Use structured bindings for map k,v pairs. Place counters for for range iterator loops locally inside their scopes. --- Crawler/CharacterMenuWindow.cpp | 3 +- Crawler/Crawler.cpp | 36 ++++++++-------- Crawler/Item.cpp | 59 +++++++++++++------------ Crawler/Menu.cpp | 76 ++++++++++++++++----------------- Crawler/TMXParser.h | 3 +- Crawler/Test.cpp | 12 +++--- Crawler/Version.h | 2 +- 7 files changed, 93 insertions(+), 98 deletions(-) diff --git a/Crawler/CharacterMenuWindow.cpp b/Crawler/CharacterMenuWindow.cpp index 69674f9b..e3ff5909 100644 --- a/Crawler/CharacterMenuWindow.cpp +++ b/Crawler/CharacterMenuWindow.cpp @@ -123,8 +123,7 @@ void Menu::InitializeCharacterMenuWindow(){ ScrollableWindowComponent*equipList=Component(data.component->parentMenu,"Equip List"); equipList->RemoveAllComponents(); - int counter=0; - for(Item&it:availableEquipment){ + for(int counter=0;Item&it:availableEquipment){ float xOffset=(counter%3)*26; Item&itemInvRef=Inventory::GetItem(it.Name()); MenuItemItemButton*equip=NEW MenuItemItemButton(CHARACTER_MENU,{{2+xOffset,2},{24,24}},itemInvRef,MenuType::ENUM_END,[](MenuFuncData data){ diff --git a/Crawler/Crawler.cpp b/Crawler/Crawler.cpp index 545c56d5..5b1a5b57 100644 --- a/Crawler/Crawler.cpp +++ b/Crawler/Crawler.cpp @@ -126,8 +126,8 @@ Crawler::Crawler() std::string ITEM_SET_CONFIG = CONFIG_PATH + "item_set_config"_S; utils::datafile::Read(DATA,ITEM_SET_CONFIG); - for(auto&key:DATA.GetProperty("ItemConfiguration").GetKeys()){ - std::string config = DATA["ItemConfiguration"][key.first].GetString(); + for(auto&[key,value]:DATA.GetProperty("ItemConfiguration").GetKeys()){ + std::string config = DATA["ItemConfiguration"][key].GetString(); utils::datafile::Read(DATA,CONFIG_PATH + "item_directory"_S + config); } @@ -1436,8 +1436,8 @@ void Crawler::LoadLevel(MapName map){ Inventory::Clear("Stage Loot"); #pragma region Monster Spawn Data Setup - for(auto&key:MAP_DATA[map].SpawnerData){ - SpawnerTag&spawnData=MAP_DATA[map].SpawnerData[key.first]; + for(auto&[key,value]:MAP_DATA[map].SpawnerData){ + SpawnerTag&spawnData=MAP_DATA[map].SpawnerData[key]; std::vector>monster_list; vf2d spawnerRadius=vf2d{spawnData.ObjectData.GetFloat("width"),spawnData.ObjectData.GetFloat("height")}/2; @@ -1565,9 +1565,8 @@ void Crawler::LoadLevel(MapName map){ #pragma endregion #pragma region Bridge Layer Setup - int counter=0; bridgeLayerIndex=-1; - for(LayerTag&layer:MAP_DATA[map].LayerData){ + for(int counter=0;LayerTag&layer:MAP_DATA[map].LayerData){ if(IsBridgeLayer(layer)){ bridgeLayerIndex=counter; } @@ -1644,9 +1643,8 @@ geom2d::rectCrawler::GetTileCollision(MapName map,vf2d pos,bool upperLevel) return NO_COLLISION; } } - int counter=0; geom2d::rectfoundRect=NO_COLLISION; - for(LayerTag&layer:MAP_DATA[map].LayerData){ + for(int counter=0;LayerTag&layer:MAP_DATA[map].LayerData){ //auto HasNoClass=[&](){return layer.tag.data.find("class")==layer.tag.data.end();}; if(counter!=bridgeLayerIndex){ int tileID=layer.tiles[int(pos.y)/game->GetCurrentMap().tilewidth][int(pos.x)/game->GetCurrentMap().tilewidth]-1; @@ -1868,21 +1866,21 @@ bool Crawler::IsReflectiveTile(TilesheetData tileSheet,int tileID){ bool Crawler::OnUserDestroy(){ GFX.Reset(); - for(auto&data:MAP_DATA){ - if(MAP_DATA[data.first].optimizedTile!=nullptr){ - delete MAP_DATA[data.first].optimizedTile; + for(auto&[key,value]:MAP_DATA){ + if(MAP_DATA[key].optimizedTile!=nullptr){ + delete MAP_DATA[key].optimizedTile; } } - for(auto&key:MAP_TILESETS){ - delete key.second.tileset; + for(auto&[key,value]:MAP_TILESETS){ + delete value.tileset; } - for(auto&data:GameState::states){ - delete data.second; + for(auto&[key,value]:GameState::states){ + delete value; } delete[]pathfinder.nodes; Menu::CleanupAllMenus(); - for(auto&key:MonsterData::imgs){ - delete key.second; + for(auto&[key,value]:MonsterData::imgs){ + delete value; } return true; } @@ -2049,8 +2047,8 @@ void Crawler::InitializeGraphics(){ mappedKeys.push_back(key); } std::sort(mappedKeys.begin(),mappedKeys.end(),[](std::pair&key1,std::pair&key2){return key1.second slot; float cooldownTime="Item.Item Cooldown Time"_F; std::vectorstatValueList; - for(auto&itemKey:data[key.first].GetKeys()){ - std::string keyName=itemKey.first; + for(auto&[itemKey,itemValue]:data[key].GetKeys()){ + std::string keyName=itemKey; if(keyName=="Description"){ - description=data[key.first][keyName].GetString(); + description=data[key][keyName].GetString(); }else if(keyName=="ItemCategory"){ - category=data[key.first][keyName].GetString(); + category=data[key][keyName].GetString(); }else if(keyName=="ItemScript"){ - scriptName=data[key.first][keyName].GetString(); + scriptName=data[key][keyName].GetString(); }else if(keyName=="Cast Time"){ - castTime=float(data[key.first][keyName].GetReal()); + castTime=float(data[key][keyName].GetReal()); }else if(keyName=="Cooldown Time"){ - castTime=float(data[key.first][keyName].GetReal()); + castTime=float(data[key][keyName].GetReal()); }else if(keyName=="Slot"){ - for(auto&val:data[key.first][keyName].GetValues()){ + for(auto&val:data[key][keyName].GetValues()){ slot.push_back(val); } }else if(keyName=="StatValues"){ - for(int i=0;i0){ EnhancementInfo enhancementStats; for(int enhancementLevel=0;enhancementLevel<=10;enhancementLevel++){ - datafile&dat=data[key.first]["StatValues["+std::to_string(enhancementLevel)+"]"]; + datafile&dat=data[key]["StatValues["+std::to_string(enhancementLevel)+"]"]; int attrIndex=0; for(ItemAttribute&attr:statValueList){ enhancementStats.SetAttribute(enhancementLevel,attr,dat.GetInt(attrIndex)); @@ -149,11 +149,11 @@ void ItemInfo::InitializeItems(){ } if(scriptName!=""){ if(!ITEM_SCRIPTS.count(scriptName)){ - ERR("Could not load script "<buttons.SetInitialized(); menus[type]->keyboardButtons.SetInitialized(); - for(auto&key:menus[type]->components){ - MenuComponent*component=key.second; + for(auto&[key,value]:menus[type]->components){ + MenuComponent*component=value; component->AfterCreate(); } } @@ -218,8 +218,8 @@ void Menu::Update(Crawler*game){ SetMouseNavigation(true); } - for(auto&key:buttons){ - for(auto&button:key.second){ + for(auto&[key,value]:buttons){ + for(auto&button:value){ if(!button->disabled){ button->hovered=false; } @@ -235,14 +235,14 @@ void Menu::Update(Crawler*game){ } }else{ selection={-1,-1}; - for(auto&key:buttons){ + for(auto&[key,value]:buttons){ int index=0; - for(auto&button:key.second){ + for(auto&button:value){ if(!button->disabled){ if(button->GetHoverState(game)){ button->hovered=true; itemHovered=true; - selection.y=key.first; + selection.y=key; selection.x=index; } } @@ -291,8 +291,8 @@ void Menu::Update(Crawler*game){ } KeyboardButtonNavigation(game,pos); - for(auto&key:buttons){ - for(auto&button:key.second){ + for(auto&[key,value]:buttons){ + for(auto&button:value){ if(button->renderInMain){ button->_Update(game); } @@ -321,8 +321,8 @@ void Menu::Draw(Crawler*game){ component->_Draw(game); } } - for(auto&key:buttons){ - for(auto&button:key.second){ + for(auto&[key,value]:buttons){ + for(auto&button:value){ if(button->renderInMain){ button->_Draw(game); } @@ -337,8 +337,8 @@ void Menu::Draw(Crawler*game){ component->_DrawDecal(game,this==Menu::stack.back()); } } - for(auto&key:buttons){ - for(auto&button:key.second){ + for(auto&[key,value]:buttons){ + for(auto&button:value){ if(button->renderInMain){ button->_DrawDecal(game,this==Menu::stack.back()); } @@ -409,17 +409,17 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){ bool selectedItem=false; if(selection==vi2d{-1,-1}){ //Highlight first item. - for(auto&key:keyboardButtons){ - selection.y=key.first; + for(auto&[key,value]:keyboardButtons){ + selection.y=key; break; } }else{ - for(auto&key:keyboardButtons){ + for(auto&[key,value]:keyboardButtons){ if(found){ //Once we discover the previous element, the next element becomes our next selection. int previousButtonX=int(keyboardButtons[selection.y][selection.x]->rect.pos.x); - selection.y=key.first; + selection.y=key; int index=0; - for(auto&button:key.second){ //Try to match a button in the same column as this button first. + for(auto&button:value){ //Try to match a button in the same column as this button first. if(previousButtonX==button->rect.pos.x){ selection.x=index; break; @@ -429,15 +429,15 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){ selectedItem=true; break; } - if(key.first==selection.y + if(key==selection.y //It's entirely possible this button was selected from the button selection list and may be out-of-bounds here. &&selection.x>=0&&selection.x=0&&selection.xrect.pos.x); @@ -473,8 +473,8 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){ } }else{ //Since we didn't find it, it means we're at the top of the list or the list is empty. Go to the last element and use that one. int lastInd=-1; - for(auto&key:keyboardButtons){ - lastInd=key.first; + for(auto&[key,value]:keyboardButtons){ + lastInd=key; } selection.y=lastInd; } @@ -495,9 +495,9 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){ if(selection==vi2d{-1,-1}&&buttons.size()>0){ //Find the first possible button entry in the map... int firstInd=-1; - for(auto&key:buttons){ - if(buttons[key.first].size()>0){ - firstInd=key.first; + for(auto&[key,value]:buttons){ + if(buttons[key].size()>0){ + firstInd=key; break; } } @@ -507,12 +507,12 @@ void Menu::KeyboardButtonNavigation(Crawler*game,vf2d menuPos){ } }else{//Mouse click. selection={-1,-1}; - for(auto&key:buttons){ + for(auto&[key,value]:buttons){ int index=0; - for(auto&button:key.second){ + for(auto&button:value){ if(!button->disabled){ if(geom2d::overlaps(geom2d::rect{button->rect.pos+menuPos,button->rect.size},game->GetMousePos())){ - selection={index,key.first}; + selection={index,key}; break; } } @@ -692,8 +692,8 @@ bool Menu::IsMenuOpen(){ } void Menu::CleanupAllMenus(){ - for(auto&key:Menu::menus){ - Menu*menu=key.second; + for(auto&[key,value]:Menu::menus){ + Menu*menu=value; for(auto&componentKey:menu->components){ MenuComponent*component=componentKey.second; component->Cleanup(); diff --git a/Crawler/TMXParser.h b/Crawler/TMXParser.h index deb96233..289b1855 100644 --- a/Crawler/TMXParser.h +++ b/Crawler/TMXParser.h @@ -470,8 +470,7 @@ class TMXParser{ } } } - int counter=0; - for(ConnectionPoint&connection:State_OverworldMap::connections){ + for(int counter=0;ConnectionPoint&connection:State_OverworldMap::connections){ for(int val:connection.neighbors){ if(val!=-1){ ConnectionPoint&neighbor=State_OverworldMap::connections.at(val); diff --git a/Crawler/Test.cpp b/Crawler/Test.cpp index 1232dddb..6ebd8d3b 100644 --- a/Crawler/Test.cpp +++ b/Crawler/Test.cpp @@ -56,12 +56,12 @@ void Test::RunMapTests(){ is("There are two LowerBridgeCollision zones in Campaign I-I", game->MAP_DATA.at(CAMPAIGN_1_1).ZoneData.count("LowerBridgeCollision") &&game->MAP_DATA.at(CAMPAIGN_1_1).ZoneData.at("LowerBridgeCollision").size()>=2); - for(auto&key:game->MAP_DATA){ - is("A Map type has been selected for map "+std::to_string(key.first), - key.second.mapType!=""&&key.second.mapType!="Unspecified"); - if(key.second.mapType=="Dungeon"){ - is("There is an EndZone in Dungeon "+std::to_string(key.first), - key.second.ZoneData.count("EndZone")); + for(auto&[key,value]:game->MAP_DATA){ + is("A Map type has been selected for map "+std::to_string(key), + value.mapType!=""&&value.mapType!="Unspecified"); + if(value.mapType=="Dungeon"){ + is("There is an EndZone in Dungeon "+std::to_string(key), + value.ZoneData.count("EndZone")); } } } \ No newline at end of file diff --git a/Crawler/Version.h b/Crawler/Version.h index 90beab66..898854fa 100644 --- a/Crawler/Version.h +++ b/Crawler/Version.h @@ -39,7 +39,7 @@ All rights reserved. #define VERSION_MAJOR 0 #define VERSION_MINOR 2 #define VERSION_PATCH 1 -#define VERSION_BUILD 3470 +#define VERSION_BUILD 3471 #define stringify(a) stringify_(a) #define stringify_(a) #a