Skip to content

Commit

Permalink
Add framerate cap
Browse files Browse the repository at this point in the history
  • Loading branch information
dashodanger committed Sep 17, 2024
1 parent 3c3d079 commit dd6e37f
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion source_files/edge/i_video.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
// clang-format on
#endif

#include "ddf_main.h"
#include "dm_state.h"
#include "edge_profiling.h"
#include "epi_str_compare.h"
#include "epi_str_util.h"
Expand Down Expand Up @@ -65,6 +67,8 @@ EDGE_DEFINE_CONSOLE_VARIABLE(pixel_aspect_ratio, "1.0", kConsoleVariableFlagRead
// including windowed mode.
EDGE_DEFINE_CONSOLE_VARIABLE(forced_pixel_aspect_ratio, "0", kConsoleVariableFlagArchive)

EDGE_DEFINE_CONSOLE_VARIABLE(framerate_limit, "500", kConsoleVariableFlagArchive)

static bool grab_state;

extern ConsoleVariable renderer_far_clip;
Expand Down Expand Up @@ -470,8 +474,37 @@ void FinishFrame(void)
if (grab_mouse.CheckModified())
GrabCursor(grab_state);

if (uncapped_frames.d_ && !single_tics)
{
if (framerate_limit.d_ >= kTicRate)
{
uint64_t target_time = 1000000ull / framerate_limit.d_;
static uint64_t start_time;

while (1)
{
uint64_t current_time = GetMicroseconds();
uint64_t elapsed_time = current_time - start_time;
uint64_t remaining_time = 0;

if (elapsed_time >= target_time)
{
start_time = current_time;
break;
}

remaining_time = target_time - elapsed_time;

if (remaining_time > 1000)
{
SleepForMilliseconds((remaining_time - 1000) / 1000);
}
}
}
}

if (uncapped_frames.d_)
fractional_tic = (float)(GetMilliseconds() * 35 % 1000) / 1000;
fractional_tic = (float)(GetMilliseconds() * kTicRate % 1000) / 1000;

if (vsync.CheckModified())
{
Expand Down

0 comments on commit dd6e37f

Please sign in to comment.