Modified the game loop to use events posted by a timer
authorMatt Mullins <mokomull@gmail.com>
Sun, 12 Jul 2009 22:42:24 +0000 (17:42 -0500)
committerMatt Mullins <mokomull@gmail.com>
Sun, 12 Jul 2009 22:42:24 +0000 (17:42 -0500)
Game.cpp
Game.h
Level.cpp

index e2679b2..db7596a 100644 (file)
--- a/Game.cpp
+++ b/Game.cpp
@@ -1,18 +1,21 @@
 #include "Game.h"
 #include "fns.h"
 #include <string>
+#include <iostream>
+
+const Uint32 Game::waitTime = 30; /* ms */
 
 Game::Game() : mCurrentLevel(0), mScreen(0)
 {
        // Initialize SDL 
-       if (SDL_Init(SDL_INIT_VIDEO) < 0) {
+       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
-       mScreen = SDL_SetVideoMode(640, 468, 32, SDL_SWSURFACE);
+       mScreen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
        if (!mScreen) {
                printf ("Couldn't set video mode: %s\n", SDL_GetError ());
                exit (2);
@@ -56,35 +59,62 @@ void Game::run()
 {
        bool done = false;
 
+       SDL_TimerID timer = SDL_AddTimer(waitTime, timerCallback, NULL);
+       if (!timer) {
+               std::cerr << "Timer creation failed" << std::endl;
+               return;
+       }
+
        // 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;
+               // Wait for an event to occur
+               if (0 == SDL_WaitEvent(&event)) {
+                       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);
+               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;
+
+               case SDL_USEREVENT:
+                       getCurrentLevel()->drawScene();
+                       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;
                        }
                }
-
-               getCurrentLevel()->drawScene();
+               default: /* fallthrough if the key wasn't escape */
+                       getCurrentLevel()->postEvent(event);
+                       break;
+               }
        }
+
+       SDL_RemoveTimer(timer);
+}
+
+Uint32 Game::timerCallback(Uint32 interval, void* data)
+{
+       SDL_Event event;
+       SDL_UserEvent uevent = {
+               SDL_USEREVENT, /* type */
+               0, /* code */
+               0, /* data1 */
+               0 }; /* data2 */
+       event.user = uevent;
+
+       SDL_PushEvent(&event);
+
+       return interval;
 }
diff --git a/Game.h b/Game.h
index ae01b02..e8185a0 100644 (file)
--- a/Game.h
+++ b/Game.h
@@ -35,6 +35,8 @@ public:
        void run();
        
 private:
+       static const Uint32 waitTime;
+
        int mCurrentLevel; /**< current index */
        std::vector<Level*> mLevels;
        SDL_Surface *mScreen;
@@ -42,6 +44,8 @@ private:
        void addLevel(Level level);
        void removeLevel(std::string levelName);
        void loadLevel(std::string levelName);
+
+       static Uint32 timerCallback(Uint32 interval, void* data);
 };
 
 #endif
index 6b78004..3ee28be 100644 (file)
--- a/Level.cpp
+++ b/Level.cpp
@@ -79,9 +79,6 @@ void Level::drawScene()
     
     // Flip the working image buffer with the mScreen buffer
     SDL_Flip (mScreen);
-    
-    // Add a little pause...
-    SDL_Delay (1);
 }
 
 void Level::DrawSprites()