From bf661d865cdfd26c48c5d10b00ba48a6e6c16249 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ty=C3=A1s=20Mustoha?= Date: Fri, 21 Jun 2024 12:20:07 +0200 Subject: [PATCH 1/3] Moved the map background conversion literals to a common file --- src/common/CMakeLists.txt | 3 ++- src/common/map.cpp | 2 +- src/common/map.h | 2 +- src/common/map/MapReader15xx.cpp | 31 ++------------------------ src/common/map/MapReader17xx.cpp | 25 +++++++++++---------- src/common/map/MapReaderConstants.h | 34 +++++++++++++++++++++++++++++ src/leveleditor/leveleditor.cpp | 8 +++---- 7 files changed, 57 insertions(+), 48 deletions(-) create mode 100644 src/common/map/MapReaderConstants.h diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index c2713402..f314ed74 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -42,6 +42,7 @@ add_library(CommonFiles STATIC map/MapReader16xx.cpp map/MapReader17xx.cpp map/MapReader18xx.cpp + map/MapReaderConstants.h math/Vec2.h MapList.cpp MapList.h @@ -77,7 +78,7 @@ if (USE_PNG_SAVE AND NOT USE_SDL2_LIBS) target_sources(CommonFiles PRIVATE savepng.cpp savepng.h) endif() -target_compile_features(CommonFiles PUBLIC cxx_std_14) +target_compile_features(CommonFiles PUBLIC cxx_std_17) if (USE_SDL2_LIBS) diff --git a/src/common/map.cpp b/src/common/map.cpp index 68044a98..cd4ab215 100644 --- a/src/common/map.cpp +++ b/src/common/map.cpp @@ -949,7 +949,7 @@ void CMap::saveMap(const std::string& file) } //Write background File - mapfile.write_string_long(szBackgroundFile); + mapfile.write_string_long(szBackgroundFile.c_str()); //Save the default on/off switch states for (short iSwitch = 0; iSwitch < 4; iSwitch++) diff --git a/src/common/map.h b/src/common/map.h index 30dc4977..d6b03623 100644 --- a/src/common/map.h +++ b/src/common/map.h @@ -246,7 +246,7 @@ class CMap bool findspawnpoint(short iType, short * x, short * y, short width, short height, bool tilealigned); bool IsInPlatformNoSpawnZone(short x, short y, short width, short height); - char szBackgroundFile[128]; + std::string szBackgroundFile; short backgroundID; short eyecandy[3]; short musicCategoryID; diff --git a/src/common/map/MapReader15xx.cpp b/src/common/map/MapReader15xx.cpp index 90a13c0e..2ce2226e 100644 --- a/src/common/map/MapReader15xx.cpp +++ b/src/common/map/MapReader15xx.cpp @@ -2,6 +2,7 @@ #include "GameValues.h" #include "map.h" +#include "MapReaderConstants.h" #include "FileIO.h" #include "TilesetManager.h" @@ -10,34 +11,6 @@ extern CTilesetManager* g_tilesetmanager; extern short g_iTileTypeConversion[NUMTILETYPES]; -const char * g_szBackgroundConversion[26] = { - "Land_Classic.png", - "Castle_Dungeon.png", - "Desert_Pyramids.png", - "Ghost_GhostHouse.png", - "Underground_Cave.png", - "Clouds_AboveTheClouds.png", - "Castle_GoombaHall.png", - "Platforms_GreenSpottedHills.png", - "Snow_SnowTrees.png", - "Desert_Desert.png", - "Underground_BrownRockWall.png", - "Land_CastleWall.png", - "Clouds_Clouds.png", - "Land_GreenMountains.png", - "Land_InTheTrees.png", - "Battle_Manor.png", - "Platforms_JaggedGreenStones.png", - "Underground_RockWallAndPlants.png", - "Underground_DarkPipes.png", - "Bonus_StarryNight.png", - "Platforms_CloudsAndWaterfalls.png", - "Battle_GoombaPillars.png", - "Bonus_HillsAtNight.png", - "Castle_CastlePillars.png", - "Land_GreenHillsAndClouds.png", - "Platforms_BlueSpottedHills.png" -}; namespace { const short g_iMusicCategoryConversion[26] {0, 3, 8, 5, 1, 9, 3, 4, 10, 8, 1, 0, 9, 0, 0, 7, 4, 1, 1, 6, 4, 7, 6, 3, 0, 4}; @@ -101,7 +74,7 @@ void MapReader1500::read_background(CMap& map, BinaryFile& mapfile) { // Read old background IDs and convert that to a background filename map.backgroundID = (short)mapfile.read_i32(); - strcpy(map.szBackgroundFile, g_szBackgroundConversion[map.backgroundID]); + map.szBackgroundFile = g_szBackgroundConversion[map.backgroundID]; } void MapReader1500::read_music_category(CMap& map, BinaryFile& mapfile) diff --git a/src/common/map/MapReader17xx.cpp b/src/common/map/MapReader17xx.cpp index 971e44d6..b18ac174 100644 --- a/src/common/map/MapReader17xx.cpp +++ b/src/common/map/MapReader17xx.cpp @@ -2,6 +2,7 @@ #include "GameValues.h" #include "map.h" +#include "MapReaderConstants.h" #include "movingplatform.h" #include "FileIO.h" #include "TilesetManager.h" @@ -11,7 +12,6 @@ #include extern CTilesetManager* g_tilesetmanager; -extern const char* g_szBackgroundConversion[26]; extern short g_iTileTypeConversion[NUMTILETYPES]; using namespace std; @@ -89,19 +89,18 @@ void MapReader1700::read_tiles(CMap& map, BinaryFile& mapfile) void MapReader1701::read_background(CMap& map, BinaryFile& mapfile) { //Read in background to use - mapfile.read_string_long(map.szBackgroundFile, 128); - - for (short iBackground = 0; iBackground < 26; iBackground++) { - const char * szFindUnderscore = strstr(g_szBackgroundConversion[iBackground], "_"); + char text[128]; + mapfile.read_string_long(text, 128); + map.szBackgroundFile = text; + for (const std::string_view background : g_szBackgroundConversion) { // All items must have an underscore in g_szBackgroundConversion - assert(szFindUnderscore); - - szFindUnderscore++; + const size_t underscorePos = background.find("_"); + assert(underscorePos != background.npos); - if (!strcmp(szFindUnderscore, map.szBackgroundFile)) { - assert(strlen(g_szBackgroundConversion[iBackground]) <= 128); - strcpy(map.szBackgroundFile, g_szBackgroundConversion[iBackground]); + if (background.substr(underscorePos + 1) == map.szBackgroundFile) { + map.szBackgroundFile = background; + break; } } } @@ -109,7 +108,9 @@ void MapReader1701::read_background(CMap& map, BinaryFile& mapfile) void MapReader1702::read_background(CMap& map, BinaryFile& mapfile) { //Read in background to use - mapfile.read_string_long(map.szBackgroundFile, 128); + char text[128]; + mapfile.read_string_long(text, 128); + map.szBackgroundFile = text; } void MapReader1700::set_preview_switches(CMap& map, BinaryFile& mapfile) diff --git a/src/common/map/MapReaderConstants.h b/src/common/map/MapReaderConstants.h new file mode 100644 index 00000000..2399f4c8 --- /dev/null +++ b/src/common/map/MapReaderConstants.h @@ -0,0 +1,34 @@ +#pragma once + +#include +#include + + +constexpr std::array g_szBackgroundConversion { + "Land_Classic.png", + "Castle_Dungeon.png", + "Desert_Pyramids.png", + "Ghost_GhostHouse.png", + "Underground_Cave.png", + "Clouds_AboveTheClouds.png", + "Castle_GoombaHall.png", + "Platforms_GreenSpottedHills.png", + "Snow_SnowTrees.png", + "Desert_Desert.png", + "Underground_BrownRockWall.png", + "Land_CastleWall.png", + "Clouds_Clouds.png", + "Land_GreenMountains.png", + "Land_InTheTrees.png", + "Battle_Manor.png", + "Platforms_JaggedGreenStones.png", + "Underground_RockWallAndPlants.png", + "Underground_DarkPipes.png", + "Bonus_StarryNight.png", + "Platforms_CloudsAndWaterfalls.png", + "Battle_GoombaPillars.png", + "Bonus_HillsAtNight.png", + "Castle_CastlePillars.png", + "Land_GreenHillsAndClouds.png", + "Platforms_BlueSpottedHills.png" +}; diff --git a/src/leveleditor/leveleditor.cpp b/src/leveleditor/leveleditor.cpp index 34d045dd..f2897a8d 100644 --- a/src/leveleditor/leveleditor.cpp +++ b/src/leveleditor/leveleditor.cpp @@ -1002,12 +1002,12 @@ int editor_edit() backgroundlist->next(); rm->spr_background.init(backgroundlist->currentPath()); - strcpy(g_map->szBackgroundFile, getFilenameFromPath(backgroundlist->currentPath()).c_str()); + g_map->szBackgroundFile = getFilenameFromPath(backgroundlist->currentPath()); if (!CheckKey(keystate, SDLK_LSHIFT) && !CheckKey(keystate, SDLK_RSHIFT)) { //Set music to background default for (short iCategory = 0; iCategory < MAXMUSICCATEGORY; iCategory++) { - if (!strncmp(g_szMusicCategoryNames[iCategory], g_map->szBackgroundFile, strlen(g_szMusicCategoryNames[iCategory]))) { + if (!strncmp(g_szMusicCategoryNames[iCategory], g_map->szBackgroundFile.c_str(), strlen(g_szMusicCategoryNames[iCategory]))) { g_map->musicCategoryID = iCategory; break; } @@ -4232,12 +4232,12 @@ int editor_backgrounds() backgroundlist->setCurrentIndex(iPage * 16 + iBackground); rm->spr_background.init(backgroundlist->currentPath()); - strcpy(g_map->szBackgroundFile, getFilenameFromPath(backgroundlist->currentPath()).c_str()); + g_map->szBackgroundFile = getFilenameFromPath(backgroundlist->currentPath()); if (event.button.button == SDL_BUTTON_LEFT) { //Set music to background default for (short iCategory = 0; iCategory < MAXMUSICCATEGORY; iCategory++) { - if (!strncmp(g_szMusicCategoryNames[iCategory], g_map->szBackgroundFile, strlen(g_szMusicCategoryNames[iCategory]))) { + if (!strncmp(g_szMusicCategoryNames[iCategory], g_map->szBackgroundFile.c_str(), strlen(g_szMusicCategoryNames[iCategory]))) { g_map->musicCategoryID = iCategory; break; } From 2c0ba017be60b4df2d50392f47c1cd018ca421ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ty=C3=A1s=20Mustoha?= Date: Fri, 21 Jun 2024 19:22:15 +0200 Subject: [PATCH 2/3] Moved the map tile conversion literals to a common file --- src/common/GlobalConstants.h | 2 -- src/common/map.cpp | 10 ++++------ src/common/map/MapReader15xx.cpp | 3 +-- src/common/map/MapReader16xx.cpp | 4 ++-- src/common/map/MapReader17xx.cpp | 5 ++--- src/common/map/MapReader18xx.cpp | 6 +++--- src/common/map/MapReaderConstants.h | 5 ++++- src/leveleditor/leveleditor.cpp | 1 + 8 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/common/GlobalConstants.h b/src/common/GlobalConstants.h index 40c6f040..391d3123 100644 --- a/src/common/GlobalConstants.h +++ b/src/common/GlobalConstants.h @@ -101,8 +101,6 @@ #define TILESETHEIGHT 30 #define TILESETWIDTH 32 -#define NUMTILETYPES 19 - #define TILESETANIMATED -1 #define TILESETNONE -2 #define TILESETUNKNOWN -3 diff --git a/src/common/map.cpp b/src/common/map.cpp index cd4ab215..23496583 100644 --- a/src/common/map.cpp +++ b/src/common/map.cpp @@ -5,13 +5,14 @@ #include "GlobalConstants.h" #include "Game.h" #include "FileIO.h" -#include "map/MapReader.h" #include "movingplatform.h" #include "path.h" #include "RandomNumberGenerator.h" #include "ResourceManager.h" #include "TilesetManager.h" #include "Version.h" +#include "map/MapReader.h" +#include "map/MapReaderConstants.h" #include "SDL_image.h" #include "sdl12wrapper.h" @@ -42,9 +43,6 @@ using std::endl; extern gfxSprite spr_frontmap[2]; -//Converts the tile type into the flags that this tile carries (solid + ice + death, etc) -short g_iTileTypeConversion[NUMTILETYPES] = {0, 1, 2, 5, 121, 9, 17, 33, 65, 6, 21, 37, 69, 3961, 265, 529, 1057, 2113, 4096}; - short g_iCurrentDrawIndex = 0; @@ -749,7 +747,7 @@ void CMap::saveMap(const std::string& file) for (short iRow = 0; iRow < platforms[iPlatform]->iTileHeight; iRow++) { //Set the tile type flags for each tile int iType = platforms[iPlatform]->iTileType[iCol][iRow].iType; - if (iType >= 0 && iType < NUMTILETYPES) { + if (0 <= iType && iType < g_iTileTypeConversion.size()) { platforms[iPlatform]->iTileType[iCol][iRow].iFlags = g_iTileTypeConversion[iType]; } else { platforms[iPlatform]->iTileType[iCol][iRow].iType = tile_nonsolid; @@ -777,7 +775,7 @@ void CMap::saveMap(const std::string& file) for (i = 0; i < MAPWIDTH; i++) { //Set the tile type flags for each tile int iType = mapdatatop[i][j].iType; - if (iType >= 0 && iType < NUMTILETYPES) { + if (0 <= iType && iType < g_iTileTypeConversion.size()) { mapdatatop[i][j].iFlags = g_iTileTypeConversion[iType]; } else { mapdatatop[i][j].iType = tile_nonsolid; diff --git a/src/common/map/MapReader15xx.cpp b/src/common/map/MapReader15xx.cpp index 2ce2226e..3f9adebc 100644 --- a/src/common/map/MapReader15xx.cpp +++ b/src/common/map/MapReader15xx.cpp @@ -9,7 +9,6 @@ #include extern CTilesetManager* g_tilesetmanager; -extern short g_iTileTypeConversion[NUMTILETYPES]; namespace { @@ -39,7 +38,7 @@ void MapReader1500::read_tiles(CMap& map, BinaryFile& mapfile) TileType iType = g_tilesetmanager->classicTileset().tileType(tile->iCol, tile->iRow); - if (iType >= 0 && iType < NUMTILETYPES) { + if (0 <= iType && iType < g_iTileTypeConversion.size()) { map.mapdatatop[i][j].iType = iType; map.mapdatatop[i][j].iFlags = g_iTileTypeConversion[iType]; } else { diff --git a/src/common/map/MapReader16xx.cpp b/src/common/map/MapReader16xx.cpp index 1c8a3002..9abb30ba 100644 --- a/src/common/map/MapReader16xx.cpp +++ b/src/common/map/MapReader16xx.cpp @@ -2,13 +2,13 @@ #include "GameValues.h" #include "map.h" +#include "MapReaderConstants.h" #include "FileIO.h" #include "TilesetManager.h" #include extern CTilesetManager* g_tilesetmanager; -extern short g_iTileTypeConversion[NUMTILETYPES]; namespace { @@ -93,7 +93,7 @@ void MapReader1600::read_tiles(CMap& map, BinaryFile& mapfile) TilesetTile * tile = &map.mapdata[i][j][k]; TileType type = g_tilesetmanager->classicTileset().tileType(tile->iCol, tile->iRow); if (type != tile_nonsolid) { - if (type >= 0 && type < NUMTILETYPES) { + if (0 <= type && type < g_iTileTypeConversion.size()) { map.mapdatatop[i][j].iType = type; map.mapdatatop[i][j].iFlags = g_iTileTypeConversion[type]; } else { diff --git a/src/common/map/MapReader17xx.cpp b/src/common/map/MapReader17xx.cpp index b18ac174..3da11a52 100644 --- a/src/common/map/MapReader17xx.cpp +++ b/src/common/map/MapReader17xx.cpp @@ -12,7 +12,6 @@ #include extern CTilesetManager* g_tilesetmanager; -extern short g_iTileTypeConversion[NUMTILETYPES]; using namespace std; @@ -164,7 +163,7 @@ void MapReader1700::read_warp_locations(CMap& map, BinaryFile& mapfile) for (unsigned short i = 0; i < MAPWIDTH; i++) { TileType iType = (TileType)mapfile.read_i32(); - if (iType >= 0 && iType < NUMTILETYPES) { + if (0 <= iType && iType < g_iTileTypeConversion.size()) { map.mapdatatop[i][j].iType = iType; map.mapdatatop[i][j].iFlags = g_iTileTypeConversion[iType]; } else { @@ -290,7 +289,7 @@ void MapReader1700::read_platform_tiles(CMap& map, BinaryFile& mapfile, type = g_tilesetmanager->classicTileset().tileType(tile->iCol, tile->iRow); } - if (type >= 0 && type < NUMTILETYPES) { + if (0 <= type && type < g_iTileTypeConversion.size()) { types[iCol][iRow].iType = type; types[iCol][iRow].iFlags = g_iTileTypeConversion[type]; } else { diff --git a/src/common/map/MapReader18xx.cpp b/src/common/map/MapReader18xx.cpp index f0bfdf7d..b21f416b 100644 --- a/src/common/map/MapReader18xx.cpp +++ b/src/common/map/MapReader18xx.cpp @@ -1,6 +1,7 @@ #include "MapReader.h" #include "map.h" +#include "MapReaderConstants.h" #include "movingplatform.h" #include "FileIO.h" #include "TilesetManager.h" @@ -8,7 +9,6 @@ #include extern CTilesetManager* g_tilesetmanager; -extern short g_iTileTypeConversion[NUMTILETYPES]; using namespace std; @@ -179,7 +179,7 @@ void MapReader1800::read_warp_locations(CMap& map, BinaryFile& mapfile) for (unsigned short i = 0; i < MAPWIDTH; i++) { TileType iType = (TileType)mapfile.read_i32(); - if (iType >= 0 && iType < NUMTILETYPES) { + if (0 <= iType && iType < g_iTileTypeConversion.size()) { map.mapdatatop[i][j].iType = iType; map.mapdatatop[i][j].iFlags = g_iTileTypeConversion[iType]; } else { @@ -349,7 +349,7 @@ void MapReader1800::read_platform_tiles(CMap& map, BinaryFile& mapfile, TileType iType = (TileType)mapfile.read_i32(); - if (iType >= 0 && iType < NUMTILETYPES) { + if (0 <= iType && iType < g_iTileTypeConversion.size()) { types[iCol][iRow].iType = iType; types[iCol][iRow].iFlags = g_iTileTypeConversion[iType]; } else { diff --git a/src/common/map/MapReaderConstants.h b/src/common/map/MapReaderConstants.h index 2399f4c8..a4e214d9 100644 --- a/src/common/map/MapReaderConstants.h +++ b/src/common/map/MapReaderConstants.h @@ -30,5 +30,8 @@ constexpr std::array g_szBackgroundConversion { "Bonus_HillsAtNight.png", "Castle_CastlePillars.png", "Land_GreenHillsAndClouds.png", - "Platforms_BlueSpottedHills.png" + "Platforms_BlueSpottedHills.png", }; + +//Converts the tile type into the flags that this tile carries (solid + ice + death, etc) +constexpr std::array g_iTileTypeConversion {0, 1, 2, 5, 121, 9, 17, 33, 65, 6, 21, 37, 69, 3961, 265, 529, 1057, 2113, 4096}; diff --git a/src/leveleditor/leveleditor.cpp b/src/leveleditor/leveleditor.cpp index f2897a8d..1b2f202e 100644 --- a/src/leveleditor/leveleditor.cpp +++ b/src/leveleditor/leveleditor.cpp @@ -102,6 +102,7 @@ enum {EDITOR_EDIT, EDITOR_TILES, EDITOR_QUIT, SAVE_AS, FIND, CLEAR_MAP, EDITOR_B #define MAX_PLATFORMS 32 #define MAX_PLATFORM_VELOCITY 16 +#define NUMTILETYPES 19 constexpr int UI_PLATFORM_ROWS = 4; constexpr int UI_PLATFORM_COLS = 8; From 6b82ef9e8f9aa94ec769f0a13d42e3f34fd0a282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ty=C3=A1s=20Mustoha?= Date: Fri, 21 Jun 2024 19:29:42 +0200 Subject: [PATCH 3/3] Removed some unused includes --- src/common/map/MapReader15xx.cpp | 2 -- src/common/map/MapReader17xx.cpp | 8 +++----- src/common/map/MapReader18xx.cpp | 8 +++----- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/common/map/MapReader15xx.cpp b/src/common/map/MapReader15xx.cpp index 3f9adebc..b25a2055 100644 --- a/src/common/map/MapReader15xx.cpp +++ b/src/common/map/MapReader15xx.cpp @@ -6,8 +6,6 @@ #include "FileIO.h" #include "TilesetManager.h" -#include - extern CTilesetManager* g_tilesetmanager; diff --git a/src/common/map/MapReader17xx.cpp b/src/common/map/MapReader17xx.cpp index 3da11a52..f356ad2a 100644 --- a/src/common/map/MapReader17xx.cpp +++ b/src/common/map/MapReader17xx.cpp @@ -8,12 +8,10 @@ #include "TilesetManager.h" #include -#include #include extern CTilesetManager* g_tilesetmanager; -using namespace std; MapReader1700::MapReader1700() : MapReader1600() @@ -194,9 +192,9 @@ bool MapReader1700::read_spawn_areas(CMap& map, BinaryFile& mapfile) map.numspawnareas[i] = (short)mapfile.read_i32(); if (map.numspawnareas[i] > MAXSPAWNAREAS) { - cout << endl << " ERROR: Number of spawn areas (" << map.numspawnareas[i] - << ") was greater than max allowed (" << MAXSPAWNAREAS << ')' - << endl; + std::cout << std::endl << " ERROR: Number of spawn areas (" << map.numspawnareas[i] + << ") was greater than max allowed (" << MAXSPAWNAREAS << ')' + << std::endl; return false; } diff --git a/src/common/map/MapReader18xx.cpp b/src/common/map/MapReader18xx.cpp index b21f416b..82cc196e 100644 --- a/src/common/map/MapReader18xx.cpp +++ b/src/common/map/MapReader18xx.cpp @@ -10,8 +10,6 @@ extern CTilesetManager* g_tilesetmanager; -using namespace std; - namespace { struct TilesetTranslation { @@ -217,9 +215,9 @@ bool MapReader1800::read_spawn_areas(CMap& map, BinaryFile& mapfile) map.numspawnareas[i] = (short)mapfile.read_i32(); if (map.numspawnareas[i] > MAXSPAWNAREAS) { - cout << endl << " ERROR: Number of spawn areas (" << map.numspawnareas[i] - << ") was greater than max allowed (" << MAXSPAWNAREAS << ')' - << endl; + std::cout << std::endl << " ERROR: Number of spawn areas (" << map.numspawnareas[i] + << ") was greater than max allowed (" << MAXSPAWNAREAS << ')' + << std::endl; return false; }