added new files and made things work again
authorIvan Hernandez <iturtleman128@gmail.com>
Tue, 31 Aug 2010 20:13:18 +0000 (15:13 -0500)
committerIvan Hernandez <iturtleman128@gmail.com>
Tue, 31 Aug 2010 20:13:18 +0000 (15:13 -0500)
Actor.cpp
Actor.h
Animation.cpp
Animation.h
Background.cpp
Background.h
CMakeLists.txt
Game.cpp
Game.h
Level.cpp
Level.h

index 6a32c04..54c636e 100644 (file)
--- a/Actor.cpp
+++ b/Actor.cpp
@@ -1,26 +1,9 @@
+#include "Actor.h"
 //the beggining
-{
+Actor::Actor(Animation *anim):
        mFrame(0),
-       mCurrentAnimation(0),
-       mSpriteAnimations.push_back(anim);
-       if(mSpriteAnimations[mCurrentAnimation] -> mBuilt)
-       {
-               if (mSpriteAnimations[mCurrentAnimation]->mNumFrames > 1) mAnimating = 1;
-       }
-}
-Actor::Draw()
+       mCurrentAnimation(0)
 {
-       if(mAnimating == 1) {
-               if(mLastUpdate+mSpriteAnimations[mCurrentAnimation]->mAnim[mFrame].pause*mSpeed<SDL_GetTicks()) {
-                       mFrame++;
-                       if(mFrame > mSpriteAnimations[mCurrentAnimation]->mNumFrames-1) mFrame=0;
-                       mLastUpdate = SDL_GetTicks();
-               }
-       }
-       SDL_Rect dest;
-       dest.x = mX;
-       dest.y = mY;
-       if(mVisible == true)
-               SDL_BlitSurface(mSpriteAnimations[mCurrentAnimation]->mAnim[mFrame].image,NULL,mScreen,&dest);
+       mAnimations.push_back(anim);
 }
 
diff --git a/Actor.h b/Actor.h
index 45ef5e8..26f4708 100644 (file)
--- a/Actor.h
+++ b/Actor.h
@@ -1,18 +1,28 @@
-#ifndef SPRITE
-#define SPRITE
+#ifndef ACTOR
+#define ACTOR
 #include "Animation.h"
 #include <SDL/SDL.h>
 #include <vector>
 
-class Sprite
+/**
+  Set of Animations
+
+  This class is tasked with the following:
+    - keeping track of the current Animation's state
+ */
+
+
+class Actor
 {
        public:
-       void draw();
-       std::vector<Animation*> mSpriteAnimations;
-       bool mAnimating;        
-       int mCurrentAnimation;
-       bool mDrawn;
-       bool mVisible;
-       int mAnimation;
-}
+       Actor(Animation *anim);
+       int mFrame;/**< The frame # */
+       int mCurrentAnimation;/**< Index to the current loaded animation */
+       int mAnimation;/**< \todo See if this is used anywhere. Else Del */
+       std::vector<Animation*> mAnimations;/**< Vector of pointers to all of this sprite's Animations */
+
+       private:
+       
 
+};
+#endif
index a7a878d..1b65d57 100644 (file)
@@ -60,12 +60,12 @@ int Animation::loadAnimation(std::string animFile)
                                SDL_Surface *temp=NULL;
                                if((temp = LoadImage("Sprites/"+file)) == NULL) return -1;
                        
-                               //set transparent color
+                               /** Set transparent color */
                                SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, transparency[0], transparency[1], transparency[2]));
-                               //loads image into animation
+                               /** Loads image into animation */
                                mAnim[count].image = SDL_DisplayFormat(temp);
                            SDL_FreeSurface(temp);
-                               //sets frame delay and makes sure height and width are correct
+                               /** sets frame delay and makes sure height and width are correct */
                                mAnim[count].pause = pause;
                        if(!mW) mW = mAnim[count].image->w; 
                        if(!mH) mH = mAnim[count].image->h;
index 57b3e81..63a597a 100644 (file)
@@ -5,17 +5,28 @@
 #include <SDL/SDL_image.h>
 
 struct SpriteFrame{
-       SDL_Surface *image;
-       int pause;
+       SDL_Surface *image;/**< Pointer to an image. */
+       int pause;;/**< Tells the amount of time to pause between this frame and the next. */
 };
 
+/**
+  An Animation
+
+  This class is tasked with the following:
+    - loading sets of images into an animation set.
+*/
+
+
 class Animation
 {
        public:
        Animation(std::string animFile);
-       int loadAnimation(std::string animFile);
-       SpriteFrame *mAnim;
-       int mBuilt, mNumFrames, mW, mH;
+       int loadAnimation(std::string animFile);/**< Loads the Animations from a file in the specified format. */
+       SpriteFrame *mAnim;/**< Pointer to the current animation. */
+       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
index 3b72594..f60c988 100644 (file)
@@ -1,8 +1,36 @@
 #include "Background.h"
+#include <iostream>
+using namespace std;
 
-Background::Background(std::string name, std::string filename):_name(name){
-       /** \todo Implememnt the constructor that gets image sizes and puts it together so that background can be used correctly. */
+Background::Background(SDL_Surface *screen, std::string name, std::string filename, Point2D ScreenPosition, SizeD ScreenSize, Point2D bgSamplePos, SizeD bgSize):mScreen(screen),_name(name),mPos(ScreenPosition),mScreenSize(ScreenSize){
+
+       source.w = bgSize.width;
+       source.h = bgSize.height;
+       source.x = bgSamplePos.X;
+       source.y = bgSamplePos.Y;
+
+       load(filename);
+
+       cout<<"background "<<_name<<" created"<<endl;   
 }
 void Background::draw(){
-       /** \todo implement draw for background scrolling and otherwise. */
+       /** \todo we need to make this do wrapping if wanted/decide if it should be a good default.*/
+       ///destination rectangle only position is used and describes where the output is drawn.
+       SDL_Rect dest;
+    dest.x = mPos.X;
+    dest.y = mPos.Y;
+
+    SDL_BlitSurface(mBackground, &source, mScreen, &dest);
 }
+
+SDL_Surface* Background::load(std::string filename){
+       mBackground = LoadImage("Backgrounds/"+filename);
+       cout<<"Loaded file"<<endl;
+       cout<<"Setting Size...";
+       _imageSize=SizeD(mBackground->w,mBackground->h);///<set the size of the background.
+       cout<<"done!"<<endl;
+       /// \todo set values of size etc.
+       return mBackground;
+}
+
+
index 05f5467..f1eeb12 100644 (file)
@@ -1,4 +1,4 @@
-#include "Game.h"// for size of game window
+// for size of game window
 #include "fns.h"
 #include <string>
 #include <iostream>
 
 class Background{
        private:
-       std::string _name;
-       SizeD _imageSize;
+       std::string _name;///<The background's name
+       SizeD _imageSize;///<The actual background image's full size.
+       SDL_Surface *mBackground;///<pointer to the actual screen 
        /** \todo _sections;//if needed */
-       SDL_Surface *mScreen;
+       SDL_Surface *mScreen;//<\todo see about a way to know about the screen without passing around the value (though this is probably most cost effective processor-wise)
+       Point2D mPos;///<The current (x,y) position
+       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.
+       
 
+       
        public:
-       Background(std::string name, std::string filename);
+       Background(SDL_Surface *screen,std::string name="default", std::string filename="bg.bmp",Point2D ScreenPosition=Point2D(0,0), SizeD screenSize=SizeD(800,600)/** \todo figure out how to get the game window's size to the background.*/,Point2D bgSamplePos=Point2D(0,0), SizeD bgSize=SizeD(800,600));
        void draw();/**< Draw background. */
        std::string getName(){ return _name; }/** Returns name. */
        void setName(std::string name){ _name = name; }/** Set Name. */
        SizeD size(){ return _imageSize;}/** returns image size */
-       
+       SDL_Surface* load(std::string name="default");///< Loads a Bacground by name.
+       SizeD getPosition(){return mPos;}
 };
 
index 4b8af4e..df3cd07 100644 (file)
@@ -4,6 +4,6 @@ project(game)
 find_package(SDL REQUIRED)
 find_package(SDL_image REQUIRED)
 
-add_executable(game Animation.cpp fns.cpp Game.cpp Level.cpp main.cpp Sprite.cpp Text.cpp)
+add_executable(game Animation.cpp fns.cpp Game.cpp Level.cpp main.cpp Sprite.cpp Text.cpp Actor.cpp Background.cpp)
 
 target_link_libraries(game ${SDL_LIBRARY} ${SDLIMAGE_LIBRARY})
index 5d281d9..4496881 100644 (file)
--- a/Game.cpp
+++ b/Game.cpp
@@ -7,25 +7,25 @@ const Uint32 Game::waitTime = 1000/60; /* ms */
 
 Game::Game() : mCurrentLevel(0), mScreen(0)
 {
-       // Initialize SDL 
+       /** Initialize SDL */
        if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
                printf ("Couldn't initialize SDL: %s\n", SDL_GetError ());
                exit (1);
        }
        atexit (SDL_Quit);
 
-       // Set 800x600 video mode
+       /** Set 800x600 video mode */
        mScreen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
        if (!mScreen) {
                printf ("Couldn't set video mode: %s\n", SDL_GetError ());
                exit (2);
        }
 
-       // Set the title of our application window handler
+       /** Set the title of our application window handler */
        SDL_WM_SetCaption("Ivan's Game", NULL);
 
        SDL_ShowCursor(0);
-       // Create the first level
+       /** Create the first level */
        mLevels.push_back(new Level(mScreen));
 }
 
@@ -60,19 +60,22 @@ void Game::run()
                return;
        }
 
-       // Game Loop
+       /** Game Loop */
        while (!done) {
-               // This will let us track events
+               /** This will let us track events */
                SDL_Event event;
 
-               // Wait for an event to occur
+               /** Wait for an event to occur */
                if (0 == SDL_WaitEvent(&event)) {
+                       std::cout<<"waiting"<<std::endl;
                        break;
                }
 
+               ///\todo This needs to be changed to where every single key-press is registered at once and put into a list of sorts and then events on keys is handled elsewhere to allow multi-keypresses and for better program flow.
                switch (event.type) {
-               // If the event is a click on the close button in the top
-               // right corner of the window, we kill the application
+               /** If the event is a click on the close button in the top
+                       right corner of the window, we kill the application
+               */
                case SDL_QUIT:
                        done = 1;
                        break;
@@ -81,10 +84,12 @@ void Game::run()
                        getCurrentLevel()->drawScene();
                        break;
 
-               // If our event reports a key being pressed down
-               // we process it
+               /** If our event reports a key being pressed down
+                       we process it
+                */
                case SDL_KEYDOWN: {
                        Uint8 *keys = SDL_GetKeyState(NULL);
+                       std::cout<<*keys<<std::endl;
                        if (keys[SDLK_ESCAPE]) {
                                done = 1;
                                break;
diff --git a/Game.h b/Game.h
index e8185a0..983460d 100644 (file)
--- a/Game.h
+++ b/Game.h
@@ -32,18 +32,19 @@ public:
         */
        int getLevelIndex();
 
+       /** Makes the game loop begin and load objects. */
        void run();
        
 private:
        static const Uint32 waitTime;
 
-       int mCurrentLevel; /**< current index */
-       std::vector<Level*> mLevels;
-       SDL_Surface *mScreen;
+       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. */
 
-       void addLevel(Level level);
-       void removeLevel(std::string levelName);
-       void loadLevel(std::string levelName);
+       void addLevel(Level level);/**< Adds a level to the vector. \todo Implement! */
+       void removeLevel(std::string levelName);/**< Removes the level with given name from the vector. \todo Implement! */
+       void loadLevel(std::string levelName);/**< Loads a level by name. \todo Implement! */
 
        static Uint32 timerCallback(Uint32 interval, void* data);
 };
index 22f223b..27d3546 100644 (file)
--- a/Level.cpp
+++ b/Level.cpp
@@ -2,15 +2,15 @@
 #include "fns.h"
 
 Level::Level(SDL_Surface* screen) :
-       mBackground(0),
+       //mBackground(),
        mScreen(screen),
        mVikingAnimation("viking.anim"),
        mSunAnimation("sun.anim")
 {
-       // load background
-       mBackground = LoadImage("Backgrounds/bg.bmp");
+       /** load background */
+       mBackground= new Background(screen);//mBackground = LoadImage("Backgrounds/bg.bmp");
 
-       // load sprites
+       /** load sprites */
        Sprite* vikings1 = new Sprite(screen, "viking1", &mVikingAnimation);
        vikings1->setPosition(10,30);
        vikings1->setSpeed(1);
@@ -60,7 +60,8 @@ void Level::DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2
 
 void Level::DrawBG()
 {
-  DrawIMG(mBackground, 0, 0);
+  //DrawIMG(mBackground, 0, 0);
+  mBackground->draw();
 }
 
 
@@ -68,16 +69,16 @@ void Level::drawScene()
 {
        Uint32 color;
 
-    // Create a black mBackgroundground using the mScreen pixel format (32 bpp)
+    /** Create a black mBackgroundground using the mScreen pixel format (32 bpp) */
     color = SDL_MapRGB(mScreen->format, 0, 0, 0);
     SDL_FillRect(mScreen, NULL, color);
        
-       //Draw BG
+       /** Draw BG */
        DrawBG();
        
        DrawSprites();
     
-    // Flip the working image buffer with the mScreen buffer
+    /** Flip the working image buffer with the mScreen buffer */
     SDL_Flip (mScreen);
 }
 
@@ -90,7 +91,7 @@ void Level::DrawSprites()
 
 void Level::LoadBG(std::string name)
 {
-       mBackground=LoadImage("Background"+name);
+       mBackground->load(name);
 }
 
 void Level::postEvent(SDL_Event event)
@@ -102,6 +103,7 @@ void Level::postEvent(SDL_Event event)
        }
 }
 
+/** Will handle all movement and dynamic triggers. */
 void Level::update()
 {
        Uint8 *keys = SDL_GetKeyState(NULL);
diff --git a/Level.h b/Level.h
index 67a9bbf..f10d794 100644 (file)
--- a/Level.h
+++ b/Level.h
@@ -1,30 +1,45 @@
 #ifndef LEVEL
 #define LEVEL
 #include "Sprite.h"
+#include "Background.h"
 #include <vector>
 #include <string>
+
 using std::vector;
 using std::string;
+
+/**
+  This is a stand alone Level that will have varying jobs
+
+  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
+ */
+
 class Level
 {
 public:
        Level(SDL_Surface *screen);
        ~Level();
-       void drawScene();
-       void LoadBG(string name);
-       virtual void postEvent(SDL_Event event);
-       void update();
+       void drawScene();/**< Draws everything that is set to draw on the screen. */
+       void LoadBG(string name);/**< Loads the Background. */
+       virtual void postEvent(SDL_Event event);/**< Passes along SDL events */
+       void update();/**< Loop that runs every game frame that calculates movement, placement, etc. */
 
 private:
-       SDL_Surface *mBackground;
-       SDL_Surface *mScreen;
-       vector<Sprite*> mSprites;
-       void DrawBG();
-       void DrawIMG();
-       void DrawIMG(SDL_Surface *img, int x, int y);
-       void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2);
-       void DrawSprites();
+       Background* mBackground;//SDL_Surface *mBackground;/**< Pointer to the current Background. */ //<\todochange this to a Background*;
+       SDL_Surface *mScreen;/**< Pointer to the screen. */
+       vector<Sprite*> mSprites;/**< Vector of all sprites on the level. \todo Maybe make into map? */
+       void DrawBG();/**< Draws the Background. \todo Why is this in existance? */
+       void DrawIMG();/**< Draws an image to the screen. (Not implemented) */
+       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. */
 
-       Animation mVikingAnimation, mSunAnimation;
+       Animation mVikingAnimation,/**< Viking animation. \todo make a storage location for Animations. */
+        mSunAnimation;/**< Sun animation. */
 };
 #endif