diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client/client.cc | 59 |
1 files changed, 41 insertions, 18 deletions
diff --git a/src/client/client.cc b/src/client/client.cc index 9db7f7e..ede6b29 100644 --- a/src/client/client.cc +++ b/src/client/client.cc @@ -24,6 +24,8 @@ namespace client { core::Cvar *cl_framerate = 0; +core::Cvar *cl_frametime = 0; + Client app; //--- engine functions -------------------------------------------- @@ -87,9 +89,11 @@ void Client::init(int count, char **arguments) cvar = core::Cvar::get("cl_colorsecond", "1.0 1.0 1.0", core::Cvar::Archive | core::Cvar::Info); cvar->set_info("[r g b] player secondary color"); - cl_framerate = core::Cvar::get("cl_framerate", "120", core::Cvar::Archive); + cl_framerate = core::Cvar::get("cl_framerate", "125", core::Cvar::Archive); cl_framerate->set_info("[int] client framerate in frames/sec"); + cl_frametime = core::Cvar::set("cl_frametime", "8", core::Cvar::ReadOnly); + cl_frametime->set_info("[int] current minimal length of one frame, in milliseconds (read-only)"); // initialize SDL, but do not initialize any subsystems SDL_Init(0); @@ -117,30 +121,49 @@ void Client::run() { con_print << "^BRunning client..." << std::endl; - Uint32 client_framerate = (Uint32)(1000/120); - Uint32 elapsed = 0; + // default framerate 125fps, 8 milliseconds + Uint32 client_frame_lenght = 8; + + Uint32 client_previous_timestamp = 0; + Uint32 client_current_timestamp = 0; console()->flush(); console()->clear_notify(); while (true) { - Uint32 chrono = SDL_GetTicks(); - - core::Application::frame((float)elapsed / 1000.0f); - video::frame((float)elapsed / 1000.0f); - input::frame((float)elapsed / 1000.0f); + // elapsed time in microseconds + client_current_timestamp = SDL_GetTicks(); + + // calculate the desired frame length + if (cl_framerate->value() < 0) { + (*cl_framerate) = 0.0f; + } else if (cl_framerate->value() > 1000.0f) { + (*cl_framerate) = 1000.0f; + } + + if (cl_framerate->value()) { + client_frame_lenght = (Uint32) roundf(1000.0f / cl_framerate->value()); + } else { + client_frame_lenght = 0; + } + (*cl_frametime) = (float) client_frame_lenght; - Uint32 current = SDL_GetTicks(); - elapsed = current - chrono; - - if (cl_framerate->value()) { - client_framerate = (Uint32) (1000.0f / cl_framerate->value()); - if (client_framerate && (elapsed < client_framerate)) { - SDL_Delay(client_framerate - elapsed); - elapsed = client_framerate; + // only advance per microsecond frame + Uint32 d = client_current_timestamp - client_previous_timestamp; + if ((d > 0)) { + if (d >= client_frame_lenght) { + float elapsed = (float)(d) / 1000.f; + core::Application::frame(elapsed); + video::frame(elapsed); + input::frame(elapsed); + + client_previous_timestamp = client_current_timestamp; + } else { + SDL_Delay(client_frame_lenght - d); } - }; - + } else { + SDL_Delay(1); + } } } |