ResourceManage-ment functional, not cleaned yet

This commit is contained in:
James Turk 2005-07-03 06:33:19 +00:00
parent 5ca1c67cba
commit 3258fa09cf
13 changed files with 100 additions and 137 deletions

View File

@ -5,7 +5,7 @@
# James Turk (jpt2433@rit.edu) # James Turk (jpt2433@rit.edu)
# #
# Version: # Version:
# $Id: SConstruct,v 1.13 2005/06/29 04:30:40 cozman Exp $ # $Id: SConstruct,v 1.14 2005/07/03 06:33:19 cozman Exp $
import os,os.path import os,os.path
import glob import glob
@ -91,6 +91,6 @@ for test_src in test_srcs:
test_name = test_src.replace('_test.cpp','') test_name = test_src.replace('_test.cpp','')
tests.append(env.Program(test_name, source=test_src, CPPPATH = INC_DIRS+['/usr/include/freetype2/'], tests.append(env.Program(test_name, source=test_src, CPPPATH = INC_DIRS+['/usr/include/freetype2/'],
LIBPATH='./lib', CPPFLAGS = '-Wall -pedantic', LIBPATH='./lib', CPPFLAGS = '-Wall -pedantic',
LIBS=['photon',OAL_LIB,'glfw',OGL_LIB,GLU_LIB,'physfs','corona','freetype','ftgl'])) LIBS=['photon',OAL_LIB,'glfw',OGL_LIB,GLU_LIB,'physfs','corona','freetype']))
env.Alias('test',tests) env.Alias('test',tests)

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: ResourceManaged.hpp,v 1.5 2005/06/27 04:24:09 cozman Exp $ // $Id: ResourceManaged.hpp,v 1.6 2005/07/03 06:33:19 cozman Exp $
#ifndef PHOTON_RESOURCEMANAGED_HPP #ifndef PHOTON_RESOURCEMANAGED_HPP
#define PHOTON_RESOURCEMANAGED_HPP #define PHOTON_RESOURCEMANAGED_HPP
@ -92,7 +92,8 @@ public:
// Parameters: // Parameters:
// name - Name to give to resource. // name - Name to give to resource.
// path - Path of resource data file. // path - Path of resource data file.
static void addResource(const std::string& name, const std::string& path); static void addResource(const std::string& name,
const typename ResMgrT::ResDescT& desc);
// Function: addResource // Function: addResource
// Define a new unaliased resource. (name == path). // Define a new unaliased resource. (name == path).
@ -101,7 +102,7 @@ public:
// //
// Parameters:. // Parameters:.
// path - Path of resource data file. // path - Path of resource data file.
static void addResource(const std::string& path); static void addResource(const typename ResMgrT::ResDescT& desc);
private: private:
std::string resName_; std::string resName_;
@ -167,15 +168,15 @@ void ResourceManaged<ResMgrT>::cleanUp()
template<class ResMgrT> template<class ResMgrT>
void ResourceManaged<ResMgrT>::addResource(const std::string& name, void ResourceManaged<ResMgrT>::addResource(const std::string& name,
const std::string& path) const typename ResMgrT::ResDescT& desc)
{ {
resMgr_.newResource(name,path); resMgr_.newResource(name, desc);
} }
template<class ResMgrT> template<class ResMgrT>
void ResourceManaged<ResMgrT>::addResource(const std::string& path) void ResourceManaged<ResMgrT>::addResource(const typename ResMgrT::ResDescT& desc)
{ {
resMgr_.newResource(path,path); resMgr_.newResource(desc.path, desc);
} }
// define the resource manager static instance // define the resource manager static instance

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: ResourceManager.hpp,v 1.7 2005/06/27 04:24:16 cozman Exp $ // $Id: ResourceManager.hpp,v 1.8 2005/07/03 06:33:19 cozman Exp $
#ifndef PHOTON_RESOURCEMANAGER_HPP #ifndef PHOTON_RESOURCEMANAGER_HPP
#define PHOTON_RESOURCEMANAGER_HPP #define PHOTON_RESOURCEMANAGER_HPP
@ -25,7 +25,15 @@ class Resource
{ {
public: public:
uint refCount; uint refCount;
std::string name; };
class ResourceDescriptor
{
public:
ResourceDescriptor() { }
ResourceDescriptor(const std::string& p) :
path(p)
{ }
std::string path; std::string path;
}; };
@ -34,9 +42,12 @@ public:
// //
// All ResourceManager work is done behind the scenes, it and all classes // All ResourceManager work is done behind the scenes, it and all classes
// derived from it are therefore left without public documentation. // derived from it are therefore left without public documentation.
template<class resT> template<class resT, class ResDescT_=ResourceDescriptor>
class ResourceManager : public boost::noncopyable class ResourceManager : public boost::noncopyable
{ {
public:
typedef ResDescT_ ResDescT;
public: public:
ResourceManager(); ResourceManager();
@ -45,13 +56,13 @@ public:
void delRef(const std::string& name); void delRef(const std::string& name);
void cleanUp(); void cleanUp();
void newResource(const std::string& name, const std::string& path); void newResource(const std::string& name, const ResDescT& path);
resT& getResource(const std::string& name); resT& getResource(const std::string& name);
private: private:
virtual void loadResource(resT &res, const std::string& path)=0; virtual void loadResourceData(resT &res, const ResDescT& path)=0;
virtual void freeResource(resT &res)=0; virtual void freeResourceData(resT &res)=0;
void deleteResource(const std::string& name); void deleteResource(const std::string& name);
@ -66,16 +77,16 @@ private:
// implementation (damn you templor, cruel god of templates!) // implementation (damn you templor, cruel god of templates!)
template<class resT> template<class resT, class ResDescT>
ResourceManager<resT>::ResourceManager() ResourceManager<resT, ResDescT>::ResourceManager()
{ } { }
template<class resT> template<class resT, class ResDescT>
ResourceManager<resT>::~ResourceManager() ResourceManager<resT, ResDescT>::~ResourceManager()
{ } { }
template<class resT> template<class resT, class ResDescT>
void ResourceManager<resT>::delRef(const std::string& name) void ResourceManager<resT, ResDescT>::delRef(const std::string& name)
{ {
MapIterator resource( resourceMap_.find(name) ); MapIterator resource( resourceMap_.find(name) );
@ -89,41 +100,39 @@ void ResourceManager<resT>::delRef(const std::string& name)
} }
} }
template<class resT> template<class resT, class ResDescT>
void ResourceManager<resT>::cleanUp() void ResourceManager<resT, ResDescT>::cleanUp()
{ {
// delete resources, until none are left // delete resources, until none are left
while(!resourceMap_.empty()) while(!resourceMap_.empty())
{ {
freeResource(resourceMap_.begin()->second); freeResourceData(resourceMap_.begin()->second);
} }
} }
template<class resT> template<class resT, class ResDescT>
void ResourceManager<resT>::newResource(const std::string& name, void ResourceManager<resT, ResDescT>::newResource(const std::string& name,
const std::string& path) const ResDescT& desc)
{ {
resT resource; resT resource;
resource.name = name;
resource.path = path;
try try
{ {
// attempt to load // attempt to load
loadResource(resource, path); loadResourceData(resource, desc);
} }
catch(ResourceException& e) catch(ResourceException& e)
{ {
// rethrow any exceptions with specific information // rethrow any exceptions with specific information
throw ResourceException("Could not load " + path + " as " + name + throw ResourceException("Could not load " + desc.path + " as " + name +
": " + e.getDesc()); ": " + e.getDesc());
} }
resourceMap_[name] = resource; // add the resource to resourceMap resourceMap_[name] = resource; // add the resource to resourceMap
} }
template<class resT> template<class resT, class ResDescT>
resT& ResourceManager<resT>::getResource(const std::string& name) resT& ResourceManager<resT, ResDescT>::getResource(const std::string& name)
{ {
MapIterator resource( resourceMap_.find(name) ); MapIterator resource( resourceMap_.find(name) );
@ -139,21 +148,22 @@ resT& ResourceManager<resT>::getResource(const std::string& name)
} }
} }
template<class resT> template<class resT, class ResDescT>
void ResourceManager<resT>::deleteResource(const std::string& name) void ResourceManager<resT, ResDescT>::deleteResource(const std::string& name)
{ {
MapIterator resource( resourceMap_.find(name) ); MapIterator resource( resourceMap_.find(name) );
// if the resource was found // if the resource was found
if(resource != resourceMap_.end()) if(resource != resourceMap_.end())
{ {
freeResource(resource->second); // free resource and remove it from the map // free resource and remove it from the map
freeResourceData(resource->second);
resourceMap_.erase(name); resourceMap_.erase(name);
} }
} }
template<class resT> template<class resT, class ResDescT>
void ResourceManager<resT>::printReport(std::ostream& os) void ResourceManager<resT, ResDescT>::printReport(std::ostream& os)
{ {
MapIterator resource( resourceMap_.begin() ); MapIterator resource( resourceMap_.begin() );

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: Font.hpp,v 1.2 2005/07/03 05:20:49 cozman Exp $ // $Id: Font.hpp,v 1.3 2005/07/03 06:33:19 cozman Exp $
#ifndef PHOTON_VIDEO_FONT_HPP #ifndef PHOTON_VIDEO_FONT_HPP
#define PHOTON_VIDEO_FONT_HPP #define PHOTON_VIDEO_FONT_HPP
@ -34,29 +34,6 @@ std::ostream& operator<<(std::ostream& os, const StreamFlusher& rhs);
// - ostream& << Font // - ostream& << Font
class Font: public ResourceManaged<FontResourceManager> class Font: public ResourceManaged<FontResourceManager>
{ {
// Resource Creation
public:
// Function: addResource
// Define a new named font resource, works just like
// <ResourceManaged::addResource> except that it takes a size for the font.
//
// Parameters:
// name - Name to give to font resource.
// path - Path of font file.
// size - Point size for the font.
static void addResource(const std::string& name, const std::string& path,
uint size);
// Function: addResource
// Define a new unaliased font resource (name == path). Works just like
// <ResourceManaged::addResource> except that it takes a size for the font.
//
// Parameters:.
// path - Path of font file.
// size - Point size for the font.
static void addResource(const std::string& path, uint size);
// Group: (Con/De)structors // Group: (Con/De)structors
public: public:
// Function: Font // Function: Font

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: FontResourceManager.hpp,v 1.2 2005/07/03 05:20:49 cozman Exp $ // $Id: FontResourceManager.hpp,v 1.3 2005/07/03 06:33:19 cozman Exp $
#ifndef PHOTON_VIDEO_FONTRESOURCEMANAGER_HPP #ifndef PHOTON_VIDEO_FONTRESOURCEMANAGER_HPP
#define PHOTON_VIDEO_FONTRESOURCEMANAGER_HPP #define PHOTON_VIDEO_FONTRESOURCEMANAGER_HPP
@ -29,7 +29,19 @@ public:
ubyte height; ubyte height;
}; };
class FontResourceManager : public ResourceManager<FontResource> class FontResourceDescriptor : public ResourceDescriptor
{
public:
FontResourceDescriptor(const std::string& str, uint sz) :
ResourceDescriptor(str), size(sz)
{ }
public:
uint size;
};
class FontResourceManager : public ResourceManager<FontResource,
FontResourceDescriptor>
{ {
public: public:
FontResourceManager(); FontResourceManager();
@ -40,15 +52,9 @@ public:
private: private:
// defined but not implemented virtual void loadResourceData(FontResource &res,
virtual void loadResource(FontResource &res, const std::string& name); const FontResourceDescriptor& desc);
virtual void freeResourceData(FontResource &res);
FontResource newResource(const std::string& name, const std::string& path,
uint size);
virtual void loadResource(FontResource &res, const std::string& name,
uint size);
virtual void freeResource(FontResource &res);
private: private:
FT_Library library_; FT_Library library_;

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: TextureResourceManager.hpp,v 1.2 2005/06/14 00:28:36 cozman Exp $ // $Id: TextureResourceManager.hpp,v 1.3 2005/07/03 06:33:19 cozman Exp $
#ifndef PHOTON_VIDEO_TEXTURERESOURCEMANAGER_HPP #ifndef PHOTON_VIDEO_TEXTURERESOURCEMANAGER_HPP
#define PHOTON_VIDEO_TEXTURERESOURCEMANAGER_HPP #define PHOTON_VIDEO_TEXTURERESOURCEMANAGER_HPP
@ -38,8 +38,9 @@ public:
uint &texID); uint &texID);
private: private:
virtual void loadResource(TextureResource &res, const std::string& name); virtual void loadResourceData(TextureResource &res,
virtual void freeResource(TextureResource &res); const ResourceDescriptor& path);
virtual void freeResourceData(TextureResource &res);
Color colorKey_; Color colorKey_;
}; };

View File

@ -12,6 +12,11 @@
<font NAME="SansSerif" SIZE="12"/> <font NAME="SansSerif" SIZE="12"/>
<node ID="Freemind_Link_1795651487" TEXT="&quot;ResourceManage&quot;ment"> <node ID="Freemind_Link_1795651487" TEXT="&quot;ResourceManage&quot;ment">
<icon BUILTIN="button_ok"/> <icon BUILTIN="button_ok"/>
<node ID="Freemind_Link_1726195047" TEXT="clean up!">
<node ID="Freemind_Link_1703292276" TEXT="hide ResourceDescriptors"/>
<node ID="Freemind_Link_1167143207" TEXT="scan documentation"/>
<node ID="Freemind_Link_513005450" TEXT="80-line limit"/>
</node>
</node> </node>
<node ID="Freemind_Link_50716011" TEXT="Texture"> <node ID="Freemind_Link_50716011" TEXT="Texture">
<icon BUILTIN="button_ok"/> <icon BUILTIN="button_ok"/>
@ -69,9 +74,13 @@
</node> </node>
<node ID="Freemind_Link_1642641448" TEXT="Ensure compilation succeeds on Win/Linux simultaneously"/> <node ID="Freemind_Link_1642641448" TEXT="Ensure compilation succeeds on Win/Linux simultaneously"/>
<node ID="Freemind_Link_330674889" TEXT="Find an OSX user to test"/> <node ID="Freemind_Link_330674889" TEXT="Find an OSX user to test"/>
<node ID="Freemind_Link_1190200631" TEXT="Clean up source"/>
</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> </node>
<node COLOR="#147f1e" ID="Freemind_Link_438641521" POSITION="left" TEXT="Version: $Id: photon.mm,v 1.10 2005/07/03 06:33:19 cozman Exp $">
<font ITALIC="true" NAME="SansSerif" SIZE="12"/>
</node>
</node> </node>
</map> </map>

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: Font.cpp,v 1.2 2005/07/03 05:20:49 cozman Exp $ // $Id: Font.cpp,v 1.3 2005/07/03 06:33:19 cozman Exp $
#include "video/Font.hpp" #include "video/Font.hpp"
@ -25,17 +25,6 @@ std::ostream& operator<<(std::ostream& os, const StreamFlusher& rhs)
return os.flush(); return os.flush();
} }
void Font::addResource(const std::string& name, const std::string& path,
uint size)
{
resMgr_.newResource(name,path,size);
}
void Font::addResource(const std::string& path, uint size)
{
resMgr_.newResource(path,path,size);
}
Font::Font() : Font::Font() :
texID_(0), listBase_(0), // initalize GL variables to zero texID_(0), listBase_(0), // initalize GL variables to zero
widths_(FontResourceManager::NUM_CHARS), // make room for 96 widths widths_(FontResourceManager::NUM_CHARS), // make room for 96 widths

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: FontResourceManager.cpp,v 1.2 2005/07/03 05:20:49 cozman Exp $ // $Id: FontResourceManager.cpp,v 1.3 2005/07/03 06:33:19 cozman Exp $
#include "video/FontResourceManager.hpp" #include "video/FontResourceManager.hpp"
@ -41,45 +41,15 @@ void FontResourceManager::getFontData(const std::string& name, uint& texID,
height = resource.height; height = resource.height;
} }
void FontResourceManager::loadResource(FontResource &res, void FontResourceManager::loadResourceData(FontResource &res,
const std::string& path) const FontResourceDescriptor& desc)
{
throw Error("loadResource(FontResource&, const std::string& is undefined "
" for Font. A size must be specified.");
}
FontResource FontResourceManager::newResource(const std::string& name,
const std::string& path,
uint size)
{
FontResource resource;
resource.name = name;
resource.path = path;
try
{
// attempt to load
loadResource(resource, path, size);
}
catch(ResourceException& e)
{
// rethrow any exceptions with specific information
throw ResourceException("Could not load " + path + " as " + name +
": " + e.getDesc());
}
resourceMap_[name] = resource; // add the resource to resourceMap
}
void FontResourceManager::loadResource(FontResource &res,
const std::string& path, uint size)
{ {
const size_t MARGIN = 3; const size_t MARGIN = 3;
res.widths.resize(NUM_CHARS); res.widths.resize(NUM_CHARS);
// Step 1: Open the font using FreeType // // Step 1: Open the font using FreeType //
util::FileBuffer buf(path); util::FileBuffer buf(desc.path);
std::vector<ubyte> data = buf.getData(); std::vector<ubyte> data = buf.getData();
FT_Face face; FT_Face face;
@ -98,7 +68,7 @@ void FontResourceManager::loadResource(FontResource &res,
} }
// Set the font size // Set the font size
FT_Set_Pixel_Sizes(face, size, 0); FT_Set_Pixel_Sizes(face, desc.size, 0);
// Step 2: Find maxAscent/Descent to calculate imageHeight // // Step 2: Find maxAscent/Descent to calculate imageHeight //
size_t imageHeight = 0; size_t imageHeight = 0;
@ -216,7 +186,7 @@ void FontResourceManager::loadResource(FontResource &res,
FT_Done_Face(face); // free the face data FT_Done_Face(face); // free the face data
} }
void FontResourceManager::freeResource(FontResource &res) void FontResourceManager::freeResourceData(FontResource &res)
{ {
if(glIsList(res.listBase)) if(glIsList(res.listBase))
{ {

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: TextureResourceManager.cpp,v 1.3 2005/06/27 04:24:16 cozman Exp $ // $Id: TextureResourceManager.cpp,v 1.4 2005/07/03 06:33:19 cozman Exp $
#include "video/TextureResourceManager.hpp" #include "video/TextureResourceManager.hpp"
@ -44,11 +44,11 @@ void TextureResourceManager::getTextureData(const std::string& name,
texID = resource.texID; texID = resource.texID;
} }
void TextureResourceManager::loadResource(TextureResource &res, void TextureResourceManager::loadResourceData(TextureResource &res,
const std::string& path) const ResourceDescriptor& path)
{ {
corona::Image *image(0); corona::Image *image(0);
util::FileBuffer buf(path); util::FileBuffer buf(path.path);
corona::File *file; corona::File *file;
std::vector<ubyte> data = buf.getData(); std::vector<ubyte> data = buf.getData();
@ -108,7 +108,7 @@ void TextureResourceManager::loadResource(TextureResource &res,
GL_UNSIGNED_BYTE, res.pixels); GL_UNSIGNED_BYTE, res.pixels);
} }
void TextureResourceManager::freeResource(TextureResource &res) void TextureResourceManager::freeResourceData(TextureResource &res)
{ {
if(res.pixels) if(res.pixels)
{ {

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: Font_test.cpp,v 1.2 2005/07/03 05:20:49 cozman Exp $ // $Id: Font_test.cpp,v 1.3 2005/07/03 06:33:19 cozman Exp $
#include "photon.hpp" #include "photon.hpp"
using namespace photon; using namespace photon;
@ -30,7 +30,7 @@ public:
video.setOrthoView(800,600); video.setOrthoView(800,600);
video::Font::addResource("font","data/arial.ttf"); video::Font::addResource("font",video::FontResourceDescriptor("data/arial.ttf",32));
font.open("font"); font.open("font");
} }

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: Image_test.cpp,v 1.2 2005/06/29 00:02:51 cozman Exp $ // $Id: Image_test.cpp,v 1.3 2005/07/03 06:33:19 cozman Exp $
#include "photon.hpp" #include "photon.hpp"
using namespace photon; using namespace photon;
@ -30,8 +30,8 @@ public:
video.setOrthoView(800,600); video.setOrthoView(800,600);
video::Image::addResource("data/test.png"); video::Image::addResource(ResourceDescriptor("data/test.png"));
video::Texture::addResource("test2","data/test2.png"); video::Texture::addResource("test2",ResourceDescriptor("data/test2.png"));
img[0].open("test2"); img[0].open("test2");
img[0].setAlpha(128); img[0].setAlpha(128);

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: Texture_test.cpp,v 1.2 2005/06/27 04:24:16 cozman Exp $ // $Id: Texture_test.cpp,v 1.3 2005/07/03 06:33:19 cozman Exp $
#include "photon.hpp" #include "photon.hpp"
using namespace photon; using namespace photon;
@ -30,8 +30,8 @@ public:
video.setOrthoView(800,600); video.setOrthoView(800,600);
video::Texture::addResource("data/test.png"); video::Texture::addResource(ResourceDescriptor("data/test.png"));
video::Texture::addResource("test2","data/test2.png"); video::Texture::addResource("test2",ResourceDescriptor("data/test2.png"));
// Testing of errors // Testing of errors
//video::Texture::addResource("nonfile"); //video::Texture::addResource("nonfile");