windows: fixed indexed color support, build on msvc6, and rgb palette
authorJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 4 Sep 2022 14:59:18 +0000 (17:59 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Sun, 4 Sep 2022 14:59:18 +0000 (17:59 +0300)
miniglut.c

index 57abff9..0f34782 100644 (file)
@@ -66,6 +66,11 @@ static int cmap_size;
 #include <GL/gl.h>
 #include "miniglut.h"
 
+#ifdef _MSC_VER
+#pragma warning (disable: 4244 4305)
+#endif
+
+
 struct ctx_info {
        int rsize, gsize, bsize, asize;
        int zsize, ssize;
@@ -1466,9 +1471,7 @@ static int create_window_wglext(const char *title, int width, int height)
        HWND tmpwin = 0;
        HDC tmpdc = 0;
        HGLRC tmpctx = 0;
-       int i, pixfmt, curbpp;
-       char palbuf[sizeof(LOGPALETTE) + 255 * sizeof(PALETTEENTRY)];
-       LOGPALETTE *logpal;
+       int pixfmt;
 
        /* create a temporary window and GL context, just to query and retrieve
         * the wglChoosePixelFormatEXT function
@@ -1542,52 +1545,6 @@ static int create_window_wglext(const char *title, int width, int height)
        GETATTR(WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB, &ctx_info.srgb);
        GETATTR(WGL_SAMPLES_ARB, &ctx_info.samples);
 
-       if(init_mode & GLUT_INDEX) {
-               logpal = (LOGPALETTE*)palbuf;
-
-               GetSystemPaletteEntries(dc, 0, 256, logpal->palPalEntry);
-
-               logpal->palVersion = 0x300;
-               logpal->palNumEntries = 256;
-
-               if(!(cmap = CreatePalette(logpal))) {
-                       panic("Failed to create palette in indexed mode");
-               }
-               SelectPalette(dc, cmap, 0);
-               RealizePalette(dc);
-
-               cmap_size = 256;
-       } else {
-               if((curbpp = GetDeviceCaps(dc, BITSPIXEL) * GetDeviceCaps(dc, PLANES)) <= 8) {
-                       /* for RGB mode in 8bpp displays we also need to set up a palette
-                        * with RGB 332 colors
-                        */
-                       logpal = (LOGPALETTE*)palbuf;
-
-                       logpal->palVersion = 0x300;
-                       logpal->palNumEntries = 256;
-
-                       for(i=0; i<256; i++) {
-                               int r = i & 7;
-                               int g = (i >> 3) & 7;
-                               int b = (i >> 5) & 3;
-
-                               logpal->palPalEntry[i].peRed = (r << 5) | (r << 2) | (r >> 1);
-                               logpal->palPalEntry[i].peGreen = (g << 5) | (g << 2) | (g >> 1);
-                               logpal->palPalEntry[i].peBlue = (b << 6) | (b << 4) | (b << 2) | b;
-                               logpal->palPalEntry[i].peFlags = PC_NOCOLLAPSE;
-                       }
-
-                       if(!(cmap = CreatePalette(logpal))) {
-                               warn("Failed to create RGB 332 palette on palettized mode. Colors will be wrong\n");
-                       } else {
-                               SelectPalette(dc, cmap, 0);
-                               RealizePalette(dc);
-                       }
-                       cmap_size = 256;
-               }
-       }
-
        return 0;
 
 fail:
@@ -1605,9 +1562,11 @@ fail:
 
 static void create_window(const char *title)
 {
-       int pixfmt;
        RECT rect;
-       int width, height;
+       int i, pixfmt, width, height;
+       char palbuf[sizeof(LOGPALETTE) + 255 * sizeof(PALETTEENTRY)];
+       LOGPALETTE *logpal;
+
 
        rect.left = init_x;
        rect.top = init_y;
@@ -1625,10 +1584,10 @@ static void create_window(const char *title)
                pfd.dwFlags |= PFD_STEREO;
        }
        if(init_mode & GLUT_INDEX) {
-               pfd.iPixelType = PFD_TYPE_RGBA;
+               pfd.iPixelType = PFD_TYPE_COLORINDEX;
                pfd.cColorBits = 8;
        } else {
-               pfd.iPixelType = PFD_TYPE_COLORINDEX;
+               pfd.iPixelType = PFD_TYPE_RGBA;
                pfd.cColorBits = 24;
        }
        if(init_mode & GLUT_ALPHA) {
@@ -1645,41 +1604,94 @@ static void create_window(const char *title)
        }
        pfd.iLayerType = PFD_MAIN_PLANE;
 
-
-       if(create_window_wglext(title, width, height) == -1) {
-
-               if(!(win = CreateWindow("MiniGLUT", title, WS_OVERLAPPEDWINDOW,
-                                       rect.left, rect.top, width, height, 0, 0, hinst, 0))) {
-                       panic("Failed to create window\n");
+       if(init_mode & (GLUT_SRGB | GLUT_MULTISAMPLE)) {
+               if(create_window_wglext(title, width, height) != -1) {
+                       goto ctxdone;
                }
-               dc = GetDC(win);
+       }
 
-               if(!(pixfmt = ChoosePixelFormat(dc, &pfd))) {
-                       panic("Failed to find suitable pixel format\n");
-               }
-               if(!SetPixelFormat(dc, pixfmt, &pfd)) {
-                       panic("Failed to set the selected pixel format\n");
-               }
-               if(!(ctx = wglCreateContext(dc))) {
-                       panic("Failed to create the OpenGL context\n");
-               }
-               wglMakeCurrent(dc, ctx);
+       /* if we don't need sRGB or multisample, or if the wglChoosePixelFormat method
+        * failed, just use the old-style ChoosePixelFormat method instead
+        */
+       if(!(win = CreateWindow("MiniGLUT", title, WS_OVERLAPPEDWINDOW,
+                               rect.left, rect.top, width, height, 0, 0, hinst, 0))) {
+               panic("Failed to create window\n");
+       }
+       dc = GetDC(win);
 
-               DescribePixelFormat(dc, pixfmt, sizeof pfd, &pfd);
-               ctx_info.rsize = pfd.cRedBits;
-               ctx_info.gsize = pfd.cGreenBits;
-               ctx_info.bsize = pfd.cBlueBits;
-               ctx_info.asize = pfd.cAlphaBits;
-               ctx_info.zsize = pfd.cDepthBits;
-               ctx_info.ssize = pfd.cStencilBits;
-               ctx_info.dblbuf = pfd.dwFlags & PFD_DOUBLEBUFFER ? 1 : 0;
-               ctx_info.samples = 0;
-               ctx_info.srgb = 0;
+       if(!(pixfmt = ChoosePixelFormat(dc, &pfd))) {
+               panic("Failed to find suitable pixel format\n");
        }
+       if(!SetPixelFormat(dc, pixfmt, &pfd)) {
+               panic("Failed to set the selected pixel format\n");
+       }
+       if(!(ctx = wglCreateContext(dc))) {
+               panic("Failed to create the OpenGL context\n");
+       }
+       wglMakeCurrent(dc, ctx);
 
+       DescribePixelFormat(dc, pixfmt, sizeof pfd, &pfd);
+       ctx_info.rsize = pfd.cRedBits;
+       ctx_info.gsize = pfd.cGreenBits;
+       ctx_info.bsize = pfd.cBlueBits;
+       ctx_info.asize = pfd.cAlphaBits;
+       ctx_info.zsize = pfd.cDepthBits;
+       ctx_info.ssize = pfd.cStencilBits;
+       ctx_info.dblbuf = pfd.dwFlags & PFD_DOUBLEBUFFER ? 1 : 0;
+       ctx_info.samples = 0;
+       ctx_info.srgb = 0;
+
+ctxdone:
        ShowWindow(win, 1);
        SetForegroundWindow(win);
        SetFocus(win);
+
+       if(init_mode & GLUT_INDEX) {
+               logpal = (LOGPALETTE*)palbuf;
+
+               GetSystemPaletteEntries(dc, 0, 256, logpal->palPalEntry);
+
+               logpal->palVersion = 0x300;
+               logpal->palNumEntries = 256;
+
+               if(!(cmap = CreatePalette(logpal))) {
+                       panic("Failed to create palette in indexed mode");
+               }
+               SelectPalette(dc, cmap, 0);
+               RealizePalette(dc);
+
+               cmap_size = 256;
+       } else {
+               if(GetDeviceCaps(dc, BITSPIXEL) * GetDeviceCaps(dc, PLANES) <= 8) {
+                       /* for RGB mode in 8bpp displays we also need to set up a palette
+                        * with RGB 332 colors
+                        */
+                       logpal = (LOGPALETTE*)palbuf;
+
+                       logpal->palVersion = 0x300;
+                       logpal->palNumEntries = 256;
+
+                       for(i=0; i<256; i++) {
+                               int r = i & 7;
+                               int g = (i >> 3) & 7;
+                               int b = (i >> 5) & 3;
+
+                               logpal->palPalEntry[i].peRed = (r << 5) | (r << 2) | (r >> 1);
+                               logpal->palPalEntry[i].peGreen = (g << 5) | (g << 2) | (g >> 1);
+                               logpal->palPalEntry[i].peBlue = (b << 6) | (b << 4) | (b << 2) | b;
+                               logpal->palPalEntry[i].peFlags = PC_NOCOLLAPSE;
+                       }
+
+                       if(!(cmap = CreatePalette(logpal))) {
+                               warn("Failed to create RGB 332 palette on palettized mode. Colors will be wrong\n");
+                       } else {
+                               SelectPalette(dc, cmap, 0);
+                               RealizePalette(dc);
+                       }
+                       cmap_size = 256;
+               }
+       }
+
        upd_pending = 1;
        reshape_pending = 1;
 }
@@ -1811,6 +1823,16 @@ static void update_modkeys(void)
        }
 }
 
+#ifndef VK_OEM_1
+#define VK_OEM_1       0xba
+#define VK_OEM_2       0xbf
+#define VK_OEM_3       0xc0
+#define VK_OEM_4       0xdb
+#define VK_OEM_5       0xdc
+#define VK_OEM_6       0xdd
+#define VK_OEM_7       0xde
+#endif
+
 static int translate_vkey(int vkey)
 {
        switch(vkey) {