]> git.mmlx.us Git - IvanGame.git/commitdiff
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 6a32c0499fd5963b25b3a595d62c84738bffcdaf..54c636e66718433619be4a330ecfbf7cf75383e2 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 45ef5e85bfd656b1a1a98d192bd0f0b5f0d76ca5..26f4708c122cfb9d99b5633bc997503af6ad40dd 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 a7a878d1ace79b40a34677bac6fc51506811d807..1b65d572bd581bbf9e6c8311dd5591943918e299 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 57b3e8149900879ec176265abc7ed4c9fa86aaa8..63a597a232575408f4ef4adf009abee05a7f1a91 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 3b725942c8b319bb2a2dbfc85a930afd904263ee..f60c9885e45657db98bfe9f30ba66b63fb8a7f9b 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 05f546797f5ba0f9ad252234f6b5f603d346c027..f1eeb12fac406ed691e03e5836e6a701743c1a89 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 4b8af4e23a1ac28eb80c117fb323631999698cba..df3cd07312d704af1b0650c39a55c590fcfe455f 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 5d281d9d320134d62402d18f46407e3e8058637b..44968811a34939560a2abc79518219eb4630a021 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 e8185a0da57d7878afda6d8f3c590699da1da669..983460d7d3c5e42dea8ac84579bd1998bbe815a9 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 22f223b03decd00d4e3a4a0907ebb41daafb2744..27d3546f444b3f408126ef522ec0587b2aa994af 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 67a9bbf4043eb149b728d711c36f45efed68a8ca..f10d7945dd8d739767df755e3497dfba47e69956 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