initial compiling version with Image added

This commit is contained in:
James Turk 2005-06-13 07:03:09 +00:00
parent 704f4e44c6
commit 6e1eae1ed1
6 changed files with 396 additions and 14 deletions

View File

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

189
include/video/Image.hpp Normal file
View File

@ -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 <Texture>, which is a resource managed class.
// <Texture> 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 <ResourceManaged>, 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 <open>.
//
// Parameters:
// name - Name of the Image <Resource> to open.
//
// See Also:
// <open>
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 <a href="http://corona.sf.net">Corona</a>.
//
// Parameters:
// name - Name of the Image <Resource> 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 <ubytes> 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 <drawRotated> and <drawClipped> 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 <ubytes> 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 <ubytes> 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:
// <setAlpha>
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

View File

@ -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 <open>.
//
// Parameters:
// name - Name of the Texture <Resource> to open.
// name - Name of the Texture <Resource> to open.
//
// See Also:
// <open>
// <open>
Texture(const std::string& name);
// Group: General

View File

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

View File

@ -16,8 +16,8 @@
</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_561271163" TEXT="Font"/>
</node>
<node ID="Freemind_Link_1851655735" TEXT="Music"/>
<node ID="Freemind_Link_1045379727" TEXT="Sound"/>
@ -41,7 +41,7 @@
<node ID="Freemind_Link_714736465" TEXT="Drawing Program?"/>
</node>
</node>
<node ID="Freemind_Link_486829238" POSITION="right" TEXT="0.2 Release">
<node FOLDED="true" ID="Freemind_Link_486829238" POSITION="right" TEXT="0.2 Release">
<node ID="Freemind_Link_216021234" TEXT="Sprite System">
<font NAME="SansSerif" SIZE="12"/>
</node>
@ -62,15 +62,7 @@
</node>
</node>
<node ID="Freemind_Link_511487087" POSITION="left" TEXT="Website">
<node ID="Freemind_Link_1605443386" TEXT="cozTheme">
<icon BUILTIN="help"/>
</node>
<node ID="Freemind_Link_1050052437" TEXT="phpWebSite">
<icon BUILTIN="help"/>
</node>
<node ID="Freemind_Link_1940448315" TEXT="Wiki">
<icon BUILTIN="help"/>
</node>
<node ID="Freemind_Link_502036778" TEXT="cozTheme with a Wiki"/>
</node>
<node FOLDED="true" ID="Freemind_Link_351891371" POSITION="left" TEXT="General Mateinance">
<font BOLD="true" NAME="SansSerif" SIZE="12"/>
@ -81,6 +73,8 @@
</node>
<node ID="Freemind_Link_682620075" POSITION="left" TEXT="Current Problems">
<font BOLD="true" NAME="SansSerif" SIZE="12"/>
<node ID="Freemind_Link_71804323" TEXT="investigate revising resource system"/>
<node ID="Freemind_Link_1424720553" TEXT="Image is not tested at all"/>
</node>
</node>
</map>

197
src/video/Image.cpp Normal file
View File

@ -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_ << " }";
}
}
}