diff --git a/Makefile b/Makefile index ef5bac1..17c743f 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,11 @@ PROJECT=treewars CXX=g++ CXXFLAGS=-DDEBUG -g `sdl-config --cflags` LDFLAGS=`sdl-config --libs` -OBJECTS=main.o drawutils.o mathutils.o graph.o gamedata.o mainevent.o gamestate.o game.o titlescreen.o +OBJECTS=\ +main.o \ +drawutils.o mathutils.o timer.o \ +graph.o gamedata.o \ +mainevent.o gamestate.o game.o titlescreen.o all: $(PROJECT) diff --git a/game.cpp b/game.cpp index 4f32d4d..6198a54 100644 --- a/game.cpp +++ b/game.cpp @@ -31,8 +31,7 @@ bool Game::init() return false; } - init_done = true; - return true; + return GameState::init(); } diff --git a/gamestate.cpp b/gamestate.cpp index 7b430f1..adc38a0 100644 --- a/gamestate.cpp +++ b/gamestate.cpp @@ -1,5 +1,7 @@ #include "gamestate.h" +unsigned int GameState::FPS = 60; + GameState::GameState(stack* state_stack, SDL_Surface* display) { this->state_stack = state_stack; @@ -8,13 +10,29 @@ GameState::GameState(stack* state_stack, SDL_Surface* display) } +bool GameState::init() +{ + timer.start(); + init_done = true; + return true; +} + + void GameState::execute() throw(StateExit) { + timer.start(); + SDL_Event event; while(SDL_PollEvent(&event)) handle_event(&event); iterate(); render(); + + // Let's avoid using *too* much of the CPU... + if (timer.get_ticks() < 1000/FPS) + { + SDL_Delay((1000/FPS) - timer.get_ticks()); + } } diff --git a/gamestate.h b/gamestate.h index 00bed1c..2212fa3 100644 --- a/gamestate.h +++ b/gamestate.h @@ -7,6 +7,7 @@ #define _GAME_STATE_H_ #include "mainevent.h" +#include "timer.h" #include #include #include @@ -22,10 +23,11 @@ class GameState : public MainEvent GameState(stack* state_stack, SDL_Surface* display); virtual ~GameState() {} - virtual bool init() = 0; void execute() throw(StateExit); bool initted() { return init_done;} + virtual bool init(); + protected: virtual void iterate() = 0; virtual void render() = 0; @@ -35,6 +37,9 @@ class GameState : public MainEvent stack* state_stack; SDL_Surface* display; bool init_done; + Timer timer; + + static unsigned int FPS; }; diff --git a/timer.cpp b/timer.cpp new file mode 100644 index 0000000..0c55718 --- /dev/null +++ b/timer.cpp @@ -0,0 +1,26 @@ +#include "timer.h" + +Timer::Timer() +{ + init_ticks = 0; + current_ticks = 0; +} + + +void Timer::start() +{ + init_ticks = SDL_GetTicks(); + current_ticks = init_ticks; +} + + +int Timer::get_ticks() +{ + // The odd case that we overflow... + // If this happens, we basically reset the timer. This isn't the most + // elegant solution, but it's the best one I see. Nothing should rely + // on this timer for super-precision as a result + if (current_ticks < init_ticks) init_ticks = current_ticks; + + return current_ticks - init_ticks; +} diff --git a/timer.h b/timer.h new file mode 100644 index 0000000..ea08426 --- /dev/null +++ b/timer.h @@ -0,0 +1,23 @@ +/* A wrapper around the SDL_GetTicks() function, to add more + * convenient and intelligent functionality + */ + +#ifndef _TIMER_H_ +#define _TIMER_H_ + +#include + +class Timer +{ + public: + Timer(); + + void start(); + int get_ticks(); + + private: + int init_ticks; + int current_ticks; +}; + +#endif diff --git a/title_banner.bmp b/title_banner.bmp new file mode 100644 index 0000000..329a6d6 Binary files /dev/null and b/title_banner.bmp differ diff --git a/titlescreen.cpp b/titlescreen.cpp index 84499f0..d25ad22 100644 --- a/titlescreen.cpp +++ b/titlescreen.cpp @@ -42,8 +42,7 @@ bool TitleScreen::init() DrawUtils::transpare(title_banner); - init_done = true; - return true; + return GameState::init(); }