- consolidate GL includes into one header

- build flags for MacOS
- replace glTexStorage2D with more compatible glTexImage2D
This commit is contained in:
ohsqueezy 2024-04-24 15:01:17 -04:00
parent 87a946a61e
commit 8498dfa004
9 changed files with 50 additions and 57 deletions

View File

@ -86,16 +86,6 @@
#pragma once
/* include Open GL */
#if defined(__EMSCRIPTEN__)
#include <GL/glew.h>
#elif defined(__ANDROID__) || defined(ANDROID)
#include <GLES3/gl3.h>
#include <GLES3/gl3ext.h>
#else
#include "glew/glew.h"
#endif
#include <ostream>
#include <vector>
#include <variant>
@ -103,7 +93,10 @@
#include <type_traits>
#include <utility>
#include <exception>
#include "glm/glm.hpp"
#include "gl.h"
#include "Log.hpp"
#include "extension.hpp"

View File

@ -33,16 +33,7 @@ the base class, like the VBO class.
#include <functional>
#include <stdexcept>
/* include Open GL */
#if defined(__EMSCRIPTEN__)
#include <GL/glew.h>
#elif defined(__ANDROID__) || defined(ANDROID)
#include <GLES3/gl3.h>
#include <GLES3/gl3ext.h>
#else
#include "glew/glew.h"
#endif
#include "gl.h"
#include "Log.hpp"
namespace sb

View File

@ -12,6 +12,15 @@
Game::Game(std::initializer_list<std::string> configuration_merge)
{
#include <cstdio>
#include <cstdlib>
/* 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<std::string> 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<std::string> 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<std::string> 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)

View File

@ -23,22 +23,16 @@
#include "SDL_mixer.h"
#include "SDL_ttf.h"
#define GL_GLEXT_PROTOTYPES
#if defined(__EMSCRIPTEN__)
#include <emscripten.h>
#include <emscripten/html5.h>
#include <GL/glew.h>
#elif defined(__ANDROID__) || defined(ANDROID)
#include <GLES3/gl3.h>
#include <GLES3/gl3ext.h>
#else
#include "glew/glew.h"
#endif
#if defined(__ANDROID__) || defined(ANDROID)
#include <android/log.h>
#endif
#include "gl.h"
#include "Node.hpp"
#include "Input.hpp"
#include "Display.hpp"

View File

@ -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 " <<

View File

@ -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 <GL/glew.h>
#elif defined(__ANDROID__) && defined(ANDROID)
#include <GLES3/gl3.h>
#include <GLES3/gl3ext.h>
#else
#include "glew/glew.h"
#endif
#include <iostream>
#include <sstream>
#include <functional>
#include <SDL.h>
/* Include Open GL so GL errors can be logged */
#include "gl.h"
namespace sb
{

View File

@ -10,23 +10,16 @@
#pragma once
/* GL functions */
#if defined(__EMSCRIPTEN__)
#include <GL/glew.h>
#elif defined(__ANDROID__) || defined(ANDROID)
#include <GLES3/gl3.h>
#include <GLES3/gl3ext.h>
#else
#include "glew/glew.h"
#endif
#include <iostream>
#include <string>
#include <map>
#include <memory>
#include <iterator>
#include <stdexcept>
#include "glm/glm.hpp"
#include "gl.h"
#include "Attributes.hpp"
#include "Texture.hpp"
#include "Carousel.hpp"

View File

@ -42,7 +42,8 @@ void Texture::generate(glm::vec2 size, GLenum format, std::optional<GLint> 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
{

12
src/gl.h Normal file
View File

@ -0,0 +1,12 @@
#define GL_GLEXT_PROTOTYPES
#if defined(__EMSCRIPTEN__)
#include <GL/glew.h>
#elif defined(__ANDROID__) && defined(ANDROID)
#include <GLES3/gl3.h>
#include <GLES3/gl3ext.h>
#elif defined(__MACOS__)
#define GL_SILENCE_DEPRECATION
#include <OpenGL/gl3.h>
#else
#include "glew/glew.h"
#endif