From 58d4fd6c2b110a15e65bf4b6b900ad41a49a6b41 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 30 Aug 2008 16:32:55 +0100 Subject: [PATCH] Implement a super awesome hack to reload images on Windows --- src/Asteroid.cpp | 1 + src/LoadOnce.hpp | 18 ++++++++++++++++-- src/Main.cpp | 6 +++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Asteroid.cpp b/src/Asteroid.cpp index a2566c9..76d502c 100644 --- a/src/Asteroid.cpp +++ b/src/Asteroid.cpp @@ -24,6 +24,7 @@ Texture *Asteroid::surfTexture[Surface::NUM_SURF_TEX]; Asteroid::Asteroid() { LOAD_ONCE { + cout << "Loading asteroids..." << endl; surfTexture[0] = new Texture("images/dirt_surface2.png"); surfTexture[1] = new Texture("images/snow_surface2.png"); surfTexture[2] = new Texture("images/red_rock_surface2.png"); diff --git a/src/LoadOnce.hpp b/src/LoadOnce.hpp index 156bf17..eef9c96 100644 --- a/src/LoadOnce.hpp +++ b/src/LoadOnce.hpp @@ -18,8 +18,22 @@ #ifndef INC_LOADONCE_HPP #define INC_LOADONCE_HPP +/* + * The global variable _forceReload can be used to force all images + * to reload. This works around an SDL bug (?) on Windows where all + * texture information is lost after the resolution is changed. + * + * This is really a bit of a hack, but quite cool, eh? The variable + * _forceReload gets incremented each time we have to reload the + * images, and the static variable _hasLoaded in each constructor + * stores the value of _forceReload the last time the images were + * loaded -- we reload if the values differ. + */ + +extern int _forceReload; + #define LOAD_ONCE \ - static bool _hasLoaded = false; \ - if (!_hasLoaded && (_hasLoaded = true)) + static int _hasLoaded = 0; \ + if ((_forceReload > _hasLoaded) && (_hasLoaded = _forceReload)) #endif diff --git a/src/Main.cpp b/src/Main.cpp index 55d40a4..0f3e86f 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -44,6 +44,9 @@ static void DestroyScreens() delete options; } +// See LoadOnce.hpp +int _forceReload = 1; + /* * Recreate all the screens. After a resolution switch for * example. @@ -51,7 +54,8 @@ static void DestroyScreens() void RecreateScreens() { DestroyScreens(); - + + _forceReload++; menu = new MainMenu(); game = new Game(); scores = new HighScores(); -- 2.39.2