Texture test passes

This commit is contained in:
James Turk 2005-06-13 05:38:06 +00:00
parent f34c0ac903
commit 704f4e44c6
9 changed files with 475 additions and 11 deletions

View File

@ -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<resT>::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

110
include/video/Texture.hpp Normal file
View File

@ -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 <ResourceManaged>, all memory management is
// taken care of.
//
// Children:
// <Image>
//
// Operators:
// - Texture = Texture
// - bool : True if texture is loaded, false if not.
// - ostream& << Texture
class Texture: public ResourceManaged<TextureResourceManager>
{
// 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 <open>.
//
// Parameters:
// name - Name of the Texture <Resource> to open.
//
// See Also:
// <open>
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 <a href="http://corona.sf.net">Corona</a>.
//
// Parameters:
// name - Name of the Texture <Resource> 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

View File

@ -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<TextureResource>
{
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

View File

@ -15,12 +15,9 @@
<icon BUILTIN="button_ok"/>
</node>
<node ID="Freemind_Link_50716011" TEXT="Texture">
<icon BUILTIN="button_ok"/>
<node ID="Freemind_Link_561271163" TEXT="Font"/>
<node ID="Freemind_Link_385334177" TEXT="Image"/>
<node ID="Freemind_Link_973985797" TEXT="TEST">
<font BOLD="true" NAME="SansSerif" SIZE="12"/>
<icon BUILTIN="messagebox_warning"/>
</node>
</node>
<node ID="Freemind_Link_1851655735" TEXT="Music"/>
<node ID="Freemind_Link_1045379727" TEXT="Sound"/>

View File

@ -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

View File

@ -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;
//

93
src/video/Texture.cpp Normal file
View File

@ -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 <class T>
T ResourceManaged<T>::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<TextureResourceManager>::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<TextureResourceManager>::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_ << " }";
}
}
}

View File

@ -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 <iostream>
#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<ubyte> 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);
}
}
}

89
test/Texture_test.cpp Normal file
View File

@ -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 <boost/lexical_cast.hpp>
// 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<std::string>(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)