#include "fns.h"
using namespace std;
-Animation::Animation(std::string animFile)
+Animation::Animation(std::string animFile):mName(animFile)
+{
+ loadAnimation(animFile);
+}
+
+Animation::Animation(std::string animFile, string name):mName(name)
{
loadAnimation(animFile);
}
{
public:
Animation(std::string animFile);
+ Animation(std::string animFile, string name);
int loadAnimation(std::string animFile);/**< Loads the Animations from a file in the specified format. */
SpriteFrame *mAnim;/**< Pointer to the current animation. As an array of SpriteFrames */
int mBuilt,/**< Using as a bool */
mNumFrames,/**< Number of frames in this Animation */
mW,/**< Animation's current width */
mH;/**< The animation's current Height */
-
- private:
- std::string mname;//add name to anim file HERE
+ bool operator==(Animation a){return mName==a.mName;}
+ std::string mName;//add name to anim file
};
#endif
#Number of Frames
#ArtAssets/ milisec Transparency
-#filename pause r g b xOffset yOffset ( collisionType <Circle has offset, radius | rectangle has offset, width, height>, ... )
+#filename pause r g b xOffset yOffset ( <n | c | r> <c=circle has offset, radius | r=rectangle has offset, width, height>, ... )
NumFrames: 4
sun1.bmp 20 0 0 255 0.0 0.0 ( c 0.0 0.0 10.0 )
sun2.bmp 20 0 0 255 0.0 0.0 ( c 0.0 0.0 10.0 )
#Number of Frames
#ArtAssets/ milisec Transparency
-#filename pause r g b xOffset yOffset ( <n | c | r> <Circle has offset, radius | rectangle has offset, width, height>, ... )
+#filename pause r g b xOffset yOffset ( <n | c | r> <c=circle has offset, radius | r=rectangle has offset, width, height>, ... )
NumFrames: 9
viking1.bmp 50 0 255 0 0.0 0.0 ( r 0 0 150.0 100.0 )
viking2.bmp 50 0 255 0 0.0 0.0 ( r 0 0 150.0 100.0 )
+++ /dev/null
-#ifndef __BEHAVIOR_H__
-#define __BEHAVIOR_H__
-#include "fns.h"
-#include <vector>
-#include <string>
-
-using std::vector;
-using std::string;
-
-/**
- This is an action that a Behavior, Sprite, or other object can have.
-
- This class is tasked with the following:
- - being a parent container for actions.
- */
-
-class Behavior
-{
-public:
- Behavior(){}
- Behavior(string Name):name(Name){}/**< Loads functions from Game */
- virtual void load(){}/**< Loading Behavior*/
- virtual void update(){}/**< Update Behavior*/
- virtual void unload(){}/**< Delete Behavior*/
-protected:
- string name;/**< Name of this behavior */
-};
-#endif
#ifndef __BEHAVIORS_H__
#define __BEHAVIORS_H__
-#include "Behavior.h"
#include <vector>
#include <string>
+#include "Game.h"
using std::vector;
using std::string;
-/**
- This is an action that a Behavior, Sprite, or other object can have.
+///\file this file should contain all Behaviors for WorldObjects inlcuding Backgrounds
- This class is tasked with the following:
- - All actions for the World Level.
- */
-class LevelBehavior : public Behavior
-{
-public:
- LevelBehavior(){}
- virtual void load(){}/**< Loading Behavior*/
- virtual void update(){}/**< Update Behavior*/
- virtual void unload(){}/**< Delete Behavior*/
- //Level* level;
-};
-
-/**
- This is an action that a Behavior, Sprite, or other object can have.
-
- This class is tasked with the following:
- - All actions for Sprites.
- */
-class SpriteBehavior : public Behavior
-{
-public:
- SpriteBehavior(){}
- virtual void load(){}/**< Loading Behavior*/
- virtual void update(){}/**< Update Behavior*/
- virtual void unload(){}/**< Delete Behavior*/
- //Sprite* sprite;
-};
#endif
find_package(SDL REQUIRED)
find_package(SDL_image REQUIRED)
-add_executable(game Animation.cpp fns.cpp Game.cpp Level.cpp main.cpp WorldObject.cpp Sprite.cpp Text.cpp Actor.cpp Background.cpp LevelWorld.cpp Collision.cpp)
+add_executable(game Animation.cpp fns.cpp Game.cpp Level.cpp main.cpp WorldObject.cpp Sprite.cpp Text.cpp Actor.cpp Background.cpp Collision.cpp LoadResources.cpp)
target_link_libraries(game ${SDL_LIBRARY} ${SDLIMAGE_LIBRARY})
#include "fns.h"
#include <string>
#include <iostream>
-#include "LoadLevels.h"
const Uint32 Game::waitTime = 1000/60; /* ms */
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.
+ 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)
SDL_WM_SetCaption("Ivan's Game", NULL);
SDL_ShowCursor(0);
- /** Create the first level if there are none */
- LoadLevels l(mScreen);
- if(l.levels.size() == 0)
- mLevels.push_back(new Level(mScreen));
- else
- mLevels = l.levels;
+
+ /** Load Resources */
+ LoadResources();
+
// set start time for fps calc
startclock = SDL_GetTicks();
}
return mCurrentLevel;
}
+void Game::setCurrentLevel(string name){
+ for(unsigned int i=0, count = mLevels.size(); i < count; i++){
+ if(mLevels[i]->getName() == name){
+ mCurrentLevel=i;
+ this->mLevels[mCurrentLevel]->load();
+ return;
+ }
+ }
+ cout<<"Level "<<name<<" does not exist."<<endl;
+}
+
+int Game::getLevelCount()
+{
+ return mLevels.size();
+}
+
void Game::run()
{
bool done = false;
return;
}
+ setCurrentLevel("World");
+
/** Game Loop */
while (!done) {
/** This will let us track events */
getCurrentLevel()->postEvent(event);
break;
}
+
getCurrentLevel()->update();
if(Game::game()->ShowFPS)
cout<<Game::game()->getFPS()<<endl;
+
//obtain the current fps data.
deltaclock = SDL_GetTicks() - startclock;
startclock = SDL_GetTicks();
SDL_RemoveTimer(timer);
}
-Behavior* Game::getBehavior(string name){
- Behavior* b = mBehaviors[name];
- if(b){
- return new Behavior(*b);
- }
- else
- return NULL;
-}
-
-void Game::loadLevel(std::string levelName){
- for(unsigned int i=0; i< mLevels.size(); ++i)
- if(mLevels[i]->getName() == levelName)
- mCurrentLevel = i;
- else
- std::cout<<"Level "<<levelName<<" not found."<<std::endl;
-}
-
Uint32 Game::timerCallback(Uint32 interval, void* data)
{
SDL_Event event;
#define __GAME_H__
#include "Level.h"
+#include "Sprite.h"
#include <vector>
#include <string>
Level* getCurrentLevel();
/**
+ Sets the current level to play.
+ \param name Name of the level to load
+ */
+ void setCurrentLevel(string name);
+
+ /**
Gets the index of the current level being played.
\return index of the current level
*/
int getLevelIndex();
+ /**
+ Gets the total count of levels.
+ \return number of levels
+ */
+ int getLevelCount();
+
/** Makes the game loop begin and load objects. */
void run();
/** Retrns the Current FPS of the game */
Uint32 getFPS() { return currentFPS;}
- Behavior* getBehavior(string name);/**<Gets the requested Behavior \param name Name of the behavior to obtain \return Copy of the requested Behavior. */
+ /** The current Sprite selected by the game */
+ static Sprite* CurrentSprite;
protected:
Game();
~Game();
- /** This is the pointer to the Static Game object insuring only one*/
+ /** This is the pointer to the Static Game object insuring only one */
static Game* m_instance;
+
+ /** Loads the game's resources. */
+ void LoadResources();
+
private:
static const Uint32 waitTime;
SDL_Surface *mScreen;/**< The surface to draw to. */
map<string,Behavior*> mBehaviors;/**< This level's Behaviors */
- void loadLevel(std::string levelName);/**< Loads a level by name. */
-
static Uint32 timerCallback(Uint32 interval, void* data);
//fps data calc
Level::Level(SDL_Surface* screen) : mScreen(screen)
{
- /** load background */
- mBackground= new Background(screen);//mBackground = LoadImage("Backgrounds/bg.bmp");
-
- mBehavior = new LevelBehavior();
-}
-
-Level::Level(SDL_Surface* screen,string n) : mScreen(screen), mName(n)
-{
- /** load background */
- mBackground= new Background(screen, n);//mBackground = LoadImage("Backgrounds/bg.bmp");
-
- mBehavior = new LevelBehavior();
+ mLoadBehavior = DoNothing;
+ mUpdateBehavior = DoNothing;
}
-Level::Level(SDL_Surface* screen,string n, string behaviorName) : mScreen(screen), mName(n)
+Level::Level(SDL_Surface* screen, string n, Behavior loadBehave, Behavior updateBehave) : mScreen(screen), mName(n), mBackground(NULL)
{
- /// load background
- mBackground= new Background(screen, n);//mBackground = LoadImage("Backgrounds/bg.bmp");
-
/// Set behavior
- mBehavior = getBehavior(behaviorName);
-
- if(!mBehavior)
- mBehavior = new LevelBehavior();
-
- /// Complete load behavior
- mBehavior->load();
+ mLoadBehavior = loadBehave;
+ mUpdateBehavior = updateBehave;
}
Level::~Level()
SDL_FillRect(mScreen, NULL, color);
/** Draw BG */
- mBackground->draw();
+ if(mBackground!=NULL)
+ mBackground->draw();
/** Draw Sprites*/
DrawSprites();
/** Will handle all movement and dynamic triggers. */
void Level::update(){
- mBehavior->update();
- //check collision with sun sprite
- mSprites[0]->collisionWithSprite("sun");
- //check collision with sun sprite
- mSprites[0]->collisionWithSprite("viking2");
+ mUpdateBehavior();
+}
+
+void Level::load(){
+ mLoadBehavior();
}
mSprites.push_back(sp);
}
+Sprite* Level::getSprite(string name){
+ for(int i=0, count=mSprites.size(); i < count; i++){
+ if(mSprites[i]->getName()==name){
+ return mSprites[i];
+ }
+ }
+ return NULL;
+}
+
void Level::removeSprite(Sprite* sp){
for(vector<Sprite*>::iterator ptr = mSprites.begin(); ptr != mSprites.end(); ptr++){
if(*ptr == sp){
}
}
+void Level::addAnimation(Animation anim){
+ mAnims.push_back(anim);
+}
+
+Animation* Level::getAnimation(string name){
+ for(int i=0, count=mAnims.size(); i < count; i++){
+ if(mAnims[i].mName==name){
+ return &mAnims[i];
+ }
+ }
+ return NULL;
+}
+
+void Level::removeAnimation(Animation anim){
+ for(vector<Animation>::iterator ptr = mAnims.begin(); ptr != mAnims.end(); ptr++){
+ if(*ptr == anim){
+ mAnims.erase(ptr);
+ return;
+ }
+ }
+}
+
void Level::addActor(string name, Actor actor){
mActors.insert(make_pair(name, actor));
}
}
return NULL;//if a sprite wasn't found return null
}
-
-LevelBehavior* Level::getBehavior(string name){
- Behavior* b = Game::game()->getBehavior(name);
- if(LevelBehavior* lb = dynamic_cast<LevelBehavior*>(b)){
- return new LevelBehavior(*lb);
- }
- else
- return NULL;
-}
#define __LEVEL_H__
#include "Sprite.h"
#include "Background.h"
-#include "Behaviors.h"
#include <vector>
#include <map>
class Level
{
public:
- Level(SDL_Surface *screen);
+ Level(SDL_Surface* screen);
/**
Level constructor
\param screen the Screen to be drawn to
\param name of the level
+ \param loadBehavior the Loading Behavior for this level (Run once).
+ \param updateBehavior the Updating Behavior for this level (Run every loop).
*/
- Level(SDL_Surface *screen, string name);
- /**
- Level constructor
- \param screen the Screen to be drawn to
- \param name of the level
- \param behavior the Behavior for this level.
- */
- Level(SDL_Surface *screen, string name, string behavior);
+ Level(SDL_Surface* screen, string name, Behavior loadBehave = DoNothing, Behavior updateBehave = DoNothing);
~Level();
void drawScene();/**< Draws everything that is set to draw on the screen. */
void LoadBG(string name);/**< Loads the Background. \param name Name of the background to load */
virtual void postEvent(SDL_Event event);/**< Passes along SDL events */
- virtual void update();/**< Loop that runs every game frame that calculates movement, placement, etc. as well as obtains key strokes (limited by keyboard hardware)*/
+ void update();/**< Loop that runs every game frame that calculates movement, placement, etc. as well as obtains key strokes (limited by keyboard hardware)*/
+ void load();/**< Runs load action*/
void addSprite(Sprite* sp);/**< add a Sprite to the list of sprites \param sp Sprite to add */
+ Sprite* getSprite(string name);/**< gets the first sprite with the given name \param name Name of the sprite \return Pointer to the requested Sprite */
void removeSprite(Sprite* sp);/**< remove the Sprite sp from the list of sprites \param ps Sprite to remove */
+ void addAnimation(Animation anim);/**< add a Actor to the list of sprites \param anim The actor to add */
+ Animation* Level::getAnimation(string name);/**< get a Actor* to the list of sprites \param name Name of the actor \return Pointer to the actor we requested or null */
+ void removeAnimation(Animation anim);/**< remove an Actor to the list of sprites \param anim The actor to add */
void addActor(string name, Actor actor);/**< add a Actor to the list of sprites \param name Name of the actor \param actor The actor to add */
void removeActor(string name);/**< remove the Actor sp from the list of sprites \param name Name of the actor to remove */
string getName(){ return mName; }/**< returns the current level's name \return Level's name */
+ void setBackground(Background* b){mBackground=b;}/**< sets the current level's background \param b Background */
+ Background* getBackground(){return mBackground;}/**< gets the current level's name \return Level's Background */
+ SDL_Surface* getScreen(){return mScreen;}/**< gets the current level's name \return Level's SDL_Surface */
+
Sprite* findSpriteByName(string name);/**< returns the first Sprite with a given name \param name Name of the sprite to return \return Pointer to the requested sprite */
- LevelBehavior* getBehavior(string name);/**<Gets the requested Behavior \param name Name of the behavior to obtain \return Copy of the requested Behavior. */
+
protected://allow inheritance
string mName;
Background* mBackground;/**< Pointer to the current Background. */
SDL_Surface *mScreen;/**< Pointer to the screen. */
- //LevelBehavior mBehavior;/**< will be used to define the start actions, update, and leaving actions of the level */
vector<Sprite*> mSprites;/**< Vector of all sprites on the level. \todo Maybe make into map? This should be a list of World objects once implemented. \todo add accessor or move to public \todo make a map by name (for collisions) \todo make a list ordered by Zorder (for drawing*/
map<string,Actor> mActors;/**< This level's actors. */
- vector<Animation> mAnim;/**< This level's Animations */
+ vector<Animation> mAnims;/**< This level's Animations */
void DrawIMG();/**< Draws an image to the screen. (Not used) */
void DrawIMG(SDL_Surface *img, int x, int y);/**< Draws the specified image to the screen at location (x,y) */
void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2);/**< Draws an image at the specified location (x,y) that is blitted with width w and height h from the point (x2,y2) of the given image */
void DrawSprites();/**< Draws all Sprites. */
void DrawCollisions();/**< Draws all Collision data */
- LevelBehavior* mBehavior;
+ Behavior mLoadBehavior;/**< will be used to define the Load action of the level */
+ Behavior mUpdateBehavior;/**< will be used to define the update action of the level */
};
#endif
--- /dev/null
+#ifndef __LEVELFNS_H__
+#define __LEVELFNS_H__
+#include "Game.h"
+#include "Level.h"
+
+///\file this file should contain all Behaviors for Levels
+
+void LevelWorldLoad(){
+ Level* l = Game::game()->getCurrentLevel();
+
+ SDL_Surface* screen = l->getScreen();
+
+ /// load background
+ l->setBackground(new Background(screen, "default", "bg.bmp"));//mBackground = LoadImage("Backgrounds/bg.bmp");
+
+ /** load animations */
+ l->addAnimation(Animation("viking.anim"));
+ l->addAnimation(Animation("sun.anim"));
+
+ /** load sprites */
+ Sprite* vikings1 = new Sprite(screen, "viking1", l->getAnimation("viking.anim"));
+ vikings1->setPosition(10,30);
+ vikings1->setSpeed(1);
+
+ Sprite* vikings2 = new Sprite(screen, "viking2", l->getAnimation("viking.anim"));
+ vikings2->setPosition(350,300);
+ vikings2->setSpeed(1.5);
+
+ Sprite* sun = new Sprite(screen, "sun", l->getAnimation("sun.anim"));
+ sun->setPosition(480,50);
+ sun->setSpeed(1);
+}
+
+void LevelWorldUpdate(){
+ Level* LevelWorld = Game::game()->getCurrentLevel();
+
+ Sprite* player = LevelWorld->getSprite("viking1");
+
+ Background* bg = LevelWorld->getBackground();
+
+ Uint8 *keys = SDL_GetKeyState(NULL);
+ if ( keys[SDLK_LEFT] ) { player->xadd(-1); }
+ if ( keys[SDLK_RIGHT] ) { player->xadd(1); }
+ if ( keys[SDLK_UP] ) { player->yadd(-1); }
+ if ( keys[SDLK_DOWN] ) { player->yadd(1); }
+ if ( keys[SDLK_a] ) { bg->xadd(-1); }
+ if ( keys[SDLK_d] ) { bg->xadd(1); }
+ if ( keys[SDLK_w] ) { bg->yadd(-1); }
+ if ( keys[SDLK_s] ) { bg->yadd(1); }
+}
+
+#endif
\ No newline at end of file
+++ /dev/null
-#include "LevelWorld.h"
-#include "fns.h"
-
-LevelWorld::LevelWorld(SDL_Surface* screen) :
- Level(screen, "World")
-{
- /** load animations */
- mAnim.push_back(Animation("viking.anim"));
- mAnim.push_back(Animation("sun.anim"));
-
- /** load sprites */
- Sprite* vikings1 = new Sprite(screen, "viking1", &mAnim[0]);
- vikings1->setPosition(10,30);
- vikings1->setSpeed(1);
-
- Sprite* vikings2 = new Sprite(screen, "viking2", &mAnim[0]);
- vikings2->setPosition(350,300);
- vikings2->setSpeed(1.5);
-
- Sprite* sun = new Sprite(screen, "sun", &mAnim[1]);
- sun->setPosition(480,50);
- sun->setSpeed(1);
-
- mSprites.push_back(vikings1);
- mSprites.push_back(vikings2);
- mSprites.push_back(sun);
-}
-
-
-
-/** Will handle all movement and dynamic triggers. */
-void LevelWorld::update()
-{
- Uint8 *keys = SDL_GetKeyState(NULL);
- if ( keys[SDLK_LEFT] ) { mSprites[0]->xadd(-1); }
- if ( keys[SDLK_RIGHT] ) { mSprites[0]->xadd(1); }
- if ( keys[SDLK_UP] ) { mSprites[0]->yadd(-1); }
- if ( keys[SDLK_DOWN] ) { mSprites[0]->yadd(1); }
- if ( keys[SDLK_a] ) { mBackground->xadd(-1); }
- if ( keys[SDLK_d] ) { mBackground->xadd(1); }
- if ( keys[SDLK_w] ) { mBackground->yadd(-1); }
- if ( keys[SDLK_s] ) { mBackground->yadd(1); }
- Level::update();
-}
-
+++ /dev/null
-#ifndef __LEVELWORLD_H__
-#define __LEVELWORLD_H__
-#include "Level.h"
-
-/**
- This is a stand alone Level that will have varying jobs in respect to the LevelWorld map
-
- This class is tasked with the following:
- - drawing the scence and all objects in order
- - loading the background
- - keeping track of animations
- - moving objects
- - handling events for the Level
- */
-
-class LevelWorld : public Level
-{
-public:
- LevelWorld(SDL_Surface *screen);
- void drawScene();/**< Draws everything that is set to draw on the screen. */
- void update();/**< Loop that runs every game frame that calculates movement, placement, etc. as well as obtains key strokes (limited by keyboard hardware)*/
-};
-#endif
+++ /dev/null
-#ifndef __LOADLEVELS_H__
-#define __LOADLEVELS_H__
-#include <vector>
-#include "fns.h"
-
-/// Add all levels and includes here
-#include "Level.h"
-#include "LevelWorld.h"
-
-using namespace std;
-
-
-/**
- This is a container for creating all the levels in a neat location so as not to bulk up the Game class
-
- This class is tasked with the following:
- - Creating all the levels.
- */
-class LoadLevels{
-public:
- LoadLevels(SDL_Surface* screen){
- levels.push_back(new LevelWorld(screen));
- }
-
- vector<Level*> levels;
-};
-#endif
--- /dev/null
+#include "Game.h"
+#include "fns.h"
+#include "LevelFns.h"
+
+///\file this file should contain implementations for loading resources
+
+void Game::LoadResources(){
+ mLevels.push_back(new Level(mScreen, "World", LevelWorldLoad, LevelWorldUpdate));
+}
+
mActor(actor),
mScreen(screen)
{
- /** \todo Later add a part that adds name to list of level sprites
- Game.Level.SpriteList.pushback(name);
- */
+ //add to current level
+ Game::game()->getCurrentLevel()->addSprite((this));
+
if(mActor.mAnimations[mActor.mCurrentAnimation] -> mBuilt)
{
if (mActor.mAnimations[mActor.mCurrentAnimation]->mNumFrames > 1) mAnimating = 1;
if(frame->collisionData[i]->checkCollisions(s->getCollisionData(), s->mPos + s->getAnimation()->animationPeg, mPos + frame->animationPeg))//if there is a collision
return s;
return NULL;//if there aren't collisions
-}
-
-SpriteBehavior* Sprite::getBehavior(string name){
- Behavior* b = Game::game()->getBehavior(name);
- if(SpriteBehavior* sb = dynamic_cast<SpriteBehavior*>(b)){
- return new SpriteBehavior(*sb);
- }
- else
- return NULL;
}
\ No newline at end of file
#include <string>
#include "fns.h"
#include "Actor.h"
-#include "Behaviors.h"
#include "WorldObject.h"
/**
void drawCollisions();/**< Draws Collision data for the Object (from outside) */
Sprite* collisionWithSprite(string name);/**< checks for collision with sprite of a given name. */
vector<Collision*>& getCollisionData();/**< Returns collision data */
- SpriteBehavior* getBehavior(string name);/**<Gets the requested Behavior \param name Name of the behavior to obtain \return Copy of the requested Behavior. */
+
private:
std::string mName;/**< Sprite's name */
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. */
- SpriteBehavior* mBehavior;/**< Sprite's Behavior */
+ Behavior mBehavior;/**< Sprite's Behavior */
};
#endif
#include <iostream>
using namespace std;
+///Behaviors use functions that take nothing and return nothing
+typedef void (*Behavior) ();
+///As the name denotes, this just simply does nothing
+static void DoNothing(){}
SDL_Surface* LoadImage( std::string filename );/**< Loads supported images. */
<ClInclude Include="Frame.h" />
<ClInclude Include="Game.h" />
<ClInclude Include="Level.h" />
- <ClInclude Include="LevelWorld.h" />
- <ClInclude Include="LoadLevels.h" />
+ <ClInclude Include="LevelFns.h" />
<ClInclude Include="Sprite.h" />
<ClInclude Include="Text.h">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ClCompile Include="fns.cpp" />
<ClCompile Include="Game.cpp" />
<ClCompile Include="Level.cpp" />
- <ClCompile Include="LevelWorld.cpp" />
+ <ClCompile Include="LoadResources.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Sprite.cpp" />
<ClCompile Include="Text.cpp">