程式語言 - Wine - C/C++ - OpenGL 1.0 - Use XFont



參考資訊:
https://www.winehq.org/
https://github.com/gamedev-net/nehe-opengl
https://wiki.winehq.org/Winelib_User%27s_Guide

main.c

#include <math.h>
#include <stdio.h>
#include <stdbool.h>
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
   
#define W 320
#define H 240
   
static WNDPROC defWndProc = NULL;
  
static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg) {
    case WM_CLOSE:
        DestroyWindow(hWnd);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return CallWindowProc(defWndProc, hWnd, uMsg, wParam, lParam);
}
    
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    HDC hDC = NULL;
    HGLRC hRC = NULL;
    HWND hWnd = NULL;
    GLuint fmt = 0;
    PIXELFORMATDESCRIPTOR pfd = { 0 };
  
    pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 16;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
   
    hWnd = CreateWindow(WC_DIALOG, "main", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, W, H, NULL, NULL, NULL, NULL);
    defWndProc = (WNDPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (long int)WndProc);
 
    hDC = GetDC(hWnd);
    fmt = ChoosePixelFormat(hDC, &pfd);
    SetPixelFormat(hDC, fmt, &pfd);
    hRC = wglCreateContext(hDC);
    wglMakeCurrent(hDC, hRC);
   
    GLuint base = glGenLists(96);
    Display *dpy = XOpenDisplay(NULL);
    XFontStruct *fontInfo = XLoadQueryFont(dpy, "-adobe-helvetica-medium-r-normal--32-*-*-*-p-*-iso8859-1");
    glXUseXFont(fontInfo->fid, 32, 96, base);
    XFreeFont(dpy, fontInfo);
    XCloseDisplay(dpy);

    RECT rc = { 0 };
    GetClientRect(hWnd, &rc);
    glViewport(0, 0, rc.right - rc.left, rc.bottom - rc.top);
 
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, W, 0, H, -1, 1);
  
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
  
    MSG msg = { 0 };
    char buf[255] = { 0 };
    while (GetMessage(&msg, NULL, 0, 0)) {
        DispatchMessage(&msg);
  
        glRasterPos2i(10, 100);
        sprintf(buf, "Hello, world!");
        glPushAttrib(GL_LIST_BIT);
        glListBase(base - 32);
        glCallLists(strlen(buf), GL_UNSIGNED_BYTE, buf);
        glPopAttrib(); 
 
        SwapBuffers(hDC);
    }
   
    glDeleteLists(base, 96);
    wglMakeCurrent(NULL, NULL);
    wglDeleteContext(hRC);
    ReleaseDC(hWnd, hDC);

    return 0;
}

編譯、執行

$ winegcc main.c -o main.exe -lGL -lGLU -lgdi32 -lopengl32 -lX11
$ wine main.exe