From c52dc124409a5b00d9e7b5beaa4f4b361f2a9fd6 Mon Sep 17 00:00:00 2001
From: Oliver Bock <oliver.bock@aei.mpg.de>
Date: Thu, 8 May 2008 10:48:59 +0200
Subject: [PATCH] Fixing windoze window issue (move/resize)

* Windoze "resets" the OpenGL context when SetVideoMode is called - ergo, we need to reinitialize the context
* Added a switch (recycle) to initialize() to distinguish between one-off and recurring actions
---
 AbstractGraphicsEngine.h |   3 +-
 Starsphere.cpp           | 102 ++++++++++++++++++++-------------------
 Starsphere.h             |   5 +-
 StarsphereS5R3.cpp       |  25 ++++++----
 StarsphereS5R3.h         |   3 +-
 WindowManager.cpp        |   3 +-
 6 files changed, 77 insertions(+), 64 deletions(-)

diff --git a/AbstractGraphicsEngine.h b/AbstractGraphicsEngine.h
index 2f033e1..9f5c8d4 100644
--- a/AbstractGraphicsEngine.h
+++ b/AbstractGraphicsEngine.h
@@ -28,8 +28,9 @@ public:
 	 * \param width The current width of the display surface
 	 * \param height The current height of the display surface
 	 * \param font A pointer to a Resource object containing TTF font faces for text rendering 
+	 * \param recycle This flag indicates whether we initialize (FALSE) or reinitialize (TRUE) the context
 	 */
-	virtual void initialize(const int width, const int height, const Resource *font) = 0;
+	virtual void initialize(const int width, const int height, const Resource *font, const bool recycle = false) = 0;
 	
 	/**
 	 * \brief This method is called when the windowing system encounters a window resize event
diff --git a/Starsphere.cpp b/Starsphere.cpp
index 650dbf3..3e344cd 100644
--- a/Starsphere.cpp
+++ b/Starsphere.cpp
@@ -542,45 +542,63 @@ void Starsphere::resize(const int width, const int height)
 /**
  *  What to do when graphics are "initialized".
  */
-void Starsphere::initialize(const int width, const int height, const Resource *font)
+void Starsphere::initialize(const int width, const int height, const Resource *font, const bool recycle)
 {
-	// Initialize the BOINC client adapter
-	m_BoincAdapter.initialize("EinsteinHS");
+	// check whether we initialize the first time or have to recycle (required for windoze)
+	if(!recycle) {
+		
+		// Initialize the BOINC client adapter
+		m_BoincAdapter.initialize("EinsteinHS");
+		
+		// create large font instances using font resource (base address + size)
+		m_FontLogo1 = new OGLFT::TranslucentTexture(&font->data()->at(0), font->data()->size(), 24, 72 );
+		if ( m_FontLogo1 == 0 || !m_FontLogo1->isValid() ) {
+		     cerr << "Could not construct logo1 font face from in memory resource!" << endl;
+		     return;
+		}
+		m_FontLogo1->setForegroundColor(1.0, 1.0, 0.0, 1.0);
+		
+		// create medium font instances using font resource (base address + size)
+		m_FontLogo2 = new OGLFT::TranslucentTexture(&font->data()->at(0), font->data()->size(), 13, 78 );	
+		if ( m_FontLogo2 == 0 || !m_FontLogo2->isValid() ) {
+		     cerr << "Could not construct logo2 font face from in memory resource!" << endl;
+		     return;
+		}
+		m_FontLogo2->setForegroundColor(0.75, 0.75, 0.75, 1.0);
+		
+		// create medium font instances using font resource (base address + size)
+		m_FontHeader = new OGLFT::TranslucentTexture(&font->data()->at(0), font->data()->size(), 13, 78 );	
+		if ( m_FontHeader == 0 || !m_FontHeader->isValid() ) {
+		     cerr << "Could not construct header font face from in memory resource!" << endl;
+		     return;
+		}
+		m_FontHeader->setForegroundColor(1.0, 1.0, 0.0, 1.0);
+			
+		// create small font instances using font resource (base address + size)
+		m_FontText = new OGLFT::TranslucentTexture(&font->data()->at(0), font->data()->size(), 11, 72 );	
+		if ( m_FontText == 0 || !m_FontText->isValid() ) {
+		     cerr << "Could not construct text font face from in memory resource!" << endl;
+		     return;
+		}
+		m_FontText->setForegroundColor(0.75, 0.75, 0.75, 1.0);
+		
+		// inital HUD offset setup
+		m_XStartPosLeft = 5;
+		m_YOffsetLarge = 18;
+		
+		setFeature(STARS, true);
+		setFeature(CONSTELLATIONS, true);
+		setFeature(PULSARS, true);
+		setFeature(OBSERVATORIES, true);
+		setFeature(SNRS, true);
+		setFeature(GLOBE, true);
+		setFeature(SEARCHINFO, true);
+		setFeature(LOGO, true);
+		setFeature(MARKER, true);
+	}
 	
 	// setup initial dimensions
 	resize(width, height);
-	
-	// create large font instances using font resource (base address + size)
-	m_FontLogo1 = new OGLFT::TranslucentTexture(&font->data()->at(0), font->data()->size(), 24, 72 );
-	if ( m_FontLogo1 == 0 || !m_FontLogo1->isValid() ) {
-	     cerr << "Could not construct logo1 font face from in memory resource!" << endl;
-	     return;
-	}
-	m_FontLogo1->setForegroundColor(1.0, 1.0, 0.0, 1.0);
-	
-	// create medium font instances using font resource (base address + size)
-	m_FontLogo2 = new OGLFT::TranslucentTexture(&font->data()->at(0), font->data()->size(), 13, 78 );	
-	if ( m_FontLogo2 == 0 || !m_FontLogo2->isValid() ) {
-	     cerr << "Could not construct logo2 font face from in memory resource!" << endl;
-	     return;
-	}
-	m_FontLogo2->setForegroundColor(0.75, 0.75, 0.75, 1.0);
-	
-	// create medium font instances using font resource (base address + size)
-	m_FontHeader = new OGLFT::TranslucentTexture(&font->data()->at(0), font->data()->size(), 13, 78 );	
-	if ( m_FontHeader == 0 || !m_FontHeader->isValid() ) {
-	     cerr << "Could not construct header font face from in memory resource!" << endl;
-	     return;
-	}
-	m_FontHeader->setForegroundColor(1.0, 1.0, 0.0, 1.0);
-		
-	// create small font instances using font resource (base address + size)
-	m_FontText = new OGLFT::TranslucentTexture(&font->data()->at(0), font->data()->size(), 11, 72 );	
-	if ( m_FontText == 0 || !m_FontText->isValid() ) {
-	     cerr << "Could not construct text font face from in memory resource!" << endl;
-	     return;
-	}
-	m_FontText->setForegroundColor(0.75, 0.75, 0.75, 1.0);
 
 	// more font setup and optimizations
 	glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
@@ -588,10 +606,6 @@ void Starsphere::initialize(const int width, const int height, const Resource *f
 	glEnable( GL_RASTER_POSITION_UNCLIPPED_IBM );
 #endif
 	
-	// inital HUD offset setup
-	m_XStartPosLeft = 5;
-	m_YOffsetLarge = 18;
-		
 	// Drawing setup:
 	glClearColor(0.0, 0.0, 0.0, 0.0); // background is black
 	glEnable(GL_CULL_FACE);
@@ -632,16 +646,6 @@ void Starsphere::initialize(const int width, const int height, const Resource *f
 	make_globe();
 	make_obs();
 
-	setFeature(STARS, true);
-	setFeature(CONSTELLATIONS, true);
-	setFeature(PULSARS, true);
-	setFeature(OBSERVATORIES, true);
-	setFeature(SNRS, true);
-	setFeature(GLOBE, true);
-	setFeature(SEARCHINFO, true);
-	setFeature(LOGO, true);
-	setFeature(MARKER, true);
-
 	glDisable(GL_CLIP_PLANE0);
 	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 	glFlush();
diff --git a/Starsphere.h b/Starsphere.h
index 5eaa0c5..f317087 100644
--- a/Starsphere.h
+++ b/Starsphere.h
@@ -70,9 +70,10 @@ public:
 	 * 
 	 * \param width The current width of the display surface
 	 * \param height The current height of the display surface
-	 * \param font A pointer to a Resource object containing TTF font faces for text rendering 
+	 * \param font A pointer to a Resource object containing TTF font faces for text rendering
+	 * \param recycle This flag indicates whether we initialize (FALSE) or reinitialize (TRUE) the context
 	 */
-	virtual void initialize(const int width, const int height, const Resource *font);
+	virtual void initialize(const int width, const int height, const Resource *font, const bool recycle = false);
 	
 	/**
 	 * \brief This method is called when the windowing system encounters a window resize event
diff --git a/StarsphereS5R3.cpp b/StarsphereS5R3.cpp
index 3d21a5d..762dba5 100644
--- a/StarsphereS5R3.cpp
+++ b/StarsphereS5R3.cpp
@@ -8,18 +8,22 @@ StarsphereS5R3::~StarsphereS5R3()
 {
 }
 
-void StarsphereS5R3::initialize(const int width, const int height, const Resource *font)
+void StarsphereS5R3::initialize(const int width, const int height, const Resource *font, const bool recycle)
 {
-	Starsphere::initialize(width, height, font);
+	Starsphere::initialize(width, height, font, recycle);
 	
-	// adjust HUD config
-	m_YOffsetMedium = 15;
-	m_XStartPosRight = width - 125;
-	m_YStartPosBottom = 70;	
-	m_Y1StartPosBottom = m_YStartPosBottom  - m_YOffsetMedium;
-	m_Y2StartPosBottom = m_Y1StartPosBottom - m_YOffsetMedium;
-	m_Y3StartPosBottom = m_Y2StartPosBottom - m_YOffsetMedium;
-	m_Y4StartPosBottom = m_Y3StartPosBottom - m_YOffsetMedium;
+	// check whether we initialize the first time or have to recycle (required for windoze)
+	if(!recycle) {
+			
+		// adjust HUD config
+		m_YOffsetMedium = 15;
+		m_XStartPosRight = width - 125;
+		m_YStartPosBottom = 70;	
+		m_Y1StartPosBottom = m_YStartPosBottom  - m_YOffsetMedium;
+		m_Y2StartPosBottom = m_Y1StartPosBottom - m_YOffsetMedium;
+		m_Y3StartPosBottom = m_Y2StartPosBottom - m_YOffsetMedium;
+		m_Y4StartPosBottom = m_Y3StartPosBottom - m_YOffsetMedium;
+	}
 }
 
 void StarsphereS5R3::resize(const int width, const int height)
@@ -68,6 +72,7 @@ void StarsphereS5R3::refreshBOINCInformation()
 	m_WUPercentDone = buffer.str();
 	buffer.str("");
 	
+	/// \todo Show accumulated time (init_data time + shmem time)
 	const double cputime = m_EinsteinAdapter.wuCPUTime();
 	const int hrs =  cputime / 3600;
 	const int min = (cputime - hrs*3600) / 60;
diff --git a/StarsphereS5R3.h b/StarsphereS5R3.h
index 0bd5a24..d10388c 100644
--- a/StarsphereS5R3.h
+++ b/StarsphereS5R3.h
@@ -40,8 +40,9 @@ public:
 	 * \param width The current width of the display surface
 	 * \param height The current height of the display surface
 	 * \param font A pointer to a Resource object containing TTF font faces for text rendering 
+	 * \param recycle This flag indicates whether we initialize (FALSE) or reinitialize (TRUE) the context
 	 */
-	void initialize(const int width, const int height, const Resource *font);
+	virtual void initialize(const int width, const int height, const Resource *font, const bool recycle = false);
 	
 	/**
 	 * \brief This method is called when the windowing system encounters a window resize event
diff --git a/WindowManager.cpp b/WindowManager.cpp
index 45a3a12..ede6175 100644
--- a/WindowManager.cpp
+++ b/WindowManager.cpp
@@ -208,7 +208,8 @@ void WindowManager::eventLoop()
 									m_VideoModeFlags);
 			
 			// notify our observers (currently exactly one)
-			eventObservers.front()->resize(m_CurrentWidth, m_CurrentHeight);
+			// (windoze needs to be reinitialized instead of just resized, oh well)
+			eventObservers.front()->initialize(m_CurrentWidth, m_CurrentHeight, 0, true);
 		}
 		else if (event.type == SDL_QUIT ||
 				(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) {
-- 
GitLab