From 6e1eae1ed10751285d0d8c69cf1a4aea85db3d2c Mon Sep 17 00:00:00 2001 From: James Turk Date: Mon, 13 Jun 2005 07:03:09 +0000 Subject: [PATCH] initial compiling version with Image added --- include/photon.hpp | 1 + include/video/Image.hpp | 189 ++++++++++++++++++++++++++++++++++++ include/video/Texture.hpp | 6 +- ndoc/Menu.txt | 1 + photon.mm | 16 +--- src/video/Image.cpp | 197 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 396 insertions(+), 14 deletions(-) create mode 100644 include/video/Image.hpp create mode 100644 src/video/Image.cpp diff --git a/include/photon.hpp b/include/photon.hpp index f22e933..1c7a39a 100644 --- a/include/photon.hpp +++ b/include/photon.hpp @@ -26,6 +26,7 @@ #include "util/FileBuffer.hpp" #include "util/filesys/filesys.hpp" #include "video/Pen.hpp" +#include "video/Image.hpp" #include "video/Texture.hpp" #include "video/Color.hpp" #include "video/TextureResourceManager.hpp" diff --git a/include/video/Image.hpp b/include/video/Image.hpp new file mode 100644 index 0000000..438320f --- /dev/null +++ b/include/video/Image.hpp @@ -0,0 +1,189 @@ +//This file is part of Photon (http://photon.sourceforge.net) +//Copyright (C) 2004-2005 James Turk +// +// Author: +// James Turk (jpt2433@rit.edu) +// +// Version: +// $Id: Image.hpp,v 1.1 2005/06/13 07:05:28 cozman Exp $ + +#ifndef PHOTON_VIDEO_IMAGE_HPP +#define PHOTON_VIDEO_IMAGE_HPP + +#include "video/Texture.hpp" + +namespace photon +{ +namespace video +{ + +// Class: Image +// 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. +// An Image simply adds the ability to draw to a quad, as well as some rotation +// per-vertex tinting, and several other bonuses. +// +// Since Image is a child of , all memory management is taken +// care of. +// +// Operators: +// - Texture = Texture +// - operator bool +// - ostream& << Texture +class Image : public Texture +{ + +// Group: (Con/De)structors +public: + // Function: Image + // Default constructor, initalizes internal state of Image. + Image(); + + + // Function: Image + // Copy constructor, copies another Image. + // + // Parameters: + // rhs - Image to construct copy of. + Image(const Image &rhs); + + // Function: Image + // Initializing constructor, loads Image via call to . + // + // Parameters: + // name - Name of the Image to open. + // + // See Also: + // + Image(const std::string& name); + + Image& operator=(const Image &rhs); + +// Group: General +public: + // Function: open + // Opens an image file, currently supported image types are BMP, GIF, JPEG, + // PCX, PNG, and TGA. (Images can be any dimensions) + // + // Loading is done via Corona. + // + // Parameters: + // name - Name of the Image to open. + void open(const std::string& name); + + // Function: setAlpha + // Sets alpha-component for Image to be used when surface is drawn. + // + // Parameters: + // alpha - Desired surface-wide alpha component for Image. + void setAlpha(ubyte alpha); + + // Function: flip + // Flips image, horizontally and/or vertically. + // + // Parameters: + // horizontal - True if a flip over horizontal axis is desired. + // vertical - True if a flip over vertical axis is desired. + void flip(bool horizontal, bool vertical); + + // Function: stretch + // Stretch image by a given factor. + // For example: 1 = no change, .5 = 50% current size, 10 = 10 times current + // size. + // + // Parameters: + // xFactor - Factor for horizontal stretch. + // yFactor - Factor for vertical stretch. + void stretch(scalar xFactor, scalar yFactor); + + // Function: resize + // Resize the image to a specific size. + // + // Parameters: + // width - New desired with for image. + // height - New desired height for image. + void resize(scalar width, scalar height); + +// Group: Drawing +public: + + // Function: draw + // draws image to the screen at a givem positon. + // + // Parameters: + // x - X position to draw image at. + // y - Y position to draw image at. + void draw(scalar x, scalar y) const; + + // Function: draw + // Draw image to screen at given position. + // + // This version has an extra parameter, a color vertex option. The 3rd + // parameter is an array of 16 defining, + // Starting in the top left corner & going counterclockwise, tint colors + // for the image. It is possible to make very nice effects with little + // effort via this option. Things like shading and lighting can be bluffed + // using this technique. + // + // There are versions of and that have the vc + // parameter which works in the same manner. + // + // Parameters: + // x - X position to draw image at. + // y - Y position to draw image at. + // vc - Array of 16 defining the colors of the 4 vertices in + // clockwise order. (Order: R1,G1,B1,A1,...B4,A4) + void draw(scalar x, scalar y, ubyte vc[]) const; + + // Function: drawRotated + // Draw image, rotated about it's own center by a specified angle. + // + // Parameters: + // x - X position to draw image at. (before rotation) + // y - Y position to draw image at. (before rotation) + // angle - Angle (in degrees) to rotate image. + void drawRotated(scalar x, scalar y, scalar angle) const; + + // Function: drawRotated + // Draw image, rotated about it's own center by a specified angle. + // Includes color vertex option. + // + // Parameters: + // x - X position to draw image at. (before rotation) + // y - Y position to draw image at. (before rotation) + // angle - Angle (in degrees) to rotate image. + // vc - Array of 16 defining the colors of the 4 vertices in + // clockwise order. (Order: R1,G1,B1,A1,...B4,A4) + void drawRotated(scalar x, scalar y, scalar angle, ubyte vc[]) const; + +// Group: Accessors +public: + + // Function: getAlpha + // Get current surface-wide alpha value for image. + // + // Returns: + // Current surface-wide alpha. + // + // See Also: + // + ubyte getAlpha() const; + + friend std::ostream& operator<<(std::ostream &o, const Image &rhs); + +private: + ubyte alpha_; + int texMinX_; + int texMinY_; + int texMaxX_; + int texMaxY_; + scalar width_; + scalar height_; +}; + +} +} + +#endif //PHOTON_VIDEO_IMAGE_HPP diff --git a/include/video/Texture.hpp b/include/video/Texture.hpp index 156fd6f..4801b63 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.1 2005/06/13 05:38:06 cozman Exp $ +// $Id: Texture.hpp,v 1.2 2005/06/13 07:05:28 cozman Exp $ #ifndef PHOTON_VIDEO_TEXTURE_HPP #define PHOTON_VIDEO_TEXTURE_HPP @@ -53,10 +53,10 @@ public: // Initializing constructor, loads Texture via call to . // // Parameters: - // name - Name of the Texture to open. + // name - Name of the Texture to open. // // See Also: - // + // Texture(const std::string& name); // Group: General diff --git a/ndoc/Menu.txt b/ndoc/Menu.txt index b72cb62..2cd087a 100644 --- a/ndoc/Menu.txt +++ b/ndoc/Menu.txt @@ -66,6 +66,7 @@ Group: photon:: { File: Color (video/Color.hpp) File: Pen (video/Pen.hpp) + File: Texture (video/Texture.hpp) File: VideoCore (video/VideoCore.hpp) } # Group: Video diff --git a/photon.mm b/photon.mm index 55546cd..e557c68 100644 --- a/photon.mm +++ b/photon.mm @@ -16,8 +16,8 @@ - + @@ -41,7 +41,7 @@ - + @@ -62,15 +62,7 @@ - - - - - - - - - + @@ -81,6 +73,8 @@ + + diff --git a/src/video/Image.cpp b/src/video/Image.cpp new file mode 100644 index 0000000..5c536d8 --- /dev/null +++ b/src/video/Image.cpp @@ -0,0 +1,197 @@ +//This file is part of Photon (http://photon.sourceforge.net) +//Copyright (C) 2004-2005 James Turk +// +// Author: +// James Turk (jpt2433@rit.edu) +// +// Version: +// $Id: Image.cpp,v 1.1 2005/06/13 07:04:29 cozman Exp $ + +#include "video/Image.hpp" + +#include "GL/gl.h" + +namespace photon +{ +namespace video +{ + +Image::Image() : + Texture(), + alpha_(255), + texMinX_(0), texMinY_(0), texMaxX_(1), texMaxY_(1) +{ +} + +Image::Image(const Image &rhs) : + Texture(rhs), + alpha_(rhs.alpha_), + texMinX_(rhs.texMinX_), texMinY_(rhs.texMinY_), + texMaxX_(rhs.texMaxX_), texMaxY_(rhs.texMaxY_) +{ + +} + +Image::Image(const std::string& name) : + alpha_(255), + texMinX_(0), texMinY_(0), texMaxX_(1), texMaxY_(1) +{ + open(name); +} + +Image& Image::operator=(const Image &rhs) +{ + if(&rhs != this) + { + Texture::operator=(rhs); + alpha_ = rhs.alpha_; + texMinX_ = rhs.texMinX_; + texMinY_ = rhs.texMinY_; + texMaxX_ = rhs.texMaxX_; + texMaxY_ = rhs.texMaxY_; + } + return *this; +} + +void Image::open(const std::string& name) +{ + Texture::open(name); +} + +void Image::setAlpha(ubyte alpha) +{ + alpha_ = alpha; +} + +void Image::flip(bool horizontal, bool vertical) +{ + //all that a flip does is invert the Min/Max coordinates + if(horizontal) + { + std::swap(texMinX_,texMaxX_); + } + if(vertical) + { + std::swap(texMinY_,texMaxY_); + } +} + +//stretching and resizing is very inexpensive, done via variables +void Image::stretch(scalar xFactor, scalar yFactor) +{ + width_ = xFactor*width_; + height_ = yFactor*height_; +} + +void Image::resize(scalar width, scalar height) +{ + width_ = width; + height_ = height; +} + +void Image::draw(scalar x, scalar y) const +{ + glColor4ub(255,255,255,alpha_); + bind(); + glBegin(GL_QUADS); + glTexCoord2i(texMinX_,texMinY_); glVertex2d(x,y); + glTexCoord2i(texMaxX_,texMinY_); glVertex2d(x+width_,y); + glTexCoord2i(texMaxX_,texMaxY_); glVertex2d(x+width_,y+height_); + glTexCoord2i(texMinX_,texMaxY_); glVertex2d(x,y+height_); + glEnd(); + glColor4ub(255,255,255,255); +} + +void Image::draw(scalar x, scalar y, ubyte vc[]) const +{ + bind(); + glBegin(GL_QUADS); + glTexCoord2i(texMinX_,texMinY_); + glColor4ub(vc[0],vc[1],vc[2],vc[3]); + glVertex2d(x,y); + + glTexCoord2i(texMaxX_,texMinY_); + glColor4ub(vc[4],vc[5],vc[6],vc[7]); + glVertex2d(x+width_,y); + + glTexCoord2i(texMaxX_,texMaxY_); + glColor4ub(vc[8],vc[9],vc[10],vc[11]); + glVertex2d(x+width_,y+height_); + + glTexCoord2i(texMinX_,texMaxY_); + glColor4ub(vc[12],vc[13],vc[14],vc[15]); + glVertex2d(x,y+height_); + glEnd(); + glColor4ub(255,255,255,255); +} + +void Image::drawRotated(scalar x, scalar y, scalar angle) const +{ + //center point + scalar cX = width_/2., cY = height_/2.; + + 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); + bind(); + //draw is modified to be based around center + glBegin(GL_QUADS); + glTexCoord2i(texMinX_,texMinY_); glVertex2d(-cX,-cY); + glTexCoord2i(texMaxX_,texMinY_); glVertex2d(-cX+width_,-cY); + glTexCoord2i(texMaxX_,texMaxY_); glVertex2d(-cX+width_,-cY+height_); + glTexCoord2i(texMinX_,texMaxY_); glVertex2d(-cX,-cY+height_); + glEnd(); + glPopMatrix(); + glColor4ub(255,255,255,255); +} + +void Image::drawRotated(scalar x, scalar y, scalar angle, ubyte vc[]) const +{ + //center point + scalar cX,cY; + cX = width_/2.0f; + cY = height_/2.0f; + + 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 + bind(); + //draw is modified to be based around center + glBegin(GL_QUADS); + glTexCoord2i(texMinX_,texMinY_); + glColor4ub(vc[0],vc[1],vc[2],vc[3]); + glVertex2d(-cX,-cY); + + glTexCoord2i(texMaxX_,texMinY_); + glColor4ub(vc[4],vc[6],vc[6],vc[7]); + glVertex2d(-cX+width_,-cY); + + glTexCoord2i(texMaxX_,texMaxY_); + glColor4ub(vc[8],vc[9],vc[10],vc[11]); + glVertex2d(-cX+width_,-cY+height_); + + glTexCoord2i(texMinX_,texMaxY_); + glColor4ub(vc[12],vc[13],vc[14],vc[15]); + glVertex2d(-cX,-cY+height_); + glEnd(); + glPopMatrix(); + glColor4ub(255,255,255,255); +} + +ubyte Image::getAlpha() const +{ + return alpha_; +} + +std::ostream& operator<<(std::ostream &o, const Image &rhs) +{ + return o << "Image: { ResID: " << rhs.resID_ << + " Dimensions: " << rhs.width_ << "x" << rhs.height_ + << " Alpha: " << rhs.alpha_ << " }"; +} + +} +}