Gel2D - The free/open source game creation suite

gelSprite.h
Go to the documentation of this file.
00001 /*
00002 Gel2D Game Engine - Cross-platform 2D gaming middleware
00003 Copyright (C) 2010-2011 Mark D. Procarione
00004 
00005 Gel2D is free software: you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation, either version 3 of the License, or
00008 (at your option) any later version.
00009 
00010 Gel2D is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 GNU General Public License for more details.
00014 */
00015 
00016 #ifndef __GELQUAD_H__
00017 #define __GELQUAD_H__
00018 
00019 #include "gelObject.h"
00020 #include "gelTimer.h"
00021 
00022 namespace gel
00023 {
00025     struct GelVert
00026     {
00028         int z;
00030         Gel2dVec pos;
00032         Gel2dVec uv;
00034         GelColor col;
00035     };
00036 
00038     class GelSprite : public GelObject, public GelTextureObject
00039     {
00040         public:
00042             GelSprite();
00043 
00045 
00048             GelSprite( Gel2dVec obj_size, Gel2dVec obj_pos, float obj_rot );
00049 
00051 
00057             void setSize( Gel2dVec size );
00058 
00060 
00062             void flip( int axis );
00063 
00065 
00068             GelColor getCol( int i = 0 ) const;
00070 
00073             void setCol( GelColor col, int i = -1 );
00075 
00080             void setRGB( float r, float g, float b, int i = -1 );
00082 
00085             float getAlpha( int i = 0 ) const;
00087 
00091             void setAlpha( float alpha, int i = -1 );
00092 
00093             void mapRect( const Gel2dVec &pos, const Gel2dVec &size );
00094 
00096 
00097             void setTexture( GelTexture *tex );
00098 
00100 
00101             void flipTexture( Orientation axis );
00102 
00104 
00105             bool isTexFlipped( Orientation axis ) const;
00106 
00108             GelAABB getAABB() const;
00109 
00111             void render();
00112 
00113         protected:
00114                         int _pointIntersect( float locx, float locy );
00115 
00116         public:
00118             GelVert v[4];
00119 
00120         protected:
00121             int xFlip;
00122             int yFlip;
00123     };
00124 
00126     class GelAnimSprite : public GelSprite
00127     {
00128                 public:
00130             GelAnimSprite();
00131 
00133 
00140             GelAnimSprite( Gel2dVec obj_size, Gel2dVec obj_pos, int col, int row, int stframe, int endframe, int nfps );
00141 
00143 
00148             void setup( int col, int row, int stframe, int endframe, int nfps );
00149 
00151 
00152             int getMode() const { return mode; }
00153 
00155 
00164             void setMode( int nmode ) { mode = nmode; }
00165 
00167 
00168             int getFPS() const { return fps; }
00169 
00171 
00172             void setFPS( int nfps ) { fps = nfps; speed = 1.0f/nfps; }
00173 
00175 
00176             int getColumns() const { return ncolumns; }
00177 
00179 
00180             void setColumns( int columns ) { ncolumns = columns; }
00181 
00183 
00184             int getRows() const { return nrows; }
00185 
00187 
00188             void setRows( int rows ) { nrows = rows; }
00189 
00190             int  getCurFrame()     const { return curFrame; }
00191             void setCurFrame(int frame)  { curFrame = frame; }
00192             int  getStartFrame()   const { return stFrame; }
00193             void setStartFrame(int frame){ stFrame = frame; }
00194             int  getEndFrame()     const { return endFrame; }
00195             void setEndFrame(int frame)  { endFrame = frame; }
00196             int  getNumFrames()    const { return endFrame-stFrame; }
00197 
00199 
00202             int getPlayState() const { return playState; }
00203 
00205 
00207             void setPlayState( int state ) { playState = state; }
00208 
00210 
00211             void update();
00212 
00214             void render();
00215 
00216         private:
00217             void tileMapUV();
00218 
00219         private:
00220             int mode;           // play mode - fwd, rev, loop, pinpong, etc.
00221             int fps;            // frame rate
00222             int ncolumns;       // number of columns in tile animation
00223             int nrows;          // number of rows in tile animation
00224             int curFrame;       // current frame of tile animation
00225 
00226             GelTimer timer;     // timer - d'oh!
00227             double elapsedTime; // time since last animation frame update
00228             float speed;        // animation speed (1.0f/fps)
00229 
00230             int stFrame;        // start frame
00231             int endFrame;       // end frame
00232 
00233             float column;       // current column
00234             float row;          // current row
00235 
00236             bool pingpong;      // stage of pingpong
00237             int playState;      // play, stop, pause
00238         };
00239 }
00240 
00241 #endif // __GELQUAD_H__