Ran fine. Tried to make position increase by object's position to failiure.
-#ifndef ACTOR
-#define ACTOR
+#ifndef __ACTOR_H__
+#define __ACTOR_H__
#include "Animation.h"
#include <SDL/SDL.h>
#include <vector>
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')
{
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;
}
-#ifndef ANIMATION
-#define ANIMATION
+#ifndef __ANIMATION_H__
+#define __ANIMATION_H__
#include "Frame.h"
#include <string>
#include <SDL/SDL.h>
#include "Background.h"
+#include "Game.h"
#include <iostream>
using namespace std;
dest.y = mPos.y;
SDL_BlitSurface(mBackground, &source, mScreen, &dest);
}
+ if(Game::game()->ShowCollisions)
+ drawCollisions(collisionData, mPos);
}
SDL_Surface* Background::load(std::string filename){
-#ifndef BACKGROUND
-#define BACKGROUND
+#ifndef __BACKGROUND_H__
+#define __BACKGROUND_H__
#include "fns.h"
#include "WorldObject.h"
#include "Collision.h"
-#ifndef BEHAVIOR
-#define BEHAVIOR
+#ifndef __BEHAVIOR_H__
+#define __BEHAVIOR_H__
#include "fns.h"
#include <vector>
#include <string>
-#ifndef BEHAVIORS
-#define BEHAVIORS
+#ifndef __BEHAVIORS_H__
+#define __BEHAVIORS_H__
#include "Behavior.h"
#include <vector>
#include <string>
#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){
-#ifndef COLLISION
-#define COLLISION
+#ifndef __COLLISION_H__
+#define __COLLISION_H__
#include "fns.h"
#include <vector>
#include <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
{
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 */
};
/**
{
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
-#ifndef FRAME
-#define FRAME
+#ifndef __FRAME_H__
+#define __FRAME_H__
#include "Collision.h"
#include <string>
#include <SDL/SDL.h>
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
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) {
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;
}
- 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.
/** 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;
-#ifndef LEVEL
-#define LEVEL
+#ifndef __LEVEL_H__
+#define __LEVEL_H__
#include "Sprite.h"
#include "Background.h"
#include "Behaviors.h"
-#ifndef LEVELWORLD
-#define LEVELWORLD
+#ifndef __LEVELWORLD_H__
+#define __LEVELWORLD_H__
#include "Level.h"
/**
-#ifndef LOADLEVELS
-#define LOADLEVELS
+#ifndef __LOADLEVELS_H__
+#define __LOADLEVELS_H__
#include <vector>
#include "fns.h"
#include "Sprite.h"
+#include "Game.h"
#include <iostream>
using std::cout;
using std::endl;
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);
}
-#ifndef SPRITE
-#define SPRITE
+#ifndef __SPRITE_H__
+#define __SPRITE_H__
#include <SDL/SDL.h>
#include <string>
#include "fns.h"
-#ifndef TEXT
-#define TEXT
+#ifndef __TEXT_H__
+#define __TEXT_H__
#include <stdio.h>
#include <stdlib.h>
#include <string>
#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);
}
}
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. */
-#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. */
// until an escape condition is reached
int main (int argc, char *argv[])
{
- Game g;
- This = &g;
- g.run();
+ Game::game()->run();
return 0;
}