Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the map literal externs #328

Merged
merged 3 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions src/common/GlobalConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@
#define TILESETHEIGHT 30
#define TILESETWIDTH 32

#define NUMTILETYPES 19

#define TILESETANIMATED -1
#define TILESETNONE -2
#define TILESETUNKNOWN -3
Expand Down
12 changes: 5 additions & 7 deletions src/common/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;


Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -949,7 +947,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++)
Expand Down
2 changes: 1 addition & 1 deletion src/common/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 4 additions & 34 deletions src/common/map/MapReader15xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,12 @@

#include "GameValues.h"
#include "map.h"
#include "MapReaderConstants.h"
#include "FileIO.h"
#include "TilesetManager.h"

#include <cstring>

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};
Expand Down Expand Up @@ -66,7 +36,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 {
Expand Down Expand Up @@ -101,7 +71,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)
Expand Down
4 changes: 2 additions & 2 deletions src/common/map/MapReader16xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

#include "GameValues.h"
#include "map.h"
#include "MapReaderConstants.h"
#include "FileIO.h"
#include "TilesetManager.h"

#include <iostream>

extern CTilesetManager* g_tilesetmanager;
extern short g_iTileTypeConversion[NUMTILETYPES];


namespace {
Expand Down Expand Up @@ -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 {
Expand Down
38 changes: 18 additions & 20 deletions src/common/map/MapReader17xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@

#include "GameValues.h"
#include "map.h"
#include "MapReaderConstants.h"
#include "movingplatform.h"
#include "FileIO.h"
#include "TilesetManager.h"

#include <cassert>
#include <cstring>
#include <iostream>

extern CTilesetManager* g_tilesetmanager;
extern const char* g_szBackgroundConversion[26];
extern short g_iTileTypeConversion[NUMTILETYPES];

using namespace std;

MapReader1700::MapReader1700()
: MapReader1600()
Expand Down Expand Up @@ -89,27 +86,28 @@ 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;
}
}
}

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)
Expand Down Expand Up @@ -163,7 +161,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 {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -289,7 +287,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 {
Expand Down
14 changes: 6 additions & 8 deletions src/common/map/MapReader18xx.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#include "MapReader.h"

#include "map.h"
#include "MapReaderConstants.h"
#include "movingplatform.h"
#include "FileIO.h"
#include "TilesetManager.h"

#include <iostream>

extern CTilesetManager* g_tilesetmanager;
extern short g_iTileTypeConversion[NUMTILETYPES];

using namespace std;


namespace {
Expand Down Expand Up @@ -179,7 +177,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 {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -349,7 +347,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 {
Expand Down
37 changes: 37 additions & 0 deletions src/common/map/MapReaderConstants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <array>
#include <string_view>


constexpr std::array<std::string_view, 26> 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",
};

//Converts the tile type into the flags that this tile carries (solid + ice + death, etc)
constexpr std::array<short, 19> g_iTileTypeConversion {0, 1, 2, 5, 121, 9, 17, 33, 65, 6, 21, 37, 69, 3961, 265, 529, 1057, 2113, 4096};
9 changes: 5 additions & 4 deletions src/leveleditor/leveleditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -1002,12 +1003,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;
}
Expand Down Expand Up @@ -4232,12 +4233,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;
}
Expand Down