From 75707bae2024a5900ab13f0569e575d7cc8b9610 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 18 Jan 2011 14:48:55 -0600 Subject: [PATCH] Made game into a singleton and compiled and added the libraries form SDL_draw Ran fine. Tried to make position increase by object's position to failiure. --- Actor.h | 4 ++-- Animation.cpp | 4 ++-- Animation.h | 4 ++-- Background.cpp | 3 +++ Background.h | 4 ++-- Behavior.h | 4 ++-- Behaviors.h | 4 ++-- Collision.cpp | 9 +++++---- Collision.h | 18 +++++++++--------- Frame.h | 6 +++--- Game.cpp | 20 ++++++++++++++++++-- Game.h | 14 +++++++++++--- Level.h | 4 ++-- LevelWorld.h | 4 ++-- LoadLevels.h | 4 ++-- Sprite.cpp | 3 +++ Sprite.h | 4 ++-- Text.h | 4 ++-- WorldObject.cpp | 6 +++--- WorldObject.h | 4 ++-- fns.h | 8 +++----- main.cpp | 4 +--- 22 files changed, 83 insertions(+), 56 deletions(-) diff --git a/Actor.h b/Actor.h index f928bcb..9e55136 100644 --- a/Actor.h +++ b/Actor.h @@ -1,5 +1,5 @@ -#ifndef ACTOR -#define ACTOR +#ifndef __ACTOR_H__ +#define __ACTOR_H__ #include "Animation.h" #include #include diff --git a/Animation.cpp b/Animation.cpp index eedee23..fca422c 100644 --- a/Animation.cpp +++ b/Animation.cpp @@ -87,7 +87,7 @@ int Animation::loadAnimation(std::string animFile) value>>yOffset; double radius; value>>radius; - mAnim[count].bounds.push_back(new CollisionCircle(Point2D(xOffset, yOffset), radius)); + mAnim[count].collisionData.push_back(new CollisionCircle(Point2D(xOffset, yOffset), radius)); } else if(c == 'r') { @@ -97,7 +97,7 @@ int Animation::loadAnimation(std::string animFile) double width = 0.0 , height = 0.0; value>>width; value>>height; - mAnim[count].bounds.push_back(new CollisionRectangle(Point2D(xOffset, yOffset), width, height)); + mAnim[count].collisionData.push_back(new CollisionRectangle(Point2D(xOffset, yOffset), width, height)); } value>>c; } diff --git a/Animation.h b/Animation.h index 3d4de32..2f57b39 100644 --- a/Animation.h +++ b/Animation.h @@ -1,5 +1,5 @@ -#ifndef ANIMATION -#define ANIMATION +#ifndef __ANIMATION_H__ +#define __ANIMATION_H__ #include "Frame.h" #include #include diff --git a/Background.cpp b/Background.cpp index f431f1d..3dd167c 100644 --- a/Background.cpp +++ b/Background.cpp @@ -1,4 +1,5 @@ #include "Background.h" +#include "Game.h" #include using namespace std; @@ -142,6 +143,8 @@ 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){ diff --git a/Background.h b/Background.h index 7ae8521..5c19249 100644 --- a/Background.h +++ b/Background.h @@ -1,5 +1,5 @@ -#ifndef BACKGROUND -#define BACKGROUND +#ifndef __BACKGROUND_H__ +#define __BACKGROUND_H__ #include "fns.h" #include "WorldObject.h" #include "Collision.h" diff --git a/Behavior.h b/Behavior.h index 7d71281..c38fef6 100644 --- a/Behavior.h +++ b/Behavior.h @@ -1,5 +1,5 @@ -#ifndef BEHAVIOR -#define BEHAVIOR +#ifndef __BEHAVIOR_H__ +#define __BEHAVIOR_H__ #include "fns.h" #include #include diff --git a/Behaviors.h b/Behaviors.h index 7c949c4..39cd4e0 100644 --- a/Behaviors.h +++ b/Behaviors.h @@ -1,5 +1,5 @@ -#ifndef BEHAVIORS -#define BEHAVIORS +#ifndef __BEHAVIORS_H__ +#define __BEHAVIORS_H__ #include "Behavior.h" #include #include diff --git a/Collision.cpp b/Collision.cpp index f0860cd..b38b360 100644 --- a/Collision.cpp +++ b/Collision.cpp @@ -1,12 +1,13 @@ #include "Collision.h" #include +#include "Game.h" -void CollisionRectangle::draw(){ - Draw_Rect(This->Screen(), pos.x, pos.y, width, height, color); +void CollisionRectangle::draw(Point2D& pos){ + Draw_Rect(Game::game()->Screen(), pos.x + mPos.x, pos.y + mPos.y, width, height, color); } -void CollisionCircle::draw(){ - Draw_Circle(This->Screen(), pos.x, pos.y, radius, color); +void CollisionCircle::draw(Point2D& pos){ + Draw_Circle(Game::game()->Screen(), pos.x + mPos.x, pos.y + mPos.y, radius, color); } bool CollisionRectangle::collision(const Collision *c){ diff --git a/Collision.h b/Collision.h index 0b7d34f..9296342 100644 --- a/Collision.h +++ b/Collision.h @@ -1,5 +1,5 @@ -#ifndef COLLISION -#define COLLISION +#ifndef __COLLISION_H__ +#define __COLLISION_H__ #include "fns.h" #include #include @@ -17,11 +17,11 @@ using std::string; class Collision { public: - Collision():pos(0.0,0.0),color(0xFF00FF00){ } - Point2D pos;/**< The position of the center of the collision data */ + Collision():mPos(0.0,0.0),color(0xFF00FF00){ } + Point2D mPos;/**< The position of the center of the collision data */ virtual bool collision(const Collision *c) = 0; /**< Check collision with objects */ /// \todo See this http://www.metanetsoftware.com/technique/tutorialA.html - virtual void draw() = 0;/**< Draws the collision data to the screen */ + virtual void draw(Point2D& pos) = 0;/**< Draws the collision data to the screen */ protected: string name;///< Name of this behavior \todo make this actually be useful Uint32 color;///< The collision box color @@ -40,11 +40,11 @@ class CollisionRectangle : public Collision { public: CollisionRectangle():width(1.0),height(1.0){}; - CollisionRectangle(Point2D pt, double w, double h):width(w),height(h){ pos = pt;} + CollisionRectangle(Point2D pt, double w, double h):width(w),height(h){ mPos = pt;} double width;/**< Rectangle's width */ double height;/**< Rectangle's height */ virtual bool collision(const Collision *c);/**< Check collision with objects */ - virtual void draw();/**< Draws the collision data to the screen */ + virtual void draw(Point2D& pos);/**< Draws the collision data to the screen */ }; /** @@ -60,10 +60,10 @@ class CollisionCircle : public Collision { public: CollisionCircle(); - CollisionCircle(Point2D pt, double r) : radius(r){ pos = pt; } + CollisionCircle(Point2D pt, double r) : radius(r){ mPos = pt; } double radius; /**< The raidus of the circle */ virtual bool collision(const Collision *c);/**< Check collision with objects */ bool collision(const CollisionRectangle*);/**< This does collision between Rectangles and Circles */ - virtual void draw();/**< Draws the collision data to the screen */ + virtual void draw(Point2D& pos);/**< Draws the collision data to the screen */ }; #endif diff --git a/Frame.h b/Frame.h index be2e080..5a574c3 100644 --- a/Frame.h +++ b/Frame.h @@ -1,5 +1,5 @@ -#ifndef FRAME -#define FRAME +#ifndef __FRAME_H__ +#define __FRAME_H__ #include "Collision.h" #include #include @@ -13,7 +13,7 @@ struct SpriteFrame{ int height;/**< Base height of the frame's image. */ vector 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 bounds;/**< The collision data for this sprite */ + vector collisionData;/**< The collision data for this sprite */ }; #endif \ No newline at end of file diff --git a/Game.cpp b/Game.cpp index b79f2fa..85fedd8 100644 --- a/Game.cpp +++ b/Game.cpp @@ -6,7 +6,20 @@ const Uint32 Game::waitTime = 1000/60; /* ms */ -Game::Game() : mCurrentLevel(0), mScreen(0) +/** Global static pointer used to ensure a single instance of the class. */ +Game* Game::m_instance = NULL; + +/** This function is called to create an instance of the class. + Calling the constructor publicly is not allowed. + The constructor is private and is only called by this Instance function. +*/ +Game* Game::game(){ + if(!m_instance) + m_instance = new Game; + return m_instance; +} + +Game::Game() : mCurrentLevel(0), mScreen(0), ShowCollisions(false) { /** Initialize SDL */ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) { @@ -99,8 +112,11 @@ void Game::run() done = 1; break; } + if (keys[SDLK_F11]) { + ShowCollisions = !ShowCollisions; + } } - default: /* fallthrough if the key wasn't escape */ + default: /* fallthrough if the key wasn't escape or control*/ getCurrentLevel()->postEvent(event); break; } diff --git a/Game.h b/Game.h index 8dee6db..802382a 100644 --- a/Game.h +++ b/Game.h @@ -12,12 +12,16 @@ - creating the SDL screen - loading levels from appropriate locations - switching levels as appropriate + NOTE: If the screen needs to be accessed, you can use Game::game()->Screen(); */ class Game { public: - Game(); - ~Game(); + /** This Makes an instance of Game if there is not one and then returns it \return The level.*/ + static Game* game(); + + /** Decides whether or not to draw bounding boxes*/ + bool ShowCollisions; /** Gets the current level being played. @@ -36,7 +40,11 @@ public: /** Returns the main screen to allow for drawing */ SDL_Surface* Screen(){ return mScreen; } - +protected: + Game(); + ~Game(); + /** This is the pointer to the Static Game object insuring only one*/ + static Game* m_instance; private: static const Uint32 waitTime; diff --git a/Level.h b/Level.h index 7aff340..e399898 100644 --- a/Level.h +++ b/Level.h @@ -1,5 +1,5 @@ -#ifndef LEVEL -#define LEVEL +#ifndef __LEVEL_H__ +#define __LEVEL_H__ #include "Sprite.h" #include "Background.h" #include "Behaviors.h" diff --git a/LevelWorld.h b/LevelWorld.h index 0b5a0ba..6084926 100644 --- a/LevelWorld.h +++ b/LevelWorld.h @@ -1,5 +1,5 @@ -#ifndef LEVELWORLD -#define LEVELWORLD +#ifndef __LEVELWORLD_H__ +#define __LEVELWORLD_H__ #include "Level.h" /** diff --git a/LoadLevels.h b/LoadLevels.h index 1221949..878d499 100644 --- a/LoadLevels.h +++ b/LoadLevels.h @@ -1,5 +1,5 @@ -#ifndef LOADLEVELS -#define LOADLEVELS +#ifndef __LOADLEVELS_H__ +#define __LOADLEVELS_H__ #include #include "fns.h" diff --git a/Sprite.cpp b/Sprite.cpp index eafa76e..212c03d 100644 --- a/Sprite.cpp +++ b/Sprite.cpp @@ -1,4 +1,5 @@ #include "Sprite.h" +#include "Game.h" #include using std::cout; using std::endl; @@ -38,4 +39,6 @@ void Sprite::draw() dest.y = mPos.y; if(mVisible == true) SDL_BlitSurface(mActor.mAnimations[mActor.mCurrentAnimation]->mAnim[mActor.mFrame].image,NULL,mScreen,&dest); + if(Game::game()->ShowCollisions) + drawCollisions(mActor.mAnimations[mActor.mCurrentAnimation]->mAnim[mActor.mFrame].collisionData, mPos); } diff --git a/Sprite.h b/Sprite.h index 7eba848..e6e7648 100644 --- a/Sprite.h +++ b/Sprite.h @@ -1,5 +1,5 @@ -#ifndef SPRITE -#define SPRITE +#ifndef __SPRITE_H__ +#define __SPRITE_H__ #include #include #include "fns.h" diff --git a/Text.h b/Text.h index 7ec5109..0bba3ba 100644 --- a/Text.h +++ b/Text.h @@ -1,5 +1,5 @@ -#ifndef TEXT -#define TEXT +#ifndef __TEXT_H__ +#define __TEXT_H__ #include #include #include diff --git a/WorldObject.cpp b/WorldObject.cpp index da2c48e..973a5d6 100644 --- a/WorldObject.cpp +++ b/WorldObject.cpp @@ -2,8 +2,8 @@ #include "fns.h" -void WorldObject::drawCollisions(vector *vec){ - for(int i=0; i < vec->size(); i++){ - (*vec)[i]->draw(); +void WorldObject::drawCollisions(vector &vec, Point2D& pos){ + for(int i=0; i < vec.size(); i++){ + vec[i]->draw(pos); } } diff --git a/WorldObject.h b/WorldObject.h index 3477005..a12380d 100644 --- a/WorldObject.h +++ b/WorldObject.h @@ -10,8 +10,8 @@ public: ZOrder(z), mVisible(true) {} - virtual void draw() = 0;/**< Draws the Object. */ - void drawCollisions(vector *vec);/**< Draws Collision data for the Object */ + virtual void draw(Point2D& pos) = 0;/**< Draws the Object. */ + void drawCollisions(vector& vec, Point2D& pos);/**< Draws Collision data for the 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. */ diff --git a/fns.h b/fns.h index 2ddbab1..6d7967a 100644 --- a/fns.h +++ b/fns.h @@ -1,13 +1,11 @@ -#ifndef FNS -#define FNS +#ifndef __FNS_H__ +#define __FNS_H__ #include #include /// \todo remove this this is just to make testing easy w/o gdb #include using namespace std; -class Game; -///This is a pointer so that all objects in-game can obtain data that is needed everywhere. \todo make so that we have This.game or This->game -Game* This; + SDL_Surface* LoadImage( std::string filename );/**< Loads supported images. */ diff --git a/main.cpp b/main.cpp index 93b8214..f8d63ef 100644 --- a/main.cpp +++ b/main.cpp @@ -88,9 +88,7 @@ bufp[0] = color; // until an escape condition is reached int main (int argc, char *argv[]) { - Game g; - This = &g; - g.run(); + Game::game()->run(); return 0; } -- 2.11.0