resource manager cleanup

This commit is contained in:
James Turk 2005-07-04 03:06:48 +00:00
parent eb0bd64f03
commit 219db7e0f1
13 changed files with 155 additions and 75 deletions

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: ResourceManaged.hpp,v 1.6 2005/07/03 06:33:19 cozman Exp $
// $Id: ResourceManaged.hpp,v 1.7 2005/07/04 03:06:48 cozman Exp $
#ifndef PHOTON_RESOURCEMANAGED_HPP
#define PHOTON_RESOURCEMANAGED_HPP
@ -166,19 +166,6 @@ void ResourceManaged<ResMgrT>::cleanUp()
resMgr_.cleanUp();
}
template<class ResMgrT>
void ResourceManaged<ResMgrT>::addResource(const std::string& name,
const typename ResMgrT::ResDescT& desc)
{
resMgr_.newResource(name, desc);
}
template<class ResMgrT>
void ResourceManaged<ResMgrT>::addResource(const typename ResMgrT::ResDescT& desc)
{
resMgr_.newResource(desc.path, desc);
}
// define the resource manager static instance
template <class T>
T ResourceManaged<T>::resMgr_;

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: ResourceManager.hpp,v 1.8 2005/07/03 06:33:19 cozman Exp $
// $Id: ResourceManager.hpp,v 1.9 2005/07/04 03:06:48 cozman Exp $
#ifndef PHOTON_RESOURCEMANAGER_HPP
#define PHOTON_RESOURCEMANAGER_HPP
@ -23,6 +23,11 @@ namespace photon
class Resource
{
public:
Resource() :
refCount(0)
{ }
public:
uint refCount;
};
@ -34,6 +39,8 @@ public:
ResourceDescriptor(const std::string& p) :
path(p)
{ }
public:
std::string path;
};
@ -53,13 +60,13 @@ public:
virtual ~ResourceManager();
void delRef(const std::string& name);
void cleanUp();
void newResource(const std::string& name, const ResDescT& path);
resT& getResource(const std::string& name);
void delRef(const std::string& name);
void cleanUp();
private:
virtual void loadResourceData(resT &res, const ResDescT& path)=0;
virtual void freeResourceData(resT &res)=0;
@ -85,31 +92,6 @@ template<class resT, class ResDescT>
ResourceManager<resT, ResDescT>::~ResourceManager()
{ }
template<class resT, class ResDescT>
void ResourceManager<resT, ResDescT>::delRef(const std::string& name)
{
MapIterator resource( resourceMap_.find(name) );
// if the resource was found
if(resource != resourceMap_.end())
{
if(--resource->second.refCount <= 0)
{
deleteResource(name);
}
}
}
template<class resT, class ResDescT>
void ResourceManager<resT, ResDescT>::cleanUp()
{
// delete resources, until none are left
while(!resourceMap_.empty())
{
freeResourceData(resourceMap_.begin()->second);
}
}
template<class resT, class ResDescT>
void ResourceManager<resT, ResDescT>::newResource(const std::string& name,
const ResDescT& desc)
@ -124,7 +106,7 @@ void ResourceManager<resT, ResDescT>::newResource(const std::string& name,
catch(ResourceException& e)
{
// rethrow any exceptions with specific information
throw ResourceException("Could not load " + desc.path + " as " + name +
throw ResourceException("Could not load " + desc.path + " as " + name +
": " + e.getDesc());
}
@ -162,6 +144,31 @@ void ResourceManager<resT, ResDescT>::deleteResource(const std::string& name)
}
}
template<class resT, class ResDescT>
void ResourceManager<resT, ResDescT>::delRef(const std::string& name)
{
MapIterator resource( resourceMap_.find(name) );
// if the resource was found
if(resource != resourceMap_.end())
{
if(--resource->second.refCount <= 0)
{
deleteResource(name);
}
}
}
template<class resT, class ResDescT>
void ResourceManager<resT, ResDescT>::cleanUp()
{
// delete resources, until none are left
while(!resourceMap_.empty())
{
deleteResource(resourceMap_.begin()->first);
}
}
template<class resT, class ResDescT>
void ResourceManager<resT, ResDescT>::printReport(std::ostream& os)
{

View File

@ -5,7 +5,9 @@
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: AudioCore.hpp,v 1.5 2005/04/21 19:30:19 cozman Exp $
// $Id: AudioCore.hpp,v 1.6 2005/07/04 03:06:48 cozman Exp $
#ifdef PHOTON_USE_OPENAL
#ifndef PHOTON_AUDIO_AUDIOCORE_HPP
#define PHOTON_AUDIO_AUDIOCORE_HPP
@ -78,3 +80,5 @@ private:
}
#endif //PHOTON_AUDIO_AUDIOCORE_HPP
#endif //PHOTON_USE_OPENAL

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: Font.hpp,v 1.3 2005/07/03 06:33:19 cozman Exp $
// $Id: Font.hpp,v 1.4 2005/07/04 03:06:48 cozman Exp $
#ifndef PHOTON_VIDEO_FONT_HPP
#define PHOTON_VIDEO_FONT_HPP
@ -87,6 +87,29 @@ public:
unsigned int calcStringWidth(const std::string& str) const;
unsigned int getHeight() const;
// Group: Resource Creation
public:
// Function: addResource
// Define a new named resource.
// (Ex. Image::addResource("monkey","images/monkey.png") would
// make it so that any attempts to load "monkey" would load the image
// images/monkey.png)
//
// Parameters:
// name - Name to give to resource.
// path - Path of resource data file.
static void addResource(const std::string& name, const std::string& path,
uint size);
// Function: addResource
// Define a new unaliased resource. (name == path).
// (Ex. Image::addResource("images/monkey.png") is essentially the same as
// Image::addResource("images/monkey.png","images/monkey.png")
//
// Parameters:.
// path - Path of resource data file.
static void addResource(const std::string& path, uint size);
private:
// font data
uint texID_;

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: Texture.hpp,v 1.3 2005/07/03 05:20:49 cozman Exp $
// $Id: Texture.hpp,v 1.4 2005/07/04 03:06:48 cozman Exp $
#ifndef PHOTON_VIDEO_TEXTURE_HPP
#define PHOTON_VIDEO_TEXTURE_HPP
@ -37,7 +37,6 @@ class Texture : public ResourceManaged<TextureResourceManager>
// Group: (Con/De)structors
public:
// Function: Texture
// Default constructor, initalizes internal state of Texture.
Texture();
@ -98,6 +97,29 @@ public:
friend std::ostream& operator<<(std::ostream &o, const Texture &rhs);
// Group: Resource Creation
public:
// Function: addResource
// Define a new named resource.
// (Ex. Image::addResource("monkey","images/monkey.png") would
// make it so that any attempts to load "monkey" would load the image
// images/monkey.png)
//
// Parameters:
// name - Name to give to resource.
// path - Path of resource data file.
static void addResource(const std::string& name, const std::string& path);
// Function: addResource
// Define a new unaliased resource. (name == path).
// (Ex. Image::addResource("images/monkey.png") is essentially the same as
// Image::addResource("images/monkey.png","images/monkey.png")
//
// Parameters:.
// path - Path of resource data file.
static void addResource(const std::string& path);
private:
scalar width_;
scalar height_;

View File

@ -12,11 +12,6 @@
<font NAME="SansSerif" SIZE="12"/>
<node ID="Freemind_Link_1795651487" TEXT="&quot;ResourceManage&quot;ment">
<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 ID="Freemind_Link_50716011" TEXT="Texture">
<icon BUILTIN="button_ok"/>
@ -24,7 +19,7 @@
<icon BUILTIN="button_ok"/>
</node>
<node ID="Freemind_Link_561271163" TEXT="Font">
<icon BUILTIN="desktop_new"/>
<icon BUILTIN="button_ok"/>
</node>
</node>
<node ID="Freemind_Link_1851655735" TEXT="Music">
@ -42,15 +37,18 @@
<icon BUILTIN="button_ok"/>
</node>
<node ID="Freemind_Link_1104675603" TEXT="Tests">
<node ID="Freemind_Link_1283736172" TEXT="Math Test(s)"/>
<node ID="Freemind_Link_1348104879" TEXT="Kernel/AppCore"/>
<node ID="Freemind_Link_288973656" TEXT="Texture">
<icon BUILTIN="button_ok"/>
</node>
<node ID="Freemind_Link_1283736172" TEXT="Math Test(s)"/>
<node ID="Freemind_Link_279976301" TEXT="Pen"/>
<node ID="Freemind_Link_609153459" TEXT="Image">
<icon BUILTIN="button_ok"/>
</node>
<node ID="Freemind_Link_1961072093" TEXT="Font">
<icon BUILTIN="button_ok"/>
</node>
<node ID="Freemind_Link_25725154" TEXT="Sound/Music"/>
</node>
</node>
@ -79,7 +77,7 @@
<node ID="Freemind_Link_682620075" POSITION="left" TEXT="Current Problems">
<font BOLD="true" NAME="SansSerif" SIZE="12"/>
</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 $">
<node COLOR="#147f1e" ID="Freemind_Link_438641521" POSITION="left" TEXT="Version: $Id: photon.mm,v 1.11 2005/07/04 03:06:48 cozman Exp $">
<font ITALIC="true" NAME="SansSerif" SIZE="12"/>
</node>
</node>

View File

@ -5,7 +5,9 @@
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: AudioCore.cpp,v 1.5 2005/05/15 02:50:52 cozman Exp $
// $Id: AudioCore.cpp,v 1.6 2005/07/04 03:06:48 cozman Exp $
#ifdef PHOTON_USE_OPENAL
#include "audio/AudioCore.hpp"
@ -135,3 +137,5 @@ std::string AudioCore::deviceName_;
}
}
#endif //PHOTON_USE_OPENAL

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: Font.cpp,v 1.3 2005/07/03 06:33:19 cozman Exp $
// $Id: Font.cpp,v 1.4 2005/07/04 03:06:48 cozman Exp $
#include "video/Font.hpp"
@ -181,5 +181,16 @@ unsigned int Font::getHeight() const
return height_;
}
void Font::addResource(const std::string& name, const std::string& path,
uint size)
{
resMgr_.newResource(name, FontResourceDescriptor(path, size));
}
void Font::addResource(const std::string& path, uint size)
{
resMgr_.newResource(path, FontResourceDescriptor(path, size));
}
}
}

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: Texture.cpp,v 1.3 2005/06/27 04:24:16 cozman Exp $
// $Id: Texture.cpp,v 1.4 2005/07/04 03:06:48 cozman Exp $
#include "video/Texture.hpp"
@ -80,5 +80,15 @@ std::ostream& operator<<(std::ostream &o, const Texture &rhs)
<< " Dimensions: " << rhs.width_ << "x" << rhs.height_ << " }";
}
void Texture::addResource(const std::string& name, const std::string& path)
{
resMgr_.newResource(name, ResourceDescriptor(path));
}
void Texture::addResource(const std::string& path)
{
resMgr_.newResource(path, ResourceDescriptor(path));
}
}
}

View File

@ -5,12 +5,15 @@
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: AudioCore_test.cpp,v 1.1 2005/05/15 02:50:07 cozman Exp $
// $Id: AudioCore_test.cpp,v 1.2 2005/07/04 03:06:48 cozman Exp $
#include "photon.hpp"
#include <iostream>
using namespace photon;
#ifdef PHOTON_USE_OPENAL
using namespace photon::audio;
class AudioTest : public Application
@ -19,8 +22,7 @@ public:
int main(const StrVec& args)
{
//throw Error("zing!");
AudioCore& audio(AudioCore::getInstance());
std::cout << audio.getAudioDeviceName();
@ -29,3 +31,12 @@ public:
};
ENTRYPOINT(AudioTest)
#else
int main()
{
std::cerr << "Photon compiled without OpenAL support.\n";
}
#endif //PHOTON_USE_OPENAL

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: Font_test.cpp,v 1.3 2005/07/03 06:33:19 cozman Exp $
// $Id: Font_test.cpp,v 1.4 2005/07/04 03:06:48 cozman Exp $
#include "photon.hpp"
using namespace photon;
@ -30,9 +30,11 @@ public:
video.setOrthoView(800,600);
video::Font::addResource("font",video::FontResourceDescriptor("data/arial.ttf",32));
font.open("font");
video::Font::addResource("font1","data/arial.ttf",32);
video::Font::addResource("font2","data/FreeMono.ttf",18);
font.open("font1");
font2.open("font2");
}
void update()
@ -51,18 +53,19 @@ public:
font.drawText(0, 0, "Photon");
font.drawText(font.calcStringWidth("Photon"), font.getHeight(),
"FPS: %.0f", app.getFramerate() );
font.beginDraw(200, 200) << "another font" << font.endDraw();
font2.beginDraw(200, 200) << "another font" << font2.endDraw();
}
private:
video::Font font;
video::Font font2;
Log log;
AppCore& app;
video::VideoCore& video;
};
class ImageTest : public Application
class FontTest : public Application
{
public:
@ -78,4 +81,4 @@ public:
}
};
ENTRYPOINT(ImageTest)
ENTRYPOINT(FontTest)

View File

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

View File

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