From 219db7e0f11786ea3572c15660e16e331ec28f73 Mon Sep 17 00:00:00 2001 From: James Turk Date: Mon, 4 Jul 2005 03:06:48 +0000 Subject: [PATCH] resource manager cleanup --- include/ResourceManaged.hpp | 15 +-------- include/ResourceManager.hpp | 67 ++++++++++++++++++++----------------- include/audio/AudioCore.hpp | 6 +++- include/video/Font.hpp | 25 +++++++++++++- include/video/Texture.hpp | 26 ++++++++++++-- photon.mm | 14 ++++---- src/audio/AudioCore.cpp | 6 +++- src/video/Font.cpp | 13 ++++++- src/video/Texture.cpp | 12 ++++++- test/AudioCore_test.cpp | 17 ++++++++-- test/Font_test.cpp | 17 ++++++---- test/Image_test.cpp | 6 ++-- test/Texture_test.cpp | 6 ++-- 13 files changed, 155 insertions(+), 75 deletions(-) diff --git a/include/ResourceManaged.hpp b/include/ResourceManaged.hpp index d5c233a..4ec99e7 100644 --- a/include/ResourceManaged.hpp +++ b/include/ResourceManaged.hpp @@ -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::cleanUp() resMgr_.cleanUp(); } -template -void ResourceManaged::addResource(const std::string& name, - const typename ResMgrT::ResDescT& desc) -{ - resMgr_.newResource(name, desc); -} - -template -void ResourceManaged::addResource(const typename ResMgrT::ResDescT& desc) -{ - resMgr_.newResource(desc.path, desc); -} - // define the resource manager static instance template T ResourceManaged::resMgr_; diff --git a/include/ResourceManager.hpp b/include/ResourceManager.hpp index befb585..91ad934 100644 --- a/include/ResourceManager.hpp +++ b/include/ResourceManager.hpp @@ -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 ResourceManager::~ResourceManager() { } -template -void ResourceManager::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 -void ResourceManager::cleanUp() -{ - // delete resources, until none are left - while(!resourceMap_.empty()) - { - freeResourceData(resourceMap_.begin()->second); - } -} - template void ResourceManager::newResource(const std::string& name, const ResDescT& desc) @@ -124,7 +106,7 @@ void ResourceManager::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::deleteResource(const std::string& name) } } +template +void ResourceManager::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 +void ResourceManager::cleanUp() +{ + // delete resources, until none are left + while(!resourceMap_.empty()) + { + deleteResource(resourceMap_.begin()->first); + } +} + template void ResourceManager::printReport(std::ostream& os) { diff --git a/include/audio/AudioCore.hpp b/include/audio/AudioCore.hpp index 137b487..ec876b7 100644 --- a/include/audio/AudioCore.hpp +++ b/include/audio/AudioCore.hpp @@ -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 diff --git a/include/video/Font.hpp b/include/video/Font.hpp index c8146a9..34f88a6 100644 --- a/include/video/Font.hpp +++ b/include/video/Font.hpp @@ -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_; diff --git a/include/video/Texture.hpp b/include/video/Texture.hpp index f7e0626..823c7d3 100644 --- a/include/video/Texture.hpp +++ b/include/video/Texture.hpp @@ -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 // 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_; diff --git a/photon.mm b/photon.mm index c8f3606..7b0efee 100644 --- a/photon.mm +++ b/photon.mm @@ -12,11 +12,6 @@ - - - - - @@ -24,7 +19,7 @@ - + @@ -42,15 +37,18 @@ - + + + + @@ -79,7 +77,7 @@ - + diff --git a/src/audio/AudioCore.cpp b/src/audio/AudioCore.cpp index 5505580..34022d6 100644 --- a/src/audio/AudioCore.cpp +++ b/src/audio/AudioCore.cpp @@ -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 diff --git a/src/video/Font.cpp b/src/video/Font.cpp index b2a306b..089f801 100644 --- a/src/video/Font.cpp +++ b/src/video/Font.cpp @@ -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)); +} + } } diff --git a/src/video/Texture.cpp b/src/video/Texture.cpp index 9c96ead..8a2bb5d 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.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)); +} + } } diff --git a/test/AudioCore_test.cpp b/test/AudioCore_test.cpp index 5d58eb3..9bbfda4 100644 --- a/test/AudioCore_test.cpp +++ b/test/AudioCore_test.cpp @@ -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 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 diff --git a/test/Font_test.cpp b/test/Font_test.cpp index b5a31ed..a32c1cc 100644 --- a/test/Font_test.cpp +++ b/test/Font_test.cpp @@ -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) diff --git a/test/Image_test.cpp b/test/Image_test.cpp index b12a03a..9720009 100644 --- a/test/Image_test.cpp +++ b/test/Image_test.cpp @@ -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); diff --git a/test/Texture_test.cpp b/test/Texture_test.cpp index 2447158..840d9b9 100644 --- a/test/Texture_test.cpp +++ b/test/Texture_test.cpp @@ -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");