Added some performance boosting framerate regulation
This commit is contained in:
parent
b50ed649f5
commit
b31a3e26ab
6
Makefile
6
Makefile
|
@ -4,7 +4,11 @@ PROJECT=treewars
|
||||||
CXX=g++
|
CXX=g++
|
||||||
CXXFLAGS=-DDEBUG -g `sdl-config --cflags`
|
CXXFLAGS=-DDEBUG -g `sdl-config --cflags`
|
||||||
LDFLAGS=`sdl-config --libs`
|
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)
|
all: $(PROJECT)
|
||||||
|
|
||||||
|
|
3
game.cpp
3
game.cpp
|
@ -31,8 +31,7 @@ bool Game::init()
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
init_done = true;
|
return GameState::init();
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#include "gamestate.h"
|
#include "gamestate.h"
|
||||||
|
|
||||||
|
unsigned int GameState::FPS = 60;
|
||||||
|
|
||||||
GameState::GameState(stack<GameState*>* state_stack, SDL_Surface* display)
|
GameState::GameState(stack<GameState*>* state_stack, SDL_Surface* display)
|
||||||
{
|
{
|
||||||
this->state_stack = state_stack;
|
this->state_stack = state_stack;
|
||||||
|
@ -8,13 +10,29 @@ GameState::GameState(stack<GameState*>* state_stack, SDL_Surface* display)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool GameState::init()
|
||||||
|
{
|
||||||
|
timer.start();
|
||||||
|
init_done = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void GameState::execute() throw(StateExit)
|
void GameState::execute() throw(StateExit)
|
||||||
{
|
{
|
||||||
|
timer.start();
|
||||||
|
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while(SDL_PollEvent(&event))
|
while(SDL_PollEvent(&event))
|
||||||
handle_event(&event);
|
handle_event(&event);
|
||||||
iterate();
|
iterate();
|
||||||
render();
|
render();
|
||||||
|
|
||||||
|
// Let's avoid using *too* much of the CPU...
|
||||||
|
if (timer.get_ticks() < 1000/FPS)
|
||||||
|
{
|
||||||
|
SDL_Delay((1000/FPS) - timer.get_ticks());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#define _GAME_STATE_H_
|
#define _GAME_STATE_H_
|
||||||
|
|
||||||
#include "mainevent.h"
|
#include "mainevent.h"
|
||||||
|
#include "timer.h"
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
|
@ -22,10 +23,11 @@ class GameState : public MainEvent
|
||||||
GameState(stack<GameState*>* state_stack, SDL_Surface* display);
|
GameState(stack<GameState*>* state_stack, SDL_Surface* display);
|
||||||
virtual ~GameState() {}
|
virtual ~GameState() {}
|
||||||
|
|
||||||
virtual bool init() = 0;
|
|
||||||
void execute() throw(StateExit);
|
void execute() throw(StateExit);
|
||||||
bool initted() { return init_done;}
|
bool initted() { return init_done;}
|
||||||
|
|
||||||
|
virtual bool init();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void iterate() = 0;
|
virtual void iterate() = 0;
|
||||||
virtual void render() = 0;
|
virtual void render() = 0;
|
||||||
|
@ -35,6 +37,9 @@ class GameState : public MainEvent
|
||||||
stack<GameState*>* state_stack;
|
stack<GameState*>* state_stack;
|
||||||
SDL_Surface* display;
|
SDL_Surface* display;
|
||||||
bool init_done;
|
bool init_done;
|
||||||
|
Timer timer;
|
||||||
|
|
||||||
|
static unsigned int FPS;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
26
timer.cpp
Normal file
26
timer.cpp
Normal file
|
@ -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;
|
||||||
|
}
|
23
timer.h
Normal file
23
timer.h
Normal file
|
@ -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 <SDL.h>
|
||||||
|
|
||||||
|
class Timer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Timer();
|
||||||
|
|
||||||
|
void start();
|
||||||
|
int get_ticks();
|
||||||
|
|
||||||
|
private:
|
||||||
|
int init_ticks;
|
||||||
|
int current_ticks;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
BIN
title_banner.bmp
Normal file
BIN
title_banner.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 286 KiB |
|
@ -42,8 +42,7 @@ bool TitleScreen::init()
|
||||||
|
|
||||||
DrawUtils::transpare(title_banner);
|
DrawUtils::transpare(title_banner);
|
||||||
|
|
||||||
init_done = true;
|
return GameState::init();
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user