From 704f4e44c6981fcfda91e2983d240753a5e3e467 Mon Sep 17 00:00:00 2001 From: James Turk Date: Mon, 13 Jun 2005 05:38:06 +0000 Subject: [PATCH] Texture test passes --- include/ResourceManager.hpp | 7 +- include/video/Texture.hpp | 110 ++++++++++++++++++++ include/video/TextureResourceManager.hpp | 49 +++++++++ photon.mm | 5 +- src/AppCore.cpp | 3 +- src/Application.cpp | 4 +- src/video/Texture.cpp | 93 +++++++++++++++++ src/video/TextureResourceManager.cpp | 126 +++++++++++++++++++++++ test/Texture_test.cpp | 89 ++++++++++++++++ 9 files changed, 475 insertions(+), 11 deletions(-) create mode 100644 include/video/Texture.hpp create mode 100644 include/video/TextureResourceManager.hpp create mode 100644 src/video/Texture.cpp create mode 100644 src/video/TextureResourceManager.cpp create mode 100644 test/Texture_test.cpp diff --git a/include/ResourceManager.hpp b/include/ResourceManager.hpp index c36ad8e..f0feb03 100644 --- a/include/ResourceManager.hpp +++ b/include/ResourceManager.hpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: ResourceManager.hpp,v 1.4 2005/06/11 05:28:41 cozman Exp $ +// $Id: ResourceManager.hpp,v 1.5 2005/06/13 05:38:06 cozman Exp $ #ifndef PHOTON_RESOURCEMANAGER_HPP #define PHOTON_RESOURCEMANAGER_HPP @@ -133,10 +133,11 @@ uint ResourceManager::newResource(const std::string& name, // attempt to load loadResource(res, path); } - catch(ResourceException&) + catch(ResourceException& e) { // rethrow any exceptions with specific information - throw ResourceException("Could not load " + path + " as " + name); + throw ResourceException("Could not load " + path + " as " + name + + ": " + e.what()); } resVec_.push_back(res); // add resource to resVec & return diff --git a/include/video/Texture.hpp b/include/video/Texture.hpp new file mode 100644 index 0000000..156fd6f --- /dev/null +++ b/include/video/Texture.hpp @@ -0,0 +1,110 @@ +//This file is part of Photon (http://photon.sourceforge.net) +//Copyright (C) 2004-2005 James Turk +// +// Author: +// James Turk (jpt2433@rit.edu) +// +// Version: +// $Id: Texture.hpp,v 1.1 2005/06/13 05:38:06 cozman Exp $ + +#ifndef PHOTON_VIDEO_TEXTURE_HPP +#define PHOTON_VIDEO_TEXTURE_HPP + +#include "video/TextureResourceManager.hpp" +#include "ResourceManaged.hpp" + +namespace photon +{ +namespace video +{ + +// Class: Texture +// Simple OO wrapper around the concept of a texture in openGL. +// +// Since Texture is a child of , all memory management is +// taken care of. +// +// Children: +// +// +// Operators: +// - Texture = Texture +// - bool : True if texture is loaded, false if not. +// - ostream& << Texture +class Texture: public ResourceManaged +{ + +// Group: (Con/De)structors +public: + + + // Function: Texture + // Default constructor, initalizes internal state of Texture. + Texture(); + + // Function: Texture + // Copy constructor, copies another Texture. + // + // Parameters: + // rhs - Texture to construct copy of. + Texture(const Texture &rhs); + + // Function: Texture + // Initializing constructor, loads Texture via call to . + // + // Parameters: + // name - Name of the Texture to open. + // + // See Also: + // + Texture(const std::string& name); + +// Group: General +public: + + // Function: open + // Opens an image file, currently supported image types are BMP, GIF, JPEG, + // PCX, PNG, and TGA. + // + // Loading is done via Corona. + // + // Parameters: + // name - Name of the Texture to open. + void open(const std::string& name); + + // Function: bind + // Makes texture the current OpenGL texture. + void bind() const; + + Texture& operator=(const Texture &rhs); + operator bool() const; + +// Group: Accessors +public: + + // Function: getWidth + // Gets width of texture. + // + // Returns: + // Width of texture. + scalar getWidth() const; + + // Function: getHeight + // Gets height of texture. + // + // Returns: + // Height of texture. + scalar getHeight() const; + + friend std::ostream& operator<<(std::ostream &o, const Texture &rhs); + +private: + scalar width_; + scalar height_; + uint texID_; +}; + +} +} + +#endif //PHOTON_VIDEO_TEXTURE_HPP diff --git a/include/video/TextureResourceManager.hpp b/include/video/TextureResourceManager.hpp new file mode 100644 index 0000000..793a72c --- /dev/null +++ b/include/video/TextureResourceManager.hpp @@ -0,0 +1,49 @@ +//This file is part of Photon (http://photon.sourceforge.net) +//Copyright (C) 2004-2005 James Turk +// +// Author: +// James Turk (jpt2433@rit.edu) +// +// Version: +// $Id: TextureResourceManager.hpp,v 1.1 2005/06/13 05:38:06 cozman Exp $ + +#ifndef PHOTON_VIDEO_TEXTURERESOURCEMANAGER_HPP +#define PHOTON_VIDEO_TEXTURERESOURCEMANAGER_HPP + +#include "ResourceManager.hpp" +#include "video/Color.hpp" + +namespace photon +{ +namespace video +{ + +class TextureResource : public Resource +{ +public: + uint texID; + uint width; + uint height; + ubyte *pixels; +}; + +class TextureResourceManager : public ResourceManager +{ +public: + void setGlobalColorKey(bool enabled, ubyte red, ubyte green, ubyte blue); + void getGlobalColorKey(bool &enabled, + ubyte &red, ubyte &green, ubyte &blue); + + void getTextureData(uint id, scalar &width, scalar &height, uint &texID); + +private: + virtual void loadResource(TextureResource &res, const std::string& name); + virtual void freeResource(TextureResource &res); + + Color colorKey_; +}; + +} +} + +#endif //PHOTON_VIDEO_TEXTURERESOURCEMANAGER_HPP diff --git a/photon.mm b/photon.mm index 0c0af81..55546cd 100644 --- a/photon.mm +++ b/photon.mm @@ -15,12 +15,9 @@ + - - - - diff --git a/src/AppCore.cpp b/src/AppCore.cpp index bae4032..0084b67 100644 --- a/src/AppCore.cpp +++ b/src/AppCore.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: AppCore.cpp,v 1.7 2005/06/11 05:28:41 cozman Exp $ +// $Id: AppCore.cpp,v 1.8 2005/06/13 05:38:06 cozman Exp $ #include "AppCore.hpp" @@ -51,6 +51,7 @@ void AppCore::createDisplay(uint width, uint height, dispWidth_ = width; dispHeight_ = height; + new video::VideoCore; video::VideoCore::getInstance().setDisplaySize(width,height); glfwSetWindowTitle(title.c_str()); // title is set separately diff --git a/src/Application.cpp b/src/Application.cpp index e6c3890..f13d0bd 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: Application.cpp,v 1.9 2005/06/11 05:28:41 cozman Exp $ +// $Id: Application.cpp,v 1.10 2005/06/13 05:38:06 cozman Exp $ #include "Application.hpp" @@ -34,8 +34,6 @@ Application::Application() : // create the singletons new Kernel; new AppCore; - new video::VideoCore; - //new audio::AudioCore; // StrVec args; // diff --git a/src/video/Texture.cpp b/src/video/Texture.cpp new file mode 100644 index 0000000..c254f8a --- /dev/null +++ b/src/video/Texture.cpp @@ -0,0 +1,93 @@ +//This file is part of Photon (http://photon.sourceforge.net) +//Copyright (C) 2004-2005 James Turk +// +// Author: +// James Turk (jpt2433@rit.edu) +// +// Version: +// $Id: Texture.cpp,v 1.1 2005/06/13 05:38:06 cozman Exp $ + +#include "video/Texture.hpp" + +#include "GL/gl.h" + +namespace photon +{ +namespace video +{ + +template +T ResourceManaged::resMgr_; + +Texture::Texture() +{ +} + +Texture::Texture(const Texture &rhs) +{ + resID_ = rhs.resID_; + resMgr_.getTextureData(resID_,width_,height_,texID_); + + //w&h after getTextureData + width_ = rhs.width_; + height_ = rhs.height_; +} + +Texture::Texture(const std::string& name) +{ + open(name); +} + +void Texture::open(const std::string& name) +{ + ResourceManaged::open(name); + resMgr_.getTextureData(resID_, width_, height_, texID_); +} + +void Texture::bind() const +{ + + if(glIsTexture(texID_) == GL_FALSE) + { + throw PreconditionException("Texture::bind call without valid image."); + } + glBindTexture(GL_TEXTURE_2D, texID_); +} + +Texture& Texture::operator=(const Texture &rhs) +{ + if(&rhs != this) + { + ResourceManaged::operator=(rhs); + resMgr_.getTextureData(resID_,width_,height_,texID_); + + //w&h after getTextureData + width_ = rhs.width_; + height_ = rhs.height_; + } + return *this; +} + +Texture::operator bool() const +{ + return glIsTexture(texID_) == GL_TRUE; +} + +scalar Texture::getWidth() const +{ + return width_; +} + +scalar Texture::getHeight() const +{ + return height_; +} + +std::ostream& operator<<(std::ostream &o, const Texture &rhs) +{ + return o << "Texture: { ResID: " << rhs.resID_ << " TexID: " << rhs.texID_ + << " Dimensions: " << rhs.width_ << "x" << rhs.height_ << " }"; +} + +} +} diff --git a/src/video/TextureResourceManager.cpp b/src/video/TextureResourceManager.cpp new file mode 100644 index 0000000..382c2e2 --- /dev/null +++ b/src/video/TextureResourceManager.cpp @@ -0,0 +1,126 @@ +//This file is part of Photon (http://photon.sourceforge.net) +//Copyright (C) 2004-2005 James Turk +// +// Author: +// James Turk (jpt2433@rit.edu) +// +// Version: +// $Id: TextureResourceManager.cpp,v 1.1 2005/06/13 05:38:06 cozman Exp $ + +#include "video/TextureResourceManager.hpp" + +#include "util/FileBuffer.hpp" +#include +#include "GL/gl.h" +#include "corona.h" + +namespace photon +{ +namespace video +{ + +void TextureResourceManager::setGlobalColorKey(bool enabled, ubyte red, + ubyte green, ubyte blue) +{ + colorKey_.setColor(red,green,blue,enabled?0:255); +} + +void TextureResourceManager::getGlobalColorKey(bool &enabled, ubyte &red, + ubyte &green, ubyte &blue) +{ + enabled = (colorKey_.alpha == 0); + red = colorKey_.red; + green = colorKey_.green; + blue = colorKey_.blue; +} + +void TextureResourceManager::getTextureData(uint id, 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; + } +} + +void TextureResourceManager::loadResource(TextureResource &res, + const std::string& path) +{ + corona::Image *image(0); + util::FileBuffer buf(path); + corona::File *file; + + std::vector data = buf.getData(); + + file = corona::CreateMemoryFile((ubyte*)&data[0],data.size()); + + if(!file) + { + throw ResourceException("corona::CreateMemoryFile failed"); + } + + image = corona::OpenImage(file,corona::PF_R8G8B8A8); + + delete file; + + if(!image) + { + throw ResourceException("corona::OpenImage failed"); + } + + 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); + delete image; //no longer need image + + if(colorKey_.alpha == 0) //ck alpha == 0, means colorkey on + { + ubyte r,g,b; + ubyte *pxl=res.pixels; + for(uint i=0; i < res.width*res.height; ++i) + { + r = *pxl++; + g = *pxl++; + b = *pxl++; + //set current pixel alpha = 0 + if(r == colorKey_.red && + g == colorKey_.green && + b == colorKey_.blue) + { + *pxl = 0; + } + *pxl++; + } + } + + glGenTextures(1,&res.texID); + glBindTexture(GL_TEXTURE_2D,res.texID); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, + GL_NEAREST_MIPMAP_LINEAR); + gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, res.width, res.height, GL_RGBA, + GL_UNSIGNED_BYTE, res.pixels); +} + +void TextureResourceManager::freeResource(TextureResource &res) +{ + if(res.pixels) + { + delete []res.pixels; + res.pixels = 0; + } + glDeleteTextures(1, &res.texID); +} + + + +} +} diff --git a/test/Texture_test.cpp b/test/Texture_test.cpp new file mode 100644 index 0000000..2ea0b03 --- /dev/null +++ b/test/Texture_test.cpp @@ -0,0 +1,89 @@ +//This file is part of Photon (http://photon.sourceforge.net) +//Copyright (C) 2004-2005 James Turk +// +// Author: +// James Turk (jpt2433@rit.edu) +// +// Version: +// $Id: Texture_test.cpp,v 1.1 2005/06/13 05:38:06 cozman Exp $ + +#include "photon.hpp" +using namespace photon; +#include + +// This is a simple framework for writing extremely simple Photon applications +// it simply displays a blank window and the framerate in the titlebar of +// the created window. This is meant to show basic interaction between the +// user and the more common features of the AppCore and the Kernel. + +class MainTask : public Task +{ + +public: + MainTask() : + Task("MainTask"), + app(AppCore::getInstance()), + video(video::VideoCore::getInstance()) + { + LogSinkPtr csp( new ConsoleSink("console") ); + log.addSink(csp); + + video.setOrthoView(800,600); + + video::Texture::addResource("data/test.png"); + img.open("data/test.png"); + + } + + void update() + { + static double t=0; + + if(app.getTime() - t > 1.0) + { + app.setTitle("FPS: " + + boost::lexical_cast(app.getFramerate()) ); + t = app.getTime(); + } + + video.clear(); + + img.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(); + } + +private: + video::Texture img; + + Log log; + AppCore& app; + video::VideoCore& video; +}; + +class Test00 : public Application +{ +public: + Test00() + { + //Log::getInstance().addSink(LogSinkPtr(new ConsoleSink("out"))); + //Log::getInstance().addSink(LogSinkPtr(new HTMLSink("debug"))); + } + + int main(const StrVec& args) + { + AppCore::getInstance().createDisplay(800,600,32,0,0,false); + + Kernel::getInstance().addTask(TaskPtr(new MainTask())); + + Kernel::getInstance().run(); + + return 0; + } +}; + +ENTRYPOINT(Test00)