Commit 13780a40 authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum
Browse files

[CHANGE] - made graphics engine a singleton

[ADD] - Resource management via WGEResourceManager
[CHANGE] - made getAppPath/getShaderPath static
parent 796ac601
......@@ -36,9 +36,14 @@
#include "../common/WColor.h"
#include "../common/WLogger.h"
#include "../kernel/WKernel.h"
#include "WGEViewer.h"
#include "WGraphicsEngine.h"
#include "exceptions/WGEInitFailed.h"
#include "WGEResourceManager.h"
// graphics engine instance as singleton
boost::shared_ptr< WGraphicsEngine > WGraphicsEngine::m_instance = boost::shared_ptr< WGraphicsEngine >();
WGraphicsEngine::WGraphicsEngine():
WThreadedRunner()
......@@ -63,6 +68,9 @@ WGraphicsEngine::WGraphicsEngine():
// ThreadPerCamera
// AutomaticSelection
m_Viewer->setThreadingModel( osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext );
// init resource manager ( it is a singleton and gets created during first "getResourceManager" request.
WGEResourceManager::getResourceManager();
}
WGraphicsEngine::~WGraphicsEngine()
......@@ -71,6 +79,17 @@ WGraphicsEngine::~WGraphicsEngine()
WLogger::getLogger()->addLogMessage( "Shutting down Graphics Engine", "GE", LL_INFO );
}
boost::shared_ptr< WGraphicsEngine > WGraphicsEngine::getGraphicsEngine()
{
if ( !m_instance )
{
m_instance = boost::shared_ptr< WGraphicsEngine >( new WGraphicsEngine() );
}
return m_instance;
}
osg::ref_ptr<WGEScene> WGraphicsEngine::getScene()
{
return m_rootNode;
......@@ -148,3 +167,4 @@ osg::Vec4 wge::osgColor( const WColor& color )
{
return osg::Vec4( color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha() );
}
......@@ -110,11 +110,27 @@ public:
void closeViewer( const std::string name );
/**
* Searchs for a viewer with a given name and returns it, if found.
* Searches for a viewer with a given name and returns it, if found.
* \param name The name of the viewer
*/
boost::shared_ptr<WGEViewer> getViewerByName( std::string name );
/**
* Returns the currently selected default font usable in osgText nodes.
*
* \return Filename of the default font.
*
* \note this also contains the absolute path.
*/
std::string getDefaultFont();
/**
* Returns instance of the graphics engine. If it does not exists, it will be created.
*
* \return the running graphics engine instance.
*/
static boost::shared_ptr< WGraphicsEngine > getGraphicsEngine();
protected:
/**
......@@ -153,6 +169,11 @@ protected:
osg::ref_ptr<osgViewer::CompositeViewer> m_Viewer;
private:
/**
* Singleton instance of WGraphicsEngine.
*/
static boost::shared_ptr< WGraphicsEngine > m_instance;
};
/**
......
......@@ -143,7 +143,7 @@ int WQt4Gui::run()
QApplication appl( argc, argv, true );
// startup graphics engine
m_ge = boost::shared_ptr< WGraphicsEngine >( new WGraphicsEngine() );
m_ge = WGraphicsEngine::getGraphicsEngine();
// and startup kernel
m_kernel = boost::shared_ptr< WKernel >( new WKernel( m_ge, shared_from_this() ) );
......
......@@ -22,6 +22,10 @@
//
//---------------------------------------------------------------------------
#ifdef __linux__
#include <unistd.h> // used for getcwd (to get current directory)
#endif
#if defined(__APPLE__)
#include <mach-o/dyld.h>
#endif
......@@ -49,6 +53,16 @@
*/
WKernel* kernel = NULL;
/**
* Define shader path.
*/
std::string WKernel::m_shaderPath = std::string();
/**
* Define app path.
*/
std::string WKernel::m_AppPath = std::string();
WKernel::WKernel( boost::shared_ptr< WGraphicsEngine > ge, boost::shared_ptr< WGUI > gui ):
WThreadedRunner()
{
......@@ -73,7 +87,7 @@ WKernel::~WKernel()
void WKernel::init()
{
// initialize
// initialize
findAppPath();
// get module factory
......@@ -84,7 +98,7 @@ void WKernel::init()
"Root module container in Kernel." ) );
// initialize graphics engine, or, at least set some stuff
m_graphicsEngine->setShaderPath( m_shaderPath );
m_graphicsEngine->setShaderPath( WKernel::m_shaderPath );
// initialize Datahandler
m_dataHandler = boost::shared_ptr< WDataHandler >( new WDataHandler() );
......@@ -142,11 +156,22 @@ void WKernel::threadMain()
WLogger::getLogger()->addLogMessage( "Shutting down Kernel", "Kernel", LL_INFO );
}
bool WKernel::findAppPath()
void WKernel::findAppPath()
{
// only get the path if not already done
if ( m_AppPath != "" )
{
return;
}
// FIXME (schurade)
// this should work on linux, have to implement it for windows and mac later
#ifdef __linux__
// This might be the better alternative to the below code but it does not print the path to the executable, but to the current
// working directory, which in the unix world is the better choice as path
// char* sappPath = get_current_dir_name();
int length;
char appPath[255];
......@@ -156,12 +181,10 @@ bool WKernel::findAppPath()
if ( length < 0 )
{
WLogger::getLogger()->addLogMessage( "Error resolving symlink /proc/self/exe.", "Kernel", LL_ERROR );
return false;
}
if ( length >= 255 )
{
WLogger::getLogger()->addLogMessage( "Path too long. Truncated.", "Kernel", LL_ERROR );
return false;
}
// the string this readlink() function returns is appended with a '@'.
......@@ -180,7 +203,6 @@ bool WKernel::findAppPath()
std::string shaderPath( appPath );
m_shaderPath = shaderPath + "shaders/";
return true;
#elif defined( __APPLE__ )
char path[1024];
uint32_t size = sizeof( path );
......@@ -200,8 +222,6 @@ bool WKernel::findAppPath()
std::string shaderPath( path );
m_shaderPath = shaderPath + "shaders/";
return true;
}
else
{
......@@ -211,9 +231,12 @@ bool WKernel::findAppPath()
}
#else
#pragma message( "Error: findAppPath not implemented for this platform" )
// for windows, use something like this:
// DWORD GetModuleFileName( NULL, HMODULE hModule, // handle to module
// LPTSTR lpFilename, // path buffer
// DWORD nSize // size of buffer
// );
#endif
return false;
}
const WBoolFlag& WKernel::isFinishRequested() const
......@@ -243,12 +266,14 @@ boost::shared_ptr< WDataHandler > WKernel::getDataHandler() const
return m_dataHandler;
}
std::string WKernel::getAppPath() const
std::string WKernel::getAppPath()
{
return m_AppPath;
findAppPath();
return WKernel::m_AppPath;
}
std::string WKernel::getShaderPath() const
std::string WKernel::getShaderPath()
{
return m_shaderPath;
findAppPath();
return WKernel::m_shaderPath;
}
......@@ -140,12 +140,12 @@ public:
/**
* get for application path
*/
std::string getAppPath() const;
static std::string getAppPath();
/**
* getter for shader path
*/
std::string getShaderPath() const;
static std::string getShaderPath();
protected:
......@@ -193,17 +193,17 @@ private:
/**
* Determine and store the application path
*/
bool findAppPath();
static void findAppPath();
/**
* the location of the openwalnut executable
*/
std::string m_AppPath;
static std::string m_AppPath;
/**
* the location of the openwalnut executable
*/
std::string m_shaderPath;
static std::string m_shaderPath;
};
#endif // WKERNEL_H
......
......@@ -28,6 +28,7 @@
#include <osg/Projection>
#include "../../kernel/WKernel.h"
#include "../../graphicsEngine/WGEResourceManager.h"
#include "WMHud.h"
......@@ -166,9 +167,8 @@ void WMHud::init()
HUDGeode->addDrawable( m_osgPickText );
std::string fontPath = WKernel::getRunningKernel()->getAppPath() + "/fonts/";
m_osgPickText->setCharacterSize( 14 );
m_osgPickText->setFont( fontPath + "LiberationSans-Regular.ttf" );
m_osgPickText->setFont( WGEResourceManager::getResourceManager()->getDefaultFont() );
m_osgPickText->setText( "nothing picked" );
m_osgPickText->setAxisAlignment( osgText::Text::SCREEN );
m_osgPickText->setPosition( osg::Vec3( 600, 80, -1.5 ) );
......
......@@ -112,6 +112,7 @@ void WMMarchingCubes::moduleMain()
{
// ok, the output has not yet sent data
// NOTE: see comment at the end of this while loop for m_moduleState
debugLog() << "Waiting for data ...";
m_moduleState.wait();
continue;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment