From 8498dfa00472431a6dd8a2ec588f0792a9e435dc Mon Sep 17 00:00:00 2001 From: frank Date: Wed, 24 Apr 2024 15:01:17 -0400 Subject: [PATCH] - consolidate GL includes into one header - build flags for MacOS - replace glTexStorage2D with more compatible glTexImage2D --- src/Attributes.hpp | 13 +++---------- src/GLObject.hpp | 11 +---------- src/Game.cpp | 22 ++++++++++++++++++---- src/Game.hpp | 8 +------- src/Log.cpp | 4 ++-- src/Log.hpp | 17 ++++------------- src/Model.hpp | 13 +++---------- src/Texture.cpp | 7 ++++++- src/gl.h | 12 ++++++++++++ 9 files changed, 50 insertions(+), 57 deletions(-) create mode 100644 src/gl.h diff --git a/src/Attributes.hpp b/src/Attributes.hpp index 9a5be14..723f9de 100644 --- a/src/Attributes.hpp +++ b/src/Attributes.hpp @@ -86,16 +86,6 @@ #pragma once -/* include Open GL */ -#if defined(__EMSCRIPTEN__) -#include -#elif defined(__ANDROID__) || defined(ANDROID) -#include -#include -#else -#include "glew/glew.h" -#endif - #include #include #include @@ -103,7 +93,10 @@ #include #include #include + #include "glm/glm.hpp" + +#include "gl.h" #include "Log.hpp" #include "extension.hpp" diff --git a/src/GLObject.hpp b/src/GLObject.hpp index 5912538..6d86829 100644 --- a/src/GLObject.hpp +++ b/src/GLObject.hpp @@ -33,16 +33,7 @@ the base class, like the VBO class. #include #include -/* include Open GL */ -#if defined(__EMSCRIPTEN__) -#include -#elif defined(__ANDROID__) || defined(ANDROID) -#include -#include -#else -#include "glew/glew.h" -#endif - +#include "gl.h" #include "Log.hpp" namespace sb diff --git a/src/Game.cpp b/src/Game.cpp index 059c446..f25a597 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -12,6 +12,15 @@ Game::Game(std::initializer_list configuration_merge) { + #include + #include + + /* Changing directory is necessary for loading external resources in a macOS app bundle. This may be useful on other platforms + * too, so it may make more sense to be configurable. */ +#if defined(__MACOS__) + fs::current_path(SDL_GetBasePath()); +#endif + /* Set custom log function that prints to stdout/stderr and to file if enabled. Temporarily set to DEBUG priority before loading * user setting. */ SDL_LogSetOutputFunction(&Game::sdl_log_override, this); @@ -108,8 +117,8 @@ Game::Game(std::initializer_list configuration_merge) } log_message = std::ostringstream(); - /* Android does not use GLEW */ -#if !defined(__ANDROID__) && !defined(ANDROID) + /* Android and macOS do not use GLEW */ +#if !defined(__ANDROID__) && !defined(ANDROID) && !defined(__MACOS__) log_message << "GLEW " << glewGetString(GLEW_VERSION); #endif @@ -132,6 +141,8 @@ Game::Game(std::initializer_list configuration_merge) /* Use compatibility mode on Windows because it seemed to be required on one version of Windows tested */ #if defined(__MINGW32__) SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY); +#elif defined(__MACOS__) + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); #endif /* Create a window with dimensions set in the config, centered, and flagged to be usable in OpenGL context */ @@ -174,8 +185,8 @@ Game::Game(std::initializer_list configuration_merge) sb::Log::log("Setting vysnc is not supported"); } - /* Initialize GLEW for GL function discovery on all platforms except Android */ -#if !defined(__ANDROID__) && !defined(ANDROID) + /* Initialize GLEW for GL function discovery on all platforms except Android and macOS */ +#if !defined(__ANDROID__) && !defined(ANDROID) && !defined(__MACOS__) GLenum error = glewInit(); if (error != GLEW_OK) { @@ -427,9 +438,12 @@ void Game::log_gl_properties() const sb::Log::log(message); + /* This block fails on macOS */ +#if !defined(__MACOS__) std::ostringstream debug_message; debug_message << "OpenGL extensions: " << glGetString(GL_EXTENSIONS); sb::Log::log(message, sb::Log::DEBUG); +#endif } void Game::log_surface_format(SDL_Surface* surface, std::string preface) diff --git a/src/Game.hpp b/src/Game.hpp index 07afe16..a151dcb 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -23,22 +23,16 @@ #include "SDL_mixer.h" #include "SDL_ttf.h" -#define GL_GLEXT_PROTOTYPES #if defined(__EMSCRIPTEN__) #include #include -#include -#elif defined(__ANDROID__) || defined(ANDROID) -#include -#include -#else -#include "glew/glew.h" #endif #if defined(__ANDROID__) || defined(ANDROID) #include #endif +#include "gl.h" #include "Node.hpp" #include "Input.hpp" #include "Display.hpp" diff --git a/src/Log.cpp b/src/Log.cpp index 6d92ffe..1a07460 100644 --- a/src/Log.cpp +++ b/src/Log.cpp @@ -53,8 +53,8 @@ bool sb::Log::gl_errors(const std::string& heading) message << "GL_OUT_OF_MEMORY, there is not enough memory left to execute the command"; } - /* The following error codes aren't available in Open GL ES */ -#if !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !defined(ANDROID) + /* The following error codes aren't available in Open GL ES or on macOS */ +#if !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !defined(ANDROID) && !defined(__MACOS__) else if (error == GL_STACK_UNDERFLOW) { message << "GL_STACK_UNDERFLOW, an attempt has been made to perform an operation that would " << diff --git a/src/Log.hpp b/src/Log.hpp index fa7cbb9..48a5a98 100644 --- a/src/Log.hpp +++ b/src/Log.hpp @@ -6,9 +6,7 @@ | ~~~~~~~~~~~~ | +------------------------------------------------------+ | SPACE ~~~~~ | / | ~~~~~~~ BOX |/ -+-------------*/ - -/*! @file ++--------------+ Log messages of specified priority through the SDL logging method, which is overridden in sb::Game to write to stdout, file, or both, depending on the user's configuration settings. @@ -17,21 +15,14 @@ sb::Game to write to stdout, file, or both, depending on the user's configuratio #pragma once -/* include Open GL */ -#if defined(__EMSCRIPTEN__) -#include -#elif defined(__ANDROID__) && defined(ANDROID) -#include -#include -#else -#include "glew/glew.h" -#endif - #include #include #include #include +/* Include Open GL so GL errors can be logged */ +#include "gl.h" + namespace sb { diff --git a/src/Model.hpp b/src/Model.hpp index 0b0c1c5..7eb740e 100644 --- a/src/Model.hpp +++ b/src/Model.hpp @@ -10,23 +10,16 @@ #pragma once -/* GL functions */ -#if defined(__EMSCRIPTEN__) -#include -#elif defined(__ANDROID__) || defined(ANDROID) -#include -#include -#else -#include "glew/glew.h" -#endif - #include #include #include #include #include #include + #include "glm/glm.hpp" + +#include "gl.h" #include "Attributes.hpp" #include "Texture.hpp" #include "Carousel.hpp" diff --git a/src/Texture.cpp b/src/Texture.cpp index d6a37f1..3f436f2 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -42,7 +42,8 @@ void Texture::generate(glm::vec2 size, GLenum format, std::optional filte { generate(); bind(); - glTexStorage2D(GL_TEXTURE_2D, 1, format, size.x, size.y); + /* Use nullptr because data will be loaded later with glTexSubImage2d */ + glTexImage2D(GL_TEXTURE_2D, 0, format, size.x, size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); } else { @@ -152,7 +153,11 @@ void Texture::load(SDL_Surface* surface) message << "Loading image from SDL surface (" << surface->w << "×" << surface->h << ", " << SDL_GetPixelFormatName(surface->format->format) << ")"; sb::Log::log(message, sb::Log::VERBOSE); +#if defined(__MACOS__) + load(surface->pixels, {surface->w, surface->h}, GL_BGRA, GL_UNSIGNED_BYTE); +#else load(surface->pixels, {surface->w, surface->h}, GL_RGBA, GL_UNSIGNED_BYTE); +#endif } else { diff --git a/src/gl.h b/src/gl.h new file mode 100644 index 0000000..a3eca8d --- /dev/null +++ b/src/gl.h @@ -0,0 +1,12 @@ +#define GL_GLEXT_PROTOTYPES +#if defined(__EMSCRIPTEN__) +#include +#elif defined(__ANDROID__) && defined(ANDROID) +#include +#include +#elif defined(__MACOS__) +#define GL_SILENCE_DEPRECATION +#include +#else +#include "glew/glew.h" +#endif