Renders sprites using OpenGL
authorIvan Hernanez <iturtleman128@gmail.com>
Tue, 9 Aug 2011 07:35:11 +0000 (02:35 -0500)
committerIvan Hernanez <iturtleman128@gmail.com>
Tue, 9 Aug 2011 07:35:11 +0000 (02:35 -0500)
\todo render bg (load from animation file)
\draw collisions

Animation.cpp
LevelFns.h
Sprite.cpp
WorldObject.h
fns.h

index df05b43..039a4de 100644 (file)
@@ -118,10 +118,10 @@ int Animation::loadAnimation(std::string animFile)
                                SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, transparency[0], transparency[1], transparency[2]));
                                
                                /** Loads image into animation */
-                               SDL_Surface* surface = SDL_DisplayFormat(temp); 
+                               SDL_Surface* surface = SDL_DisplayFormatAlpha(temp);
+                               SDL_FreeSurface(temp);
                                mFrames[count].width=surface->w;
                                mFrames[count].height=surface->h;
-                           SDL_FreeSurface(temp);
 
                                // Check that the image\92s width is a power of 2
                                if ( (surface->w & (surface->w - 1)) != 0 ) {
@@ -139,21 +139,11 @@ int Animation::loadAnimation(std::string animFile)
                                GLenum texture_format=NULL;
 
                                //contains an alpha channel
-                               if(nofcolors==4)
-                               {
-                                       if(surface->format->Rmask==0x000000ff)
-                                               texture_format=GL_RGBA;
-                                       else
-                                               texture_format=GL_BGRA;
-                               }
-                               else if(nofcolors==3) //no alpha channel
-                               {
-                                       if(surface->format->Rmask==0x000000ff)
-                                               texture_format=GL_RGB;
-                                       else
-                                               texture_format=GL_BGR;
-                               }
+                               if(surface->format->Rmask==0x000000ff)
+                                       texture_format=GL_RGBA;
                                else
+                                       texture_format=GL_BGRA;
+                               if(!(nofcolors==4 || nofcolors==3))
                                {
                                        cout<<"warning: the image is not truecolor...this will break "<<endl;
                                }
@@ -168,8 +158,7 @@ int Animation::loadAnimation(std::string animFile)
                                glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
 
                                // when texture area is small, bilinear filter the closest mipmap
-                               glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
-                                       GL_LINEAR_MIPMAP_NEAREST );
+                               glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
                                // when texture area is large, bilinear filter the original
                                glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
 
index 7bb00a3..854190a 100644 (file)
@@ -19,7 +19,7 @@ void LevelWorldLoad(){
 
        /** load sprites */
        Sprite* vikings1 = new Sprite(screen, "viking1", l->getAnimation("viking.anim"));
-       vikings1->setPosition(10,30);
+       vikings1->setPosition(0,0);
        vikings1->setSpeed(1);
 
        Sprite* vikings2 = new Sprite(screen, "viking2", l->getAnimation("viking.anim"));
index bd501cb..e5303b5 100644 (file)
@@ -41,27 +41,34 @@ void Sprite::draw()
                        mLastUpdate = SDL_GetTicks();
                }
        }
-       SDL_Rect dest;
        if(mVisible == true){
+               glEnable(GL_BLEND);
+               glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+               glColor4f(1, 1, 1, mTransparency);
                glLoadIdentity();
+               glTranslated(mPos.x,mPos.y,0);//move to position
+               glTranslated(frame->animationPeg.x,frame->animationPeg.y,0);//move back to where it was
+               glRotatef(mAngle,0.0f,0.0f,1.0f);//rotate shape
+               glTranslated(-frame->animationPeg.x,-frame->animationPeg.y,0);//place animation peg on origin
                glBindTexture(GL_TEXTURE_2D, frame->image);
                glBegin( GL_QUADS );
                // Top-left vertex (corner)
-               glTexCoord2i( 0, 0 );
-               glVertex3f( mPos.x, mPos.y, 0 );
+               glTexCoord2f( 0, 0 );
+               glVertex3d( 0, 0, 0 );
 
                // Bottom-left vertex (corner)
-               glTexCoord2i( 1, 0 );
-               glVertex3f( mPos.x+frame->width, mPos.y, 0 );
+               glTexCoord2f( 1, 0 );
+               glVertex3d( frame->width, 0, 0 );
 
                // Bottom-right vertex (corner)
-               glTexCoord2i( 1, 1 );
-               glVertex3f( mPos.x+frame->width, mPos.y+frame->height, 0 );
+               glTexCoord2f( 1, 1 );
+               glVertex3d( frame->width, frame->height, 0 );
 
                // Top-right vertex (corner)
-               glTexCoord2i( 0, 1 );
-               glVertex3f( mPos.x, mPos.y+frame->height, 0 );
+               glTexCoord2f( 0, 1 );
+               glVertex3d( 0, frame->height, 0 );
                glEnd();
+               glDisable(GL_BLEND);
                glLoadIdentity();
        }
 
index 128d572..09794d0 100644 (file)
@@ -7,8 +7,10 @@ class WorldObject
 {
 public:
        WorldObject(int z = 0) :
-         ZOrder(z),
-                 mVisible(true)
+               ZOrder(z),
+               mVisible(true),
+               mTransparency(1),
+               mAngle(0)
          {}
        virtual void draw() = 0;/**< Draws the Object. */
        void drawCollisions(vector<Collision*>& vec, const Point2D& pos);/**< Draws Collision data for the Object */
@@ -17,10 +19,16 @@ public:
        void xset(int x) {mPos.x = x;}/**< Sets the Sprite's x Coordinate. */
        void yset(int y) {mPos.y = y;}/**< Sets the Sprite's y coordinate.  */
        void setPosition(int x, int y) {mPos.x = x; mPos.y = y;}/**< Sets the Sprite's x an y coordinate. */
+       void setTransparency(float f){ mTransparency = f>1?1:f<0?0:f;}/**< Sets the Sprite's transparency. \param f The sprite's transparancy [0,1] other values will be force set */
+       float getTransparency(){return mTransparency;}/**< Gets the Sprite's transparency. */
+       void setAngle(float a){ mAngle = a; while(a>360)a-=360;}/**< Sets the Sprite's angle in degrees. \param a Angle in degrees */
+       float getAngle(){return mAngle;}/**< Gets the Sprite's angle. */
 
 protected:
        Point2D mPos;/**< The current (x,y) position */
        bool mVisible;/**< Determine if Object should be visible */
+       float mTransparency;/**< Transparency! */
+       float mAngle;/**< Angle of rotation */
        int ZOrder;/**< Stacking order. Determines what draws on top. \todo implement. */
 };
 #endif
diff --git a/fns.h b/fns.h
index d8cfbb3..2c2e65c 100644 (file)
--- a/fns.h
+++ b/fns.h
@@ -12,6 +12,8 @@ typedef void (*Behavior) ();
 static void DoNothing(){}
 ///Texture name
 typedef unsigned int Texture;
+///deg/rad
+const float deg2rad = (float)(3.14159/180);
 
 SDL_Surface* LoadImage( std::string filename );/**< Loads supported images. */