From 341cbce91bd967ef24aa3624c5cf815cc9d728bb Mon Sep 17 00:00:00 2001
From: Oliver Bock <oliver.bock@aei.mpg.de>
Date: Fri, 11 Apr 2008 16:35:04 +0200
Subject: [PATCH] Added new factory for graphics engines.

* Support for flexible requests of engine and specific science app support
* Could easily be passed as command line parameter for instance
---
 GraphicsEngineFactory.cpp | 27 +++++++++++++++++++++
 GraphicsEngineFactory.h   | 51 +++++++++++++++++++++++++++++++++++++++
 main.C                    | 25 ++++++++++++++-----
 3 files changed, 97 insertions(+), 6 deletions(-)
 create mode 100644 GraphicsEngineFactory.cpp
 create mode 100644 GraphicsEngineFactory.h

diff --git a/GraphicsEngineFactory.cpp b/GraphicsEngineFactory.cpp
new file mode 100644
index 0000000..42c1912
--- /dev/null
+++ b/GraphicsEngineFactory.cpp
@@ -0,0 +1,27 @@
+#include "GraphicsEngineFactory.h"
+
+GraphicsEngineFactory::~GraphicsEngineFactory()
+{
+}
+GraphicsEngineFactory::GraphicsEngineFactory()
+{
+}
+
+AbstractGraphicsEngine * GraphicsEngineFactory::createInstance(
+							GraphicsEngineFactory::Engines engine,
+							GraphicsEngineFactory::Applications application)
+{
+	switch(engine) {
+		case Starsphere:
+			switch(application) {
+				case EinsteinS5R3:
+					return new StarsphereS5R3();
+					break;
+				default:
+					return NULL;
+			}
+			break;
+		default:
+			return NULL;	
+	}
+}
diff --git a/GraphicsEngineFactory.h b/GraphicsEngineFactory.h
new file mode 100644
index 0000000..14c5fcb
--- /dev/null
+++ b/GraphicsEngineFactory.h
@@ -0,0 +1,51 @@
+#ifndef GRAPHICSENGINEFACTORY_H_
+#define GRAPHICSENGINEFACTORY_H_
+
+#include "AbstractGraphicsEngine.h"
+#include "StarsphereS5R3.h"
+
+/**
+ * \brief Factory to create graphics engines
+ *
+ * \author Oliver Bock\n
+ * Max-Planck-Institute for Gravitational Physics\n
+ * Hannover, Germany
+ */
+class GraphicsEngineFactory
+{
+public:
+	virtual ~GraphicsEngineFactory();
+	
+	/// Identifiers of supported graphics engines
+	enum Engines {
+		Starsphere = 1
+	};
+	
+	/// Identifiers of supported science applications
+	enum Applications {
+		EinsteinS5R3 = 53
+	};
+	
+	/**
+	 * \brief Instaniates a new graphics engine
+	 * 
+	 * Use this method to create a new grahics engine instance. However, please make
+	 * that you use only sensible combinations of \c engine and \c application (you
+	 * should know them).
+	 * 
+	 * \param engine The identifier of the requested graphics engine
+	 * \param application The identifier of the requested science application support
+	 * 
+	 * \return The pointer to the new engine instance
+	 * 
+	 * \see Engines
+	 * \see Applications
+	 */
+	static AbstractGraphicsEngine * createInstance(Engines engine, Applications application);
+	
+private:
+	// Contructor (private since this a purely static factory)
+	GraphicsEngineFactory();
+};
+
+#endif /*GRAPHICSENGINEFACTORY_H_*/
diff --git a/main.C b/main.C
index 7c374fd..1798106 100644
--- a/main.C
+++ b/main.C
@@ -31,7 +31,8 @@
 
 #include "WindowManager.h"
 #include "ResourceFactory.h"
-#include "Starsphere.h"
+#include "AbstractGraphicsEngine.h"
+#include "GraphicsEngineFactory.h"
 
 using namespace std;
 
@@ -41,10 +42,19 @@ int main(int argc, char **argv)
 	// prepare main objects
 	WindowManager window;
 	ResourceFactory factory;
-	Starsphere graphics; 
+	AbstractGraphicsEngine *graphics = GraphicsEngineFactory::createInstance(
+											GraphicsEngineFactory::Starsphere,
+											GraphicsEngineFactory::EinsteinS5R3);
+	
+	if(!graphics) {
+		cerr << "Requested graphics engine could not be found/instantiated!" << endl;
+		exit(1);
+	}
     
     // initialize window manager 
     if(!window.initialize()) {
+    	cerr << "Window manager could not be initialized!" << endl;
+    	delete graphics;
         exit(1);
     }
     
@@ -53,11 +63,13 @@ int main(int argc, char **argv)
 	
 	if(fontResource == NULL) {
 		cerr << "Font resource could not be loaded!" << endl;
+		delete graphics;
 		exit(1);
 	}
 	
 	if(fontResource->data()->size() <= 0) {
 		cerr << "Font resource could not be loaded!" << endl;
+		delete graphics;
 		delete fontResource;
 		exit(1);
 	}
@@ -65,7 +77,7 @@ int main(int argc, char **argv)
     window.setWindowCaption("Einstein@Home");
 	
     // register starsphere as event observer
-    window.registerEventObserver(&graphics);
+    window.registerEventObserver(graphics);
 
 #ifdef NDEBUG
 	// switch to fullscreen when in release mode
@@ -73,14 +85,15 @@ int main(int argc, char **argv)
 #endif
 
 	// pepare rendering
-	graphics.initialize(window.windowWidth(), window.windowHeight(), fontResource);
-	graphics.refreshBOINCInformation();
+	graphics->initialize(window.windowWidth(), window.windowHeight(), fontResource);
+	graphics->refreshBOINCInformation();
 
 	// enter main event loop
 	window.eventLoop();
 	
 	// clean up end exit
-	window.unregisterEventObserver(&graphics);
+	window.unregisterEventObserver(graphics);
+	delete graphics;
 	delete fontResource;
 	
 	exit(0);
-- 
GitLab