[gens-sdl] Initialize and use GLEW.
authorDavid Korth <gerbilsoft@gerbilsoft.com>
Wed, 2 Sep 2015 01:10:28 +0000 (21:10 -0400)
committerDavid Korth <gerbilsoft@gerbilsoft.com>
Wed, 2 Sep 2015 01:10:28 +0000 (21:10 -0400)
This lets us get rid of some hacks to work around MSVC's ancient
OpenGL headers, plus it lets us check if the GL implementation
actually supports certain functionality, e.g. packed pixels.

GLTex: TODO: Move internal variables to GLTexPrivate?

src/gens-sdl/CMakeLists.txt
src/gens-sdl/GLBackend.cpp
src/gens-sdl/GLTex.cpp
src/gens-sdl/GLTex.hpp

index 1569736..c99089e 100644 (file)
@@ -24,6 +24,7 @@ INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIR})
 # Include the OpenGL directory.
 # TODO: FIND_PACKAGE(OpenGL) here even though it's in the root CMakeLists.txt?
 INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR})
+INCLUDE_DIRECTORIES(${GLEW_INCLUDE_DIR})
 
 # Write the config.h file.
 CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/config.gens-sdl.h.in" "${CMAKE_CURRENT_BINARY_DIR}/config.gens-sdl.h")
@@ -85,8 +86,12 @@ DO_SPLIT_DEBUG(gens-sdl)
 TARGET_LINK_LIBRARIES(gens-sdl
        ${SDL2_LIBRARY}
        ${OPENGL_gl_LIBRARY}
+       ${GLEW_LIBRARY}
        )
 
+# Set the compile definitions.
+ADD_DEFINITIONS(${GLEW_DEFINITIONS})
+
 # Compress the EXE.
 IF(COMPRESS_EXE)
        INCLUDE(CompressExeWithUpx)
index d7f62eb..6aea678 100644 (file)
@@ -34,11 +34,8 @@ using LibGens::FastBlur;
 #include <climits>
 #include <cstdio>
 
-// OpenGL
-#ifdef _WIN32
-#include <windows.h>
-#endif
-#include <GL/gl.h>
+// OpenGL (GLEW)
+#include <GL/glew.h>
 
 // Onscreen Display.
 #include "OsdGL.hpp"
@@ -140,6 +137,21 @@ void GLBackendPrivate::reallocTexture(void)
 {
        // TODO: makeCurrent()?
 
+       // Initialize GLEW.
+       // TODO: Initialize this in the main program, not here?
+       // TODO: Multi-context?
+       static bool glew_initialized = false;
+       if (!glew_initialized) {
+               int ret = glewInit();
+               if (ret != GLEW_OK) {
+                       // TODO: Error handling.
+                       // Return an error code?
+                       fprintf(stderr, "GLEW initialization failed: %d\n", ret);
+                       //return ret;
+               }
+               glew_initialized = true;
+       }
+
        MdFb *fb = q->m_fb;
        if (!fb) {
                // No framebuffer.
index 6c78187..1971db3 100644 (file)
@@ -25,6 +25,7 @@
 #include <cerrno>
 #include <cstdlib>
 #include <cstring>
+#include <cstdio>
 
 // System byte order.
 #include "libcompat/byteorder.h"
 #define SDLGL_UNSIGNED_BYTE GL_UNSIGNED_BYTE
 #endif
 
-// MSVC ships with *ancient* GL headers.
-// TODO: Use GLEW.
-#if !defined(GL_BGRA) && defined(GL_BGRA_EXT)
-#define GL_BGRA GL_BGRA_EXT
-#endif
-
-// NOTE: These constants are provided by OpenGL 1.2,
-// *not* GL_EXT_packed_pixels.
-#if defined(GL_UNSIGNED_SHORT_1_5_5_5_REV) && \
-    defined(GL_UNSIGNED_SHORT_5_6_5)
-#define GL_HEADER_HAS_GL_1_2_PACKED_PIXELS
-#endif
-
 namespace GensSdl {
 
 GLTex::GLTex()
@@ -76,42 +64,30 @@ GLTex::~GLTex()
  */
 int GLTex::alloc(Format format, int w, int h)
 {
-       // BIG COMMIT NOTE: Third parameter of glTexImage2D()
-       // is NOT number of components; it's internal format,
-       // which should be the same as format.
-
        // Determine the internal texture format.
+       const char *texFormat;
        switch (format) {
                case FMT_UNKNOWN:
                default:
                        // Unknown format.
                        dealloc();
                        return -EINVAL;
-#ifdef GL_HEADER_HAS_GL_1_2_PACKED_PIXELS
-               // TODO: Verify that OpenGL 1.2 is supported.
-               // TODO: GL_RGB5 seems to work as an internal format.
-               // https://www.opengl.org/registry/doc/glspec121_bookmarked.pdf
                case FMT_XRGB1555:
-                       // TODO: Store as GL_RGB5 internally?
+                       // 15-bit color requires OpenGL 1.2 or GL_APPLE_packed_pixels.
+                       // TODO: GL_RGB5 seems to work as an internal format.
+                       // https://www.opengl.org/registry/doc/glspec121_bookmarked.pdf
                        this->intformat = GL_RGBA;
                        this->format = GL_BGRA;
                        this->type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
+                       texFormat = "XRGB1555";
                        break;
                case FMT_RGB565:
+                       // 16-bit color requires OpenGL 1.2 or GL_APPLE_packed_pixels.
                        this->intformat = GL_RGB;
                        this->format = GL_RGB;
                        this->type = GL_UNSIGNED_SHORT_5_6_5;
+                       texFormat = "RGB565";
                        break;
-#else /* !GL_HEADER_HAS_GL_1_2_PACKED_PIXELS */
-               case FMT_XRGB1555:
-               case FMT_RGB565:
-                       // OpenGL 1.2 is required for 15-bit and 16-bit color.
-                       // NOTE: GL_EXT_packed_pixels could work for 15-bit.
-                       // NOTE: GL_APPLE_packed_pixels could work for 15-bit and 16-bit.
-                       // TODO: Error code?
-                       dealloc();
-                       return -EINVAL;
-#endif /* GL_HEADER_HAS_GL_1_2_PACKED_PIXELS */
                case FMT_XRGB8888:
                        // TODO: Verify that GL_BGRA is supported.
                        // Pretty much everything supports it,
@@ -119,6 +95,7 @@ int GLTex::alloc(Format format, int w, int h)
                        this->intformat = GL_RGBA;
                        this->format = GL_BGRA;
                        this->type = SDLGL_UNSIGNED_BYTE;
+                       texFormat = "XRGB8888";
                        break;
                case FMT_ALPHA8:
                        // TODO: Does GL_ALPHA8 work everywhere?
@@ -128,9 +105,46 @@ int GLTex::alloc(Format format, int w, int h)
                        // GL_ALPHA is single-component, so we probably
                        // shouldn't use SDLGL_UNSIGNED_BYTE.
                        this->type = GL_UNSIGNED_BYTE;
+                       texFormat = "ALPHA8";
                        break;
        }
 
+       // GL_BGRA isn't part of OpenGL 1.1.
+       // COMMIT NOTE: MSVC 2010 didn't complain when it had "this->format = GL_BGRA".
+       // Test again with /W4, and test newer versions?
+       if (this->format == GL_BGRA) {
+               // GL_BGRA requires either OpenGL 1.2 or GL_EXT_bgra.
+               if (!GLEW_VERSION_1_2 && !GLEW_EXT_bgra) {
+                       fprintf(stderr, "WARNING: OpenGL 1.2 is not supported, and GL_EXT_bgra is missing.\n"
+                               "Texture format %s will not work correctly.", texFormat);
+                       dealloc();
+                       this->intformat = 0;
+                       this->format = 0;
+                       this->type = 0;
+                       return -EINVAL;
+               }
+       }
+
+       // 15-bit/16-bit color isn't part of OpenGL 1.1.
+       if (this->type == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
+           this->type == GL_UNSIGNED_SHORT_5_6_5) {
+               // Both 15-bit and 16-bit color require either OpenGL 1.2 or
+               // the GL_APPLE_packed_pixels extension.
+               // GL_EXT_packed_pixels has GL_UNSIGNED_SHORT_5_5_5_1_EXT,
+               // but not GL_UNSIGNED_SHORT_1_5_5_5_REV, and it doesn't
+               // have any 565 formats at all.
+               // TODO: Regenerate GLEW to have GL_APPLE_packed_pixels.
+               if (!GLEW_VERSION_1_2 /*&& !GLEW_APPLE_packed_pixels*/) {
+                       fprintf(stderr, "WARNING: OpenGL 1.2 is not supported, and GL_APPLE_packed_pixels is missing.\n"
+                               "Texture format %s will not work correctly.\n", texFormat);
+                       dealloc();
+                       this->intformat = 0;
+                       this->format = 0;
+                       this->type = 0;
+                       return -EINVAL;
+               }
+       }
+
        // Create and initialize a GL texture.
        if (name == 0) {
                glGenTextures(1, &name);
index 16aaa99..2af3413 100644 (file)
 // C includes. (C++ namespace)
 #include <climits>
 
-// OpenGL
-#ifdef _WIN32
-#include <windows.h>
-#endif
-#include <GL/gl.h>
+// OpenGL (GLEW)
+#include <GL/glew.h>
 
 namespace GensSdl {
 
@@ -115,6 +112,7 @@ class GLTex
                inline double ratioH(void) const;
 
        protected:
+               // TODO: Move to GLTexPrivate?
                GLenum intformat;       // Internal format. (GL_RGB, GL_RGBA)
                GLenum format;          // Texture format. (GL_RGB, GL_BGRA)
                GLenum type;            // Texture type. (GL_UNSIGNED_BYTE, etc.)
@@ -122,6 +120,7 @@ class GLTex
        public:
                // TODO: Accessors.
                // TODO: Size type?
+               // TODO: Move to GLTexPrivate?
                int texW, texH;         // Texture size. (1x == 512x256 for pow2 textures.)
                int texVisW, texVisH;   // Texture visible size. (1x == 320x240)