Last active
August 18, 2024 22:23
-
-
Save therathatter/1bc149bc292f48a0c5511df84de2f3dc to your computer and use it in GitHub Desktop.
Pump It Up 60 FPS locker (or really, anything that uses glXSwapBuffers)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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