程式語言 - Simple DirectMedia Layer (SDL) - v1.2 - C/C++ - OpenGL 1.0 - Use XFont



參考資訊:
https://github.com/gamedev-net/nehe-opengl

main.c

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>

int main(int argc, char *argv[])
{
    const int W = 320;
    const int H = 240;
    char buf[255] = { 0 };
    Display *dpy = NULL;
    XFontStruct *fontInfo = NULL;
    GLuint base = 0;
    GLdouble nearPlane = 0.1;
    GLdouble farPlane = 100.0;
    GLdouble top = nearPlane * tan(45.0 * 3.14159 / 360.0);
    GLdouble right = top * ((GLfloat)W / (GLfloat)H);
    SDL_Surface *screen = NULL;

    SDL_Init(SDL_INIT_VIDEO);
    screen = SDL_SetVideoMode(W, H, 16, SDL_SWSURFACE | SDL_OPENGL);

    base = glGenLists(96);
    dpy = XOpenDisplay(NULL);
    fontInfo = XLoadQueryFont(dpy, "-adobe-helvetica-medium-r-normal--32-*-*-*-p-*-iso8859-1");
    glXUseXFont(fontInfo->fid, 32, 96, base);
    XFreeFont(dpy, fontInfo);
    XCloseDisplay(dpy);

    glViewport(0, 0, W, H);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, W, 0, H, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glRasterPos2i(10, 100);
    sprintf(buf, "Hello, world!");
    glPushAttrib(GL_LIST_BIT);
    glListBase(base - 32);
    glCallLists(strlen(buf), GL_UNSIGNED_BYTE, buf);
    glPopAttrib();

    SDL_GL_SwapBuffers();
    SDL_Delay(3000);

    SDL_Quit();
    return 0;
}

編譯、執行

$ gcc main.c -I/usr/include/SDL -o main -lSDL -lGL -lGLU -lX11
$ ./main