From d3e6e4062c8b3691d132a50d3a5cf9d101c7e38e Mon Sep 17 00:00:00 2001
From: Oliver Bock <oliver.bock@aei.mpg.de>
Date: Fri, 4 Apr 2008 21:38:23 +0200
Subject: [PATCH] Fixed weird "HUD killing bug" (see
 29943e253fccba88122977deb1c233ffa2e8dcfe)

* For some reason main() must be structured as it is now
* Moving toggleFullscreen() or setWindowCaption() into initialize() causes the bug to reappear
* I don't yet understand the problem, let's hope that's not some strange race condition
---
 WindowManager.cpp | 14 +++-----------
 WindowManager.h   | 10 +++++-----
 main.C            | 36 ++++++++++++++++--------------------
 3 files changed, 24 insertions(+), 36 deletions(-)

diff --git a/WindowManager.cpp b/WindowManager.cpp
index 416426d..47ccd57 100644
--- a/WindowManager.cpp
+++ b/WindowManager.cpp
@@ -8,11 +8,8 @@ WindowManager::~WindowManager()
 {
 }
 
-bool WindowManager::initialize(const bool fullscreen, const int width, const int height)
+bool WindowManager::initialize(const int width, const int height)
 {
-	// be sure there's at least one observer!
-	assert(eventObservers.size() > 0);
-	
 	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
 		cerr << "Window system could not be initalized: " << SDL_GetError() << endl;
 		return false;
@@ -117,11 +114,6 @@ bool WindowManager::initialize(const bool fullscreen, const int width, const int
 							m_DesktopBitsPerPixel,
 							m_VideoModeFlags);
 	
-	// switch to fullscreen if requested
-	if(fullscreen && m_FullscreenModeAvailable) {
-		toggleFullscreen();
-	}
-	
 	if (m_DisplaySurface == NULL) {
 		cerr << "Could not acquire rendering surface: " << SDL_GetError() << endl;
 		return false;
@@ -323,12 +315,12 @@ int WindowManager::windowHeight() const
 	return m_CurrentHeight;
 }
 
-void WindowManager::setWindowCaption(const string caption)
+void WindowManager::setWindowCaption(const string caption) const
 {
 	SDL_WM_SetCaption(caption.c_str(), NULL);
 }
 
-void WindowManager::setWindowIcon(const string filename)
+void WindowManager::setWindowIcon(const string filename) const
 {
 	if (filename.length() > 0) {
 		SDL_WM_SetIcon(SDL_LoadBMP(filename.c_str()), NULL);
diff --git a/WindowManager.h b/WindowManager.h
index a64b362..af954b8 100644
--- a/WindowManager.h
+++ b/WindowManager.h
@@ -18,7 +18,7 @@ public:
 	WindowManager();
 	virtual ~WindowManager();
 	
-	bool initialize(const bool fullscreen, const int width = 1024, const int height = 768);
+	bool initialize(const int width = 1024, const int height = 768);
 	void registerEventObserver(AbstractGraphicsEngine *engine);
 	void unregisterEventObserver(AbstractGraphicsEngine *engine);
 	void eventLoop();
@@ -26,14 +26,14 @@ public:
 	int windowWidth() const;
 	int windowHeight() const;
 	
-	void setWindowCaption(const string caption);
-	void setWindowIcon(const string filename);
+	void setWindowCaption(const string caption) const;
+	void setWindowIcon(const string filename) const;
 	void toggleFullscreen();
 
 private:
     // FIXME: work around static, otherwise event conflict when more than one instance
-    static Uint32 timerCallbackRenderEvent( Uint32 interval, void *param );
-    static Uint32 timerCallbackBOINCUpdateEvent( Uint32 interval, void *param );
+    static Uint32 timerCallbackRenderEvent(Uint32 interval, void *param);
+    static Uint32 timerCallbackBOINCUpdateEvent(Uint32 interval, void *param);
     
     int m_DesktopWidth;
     int m_DesktopHeight;
diff --git a/main.C b/main.C
index 5897a72..18da435 100644
--- a/main.C
+++ b/main.C
@@ -38,44 +38,40 @@ using namespace std;
 
 int main(int argc, char **argv)
 {
-    Starsphere graphics;
-    WindowManager window;
-    
-    // register starsphere as event observer
-    window.registerEventObserver(&graphics);
-    
-#ifdef NDEBUG
-    const bool fullscreen = true;
-#else
-    const bool fullscreen = false;
-#endif
+	// prepare main objects
+	WindowManager window;
+	ResourceFactory factory;
+	Starsphere graphics; 
     
     // initialize window manager 
-    if(!window.initialize(fullscreen)) {
-    	window.unregisterEventObserver(&graphics);
+    if(!window.initialize()) {
         exit(1);
     }
     
-    window.setWindowCaption("Einstein@Home");
-	
-	// prepare resource factory
-	ResourceFactory factory;
-	
 	// create font resource instance
 	const Resource *fontResource = factory.createInstance("FontSansSerif");
 	
 	if(fontResource == NULL) {
 		cerr << "Font resource could not be loaded!" << endl;
-		window.unregisterEventObserver(&graphics);
 		exit(1);
 	}
 	
 	if(fontResource->data()->size() == 0) {
 		cerr << "Font resource could not be loaded!" << endl;
-		window.unregisterEventObserver(&graphics);
+		delete fontResource;
 		exit(1);
 	}
+
+    window.setWindowCaption("Einstein@Home");
 	
+    // register starsphere as event observer
+    window.registerEventObserver(&graphics);
+
+#ifdef NDEBUG
+	// switch to fullscreen when in release mode
+	window.toggleFullscreen();
+#endif
+
 	// pepare rendering
 	graphics.initialize(window.windowWidth(), window.windowHeight(), fontResource);
 
-- 
GitLab