Added Point2D +, -. Changed Sprite Draw to shift drawing pos by the AnimationPeg
authorunknown <ivan@.ne.tamu.edu>
Thu, 13 Jan 2011 22:22:04 +0000 (16:22 -0600)
committerunknown <ivan@.ne.tamu.edu>
Thu, 13 Jan 2011 22:22:04 +0000 (16:22 -0600)
making collision work (broken atm)
separated frame from collision changed animations for Sprite/sun

Animation.cpp
Animation.h
ArtAssets/Sprites/sun.anim
ArtAssets/Sprites/viking.anim
Collision.cpp [new file with mode: 0644]
Collision.h
Frame.h [new file with mode: 0644]
Sprite.cpp
fns.h

index b21e01e..6f78126 100644 (file)
@@ -20,7 +20,7 @@ int Animation::loadAnimation(std::string animFile)
 {
        //variables
        string buffer,file;
-       vector<int> transparency;
+       int transparency[3];
        int pause;
        animFile="ArtAssets/Sprites/"+animFile;
        //file
@@ -48,27 +48,75 @@ int Animation::loadAnimation(std::string animFile)
                        }
                        else
                        {
+                               /** File name */
                                file=temp;
+                               /** Pause time */
                                value>>pause;
+                               /** transparency #'s R G B */
                                for(int i=0; i<3;++i)
                                {
                                        int num;
                                        value>>num;
                                        if(num >= 0 && num<=255)
-                                               transparency.push_back(num);
+                                               transparency[i] = num;///< \todo check to see if this is being stupid.
+                                       else
+                                       {
+                                               transparency[i] = 0;
+                                               cout<<"Invalid value!"<<endl;
+                                       }
                                }
+                               /** AnimationPeg */
+                               double x=0.0, y=0.0;
+                               value>>x;
+                               value>>y;
+                               
+                               /** Collision data */
+                               char c;
+                               value>>c;
+                               while(c != ')'){
+                                       if(c == 'n')
+                                               continue;
+                                       else if(c == 'c')
+                                       {
+                                               double xOffset = 0.0, yOffset = 0.0;
+                                               value>>xOffset;
+                                               value>>yOffset;
+                                               double radius;
+                                               value>>radius;
+                                               mAnim[count].bounds.push_back(CollisionCircle(Point2D(xOffset, yOffset), radius));
+                                       }
+                                       else if(c == 'r')
+                                       {
+                                               double xOffset = 0.0, yOffset = 0.0;
+                                               value>>xOffset;
+                                               value>>yOffset;
+                                               double width = 0.0 , height = 0.0;
+                                               value>>width;
+                                               value>>height;
+                                               mAnim[count].bounds.push_back(CollisionRectangle(Point2D(xOffset, yOffset), width, height));
+                                       }
+                                       value>>c;
+                               }
+
+                               /** Load image for Frame */
                                SDL_Surface *temp=NULL;
-                               if((temp = LoadImage("Sprites/"+file)) == NULL) return -1;
+                               if((temp = LoadImage("Sprites/"+file)) == NULL) return -1;//Load image for Frame
                        
                                /** Set transparent color */
                                SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, transparency[0], transparency[1], transparency[2]));
+                               
                                /** Loads image into animation */
                                mAnim[count].image = SDL_DisplayFormat(temp);
                            SDL_FreeSurface(temp);
+                               
                                /** sets frame delay and makes sure height and width are correct */
                                mAnim[count].pause = pause;
                        if(!mW) mW = mAnim[count].image->w; 
                        if(!mH) mH = mAnim[count].image->h;
+                               
+                               /** Set the animation Peg*/
+                               mAnim[count].animationPeg = Point2D(x,y);
+
                                count++;
                        }
        }
index a4b3800..3d4de32 100644 (file)
@@ -1,23 +1,12 @@
-#ifndef SPRITEBASE
-#define SPRITEBASE
-#include "Collision.h"
+#ifndef ANIMATION
+#define ANIMATION
+#include "Frame.h"
 #include <string>
 #include <SDL/SDL.h>
 #include <SDL/SDL_image.h>
-#include <vector>
 
 using std::vector;
 
-struct SpriteFrame{
-       SDL_Surface *image;/**< Pointer to an image. */
-       int pause;/**< Tells the amount of time to pause between this frame and the next. */
-       int width;/**< Base width of the frame's image. */
-       int height;/**< Base height of the frame's image. */
-       vector<Point2D> hotSpots;/**< Hot spots that can be used for locating objects on the sprite default is tagged to center of the sprite \todo implement default*/
-       Point2D animationPeg;/**< The offeset from position to place the image. Defaults to (0,0) \todo implement */
-       vector<Collision> bounds;/**< The collision data for this sprite */
-};
-
 /**
   An Animation
 
@@ -31,7 +20,7 @@ class Animation
        public:
        Animation(std::string animFile);
        int loadAnimation(std::string animFile);/**< Loads the Animations from a file in the specified format. */
-       SpriteFrame *mAnim;/**< Pointer to the current animation. */
+       SpriteFrame *mAnim;/**< Pointer to the current animation. As an array of SpriteFrames */
        int mBuilt,/**< Using as a bool */
         mNumFrames,/**< Number of frames in this Animation */
         mW,/**< Animation's current width */
index 81cfb53..c0932cd 100644 (file)
@@ -1,8 +1,8 @@
 #Number of Frames 
 #ArtAssets/ milisec    Transparency
-#filename      pause   r       g       b 
+#filename      pause   r       g       b       xOffset yOffset ( collisionType <Circle has offset, radius | rectangle has offset, width, height>
 NumFrames: 4
-sun1.bmp 20 0 0 255
-sun2.bmp 20 0 0 255
-sun3.bmp 20 0 0 255
-sun4.bmp 20 0 0 255
+sun1.bmp       20      0 0 255 0.0 0.0 ( n )
+sun2.bmp       20      0 0 255 0.0 0.0 ( n )
+sun3.bmp       20      0 0 255 0.0 0.0 ( n )
+sun4.bmp       20      0 0 255 0.0 0.0 ( n )
index f77cabf..4d21111 100644 (file)
@@ -1,13 +1,13 @@
 #Number of Frames 
 #ArtAssets/ milisec    Transparency
-#filename      pause   r       g       b 
+#filename      pause   r       g       b       xOffset yOffset 
 NumFrames: 9
-viking1.bmp 50 0 255 0
-viking2.bmp 50 0 255 0 
-viking3.bmp 50 0 255 0
-viking4.bmp 50 0 255 0
-viking5.bmp 50 0 255 0
-viking6.bmp 50 0 255 0
-viking7.bmp 50 0 255 0
-viking8.bmp 50 0 255 0
-viking9.bmp 50 0 255 0
+viking1.bmp 50 0 255 0 0.0 0.0 ( n )
+viking2.bmp 50 0 255 0 0.0 0.0 ( n )
+viking3.bmp 50 0 255 0 0.0 0.0 ( n )
+viking4.bmp 50 0 255 0 0.0 0.0 ( n )
+viking5.bmp 50 0 255 0 0.0 0.0 ( n )
+viking6.bmp 50 0 255 0 0.0 0.0 ( n )
+viking7.bmp 50 0 255 0 0.0 0.0 ( n )
+viking8.bmp 50 0 255 0 0.0 0.0 ( n )
+viking9.bmp 50 0 255 0 0.0 0.0 ( n )
diff --git a/Collision.cpp b/Collision.cpp
new file mode 100644 (file)
index 0000000..380e45c
--- /dev/null
@@ -0,0 +1,11 @@
+#include "Collision.h"
+
+bool CollisionRectangle::collision(CollisionRectangle &c){
+}
+bool CollisionRectangle::collision(CollisionCircle &c){
+}
+bool CollisionCircle::collision(CollisionRectangle &c){
+}
+bool CollisionCircle::collision(CollisionCircle &c){
+}
+
index 1228f15..1a652b9 100644 (file)
@@ -17,10 +17,10 @@ using std::string;
 class Collision
 {
 public:
-       Collision();
+       Collision():pos(0.0,0.0){}
        Point2D pos;/**< The position of the center of the collision data */
-       virtual bool rectColl(Collision &c);/**< Check collision with Rectangle */
-       virtual bool circColl(Collision &c);/**< Check collision with Circle */
+       virtual bool collision(CollisionRectangle &c);/**< Check collision with Rectangle */
+       virtual bool collision(CollisionCircle &c);/**< Check collision with Circle */
        /// \todo See this http://www.metanetsoftware.com/technique/tutorialA.html
 protected:
        string name;///< Name of this behavior \todo make this actually be useful
@@ -38,9 +38,13 @@ protected:
 class CollisionRectangle : public Collision
 {
 public:
-       CollisionRectangle();
-       int width;
-       int height;
+       CollisionRectangle():width(1.0),height(1.0){};
+       CollisionRectangle(Point2D pt, double w, double h):width(w),height(h){ pos = pt;}
+       double width;/**< Rectangle's width */
+       double height;/**< Rectangle's height */
+
+       bool collision(CollisionRectangle &c);/**< Check collision with Rectangle */
+       bool collision(CollisionCircle &c);/**< Check collision with Circle */
 };
 
 /**
@@ -56,6 +60,10 @@ class CollisionCircle : public Collision
 {
 public:
        CollisionCircle();
-       int radius;/**< The raidus of the circle */
+       CollisionCircle(Point2D pt, double r):radius(r){ pos = pt; }
+       double radius;/**< The raidus of the circle */
+
+       bool collision(CollisionRectangle &c);/**< Check collision with Rectangle */
+       bool collision(CollisionCircle &c);/**< Check collision with Circle */
 };
 #endif
diff --git a/Frame.h b/Frame.h
new file mode 100644 (file)
index 0000000..b6c9c95
--- /dev/null
+++ b/Frame.h
@@ -0,0 +1,19 @@
+#ifndef FRAME
+#define FRAME
+#include "Collision.h"
+#include <string>
+#include <SDL/SDL.h>
+#include <SDL/SDL_image.h>
+#include <vector>
+
+struct SpriteFrame{
+       SDL_Surface *image;/**< Pointer to an image. */
+       int pause;/**< Tells the amount of time to pause between this frame and the next. */
+       int width;/**< Base width of the frame's image. */
+       int height;/**< Base height of the frame's image. */
+       vector<Point2D> hotSpots;/**< Hot spots that can be used for locating objects on the sprite default is tagged to center of the sprite \todo implement default*/
+       Point2D animationPeg;/**< The offeset from position to place the image. Defaults to (0,0) \todo implement */
+       vector<Collision> bounds;/**< The collision data for this sprite */
+};
+
+#endif
\ No newline at end of file
index e2c3bff..9bbff36 100644 (file)
@@ -23,8 +23,15 @@ void Sprite::draw()
 {
        if(mAnimating == 1) {
                if(mLastUpdate+mActor.mAnimations[mActor.mCurrentAnimation]->mAnim[mActor.mFrame].pause*mSpeed<SDL_GetTicks()) {
+                       //obtain current peg 
+                       Point2D ppos = mActor.mAnimations[mActor.mCurrentAnimation]->mAnim[mActor.mFrame].animationPeg;
                        mActor.mFrame++;
-                       if(mActor.mFrame > mActor.mAnimations[mActor.mCurrentAnimation]->mNumFrames-1) mActor.mFrame=0;
+                       if(mActor.mFrame > mActor.mAnimations[mActor.mCurrentAnimation]->mNumFrames-1)
+                               mActor.mFrame=0;
+                       //obtain next peg
+                       Point2D npos = mActor.mAnimations[mActor.mCurrentAnimation]->mAnim[mActor.mFrame].animationPeg;
+                       //move current position to difference of two
+                       mPos.add(ppos - npos);
                        mLastUpdate = SDL_GetTicks();
                }
        }
diff --git a/fns.h b/fns.h
index 33b8056..378f3e8 100644 (file)
--- a/fns.h
+++ b/fns.h
@@ -36,7 +36,10 @@ class Point2D
        Point2D sub(Point2D pt);/**< Subtracts the value of the point from this \return This after modification*/
        Point2D mult(double d);/**< Multiplies the values of this by i \return This after modificaiton*/
        Point2D div(double d);/**< Divides the values of this by i \return This after modificaiton*/
-
+       Point2D operator+ (Point2D& pt){ return Point2D(x + pt.x, y + pt.y); }
+       Point2D operator- (Point2D& pt){ return Point2D(x - pt.x, y - pt.y); }
+       Point2D operator+= (Point2D& pt){  return add(pt); }
+       Point2D operator-= (Point2D& pt){ sub(pt); }
 };
 
 /** Just for more logical names for sizes of objects*/