From: Ivan Hernanez Date: Tue, 9 Aug 2011 07:35:11 +0000 (-0500) Subject: Renders sprites using OpenGL X-Git-Url: http://git.mmlx.us/?a=commitdiff_plain;h=ca7f2ac45e5a5b7261265358555da523b4f95f9e;p=IvanGame.git Renders sprites using OpenGL \todo render bg (load from animation file) \draw collisions --- diff --git a/Animation.cpp b/Animation.cpp index df05b43..039a4de 100644 --- a/Animation.cpp +++ b/Animation.cpp @@ -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’s 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 "<getAnimation("viking.anim")); - vikings1->setPosition(10,30); + vikings1->setPosition(0,0); vikings1->setSpeed(1); Sprite* vikings2 = new Sprite(screen, "viking2", l->getAnimation("viking.anim")); diff --git a/Sprite.cpp b/Sprite.cpp index bd501cb..e5303b5 100644 --- a/Sprite.cpp +++ b/Sprite.cpp @@ -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(); } diff --git a/WorldObject.h b/WorldObject.h index 128d572..09794d0 100644 --- a/WorldObject.h +++ b/WorldObject.h @@ -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& 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 --- 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. */