Skip to content

Instantly share code, notes, and snippets.

@skull-squadron
Created October 29, 2024 02:36
Show Gist options
  • Save skull-squadron/912e164eaebc042a3a42be6f65776b0b to your computer and use it in GitHub Desktop.
Save skull-squadron/912e164eaebc042a3a42be6f65776b0b to your computer and use it in GitHub Desktop.
Patch to make aafire from aalib to stablize frame rate
--- a/src/aafire.c
+++ b/src/aafire.c
@@ -2,4 +2,5 @@
#include <stdlib.h> /* exit() */
+#include <time.h>
#include "aalib.h"
#define XSIZ aa_imgwidth(context)
@@ -135,14 +136,29 @@ drawfire (void)
aa_scrheight (context));
aa_flush (context);
}
+#define DESIRED_FPS 5
+#define MS_DELAY (1000 / DESIRED_FPS)
+#define MAX_NS 1000000000
+
+static struct timespec
+add_ms (struct timespec base, int ms)
+{
+ long nsec = base.tv_nsec + (long)(100000 * ms);
+ return (struct timespec){ .tv_nsec = (nsec >= MAX_NS) ? nsec - MAX_NS : nsec, .tv_sec = (nsec >= MAX_NS) ? base.tv_sec + 1 : base.tv_sec };
+}
+
static void
game (void)
{
int event;
+ struct timespec cur, next;
gentable ();
while (!(event = aa_getevent (context, 0)) || event == AA_RESIZE)
{
+ clock_gettime (CLOCK_MONOTONIC, &cur);
drawfire ();
+ next = add_ms (cur, MS_DELAY);
+ clock_nanosleep (CLOCK_MONOTONIC, TIMER_ABSTIME, &next, NULL);
}
}
int
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment