From b31a3e26ab304a0017c40ee0d1c08fcfbb5ef37c Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Fri, 1 Jul 2011 11:40:38 -0400 Subject: [PATCH] Added some performance boosting framerate regulation --- Makefile | 6 +++++- game.cpp | 3 +-- gamestate.cpp | 18 ++++++++++++++++++ gamestate.h | 7 ++++++- timer.cpp | 26 ++++++++++++++++++++++++++ timer.h | 23 +++++++++++++++++++++++ title_banner.bmp | Bin 0 -> 292854 bytes titlescreen.cpp | 3 +-- 8 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 timer.cpp create mode 100644 timer.h create mode 100644 title_banner.bmp 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 0000000000000000000000000000000000000000..329a6d61dc30211737c06d5f4c15f4fdebed44b7 GIT binary patch literal 292854 zcmeHQO|~vMPJD2fF=WCLSOE)Q&U)Ab3t&C0ifQ4l?9x%$R_MpF(|JoAx;s(|MTGQy z_vJ16Q1u`G`G5cQPrv^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)+NCg>uTnQU`%LK8)>A!@ZUc(jnQCIA-)kzB;=A&lW4LM`r87~xEBCB z-tx-6%jd~m7{_)=J!CAH*fnPJIvQm+EeqVei+q9)_LW+f>?Orh>$~%-0Y<{^NMZdk zYFBY7ce9p9uAlLKZhERM_q1bG_955>Uu3GgyqGO!t!^X{R6A*KT36L_3DXR`7_;LF z0LR2@s=ENV@Yw2RUwj<187l`e8ykeL63m@#NPw&%V)UVz{2jL1%?P+~$ok}~sK>gX7^mdwFw~BRvxcx z7pM!=1?mEIf%`5{@8|nIlf%EFm0ew+E>IV!3tV`CdOz3uxh_x_s0-8u>H_y&px)2- zeI^(F8ds6(0(F79KwaSQ1?v4=@8`NeU7#*d7pM!|cY%68-}jju{uQn4>H>9vxe!lNBx$xJxic}Y<3)BVb0*5b9@8^0y*9GbVb%DA- zUEsb8)cg6q&*bp0Xk}Lys0-8u>H-&Dpx)2*ey$7D1?mEIfx5tb7pV91eV@sNzs6Oh ixIUZe1UpD*Za9HP#35R)CKAS_g&!Eum1;52o*R0 literal 0 HcmV?d00001 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(); }