Implemented WorldObject!
authorunknown <Ivan Lloyd@.(none)>
Tue, 18 Jan 2011 05:05:35 +0000 (23:05 -0600)
committerunknown <Ivan Lloyd@.(none)>
Tue, 18 Jan 2011 05:05:35 +0000 (23:05 -0600)
Background.cpp
Background.h
Sprite.cpp
Sprite.h
WorldObject.h

index 5e395b0..f431f1d 100644 (file)
@@ -2,7 +2,13 @@
 #include <iostream>
 using namespace std;
 
-Background::Background(SDL_Surface *screen, std::string name, std::string filename, bool wrap, Point2D ScreenPosition, SizeD ScreenSize, Point2D bgSamplePos, SizeD bgSize):mScreen(screen),_name(name),wrapping(wrap),mPos(ScreenPosition),mScreenSize(ScreenSize){
+Background::Background(SDL_Surface *screen, std::string name, std::string filename, bool wrap, Point2D ScreenPosition, SizeD ScreenSize, Point2D bgSamplePos, SizeD bgSize) :
+mScreen(screen),
+       _name(name),
+       wrapping(wrap),
+       mScreenSize(ScreenSize)
+{
+       mPos = ScreenPosition;
        mScreenSize.w = screen->w;
        mScreenSize.h = screen->h;
 
@@ -12,7 +18,7 @@ Background::Background(SDL_Surface *screen, std::string name, std::string filena
        source.y = bgSamplePos.y;
 
        load(filename);
-       
+
        cout<<"background \""<<_name<<"\" created"<<endl;       
 }
 void Background::draw(){
@@ -27,92 +33,92 @@ void Background::draw(){
        const double H = _imageSize.h;
        /*
        while (x > X) // move it to the left til it's on the screen
-               x -= X;
+       x -= X;
        while (y > Y) // move it up til it's on the screen
-               y -= Y;
+       y -= Y;
        while (x < 0) // move it to the right until it's in the drawable area
-               x += X;
+       x += X;
        while (y < 0) // move it down til it's on the screen
-               y += Y;
+       y += Y;
        //*/
        /*if (wrapping) {
-               SDL_Rect source;
-               if (x>=0 && y>=0){
-                       // Top-left portion of image
-                       dest.x = x;
-                       dest.y = y;
-                       source.x = 0;
-                       source.y = 0;
-                       source.w = X - x;
-                       source.h = Y - y;
-                       SDL_BlitSurface(mBackground, &source, mScreen, &dest);
-                       
-                       // Lower-left portion of image
-                       dest.x = x;
-                       dest.y = 0;
-                       source.x = 0;
-                       source.y = H - y;
-                       source.w = X - x;
-                       source.h = y;
-                       SDL_BlitSurface(mBackground, &source, mScreen, &dest);
+       SDL_Rect source;
+       if (x>=0 && y>=0){
+       // Top-left portion of image
+       dest.x = x;
+       dest.y = y;
+       source.x = 0;
+       source.y = 0;
+       source.w = X - x;
+       source.h = Y - y;
+       SDL_BlitSurface(mBackground, &source, mScreen, &dest);
+
+       // Lower-left portion of image
+       dest.x = x;
+       dest.y = 0;
+       source.x = 0;
+       source.y = H - y;
+       source.w = X - x;
+       source.h = y;
+       SDL_BlitSurface(mBackground, &source, mScreen, &dest);
 
-                       // Lower-right portion of image
-                       dest.x = 0;
-                       dest.y = 0;
-                       source.x = W - x;///< \todo this needs to look right. it's grabbing the wrong part of the image but when it's getting the right part it freaks out and flashes when the bg moves off to the negative. need a check to make sure there is not more background to go before it just draws a section.
-                       source.y = H - y;
-                       source.w = x;
-                       source.h = y;
-                       SDL_BlitSurface(mBackground, &source, mScreen, &dest);
+       // Lower-right portion of image
+       dest.x = 0;
+       dest.y = 0;
+       source.x = W - x;///< \todo this needs to look right. it's grabbing the wrong part of the image but when it's getting the right part it freaks out and flashes when the bg moves off to the negative. need a check to make sure there is not more background to go before it just draws a section.
+       source.y = H - y;
+       source.w = x;
+       source.h = y;
+       SDL_BlitSurface(mBackground, &source, mScreen, &dest);
 
-                       // Top-right portion of image
-                       dest.x = 0;
-                       dest.y = y;
-                       source.x = W - x;
-                       source.y = 0;
-                       source.w = x;
-                       source.h = Y - y;
-                       SDL_BlitSurface(mBackground, &source, mScreen, &dest);
-               }
-               if (x<0 || y<0){
-                       x = -x;
-                       y = -y;
-                       // Top-left portion of image
-                       dest.x = 0;
-                       dest.y = 0;
-                       source.x = x;
-                       source.y = y;
-                       source.w = X - x;
-                       source.h = Y - y;
-                       SDL_BlitSurface(mBackground, &source, mScreen, &dest);
-                       
-                       /// Lower-left portion of image \todo this be's broken
-                       dest.x = X - x;
-                       dest.y = 0;
-                       source.x = 0;
-                       source.y = H - y;
-                       source.w = x;
-                       source.h = Y - y;
-                       SDL_BlitSurface(mBackground, &source, mScreen, &dest);
+       // Top-right portion of image
+       dest.x = 0;
+       dest.y = y;
+       source.x = W - x;
+       source.y = 0;
+       source.w = x;
+       source.h = Y - y;
+       SDL_BlitSurface(mBackground, &source, mScreen, &dest);
+       }
+       if (x<0 || y<0){
+       x = -x;
+       y = -y;
+       // Top-left portion of image
+       dest.x = 0;
+       dest.y = 0;
+       source.x = x;
+       source.y = y;
+       source.w = X - x;
+       source.h = Y - y;
+       SDL_BlitSurface(mBackground, &source, mScreen, &dest);
 
-                       // Lower-right portion of image
-                       dest.x = X - x;
-                       dest.y = Y - y;
-                       source.x = 0;
-                       source.y = 0;
-                       source.w = x;
-                       source.h = y;
-                       SDL_BlitSurface(mBackground, &source, mScreen, &dest);
+       /// Lower-left portion of image \todo this be's broken
+       dest.x = X - x;
+       dest.y = 0;
+       source.x = 0;
+       source.y = H - y;
+       source.w = x;
+       source.h = Y - y;
+       SDL_BlitSurface(mBackground, &source, mScreen, &dest);
 
-                       // Top-right portion of image
-                       dest.x = 0;
-                       dest.y = Y - y;
-                       source.x = x;
-                       source.y = 0;
-                       source.w = X - x;
-                       source.h = y;
-                       SDL_BlitSurface(mBackground, &source, mScreen, &dest);
-               }
+       // Lower-right portion of image
+       dest.x = X - x;
+       dest.y = Y - y;
+       source.x = 0;
+       source.y = 0;
+       source.w = x;
+       source.h = y;
+       SDL_BlitSurface(mBackground, &source, mScreen, &dest);
+
+       // Top-right portion of image
+       dest.x = 0;
+       dest.y = Y - y;
+       source.x = x;
+       source.y = 0;
+       source.w = X - x;
+       source.h = y;
+       SDL_BlitSurface(mBackground, &source, mScreen, &dest);
+       }
        } 
        else */if(wrapping){
                while (x < 0) // move it to the left til it's on the screen
@@ -120,7 +126,7 @@ void Background::draw(){
                while (y < 0) // move it up til it's on the screen
                        y += H;
                while(x > 0)//move back til we are at the spot to the left of the current background
-                        x -= W;
+                       x -= W;
                int initx = x;
                while(y > 0)
                        y -= H;
index 671984e..7ae8521 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef BACKGROUND
 #define BACKGROUND
 #include "fns.h"
+#include "WorldObject.h"
 #include "Collision.h"
 #include <string>
 #include <vector>
@@ -16,14 +17,13 @@ using std::vector;
        - drawing the background
  */
 
-class Background{
+class Background : public WorldObject{
        private:
        string _name;///<The background's name
        SizeD _imageSize;///<The actual background image's full size.
 
        SDL_Surface *mBackground;///<pointer to the actual screen 
        SDL_Surface *mScreen;///< The screen to be drawn to.
-       Point2D mPos;///<The current (x,y) position
        SizeD mScreenSize;///<This is the size of the screen to be drawn to. used to get the sections of the background when moving and for screen-at-a-time scrolling.
        SDL_Rect source;///< This is the rectangle that gets taken from the actual background image (h,w) determine the size of the rectangle to sample from the background image. (x,y) determine the position of the rectangle's top left corner on the actual background image.
        bool wrapping;
@@ -48,10 +48,5 @@ class Background{
        SizeD getPosition(){return mPos;}
        void setWrap(bool tf);
        bool getWrap();
-       void xadd(int num) {mPos.x += num;}/**< Increase x coordiante by a given amount. */
-    void yadd(int num) {mPos.y += num;}/**< Increase y coordinate by a given amount. */
-    void xset(int x) {mPos.x = x;}/**< Sets the Background's x Coordinate. */
-    void yset(int y) {mPos.y = y;}/**< Sets the Background's y coordinate.  */
-    void setPosition(int x, int y) {mPos.x = x; mPos.y = y;}/**< Sets the Background's x an y coordinate. */
 };
 #endif
index 9bbff36..eafa76e 100644 (file)
@@ -2,13 +2,11 @@
 #include <iostream>
 using std::cout;
 using std::endl;
-Sprite::Sprite(SDL_Surface *screen, std::string name, Actor actor, int z) :
+Sprite::Sprite(SDL_Surface *screen, std::string name, Actor actor) :
        mDrawn(0),
-       mVisible(true),
        mLastUpdate(0),
        mActor(actor),
-       mScreen(screen),
-       ZOrder(z)
+       mScreen(screen)
 {
        /** \todo Later add a part that adds name to list of level sprites 
                Game.Level.SpriteList.pushback(name);
index 3fc47da..7eba848 100644 (file)
--- a/Sprite.h
+++ b/Sprite.h
@@ -5,6 +5,7 @@
 #include "fns.h"
 #include "Actor.h"
 #include "Behaviors.h"
+#include "WorldObject.h"
 
 /**
   Characters and other mobile objects displayed on screen with animations.
     - loading and controlling an Animation 
  */
 
-class Sprite
+class Sprite : public WorldObject
 {
        public:
-       Sprite(SDL_Surface *screen, std::string name, Actor actor, int z = 0);
+       Sprite(SDL_Surface *screen, std::string name, Actor actor);
        void setAnimation(int animation){ mActor.mCurrentAnimation = animation; }/**< changes to the specified animation beginning at 0. */
        int getAnimation(){ return mActor.mCurrentAnimation; }/**< returns active animation. */
        void setFrame(int frame) { mActor.mFrame = frame; }/**< cahnges to the specified frame of the animation beginning at 0. */
@@ -28,12 +29,7 @@ class Sprite
        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 xadd(int num) {mPos.x += num;}/**< Increase X coordiante by a given amount. */
-       void yadd(int num) {mPos.y += num;}/**< Increase y coordinate by a given amount. */
-       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. */
-
+       
 
        private:
        std::string mName;/**< Sprite's name */
@@ -43,9 +39,6 @@ class Sprite
        long mLastUpdate;/**< Number that indicates when the Sprite has last updated. Overflows in about 24 days so no worries. */
        Actor mActor;/**< This Sprite's Actor. */
        SDL_Surface *mScreen;/**< Screen to be drawn to. */
-       Point2D mPos;/**< Object's position */
-       bool mVisible;/**< Determine if Object should be visible */
        //SpriteBehavior mBehavior;/**< \todo implement*/
-       int ZOrder;/**< Stacking order. Determines what draws on top. \todo implement. */
 };
 #endif
index d9f5787..8dc0dbb 100644 (file)
@@ -3,15 +3,19 @@
 class WorldObject
 {
        public:
-       void draw();/**< draws Sprite. */
-       void xadd(int num) {mPos.X += num;}/**< Increase X coordiante by a given amount. */
-       void yadd(int num) {mPos.Y += num;}/**< Increase Y coordinate by a given amount. */
-       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. */
+               WorldObject(int z = 0) :
+                       ZOrder(z),
+                       mVisible(true)
+                       {}
+       virtual void draw() = 0;/**< draws object. */
+       void xadd(int num) {mPos.x += num;}/**< Increase x coordiante by a given amount. */
+       void yadd(int num) {mPos.y += num;}/**< Increase y coordinate by a given amount. */
+       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. */
 
-       private:
-       Point2D mPos;/**< Object's position */
+       protected:
+       Point2D mPos;/**< The current (x,y) position */
        bool mVisible;/**< Determine if Object should be visible */
        int ZOrder;/**< Stacking order. Determines what draws on top. \todo implement. */
 };