From 4236927a19b2037c05af8193abd03aeaf5ba2c23 Mon Sep 17 00:00:00 2001
From: Oliver Behnke <oliver.behnke@aei.mpg.de>
Date: Wed, 4 Mar 2020 11:18:43 +0100
Subject: [PATCH] Fix up initial window and OpenGL context instantiation (incl.
 fallback logic)

---
 src/framework/WindowManager.cpp | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/src/framework/WindowManager.cpp b/src/framework/WindowManager.cpp
index e300e57..b6b0736 100644
--- a/src/framework/WindowManager.cpp
+++ b/src/framework/WindowManager.cpp
@@ -123,7 +123,7 @@ bool WindowManager::initialize(const int width, const int height, const int fram
 	m_CurrentWidth = m_WindowedWidth;
 	m_CurrentHeight = m_WindowedHeight;
 
-	// finally, get window
+	// create window
 	m_Window = SDL_CreateWindow(
 							"Einstein@Home",
 							SDL_WINDOWPOS_CENTERED,
@@ -135,6 +135,13 @@ bool WindowManager::initialize(const int width, const int height, const int fram
 	if (m_Window == NULL) {
 		cerr << "Could not acquire rendering surface (" << SDL_GetError() << "): will try fallback..." << endl;
 	}
+	else {
+		// create OpenGL context
+		m_GLContext = SDL_GL_CreateContext(m_Window);
+		if (m_GLContext == NULL) {
+			cerr << "Could not acquire OpenGL context (" << SDL_GetError() << "): will try fallback..." << endl;
+		}
+	}
 
 	// check if we got acceleration
 	int accelerated = 0;
@@ -142,7 +149,8 @@ bool WindowManager::initialize(const int width, const int height, const int fram
 		cerr << "Could not ensure accelerated rendering surface. Assuming no acceleration..." << endl;
 	}
 
-	if (m_Window == NULL || !accelerated) {
+	// handle fallback
+	if (m_Window == NULL || m_GLContext == NULL || !accelerated) {
 		cerr << "Disabling high quality features..." << endl;
 
 		// disable features that demand acceleration
@@ -153,6 +161,7 @@ bool WindowManager::initialize(const int width, const int height, const int fram
 		// note, requires to extend starsphere's constructor (uses its own BOINCClientAdapter!)
 
 		// reset window
+		if (m_Window) SDL_DestroyWindow(m_Window);
 		m_Window = SDL_CreateWindow(
 								"Einstein@Home",
 								SDL_WINDOWPOS_CENTERED,
@@ -165,13 +174,14 @@ bool WindowManager::initialize(const int width, const int height, const int fram
 			cerr << "Could not acquire window (" << SDL_GetError() << "): giving up!" << endl;
 			return false;
 		}
-	}
-
-	// finally create OpenGL context
-	m_GLContext = SDL_GL_CreateContext(m_Window);
-	if (m_GLContext == NULL) {
-		cerr << "Could not acquire OpenGL context (" << SDL_GetError() << "): giving up!" << endl;
-		return false;
+		else {
+			// create OpenGL context
+			m_GLContext = SDL_GL_CreateContext(m_Window);
+			if (m_GLContext == NULL) {
+				cerr << "Could not acquire OpenGL context (" << SDL_GetError() << "): giving up!" << endl;
+				SDL_DestroyWindow(m_Window);
+			}
+		}
 	}
 
 	return true;
-- 
GitLab