From 7573d8a4a43b0bbc61fcaf64b75243e5330ad3ec Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 17 Jan 2011 23:05:35 -0600 Subject: [PATCH] Implemented WorldObject! --- Background.cpp | 166 ++++++++++++++++++++++++++++++--------------------------- Background.h | 9 +--- Sprite.cpp | 6 +-- Sprite.h | 15 ++---- WorldObject.h | 20 ++++--- 5 files changed, 106 insertions(+), 110 deletions(-) diff --git a/Background.cpp b/Background.cpp index 5e395b0..f431f1d 100644 --- a/Background.cpp +++ b/Background.cpp @@ -2,7 +2,13 @@ #include 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"< 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; diff --git a/Background.h b/Background.h index 671984e..7ae8521 100644 --- a/Background.h +++ b/Background.h @@ -1,6 +1,7 @@ #ifndef BACKGROUND #define BACKGROUND #include "fns.h" +#include "WorldObject.h" #include "Collision.h" #include #include @@ -16,14 +17,13 @@ using std::vector; - drawing the background */ -class Background{ +class Background : public WorldObject{ private: string _name;/// 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); diff --git a/Sprite.h b/Sprite.h index 3fc47da..7eba848 100644 --- 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. @@ -13,10 +14,10 @@ - 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 diff --git a/WorldObject.h b/WorldObject.h index d9f5787..8dc0dbb 100644 --- a/WorldObject.h +++ b/WorldObject.h @@ -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. */ }; -- 2.11.0