Commit d406908d authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum

[CHANGE] - extended path determination mechanism in OW, modules now have their...

[CHANGE] - extended path determination mechanism in OW, modules now have their own, local path, adopting CMake Files according to it
parent 74b9be3b
......@@ -12,7 +12,7 @@ INCLUDE( ${PROJECT_SOURCE_DIR}/../tools/CMakeModules/Utils.cmake )
SET( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin )
SET( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib )
SET( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib )
SET( SHARED_FILES_RELATIVE "../share/${CMAKE_PROJECT_NAME}" CACHE PATH "Relative path to bin/ denoting the destination for shaders and cfg files." ) # this path us the relative path to bin/ used for all shared files like shaders and config file
SET( SHARED_FILES_RELATIVE "../share/${CMAKE_PROJECT_NAME}" CACHE INTERNAL "Relative path to bin/ denoting the destination for shaders and cfg files." ) # this path us the relative path to bin/ used for all shared files like shaders and config file
# CMAKE automatism to select static vs. shared building:
#-------------------------------------------------------
......@@ -133,8 +133,6 @@ ENDIF()
SET( CMAKE_CXX_FLAGS_RELWITHDEBINFO "-g -DDEBUG -O2" CACHE STRING "" FORCE )
add_definitions( '-DW_SHARED_FILES_RELATIVE="${SHARED_FILES_RELATIVE}"' ) # this relative path is used for all shared files like shaders and config files
ADD_SUBDIRECTORY( dataHandler )
ADD_SUBDIRECTORY( graphicsEngine )
ADD_SUBDIRECTORY( gui )
......
......@@ -15,11 +15,6 @@ TARGET_LINK_LIBRARIES( OWcommon ${Boost_LIBRARIES} ${CMAKE_DL_LIBS} )
# with these defines.
add_definitions( '-DW_LIB_PREFIX="${CMAKE_SHARED_LIBRARY_PREFIX}"' )
add_definitions( '-DW_LIB_SUFFIX="${CMAKE_SHARED_LIBRARY_SUFFIX}"' )
IF( CMAKE_HOST_WIN32 )
add_definitions( '-DW_LIB_PATH="."' ) # use relative paths here
ELSE()
add_definitions( '-DW_LIB_PATH="../lib"' )
ENDIF()
# Unit tests
IF( OW_COMPILE_TESTS )
......
......@@ -22,71 +22,113 @@
//
//---------------------------------------------------------------------------
#include <stdlib.h>
#include <boost/tokenizer.hpp>
#include <string>
#include "WPreferences.h"
#include <boost/filesystem.hpp>
#include "WPathHelper.h"
#include "WGEResourceManager.h"
#include "WGraphicsEngine.h"
// path helper instance as singleton
boost::shared_ptr< WPathHelper > WPathHelper::m_instance = boost::shared_ptr< WPathHelper >();
// resource manager instance as singleton
boost::shared_ptr< WGEResourceManager > WGEResourceManager::m_instance = boost::shared_ptr< WGEResourceManager >();
WGEResourceManager::WGEResourceManager()
WPathHelper::WPathHelper()
{
// initialize
// initialize members
}
WGEResourceManager::~WGEResourceManager()
WPathHelper::~WPathHelper()
{
// cleanup
}
boost::shared_ptr< WGEResourceManager > WGEResourceManager::getResourceManager()
boost::shared_ptr< WPathHelper > WPathHelper::getPathHelper()
{
if ( !m_instance )
{
m_instance = boost::shared_ptr< WGEResourceManager >( new WGEResourceManager() );
m_instance = boost::shared_ptr< WPathHelper >( new WPathHelper() );
}
return m_instance;
}
std::string WGEResourceManager::getDefaultFont()
void WPathHelper::setAppPath( boost::filesystem::path appPath )
{
m_appPath = appPath;
m_sharePath = m_appPath / "../share/OpenWalnut";
m_libPath = m_appPath / "../lib";
m_modulePath = m_libPath / "modules";
}
boost::filesystem::path WPathHelper::getAppPath()
{
return getPathHelper()->m_appPath;
}
boost::filesystem::path WPathHelper::getConfigFile()
{
// TODO(ebaum): we should prefer a user config file in ~.walnut.cfg in future. This needs to be platform independent of course.
return getPathHelper()->m_sharePath / "walnut.cfg";
}
boost::filesystem::path WPathHelper::getFontPath()
{
return getPathHelper()->m_sharePath / "fonts";
}
boost::filesystem::path WPathHelper::getShaderPath()
{
return getPathHelper()->m_sharePath / "shaders";
}
WPathHelper::Fonts WPathHelper::getAllFonts()
{
// NOTE: as osgVoewer::StatsHandler uses hard coded font names, we need to ensure that our default font also is available as file "arial.ttf"
// in the fonts subdirectory. So, if you change the default font here please also ensure arial.ttf points to/is the same as the new default
// font.
return m_boldFont;
Fonts fonts;
fonts.Regular = getFontPath() / "LiberationMono-Regular.ttf";
fonts.Bold = getFontPath() / "LiberationMono-Bold.ttf";
fonts.Italic = getFontPath() / "LiberationMono-Italic.ttf";
fonts.Default = fonts.Bold;
return fonts;
}
std::string WGEResourceManager::getRegularFont()
boost::filesystem::path WPathHelper::getModulePath()
{
return m_regularFont;
return getPathHelper()->m_modulePath;
}
std::string WGEResourceManager::getItalicFont()
boost::filesystem::path WPathHelper::getLibPath()
{
return m_italicFont;
return getPathHelper()->m_libPath;
}
std::string WGEResourceManager::getBoldFont()
boost::filesystem::path WPathHelper::getSharePath()
{
return m_boldFont;
return getPathHelper()->m_sharePath;
}
void WGEResourceManager::setFontPath( std::string path )
std::vector< boost::filesystem::path > WPathHelper::getAllModulePaths()
{
// NOTE: the osgViewer::StatsHandler uses a hard coded font filename. :-(. Fortunately OSG allows us to modify the search path using
// environment variables:
setenv( "OSGFILEPATH", path.c_str(), 1 );
namespace fs = boost::filesystem;
fs::path fontPath = path;
m_regularFont = fs::path( fontPath / "LiberationMono-Regular.ttf" ).file_string();
m_boldFont = fs::path( fontPath / "LiberationMono-Bold.ttf" ).file_string();
m_italicFont = fs::path( fontPath / "LiberationMono-Italic.ttf" ).file_string();
// the list of paths
std::vector< boost::filesystem::path > paths;
// the first element always is the global search path
paths.push_back( getModulePath() );
std::string additionalPaths = "";
if ( !WPreferences::getPreference< std::string >( "modules.path", &additionalPaths ) )
{
// no config option found.
return paths;
}
// separate list of additional paths:
typedef boost::tokenizer< boost::char_separator< char > > tokenizer;
boost::char_separator< char > sep( ";" );
tokenizer tok( additionalPaths, sep );
for ( tokenizer::iterator it = tok.begin(); it != tok.end(); ++it )
{
paths.push_back( boost::filesystem::path( *it ) );
}
return paths;
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WPATHHELPER_H
#define WPATHHELPER_H
#include <vector>
#include <boost/filesystem.hpp>
#include <boost/shared_ptr.hpp>
/**
* Singleton class helping to find files and paths. It is a useful to to search for resources and the central place to "hardcode" relative paths.
* It contains global paths only. Modules have their OWN local paths.
*/
class WPathHelper
{
public:
/**
* Default constructor.
*/
WPathHelper();
/**
* Destructor.
*/
virtual ~WPathHelper();
/**
* Returns instance of the path helper. If it does not exists, it will be created.
*
* \return the running path helper instance.
*/
static boost::shared_ptr< WPathHelper > getPathHelper();
/**
* Set the current application path. This should be called only once.
*
* \param appPath the application path
*/
void setAppPath( boost::filesystem::path appPath );
/**
* The path where the binary file resides in. This is for example /usr/bin.
*
* \return the application path.
*/
static boost::filesystem::path getAppPath();
/**
* The path of the configuration file walnut.cfg. Always use this file to parse configurations.
*
* \return the config file.
*/
static boost::filesystem::path getConfigFile();
/**
* The path where font files reside in.
*
* \return the font path.
*/
static boost::filesystem::path getFontPath();
/**
* Paths to all known fonts.
*/
typedef struct
{
boost::filesystem::path Default;
boost::filesystem::path Regular;
boost::filesystem::path Italic;
boost::filesystem::path Bold;
}
Fonts;
/**
* The paths to all fonts supported.
*
* \return the file paths to all fonts
*/
static Fonts getAllFonts();
/**
* The path to the global shaders. Modules usually have their own local shader directory.
*
* \return global shader path.
*/
static boost::filesystem::path getShaderPath();
/**
* The path to the globally installed modules. This does not respect any environment variables or config options! Use this only to search
* global modules. To get a list of all module search paths, including user defined ones, use getAllModulePaths().
*
* \return path to globally installed modules.
*/
static boost::filesystem::path getModulePath();
/**
* This returns a list of search paths for modules. This list is defined by the "modules.path" - option in walnut.cfg. All of these
* directories CAN contain modules. On startup, they get searched in the specified order.
*
* \return list of search paths for modules
*/
static std::vector< boost::filesystem::path > getAllModulePaths();
/**
* The path to the OW libs. You normally should not need this.
*
* \return the path to the libs.
*/
static boost::filesystem::path getLibPath();
/**
* The path where shared files reside in.
*
* \return the shared files path.
*/
static boost::filesystem::path getSharePath();
protected:
private:
/**
* Application path. NOT the path of the binary. The binary path is m_appPath+"/walnut".
*/
boost::filesystem::path m_appPath;
/**
* The path where all the shared files reside in.
*/
boost::filesystem::path m_sharePath;
/**
* The path to the globally installed modules.
*/
boost::filesystem::path m_modulePath;
/**
* The path to the OW libs.
*/
boost::filesystem::path m_libPath;
/**
* Singleton instance of WPathHelper.
*/
static boost::shared_ptr< WPathHelper > m_instance;
};
#endif // WPATHHELPER_H
......@@ -276,6 +276,6 @@ std::string WSharedLib::getSystemSuffix()
std::string WSharedLib::getSystemLibPath()
{
return W_LIB_PATH;
return "../lib";
}
......@@ -14,7 +14,6 @@ IF( OW_COMPILE_TESTS )
"WGESignals.cpp"
"WGraphicsEngine.cpp"
"WGEScene.cpp"
"WGEResourceManager.cpp"
"WGEViewer.cpp"
"WGEGraphicsWindow.cpp"
"WGEGroupNode.cpp"
......
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WGERESOURCEMANAGER_H
#define WGERESOURCEMANAGER_H
#include <string>
#include <boost/shared_ptr.hpp>
/**
* Class used to manage out-of-code resources used in the graphics engine. Those resources are especially fonts, shaders and
* icons/symbols/textures. This is useful to avoid a lot of hard coded path/filenames in the source tree.
*/
class WGEResourceManager
{
public:
/**
* Default constructor.
*/
WGEResourceManager();
/**
* Destructor.
*/
virtual ~WGEResourceManager();
/**
* Returns instance of the resource manager. If it does not exists, it will be created.
*
* \return the running resource manager instance.
*/
static boost::shared_ptr< WGEResourceManager > getResourceManager();
/**
* Returns the default font which should be used at the most cases.
*
* \return the absolute filename to the font.
*/
std::string getDefaultFont();
/**
* Returns the regular (non-bold) font.
* \note you should use getDefaultFont() in the most cases.
*
* \return the absolute filename to the font.
*/
std::string getRegularFont();
/**
* Returns the italic font.
* \note you should use getDefaultFont() in the most cases.
*
* \return the absolute filename to the font.
*/
std::string getItalicFont();
/**
* Returns the bold font.
* \note you should use getDefaultFont() in the most cases.
*
* \return the absolute filename to the font.
*/
std::string getBoldFont();
/**
* Updates the internal font paths according to the specified one.
*
* \param path the new path.
*/
void setFontPath( std::string path );
protected:
/**
* Filename of standard regular font.
*/
std::string m_regularFont;
/**
* Filename of standard bold font.
*/
std::string m_boldFont;
/**
* Filename of standard italic font.
*/
std::string m_italicFont;
private:
/**
* Singleton instance of WGraphicsEngine.
*/
static boost::shared_ptr< WGEResourceManager > m_instance;
};
#endif // WGERESOURCEMANAGER_H
......@@ -45,7 +45,6 @@
#include "WGraphicsEngine.h"
#include "exceptions/WGEInitFailed.h"
#include "exceptions/WGESignalSubscriptionFailed.h"
#include "WGEResourceManager.h"
// graphics engine instance as singleton
boost::shared_ptr< WGraphicsEngine > WGraphicsEngine::m_instance = boost::shared_ptr< WGraphicsEngine >();
......@@ -57,12 +56,6 @@ WGraphicsEngine::WGraphicsEngine():
// initialize members
m_rootNode = new WGEScene();
m_shaderPath = "";
m_fontPath = "";
// init resource manager ( it is a singleton and gets created during first "getResourceManager" request.
WGEResourceManager::getResourceManager();
}
WGraphicsEngine::~WGraphicsEngine()
......@@ -87,29 +80,6 @@ osg::ref_ptr<WGEScene> WGraphicsEngine::getScene()
return m_rootNode;
}
std::string WGraphicsEngine::getShaderPath() const
{
return m_shaderPath;
}
void WGraphicsEngine::setShaderPath( std::string path )
{
m_shaderPath = path;
}
std::string WGraphicsEngine::getFontPath() const
{
return m_fontPath;
}
void WGraphicsEngine::setFontPath( std::string path )
{
m_fontPath = path;
// we need to propagate the change to the resource manager
WGEResourceManager::getResourceManager()->setFontPath( path );
}
boost::shared_ptr<WGEViewer> WGraphicsEngine::createViewer( std::string name, int x, int y,
int width, int height, WGECamera::ProjectionMode projectionMode,
WColor bgColor )
......
......@@ -78,34 +78,6 @@ public:
*/
osg::ref_ptr<WGEScene> getScene();
/**
* Return the path where the shaders are expected to be located.
*
* \return shader path
*/
std::string getShaderPath() const;
/**
* Sets the shader path.
*
* \param path path to shaders.
*/
void setShaderPath( std::string path );
/**
* Return the path where the fonts are expected to be located.
*
* \return font path
*/
std::string getFontPath() const;
/**
* Sets the font path.
*
* \param path path to fonts.
*/
void setFontPath( std::string path );
/**
* Creates a new viewer. Does basic initialization and sets the default scene.
*
......@@ -145,15 +117,6 @@ public:
*/
boost::shared_ptr< WGEViewer > getViewer();
/**
* 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.
*
......@@ -203,13 +166,6 @@ protected:
*/
boost::mutex m_viewersLock;
/**
* Path to the shaders.
*/
std::string m_shaderPath;
std::string m_fontPath; //!< Path where the fonts are expected.
/**
* OpenSceneGraph composite viewer. Contains all created osgViewer::Views.
*/
......
......@@ -39,12 +39,13 @@
#include "WGraphicsEngine.h"
#include "../common/WLogger.h"
#include "../common/WPathHelper.h"
#include "WShader.h"
WShader::WShader( std::string name ):
WShader::WShader( std::string name, boost::filesystem::path search ):
osg::Program(),
m_shaderPath( WGraphicsEngine::getGraphicsEngine()->getShaderPath() ),
m_shaderPath( search ),
m_name( name ),
m_reload( true ),
m_shaderLoaded( false ),
......@@ -209,8 +210,44 @@ std::string WShader::processShader( const std::string filename, bool optional, i
// this is an expression for the #version statement
static const boost::regex ver( "^[ ]*#[ ]*version[ ]+[123456789][0123456789]+.*$" );
// the input stream
std::string fn = ( boost::filesystem::path( m_shaderPath ) / filename ).file_string();
// the input stream, first check existence of shader
// search these places in this order:
// 1. m_shaderPath
// 2. m_shaderPath / shaders
// 3. WPathHelper::getShaderPath()
// use one of the following paths
std::string fn = filename;
std::string fnLocal = ( m_shaderPath / filename ).file_string();
std::string fnLocalShaders = ( m_shaderPath / "shaders" / filename ).file_string();
std::string fnGlobal = ( WPathHelper::getShaderPath() / filename ).file_string();
if ( boost::filesystem::exists( m_shaderPath / filename ) )