From: Ivan Hernandez Date: Fri, 17 Dec 2010 05:20:17 +0000 (-0600) Subject: added the LevelWorld class and fixed a few things X-Git-Url: http://git.mmlx.us/?a=commitdiff_plain;h=ed1904a75c1d77430c4e66d8285972ff6d0ff21f;p=IvanGame.git added the LevelWorld class and fixed a few things --- diff --git a/ArtAssets/image.bmp b/ArtAssets/image.bmp deleted file mode 100644 index aaa70a3..0000000 Binary files a/ArtAssets/image.bmp and /dev/null differ diff --git a/Background.cpp b/Background.cpp index 7af7dd5..1a33a68 100644 --- a/Background.cpp +++ b/Background.cpp @@ -12,73 +12,130 @@ Background::Background(SDL_Surface *screen, std::string name, std::string filena source.x = bgSamplePos.X; source.y = bgSamplePos.Y; - MaxTexWidth = 512; - MaxTexHeight = 512; - load(filename); - // Number of sections each dimension, rounded up - _sections.width = (_imageSize.width - 1) / MaxTexWidth + 1; - _sections.height = (_imageSize.height - 1) / MaxTexHeight + 1; cout<<"background \""<<_name<<"\" created"< mScreenSize.width) // move it to the left til it's on the screen - x -= mScreenSize.width; - while (y > mScreenSize.height) // move it up til it's on the screen - y -= mScreenSize.height; + double x = mPos.X; + double y = mPos.Y; + const double X = mScreenSize.width; + const double Y = mScreenSize.height; + const double W = _imageSize.width; + const double H = _imageSize.height; + /* + while (x > X) // move it to the left til it's on the screen + x -= X; + while (y > Y) // move it up til it's on the screen + y -= Y; while (x < 0) // move it to the right until it's in the drawable area - x += mScreenSize.width; + x += X; while (y < 0) // move it down til it's on the screen - y += mScreenSize.height; - - if (wrapping) { + y += Y; + //*/ + /*if (wrapping) { SDL_Rect source; + if (x>=0 && y>=0){ + // Top-left portion of image + dest.x = x; + dest.y = y; + source.x = 0; + source.y = 0; + source.w = X - x; + source.h = Y - y; + SDL_BlitSurface(mBackground, &source, mScreen, &dest); + + // Lower-left portion of image + dest.x = x; + dest.y = 0; + source.x = 0; + source.y = H - y; + source.w = X - x; + source.h = y; + SDL_BlitSurface(mBackground, &source, mScreen, &dest); - // Top-left portion of image - dest.x = x; - dest.y = y; - source.x = 0; - source.y = 0; - source.w = mScreenSize.width - x; - source.h = mScreenSize.height - y; - SDL_BlitSurface(mBackground, &source, mScreen, &dest); + // Lower-right portion of image + dest.x = 0; + dest.y = 0; + source.x = W - x;///< \todo this needs to look right. it's grabbing the wrong part of the image but when it's getting the right part it freaks out and flashes when the bg moves off to the negative. need a check to make sure there is not more background to go before it just draws a section. + source.y = H - y; + source.w = x; + source.h = y; + SDL_BlitSurface(mBackground, &source, mScreen, &dest); - // Top-right portion of image - dest.x = 0; - dest.y = y; - source.x = mScreenSize.width - x; - source.y = 0; - source.w = x; - source.h = mScreenSize.height - y; - SDL_BlitSurface(mBackground, &source, mScreen, &dest); + // Top-right portion of image + dest.x = 0; + dest.y = y; + source.x = W - x; + source.y = 0; + source.w = x; + source.h = Y - y; + SDL_BlitSurface(mBackground, &source, mScreen, &dest); + } + if (x<0 || y<0){ + x = -x; + y = -y; + // Top-left portion of image + dest.x = 0; + dest.y = 0; + source.x = x; + source.y = y; + source.w = X - x; + source.h = Y - y; + SDL_BlitSurface(mBackground, &source, mScreen, &dest); + + /// Lower-left portion of image \todo this be's broken + dest.x = X - x; + dest.y = 0; + source.x = 0; + source.y = H - y; + source.w = x; + source.h = Y - y; + SDL_BlitSurface(mBackground, &source, mScreen, &dest); - // Lower-left portion of image - dest.x = 0; - dest.y = 0; - source.x = mScreenSize.width - x; - source.y = mScreenSize.height - y; - source.w = x; - source.h = y; - SDL_BlitSurface(mBackground, &source, mScreen, &dest); + // Lower-right portion of image + dest.x = X - x; + dest.y = Y - y; + source.x = 0; + source.y = 0; + source.w = x; + source.h = y; + SDL_BlitSurface(mBackground, &source, mScreen, &dest); - // Lower-right portion of image - dest.x = x; - dest.y = 0; - source.x = 0; - source.y = mScreenSize.height - y; - source.w = mScreenSize.width - x; - source.h = y; - SDL_BlitSurface(mBackground, &source, mScreen, &dest); - } else { + // Top-right portion of image + dest.x = 0; + dest.y = Y - y; + source.x = x; + source.y = 0; + source.w = X - x; + source.h = y; + SDL_BlitSurface(mBackground, &source, mScreen, &dest); + } + } + else */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 = x; + while(y > 0) + y -= H; + for(y; y < Y; y += H) + for(x = initx; x < X; x += W){ + dest.x = x; + dest.y = y; + SDL_BlitSurface(mBackground, NULL, mScreen, &dest); + } + } + else { dest.x = mPos.X; dest.y = mPos.Y; - // SDL_BlitSurface(mBackground, &source, mScreen, &dest); + SDL_BlitSurface(mBackground, &source, mScreen, &dest); } } @@ -88,7 +145,6 @@ SDL_Surface* Background::load(std::string filename){ cout<<"Setting Size..."; _imageSize=SizeD(mBackground->w,mBackground->h);/// #include @@ -15,9 +16,6 @@ class Background{ private: std::string _name;/// #include +#include "LoadLevels.h" const Uint32 Game::waitTime = 1000/60; /* ms */ @@ -25,8 +26,13 @@ Game::Game() : mCurrentLevel(0), mScreen(0) SDL_WM_SetCaption("Ivan's Game", NULL); SDL_ShowCursor(0); - /** Create the first level */ - mLevels.push_back(new Level(mScreen)); + /** 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; + } Game::~Game() @@ -34,6 +40,7 @@ Game::~Game() for (size_t i = 0; i < mLevels.size(); ++i) { delete mLevels[i]; } + delete mScreen; } Level* Game::getCurrentLevel() @@ -105,6 +112,14 @@ void Game::run() SDL_RemoveTimer(timer); } +void Game::loadLevel(std::string levelName){ + for(int i=0; i< mLevels.size(); ++i) + if(mLevels[i]->getName() == levelName) + mCurrentLevel = i; + else + std::cout<<"Level "< #include @@ -42,9 +41,7 @@ private: std::vector 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);/**< 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! */ + void loadLevel(std::string levelName);/**< Loads a level by name. */ static Uint32 timerCallback(Uint32 interval, void* data); }; diff --git a/Level.cpp b/Level.cpp index dc2152c..00cf727 100644 --- a/Level.cpp +++ b/Level.cpp @@ -1,33 +1,17 @@ #include "Level.h" #include "fns.h" -Level::Level(SDL_Surface* screen) : - //mBackground(), - mScreen(screen), - mVikingAnimation("viking.anim"), - mSunAnimation("sun.anim") +Level::Level(SDL_Surface* screen) : mScreen(screen) { /** load background */ mBackground= new Background(screen);//mBackground = LoadImage("Backgrounds/bg.bmp"); - - /** load sprites */ - Sprite* vikings1 = new Sprite(screen, "viking1", &mVikingAnimation); - vikings1->setPosition(10,30); - vikings1->setSpeed(1); - - Sprite* vikings2 = new Sprite(screen, "viking2", &mVikingAnimation); - vikings2->setPosition(350,300); - vikings2->setSpeed(1.5); - - Sprite* sun = new Sprite(screen, "sun", &mSunAnimation); - sun->setPosition(480,50); - sun->setSpeed(1); - - mSprites.push_back(vikings1); - mSprites.push_back(vikings2); - mSprites.push_back(sun); } +Level::Level(SDL_Surface* screen,string n) : mScreen(screen), mName(n) +{ + /** load background */ + mBackground= new Background(screen, n);//mBackground = LoadImage("Backgrounds/bg.bmp"); +} Level::~Level() { for (size_t i = 0; i < mSprites.size(); ++i) { @@ -58,13 +42,6 @@ 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); - mBackground->draw(); -} - - void Level::drawScene() { Uint32 color; @@ -74,8 +51,9 @@ void Level::drawScene() SDL_FillRect(mScreen, NULL, color); /** Draw BG */ - DrawBG(); + mBackground->draw(); + /** Draw Sprites*/ DrawSprites(); /** Flip the working image buffer with the mScreen buffer */ @@ -104,16 +82,27 @@ void Level::postEvent(SDL_Event event) } /** Will handle all movement and dynamic triggers. */ -void Level::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); } +void Level::update(){ + +} + + +void Level::addSprite(Sprite* sp){ + mSprites.push_back(sp); +} + +void Level::removeSprite(Sprite* sp){ + for(int i=0; i +#include #include using std::vector; @@ -23,23 +24,29 @@ class Level { public: Level(SDL_Surface *screen); + Level(SDL_Surface *screen, string name); ~Level(); - void drawScene();/**< Draws everything that is set to draw on the screen. */ + virtual 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. */ + virtual void update();/**< Loop that runs every game frame that calculates movement, placement, etc. \todo make this be abstract so that levels are based off this class.*/ + void addSprite(Sprite* sp);///< add a Sprite to the list of sprites + void removeSprite(Sprite* sp);///< remove the Sprite sp from the list of sprites + void addActor(string name, Actor actor);///< add a Actor to the list of sprites + void removeActor(string name);///< remove the Actor sp from the list of sprites + string getName(){ return mName; }///< returns the current level's name -private: - Background* mBackground;/**< Pointer to the current Background. \todo change this to a Background\current background also should be a vector or list of backgroundsfor the current level.*/ +protected://allow inheritance + string mName; + Background* mBackground;/**< Pointer to the current Background. */ SDL_Surface *mScreen;/**< Pointer to the screen. */ - vector mSprites;/**< Vector of all sprites on the level. \todo Maybe make into map? */ - void DrawBG();/**< Draws the Background. \todo make this draw the current level*/ - void DrawIMG();/**< Draws an image to the screen. (Not implemented) */ + vector mSprites;/**< Vector of all sprites on the level. \todo Maybe make into map? This should be a list of World objects once implemented.*/ + 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. */ - Animation mVikingAnimation,/**< Viking animation. \todo make a storage location for Animations. */ - mSunAnimation;/**< Sun animation. */ + map mActors;/**< This level's actors. */ + vector mAnim;/**< This level's Animations */ }; #endif diff --git a/LevelWorld.cpp b/LevelWorld.cpp new file mode 100644 index 0000000..34f77ae --- /dev/null +++ b/LevelWorld.cpp @@ -0,0 +1,62 @@ +#include "LevelWorld.h" +#include "fns.h" + +/// \todo set up a level to be poplulated from something. +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); +} + +void LevelWorld::drawScene() +{ + Uint32 color; + + /** 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 */ + mBackground->draw(); + + /** Draw Sprites*/ + DrawSprites(); + + /** Flip the working image buffer with the mScreen buffer */ + SDL_Flip (mScreen); +} + +/** 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); } + +} + diff --git a/LevelWorld.h b/LevelWorld.h new file mode 100644 index 0000000..436d4c8 --- /dev/null +++ b/LevelWorld.h @@ -0,0 +1,24 @@ +#ifndef LEVELWORLD +#define LEVELWORLD +#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.*/ + +}; +#endif diff --git a/Sprite.cpp b/Sprite.cpp index 40cc00e..03917ab 100644 --- a/Sprite.cpp +++ b/Sprite.cpp @@ -2,11 +2,11 @@ #include using std::cout; using std::endl; -Sprite::Sprite(SDL_Surface *screen, std::string name, Animation *anim) : +Sprite::Sprite(SDL_Surface *screen, std::string name, Actor actor) : mDrawn(0), mVisible(true), mLastUpdate(0), - mActor(anim), + mActor(actor), mScreen(screen) /** \todo implement name and mName seee if I even need both of them. */ { @@ -34,4 +34,3 @@ void Sprite::draw() if(mVisible == true) SDL_BlitSurface(mActor.mAnimations[mActor.mCurrentAnimation]->mAnim[mActor.mFrame].image,NULL,mScreen,&dest); } - diff --git a/Sprite.h b/Sprite.h index 3529973..9d22e36 100644 --- a/Sprite.h +++ b/Sprite.h @@ -6,19 +6,16 @@ #include "Actor.h" /** - Characters and other mobile objects displayed on screen. + Characters and other mobile objects displayed on screen with animations. This class is tasked with the following: - loading and controlling an Animation - - movement of objects - - check collision with other objects */ class Sprite { public: - Sprite(SDL_Surface *screen, std::string name, Animation *anim); - void draw();/**< draws Sprite. */ + Sprite(SDL_Surface *screen, std::string name, Actor actor); void setAnimation(int animation){ mActor.mCurrentAnimation = animation; }/**< changes to the specified animation beginning at 0. */ int getAnimation(){ return mActor.mCurrentAnimation; }/**< returns active animation. */ void setFrame(int frame) { mActor.mFrame = frame; }/**< cahnges to the specified frame of the animation beginning at 0. */ @@ -29,23 +26,25 @@ class Sprite 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. */ + std::string name; + void draw();/**< draws Sprite. */ 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. */ - std::string name; + private: std::string mName;/**< Sprite's name */ - Point2D mPos;/**< Sprite's position */ bool mAnimating;/**< Tells whether to animate or not */ bool mDrawn;/**< Tells if the object has been drawn the first time */ - bool mVisible;/**< Determine if Sprite should be visible */ 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. */ + Point2D mPos;/**< Object's position */ + bool mVisible;/**< Determine if Object should be visible */ int ZOrder;/**< Stacking order. Determines what draws on top. \todo implement. */ }; #endif diff --git a/fns.h b/fns.h index 5586a4b..2047c42 100644 --- a/fns.h +++ b/fns.h @@ -3,6 +3,10 @@ #include #include +/// \todo remove this this is just to make testing easy w/o gdb +#include +using namespace std; + SDL_Surface* LoadImage( std::string filename );/**< Loads supported images. */ //class SizeD; diff --git a/main.cpp b/main.cpp index 5384178..8524052 100644 --- a/main.cpp +++ b/main.cpp @@ -92,33 +92,6 @@ int main (int argc, char *argv[]) Game g; g.run(); -#if 0 - //load sprites - //Animation vikingAnimation = new Animation("viking.anim"); - vikingAnimation.loadAnimation("viking.anim"); - //Animation sunAnimation = new Animation(); - sunAnimation.loadAnimation("sun.anim"); - - Sprite vikings1(screen, "viking1", &vikingAnimation); - vikings1.setPosition(10,30); - vikings1.setSpeed(1); - - Sprite vikings2(screen, "viking2", &vikingAnimation); - vikings2.setPosition(350,300); - vikings2.setSpeed(1.5); - - Sprite sun(screen, "sun", &sunAnimation); - sun.setPosition(480,50); - sun.setSpeed(1); - - SpriteList.push_back(vikings1); - SpriteList.push_back(vikings2); - SpriteList.push_back(sun); - - //Hide Cursor - SDL_ShowCursor(0); -#endif - return 0; }