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)
//
// 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<class ResMgrT>
ResourceManaged<ResMgrT>::ResourceManaged() :
resID_(Resource::InvalidID)
{
}
ResourceManaged<ResMgrT>::ResourceManaged()
{ }
template<class ResMgrT>
ResourceManaged<ResMgrT>::ResourceManaged(const std::string& name)
{
}
ResourceManaged<ResMgrT>::ResourceManaged(const std::string& name) :
resName_(name)
{ }
template<class ResMgrT>
ResourceManaged<ResMgrT>::~ResourceManaged()
@ -122,11 +120,8 @@ ResourceManaged<ResMgrT>::operator=(const ResourceManaged<ResMgrT> &rhs)
{
if(this != &rhs)
{
if(resID_ != Resource::InvalidID)
{
release();
}
resID_ = rhs.resID_;
release();
resName_ = rhs.resName_;
}
return *this;
}
@ -135,14 +130,17 @@ template<class ResMgrT>
void ResourceManaged<ResMgrT>::open(const std::string& name)
{
release();
resID_ = resMgr_.getResID(name);
resName_ = name;
}
template<class ResMgrT>
void ResourceManaged<ResMgrT>::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<class ResMgrT>
@ -164,6 +162,10 @@ void ResourceManaged<ResMgrT>::addResource(const std::string& path)
resMgr_.newResource(path,path);
}
// define the resource manager static instance
template <class T>
T ResourceManaged<T>::resMgr_;
}
#endif //PHOTON_RESOURCEMANAGED_HPP

View File

@ -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 <vector>
#include <map>
#include <string>
#include <boost/utility.hpp>
@ -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<resT> resVec_;
private:
typedef std::map<std::string,resT> MapT;
typedef typename MapT::iterator MapIterator;
MapT resourceMap_;
};
// implementation (damn you templor, cruel god of templates!)
template<class resT>
ResourceManager<resT>::ResourceManager()
{
}
{ }
template<class resT>
ResourceManager<resT>::~ResourceManager()
{
}
{ }
template<class resT>
uint ResourceManager<resT>::getResID(const std::string& name)
void ResourceManager<resT>::delRef(const std::string& name)
{
uint id(0);
// 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
}
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<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);
if(--resource->second.refCount <= 0)
{
deleteResource(name);
}
}
}
@ -112,26 +90,24 @@ template<class resT>
void ResourceManager<resT>::cleanUp()
{
// delete resources, until none are left
for(typename std::vector<resT>::iterator i=resVec_.begin();
i != resVec_.end();
++i)
while(!resourceMap_.empty())
{
freeResource(*i);
freeResource(resourceMap_.begin()->second);
}
}
template<class resT>
uint ResourceManager<resT>::newResource(const std::string& name,
void ResourceManager<resT>::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<resT>::newResource(const std::string& name,
": " + e.what());
}
resVec_.push_back(res); // add resource to resVec & return
return static_cast<uint>(resVec_.size()-1);
resourceMap_[name] = resource; // add the resource to resourceMap
}
template<class resT>
void ResourceManager<resT>::deleteResource(uint id)
resT& ResourceManager<resT>::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<class resT>
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)
//
// 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);

View File

@ -4,7 +4,7 @@
<cloud/>
<font BOLD="true" NAME="SansSerif" SIZE="12"/>
<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"/>
<node ID="Freemind_Link_1613164220" TEXT="better SConstruct file">
<icon BUILTIN="button_ok"/>
@ -16,7 +16,9 @@
</node>
<node ID="Freemind_Link_50716011" TEXT="Texture">
<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>
<node ID="Freemind_Link_1851655735" TEXT="Music"/>
@ -73,7 +75,6 @@
</node>
<node ID="Freemind_Link_682620075" POSITION="left" TEXT="Current Problems">
<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>
</node>

View File

@ -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_ << " }";
}

View File

@ -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 <class T>
T ResourceManaged<T>::resMgr_;
Texture::Texture()
{
}
{ }
Texture::Texture(const Texture &rhs)
Texture::Texture(const Texture &rhs) :
ResourceManaged<TextureResourceManager>(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<TextureResourceManager>::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<TextureResourceManager>::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_ << " }";
}

View File

@ -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 <iostream>
#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);