From: <jer...@us...> - 2008-06-12 15:27:26
|
Revision: 6548 http://playerstage.svn.sourceforge.net/playerstage/?rev=6548&view=rev Author: jeremy_asher Date: 2008-06-12 15:27:04 -0700 (Thu, 12 Jun 2008) Log Message: ----------- Implemented FileManager class to help with resource locating Modified Paths: -------------- code/stage/trunk/libstage/CMakeLists.txt code/stage/trunk/libstage/canvas.cc code/stage/trunk/libstage/stage.hh code/stage/trunk/libstage/texture_manager.cc code/stage/trunk/libstage/worldgui.cc Added Paths: ----------- code/stage/trunk/libstage/file_manager.cc code/stage/trunk/libstage/file_manager.hh Modified: code/stage/trunk/libstage/CMakeLists.txt =================================================================== --- code/stage/trunk/libstage/CMakeLists.txt 2008-06-12 16:55:06 UTC (rev 6547) +++ code/stage/trunk/libstage/CMakeLists.txt 2008-06-12 22:27:04 UTC (rev 6548) @@ -1,6 +1,8 @@ add_library( stage SHARED stage.hh # get headers into IDE projects + file_manager.hh + file_manager.cc ancestor.cc block.cc canvas.cc Modified: code/stage/trunk/libstage/canvas.cc =================================================================== --- code/stage/trunk/libstage/canvas.cc 2008-06-12 16:55:06 UTC (rev 6547) +++ code/stage/trunk/libstage/canvas.cc 2008-06-12 22:27:04 UTC (rev 6548) @@ -7,6 +7,7 @@ #include "stage_internal.hh" #include "texture_manager.hh" #include "replace.h" +#include <string> using namespace Stg; @@ -557,7 +558,14 @@ //TODO find a better home for loading textures if( loaded_texture == false ) { - GLuint stall_id = TextureManager::getInstance().loadTexture( "assets/stall.png" ); + std::string fullpath; + fullpath = world->fileMan.fullPath( "stall.png" ); + if ( fullpath == "" ) { + PRINT_DEBUG( "Unable to load texture.\n" ); + } + + GLuint stall_id = TextureManager::getInstance().loadTexture( fullpath.c_str() ); + TextureManager::getInstance()._stall_texture_id = stall_id; loaded_texture = true; Added: code/stage/trunk/libstage/file_manager.cc =================================================================== --- code/stage/trunk/libstage/file_manager.cc (rev 0) +++ code/stage/trunk/libstage/file_manager.cc 2008-06-12 22:27:04 UTC (rev 6548) @@ -0,0 +1,65 @@ +#include "file_manager.hh" +#include "stage.hh" // to get PRINT_DEBUG + +namespace Stg +{ + FileManager::FileManager() { + SharePath = INSTALL_PREFIX "/share/stage"; + AssetPath = SharePath + '/' + "assets"; + CtrlPath = getenv("STAGEPATH"); + WorldsRoot = "."; + + paths.push_back( "." ); + paths.push_back( SharePath ); + paths.push_back( AssetPath ); + paths.push_back( CtrlPath ); + } + + std::string FileManager::fullPath( std::string filename ) { + PRINT_DEBUG1("FileManager: trying %s\n", filename.c_str()); + if ( readable( filename ) ) + return filename; + + for ( unsigned int i=0; i<paths.size(); i++ ) { + std::string path = paths[i] + '/' + filename; + PRINT_DEBUG1("FileManager: trying %s\n", path.c_str()); + if ( readable( path ) ) { + return path; + } + } + + PRINT_DEBUG("FileManager: Not found.\n"); + return ""; + } + + /*std::string FileManager::fullPathImage( std::string filename ) { + std::string path = ImgPath + '/' + filename; + if ( readable ( path ) ) + return path; + else + return ""; + }*/ + + bool FileManager::readable( std::string path ) { + std::ifstream iFile; + iFile.open( path.c_str() ); + if ( iFile.is_open() ) { + iFile.close(); + return true; + } + else { + return false; + } + } + + std::string FileManager::stripFilename( std::string path ) { + std::string pathChars( "\\/" ); + size_t loc = path.find_last_of( pathChars ); + if ( loc < 0 ) + return path; + else + return path.substr( 0, loc ); + } + +}; // namespace Stg + Added: code/stage/trunk/libstage/file_manager.hh =================================================================== --- code/stage/trunk/libstage/file_manager.hh (rev 0) +++ code/stage/trunk/libstage/file_manager.hh 2008-06-12 22:27:04 UTC (rev 6548) @@ -0,0 +1,38 @@ +#ifndef _FILE_MANAGER_HH_ +#define _FILE_MANAGER_HH_ + +// Normally passed by build scripts +#ifndef INSTALL_PREFIX +#define INSTALL_PREFIX "." +#endif + +#include <fstream> +#include <string> +#include <vector> + +namespace Stg { + + class FileManager { + private: + std::vector<std::string> paths; + std::string SharePath; + std::string AssetPath; + std::string CtrlPath; + std::string WorldsRoot; + + std::string stripFilename( std::string path ); + public: + FileManager(); + + std::string fullPath( std::string filename ); + //std::string fullPathImage( std::string filename ); + + inline const std::string worldsRoot() const { return WorldsRoot; } + inline void newWorld( std::string worldfile ) { + WorldsRoot = stripFilename( worldfile ); } + + bool readable( std::string path ); + }; + +}; +#endif Modified: code/stage/trunk/libstage/stage.hh =================================================================== --- code/stage/trunk/libstage/stage.hh 2008-06-12 16:55:06 UTC (rev 6547) +++ code/stage/trunk/libstage/stage.hh 2008-06-12 22:27:04 UTC (rev 6548) @@ -79,6 +79,7 @@ #include <GL/glu.h> #endif +#include "file_manager.hh" /** The Stage library uses its own namespace */ namespace Stg @@ -928,8 +929,8 @@ friend class StgBlock; friend class StgTime; - private: - +private: + static bool quit_all; // quit all worlds ASAP static unsigned int next_id; //< initialized to zero, used tob //allocate unique sequential world ids @@ -1041,6 +1042,8 @@ double ppm ); virtual ~StgWorld(); + + FileManager fileMan; stg_usec_t SimTimeNow(void){ return sim_time;} ; stg_usec_t RealTimeNow(void); @@ -1892,9 +1895,9 @@ static void SaveCallback( Fl_Widget* wid, StgWorldGui* world ); static void SaveAsCallback( Fl_Widget* wid, StgWorldGui* world ); static void QuitCallback( Fl_Widget* wid, StgWorldGui* world ); - static void About_cb( Fl_Widget* wid ); + static void About_cb( Fl_Widget* wid, StgWorldGui* world ); static void HelpAboutCallback( Fl_Widget* wid ); - static void view_toggle_cb(Fl_Menu_Bar* menubar, StgCanvas* canvas ); + static void view_toggle_cb( Fl_Menu_Bar* menubar, StgCanvas* canvas ); static void WindowCallback( Fl_Widget* wid, StgWorldGui* world ); bool SaveAsDialog(); Modified: code/stage/trunk/libstage/texture_manager.cc =================================================================== --- code/stage/trunk/libstage/texture_manager.cc 2008-06-12 16:55:06 UTC (rev 6547) +++ code/stage/trunk/libstage/texture_manager.cc 2008-06-12 22:27:04 UTC (rev 6548) @@ -7,29 +7,15 @@ */ #include "texture_manager.hh" +#include "file_manager.hh" #include <sstream> -//TODO Windows Port Fl_Shared_Image* TextureManager::loadImage( const char* filename ) -{ - if( filename[ 0 ] == '/' || filename[ 0 ] == '~' ) - return Fl_Shared_Image::get( filename ); - - //TODO move this somewhere else, and include STAGEPATH, and path relative to user supplied world file - const char* prefixes[] = { - ".", - INSTALL_PREFIX "/share/stage", - NULL - }; - +{ Fl_Shared_Image *img = NULL; - int i = 0; - while( img == NULL && prefixes[ i ] != NULL ) { - std::ostringstream oss; - oss << prefixes[ i ] << "/" << filename; - img = Fl_Shared_Image::get( oss.str().c_str() ); - i++; - } + + img = Fl_Shared_Image::get( filename ); + return img; } Modified: code/stage/trunk/libstage/worldgui.cc =================================================================== --- code/stage/trunk/libstage/worldgui.cc 2008-06-12 16:55:06 UTC (rev 6547) +++ code/stage/trunk/libstage/worldgui.cc 2008-06-12 22:27:04 UTC (rev 6548) @@ -105,6 +105,8 @@ #include <FL/Fl_Text_Display.H> #include <FL/Fl_File_Chooser.H> +#include "file_manager.hh" + static const char* MITEM_VIEW_DATA = "&View/&Data"; static const char* MITEM_VIEW_BLOCKS = "&View/&Blocks"; static const char* MITEM_VIEW_GRID = "&View/&Grid"; @@ -174,7 +176,7 @@ FL_MENU_TOGGLE| (canvas->showflags & STG_SHOW_TRAILRISE ? FL_MENU_VALUE : 0 )); mbar->add( "&Help", 0, 0, 0, FL_SUBMENU ); - mbar->add( "Help/&About Stage...", NULL, (Fl_Callback *)About_cb ); + mbar->add( "Help/&About Stage...", NULL, (Fl_Callback *)About_cb, this ); //mbar->add( "Help/HTML Documentation", FL_CTRL + 'g', (Fl_Callback *)dummy_cb ); callback( (Fl_Callback*)WindowCallback, this ); @@ -271,8 +273,7 @@ //bool success; const char* pattern = "World Files (*.world)"; - // todo: replace this with real path - worldsPath = "/Users/jeremya/stage_trunk/worlds"; + worldsPath = world->fileMan.worldsRoot().c_str(); Fl_File_Chooser fc( worldsPath, pattern, Fl_File_Chooser::CREATE, "Load World File..." ); fc.ok_label( "Load" ); @@ -282,15 +283,25 @@ filename = fc.value(); - if (filename != NULL) { - // if (initialized) { - world->Stop(); - world->UnLoad(); - // } + if (filename != NULL) { // chose something + if ( world->fileMan.readable( filename ) ) { + // file is readable, clear and load + + // if (initialized) { + world->Stop(); + world->UnLoad(); + // } + + // todo: make sure loading is successful + world->fileMan.newWorld( filename ); + world->Load( filename ); + world->Start(); // if (stopped) + } + else { + fl_alert( "Unable to read selected world file." ); + } - // todo: make sure loading is successful - world->Load( filename ); - world->Start(); + } } @@ -407,7 +418,7 @@ //printf( "value: %d\n", item->value() ); } -void StgWorldGui::About_cb( Fl_Widget* ) +void StgWorldGui::About_cb( Fl_Widget*, StgWorldGui* world ) { fl_register_images(); @@ -416,30 +427,22 @@ const int Spc = 10; const int ButtonH = 25; const int ButtonW = 60; + const int pngH = 82; + //const int pngW = 264; Fl_Window win( Width, Height ); // make a window - // <temporary hack> - const char* stagepath = getenv("STAGEPATH"); - const char* logopath = "../../../assets/logo.png"; - char* fullpath = (char*)malloc( strlen(stagepath) + strlen(logopath) + 2 ); - strcpy( fullpath, stagepath ); - strcat( fullpath, "/" ); - strcat( fullpath, logopath ); - printf("fullpath: %s\n", fullpath); - Fl_PNG_Image png(fullpath); // load image into ram - free( fullpath ); - Fl_Box box( Spc, Spc, - Width-2*Spc, png.h() ); // widget that will contain image - + Width-2*Spc, pngH ); // widget that will contain image - // </temporary hack> + std::string fullpath; + fullpath = world->fileMan.fullPath( "stagelogo.png" ); + Fl_PNG_Image png( fullpath.c_str() ); // load image into ram box.image(png); // attach image to box - - Fl_Text_Display text( Spc, png.h()+2*Spc, - Width-2*Spc, Height-png.h()-ButtonH-4*Spc ); + + Fl_Text_Display text( Spc, pngH+2*Spc, + Width-2*Spc, Height-pngH-ButtonH-4*Spc ); text.box( FL_NO_BOX ); text.color(win.color()); @@ -462,6 +465,8 @@ win.show(); while (win.shown()) Fl::wait(); + + } void StgWorldGui::HelpAboutCallback( Fl_Widget* wid ) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |