#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);
{
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;
}
void run();
private:
+ static const Uint32 waitTime;
+
int mCurrentLevel; /**< current index */
std::vector<Level*> mLevels;
SDL_Surface *mScreen;
void addLevel(Level level);
void removeLevel(std::string levelName);
void loadLevel(std::string levelName);
+
+ static Uint32 timerCallback(Uint32 interval, void* data);
};
#endif