From: Matt Mullins Date: Sun, 12 Jul 2009 19:50:26 +0000 (-0500) Subject: Moved a lot of SDL from main.cpp to Game X-Git-Url: http://git.mmlx.us/?a=commitdiff_plain;h=56011e9456a4026c9259a4691ed326096a1c4e82;p=IvanGame.git Moved a lot of SDL from main.cpp to Game --- diff --git a/Game.cpp b/Game.cpp index 5552bfa..a93b60f 100644 --- a/Game.cpp +++ b/Game.cpp @@ -4,6 +4,28 @@ Game::Game() : mCurrentLevel(0), mScreen(0) { + // Initialize SDL + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + printf ("Couldn't initialize SDL: %s\n", SDL_GetError ()); + exit (1); + } + atexit (SDL_Quit); + + // Set 800x600 video mode + mScreen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE | SDL_DOUBLEBUF); + if (!mScreen) { + printf ("Couldn't set 800x600 32b video mode: %s\n", SDL_GetError ()); + exit (2); + } + + // Set the title of our application window handler + SDL_WM_SetCaption("Ivan's Game", NULL); + + // We activate keyboard repetition. Therefore, when a key stays pressed down + // it will keep moving the image around the screen + // To see prcisely what this toggle does, just comment the line and recompile + // the code... + SDL_EnableKeyRepeat(25, 20); } Game::Game(SDL_Surface* screen) : mCurrentLevel(0), mScreen(screen) @@ -14,14 +36,53 @@ Game::~Game() { } -Level* Game::getCurrentLevel() { - if (mCurrentLevel < mLevels.size()) { +Level* Game::getCurrentLevel() +{ + if (mCurrentLevel < (ssize_t)mLevels.size()) { return mLevels[mCurrentLevel]; } else { return NULL; } } -int Game::getLevelIndex() { +int Game::getLevelIndex() +{ return mCurrentLevel; } + +void Game::run() +{ + bool done = false; + + // Game Loop + while (!done) { + // This will let us track events + SDL_Event event; + + // We fill 'event' with the first event in the event queue + while (SDL_PollEvent(&event)) { + 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 + case SDL_QUIT: + done = 1; + break; + + // If our event reports a key being pressed down + // we process it + case SDL_KEYDOWN: { + Uint8 *keys = SDL_GetKeyState(NULL); + if (keys[SDLK_ESCAPE]) { + done = 1; + break; + } + } + default: /* fallthrough if the key wasn't escape */ + getCurrentLevel()->postEvent(event); + break; + } + } + + getCurrentLevel()->drawScene(); + } +} diff --git a/Game.h b/Game.h index e0b9c1c..2a4db57 100644 --- a/Game.h +++ b/Game.h @@ -32,6 +32,8 @@ public: \return index of the current level */ int getLevelIndex(); + + void run(); private: int mCurrentLevel; /**< current index */ diff --git a/Level.cpp b/Level.cpp index 4bbfe31..fc31120 100644 --- a/Level.cpp +++ b/Level.cpp @@ -1,12 +1,19 @@ #include "Level.h" #include "fns.h" +Level::Level(SDL_Surface* screen) : + mBackground(0), + mScreen(screen) +{ + mBackground = LoadImage("Backgrounds/bg.bmp"); +} + void Level::DrawIMG(SDL_Surface *img, int x, int y) { SDL_Rect dest; dest.x = x; dest.y = y; - SDL_BlitSurface(img, NULL, screen, &dest); + SDL_BlitSurface(img, NULL, mScreen, &dest); } @@ -20,31 +27,31 @@ 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, screen, &dest); + SDL_BlitSurface(img, &src, mScreen, &dest); } void Level::DrawBG() { - DrawIMG(back, 0, 0); + DrawIMG(mBackground, 0, 0); } -void Level::DrawScene(SDL_Surface *screen) +void Level::drawScene() { Uint32 color; - // Create a black background using the screen pixel format (32 bpp) - color = SDL_MapRGB (screen->format, 0, 0, 0); - SDL_FillRect (screen, NULL, 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 DrawBG(); DrawSprites(); - // Flip the working image buffer with the screen buffer - SDL_Flip (screen); + // Flip the working image buffer with the mScreen buffer + SDL_Flip (mScreen); // Add a little pause... SDL_Delay (1); @@ -52,22 +59,32 @@ void Level::DrawScene(SDL_Surface *screen) void Level::DrawSprites() { - for(int i=0; i SpriteList; + vector mSprites; void DrawBG(); void DrawIMG(); void DrawIMG(SDL_Surface *img, int x, int y); diff --git a/main.cpp b/main.cpp index 111a75c..2d18d73 100644 --- a/main.cpp +++ b/main.cpp @@ -7,6 +7,7 @@ #include #include #include "fns.h" +#include "Game.h" using namespace std; @@ -170,41 +171,6 @@ void DrawBG() bool init() { - // Initialize SDL - if (SDL_Init (SDL_INIT_VIDEO) < 0) - { - printf ("Couldn't initialize SDL: %s\n", SDL_GetError ()); - exit (1); - } - - atexit (SDL_Quit); - - // Set 800x600 32-bits video mode - screen = SDL_SetVideoMode (800, 600, 32, SDL_SWSURFACE | SDL_DOUBLEBUF); - if (screen == NULL) - { - printf ("Couldn't set 800x600 32b video mode: %s\n", SDL_GetError ()); - exit (2); - } - - // Set the title of our application window handler - SDL_WM_SetCaption ("SDL MultiMedia Application", NULL); - - // We now load our first bitmap image. - back = LoadImage("Backgrounds/bg.bmp"); - image = LoadImage("image.bmp"); - - // This lets us set the original coordinates of our image on the screen. - // Don't forget that top left corner is the origin (0,0) - //rect.x = 200; - //rect.y = 200; - - // We activate keyboard repetition. Therefore, when a key stays pressed down - // it will keep moving the image around the screen - // To see prcisely what this toggle does, just comment the line and recompile - // the code... - SDL_EnableKeyRepeat(25, 20); - return true; } @@ -274,15 +240,10 @@ void DrawScene(SDL_Surface *screen) // until an escape condition is reached int main (int argc, char *argv[]) { - // Used to loop - int done = 0; - // The keys variable is used to store the state of the keyboard at - // each frame and see if which keys are pressed down - Uint8 *keys; - - // Initialise SDL and all the rest... - init(); + Game g; + g.run(); +#if 0 //load sprites //Animation vikingAnimation = new Animation("viking.anim"); vikingAnimation.loadAnimation("viking.anim"); @@ -307,40 +268,8 @@ int main (int argc, char *argv[]) //Hide Cursor SDL_ShowCursor(0); +#endif - // Game Loop - while (!done) - { - // This will let us track events - SDL_Event event; - - // We fill 'event' with the first event in the event queue - while (SDL_PollEvent (&event)) - { - switch (event.type) - { - // If our event reports a key being pressed down - // we process it - case SDL_KEYDOWN: - keys = SDL_GetKeyState(NULL); - if ( keys[SDLK_LEFT] ) { SpriteList[0].xadd(-1);} - if ( keys[SDLK_RIGHT] ) { SpriteList[0].xadd(1);} - if ( keys[SDLK_UP] ) { SpriteList[0].yadd(-1);} - if ( keys[SDLK_DOWN] ) { SpriteList[0].yadd(1);} - if ( keys[SDLK_ESCAPE] ) { done = 1;} - break; - // 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; - default: - break; - } - } - // Render the scene - DrawScene (screen); - } return 0; }