From 5a316d65e681b3ae97177b4a3aa0edb48d6d2446 Mon Sep 17 00:00:00 2001 From: Info Teddy Date: Tue, 28 Jan 2020 23:35:03 -0800 Subject: [PATCH] Allow using help/graphics/music/game/key/map/obj everywhere This commit makes `help`, `graphics`, `music`, `game`, `key`, `map`, and `obj` essentially static global objects that can be used everywhere. This is useful in case we ever need to add a new function in the future, so we don't have to bother with passing a new argument in which means we have to pass a new argument in to the function that calls that function which means having to pass a new argument into the function that calls THAT function, etc. which is a real headache when working on fan mods of the source code. Note that this changes NONE of the existing function signatures, it merely just makes those variables accessible everywhere in the same way `script` and `ed` are. Also note that some classes had to be initialized after the filesystem was initialized, but C++ would keep initializing them before the filesystem got initialized, because I *had* to put them at the top of `main.cpp`, or else they wouldn't be global variables. The only way to work around this was to use entityclass's initialization style (which I'm pretty sure entityclass of all things doesn't need to be initialized this way), where you actually initialize the class in an `init()` function, and so then you do `graphics.init()` after the filesystem initialization, AFTER doing `Graphics graphics` up at the top. I've had to do this for `graphics` (but only because its child GraphicsResources `grphx` needs to be initialized this way), `music`, and `game`. I don't think this will affect anything. Other than that, `help`, `key`, and `map` are still using the C++-intended method of having ClassName::ClassName() functions. --- desktop_version/src/Game.cpp | 4 ++-- desktop_version/src/Game.h | 4 +++- desktop_version/src/Graphics.cpp | 4 +++- desktop_version/src/Graphics.h | 4 +++- desktop_version/src/GraphicsResources.cpp | 2 +- desktop_version/src/GraphicsResources.h | 2 +- desktop_version/src/KeyPoll.h | 1 + desktop_version/src/Map.h | 2 ++ desktop_version/src/Music.cpp | 2 +- desktop_version/src/Music.h | 3 ++- desktop_version/src/UtilityClass.h | 2 ++ desktop_version/src/main.cpp | 25 ++++++++++++++++------- 12 files changed, 39 insertions(+), 16 deletions(-) diff --git a/desktop_version/src/Game.cpp b/desktop_version/src/Game.cpp index 22bdd210..c2762ab6 100644 --- a/desktop_version/src/Game.cpp +++ b/desktop_version/src/Game.cpp @@ -113,9 +113,9 @@ bool GetButtonFromString(const char *pText, SDL_GameControllerButton *button) } -Game::Game(void): - mutebutton(0) +void Game::init(void) { + mutebutton = 0; infocus = true; paused = false; muted = false; diff --git a/desktop_version/src/Game.h b/desktop_version/src/Game.h index 08ee639c..657d7582 100644 --- a/desktop_version/src/Game.h +++ b/desktop_version/src/Game.h @@ -17,7 +17,7 @@ class musicclass; class Game { public: - Game(void); + void init(void); ~Game(void); @@ -362,4 +362,6 @@ public: bool skipfakeload; }; +extern Game game; + #endif /* GAME_H */ diff --git a/desktop_version/src/Graphics.cpp b/desktop_version/src/Graphics.cpp index f40f0a33..bc5270c3 100644 --- a/desktop_version/src/Graphics.cpp +++ b/desktop_version/src/Graphics.cpp @@ -4,8 +4,10 @@ #include "Map.h" #include "Screen.h" -Graphics::Graphics() +void Graphics::init() { + grphx.init(); + flipmode = false; setRect(tiles_rect, 0,0,8,8); setRect(sprites_rect, 0,0,32,32); diff --git a/desktop_version/src/Graphics.h b/desktop_version/src/Graphics.h index a2b10d7c..2aa7c2ac 100644 --- a/desktop_version/src/Graphics.h +++ b/desktop_version/src/Graphics.h @@ -23,7 +23,7 @@ class map; class Graphics { public: - Graphics(); + void init(); ~Graphics(); GraphicsResources grphx; @@ -282,4 +282,6 @@ public: bool showmousecursor; }; +extern Graphics graphics; + #endif /* GRAPHICS_H */ diff --git a/desktop_version/src/GraphicsResources.cpp b/desktop_version/src/GraphicsResources.cpp index 379d1064..b7eb7c1b 100644 --- a/desktop_version/src/GraphicsResources.cpp +++ b/desktop_version/src/GraphicsResources.cpp @@ -80,7 +80,7 @@ SDL_Surface* LoadImage(const char *filename, bool noBlend = true, bool noAlpha = } } -GraphicsResources::GraphicsResources(void) +void GraphicsResources::init(void) { im_tiles = LoadImage("graphics/tiles.png"); im_tiles2 = LoadImage("graphics/tiles2.png"); diff --git a/desktop_version/src/GraphicsResources.h b/desktop_version/src/GraphicsResources.h index 67060c4c..16c694d7 100644 --- a/desktop_version/src/GraphicsResources.h +++ b/desktop_version/src/GraphicsResources.h @@ -6,7 +6,7 @@ class GraphicsResources { public: - GraphicsResources(void); + void init(void); ~GraphicsResources(void); SDL_Surface* im_tiles; diff --git a/desktop_version/src/KeyPoll.h b/desktop_version/src/KeyPoll.h index 2a21405c..56333461 100644 --- a/desktop_version/src/KeyPoll.h +++ b/desktop_version/src/KeyPoll.h @@ -81,5 +81,6 @@ private: Uint32 wasFullscreen; }; +extern KeyPoll key; #endif /* KEYPOLL_H */ diff --git a/desktop_version/src/Map.h b/desktop_version/src/Map.h index 604b296d..7287f188 100644 --- a/desktop_version/src/Map.h +++ b/desktop_version/src/Map.h @@ -179,4 +179,6 @@ public: int cursorstate, cursordelay; }; +extern mapclass map; + #endif /* MAPGAME_H */ diff --git a/desktop_version/src/Music.cpp b/desktop_version/src/Music.cpp index d162bf6e..04c6debf 100644 --- a/desktop_version/src/Music.cpp +++ b/desktop_version/src/Music.cpp @@ -3,7 +3,7 @@ #include "Music.h" #include "BinaryBlob.h" -musicclass::musicclass() +void musicclass::init() { soundTracks.push_back(SoundTrack( "sounds/jump.wav" )); soundTracks.push_back(SoundTrack( "sounds/jump2.wav" )); diff --git a/desktop_version/src/Music.h b/desktop_version/src/Music.h index dd081eae..9afffb2a 100644 --- a/desktop_version/src/Music.h +++ b/desktop_version/src/Music.h @@ -10,7 +10,7 @@ class musicclass { public: - musicclass(); + void init(); void play(int t); void loopmusic(); @@ -64,5 +64,6 @@ public: bool usingmmmmmm; }; +extern musicclass music; #endif /* MUSIC_H */ diff --git a/desktop_version/src/UtilityClass.h b/desktop_version/src/UtilityClass.h index 24e89885..600be5fc 100644 --- a/desktop_version/src/UtilityClass.h +++ b/desktop_version/src/UtilityClass.h @@ -43,4 +43,6 @@ public: std::vector splitseconds; }; +extern UtilityClass help; + #endif /* UTILITYCLASS_H */ diff --git a/desktop_version/src/main.cpp b/desktop_version/src/main.cpp index 3adf9ab3..4dba0fa4 100644 --- a/desktop_version/src/main.cpp +++ b/desktop_version/src/main.cpp @@ -34,6 +34,14 @@ scriptclass script; editorclass ed; +UtilityClass help; +Graphics graphics; +musicclass music; +Game game; +KeyPoll key; +mapclass map; +entityclass obj; + int main(int argc, char *argv[]) { if(!FILESYSTEM_init(argv[0])) @@ -90,16 +98,19 @@ int main(int argc, char *argv[]) - UtilityClass help; + //UtilityClass help; // Load Ini - Graphics graphics; + //Graphics graphics; + graphics.init(); - musicclass music; - Game game; + //musicclass music; + music.init(); + //Game game; + game.init(); game.infocus = true; graphics.MakeTileArray(); @@ -159,8 +170,8 @@ int main(int argc, char *argv[]) game.menustart = false; game.mainmenu = 0; - KeyPoll key; - mapclass map; + //KeyPoll key; + //mapclass map; map.ypos = (700-29) * 8; map.bypos = map.ypos / 2; @@ -217,7 +228,7 @@ int main(int argc, char *argv[]) if(game.bestrank[4]>=3) NETWORK_unlockAchievement("vvvvvvtimetrial_warp_fixed"); if(game.bestrank[5]>=3) NETWORK_unlockAchievement("vvvvvvtimetrial_final_fixed"); - entityclass obj; + //entityclass obj; obj.init(); //Quick hack to start in final level ---- //Might be useful to leave this commented in for testing