Created
December 16, 2012 08:42
-
-
Save ZaneA/4305411 to your computer and use it in GitHub Desktop.
An LD_PRELOAD hack to create an OpenGL overlay.
This file contains 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
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`. |
This file contains 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 <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); | |
} |
This file contains 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
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