Last active
August 29, 2015 14:00
-
-
Save jakemco/11386963 to your computer and use it in GitHub Desktop.
Networked Game Engine Loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int TICKS_PER_SECOND = 25; | |
const int MS_PER_TICK = 1000 / TICKS_PER_SECOND; | |
const int MAX_SKIPPED_FRAMES = 5; | |
void EngineInstance::run() { | |
this->last_tick_time = getTime() - TICKS_PER_SECOND; | |
while (this->shouldKeepRunning()) { | |
this->checkForUpdate(); | |
display(old_state, new_state); | |
send(); //either state or input | |
if(is_server) sleep( last_tick_time + MS_PER_TICK - getTime() ); | |
} | |
} | |
void Sever_or_Standalone::checkForUpdate() { | |
long current_time = getTime(); | |
if (current_time >= last_tick_time + MS_PER_TICK) { | |
float dt = current_time - last_tick_time; | |
last_tick_time = current_time; | |
// this is mostly logical | |
old_state = new_state; | |
new_state = update(old_state, dt); | |
} | |
} | |
void Client::checkUpdate() { | |
if (NetworkUpdateRecieved()) { | |
HandleNetworkUpdate(); | |
} | |
} | |
void EngineInstance::update(old_state, dt) { | |
events = revieve_all_events(); | |
update_all_objects(events, old_state, dt); | |
} | |
void Client_or_Standalone::display(State old_state, State new_state) { | |
// generally between 0 and 1, how far to interpolate between old and new state | |
float interp = (float)(getTime() - last_tick_time) / (float)TICKS_PER_SECOND; | |
render(old_state,new_state,interp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment