Skip to content

Instantly share code, notes, and snippets.

@ZaneA
Created December 16, 2012 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZaneA/4305411 to your computer and use it in GitHub Desktop.
Save ZaneA/4305411 to your computer and use it in GitHub Desktop.
An LD_PRELOAD hack to create an OpenGL overlay.
An example hack to use `LD_PRELOAD` with OpenGL to create an "Overlay". Could do with nicer OpenGL and everything, and only works with GLFW in its current form.
Compile with `make`. Run with `LD_PRELOAD=liboverlay.so /path/to/application`.
#include <GL/gl.h>
#include <FTGL/ftgl.h>
#include <stdio.h>
#include <dlfcn.h>
static void init () __attribute__((constructor));
void performOverlay();
void (*_glfwSwapBuffers)() = NULL;
void glfwSwapBuffers()
{
if (_glfwSwapBuffers == NULL) {
void *handle = dlopen("libglfw.so", RTLD_LAZY);
if (handle) {
_glfwSwapBuffers = dlsym(handle, "glfwSwapBuffers");
dlclose(handle);
}
}
performOverlay();
_glfwSwapBuffers();
}
static void init()
{
}
void performOverlay()
{
static FTGLfont *font = NULL;
if (font == NULL) {
font = ftglCreateTextureFont("font.ttf");
ftglSetFontFaceSize(font, 16, 16);
}
glColor4f(0, 0, 0, 1);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, 800.0, 0.0, 600.0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(10, 590 - 16, 0.0);
ftglRenderFont(font, "Here I should print some useful information.", FTGL_RENDER_ALL);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
overlay : main.c
gcc -o liboverlay.so -shared -fPIC main.c -lGL -I/usr/include/freetype2 -lftgl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment