initial commit of rewrite of Resource system

This commit is contained in:
James Turk 2005-06-14 00:28:36 +00:00
parent 6e1eae1ed1
commit e0d488d2fd
7 changed files with 98 additions and 119 deletions

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // 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 #ifndef PHOTON_RESOURCEMANAGED_HPP
#define PHOTON_RESOURCEMANAGED_HPP #define PHOTON_RESOURCEMANAGED_HPP
@ -93,22 +93,20 @@ public:
static void addResource(const std::string& path); static void addResource(const std::string& path);
protected: protected:
std::string resName_;
static ResMgrT resMgr_; static ResMgrT resMgr_;
uint resID_;
}; };
//and he said "the implementation shall follow, as it is written" //and he said "the implementation shall follow, as it is written"
template<class ResMgrT> template<class ResMgrT>
ResourceManaged<ResMgrT>::ResourceManaged() : ResourceManaged<ResMgrT>::ResourceManaged()
resID_(Resource::InvalidID) { }
{
}
template<class ResMgrT> template<class ResMgrT>
ResourceManaged<ResMgrT>::ResourceManaged(const std::string& name) ResourceManaged<ResMgrT>::ResourceManaged(const std::string& name) :
{ resName_(name)
} { }
template<class ResMgrT> template<class ResMgrT>
ResourceManaged<ResMgrT>::~ResourceManaged() ResourceManaged<ResMgrT>::~ResourceManaged()
@ -122,11 +120,8 @@ ResourceManaged<ResMgrT>::operator=(const ResourceManaged<ResMgrT> &rhs)
{ {
if(this != &rhs) if(this != &rhs)
{ {
if(resID_ != Resource::InvalidID) release();
{ resName_ = rhs.resName_;
release();
}
resID_ = rhs.resID_;
} }
return *this; return *this;
} }
@ -135,14 +130,17 @@ template<class ResMgrT>
void ResourceManaged<ResMgrT>::open(const std::string& name) void ResourceManaged<ResMgrT>::open(const std::string& name)
{ {
release(); release();
resID_ = resMgr_.getResID(name); resName_ = name;
} }
template<class ResMgrT> template<class ResMgrT>
void ResourceManaged<ResMgrT>::release() void ResourceManaged<ResMgrT>::release()
{ {
resMgr_.delRef(resID_); if(!resName_.empty()) // release is a no-op on an invalid resource
resID_ = Resource::InvalidID; {
resMgr_.delRef(resName_); // decrement the refcount
resName_.clear(); // empty string = invalid resource
}
} }
template<class ResMgrT> template<class ResMgrT>
@ -164,6 +162,10 @@ void ResourceManaged<ResMgrT>::addResource(const std::string& path)
resMgr_.newResource(path,path); resMgr_.newResource(path,path);
} }
// define the resource manager static instance
template <class T>
T ResourceManaged<T>::resMgr_;
} }
#endif //PHOTON_RESOURCEMANAGED_HPP #endif //PHOTON_RESOURCEMANAGED_HPP

View File

@ -5,12 +5,12 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // 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 #ifndef PHOTON_RESOURCEMANAGER_HPP
#define PHOTON_RESOURCEMANAGER_HPP #define PHOTON_RESOURCEMANAGER_HPP
#include <vector> #include <map>
#include <string> #include <string>
#include <boost/utility.hpp> #include <boost/utility.hpp>
@ -24,13 +24,6 @@ namespace photon
class Resource class Resource
{ {
public: public:
static const uint InvalidID=0xffffffff;
Resource() :
refCount(0)
{
}
uint refCount; uint refCount;
std::string name; std::string name;
std::string path; std::string path;
@ -49,62 +42,47 @@ public:
virtual ~ResourceManager(); virtual ~ResourceManager();
uint getResID(const std::string& name); void delRef(const std::string& name);
void delRef(uint id);
void cleanUp(); 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: private:
virtual void loadResource(resT &res, const std::string& path)=0; virtual void loadResource(resT &res, const std::string& path)=0;
virtual void freeResource(resT &res)=0; virtual void freeResource(resT &res)=0;
void deleteResource(uint id); void deleteResource(const std::string& name);
protected: private:
std::vector<resT> resVec_; typedef std::map<std::string,resT> MapT;
typedef typename MapT::iterator MapIterator;
MapT resourceMap_;
}; };
// implementation (damn you templor, cruel god of templates!) // implementation (damn you templor, cruel god of templates!)
template<class resT> template<class resT>
ResourceManager<resT>::ResourceManager() ResourceManager<resT>::ResourceManager()
{ { }
}
template<class resT> template<class resT>
ResourceManager<resT>::~ResourceManager() ResourceManager<resT>::~ResourceManager()
{ { }
}
template<class resT> template<class resT>
uint ResourceManager<resT>::getResID(const std::string& name) void ResourceManager<resT>::delRef(const std::string& name)
{ {
uint id(0); MapIterator resource( resourceMap_.find(name) );
// loop through resources until the resource name in question is found
for(typename std::vector<resT>::iterator i=resVec_.begin();
i != resVec_.end() && i->name != name;
++i)
{
++id; // increment id
}
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 + "\""); if(--resource->second.refCount <= 0)
} {
deleteResource(name);
return id; }
}
template<class resT>
void ResourceManager<resT>::delRef(uint id)
{
// if decremented count is <= 0, delete resource
if(id < resVec_.size() && --resVec_[id].refCount <= 0)
{
deleteResource(id);
} }
} }
@ -112,26 +90,24 @@ template<class resT>
void ResourceManager<resT>::cleanUp() void ResourceManager<resT>::cleanUp()
{ {
// delete resources, until none are left // delete resources, until none are left
for(typename std::vector<resT>::iterator i=resVec_.begin(); while(!resourceMap_.empty())
i != resVec_.end();
++i)
{ {
freeResource(*i); freeResource(resourceMap_.begin()->second);
} }
} }
template<class resT> template<class resT>
uint ResourceManager<resT>::newResource(const std::string& name, void ResourceManager<resT>::newResource(const std::string& name,
const std::string& path) const std::string& path)
{ {
resT res; resT resource;
res.name = name; resource.name = name;
res.path = path; resource.path = path;
try try
{ {
// attempt to load // attempt to load
loadResource(res, path); loadResource(resource, path);
} }
catch(ResourceException& e) catch(ResourceException& e)
{ {
@ -140,21 +116,35 @@ uint ResourceManager<resT>::newResource(const std::string& name,
": " + e.what()); ": " + e.what());
} }
resVec_.push_back(res); // add resource to resVec & return resourceMap_[name] = resource; // add the resource to resourceMap
return static_cast<uint>(resVec_.size()-1);
} }
template<class resT> template<class resT>
void ResourceManager<resT>::deleteResource(uint id) resT& ResourceManager<resT>::getResource(const std::string& name)
{ {
// check boundaries MapIterator resource( resourceMap_.find(name) );
if(id >= resVec_.size())
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 template<class resT>
resVec_.erase(resVec_.begin()+id); void ResourceManager<resT>::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);
}
} }
} }

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // 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 #ifndef PHOTON_VIDEO_TEXTURERESOURCEMANAGER_HPP
#define PHOTON_VIDEO_TEXTURERESOURCEMANAGER_HPP #define PHOTON_VIDEO_TEXTURERESOURCEMANAGER_HPP
@ -34,7 +34,8 @@ public:
void getGlobalColorKey(bool &enabled, void getGlobalColorKey(bool &enabled,
ubyte &red, ubyte &green, ubyte &blue); 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: private:
virtual void loadResource(TextureResource &res, const std::string& name); virtual void loadResource(TextureResource &res, const std::string& name);

View File

@ -4,7 +4,7 @@
<cloud/> <cloud/>
<font BOLD="true" NAME="SansSerif" SIZE="12"/> <font BOLD="true" NAME="SansSerif" SIZE="12"/>
<node ID="_" POSITION="right" TEXT="0.1 Release"> <node ID="_" POSITION="right" TEXT="0.1 Release">
<arrowlink DESTINATION="Freemind_Link_511487087" ENDARROW="Default" ENDINCLINATION="114;-120;" ID="Freemind_Arrow_Link_1969744902" STARTARROW="None" STARTINCLINATION="-94;-430;"/> <arrowlink DESTINATION="Freemind_Link_511487087" ENDARROW="Default" ENDINCLINATION="67;0;" ID="Freemind_Arrow_Link_1969744902" STARTARROW="None" STARTINCLINATION="-28;-391;"/>
<font NAME="SansSerif" SIZE="12"/> <font NAME="SansSerif" SIZE="12"/>
<node ID="Freemind_Link_1613164220" TEXT="better SConstruct file"> <node ID="Freemind_Link_1613164220" TEXT="better SConstruct file">
<icon BUILTIN="button_ok"/> <icon BUILTIN="button_ok"/>
@ -16,7 +16,9 @@
</node> </node>
<node ID="Freemind_Link_50716011" TEXT="Texture"> <node ID="Freemind_Link_50716011" TEXT="Texture">
<icon BUILTIN="button_ok"/> <icon BUILTIN="button_ok"/>
<node ID="Freemind_Link_385334177" TEXT="Image"/> <node ID="Freemind_Link_385334177" TEXT="Image">
<icon BUILTIN="button_ok"/>
</node>
<node ID="Freemind_Link_561271163" TEXT="Font"/> <node ID="Freemind_Link_561271163" TEXT="Font"/>
</node> </node>
<node ID="Freemind_Link_1851655735" TEXT="Music"/> <node ID="Freemind_Link_1851655735" TEXT="Music"/>
@ -73,7 +75,6 @@
</node> </node>
<node ID="Freemind_Link_682620075" POSITION="left" TEXT="Current Problems"> <node ID="Freemind_Link_682620075" POSITION="left" TEXT="Current Problems">
<font BOLD="true" NAME="SansSerif" SIZE="12"/> <font BOLD="true" NAME="SansSerif" SIZE="12"/>
<node ID="Freemind_Link_71804323" TEXT="investigate revising resource system"/>
<node ID="Freemind_Link_1424720553" TEXT="Image is not tested at all"/> <node ID="Freemind_Link_1424720553" TEXT="Image is not tested at all"/>
</node> </node>
</node> </node>

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // 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" #include "video/Image.hpp"
@ -17,20 +17,16 @@ namespace video
{ {
Image::Image() : Image::Image() :
Texture(),
alpha_(255), alpha_(255),
texMinX_(0), texMinY_(0), texMaxX_(1), texMaxY_(1) texMinX_(0), texMinY_(0), texMaxX_(1), texMaxY_(1)
{ { }
}
Image::Image(const Image &rhs) : Image::Image(const Image &rhs) :
Texture(rhs), Texture(rhs),
alpha_(rhs.alpha_), alpha_(rhs.alpha_),
texMinX_(rhs.texMinX_), texMinY_(rhs.texMinY_), texMinX_(rhs.texMinX_), texMinY_(rhs.texMinY_),
texMaxX_(rhs.texMaxX_), texMaxY_(rhs.texMaxY_) texMaxX_(rhs.texMaxX_), texMaxY_(rhs.texMaxY_)
{ { }
}
Image::Image(const std::string& name) : Image::Image(const std::string& name) :
alpha_(255), alpha_(255),
@ -188,7 +184,7 @@ ubyte Image::getAlpha() const
std::ostream& operator<<(std::ostream &o, const Image &rhs) 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_ " Dimensions: " << rhs.width_ << "x" << rhs.height_
<< " Alpha: " << rhs.alpha_ << " }"; << " Alpha: " << rhs.alpha_ << " }";
} }

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // 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" #include "video/Texture.hpp"
@ -16,21 +16,13 @@ namespace photon
namespace video namespace video
{ {
template <class T>
T ResourceManaged<T>::resMgr_;
Texture::Texture() Texture::Texture()
{ { }
}
Texture::Texture(const Texture &rhs) Texture::Texture(const Texture &rhs) :
ResourceManaged<TextureResourceManager>(rhs)
{ {
resID_ = rhs.resID_; resMgr_.getTextureData(resName_, width_, height_, texID_);
resMgr_.getTextureData(resID_,width_,height_,texID_);
//w&h after getTextureData
width_ = rhs.width_;
height_ = rhs.height_;
} }
Texture::Texture(const std::string& name) Texture::Texture(const std::string& name)
@ -41,12 +33,11 @@ Texture::Texture(const std::string& name)
void Texture::open(const std::string& name) void Texture::open(const std::string& name)
{ {
ResourceManaged<TextureResourceManager>::open(name); ResourceManaged<TextureResourceManager>::open(name);
resMgr_.getTextureData(resID_, width_, height_, texID_); resMgr_.getTextureData(resName_, width_, height_, texID_);
} }
void Texture::bind() const void Texture::bind() const
{ {
if(glIsTexture(texID_) == GL_FALSE) if(glIsTexture(texID_) == GL_FALSE)
{ {
throw PreconditionException("Texture::bind call without valid image."); throw PreconditionException("Texture::bind call without valid image.");
@ -59,7 +50,7 @@ Texture& Texture::operator=(const Texture &rhs)
if(&rhs != this) if(&rhs != this)
{ {
ResourceManaged<TextureResourceManager>::operator=(rhs); ResourceManaged<TextureResourceManager>::operator=(rhs);
resMgr_.getTextureData(resID_,width_,height_,texID_); resMgr_.getTextureData(resName_, width_, height_, texID_);
//w&h after getTextureData //w&h after getTextureData
width_ = rhs.width_; width_ = rhs.width_;
@ -85,7 +76,7 @@ scalar Texture::getHeight() const
std::ostream& operator<<(std::ostream &o, const Texture &rhs) 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_ << " }"; << " Dimensions: " << rhs.width_ << "x" << rhs.height_ << " }";
} }

View File

@ -5,12 +5,12 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // 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 "video/TextureResourceManager.hpp"
#include "util/FileBuffer.hpp" #include "util/FileBuffer.hpp"
#include <iostream>
#include "GL/gl.h" #include "GL/gl.h"
#include "corona.h" #include "corona.h"
@ -34,16 +34,14 @@ void TextureResourceManager::getGlobalColorKey(bool &enabled, ubyte &red,
blue = colorKey_.blue; blue = colorKey_.blue;
} }
void TextureResourceManager::getTextureData(uint id, scalar &width, void TextureResourceManager::getTextureData(const std::string& name,
scalar &height, uint &texID) scalar &width, scalar &height,
uint &texID)
{ {
if(id < resVec_.size()) TextureResource resource( getResource(name) );
{ width = resource.width;
++resVec_[id].refCount; height = resource.height;
width = resVec_[id].width; texID = resource.texID;
height = resVec_[id].height;
texID = resVec_[id].texID;
}
} }
void TextureResourceManager::loadResource(TextureResource &res, void TextureResourceManager::loadResource(TextureResource &res,
@ -73,7 +71,7 @@ void TextureResourceManager::loadResource(TextureResource &res,
res.width = image->getWidth(); res.width = image->getWidth();
res.height = image->getHeight(); 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 //size to allocate = w*h*4 = size of bitmap * bytes per pixel
res.pixels = new ubyte[res.width*res.height*4]; res.pixels = new ubyte[res.width*res.height*4];
std::memcpy(res.pixels,image->getPixels(),res.width*res.height*4); std::memcpy(res.pixels,image->getPixels(),res.width*res.height*4);