From cbceeccf78d6e19f8a4effd69216b572cf1879c9 Mon Sep 17 00:00:00 2001 From: Misa Date: Sun, 27 Sep 2020 19:15:06 -0700 Subject: [PATCH] Clean up and prevent unnecessary qualifiers to self By "unnecessary qualifiers to self", I mean something like using the 'game.' qualifier for a variable on the Game class when you're inside a function on the Game class itself. This patch is to enforce consistency as most of the code doesn't have these unnecessary qualifiers. To prevent further unnecessary qualifiers to self, I made it so the extern in each header file can be omitted by using a define. That way, if someone writes something referring to 'game.' on a Game function, there will be a compile error. However, if you really need to have a reference to the global name, and you're within the same .cpp file as the implementation of that object, you can just do the extern at the function-level. A good example of this is editorinput()/editorrender()/editorlogic() in editor.cpp. In my opinion, they should probably be split off into their own separate file because editor.cpp is getting way too big, but this will do for now. --- desktop_version/src/Entity.cpp | 1 + desktop_version/src/Entity.h | 20 ++++++++++-- desktop_version/src/Game.cpp | 19 ++++++------ desktop_version/src/Game.h | 2 ++ desktop_version/src/Graphics.cpp | 7 +++-- desktop_version/src/Graphics.h | 2 ++ desktop_version/src/KeyPoll.cpp | 1 + desktop_version/src/KeyPoll.h | 2 ++ desktop_version/src/Map.cpp | 1 + desktop_version/src/Map.h | 2 ++ desktop_version/src/Music.cpp | 2 ++ desktop_version/src/Music.h | 2 ++ desktop_version/src/Script.cpp | 1 + desktop_version/src/Script.h | 2 ++ desktop_version/src/UtilityClass.cpp | 1 + desktop_version/src/UtilityClass.h | 2 ++ desktop_version/src/editor.cpp | 46 ++++++++++++++++------------ desktop_version/src/editor.h | 2 ++ 18 files changed, 81 insertions(+), 34 deletions(-) diff --git a/desktop_version/src/Entity.cpp b/desktop_version/src/Entity.cpp index efa96404..027a953d 100644 --- a/desktop_version/src/Entity.cpp +++ b/desktop_version/src/Entity.cpp @@ -1,3 +1,4 @@ +#define OBJ_DEFINITION #include "Entity.h" #include diff --git a/desktop_version/src/Entity.h b/desktop_version/src/Entity.h index b2a7ec08..f8bc8e88 100644 --- a/desktop_version/src/Entity.h +++ b/desktop_version/src/Entity.h @@ -10,8 +10,22 @@ #include "BlockV.h" #include "Game.h" -#define removeentity_iter(index) { if (obj.removeentity(index)) index--; } -#define removeblock_iter(index) { obj.removeblock(index); index--; } +#define removeentity_iter(index) \ + do \ + { \ + extern entityclass obj; \ + if (obj.removeentity(index)) \ + { \ + index--; \ + } \ + } while (false) +#define removeblock_iter(index) \ + do \ + { \ + extern entityclass obj; \ + obj.removeblock(index); \ + index--; \ + } while (false) enum { @@ -193,6 +207,8 @@ public: bool customcrewmoods[Game::numcrew]; }; +#ifndef OBJ_DEFINITION extern entityclass obj; +#endif #endif /* ENTITY_H */ diff --git a/desktop_version/src/Game.cpp b/desktop_version/src/Game.cpp index cabf239a..ec31f8cb 100644 --- a/desktop_version/src/Game.cpp +++ b/desktop_version/src/Game.cpp @@ -1,3 +1,4 @@ +#define GAME_DEFINITION #include "Game.h" #include @@ -6619,8 +6620,8 @@ void Game::createmenu( enum Menu::MenuName t, bool samemenu/*= false*/ ) option("animated backgrounds"); option("screen effects"); option("text outline"); - option("invincibility", !ingame_titlemode || (!game.insecretlab && !game.intimetrial && !game.nodeathmode)); - option("slowdown", !ingame_titlemode || (!game.insecretlab && !game.intimetrial && !game.nodeathmode)); + option("invincibility", !ingame_titlemode || (!insecretlab && !intimetrial && !nodeathmode)); + option("slowdown", !ingame_titlemode || (!insecretlab && !intimetrial && !nodeathmode)); option("return"); menuyoff = 0; break; @@ -7196,13 +7197,13 @@ void Game::returntolab() #if !defined(NO_CUSTOM_LEVELS) void Game::returntoeditor() { - game.gamestate = EDITORMODE; + gamestate = EDITORMODE; graphics.textbox.clear(); - game.hascontrol = true; - game.advancetext = false; - game.completestop = false; - game.state = 0; + hascontrol = true; + advancetext = false; + completestop = false; + state = 0; graphics.showcutscenebars = false; graphics.fademode = 0; @@ -7234,13 +7235,13 @@ void Game::returntopausemenu() map.kludge_to_bg(); map.tdrawback = true; graphics.backgrounddrawn = false; - game.mapheld = true; + mapheld = true; graphics.flipmode = graphics.setflipmode; if (!map.custommode && !graphics.flipmode) { obj.flags[73] = true; } - game.shouldreturntopausemenu = true; + shouldreturntopausemenu = true; } void Game::unlockAchievement(const char *name) { diff --git a/desktop_version/src/Game.h b/desktop_version/src/Game.h index d0b50ef7..4ad44a14 100644 --- a/desktop_version/src/Game.h +++ b/desktop_version/src/Game.h @@ -425,6 +425,8 @@ public: bool disablepause; }; +#ifndef GAME_DEFINITION extern Game game; +#endif #endif /* GAME_H */ diff --git a/desktop_version/src/Graphics.cpp b/desktop_version/src/Graphics.cpp index 585c4847..f6d9000d 100644 --- a/desktop_version/src/Graphics.cpp +++ b/desktop_version/src/Graphics.cpp @@ -1,3 +1,4 @@ +#define GRAPHICS_DEFINITION #include "Graphics.h" #include @@ -2991,16 +2992,16 @@ void Graphics::renderwithscreeneffects() { if (game.flashlight > 0 && !game.noflashingmode) { - graphics.flashlight(); + flashlight(); } if (game.screenshake > 0 && !game.noflashingmode) { - graphics.screenshake(); + screenshake(); } else { - graphics.render(); + render(); } } diff --git a/desktop_version/src/Graphics.h b/desktop_version/src/Graphics.h index 109471be..d7a46366 100644 --- a/desktop_version/src/Graphics.h +++ b/desktop_version/src/Graphics.h @@ -317,6 +317,8 @@ public: Uint32 crewcolourreal(int t); }; +#ifndef GRAPHICS_DEFINITION extern Graphics graphics; +#endif #endif /* GRAPHICS_H */ diff --git a/desktop_version/src/KeyPoll.cpp b/desktop_version/src/KeyPoll.cpp index f59170bd..64fa14ed 100644 --- a/desktop_version/src/KeyPoll.cpp +++ b/desktop_version/src/KeyPoll.cpp @@ -1,3 +1,4 @@ +#define KEY_DEFINITION #include "KeyPoll.h" #include diff --git a/desktop_version/src/KeyPoll.h b/desktop_version/src/KeyPoll.h index 82eefe6c..6c8f3264 100644 --- a/desktop_version/src/KeyPoll.h +++ b/desktop_version/src/KeyPoll.h @@ -81,6 +81,8 @@ private: Uint32 wasFullscreen; }; +#ifndef KEY_DEFINITION extern KeyPoll key; +#endif #endif /* KEYPOLL_H */ diff --git a/desktop_version/src/Map.cpp b/desktop_version/src/Map.cpp index ca4c9bb5..007621a5 100644 --- a/desktop_version/src/Map.cpp +++ b/desktop_version/src/Map.cpp @@ -1,3 +1,4 @@ +#define MAP_DEFINITION #include "Map.h" #include "editor.h" diff --git a/desktop_version/src/Map.h b/desktop_version/src/Map.h index b777b311..53af2482 100644 --- a/desktop_version/src/Map.h +++ b/desktop_version/src/Map.h @@ -190,6 +190,8 @@ public: } }; +#ifndef MAP_DEFINITION extern mapclass map; +#endif #endif /* MAPGAME_H */ diff --git a/desktop_version/src/Music.cpp b/desktop_version/src/Music.cpp index 87435c21..543462d4 100644 --- a/desktop_version/src/Music.cpp +++ b/desktop_version/src/Music.cpp @@ -1,3 +1,4 @@ +#define MUSIC_DEFINITION #include "Music.h" #include @@ -152,6 +153,7 @@ void musicclass::init() void songend() { + extern musicclass music; music.songEnd = SDL_GetPerformanceCounter(); music.currentsong = -1; } diff --git a/desktop_version/src/Music.h b/desktop_version/src/Music.h index 764a6dfe..62b48f89 100644 --- a/desktop_version/src/Music.h +++ b/desktop_version/src/Music.h @@ -58,6 +58,8 @@ public: Uint64 songEnd; }; +#ifndef MUSIC_DEFINITION extern musicclass music; +#endif #endif /* MUSIC_H */ diff --git a/desktop_version/src/Script.cpp b/desktop_version/src/Script.cpp index d1d6b3e5..c9bbaab2 100644 --- a/desktop_version/src/Script.cpp +++ b/desktop_version/src/Script.cpp @@ -1,3 +1,4 @@ +#define SCRIPT_DEFINITION #include "Script.h" #include "editor.h" diff --git a/desktop_version/src/Script.h b/desktop_version/src/Script.h index e4d07b13..2d915d09 100644 --- a/desktop_version/src/Script.h +++ b/desktop_version/src/Script.h @@ -68,6 +68,8 @@ public: std::vector