Skip to content

Instantly share code, notes, and snippets.

@therathatter
Created June 18, 2022 06:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save therathatter/1bc149bc292f48a0c5511df84de2f3dc to your computer and use it in GitHub Desktop.
Save therathatter/1bc149bc292f48a0c5511df84de2f3dc to your computer and use it in GitHub Desktop.
Pump It Up 60 FPS locker
#include <dlfcn.h>
#include <GL/glx.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <sched.h>
#include <time.h>
#include <err.h>
static uint64_t get_time_ns(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * (uint64_t)1e9 + (uint64_t)ts.tv_nsec;
}
extern "C" void glXSwapBuffers(Display *dpy, GLXDrawable drawable)
{
static auto o_swap_buffers = reinterpret_cast<decltype(&glXSwapBuffers)>(dlsym(RTLD_NEXT, "glXSwapBuffers"));
o_swap_buffers(dpy, drawable);
if (60 > 0) {
static uint64_t last_time;
const double interval = 1e9 / 60;
const useconds_t step = interval / 1e6;
while (last_time > 0 && get_time_ns() - last_time < interval) {
sched_yield();
usleep(step);
}
last_time = get_time_ns();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment