changeset 644:b84dbc4665b0

loaders extension can now handle multiple loaders for different filetypes. A difference between map and object files is now being made! Fixed a small redundancy in the guimanager. Added clearResourceLoaders() to the Pool class. This can be used if different resourceloaders have to be added at a later point in time (running editor for example, a plugin might want to have it's own loader added)
author nihathrael@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 11 Oct 2010 22:20:00 +0000
parents edf6dcfe8cd4
children 291ba2946c73
files engine/core/gui/guimanager.cpp engine/core/util/resource/pool.cpp engine/core/util/resource/pool.h engine/core/util/resource/resource.i engine/python/fife/extensions/loaders.py
diffstat 5 files changed, 77 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/engine/core/gui/guimanager.cpp	Sat Oct 09 17:00:33 2010 +0000
+++ b/engine/core/gui/guimanager.cpp	Mon Oct 11 22:20:00 2010 +0000
@@ -53,7 +53,7 @@
 	static Logger _log(LM_GUI);
 
 	GUIManager::GUIManager(ImagePool& pool) :
-		m_gcn_gui(new gcn::Gui()), 
+		m_gcn_gui(new gcn::Gui()),
 		m_focushandler(0),
 		m_gcn_topcontainer(new gcn::Container()),
 		m_imgloader(new GuiImageLoader(pool)) ,
@@ -88,8 +88,7 @@
 	}
 
 	bool GUIManager::onSdlEvent(SDL_Event& evt) {
-		gcn::SDLInput *input = dynamic_cast<gcn::SDLInput*>(m_gcn_gui->getInput());
-		if (!input) {
+		if (!m_input) {
 			FL_WARN(_log, "GUIManager, GuichanGUI->getInput == 0 ... discarding events!");
 			return false;
 		}
@@ -98,7 +97,7 @@
 			case SDL_MOUSEBUTTONDOWN:
 			case SDL_MOUSEBUTTONUP:
 				if( m_gcn_topcontainer->getWidgetAt(evt.button.x,evt.button.y) ) {
-					input->pushInput(evt);
+					m_input->pushInput(evt);
 					return true;
 				}
 				m_focushandler->focusNone();
@@ -107,14 +106,14 @@
 			case SDL_MOUSEMOTION:
 				if( m_gcn_topcontainer->getWidgetAt(evt.button.x,evt.button.y) ) {
 					m_had_mouse = true;
-					input->pushInput(evt);
+					m_input->pushInput(evt);
 					return true;
 				}
 				if( m_had_mouse ) {
 					// We only keep the mouse if a widget/window has requested
 					// dragging.
 					m_had_mouse = bool(m_focushandler->getDraggedWidget());
-					input->pushInput(evt);
+					m_input->pushInput(evt);
 					return true;
 				}
 				return false;
@@ -122,7 +121,7 @@
 			case SDL_KEYDOWN:
 			case SDL_KEYUP:
 				if(m_focushandler->getFocused()) {
-					input->pushInput(evt);
+					m_input->pushInput(evt);
 					return true;
 				}
 				return false;
@@ -189,7 +188,7 @@
 			font = new SubImageFont(fontpath, fontglyphs, m_pool);
 		}
 		guifont = new GuiFont(font);
-		
+
 		m_fonts.push_back(guifont);
 		return guifont;
 	}
@@ -203,7 +202,7 @@
 				return;
 			}
 			++i;
-		}	
+		}
 	}
 
 	GuiFont* GUIManager::setDefaultFont(const std::string& path, unsigned int size, const std::string& glyphs) {
@@ -244,7 +243,7 @@
 		// Convert from guichan keyval to FIFE keyval
 		int keyval = gcnevt.getKey().getValue();
 		keyval = convertGuichanKeyToFifeKey(keyval);
-		
+
 		keyevt.setKey(Key(static_cast<Key::KeyType>(keyval), keyval));
 
 		return keyevt;
@@ -460,7 +459,7 @@
 					value = value - 'A' + 'a';
 				}
 
-				// FIXME: Accented characters (á) will not get converted properly.
+				// FIXME: Accented characters (á) will not get converted properly.
 				break;
 		}
 
--- a/engine/core/util/resource/pool.cpp	Sat Oct 09 17:00:33 2010 +0000
+++ b/engine/core/util/resource/pool.cpp	Mon Oct 11 22:20:00 2010 +0000
@@ -34,8 +34,8 @@
 
 namespace FIFE {
 	static Logger _log(LM_POOL);
-	
-	Pool::Pool(const std::string& name): 
+
+	Pool::Pool(const std::string& name):
 		m_entries(),
 		m_location_to_entry(),
 		m_loaders(),
@@ -54,7 +54,7 @@
 			delete (*loader);
 		}
 	}
-	
+
 	void Pool::reset() {
 		std::vector<PoolEntry*>::iterator entry;
 		for (entry = m_entries.begin(); entry != m_entries.end(); entry++) {
@@ -88,12 +88,16 @@
 		m_loaders.push_back(loader);
 	}
 
+	void Pool::clearResourceLoaders() {
+		m_loaders.clear();
+	}
+
 	int Pool::addResourceFromLocation(ResourceLocation* loc) {
 		ResourceLocationToEntry::const_iterator it = m_location_to_entry.find(loc);
 		if (it != m_location_to_entry.end()) {
 			return it->second;
 		}
-		
+
 		PoolEntry* entry = new PoolEntry();
 		entry->location = loc->clone();
 		m_entries.push_back(entry);
@@ -128,7 +132,7 @@
 				msg << "#" << index << "<" << entry->location->getFilename()
 				    << "> in pool " << m_name;
 				FL_ERR(_log, msg);
-	      
+
 				throw NotFound(msg.str);
 			}
 
@@ -224,7 +228,7 @@
 	void Pool::sanityCheck() {
 		// It is easy to mess up the important consistent
 		// less-than operator for the location classes.
-		// This will check if according to the '==' operator 
+		// This will check if according to the '==' operator
 		// entries are duplicate (=memory leaks).
 		// Slow and inaccurate. But should barf at real messups.
 		for(unsigned i = 0; i != m_entries.size(); ++i) {
--- a/engine/core/util/resource/pool.h	Sat Oct 09 17:00:33 2010 +0000
+++ b/engine/core/util/resource/pool.h	Mon Oct 11 22:20:00 2010 +0000
@@ -54,7 +54,7 @@
 	/**  Pool is used to optimize memory usage for resources
 	 *
 	 * Pool guarantees that there is minimal amount of resources
-	 *   used in cases when it is would possible that multiple 
+	 *   used in cases when it is would possible that multiple
 	 *   instances of the same data would be loaded into the memory.
 	 *   Pool is the owner for resources taking care of their deletion.
 	 */
@@ -77,11 +77,15 @@
 		 */
 		virtual void addResourceLoader(ResourceLoader* loader);
 
+		/** Clear the resource loaders
+		 */
+		virtual void clearResourceLoaders();
+
 		/** Adds new resource into the pool using the given location.
 		 * @return The index of the resource in the pool.
 		 */
 		virtual int addResourceFromLocation(ResourceLocation* loc);
-		
+
 		/** This is a convenience version of addResourceFromLocation().
 		 * It converts the filename into a ResourceLocation and then
 		 * calls addResourceFromLocation.
@@ -92,13 +96,13 @@
 		virtual int addResourceFromFile(const std::string& filename);
 
 		/** Gets resource from pool with given index
-		 * 
+		 *
 		 * @param inc Specifies weither this call will increase the ref counter
 		 */
 		virtual IResource& get(unsigned int index, bool inc = false);
-		
+
 		/** Removes the resource from pool if reference counter is null
-		 * 
+		 *
 		 * @param dec Specifies weither the ref counter will be decreased
 		 * before checking
 		 */
--- a/engine/core/util/resource/resource.i	Sat Oct 09 17:00:33 2010 +0000
+++ b/engine/core/util/resource/resource.i	Mon Oct 11 22:20:00 2010 +0000
@@ -76,7 +76,7 @@
 		virtual void save(const ResourceLocation& location, IResource* resource) = 0;
 		virtual void save(const std::string& filename, IResource* resource) { save(ResourceLocation(filename), resource); }
 	};
-	
+
 
 	enum { RES_LOADED = 0x01, RES_NON_LOADED  = 0x02};
 
@@ -89,13 +89,14 @@
 		virtual int getResourceCount(int status);
 		virtual int purgeLoadedResources();
 		virtual void addResourceLoader(ResourceLoader* loader);
+		virtual void clearResourceLoaders();
 		virtual void release(unsigned int index, bool dec = false);
 		virtual IResource& get(unsigned int index, bool inc = false);
 		virtual void printStatistics();
 
 	private:
 		Pool();
-	};	
+	};
 
 	class ResourcePtr {
 	public:
--- a/engine/python/fife/extensions/loaders.py	Sat Oct 09 17:00:33 2010 +0000
+++ b/engine/python/fife/extensions/loaders.py	Mon Oct 11 22:20:00 2010 +0000
@@ -23,6 +23,8 @@
 
 # Loader interface for FIFE's native xml format.
 
+import os.path
+
 from fife import fife
 
 from fife.extensions.serializers.xmlmap import XMLMapLoader
@@ -30,28 +32,32 @@
 
 from fife.extensions.serializers.xmlobject import XMLObjectLoader
 
+objectFileMapping = { 'xml' : XMLObjectLoader }
+mapFileMapping = { 'xml' : XMLMapLoader}
 fileExtensions = ('xml',)
+mapFileExtensions = ('xml',)
 
 def loadMapFile(path, engine, callback=None, debug=True):
 	""" load map file and get (an optional) callback if major stuff is done:
 	- map creation
 	- parsed imports
-	- parsed layers 
+	- parsed layers
 	- parsed cameras
 	the callback will send both a string and a float (which shows
 	the overall process), callback(string, float)
-	
+
 	@type	engine:		object
 	@param	engine: 	FIFE engine instance
 	@type	callback:	function
 	@param	callback:	callback for maploading progress
 	@type	debug:		bool
 	@param	debug:		flag to activate / deactivate print statements
-	
+
 	@type	map:	object
 	@return	map:	FIFE map object
 	"""
-	map_loader = XMLMapLoader(engine, callback, debug)
+	(filename, extension) = os.path.splitext(path)
+	map_loader = mapFileMapping[extension[1:]](engine, callback, debug)
 	map = map_loader.loadResource(fife.ResourceLocation(path))
 	if debug: print "--- Loading map took: ", map_loader.time_to_load, " seconds."
 	return map
@@ -59,7 +65,7 @@
 
 def loadImportFile(path, engine, debug=False):
 	""" uses XMLObjectLoader to load import files from path
-	
+
 	@type	path:	string
 	@param	path:	path to import file
 	@type	engine:	object
@@ -67,7 +73,8 @@
 	@type	debug:	bool
 	@param	debug:	flag to activate / deactivate print statements
 	"""
-	object_loader = XMLObjectLoader(engine.getImagePool(), engine.getAnimationPool(), engine.getModel(), engine.getVFS())
+	(filename, extension) = os.path.splitext(path)
+	object_loader = objectFileMapping[extension[1:]](engine.getImagePool(), engine.getAnimationPool(), engine.getModel(), engine.getVFS())
 	res = None
 	try:
 		res = object_loader.loadResource(fife.ResourceLocation(path))
@@ -82,7 +89,7 @@
 
 def loadImportDir(path, engine, debug=False):
 	""" helper function to call loadImportFile on a directory
-	
+
 	@type	path:	string
 	@param	path:	path to import directory
 	@type	engine:	object
@@ -101,9 +108,39 @@
 	@type	engine:	object
 	@param	engine:	FIFE engine instance
 	@type	debug:	bool
-	@param	debug:	flag to activate / deactivate print statements	
+	@param	debug:	flag to activate / deactivate print statements
 	"""
 	loadImportDir(path, engine, debug)
 
 	for dir in filter(lambda d: not d.startswith('.'), engine.getVFS().listDirectories(path)):
 		loadImportDirRec('/'.join([path, dir]), engine, debug)
+
+
+def addObjectFileLoader(fileExtension, loaderClass):
+	"""Add a new loader for fileextension
+	@type   fileExtension: string
+	@param  fileExtension: The file extension the loader is registered for
+	@type   loaderClass:   object
+	@param  loaderClass:   A fife.ResourceLoader implementation that loads objects
+	                       from files with the given fileExtension
+	"""
+	objectFileMapping[fileExtension] = loaderClass
+	_updateFileExtenions()
+
+
+def addMapLoader(fileExtension, loaderClass):
+	"""Add a new loader for fileextension
+	@type   fileExtension: string
+	@param  fileExtension: The file extension the loader is registered for
+	@type   loaderClass:   object
+	@param  loaderClass:   A fife.ResourceLoader implementation that loads maps
+	                       from files with the given fileExtension
+	"""
+	mapFileMapping[fileExtension] = loaderClass
+	_updateMapFileExtensions()
+
+def _updateFileExtensions():
+	fileExtensions = set(objectFileMapping.keys())
+
+def _updateMapFileExtensions():
+	mapFileExtensions = set(mapFileMapping.keys())
\ No newline at end of file