Made game into a singleton and compiled and added the libraries form SDL_draw
authorunknown <ivan@.ne.tamu.edu>
Tue, 18 Jan 2011 20:48:55 +0000 (14:48 -0600)
committerunknown <ivan@.ne.tamu.edu>
Tue, 18 Jan 2011 20:48:55 +0000 (14:48 -0600)
Ran fine. Tried to make position increase by object's position to failiure.

22 files changed:
Actor.h
Animation.cpp
Animation.h
Background.cpp
Background.h
Behavior.h
Behaviors.h
Collision.cpp
Collision.h
Frame.h
Game.cpp
Game.h
Level.h
LevelWorld.h
LoadLevels.h
Sprite.cpp
Sprite.h
Text.h
WorldObject.cpp
WorldObject.h
fns.h
main.cpp

diff --git a/Actor.h b/Actor.h
index f928bcb..9e55136 100644 (file)
--- a/Actor.h
+++ b/Actor.h
@@ -1,5 +1,5 @@
-#ifndef ACTOR
-#define ACTOR
+#ifndef __ACTOR_H__
+#define __ACTOR_H__
 #include "Animation.h"
 #include <SDL/SDL.h>
 #include <vector>
index eedee23..fca422c 100644 (file)
@@ -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;
                                        }
index 3d4de32..2f57b39 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef ANIMATION
-#define ANIMATION
+#ifndef __ANIMATION_H__
+#define __ANIMATION_H__
 #include "Frame.h"
 #include <string>
 #include <SDL/SDL.h>
index f431f1d..3dd167c 100644 (file)
@@ -1,4 +1,5 @@
 #include "Background.h"
+#include "Game.h"
 #include <iostream>
 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){
index 7ae8521..5c19249 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef BACKGROUND
-#define BACKGROUND
+#ifndef __BACKGROUND_H__
+#define __BACKGROUND_H__
 #include "fns.h"
 #include "WorldObject.h"
 #include "Collision.h"
index 7d71281..c38fef6 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef BEHAVIOR
-#define BEHAVIOR
+#ifndef __BEHAVIOR_H__
+#define __BEHAVIOR_H__
 #include "fns.h"
 #include <vector>
 #include <string>
index 7c949c4..39cd4e0 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef BEHAVIORS
-#define BEHAVIORS
+#ifndef __BEHAVIORS_H__
+#define __BEHAVIORS_H__
 #include "Behavior.h"
 #include <vector>
 #include <string>
index f0860cd..b38b360 100644 (file)
@@ -1,12 +1,13 @@
 #include "Collision.h"
 #include <SDL/SDL_draw.h>
+#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){
index 0b7d34f..9296342 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef COLLISION
-#define COLLISION
+#ifndef __COLLISION_H__
+#define __COLLISION_H__
 #include "fns.h"
 #include <vector>
 #include <string>
@@ -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 (file)
--- a/Frame.h
+++ b/Frame.h
@@ -1,5 +1,5 @@
-#ifndef FRAME
-#define FRAME
+#ifndef __FRAME_H__
+#define __FRAME_H__
 #include "Collision.h"
 #include <string>
 #include <SDL/SDL.h>
@@ -13,7 +13,7 @@ struct SpriteFrame{
        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 */
+       vector<Collision*> collisionData;/**< The collision data for this sprite */
 };
 
 #endif
\ No newline at end of file
index b79f2fa..85fedd8 100644 (file)
--- 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 (file)
--- a/Game.h
+++ b/Game.h
     - 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 (file)
--- 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"
index 0b5a0ba..6084926 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef LEVELWORLD
-#define LEVELWORLD
+#ifndef __LEVELWORLD_H__
+#define __LEVELWORLD_H__
 #include "Level.h"
 
 /**
index 1221949..878d499 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef LOADLEVELS
-#define LOADLEVELS
+#ifndef __LOADLEVELS_H__
+#define __LOADLEVELS_H__
 #include <vector>
 #include "fns.h"
 
index eafa76e..212c03d 100644 (file)
@@ -1,4 +1,5 @@
 #include "Sprite.h"
+#include "Game.h"
 #include <iostream>
 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);
 }
index 7eba848..e6e7648 100644 (file)
--- a/Sprite.h
+++ b/Sprite.h
@@ -1,5 +1,5 @@
-#ifndef SPRITE
-#define SPRITE
+#ifndef __SPRITE_H__
+#define __SPRITE_H__
 #include <SDL/SDL.h>
 #include <string>
 #include "fns.h"
diff --git a/Text.h b/Text.h
index 7ec5109..0bba3ba 100644 (file)
--- a/Text.h
+++ b/Text.h
@@ -1,5 +1,5 @@
-#ifndef TEXT
-#define TEXT
+#ifndef __TEXT_H__
+#define __TEXT_H__
 #include <stdio.h>
 #include <stdlib.h>
 #include <string>
index da2c48e..973a5d6 100644 (file)
@@ -2,8 +2,8 @@
 #include "fns.h"
 
 
-void WorldObject::drawCollisions(vector<Collision*> *vec){
-       for(int i=0; i < vec->size(); i++){
-               (*vec)[i]->draw();
+void WorldObject::drawCollisions(vector<Collision*> &vec, Point2D& pos){
+       for(int i=0; i < vec.size(); i++){
+               vec[i]->draw(pos);
        }
 }
index 3477005..a12380d 100644 (file)
@@ -10,8 +10,8 @@ public:
          ZOrder(z),
                  mVisible(true)
          {}
-         virtual void draw() = 0;/**< Draws the Object. */
-         void drawCollisions(vector<Collision*> *vec);/**< Draws Collision data for the Object */
+         virtual void draw(Point2D& pos) = 0;/**< Draws the Object. */
+         void drawCollisions(vector<Collision*>& 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 (file)
--- a/fns.h
+++ b/fns.h
@@ -1,13 +1,11 @@
-#ifndef FNS
-#define FNS
+#ifndef __FNS_H__
+#define __FNS_H__
 #include <SDL/SDL.h>
 #include <string>
 /// \todo remove this this is just to make testing easy w/o gdb
 #include <iostream>
 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. */
 
index 93b8214..f8d63ef 100644 (file)
--- 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;
 }