diff --git a/include/ResourceManaged.hpp b/include/ResourceManaged.hpp index 2c126a8..371e388 100644 --- a/include/ResourceManaged.hpp +++ b/include/ResourceManaged.hpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: ResourceManaged.hpp,v 1.4 2005/06/14 00:28:36 cozman Exp $ +// $Id: ResourceManaged.hpp,v 1.5 2005/06/27 04:24:09 cozman Exp $ #ifndef PHOTON_RESOURCEMANAGED_HPP #define PHOTON_RESOURCEMANAGED_HPP @@ -65,12 +65,23 @@ public: // Generally called by destructor, so should rarely be called. virtual void release(); +// Group: Accessors +public: + // Function: getName + // Get the name associated with the resource. + // + // Returns: + // Name of resource, or empty string if no resource is loaded. + std::string getName() const; + // Group: Resource Manager Access - +public: + static ResMgrT resMgr_; + // Function: cleanUp // Cleans up any unused resources of the type. // (Ex. Image::cleanUp() will unload all images.) - virtual void cleanUp(); + static void cleanUp(); // Function: addResource // Define a new named resource. @@ -92,9 +103,8 @@ public: // path - Path of resource data file. static void addResource(const std::string& path); -protected: +private: std::string resName_; - static ResMgrT resMgr_; }; //and he said "the implementation shall follow, as it is written" @@ -143,6 +153,12 @@ void ResourceManaged::release() } } +template +std::string ResourceManaged::getName() const +{ + return resName_; +} + template void ResourceManaged::cleanUp() { diff --git a/include/ResourceManager.hpp b/include/ResourceManager.hpp index 1b23691..00adbe5 100644 --- a/include/ResourceManager.hpp +++ b/include/ResourceManager.hpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: ResourceManager.hpp,v 1.6 2005/06/14 00:28:36 cozman Exp $ +// $Id: ResourceManager.hpp,v 1.7 2005/06/27 04:24:16 cozman Exp $ #ifndef PHOTON_RESOURCEMANAGER_HPP #define PHOTON_RESOURCEMANAGER_HPP @@ -54,6 +54,9 @@ private: virtual void freeResource(resT &res)=0; void deleteResource(const std::string& name); + +public: + void printReport(std::ostream& os); private: typedef std::map MapT; @@ -113,7 +116,7 @@ void ResourceManager::newResource(const std::string& name, { // rethrow any exceptions with specific information throw ResourceException("Could not load " + path + " as " + name + - ": " + e.what()); + ": " + e.getDesc()); } resourceMap_[name] = resource; // add the resource to resourceMap @@ -126,11 +129,13 @@ resT& ResourceManager::getResource(const std::string& name) if(resource != resourceMap_.end()) { + // increment the refCount and return the resource for use + ++resource->second.refCount; return resource->second; } else { - throw ResourceException(); + throw ResourceException("No resource named \"" + name + "\" exists."); } } @@ -147,6 +152,19 @@ void ResourceManager::deleteResource(const std::string& name) } } +template +void ResourceManager::printReport(std::ostream& os) +{ + MapIterator resource( resourceMap_.begin() ); + + for(MapIterator i = resourceMap_.begin(); i != resourceMap_.end(); ++i) + { + os << i->second.name << "\t" << i->second.path << "\t" + << i->second.refCount << "\n"; + } +} + + } #endif //PHOTON_RESOURCEMANAGER_HPP diff --git a/include/exceptions.hpp b/include/exceptions.hpp index 0448d21..cd47200 100644 --- a/include/exceptions.hpp +++ b/include/exceptions.hpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: exceptions.hpp,v 1.4 2005/03/01 07:51:04 cozman Exp $ +// $Id: exceptions.hpp,v 1.5 2005/06/27 04:24:16 cozman Exp $ #ifndef PHOTON_EXCEPTIONS_HPP #define PHOTON_EXCEPTIONS_HPP @@ -56,8 +56,16 @@ public: // Throwable family) define what() that returns a description of the // exception. // - // Returns: std::string describing the error + // Returns: Formatted std::string describing the error std::string virtual what() const throw(); + + // Function: getDesc + // Get just the description portion of an exception, used for chaining + // exceptions. + // + // Returns: Unformatted std::string describing the cause of the error + std::string virtual getDesc() const throw(); + friend std::ostream& operator<<(std::ostream& os, const Throwable& rhs); private: diff --git a/photon.mm b/photon.mm index b31ac8a..cf57259 100644 --- a/photon.mm +++ b/photon.mm @@ -4,7 +4,6 @@ - @@ -32,11 +31,12 @@ + + - + - @@ -63,9 +63,6 @@ - - - @@ -75,7 +72,6 @@ - diff --git a/src/exceptions.cpp b/src/exceptions.cpp index ccdaf50..8d98e74 100644 --- a/src/exceptions.cpp +++ b/src/exceptions.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: exceptions.cpp,v 1.6 2005/02/27 05:52:00 cozman Exp $ +// $Id: exceptions.cpp,v 1.7 2005/06/27 04:24:16 cozman Exp $ #include "exceptions.hpp" @@ -34,6 +34,11 @@ std::string Throwable::what() const throw() return ss.str(); } +std::string Throwable::getDesc() const throw() +{ + return description_; +} + std::ostream& operator<<(std::ostream& os, const Throwable& rhs) { return os << rhs.what(); diff --git a/src/util/FileBuffer.cpp b/src/util/FileBuffer.cpp index acabf0f..cc15c00 100644 --- a/src/util/FileBuffer.cpp +++ b/src/util/FileBuffer.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: FileBuffer.cpp,v 1.6 2005/06/11 05:28:41 cozman Exp $ +// $Id: FileBuffer.cpp,v 1.7 2005/06/27 04:24:16 cozman Exp $ #include "util/FileBuffer.hpp" @@ -34,7 +34,7 @@ void FileBuffer::open(const std::string& filename) file_ = PHYSFS_openRead(filename.c_str()); if(file_ == 0) { - throw ResourceException(); + throw ResourceException("Could not open file \"" + filename + "\"."); } } diff --git a/src/video/Image.cpp b/src/video/Image.cpp index 4cdc134..80dec8d 100644 --- a/src/video/Image.cpp +++ b/src/video/Image.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: Image.cpp,v 1.2 2005/06/14 00:28:36 cozman Exp $ +// $Id: Image.cpp,v 1.3 2005/06/27 04:24:16 cozman Exp $ #include "video/Image.hpp" @@ -18,14 +18,16 @@ namespace video Image::Image() : alpha_(255), - texMinX_(0), texMinY_(0), texMaxX_(1), texMaxY_(1) + texMinX_(0), texMinY_(0), texMaxX_(1), texMaxY_(1), + width_(0), height_(0) { } Image::Image(const Image &rhs) : Texture(rhs), alpha_(rhs.alpha_), texMinX_(rhs.texMinX_), texMinY_(rhs.texMinY_), - texMaxX_(rhs.texMaxX_), texMaxY_(rhs.texMaxY_) + texMaxX_(rhs.texMaxX_), texMaxY_(rhs.texMaxY_), + width_(rhs.width_), height_(rhs.height_) { } Image::Image(const std::string& name) : @@ -45,6 +47,8 @@ Image& Image::operator=(const Image &rhs) texMinY_ = rhs.texMinY_; texMaxX_ = rhs.texMaxX_; texMaxY_ = rhs.texMaxY_; + width_ = rhs.width_; + height_ = rhs.height_; } return *this; } @@ -52,6 +56,8 @@ Image& Image::operator=(const Image &rhs) void Image::open(const std::string& name) { Texture::open(name); + width_ = getWidth(); + height_ = getHeight(); } void Image::setAlpha(ubyte alpha) @@ -75,8 +81,8 @@ void Image::flip(bool horizontal, bool vertical) //stretching and resizing is very inexpensive, done via variables void Image::stretch(scalar xFactor, scalar yFactor) { - width_ = xFactor*width_; - height_ = yFactor*height_; + width_ = xFactor*getWidth(); + height_ = yFactor*getHeight(); } void Image::resize(scalar width, scalar height) @@ -184,7 +190,7 @@ ubyte Image::getAlpha() const std::ostream& operator<<(std::ostream &o, const Image &rhs) { - return o << "Image: { Name: " << rhs.resName_ << + return o << "Image: { Name: " << rhs.getName() << " Dimensions: " << rhs.width_ << "x" << rhs.height_ << " Alpha: " << rhs.alpha_ << " }"; } diff --git a/src/video/Texture.cpp b/src/video/Texture.cpp index db4077b..9c96ead 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.2 2005/06/14 00:28:36 cozman Exp $ +// $Id: Texture.cpp,v 1.3 2005/06/27 04:24:16 cozman Exp $ #include "video/Texture.hpp" @@ -22,7 +22,7 @@ Texture::Texture() Texture::Texture(const Texture &rhs) : ResourceManaged(rhs) { - resMgr_.getTextureData(resName_, width_, height_, texID_); + resMgr_.getTextureData(getName(), width_, height_, texID_); } Texture::Texture(const std::string& name) @@ -33,7 +33,7 @@ Texture::Texture(const std::string& name) void Texture::open(const std::string& name) { ResourceManaged::open(name); - resMgr_.getTextureData(resName_, width_, height_, texID_); + resMgr_.getTextureData(getName(), width_, height_, texID_); } void Texture::bind() const @@ -50,7 +50,7 @@ Texture& Texture::operator=(const Texture &rhs) if(&rhs != this) { ResourceManaged::operator=(rhs); - resMgr_.getTextureData(resName_, width_, height_, texID_); + resMgr_.getTextureData(getName(), width_, height_, texID_); //w&h after getTextureData width_ = rhs.width_; @@ -76,7 +76,7 @@ scalar Texture::getHeight() const std::ostream& operator<<(std::ostream &o, const Texture &rhs) { - return o << "Texture: { Name: " << rhs.resName_ << " TexID: " << rhs.texID_ + return o << "Texture: { Name: " << rhs.getName() << " TexID: " << rhs.texID_ << " Dimensions: " << rhs.width_ << "x" << rhs.height_ << " }"; } diff --git a/src/video/TextureResourceManager.cpp b/src/video/TextureResourceManager.cpp index d7c0880..785784f 100644 --- a/src/video/TextureResourceManager.cpp +++ b/src/video/TextureResourceManager.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: TextureResourceManager.cpp,v 1.2 2005/06/14 00:28:36 cozman Exp $ +// $Id: TextureResourceManager.cpp,v 1.3 2005/06/27 04:24:16 cozman Exp $ #include "video/TextureResourceManager.hpp" @@ -57,7 +57,7 @@ void TextureResourceManager::loadResource(TextureResource &res, if(!file) { - throw ResourceException("corona::CreateMemoryFile failed"); + throw APIError("corona::CreateMemoryFile failed"); } image = corona::OpenImage(file,corona::PF_R8G8B8A8); @@ -66,7 +66,7 @@ void TextureResourceManager::loadResource(TextureResource &res, if(!image) { - throw ResourceException("corona::OpenImage failed"); + throw ResourceException("Invalid image format."); } res.width = image->getWidth(); @@ -118,7 +118,5 @@ void TextureResourceManager::freeResource(TextureResource &res) glDeleteTextures(1, &res.texID); } - - } } diff --git a/src/video/VideoCore.cpp b/src/video/VideoCore.cpp index c4803d2..24e9e4b 100644 --- a/src/video/VideoCore.cpp +++ b/src/video/VideoCore.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: VideoCore.cpp,v 1.4 2005/06/11 05:28:41 cozman Exp $ +// $Id: VideoCore.cpp,v 1.5 2005/06/27 04:24:16 cozman Exp $ #include "video/VideoCore.hpp" @@ -115,11 +115,13 @@ void VideoCore::initOpenGL() glShadeModel(GL_SMOOTH); // Setup depth checking. - //glDepthFunc(GL_LEQUAL); - //glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + glEnable(GL_DEPTH_TEST); //setup hints glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); + + glEnable(GL_POLYGON_SMOOTH); //enable texturing glEnable(GL_TEXTURE_2D); diff --git a/test/Texture_test.cpp b/test/Texture_test.cpp index 2ea0b03..c53fc63 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.1 2005/06/13 05:38:06 cozman Exp $ +// $Id: Texture_test.cpp,v 1.2 2005/06/27 04:24:16 cozman Exp $ #include "photon.hpp" using namespace photon; @@ -31,7 +31,16 @@ public: video.setOrthoView(800,600); video::Texture::addResource("data/test.png"); - img.open("data/test.png"); + video::Texture::addResource("test2","data/test2.png"); + + // Testing of errors + //video::Texture::addResource("nonfile"); + //video::Texture::addResource("Texture_test.cpp"); + //tex[0].open("test0"); + + tex[0].open("data/test.png"); + tex[1].open("test2"); + tex[2] = tex[1]; } @@ -48,31 +57,45 @@ public: video.clear(); - img.bind(); + tex[0].bind(); glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex2f(0,0); glTexCoord2f(1,0); glVertex2f(100,0); glTexCoord2f(1,1); glVertex2f(100,100); glTexCoord2f(0,1); glVertex2f(0,100); glEnd(); + + tex[1].bind(); + glBegin(GL_QUADS); + glTexCoord2f(0,0); glVertex2f(250,200); + glTexCoord2f(1,0); glVertex2f(300,250); + glTexCoord2f(1,1); glVertex2f(250,300); + glTexCoord2f(0,1); glVertex2f(200,250); + glEnd(); + + if(tex[2]) + { + tex[2].bind(); + glBegin(GL_QUADS); + glTexCoord2f(0,0); glVertex2f(400,400); + glTexCoord2f(1,0); glVertex2f(500,400); + glTexCoord2f(1,1); glVertex2f(500,500); + glTexCoord2f(0,1); glVertex2f(400,500); + glEnd(); + } } private: - video::Texture img; + video::Texture tex[3]; Log log; AppCore& app; video::VideoCore& video; }; -class Test00 : public Application +class TextureTest : public Application { public: - Test00() - { - //Log::getInstance().addSink(LogSinkPtr(new ConsoleSink("out"))); - //Log::getInstance().addSink(LogSinkPtr(new HTMLSink("debug"))); - } int main(const StrVec& args) { @@ -86,4 +109,4 @@ public: } }; -ENTRYPOINT(Test00) +ENTRYPOINT(TextureTest)