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



參考資訊:
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)
{
    GLuint tex = 0;  
    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);
 
    BITMAP bm = { 0 };
    BITMAPINFO bmi = { 0 };
    HBITMAP bmp = (HBITMAP)LoadImageA(NULL, "main.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    GetObject(bmp, sizeof(BITMAP), &bm);
  
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biWidth = bm.bmWidth;
    bmi.bmiHeader.biHeight = -bm.bmHeight;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 24;
    bmi.bmiHeader.biCompression = BI_RGB;
  
    int size = bm.bmWidth * bm.bmHeight * 3;
    BYTE *pixels = HeapAlloc(GetProcessHeap(), 0, size);
    HDC hdc = GetDC(NULL);
    GetDIBits(hdc, bmp, 0, bm.bmHeight, pixels, &bmi, DIB_RGB_COLORS);
    ReleaseDC(NULL, hdc);
 
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);
    glEnable(GL_TEXTURE_2D);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bm.bmWidth, bm.bmHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, pixels);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   
    HeapFree(GetProcessHeap(), 0, pixels);
    DeleteObject(bmp);
 
    GLuint base = glGenLists(256);
    glBindTexture(GL_TEXTURE_2D, tex);
    for (int i = 0; i < 256; i++) {
        const int sz = 16;
        const float fix = 0.0625;
        float cx = 1 - (float)(i % sz) / (GLfloat)sz;
        float cy = 1 - (float)(i / sz) / (GLfloat)sz;
  
        glNewList(base + (255 - i), GL_COMPILE);
            glBegin(GL_QUADS);
                glTexCoord2f(cx - fix, cy);
                glVertex2i(0, 0);
  
                glTexCoord2f(cx, cy);
                glVertex2i(sz, 0);
  
                glTexCoord2f(cx, cy - fix);
                glVertex2i(sz, sz);
  
                glTexCoord2f(cx - fix, cy - fix);
                glVertex2i(0, sz);
            glEnd();
  
            glTranslated(16, 0, 0);
        glEndList();
    }

    glViewport(0, 0, W, H); 

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

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
  
    MSG msg = { 0 };
    char buf[255] = { "Hello, world!" };
    while (GetMessage(&msg, NULL, 0, 0)) {
        DispatchMessage(&msg);
  
        glLoadIdentity();
        glTranslated(10, 100, 0);
        glListBase(base - 32);
        glCallLists(strlen(buf), GL_BYTE, buf);
 
        SwapBuffers(hDC);
    }
  
    glDeleteLists(base, 256); 
    glDeleteTextures(1, &tex);
    wglMakeCurrent(NULL, NULL);
    wglDeleteContext(hRC);
    ReleaseDC(hWnd, hDC);

    return 0;
}

編譯、執行

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