Translated Level class from c# (mostly)
authorIvan Hernanez <iturtleman128@gmail.com>
Fri, 1 Jun 2012 03:04:13 +0000 (22:04 -0500)
committerIvan Hernanez <iturtleman128@gmail.com>
Fri, 1 Jun 2012 03:04:13 +0000 (22:04 -0500)
changed textrue from unsigned int to GLuint
added << for Point2D
World object implements ZOrder
got rid of SDL_Surface* screen from most items
moved text to class Text
Moved mName from Sprite to WorldObject
Added update behavior to sprites
Changed Loading to access loadinglevel instead of currentlevel (allowing loading of other levels)
Added framework for pausing and menu screens
changed currentlevel to int from uint so we can determine if the first level was loaded
Reworked background to use textures (draws again but doesn't wrap yet)

Implemented a lot of shit.

14 files changed:
Background.cpp
Background.h
Game.cpp
Game.h
Level.cpp
Level.h
LevelFns.h
LoadResources.cpp
Sprite.cpp
Sprite.h
Text.cpp
Text.h
WorldObject.h
fns.h

index 021db5a..ba05aad 100644 (file)
 #include <iostream>
 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),
-       mScreenSize(ScreenSize)
+Background::Background( std::string name, std::string filename, bool wrap, Point2D pos) :
+WorldObject(name,-10000, pos),
+       wrapping(wrap)
 {
-       mPos = ScreenPosition;
-       mScreenSize.w = screen->w;
-       mScreenSize.h = screen->h;
+       load(filename);
+       cout<<mPos<<endl<<mVisible<<endl<<mTransparency<<endl<<mAngle<<endl<<mScale<<endl<<ZOrder<<endl;
+       cout<<"background \""<<mName<<"\" created"<<endl;       
+}
+void Background::draw(){
+       glEnable(GL_BLEND);
+       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+       glColor4f(1, 1, 1, mTransparency);
+       glLoadIdentity();
+       glTranslated(mPos.x,mPos.y,0);//move to position
+               glRotatef(mAngle,0.0f,0.0f,1.0f);//rotate shape
+       glScaled(mScale, mScale, mScale);
+       glBindTexture(GL_TEXTURE_2D, mBackground);
+       glBegin( GL_QUADS );
+       // Top-left vertex (corner)
+       glTexCoord2f( 0, 0 );
+       glVertex3d( 0, 0, 0 );
 
-       source.w = (int)bgSize.w;
-       source.h = (int)bgSize.h;
-       source.x = (int)bgSamplePos.x;
-       source.y = (int)bgSamplePos.y;
+       // Bottom-left vertex (corner)
+       glTexCoord2f( 1, 0 );
+       glVertex3d( width, 0, 0 );
 
-       load(filename);
+       // Bottom-right vertex (corner)
+       glTexCoord2f( 1, 1 );
+       glVertex3d( width, height, 0 );
 
-       cout<<"background \""<<_name<<"\" created"<<endl;       
+       // Top-right vertex (corner)
+       glTexCoord2f( 0, 1 );
+       glVertex3d( 0, height, 0 );
+       glEnd();
+       glDisable(GL_BLEND);
+       glLoadIdentity();
 }
-void Background::draw(){
-       ///destination rectangle only position is used and describes where the output is drawn.
-       SDL_Rect dest;///<the place where the sample will be drawn (can be reused)
-       double x = mPos.x;
-       double y = mPos.y;
-       const double X = mScreenSize.w;
-       const double Y = mScreenSize.h;
-       const double W = _imageSize.w;
-       const double H = _imageSize.h;
-       /*
-       if(wrapping){
-               while (x < 0) // move it to the left til it's on the screen
-                       x += W;
-               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;
-               int initx = (int)x;
-               while(y > 0)
-                       y -= H;
-               for(y; y < Y; y += H)
-                       for(x = initx; x < X; x += W){
-                               dest.x = (int)x;
-                               dest.y = (int)y;
-                               SDL_BlitSurface(mBackground, NULL, mScreen, &dest);
-                       }
-       }
-       else {
-               dest.x = (int)mPos.x;
-               dest.y = (int)mPos.y;
-               SDL_BlitSurface(mBackground, &source, mScreen, &dest);
-       }
-       //*/
 
+void Background::update(){
 }
 
-SDL_Surface* Background::load(std::string filename){
-       /*mBackground = LoadImage("Backgrounds/"+filename);
+
+int Background::load(std::string filename){
+       /** Load image for Frame */
+       SDL_Surface *temp=NULL;
+       if((temp = LoadImage("Backgrounds/"+filename)) == NULL) return -1;//Load image for Frame
        cout<<"Loaded file"<<endl;
-       cout<<"Setting Size...";
-       _imageSize=SizeD(mBackground->w,mBackground->h);///<set the size of the background.
-       cout<<"done!"<<endl;
-       return mBackground;*/
-       return NULL;
+
+       /** Set transparent color */
+       //SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, transparency[0], transparency[1], transparency[2]));
+
+       /** Loads image into animation */
+       SDL_Surface* surface = SDL_DisplayFormatAlpha(temp);
+       SDL_FreeSurface(temp);
+       width=surface->w;
+       height=surface->h;
+
+       // Check that the image\92s width is a power of 2
+       if ( (surface->w & (surface->w - 1)) != 0 ) {
+               cout<<"warning: "<<filename<<"'s width is not a power of 2"<<endl;
+       }
+
+       // Also check if the height is a power of 2
+       if ( (surface->h & (surface->h - 1)) != 0 ) {
+               cout<<"warning: "<<filename<<"'s height is not a power of 2"<<endl;
+       }
+
+       //get number of channels in the SDL surface
+       GLint nofcolors=surface->format->BytesPerPixel;
+
+       GLenum texture_format=NULL;
+
+       //contains an alpha channel
+       if(surface->format->Rmask==0x000000ff)
+               texture_format=GL_RGBA;
+       else
+               texture_format=GL_BGRA;
+       if(!(nofcolors==4 || nofcolors==3))
+       {
+               cout<<"warning: the image is not truecolor...this will break "<<endl;
+       }
+
+       // allocate a texture name
+       glGenTextures( 1, &mBackground );
+
+       // select our current texture
+       glBindTexture( GL_TEXTURE_2D, mBackground );
+
+       // select modulate to mix texture with color for shading
+       glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
+
+       // when texture area is small, bilinear filter the closest mipmap
+       glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+       // when texture area is large, bilinear filter the original
+       glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
+
+       // the texture wraps over at the edges (repeat)
+       glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
+       glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
+
+       glTexImage2D( GL_TEXTURE_2D, 0, nofcolors, surface->w, surface->h, 0,
+               texture_format, GL_UNSIGNED_BYTE, surface->pixels );
+
+       return 0;
 }
 
 void Background::setWrap(bool tf){
index bfbcdf1..8ce2d99 100644 (file)
@@ -19,32 +19,23 @@ using std::vector;
 
 class Background : public WorldObject{
        private:
-       string _name;///<The background's name
-       SizeD _imageSize;///<The actual background image's full size.
-
        Texture mBackground;///<Background Texture
-       SDL_Surface *mScreen;///< The screen to be drawn to.
-       SizeD mScreenSize;///<This is the size of the screen to be drawn to. used to get the sections of the background when moving and for screen-at-a-time scrolling.
-       SDL_Rect source;///< This is the rectangle that gets taken from the actual background image (h,w) determine the size of the rectangle to sample from the background image. (x,y) determine the position of the rectangle's top left corner on the actual background image.
+       int width;
+       int height;
        bool wrapping;
        vector<Collision*> collisionData;///<The collision data for this Background \todo implement collisions
 
-       
        public:
-       Background(     SDL_Surface *screen,
-                               string name="default", 
+       Background(     string name="default", 
                                string filename="bg.bmp", 
                                bool wrap=true, 
-                               Point2D ScreenPosition=Point2D(0,0), 
-                               SizeD screenSize=SizeD(800,600),
-                               Point2D bgSamplePos=Point2D(0,0), 
-                               SizeD bgSize=SizeD(800,600)
+                               Point2D ScreenPosition=Point2D()
        );
        void draw();/**< Draw background. */
-       string getName(){ return _name; }/** Returns name. */
-       void setName(string name){ _name = name; }/** Set Name. */
-       SizeD size(){ return _imageSize;}/** returns image size */
-       SDL_Surface* load(string name="default");///< Loads a Bacground by name.
+       void update();/**< Update to run each loop */
+       string getName(){ return mName; }/** Returns name. */
+       void setName(string name){ mName = name; }/** Set Name. */
+       int load(string name="default");///< Loads a Bacground by name.
        SizeD getPosition(){return mPos;}
        void setWrap(bool tf);
        bool getWrap();
index 995167e..1653271 100644 (file)
--- a/Game.cpp
+++ b/Game.cpp
@@ -41,7 +41,7 @@ Game* Game::game(){
        return m_instance;
 }
 
-Game::Game() : mCurrentLevel(0), mScreen(0), ShowCollisions(false), ShowFPS(false), startclock(0), deltaclock(0), currentFPS(0), Paused(false)
+Game::Game() : mCurrentLevel(-1), mScreen(0), ShowCollisions(false), ShowFPS(false), startclock(0), deltaclock(0), currentFPS(0), Paused(false)
 {
        int screenwidth = 640;
        int screenheight = 480;
@@ -109,10 +109,31 @@ int Game::getLevelIndex()
 }
 
 void Game::setCurrentLevel(string name){
+       if(mCurrentLevel<0){
+               loadCurrentLevel(name);
+               return;
+       }
+       else
+       {
+               mLevels[mCurrentLevel]->unload();
+               for(unsigned int i=0, count = mLevels.size(); i < count; i++){
+                       if(mLevels[i]->getName() == name){
+                               mCurrentLevel=i;
+                               return;
+                       }
+               }
+       }
+       cout<<"Level "<<name<<" does not exist."<<endl;
+}
+
+void Game::loadCurrentLevel(string name){
        for(unsigned int i=0, count = mLevels.size(); i < count; i++){
                if(mLevels[i]->getName() == name){
-                       mCurrentLevel=i;
-                       this->mLevels[mCurrentLevel]->load();
+                       LoadingLevel = mLevels[i];
+                       this->mLevels[i]->load();
+                       LoadingLevel = NULL;
+                       if(mCurrentLevel<0)
+                               mCurrentLevel = i;
                        return;
                }
        }
@@ -134,6 +155,7 @@ void Game::run()
                return;
        }
 
+       
        setCurrentLevel("World");
 
        /** Game Loop */
diff --git a/Game.h b/Game.h
index 78467dc..81178d4 100644 (file)
--- a/Game.h
+++ b/Game.h
@@ -40,6 +40,12 @@ public:
        void setCurrentLevel(string name);
 
        /**
+         Loads content for the next level to play.
+         \param name Name of the level to load
+        */
+       void loadCurrentLevel(string name);
+
+       /**
          Gets the index of the current level being played.
          \return index of the current level
         */
@@ -58,7 +64,7 @@ public:
        SDL_Surface* Screen(){ return mScreen; }
 
        /** Retrns the Current FPS of the game */
-       Uint32 getFPS() { return currentFPS;}
+       Uint32 getFPS() { return currentFPS; }
 
        /** The current Sprite selected by the game */
        static Sprite* CurrentSprite;
@@ -81,7 +87,7 @@ protected:
 private:
        static const Uint32 waitTime;
 
-       unsigned int mCurrentLevel;/**< Current Level index. */
+       int mCurrentLevel;/**< Current Level index. */
        std::vector<Level*> mLevels;/**< Vector of pointers to all of the levels. \todo Make into map? */
        SDL_Surface *mScreen;/**< The surface to draw to. */
        map<string,Behavior*> mBehaviors;/**< This level's Behaviors */
index ffe14cf..d2fcd46 100644 (file)
--- a/Level.cpp
+++ b/Level.cpp
@@ -2,19 +2,20 @@
 #include "Game.h"
 #include "fns.h"
 #include <GL/gl.h>
+#include <algorithm>
 
-Level::Level(SDL_Surface* screen) : mScreen(screen)
+Level::Level()
 {
        mLoadBehavior = DoNothing;
        mUpdateBehavior = DoNothing;
 }
 
-Level::Level(SDL_Surface* screen, string n, Condition winCond, Behavior loadBehave, Behavior updateBehave, Behavior endBehave) : mScreen(screen), mName(n), mBackground(NULL), Loaded(false), mTextures(map<string, Texture>()), ToAdd(vector<WorldObject*>()), ToRemove(vector<WorldObject*>())
+Level::Level(string n, Condition winCond, Behavior loadBehave, Behavior updateBehave, Behavior endBehave) : mName(n), mBackground(NULL), Loaded(false), mTextures(map<string, Texture>()), ToAdd(vector<WorldObject*>()), ToRemove(vector<WorldObject*>())
 {
        /// Set behavior
        mLoadBehavior = loadBehave;
        mUpdateBehavior = updateBehave;
-       mEndBehavior = endBehave;
+       mUnloadBehavior = endBehave;
        mWinCondition = winCond;
 }
 
@@ -30,10 +31,9 @@ void Level::DrawIMG(SDL_Surface *img, int x, int y)
        SDL_Rect dest;
        dest.x = x;
        dest.y = y;
-       SDL_BlitSurface(img, NULL, mScreen, &dest);
+       SDL_BlitSurface(img, NULL, Game::game()->Screen(), &dest);
 }
 
-
 void Level::DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2)
 {
        SDL_Rect dest;
@@ -44,15 +44,13 @@ void Level::DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2
        src.y = y2;
        src.w = w;
        src.h = h;
-       SDL_BlitSurface(img, &src, mScreen, &dest);
+       SDL_BlitSurface(img, &src, Game::game()->Screen(), &dest);
 }
 
-
 void Level::drawScene()
 {
 
        glLoadIdentity();
-       Uint32 color;
 
        /** Create a black mBackgroundground using the mScreen pixel format (32 bpp) */
        glClear(GL_COLOR_BUFFER_BIT);
@@ -62,7 +60,7 @@ void Level::drawScene()
                mBackground->draw();
 
        /** Draw Sprites*/
-       DrawObjects();
+       DrawSprites();
 
        /** Draw Boundary Data */
        DrawCollisions();
@@ -71,10 +69,10 @@ void Level::drawScene()
        SDL_GL_SwapBuffers();
 }
 
-void Level::DrawObjects()
+void Level::DrawSprites()
 {
-       for (size_t i=0; i<mWorldObjects.size(); ++i) {
-               mWorldObjects[i]->draw();
+       for (size_t i=0,size=mSprites.size(); i<size; ++i) {
+               mSprites[i]->draw();
        }
 }
 
@@ -104,16 +102,124 @@ void Level::postEvent(SDL_Event event)
 
 /** Will handle all movement and dynamic triggers. */
 void Level::update(){
-       mUpdateBehavior();
+       sort(mWorldObjects.begin(),mWorldObjects.end());
+       if (Loaded)
+       {
+               if (!Game::game()->Paused)
+               {
+                       mUpdateBehavior();
+                       if (mWinCondition())
+                       {
+                               unload();
+                               return;
+                       }
+                       for(unsigned int i=0, size = mWorldObjects.size(); i<size; i++){
+                               if (!mWinCondition())
+                                       mWorldObjects[i]->update();
+                               else
+                               {
+                                       unload();
+                                       return;
+                               }
+                       }
+                       for(unsigned int i=0, size = ToRemove.size(); i<size; i++){
+                               {
+                                       // Try to remove from mWorldObjects,
+                                       // if failed see if it's currently pending add
+                                       bool removed=false;
+                                       for(vector<WorldObject* >::iterator ptr = mWorldObjects.begin(); ptr != mWorldObjects.end(); ptr++){
+                                               if(*ptr == ToRemove[i]){
+                                                       delete *ptr;
+                                                       mWorldObjects.erase(ptr);
+                                                       removed=true;
+                                                       break;
+                                               }
+                                       }
+                                       if(removed){
+                                               for(vector<Sprite* >::iterator ptr = mSprites.begin(); ptr != mSprites.end(); ptr++){
+                                                       if(*ptr == ToRemove[i]){
+                                                               mSprites.erase(ptr);
+                                                               removed=true;
+                                                               break;
+                                                       }
+                                               }
+                                       } else{
+                                               for(vector<WorldObject* >::iterator ptr = ToAdd.begin(); ptr != ToAdd.end(); ptr++){
+                                                       if(*ptr == ToRemove[i]){
+                                                               ToAdd.erase(ptr);
+                                                               break;
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+                       ToRemove.clear();
+                       for(unsigned int i=0, size = ToAdd.size(); i<size; i++){
+                               mWorldObjects.push_back(ToAdd[i]);
+                               if(Sprite* sp = dynamic_cast<Sprite*>(ToAdd[i]))
+                                       mSprites.push_back(sp);
+                       }
+                       ToAdd.clear();
+                       /// \todo do collisions
+                       /*Collision.Update();
+                       foreach (WorldObject sp in mWorldObjects)
+                       {
+                       sp.DoCollisions();
+                       }*/
+               }
+               else {
+                       /// \todo pause/unpause
+               }
+       }
+       else {
+               /// \todo loading screen
+       }
 }
 
 void Level::load(){
+       clearData();
        mLoadBehavior();
+       /// \todo Add menu
+       Loaded = true;
 }
 
+void Level::unload(){
+       mUnloadBehavior();
+       clearData();
+       Loaded = false;
+}
+
+void Level::clearData(){
+       /// \todo stop and delete sound
+
+       //clear WorldObjects
+       for(unsigned int i=0,size=mWorldObjects.size(); i<size; i++){
+               delete mWorldObjects[i];
+       }
+       mWorldObjects.clear();
+       mSprites.clear();
+
+       //clear textures
+       int count = mTextures.size();
+       vector<GLuint> texts = vector<GLuint>();
+       for(map<string, Texture>::iterator it = mTextures.begin(),end=mTextures.end();it!=end;it++){
+               glDeleteTextures(1, &it->second);
+       }
+       mTextures.clear();
+
+       //clear actors
+       mActors.clear();
+
+       //clear anims
+       mAnims.clear();
+
+       /// \todo clear collision data
+
+
+}
 
 void Level::addSprite(Sprite* sp){
-       mSprites.push_back(sp);
+       ToAdd.push_back(sp);
 }
 
 Sprite* Level::getSprite(string name){
@@ -126,12 +232,7 @@ Sprite* Level::getSprite(string name){
 }
 
 void Level::removeSprite(Sprite* sp){
-       for(vector<Sprite*>::iterator ptr = mSprites.begin(); ptr != mSprites.end(); ptr++){
-               if(*ptr == sp){
-                       mSprites.erase(ptr);
-                       return;
-               }
-       }
+       ToRemove.push_back(sp);
 }
 
 void Level::addAnimation(Animation anim){
diff --git a/Level.h b/Level.h
index 984c94f..27cde98 100644 (file)
--- a/Level.h
+++ b/Level.h
@@ -24,21 +24,21 @@ This class is tasked with the following:
 class Level
 {
 public:
-       Level(SDL_Surface* screen);
+       Level();
        /**
        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, Condition winCond, Behavior loadBehave = DoNothing, Behavior updateBehave = DoNothing, Behavior endBehave = DoNothing);
+       Level(string name, Condition winCond, Behavior loadBehave = DoNothing, Behavior updateBehave = DoNothing, Behavior endBehave = 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 */
        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 unload();/**< 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 */
@@ -50,14 +50,13 @@ public:
        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 */
-       bool Loaded;
+       bool Loaded;/**< Shows if the level has loaded or not (level changing) */
+       void Unload();/** What should be done when this level is left (not deleted) */
 
 protected://allow inheritance
        string mName;
        Background* mBackground;/**< Pointer to the current Background. */
-       SDL_Surface *mScreen;/**< Pointer to the screen. */
        vector<WorldObject*> mWorldObjects;/**< Vector of all objects on the level.  \todo make a list ordered by Zorder (for drawing)*/
        vector<Sprite*> mSprites;/**< Vector of all sprites on the level */
        map<string,Actor> mActors;/**< This level's actors. */
@@ -69,11 +68,12 @@ protected://allow inheritance
        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 DrawObjects();/**< Draws all Sprites. */
+       void DrawSprites();/**< Draws all Sprites. */
        void DrawCollisions();/**< Draws all Collision data */
        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 */
-       Behavior mEndBehavior;/**< will be used to define the end action of the level as it closes*/
+       Behavior mUnloadBehavior;/**< will be used to define the end action of the level as it closes*/
        Condition mWinCondition;/**< Conditions for the level to wait upon before exiting.*/
+       void clearData();/**< This will clear the data and make this level as if new */
 };
 #endif
index 13d0333..c700d02 100644 (file)
@@ -6,27 +6,27 @@
 ///\file this file should contain all Behaviors for Levels
 
 void LevelWorldLoad(){
-       Level* l = Game::game()->getCurrentLevel();
+       Level* l = Game::game()->LoadingLevel;
 
-       SDL_Surface* screen = l->getScreen();
+       SDL_Surface* screen = Game::game()->Screen();
 
        /// load background
-       l->setBackground(new Background(screen, "default", "bg.bmp"));//mBackground = LoadImage("Backgrounds/bg.bmp");
+       l->setBackground(new Background("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"));
+       Sprite* vikings1 = new Sprite("viking1", l->getAnimation("viking.anim"));
        vikings1->setPosition(0,0);
        vikings1->setSpeed(1);
 
-       Sprite* vikings2 = new Sprite(screen, "viking2", l->getAnimation("viking.anim"));
+       Sprite* vikings2 = new Sprite("viking2", l->getAnimation("viking.anim"));
        vikings2->setPosition(350,300);
        vikings2->setSpeed(1.5);
 
-       Sprite* sun = new Sprite(screen, "sun", l->getAnimation("sun.anim"));
+       Sprite* sun = new Sprite("sun", l->getAnimation("sun.anim"));
        sun->setPosition(480,50);
        sun->setSpeed(1);
 }
@@ -39,14 +39,18 @@ void LevelWorldUpdate(){
        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); }
+       if(player){
+               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(bg){
+               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
index 5fcd495..4b28646 100644 (file)
@@ -5,6 +5,6 @@
 ///\file this file should contain implementations for loading resources
 
 void Game::LoadResources(){
-       mLevels.push_back(new Level(mScreen, "World", NeverEnd, LevelWorldLoad, LevelWorldUpdate));
+       mLevels.push_back(new Level("World", NeverEnd, LevelWorldLoad, LevelWorldUpdate));
 }
 
index aad2f4e..8b76308 100644 (file)
@@ -5,15 +5,19 @@
 
 using std::cout;
 using std::endl;
-Sprite::Sprite(SDL_Surface *screen, std::string name, Actor actor) :
-mName(name),
+Sprite::Sprite( std::string name, Actor actor) :
+       WorldObject(name),
        mDrawn(0),
        mLastUpdate(0),
        mActor(actor),
-       mScreen(screen)
+       mBehavior(DoNothing)
 {
        //add to current level
-       Game::game()->getCurrentLevel()->addSprite((this));
+       Level* l = Game::game()->LoadingLevel;
+       if(l!=0)
+               l->addSprite((this));
+       else
+               Game::game()->getCurrentLevel()->addSprite(this);
 
        if(mActor.mAnimations[mActor.mCurrentAnimation] -> mBuilt)
        {
@@ -74,6 +78,10 @@ void Sprite::draw()
        }
 }
 
+void Sprite::update(){
+       mBehavior();
+}
+
 vector<Collision*>& Sprite::getCollisionData(){
        return mActor.mAnimations[mActor.mCurrentAnimation]->mFrames[mActor.mFrame].collisionData;
 }
index 0bb6ab4..37f43e6 100644 (file)
--- a/Sprite.h
+++ b/Sprite.h
 class Sprite : public WorldObject
 {
        public:
-       Sprite(SDL_Surface *screen, std::string name, Actor actor);
+       Sprite(std::string name, Actor actor);
        void setAnimation(int animation){ mActor.mCurrentAnimation = animation; }/**< changes to the specified animation beginning at 0. */
        SpriteFrame* getAnimation(){ return &mActor.mAnimations[mActor.mCurrentAnimation]->mFrames[mActor.mFrame]; }/**< returns active animation. (actually the current frame within the animation) \todo see if this is slower maybe undo */
        void setFrame(int frame) { mActor.mFrame = frame; }/**< cahnges to the specified frame of the animation beginning at 0. */
        int getFrame() { return mActor.mFrame; }/**< returns active frame. */
        void setSpeed(float speed) { mSpeed = speed; }/**< sets the Sprite's speed. */
        float getSpeed() { return mSpeed;}/**< returns a Sprite's current speed. */
-       std::string getName(){ return mName; }/**< returns the Sprite's name */
        void toggleAnim(){ mAnimating = !mAnimating; }/**< Pauses or resumes an animation. */
        void startAnim() {mAnimating = 1; }/**< Causes the animation to play. */
        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 drawCollisions();/**< Draws Collision data for the Object (from outside) */
+       void update();/**< Update to run each loop */
        Sprite* collisionWithSprite(string name);/**< checks for collision with sprite of a given name. */
        vector<Collision*>& getCollisionData();/**< Returns collision data */
        
        
 private:
-       std::string mName;/**< Sprite's name */
        bool mAnimating;/**< Tells whether to animate or not */
        bool mDrawn;/**< Tells if the object has been drawn the first time */
        float mSpeed;/**< Movement speed of the 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. */
        Behavior mBehavior;/**< Sprite's Behavior */
 };
 #endif
index 0238f09..6807189 100644 (file)
--- a/Text.cpp
+++ b/Text.cpp
@@ -1,17 +1,32 @@
 #include "Text.h"
 #include <iostream>
 #include <string>
-#include "Game.h"
+//#include "Game.h"
 #include "fns.h"
 #include <GL/gl.h>
 using namespace std;
 
+Text::Text(string name, string t, SDL_Color color) : WorldObject(name){
+       // Load a font
+       font = TTF_OpenFont("FreeSans.ttf", 24);
+
+       if(font){
+               //SDL_Color text_color = {r,g, b};
+               string txt="A journey of a thousand miles begins with a single step.";
+               SDL_Rect rect = {0,0,100,100};
+               draw();
+       }
+       else
+               cerr << "TTF_OpenFont() Failed: " << TTF_GetError() << endl;
+
+       TTF_CloseFont(font);
+}
+
 void Text::draw()
 {
        const char* txt = text.c_str();
        SDL_Surface *initial;
        SDL_Surface *intermediary;
-       SDL_Rect rect;
        SDL_Rect location = {mPos.x, mPos.y, width, height};
        int w,h;
        Texture texture;
@@ -43,9 +58,7 @@ void Text::draw()
 
        /* Draw a quad at location */
        glBegin(GL_QUADS);
-       /* Recall that the origin is in the lower-left corner
-       That is why the TexCoords specify different corners
-       than the Vertex coors seem to. */
+
        glTexCoord2f(0.0f, 0.0f); 
        glVertex2f(location.x , location.y);
        glTexCoord2f(1.0f, 0.0f); 
@@ -69,21 +82,5 @@ void Text::draw()
        glDeleteTextures(1, &texture);
 }
 
-Text::Text(string t, SDL_Color color){
-       // Load a font
-       font = TTF_OpenFont("FreeSans.ttf", 24);
-
-       if(font){
-               // Write text to surface
-               SDL_Surface *text;
-               //SDL_Color text_color = {r,g, b};
-               string txt="A journey of a thousand miles begins with a single step.";
-               SDL_Rect rect = {0,0,100,100};
-               draw();
-       }
-       else
-               cerr << "TTF_OpenFont() Failed: " << TTF_GetError() << endl;
-
-       TTF_CloseFont(font);
-}
-
+void Text::update(){
+}
\ No newline at end of file
diff --git a/Text.h b/Text.h
index 9ae9993..111a9f1 100644 (file)
--- a/Text.h
+++ b/Text.h
@@ -21,8 +21,9 @@ class Text : public WorldObject
        string text;
        SDL_Color color;
        void draw();
+       void update();
        int width;
        int height;
-       Text(string t, SDL_Color color);
+       Text(string name, string t, SDL_Color color);
 };
 #endif
index 27f0df0..b9dbe2a 100644 (file)
@@ -6,7 +6,9 @@
 class WorldObject
 {
 public:
-       WorldObject(int z = 0) :
+       WorldObject(std::string name,int z = 0, Point2D pos = Point2D()) :
+           mName(name),
+               mPos(pos),
                ZOrder(z),
                mVisible(true),
                mTransparency(1),
@@ -16,17 +18,24 @@ public:
        virtual void draw() = 0;/**< Draws the Object. */
        virtual void drawCollisions();/**< Draws Collision data for the Object */
        void drawCollisions(vector<Collision*>& vec, const Point2D& pos);/**< Draws Collision data for the Object */
+       virtual void update() = 0;/**< Update to run each loop */
        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. */
+       Point2D getPosition() { return mPos; }/**< Gets the Sprite's position */
        void setTransparency(float f){ mTransparency = f>1?1:f<0?0:f;}/**< Sets the Sprite's transparency. \param f The sprite's transparancy [0,1] other values will be force set */
        float getTransparency(){return mTransparency;}/**< Gets the Sprite's transparency. */
        void setAngle(float a){ mAngle = a; while(a>360)a-=360;}/**< Sets the Sprite's angle in degrees. \param a Angle in degrees */
        float getAngle(){return mAngle;}/**< Gets the Sprite's angle. */
+       void setZOrder(int i) { ZOrder=i; }
+       int     getZOrder() { return ZOrder; }
+       std::string getName(){ return mName; }/**< returns the Sprite's name */
+       bool operator<(WorldObject &rhs){ return ZOrder < rhs.getZOrder(); }
 
 protected:
+       std::string mName;/**< Sprite's name */
        Point2D mPos;/**< The current (x,y) position */
        bool mVisible;/**< Determine if Object should be visible */
        float mTransparency;/**< Transparency! */
diff --git a/fns.h b/fns.h
index 06a7794..c3c134b 100644 (file)
--- a/fns.h
+++ b/fns.h
@@ -4,6 +4,7 @@
 #include <string>
 /// \todo remove this this is just to make testing easy w/o gdb
 #include <iostream>
+#include <GL/gl.h>
 using namespace std;
 
 ///Behaviors use functions that take nothing and return nothing
@@ -15,7 +16,7 @@ typedef bool (*Condition) ();
 ///Condition that is always false
 static bool NeverEnd(){return false;}
 ///Texture name
-typedef unsigned int Texture;
+typedef GLuint Texture;
 ///deg/rad
 const float deg2rad = (float)(3.14159/180);
 
@@ -54,6 +55,11 @@ class Point2D
        Point2D operator- (const Point2D& pt) const { return Point2D(x - pt.x, y - pt.y); }
        Point2D operator+= (Point2D& pt){  return add(pt); }
        Point2D operator-= (Point2D& pt){ return sub(pt); }
+       friend ostream &operator<<(ostream &out, Point2D p)     //output
+       {
+               out<<"("<<p.x<<","<<p.y<<")";
+               return out;
+       }
 };
 
 /** Just for more logical names for sizes of objects*/