diff --git a/include/video/Font.hpp b/include/video/Font.hpp index cf96fc5..bd9542f 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.6 2005/07/18 05:14:18 cozman Exp $ +// $Id: Font.hpp,v 1.7 2005/07/18 07:19:48 cozman Exp $ #ifndef PHOTON_VIDEO_FONT_HPP #define PHOTON_VIDEO_FONT_HPP @@ -33,7 +33,7 @@ std::ostream& operator<<(std::ostream& os, const StreamFlusher& rhs); // - Font = Font // - bool : True if font is loaded, false if not. // - ostream& << Font -class Font: public ResourceManaged +class Font : public ResourceManaged { // Group: (Con/De)structors public: @@ -98,16 +98,16 @@ public: // Group: Drawing public: - void drawText(float x, float y, const char *str, ...) const; - void drawText(float x, float y, const std::string& str) const; + void drawText(scalar x, scalar y, const char *str, ...) const; + void drawText(scalar x, scalar y, const std::string& str) const; - std::ostream& beginDraw(float x, float y); + std::ostream& beginDraw(scalar x, scalar y); StreamFlusher endDraw(); // Group: Font Metrics public: - unsigned int calcStringWidth(const std::string& str) const; - unsigned int getHeight() const; + uint calcStringWidth(const std::string& str) const; + uint getHeight() const; // Group: Resource Creation public: @@ -140,8 +140,8 @@ private: ubyte height_; // stream drawing stuff std::ostringstream ss_; - float drawX_; - float drawY_; + scalar drawX_; + scalar drawY_; // color Color color_; }; diff --git a/include/video/FontResourceManager.hpp b/include/video/FontResourceManager.hpp index c63ceb2..3357422 100644 --- a/include/video/FontResourceManager.hpp +++ b/include/video/FontResourceManager.hpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: FontResourceManager.hpp,v 1.3 2005/07/03 06:33:19 cozman Exp $ +// $Id: FontResourceManager.hpp,v 1.4 2005/07/18 07:19:48 cozman Exp $ #ifndef PHOTON_VIDEO_FONTRESOURCEMANAGER_HPP #define PHOTON_VIDEO_FONTRESOURCEMANAGER_HPP @@ -57,10 +57,10 @@ private: virtual void freeResourceData(FontResource &res); private: - FT_Library library_; + FT_Library library_; // only need one FT_Library, so manager owns it public: - static const unsigned int SPACE = 32; - static const unsigned int NUM_CHARS = 96; + static const uint SPACE = 32; + static const uint NUM_CHARS = 96; }; } diff --git a/include/video/Image.hpp b/include/video/Image.hpp index 438320f..57417bd 100644 --- a/include/video/Image.hpp +++ b/include/video/Image.hpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: Image.hpp,v 1.1 2005/06/13 07:05:28 cozman Exp $ +// $Id: Image.hpp,v 1.2 2005/07/18 07:19:48 cozman Exp $ #ifndef PHOTON_VIDEO_IMAGE_HPP #define PHOTON_VIDEO_IMAGE_HPP @@ -18,7 +18,7 @@ namespace video { // Class: Image -// Image is a class which is used to store a single image, for use in 2D games. +// Image is a class which is used to store a single image, for use in 2D games. // // Image is derived from , which is a resource managed class. // is a simple OO wrapper around the concept of a texture in OpenGL. diff --git a/include/video/TextureResourceManager.hpp b/include/video/TextureResourceManager.hpp index ab98123..ddc2f9e 100644 --- a/include/video/TextureResourceManager.hpp +++ b/include/video/TextureResourceManager.hpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: TextureResourceManager.hpp,v 1.3 2005/07/03 06:33:19 cozman Exp $ +// $Id: TextureResourceManager.hpp,v 1.4 2005/07/18 07:19:48 cozman Exp $ #ifndef PHOTON_VIDEO_TEXTURERESOURCEMANAGER_HPP #define PHOTON_VIDEO_TEXTURERESOURCEMANAGER_HPP @@ -24,7 +24,7 @@ public: uint texID; uint width; uint height; - ubyte *pixels; + ubyte *pixels; // keep pixels around for future use (post-load colorkeying) }; class TextureResourceManager : public ResourceManager diff --git a/include/video/VideoCore.hpp b/include/video/VideoCore.hpp index 1e86ecb..4c866d8 100644 --- a/include/video/VideoCore.hpp +++ b/include/video/VideoCore.hpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: VideoCore.hpp,v 1.2 2005/03/15 18:53:12 cozman Exp $ +// $Id: VideoCore.hpp,v 1.3 2005/07/18 07:19:48 cozman Exp $ #ifndef PHOTON_VIDEO_VIDEOCORE_HPP #define PHOTON_VIDEO_VIDEOCORE_HPP @@ -159,7 +159,7 @@ public: // behind the scenes private: - void initOpenGL(); + void initOpenGL(); // set desired OpenGL options // data members private: diff --git a/src/video/Font.cpp b/src/video/Font.cpp index d4fd374..57217bc 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.5 2005/07/17 07:14:09 cozman Exp $ +// $Id: Font.cpp,v 1.6 2005/07/18 07:19:48 cozman Exp $ #include "video/Font.hpp" @@ -36,6 +36,7 @@ Font::Font(const Font &rhs) : ResourceManaged(rhs), drawX_(0), drawY_(0) { + // a font is a texture, 96 lists, 96 widths, and a height resMgr_.getFontData(getName(), texID_, listBase_, widths_, height_); } @@ -53,6 +54,7 @@ void Font::open(const std::string& name) bool Font::isValid() const { + // valid if texture is created return glIsTexture(texID_) == GL_TRUE; } @@ -81,7 +83,7 @@ Color Font::getColor() const return color_; } -void Font::drawText(float x, float y, const char *str, ...) const +void Font::drawText(scalar x, scalar y, const char *str, ...) const { if(!isValid()) { @@ -97,15 +99,15 @@ void Font::drawText(float x, float y, const char *str, ...) const // push attrib before setting color glPushAttrib(GL_CURRENT_BIT); - glColor4ub(color_.red, color_.green, color_.blue, color_.alpha); + color_.makeGLColor(); glBindTexture(GL_TEXTURE_2D, texID_); glPushMatrix(); glTranslated(x,y,0); - for(unsigned int i=0; i < std::strlen(buf); ++i) + for(uint i=0; i < std::strlen(buf); ++i) { // ch-SPACE = DisplayList offset - unsigned char ch( buf[i] - FontResourceManager::SPACE ); + ubyte ch( buf[i] - FontResourceManager::SPACE ); // replace characters outside the valid range with undrawable if(ch > FontResourceManager::NUM_CHARS) { @@ -123,7 +125,7 @@ void Font::drawText(float x, float y, const char *str, ...) const glPopAttrib(); } -void Font::drawText(float x, float y, const std::string& str) const +void Font::drawText(scalar x, scalar y, const std::string& str) const { if(!isValid()) { @@ -132,7 +134,7 @@ void Font::drawText(float x, float y, const std::string& str) const // push attrib before setting color glPushAttrib(GL_CURRENT_BIT); - glColor4ub(color_.red, color_.green, color_.blue, color_.alpha); + color_.makeGLColor(); glBindTexture(GL_TEXTURE_2D, texID_); glPushMatrix(); @@ -140,7 +142,7 @@ void Font::drawText(float x, float y, const std::string& str) const for(std::string::const_iterator i = str.begin(); i != str.end(); ++i) { // ch-SPACE = DisplayList offset - unsigned char ch( *i - FontResourceManager::SPACE ); + ubyte ch( *i - FontResourceManager::SPACE ); // replace characters outside the valid range with undrawable if(ch > FontResourceManager::NUM_CHARS) { @@ -158,7 +160,7 @@ void Font::drawText(float x, float y, const std::string& str) const glPopAttrib(); } -std::ostream& Font::beginDraw(float x, float y) +std::ostream& Font::beginDraw(scalar x, scalar y) { // clear the string and store the draw-position ss_.str(""); @@ -171,28 +173,28 @@ StreamFlusher Font::endDraw() { drawText(drawX_, drawY_, ss_.str()); // draw the string ss_.str(""); // clear the buffer - return StreamFlusher(); + return StreamFlusher(); // special null class that simply outputs nothing } -unsigned int Font::calcStringWidth(const std::string& str) const +uint Font::calcStringWidth(const std::string& str) const { if(!isValid()) { throw PreconditionException("Invalid Font::calcStringWidth call."); } - unsigned int width=0; + uint width(0); // accumulator for widths // iterate through widths of each char and accumulate width of string for(std::string::const_iterator i = str.begin(); i < str.end(); ++i) { - width += widths_[static_cast(*i) - + width += widths_[static_cast(*i) - FontResourceManager::SPACE]; } return width; } -unsigned int Font::getHeight() const +uint Font::getHeight() const { if(!isValid()) { diff --git a/src/video/FontResourceManager.cpp b/src/video/FontResourceManager.cpp index 876a973..6a5993e 100644 --- a/src/video/FontResourceManager.cpp +++ b/src/video/FontResourceManager.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: FontResourceManager.cpp,v 1.3 2005/07/03 06:33:19 cozman Exp $ +// $Id: FontResourceManager.cpp,v 1.4 2005/07/18 07:19:48 cozman Exp $ #include "video/FontResourceManager.hpp" @@ -18,6 +18,7 @@ namespace video FontResourceManager::FontResourceManager() { + // initialize library upon creation if(FT_Init_FreeType(&library_) != 0) { throw APIError("Could not initialize FreeType2 library."); @@ -26,6 +27,7 @@ FontResourceManager::FontResourceManager() FontResourceManager::~FontResourceManager() { + // deinitialize library on destruction FT_Done_FreeType(library_); } @@ -188,6 +190,7 @@ void FontResourceManager::loadResourceData(FontResource &res, void FontResourceManager::freeResourceData(FontResource &res) { + // free both the lists and the texture if(glIsList(res.listBase)) { glDeleteLists(res.listBase, NUM_CHARS); diff --git a/src/video/Image.cpp b/src/video/Image.cpp index 80dec8d..79a7f7f 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.3 2005/06/27 04:24:16 cozman Exp $ +// $Id: Image.cpp,v 1.4 2005/07/18 07:19:48 cozman Exp $ #include "video/Image.hpp" @@ -41,6 +41,7 @@ Image& Image::operator=(const Image &rhs) { if(&rhs != this) { + // copy texture and all Image members Texture::operator=(rhs); alpha_ = rhs.alpha_; texMinX_ = rhs.texMinX_; @@ -93,6 +94,8 @@ void Image::resize(scalar width, scalar height) void Image::draw(scalar x, scalar y) const { + // avoid wonky coloring, but use alpha + glPushAttrib(GL_CURRENT_BIT); glColor4ub(255,255,255,alpha_); bind(); glBegin(GL_QUADS); @@ -101,11 +104,13 @@ void Image::draw(scalar x, scalar y) const glTexCoord2i(texMaxX_,texMaxY_); glVertex2d(x+width_,y+height_); glTexCoord2i(texMinX_,texMaxY_); glVertex2d(x,y+height_); glEnd(); - glColor4ub(255,255,255,255); + glPopAttrib(); } void Image::draw(scalar x, scalar y, ubyte vc[]) const { + // store current state, so vc doesn't destroy + glPushAttrib(GL_CURRENT_BIT); bind(); glBegin(GL_QUADS); glTexCoord2i(texMinX_,texMinY_); @@ -124,7 +129,7 @@ void Image::draw(scalar x, scalar y, ubyte vc[]) const glColor4ub(vc[12],vc[13],vc[14],vc[15]); glVertex2d(x,y+height_); glEnd(); - glColor4ub(255,255,255,255); + glPopAttrib(); } void Image::drawRotated(scalar x, scalar y, scalar angle) const @@ -132,11 +137,14 @@ void Image::drawRotated(scalar x, scalar y, scalar angle) const //center point scalar cX = width_/2., cY = height_/2.; + // avoid wonky coloring, but use alpha + glPushAttrib(GL_CURRENT_BIT); + glPushMatrix(); glLoadIdentity(); glTranslated(x+cX,y+cY,0); //translate to center glRotated(angle,0,0,1.0f); //rotate on z axis, to keep x&y in 2D plane - glColor4ub(255,255,255,255); + glColor4ub(255,255,255,alpha_); bind(); //draw is modified to be based around center glBegin(GL_QUADS); @@ -146,7 +154,7 @@ void Image::drawRotated(scalar x, scalar y, scalar angle) const glTexCoord2i(texMinX_,texMaxY_); glVertex2d(-cX,-cY+height_); glEnd(); glPopMatrix(); - glColor4ub(255,255,255,255); + glPopAttrib(); } void Image::drawRotated(scalar x, scalar y, scalar angle, ubyte vc[]) const @@ -156,6 +164,8 @@ void Image::drawRotated(scalar x, scalar y, scalar angle, ubyte vc[]) const cX = width_/2.0f; cY = height_/2.0f; + // store current state, so vc doesn't destroy + glPushAttrib(GL_CURRENT_BIT); glPushMatrix(); glLoadIdentity(); glTranslated(x+cX,y+cY,0); //translate to center @@ -180,7 +190,7 @@ void Image::drawRotated(scalar x, scalar y, scalar angle, ubyte vc[]) const glVertex2d(-cX,-cY+height_); glEnd(); glPopMatrix(); - glColor4ub(255,255,255,255); + glPopAttrib(); } ubyte Image::getAlpha() const diff --git a/src/video/Pen.cpp b/src/video/Pen.cpp index 7ac4a23..0ed05e8 100644 --- a/src/video/Pen.cpp +++ b/src/video/Pen.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: Pen.cpp,v 1.3 2005/04/21 19:30:19 cozman Exp $ +// $Id: Pen.cpp,v 1.4 2005/07/18 07:19:48 cozman Exp $ #include "video/Pen.hpp" @@ -46,22 +46,24 @@ void Pen::setColor(const Color& color) void Pen::drawPoint(const math::Point2& point) const { glBindTexture(GL_TEXTURE_2D,0); + glPushAttrib(GL_CURRENT_BIT); color_.makeGLColor(); - glBegin(GL_POINTS); + glBegin(GL_POINTS); // draw single point glVertex2d(point.x,point.y); glEnd(); - glColor4ub(255,255,255,255); + glPopAttrib(); } void Pen::drawLine(const math::Point2& p1, const math::Point2& p2) const { glBindTexture(GL_TEXTURE_2D,0); + glPushAttrib(GL_CURRENT_BIT); color_.makeGLColor(); - glBegin(GL_LINES); + glBegin(GL_LINES); // draw single line glVertex2d(p1.x,p1.y); glVertex2d(p2.x,p2.y); glEnd(); - glColor4ub(255,255,255,255); + glPopAttrib(); } void Pen::drawVector(const math::Point2& point, @@ -71,7 +73,7 @@ void Pen::drawVector(const math::Point2& point, math::Vector2 v; x2 = point.x+vector.x; y2 = point.y+vector.y; - //calculate an arrow (5pi/6) + //calculate an arrow head (5pi/6 radian angle offset) v.resolveRad(vector.getMagnitude()/5,vector.getAngleRad()+(5./6)*math::PI); x3 = x2+v.x; y3 = y2-v.y; @@ -80,47 +82,50 @@ void Pen::drawVector(const math::Point2& point, y4 = y2-v.y; glBindTexture(GL_TEXTURE_2D,0); + glPushAttrib(GL_CURRENT_BIT); color_.makeGLColor(); - glBegin(GL_LINE_STRIP); + glBegin(GL_LINE_STRIP); // use line strip to draw arrow glVertex2d(point.x,point.y); glVertex2d(x2,y2); glVertex2d(x3,y3); glVertex2d(x4,y4); glVertex2d(x2,y2); glEnd(); - glColor4ub(255,255,255,255); + glPopAttrib(); } void Pen::drawRectangle(const math::Rect &rect) const { glBindTexture(GL_TEXTURE_2D,0); + glPushAttrib(GL_CURRENT_BIT); color_.makeGLColor(); - glBegin(GL_LINE_STRIP); + glBegin(GL_LINE_STRIP); // use line strips to draw outline of rectangle glVertex2d(rect.getLeft(),rect.getTop()); glVertex2d(rect.getRight(),rect.getTop()); glVertex2d(rect.getRight(),rect.getBottom()); glVertex2d(rect.getLeft(),rect.getBottom()); glVertex2d(rect.getLeft(),rect.getTop()); glEnd(); - glColor4ub(255,255,255,255); + glPopAttrib(); } void Pen::fillRectangle(const math::Rect &rect) const { glBindTexture(GL_TEXTURE_2D,0); + glPushAttrib(GL_CURRENT_BIT); color_.makeGLColor(); - glBegin(GL_QUADS); + glBegin(GL_QUADS); // draw one colored quad glVertex2d(rect.getLeft(),rect.getTop()); glVertex2d(rect.getRight(),rect.getTop()); glVertex2d(rect.getRight(),rect.getBottom()); glVertex2d(rect.getLeft(),rect.getBottom()); glEnd(); - glColor4ub(255,255,255,255); + glPopAttrib(); } void Pen::drawCircle(const math::Circle &circle) const { - //written from Bresenham's algorithm + //written from Bresenham's circle algorithm double cx(circle.getCenter().x); double cy(circle.getCenter().y); double d(3-(2*circle.getRadius())); @@ -128,8 +133,10 @@ void Pen::drawCircle(const math::Circle &circle) const double y(circle.getRadius()); glBindTexture(GL_TEXTURE_2D,0); + glPushAttrib(GL_CURRENT_BIT); color_.makeGLColor(); - glBegin(GL_POINTS); + + glBegin(GL_POINTS); // draw points, creating a circle while(y > x) { glVertex2d(cx+x,cy+y); @@ -153,12 +160,12 @@ void Pen::drawCircle(const math::Circle &circle) const ++x; } glEnd(); - glColor4ub(255,255,255,255); + glPopAttrib(); } void Pen::fillCircle(const math::Circle &circle) const { - //written from Bresenham's algorithm + //written from Bresenham's circle algorithm double cx(circle.getCenter().x); double cy(circle.getCenter().y); double d(3-(2*circle.getRadius())); @@ -166,8 +173,10 @@ void Pen::fillCircle(const math::Circle &circle) const double y(circle.getRadius()); glBindTexture(GL_TEXTURE_2D,0); + glPushAttrib(GL_CURRENT_BIT); color_.makeGLColor(); - glBegin(GL_LINES); + + glBegin(GL_LINES); // draw lines instead of points, filling the circle while(y > x) { glVertex2d(cx+x,cy+y); @@ -191,7 +200,7 @@ void Pen::fillCircle(const math::Circle &circle) const ++x; } glEnd(); - glColor4ub(255,255,255,255); + glPopAttrib(); } } diff --git a/src/video/Texture.cpp b/src/video/Texture.cpp index 8a2bb5d..d24b457 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.4 2005/07/04 03:06:48 cozman Exp $ +// $Id: Texture.cpp,v 1.5 2005/07/18 07:19:48 cozman Exp $ #include "video/Texture.hpp" @@ -22,6 +22,7 @@ Texture::Texture() Texture::Texture(const Texture &rhs) : ResourceManaged(rhs) { + // the resource is width, height, and GL texture name/ID resMgr_.getTextureData(getName(), width_, height_, texID_); } @@ -32,6 +33,7 @@ Texture::Texture(const std::string& name) void Texture::open(const std::string& name) { + // open the texture and grab the resource contents ResourceManaged::open(name); resMgr_.getTextureData(getName(), width_, height_, texID_); } diff --git a/src/video/TextureResourceManager.cpp b/src/video/TextureResourceManager.cpp index 777cac0..4f7132e 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.4 2005/07/03 06:33:19 cozman Exp $ +// $Id: TextureResourceManager.cpp,v 1.5 2005/07/18 07:19:48 cozman Exp $ #include "video/TextureResourceManager.hpp" @@ -22,12 +22,14 @@ namespace video void TextureResourceManager::setGlobalColorKey(bool enabled, ubyte red, ubyte green, ubyte blue) { + // if enabled, sets alpha to 0, which is indicator that colorkeying is on colorKey_.setColor(red,green,blue,enabled?0:255); } void TextureResourceManager::getGlobalColorKey(bool &enabled, ubyte &red, ubyte &green, ubyte &blue) { + // if alpha is 0, colorkeying is enabled enabled = (colorKey_.alpha == 0); red = colorKey_.red; green = colorKey_.green; @@ -51,6 +53,7 @@ void TextureResourceManager::loadResourceData(TextureResource &res, util::FileBuffer buf(path.path); corona::File *file; + // load via FileBuffer to allow loading of archived content std::vector data = buf.getData(); file = corona::CreateMemoryFile((ubyte*)&data[0],data.size()); @@ -77,26 +80,30 @@ void TextureResourceManager::loadResourceData(TextureResource &res, std::memcpy(res.pixels,image->getPixels(),res.width*res.height*4); delete image; //no longer need image + // implementation of the color key if(colorKey_.alpha == 0) //ck alpha == 0, means colorkey on { ubyte r,g,b; ubyte *pxl=res.pixels; + // go through all pixels (width*height = numPixels) for(uint i=0; i < res.width*res.height; ++i) { - r = *pxl++; - g = *pxl++; - b = *pxl++; - //set current pixel alpha = 0 + r = *pxl++; // get red component + g = *pxl++; // get green component + b = *pxl++; // get blue component + + //set current pixel alpha = 0 if each component matches the colorKey if(r == colorKey_.red && g == colorKey_.green && b == colorKey_.blue) { - *pxl = 0; + *pxl = 0; // make transparent } - *pxl++; + *pxl++; // go to next pixel } } + // actually bind the OpenGL texture glGenTextures(1,&res.texID); glBindTexture(GL_TEXTURE_2D,res.texID); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); @@ -110,11 +117,13 @@ void TextureResourceManager::loadResourceData(TextureResource &res, void TextureResourceManager::freeResourceData(TextureResource &res) { + // pixels can be deleted if(res.pixels) { delete []res.pixels; res.pixels = 0; } + // free OpenGL texture identifier glDeleteTextures(1, &res.texID); }