From e0d488d2fd32d56c9e82c51638f5c0654231ea6d Mon Sep 17 00:00:00 2001 From: James Turk Date: Tue, 14 Jun 2005 00:28:36 +0000 Subject: [PATCH] initial commit of rewrite of Resource system --- include/ResourceManaged.hpp | 36 ++++---- include/ResourceManager.hpp | 110 +++++++++++------------ include/video/TextureResourceManager.hpp | 5 +- photon.mm | 7 +- src/video/Image.cpp | 12 +-- src/video/Texture.cpp | 25 ++---- src/video/TextureResourceManager.cpp | 22 +++-- 7 files changed, 98 insertions(+), 119 deletions(-) diff --git a/include/ResourceManaged.hpp b/include/ResourceManaged.hpp index a7d0f20..2c126a8 100644 --- a/include/ResourceManaged.hpp +++ b/include/ResourceManaged.hpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: ResourceManaged.hpp,v 1.3 2005/06/10 07:06:06 cozman Exp $ +// $Id: ResourceManaged.hpp,v 1.4 2005/06/14 00:28:36 cozman Exp $ #ifndef PHOTON_RESOURCEMANAGED_HPP #define PHOTON_RESOURCEMANAGED_HPP @@ -93,22 +93,20 @@ public: static void addResource(const std::string& path); protected: + std::string resName_; static ResMgrT resMgr_; - uint resID_; }; //and he said "the implementation shall follow, as it is written" template -ResourceManaged::ResourceManaged() : - resID_(Resource::InvalidID) -{ -} +ResourceManaged::ResourceManaged() +{ } template -ResourceManaged::ResourceManaged(const std::string& name) -{ -} +ResourceManaged::ResourceManaged(const std::string& name) : + resName_(name) +{ } template ResourceManaged::~ResourceManaged() @@ -122,11 +120,8 @@ ResourceManaged::operator=(const ResourceManaged &rhs) { if(this != &rhs) { - if(resID_ != Resource::InvalidID) - { - release(); - } - resID_ = rhs.resID_; + release(); + resName_ = rhs.resName_; } return *this; } @@ -135,14 +130,17 @@ template void ResourceManaged::open(const std::string& name) { release(); - resID_ = resMgr_.getResID(name); + resName_ = name; } template void ResourceManaged::release() { - resMgr_.delRef(resID_); - resID_ = Resource::InvalidID; + if(!resName_.empty()) // release is a no-op on an invalid resource + { + resMgr_.delRef(resName_); // decrement the refcount + resName_.clear(); // empty string = invalid resource + } } template @@ -164,6 +162,10 @@ void ResourceManaged::addResource(const std::string& path) resMgr_.newResource(path,path); } +// define the resource manager static instance +template +T ResourceManaged::resMgr_; + } #endif //PHOTON_RESOURCEMANAGED_HPP diff --git a/include/ResourceManager.hpp b/include/ResourceManager.hpp index f0feb03..1b23691 100644 --- a/include/ResourceManager.hpp +++ b/include/ResourceManager.hpp @@ -5,12 +5,12 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: ResourceManager.hpp,v 1.5 2005/06/13 05:38:06 cozman Exp $ +// $Id: ResourceManager.hpp,v 1.6 2005/06/14 00:28:36 cozman Exp $ #ifndef PHOTON_RESOURCEMANAGER_HPP #define PHOTON_RESOURCEMANAGER_HPP -#include +#include #include #include @@ -24,13 +24,6 @@ namespace photon class Resource { public: - static const uint InvalidID=0xffffffff; - - Resource() : - refCount(0) - { - } - uint refCount; std::string name; std::string path; @@ -49,62 +42,47 @@ public: virtual ~ResourceManager(); - uint getResID(const std::string& name); - void delRef(uint id); + void delRef(const std::string& name); void cleanUp(); - uint newResource(const std::string& name, const std::string& path); + void newResource(const std::string& name, const std::string& path); + + resT& getResource(const std::string& name); private: virtual void loadResource(resT &res, const std::string& path)=0; virtual void freeResource(resT &res)=0; - void deleteResource(uint id); + void deleteResource(const std::string& name); -protected: - std::vector resVec_; +private: + typedef std::map MapT; + typedef typename MapT::iterator MapIterator; + MapT resourceMap_; }; // implementation (damn you templor, cruel god of templates!) template ResourceManager::ResourceManager() -{ -} +{ } template ResourceManager::~ResourceManager() -{ -} +{ } template -uint ResourceManager::getResID(const std::string& name) +void ResourceManager::delRef(const std::string& name) { - uint id(0); - - // loop through resources until the resource name in question is found - for(typename std::vector::iterator i=resVec_.begin(); - i != resVec_.end() && i->name != name; - ++i) - { - ++id; // increment id - } + MapIterator resource( resourceMap_.find(name) ); - if(id == resVec_.size()) // not found -> throw a ResourceException + // if the resource was found + if(resource != resourceMap_.end()) { - throw ResourceException("Failed to find resource \"" + name + "\""); - } - - return id; -} - -template -void ResourceManager::delRef(uint id) -{ - // if decremented count is <= 0, delete resource - if(id < resVec_.size() && --resVec_[id].refCount <= 0) - { - deleteResource(id); + if(--resource->second.refCount <= 0) + { + deleteResource(name); + } } } @@ -112,26 +90,24 @@ template void ResourceManager::cleanUp() { // delete resources, until none are left - for(typename std::vector::iterator i=resVec_.begin(); - i != resVec_.end(); - ++i) + while(!resourceMap_.empty()) { - freeResource(*i); + freeResource(resourceMap_.begin()->second); } } template -uint ResourceManager::newResource(const std::string& name, +void ResourceManager::newResource(const std::string& name, const std::string& path) { - resT res; - res.name = name; - res.path = path; + resT resource; + resource.name = name; + resource.path = path; try { // attempt to load - loadResource(res, path); + loadResource(resource, path); } catch(ResourceException& e) { @@ -140,21 +116,35 @@ uint ResourceManager::newResource(const std::string& name, ": " + e.what()); } - resVec_.push_back(res); // add resource to resVec & return - return static_cast(resVec_.size()-1); + resourceMap_[name] = resource; // add the resource to resourceMap } template -void ResourceManager::deleteResource(uint id) +resT& ResourceManager::getResource(const std::string& name) { - // check boundaries - if(id >= resVec_.size()) + MapIterator resource( resourceMap_.find(name) ); + + if(resource != resourceMap_.end()) { - throw RangeException("Attempt to delete invalid resource."); + return resource->second; } + else + { + throw ResourceException(); + } +} - freeResource(resVec_[id]); // free the resource and erase it from the vec - resVec_.erase(resVec_.begin()+id); +template +void ResourceManager::deleteResource(const std::string& name) +{ + MapIterator resource( resourceMap_.find(name) ); + + // if the resource was found + if(resource != resourceMap_.end()) + { + freeResource(resource->second); // free resource and remove it from the map + resourceMap_.erase(name); + } } } diff --git a/include/video/TextureResourceManager.hpp b/include/video/TextureResourceManager.hpp index 793a72c..cd90404 100644 --- a/include/video/TextureResourceManager.hpp +++ b/include/video/TextureResourceManager.hpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: TextureResourceManager.hpp,v 1.1 2005/06/13 05:38:06 cozman Exp $ +// $Id: TextureResourceManager.hpp,v 1.2 2005/06/14 00:28:36 cozman Exp $ #ifndef PHOTON_VIDEO_TEXTURERESOURCEMANAGER_HPP #define PHOTON_VIDEO_TEXTURERESOURCEMANAGER_HPP @@ -34,7 +34,8 @@ public: void getGlobalColorKey(bool &enabled, ubyte &red, ubyte &green, ubyte &blue); - void getTextureData(uint id, scalar &width, scalar &height, uint &texID); + void getTextureData(const std::string& name, scalar &width, scalar &height, + uint &texID); private: virtual void loadResource(TextureResource &res, const std::string& name); diff --git a/photon.mm b/photon.mm index e557c68..b31ac8a 100644 --- a/photon.mm +++ b/photon.mm @@ -4,7 +4,7 @@ - + @@ -16,7 +16,9 @@ - + + + @@ -73,7 +75,6 @@ - diff --git a/src/video/Image.cpp b/src/video/Image.cpp index 5c536d8..4cdc134 100644 --- a/src/video/Image.cpp +++ b/src/video/Image.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: Image.cpp,v 1.1 2005/06/13 07:04:29 cozman Exp $ +// $Id: Image.cpp,v 1.2 2005/06/14 00:28:36 cozman Exp $ #include "video/Image.hpp" @@ -17,20 +17,16 @@ namespace video { Image::Image() : - Texture(), alpha_(255), texMinX_(0), texMinY_(0), texMaxX_(1), texMaxY_(1) -{ -} +{ } Image::Image(const Image &rhs) : Texture(rhs), alpha_(rhs.alpha_), texMinX_(rhs.texMinX_), texMinY_(rhs.texMinY_), texMaxX_(rhs.texMaxX_), texMaxY_(rhs.texMaxY_) -{ - -} +{ } Image::Image(const std::string& name) : alpha_(255), @@ -188,7 +184,7 @@ ubyte Image::getAlpha() const std::ostream& operator<<(std::ostream &o, const Image &rhs) { - return o << "Image: { ResID: " << rhs.resID_ << + return o << "Image: { Name: " << rhs.resName_ << " Dimensions: " << rhs.width_ << "x" << rhs.height_ << " Alpha: " << rhs.alpha_ << " }"; } diff --git a/src/video/Texture.cpp b/src/video/Texture.cpp index c254f8a..db4077b 100644 --- a/src/video/Texture.cpp +++ b/src/video/Texture.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: Texture.cpp,v 1.1 2005/06/13 05:38:06 cozman Exp $ +// $Id: Texture.cpp,v 1.2 2005/06/14 00:28:36 cozman Exp $ #include "video/Texture.hpp" @@ -16,21 +16,13 @@ namespace photon namespace video { -template -T ResourceManaged::resMgr_; - Texture::Texture() -{ -} +{ } -Texture::Texture(const Texture &rhs) +Texture::Texture(const Texture &rhs) : + ResourceManaged(rhs) { - resID_ = rhs.resID_; - resMgr_.getTextureData(resID_,width_,height_,texID_); - - //w&h after getTextureData - width_ = rhs.width_; - height_ = rhs.height_; + resMgr_.getTextureData(resName_, width_, height_, texID_); } Texture::Texture(const std::string& name) @@ -41,12 +33,11 @@ Texture::Texture(const std::string& name) void Texture::open(const std::string& name) { ResourceManaged::open(name); - resMgr_.getTextureData(resID_, width_, height_, texID_); + resMgr_.getTextureData(resName_, width_, height_, texID_); } void Texture::bind() const { - if(glIsTexture(texID_) == GL_FALSE) { throw PreconditionException("Texture::bind call without valid image."); @@ -59,7 +50,7 @@ Texture& Texture::operator=(const Texture &rhs) if(&rhs != this) { ResourceManaged::operator=(rhs); - resMgr_.getTextureData(resID_,width_,height_,texID_); + resMgr_.getTextureData(resName_, width_, height_, texID_); //w&h after getTextureData width_ = rhs.width_; @@ -85,7 +76,7 @@ scalar Texture::getHeight() const std::ostream& operator<<(std::ostream &o, const Texture &rhs) { - return o << "Texture: { ResID: " << rhs.resID_ << " TexID: " << rhs.texID_ + return o << "Texture: { Name: " << rhs.resName_ << " TexID: " << rhs.texID_ << " Dimensions: " << rhs.width_ << "x" << rhs.height_ << " }"; } diff --git a/src/video/TextureResourceManager.cpp b/src/video/TextureResourceManager.cpp index 382c2e2..d7c0880 100644 --- a/src/video/TextureResourceManager.cpp +++ b/src/video/TextureResourceManager.cpp @@ -5,12 +5,12 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: TextureResourceManager.cpp,v 1.1 2005/06/13 05:38:06 cozman Exp $ +// $Id: TextureResourceManager.cpp,v 1.2 2005/06/14 00:28:36 cozman Exp $ #include "video/TextureResourceManager.hpp" #include "util/FileBuffer.hpp" -#include + #include "GL/gl.h" #include "corona.h" @@ -34,16 +34,14 @@ void TextureResourceManager::getGlobalColorKey(bool &enabled, ubyte &red, blue = colorKey_.blue; } -void TextureResourceManager::getTextureData(uint id, scalar &width, - scalar &height, uint &texID) +void TextureResourceManager::getTextureData(const std::string& name, + scalar &width, scalar &height, + uint &texID) { - if(id < resVec_.size()) - { - ++resVec_[id].refCount; - width = resVec_[id].width; - height = resVec_[id].height; - texID = resVec_[id].texID; - } + TextureResource resource( getResource(name) ); + width = resource.width; + height = resource.height; + texID = resource.texID; } void TextureResourceManager::loadResource(TextureResource &res, @@ -73,7 +71,7 @@ void TextureResourceManager::loadResource(TextureResource &res, res.width = image->getWidth(); res.height = image->getHeight(); - std::cerr << res.width << "x" << res.height << std::endl; + //size to allocate = w*h*4 = size of bitmap * bytes per pixel res.pixels = new ubyte[res.width*res.height*4]; std::memcpy(res.pixels,image->getPixels(),res.width*res.height*4);