Mercurial > fife-parpg
changeset 156:376b8afc9a18
Fixed a horrendous misconeception in the pool.
The map was sorted by pointer comparison.
Needs a bit cleanup, though.
author | phoku@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 12 Oct 2008 20:30:09 +0000 |
parents | 29309cd5e240 |
children | bb9902910067 |
files | engine/core/util/resource/pool.cpp engine/core/util/resource/pool.h engine/core/util/resource/resource.i engine/core/video/image_location.cpp engine/core/video/video.i engine/extensions/serializers/xmlobject.py tests/core_tests/test_imagepool.cpp |
diffstat | 7 files changed, 56 insertions(+), 35 deletions(-) [+] |
line wrap: on
line diff
--- a/engine/core/util/resource/pool.cpp Sun Oct 12 19:23:10 2008 +0000 +++ b/engine/core/util/resource/pool.cpp Sun Oct 12 20:30:09 2008 +0000 @@ -87,22 +87,23 @@ m_loaders.push_back(loader); } - int Pool::addResourceFromLocation(const ResourceLocation& loc) { + 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; + return it->second; } PoolEntry* entry = new PoolEntry(); - entry->location = loc.clone(); + entry->location = loc->clone(); m_entries.push_back(entry); size_t index = m_entries.size() - 1; - m_location_to_entry[loc] = index; + m_location_to_entry[entry->location] = index; return index; } int Pool::addResourceFromFile(const std::string& filename) { - return addResourceFromLocation(ResourceLocation(filename)); + ResourceLocation r = ResourceLocation(filename); + return addResourceFromLocation(&r); } IResource& Pool::get(unsigned int index, bool inc) {
--- a/engine/core/util/resource/pool.h Sun Oct 12 19:23:10 2008 +0000 +++ b/engine/core/util/resource/pool.h Sun Oct 12 20:30:09 2008 +0000 @@ -40,6 +40,13 @@ namespace FIFE { + struct ResourceLocationComparator { + bool operator()(const ResourceLocation* r1, const ResourceLocation* r2) const + { + return r1->operator<(*r2); + } + }; + class IResource; enum { RES_LOADED = 0x01, RES_NON_LOADED = 0x02}; @@ -73,7 +80,7 @@ /** Adds new resource into the pool using the given location. * @return The index of the resource in the pool. */ - virtual int addResourceFromLocation(const ResourceLocation& loc); + virtual int addResourceFromLocation(ResourceLocation* loc); /** This is a convenience version of addResourceFromLocation(). * It converts the filename into a ResourceLocation and then @@ -144,7 +151,7 @@ void findAndSetProvider(PoolEntry& entry); std::vector<PoolEntry*> m_entries; - typedef std::map<ResourceLocation, int> ResourceLocationToEntry; + typedef std::map<ResourceLocation*, int, ResourceLocationComparator> ResourceLocationToEntry; ResourceLocationToEntry m_location_to_entry; std::vector<ResourceLoader*> m_loaders; std::string m_name;
--- a/engine/core/util/resource/resource.i Sun Oct 12 19:23:10 2008 +0000 +++ b/engine/core/util/resource/resource.i Sun Oct 12 20:30:09 2008 +0000 @@ -85,7 +85,7 @@ static const int INVALID_ID = -1; virtual ~Pool(); virtual int addResourceFromFile(const std::string& filename); - virtual int addResourceFromLocation(const ResourceLocation& loc); + virtual int addResourceFromLocation(ResourceLocation* loc); virtual int getResourceCount(int status); virtual int purgeLoadedResources(); virtual void addResourceLoader(ResourceLoader* loader);
--- a/engine/core/video/image_location.cpp Sun Oct 12 19:23:10 2008 +0000 +++ b/engine/core/video/image_location.cpp Sun Oct 12 20:30:09 2008 +0000 @@ -74,32 +74,39 @@ return true; if( m_type > loc.getType() ) return false; - if( m_filename < loc.getFilename() ) - return true; - if( m_filename > loc.getFilename() ) - return false; const ImageLocation* r = dynamic_cast<const ImageLocation*>(&loc); if (!r) { - return true; - } - - if (m_xshift < r->m_xshift) { - return false; - } - if (m_yshift < r->m_yshift) { return false; } - if (m_width < r->m_width) { + + if(m_xshift < r->m_xshift) + return true; + if(m_xshift > r->m_xshift) return false; - } - if (m_height < r->m_height) { + + if(m_yshift < r->m_yshift) + return true; + if(m_yshift > r->m_yshift) return false; - } - if (m_parent_image < r->m_parent_image) { + + if(m_width < r->m_width) + return true; + if(m_width > r->m_width) return false; - } - return true; + + if(m_height < r->m_height) + return true; + if(m_height > r->m_height) + return false; + + + if( m_parent_image < r->m_parent_image ) + return true; + if( m_parent_image > r->m_parent_image ) + return false; + + return m_filename < loc.getFilename(); } ResourceLocation* ImageLocation::clone() const {
--- a/engine/core/video/video.i Sun Oct 12 19:23:10 2008 +0000 +++ b/engine/core/video/video.i Sun Oct 12 20:30:09 2008 +0000 @@ -108,7 +108,7 @@ class ImageLocation: public ResourceLocation { public: ImageLocation(const std::string& filename); - virtual ~ResourceLocation() {}; + virtual ~ImageLocation() {}; virtual void setXShift(int xshift) { m_xshift = xshift; } virtual int getXShift() const { return m_xshift; } virtual void setYShift(int yshift) { m_yshift = yshift; }
--- a/engine/extensions/serializers/xmlobject.py Sun Oct 12 19:23:10 2008 +0000 +++ b/engine/extensions/serializers/xmlobject.py Sun Oct 12 20:30:09 2008 +0000 @@ -99,11 +99,12 @@ path.pop() path.append(str(source)) - id = self.image_pool.addResourceFromFile('/'.join(path)) + image_location = fife.ImageLocation('/'.join(path)) + image_location .setXShift(int( image.get('x_offset', 0) )) + image_location .setYShift(int( image.get('y_offset', 0) )) + id = self.image_pool.addResourceFromLocation(image_location) object.get2dGfxVisual().addStaticImage(int( image.get('direction', 0) ), id) - img = self.image_pool.getImage(id) - img.setXShift(int( image.get('x_offset', 0) )) - img.setYShift(int( image.get('y_offset', 0) )) + #img = self.image_pool.getImage(id) def parse_actions(self, objelt, object): for action in objelt.findall('action'):
--- a/tests/core_tests/test_imagepool.cpp Sun Oct 12 19:23:10 2008 +0000 +++ b/tests/core_tests/test_imagepool.cpp Sun Oct 12 20:30:09 2008 +0000 @@ -83,12 +83,14 @@ CHECK_EQUAL(0, pool.getResourceCount(RES_LOADED)); CHECK_EQUAL(0, pool.getResourceCount(RES_NON_LOADED)); - pool.addResourceFromLocation(ImageLocation(IMAGE_FILE)); + ImageLocation location(IMAGE_FILE); + pool.addResourceFromLocation(&location); CHECK_EQUAL(0, pool.getResourceCount(RES_LOADED)); CHECK_EQUAL(1, pool.getResourceCount(RES_NON_LOADED)); - ImageLocation location(SUBIMAGE_FILE); + + location = ImageLocation(SUBIMAGE_FILE); ImageLoader imgprovider(vfs.get()); - int fullImgInd = pool.addResourceFromLocation(ImageLocation(SUBIMAGE_FILE)); + int fullImgInd = pool.addResourceFromLocation(&location); CHECK_EQUAL(0, pool.getResourceCount(RES_LOADED)); CHECK_EQUAL(2, pool.getResourceCount(RES_NON_LOADED)); Image& img = pool.getImage(fullImgInd); @@ -102,7 +104,10 @@ int h = H / 12; location.setWidth(w); location.setHeight(h); - pool.addResourceFromLocation(location); + CHECK(w != 0 && h !=0); + + int subImgInd = pool.addResourceFromLocation(&location); + CHECK(fullImgInd != subImgInd); CHECK_EQUAL(1, pool.getResourceCount(RES_LOADED)); CHECK_EQUAL(2, pool.getResourceCount(RES_NON_LOADED));