Skip to content

Commit 8080135

Browse files
committed
fix: tried accounting for system delay via a small offset on the max fps (4)
1 parent 07444b1 commit 8080135

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

src/lsd.cpp

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ int glyph_width = 0;
4040
int glyph_height = 0;
4141

4242
double delta_time = 0;
43+
const double target_frame_time = 1.0 / LSD::MAX_FPS;
44+
double frame_start_time = 0.0;
45+
double frame_end_time = 0.0;
4346

4447
LSD::Types::AnsiState ansi_state;
4548

@@ -868,6 +871,25 @@ static void scroll_callback(GLFWwindow *, double, double yoff)
868871
}
869872
LSD::dirt_flag = true;
870873
}
874+
875+
void handle_delta_time()
876+
{
877+
878+
double frame_end = glfwGetTime();
879+
880+
double frame_time = frame_end - frame_start_time;
881+
882+
double remaining = LSD::target_frame_time - frame_time;
883+
884+
if (remaining > 0.0) { std::this_thread::sleep_for(std::chrono::duration<double>(remaining)); }
885+
886+
// FINAL timestamp AFTER sleep
887+
double frame_final = glfwGetTime();
888+
LSD::delta_time = frame_final - frame_start_time;
889+
890+
printf("%d\n", (int)(1.0 / LSD::delta_time));
891+
}
892+
871893
}// namespace LSD
872894
int main()
873895
{
@@ -898,7 +920,7 @@ int main()
898920
return -1;
899921
}
900922

901-
// ── Background shader (optional — falls back to clear colour) ────────────
923+
// Background shader falls back to clear color
902924
LSD::g_background_program = LSD::loadShaders("bg.vert", "bg.frag");
903925
if (LSD::g_background_program)
904926
{
@@ -1019,7 +1041,7 @@ int main()
10191041

10201042
while (!glfwWindowShouldClose(win))
10211043
{
1022-
auto frame_start_time = glfwGetTime();
1044+
LSD::frame_start_time = glfwGetTime();
10231045
LSD::fill_status_bar();
10241046
if (LSD::dirt_flag.exchange(false))
10251047
{
@@ -1064,19 +1086,8 @@ int main()
10641086

10651087
glfwSwapBuffers(win);
10661088
glfwPollEvents();
1067-
const double target_frame_time = 1.0 / LSD::MAX_FPS;
1068-
double frame_end = glfwGetTime();
1069-
double frame_time = frame_end - frame_start_time;
1070-
1071-
double remaining = target_frame_time - frame_time;
1072-
1073-
if (remaining > 0.0) { std::this_thread::sleep_for(std::chrono::duration<double>(remaining)); }
1074-
1075-
// FINAL timestamp AFTER sleep
1076-
double frame_final = glfwGetTime();
1077-
LSD::delta_time = frame_final - frame_start_time;
10781089

1079-
printf("%d\n", (int)(1.0 / LSD::delta_time));
1090+
LSD::handle_delta_time();
10801091
}
10811092

10821093
LSD::pty.stop();

src/lsd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static const int FONT_SIZE_MIN = 8;
2121
static const int FONT_SIZE_MAX = 72;
2222
static const int ATLAS_WIDTH = 512;
2323
static const int ATLAS_HEIGHT = 512;
24-
static const int MAX_FPS = 240;
24+
static const int MAX_FPS = 240 + 4;// +4 to adjust for the schedulers bullshit
2525

2626
// Variables that need to be shared (extern)
2727
extern std::string WINDOW_TITLE;

0 commit comments

Comments
 (0)