Rearranged data and fixed classes to work with Collision. Draw rectangle and circle...
authorunknown <Ivan Lloyd@.(none)>
Thu, 27 Jan 2011 15:44:10 +0000 (09:44 -0600)
committerunknown <Ivan Lloyd@.(none)>
Thu, 27 Jan 2011 15:44:10 +0000 (09:44 -0600)
ArtAssets/Sprites/viking.anim
Background.cpp
Background.h
Collision.cpp
Level.cpp
Level.h
Sprite.cpp
Sprite.h

index 56ad9a8..22b240c 100644 (file)
@@ -2,12 +2,12 @@
 #ArtAssets/ milisec    Transparency
 #filename      pause   r       g       b       xOffset yOffset ( <n | c | r> <Circle has offset, radius | rectangle has offset, width, height>, ... )
 NumFrames: 9
-viking1.bmp 50 0 255 0 0.0 0.0 ( r -75 -50 150.0 100.0 )
-viking2.bmp 50 0 255 0 0.0 0.0 ( r -75 -50 150.0 100.0 )
-viking3.bmp 50 0 255 0 0.0 0.0 ( r -75 -50 150.0 100.0 )
-viking4.bmp 50 0 255 0 0.0 0.0 ( r -75 -50 150.0 100.0  c 0.0.0 5.0 )
-viking5.bmp 50 0 255 0 0.0 0.0 ( r -75 -50 150.0 100.0 )
-viking6.bmp 50 0 255 0 0.0 0.0 ( r -75 -50 150.0 100.0 )
-viking7.bmp 50 0 255 0 0.0 0.0 ( r -75 -50 150.0 100.0 )
-viking8.bmp 50 0 255 0 0.0 0.0 ( r -75 -50 150.0 100.0 )
-viking9.bmp 50 0 255 0 0.0 0.0 ( r -75 -50 150.0 100.0 )
+viking1.bmp 50 0 255 0 0.0 0.0 ( r 0 150.0 100.0 )
+viking2.bmp 50 0 255 0 0.0 0.0 ( r 0 150.0 100.0 )
+viking3.bmp 50 0 255 0 0.0 0.0 ( r 0 150.0 100.0 )
+viking4.bmp 50 0 255 0 0.0 0.0 ( r 0 0 150.0 100.0  c 0.0 0.0 5.0 )
+viking5.bmp 50 0 255 0 0.0 0.0 ( r 0 150.0 100.0 )
+viking6.bmp 50 0 255 0 0.0 0.0 ( r 0 150.0 100.0 )
+viking7.bmp 50 0 255 0 0.0 0.0 ( r 0 150.0 100.0 )
+viking8.bmp 50 0 255 0 0.0 0.0 ( r 0 150.0 100.0 )
+viking9.bmp 50 0 255 0 0.0 0.0 ( r 0 150.0 100.0 )
index 3dd167c..d5a0284 100644 (file)
@@ -143,8 +143,6 @@ void Background::draw(){
                dest.y = mPos.y;
                SDL_BlitSurface(mBackground, &source, mScreen, &dest);
        }
-       if(Game::game()->ShowCollisions)
-               drawCollisions(collisionData, mPos);
 }
 
 SDL_Surface* Background::load(std::string filename){
@@ -164,4 +162,7 @@ bool Background::getWrap(){
        return wrapping;
 }
 
+void Background::drawCollisions(){
+       WorldObject::drawCollisions(collisionData, mPos);
+}
 
index 5c19249..324e87c 100644 (file)
@@ -48,5 +48,7 @@ class Background : public WorldObject{
        SizeD getPosition(){return mPos;}
        void setWrap(bool tf);
        bool getWrap();
+       void drawCollisions();/**< Draws Collision data for the Object (from outside) */
+
 };
 #endif
index 53776df..5f8b60e 100644 (file)
@@ -21,48 +21,53 @@ void Sulock(SDL_Surface *screen)
     }
 }
 
-void DrawPixel(SDL_Surface *screen, int x, int y, Uint32 color)
+void DrawPixel(SDL_Surface *screen, int x, int y, Uint32 c)
 {
-    switch (screen->format->BytesPerPixel)
-    {
-    case 1: // Assuming 8-bpp
-        {
-            Uint8 *bufp;
-            bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
-            *bufp = color;
-        }
-        break;
-    case 2: // Probably 15-bpp or 16-bpp
-        {
-            Uint16 *bufp;
-            bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
-            *bufp = color;
-        }
-        break;
-    case 3: // Slow 24-bpp mode, usually not used
-        {
-            Uint8 *bufp;
-            bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
-            if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
-            {
-                               bufp[0] = color;
-                bufp[1] = color >> 8;
-                bufp[2] = color >> 16;
-            } else {
-                bufp[2] = color;
-                bufp[1] = color >> 8;
-                bufp[0] = color >> 16;
-            }
-        }
-        break;
-    case 4: // Probably 32-bpp
-        {
-            Uint32 *bufp;
-            bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
-            *bufp = color;
-        }
-        break;
-    }
+       //keep in bounds
+       if(x>0 && y>0 && x < screen->w && y < screen->h){
+               Uint32 color = c&0xFFFFFF;
+               //Uint32 color1 = SDL_MapRGB(screen->format, c&0xff, c&0xFF00, c&0xFF0000);
+               switch (screen->format->BytesPerPixel)
+               {
+               case 1: // Assuming 8-bpp
+                       {
+                               Uint8 *bufp;
+                               bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
+                               *bufp = color;
+                       }
+                       break;
+               case 2: // Probably 15-bpp or 16-bpp
+                       {
+                               Uint16 *bufp;
+                               bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
+                               *bufp = color;
+                       }
+                       break;
+               case 3: // Slow 24-bpp mode, usually not used
+                       {
+                               Uint8 *bufp;
+                               bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
+                               if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
+                               {
+                                       bufp[0] = color;
+                                       bufp[1] = color >> 8;
+                                       bufp[2] = color >> 16;
+                               } else {
+                                       bufp[2] = color;
+                                       bufp[1] = color >> 8;
+                                       bufp[0] = color >> 16;
+                               }
+                       }
+                       break;
+               case 4: // Probably 32-bpp
+                       {
+                               Uint32 *bufp;
+                               bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
+                               *bufp = color;
+                               break;
+                       }
+               }
+       }
 }
 
 /**
@@ -71,15 +76,15 @@ void DrawPixel(SDL_Surface *screen, int x, int y, Uint32 color)
        \param pos This is the position to draw the points (not & because may want to have an added value pushed in)
        \param color the color to be drawn
 */
-void DrawCircle(SDL_Surface* screen, const Point2D pos, Uint32 color){
-       DrawPixel(screen,  pos.x,  pos.y, color);
-       DrawPixel(screen, -pos.x,  pos.y, color);
-       DrawPixel(screen,  pos.x, -pos.y, color);
-       DrawPixel(screen, -pos.x, -pos.y, color);
-       DrawPixel(screen,  pos.y,  pos.x, color);
-       DrawPixel(screen, -pos.y,  pos.x, color);
-       DrawPixel(screen,  pos.y, -pos.x, color);
-       DrawPixel(screen, -pos.y, -pos.x, color);
+void DrawCircle(SDL_Surface* screen, const Point2D wPos, const Point2D pos, Uint32 color){
+       DrawPixel(screen, wPos.x + pos.x, wPos.y + pos.y, color);
+       DrawPixel(screen, wPos.x - pos.x, wPos.y + pos.y, color);
+       DrawPixel(screen, wPos.x + pos.x, wPos.y - pos.y, color);
+       DrawPixel(screen, wPos.x - pos.x, wPos.y - pos.y, color);
+       DrawPixel(screen, wPos.x + pos.y, wPos.y + pos.x, color);
+       DrawPixel(screen, wPos.x - pos.y, wPos.y + pos.x, color);
+       DrawPixel(screen, wPos.x + pos.y, wPos.y - pos.x, color);
+       DrawPixel(screen, wPos.x - pos.y, wPos.y - pos.x, color);
 }
 /**
        This function is tasked with drawing lines by the midpoint method. 
@@ -93,6 +98,7 @@ void DrawLine(SDL_Surface* screen, const Point2D start, Point2D end, Uint32 colo
        //values for calculation and max values
        int x,y;//start as low vals used for algorithm
        int xMax, yMax;
+       
        //if the end point is lower swap em
        if(end.y < start.y){
                x = end.x;
@@ -101,45 +107,67 @@ void DrawLine(SDL_Surface* screen, const Point2D start, Point2D end, Uint32 colo
                yMax = start.y;
        }
        else {
-               x = end.x;
-               y = end.y;
-               xMax = start.x;
-               yMax = start.y;
+               x = start.x;
+               y = start.y;
+               xMax = end.x;
+               yMax = end.y;
        }
+
        //the change in x
        int dX = xMax - x;
-       //change in y
-       int dY = y - yMax;
-       int sum = 2 * dY + dX;
-       DrawPixel(screen, x, y, color);
-       while( x < xMax){
-               if(sum < 0){
-                       sum += 2 * dX;
+
+       //if divide by 0 draw a vertical line
+       if(dX) {
+               //change in y
+               int dY = y - yMax;
+               int sum = 2 * dY + dX;
+               DrawPixel(screen, x, y, color);
+
+               while( x < xMax){
+                       if(sum < 0){
+                               sum += 2 * dX;
+                               y++;
+                       }
+                       x++;
+                       sum += 2 * dY;
+                       DrawPixel(screen, x, y, color);
+               }
+       }
+       else {
+               while(y < yMax){
+                       DrawPixel(screen, x, y, color);
                        y++;
                }
-               x++;
-               sum += 2 * dY;
-               DrawPixel(screen, x, y, color);
        }
        Sulock(screen);
 }
 
 void CollisionRectangle::draw(const Point2D& pos){
+       Point2D nPos = pos + mPos;
        //top
-       Point2D startPos = pos + mPos ;
-       Point2D endPos = pos + mPos ;
+       Point2D startPos = nPos + Point2D(-width/2, -height/2);
+       Point2D endPos = nPos + Point2D(width/2, -height/2);
        DrawLine(Game::game()->Screen(), startPos, endPos, color);
        //bottom
+       startPos = nPos + Point2D(-width/2, height/2);
+       endPos = nPos + Point2D(width/2, height/2);
+       DrawLine(Game::game()->Screen(), startPos, endPos, color);
        //left
+       startPos = nPos + Point2D(-width/2, -height/2);
+       endPos = nPos + Point2D(-width/2, height/2);
+       DrawLine(Game::game()->Screen(), startPos, endPos, color);
        //right
+       startPos = nPos + Point2D(width/2, -height/2);
+       endPos = nPos + Point2D(width/2, height/2);
+       DrawLine(Game::game()->Screen(), startPos, endPos, color);
 }
 
 void CollisionCircle::draw(const Point2D& pos){
+       Slock(Game::game()->Screen());
        int x=0,y=radius;///<The relative x,y pos
        int midpt = 1-radius;///<the midpt of the circle
-       Slock(Game::game()->Screen());
        //draw pts
-       DrawCircle(Game::game()->Screen(), pos + mPos, color);
+       DrawCircle(Game::game()->Screen(), pos + mPos, Point2D(x,y), color);
        //calculate other points
        while(x<y){
                x++;
@@ -150,7 +178,7 @@ void CollisionCircle::draw(const Point2D& pos){
                        midpt += 2 * (x-y) + 1;
                }
                //draw pts
-               DrawCircle(Game::game()->Screen(), pos + mPos, color);
+               DrawCircle(Game::game()->Screen(), pos + mPos, Point2D(x,y), color);
        }
        Sulock(Game::game()->Screen());
 }
index a884c46..c8d6d1c 100644 (file)
--- a/Level.cpp
+++ b/Level.cpp
@@ -1,4 +1,5 @@
 #include "Level.h"
+#include "Game.h"
 #include "fns.h"
 
 Level::Level(SDL_Surface* screen) : mScreen(screen)
@@ -65,7 +66,10 @@ void Level::drawScene()
        /** Draw Sprites*/
        DrawSprites();
     
-    /** Flip the working image buffer with the mScreen buffer */
+       /** Draw Boundary Data */
+       DrawCollisions();
+    
+       /** Flip the working image buffer with the mScreen buffer */
     SDL_Flip (mScreen);
 }
 
@@ -76,6 +80,16 @@ void Level::DrawSprites()
        }
 }
 
+void Level::DrawCollisions()
+{
+       if(Game::game()->ShowCollisions){
+               mBackground->drawCollisions();
+               for (size_t i=0; i<mSprites.size(); ++i) {
+                       mSprites[i]->drawCollisions();
+               }
+       }
+}
+
 void Level::LoadBG(std::string name)
 {
        mBackground->load(name);
diff --git a/Level.h b/Level.h
index e399898..48d0e01 100644 (file)
--- a/Level.h
+++ b/Level.h
@@ -52,6 +52,7 @@ protected://allow inheritance
        void DrawIMG(SDL_Surface *img, int x, int y);/**< Draws the specified image to the screen at location (x,y) */
        void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2);/**< Draws an image at the specified location (x,y) that is blitted with width w and height h from the point (x2,y2) of the given image */
        void DrawSprites();/**< Draws all Sprites. */
+       void DrawCollisions();/**< Draws all Colision data */
        
 };
 #endif
index b175306..dabf21c 100644 (file)
@@ -4,6 +4,7 @@
 using std::cout;
 using std::endl;
 Sprite::Sprite(SDL_Surface *screen, std::string name, Actor actor) :
+       mName(name),
        mDrawn(0),
        mLastUpdate(0),
        mActor(actor),
@@ -43,8 +44,11 @@ void Sprite::draw()
        dest.y = mPos.y;
        if(mVisible == true)
                SDL_BlitSurface(frame.image,NULL,mScreen,&dest);
-       if(Game::game()->ShowCollisions){
-               Point2D pt =mPos + frame.animationPeg;
-               drawCollisions(frame.collisionData, pt);
-       }
+
 }
+
+void Sprite::drawCollisions(){
+               SpriteFrame frame = mActor.mAnimations[mActor.mCurrentAnimation]->mAnim[mActor.mFrame];
+               Point2D pt = mPos + frame.animationPeg;
+               WorldObject::drawCollisions(frame.collisionData, pt);
+}
\ No newline at end of file
index e6e7648..b21c681 100644 (file)
--- a/Sprite.h
+++ b/Sprite.h
@@ -29,6 +29,7 @@ class Sprite : public WorldObject
        void stopAnim() {mAnimating = 0;}/**< Causes the animation to stop. */
        void rewind() {mActor.mFrame = 0;}/**< Resets the Sprite's animation to the first frame. */
        void draw();/**< draws Sprite. */
+       void drawCollisions();/**< Draws Collision data for the Object (from outside) */
        
 
        private: