From a399023cca818ad58005220a62b7dedf0727b71f Mon Sep 17 00:00:00 2001 From: Tropical <42101043+tropicaaal@users.noreply.github.com> Date: Fri, 10 Apr 2026 16:27:00 -0700 Subject: [PATCH] refactor: switch to glew for OpenGL loading --- meson.build | 2 +- subprojects/glew.wrap | 12 +++ .../UIScene_CreateWorldMenu.cpp | 7 -- targets/platform/renderer/gl/GLRenderer.cpp | 34 -------- targets/platform/renderer/gl/gl3_loader.h | 37 ++++----- targets/platform/renderer/gl/gl_compat.h | 2 +- targets/platform/renderer/meson.build | 2 +- targets/platform/stubs.h | 3 +- targets/platform/thread/C4JThread.cpp | 78 ++++++++++--------- 9 files changed, 73 insertions(+), 104 deletions(-) create mode 100644 subprojects/glew.wrap diff --git a/meson.build b/meson.build index 620df8762..0e9f62b38 100644 --- a/meson.build +++ b/meson.build @@ -22,7 +22,7 @@ python = pymod.find_installation('python3', required: true) cc = meson.get_compiler('cpp') -global_cpp_args = ['-Wshift-count-overflow', '-pipe'] +global_cpp_args = [] global_cpp_defs = [ '-DSPLIT_SAVES', '-D_LARGE_WORLDS', diff --git a/subprojects/glew.wrap b/subprojects/glew.wrap new file mode 100644 index 000000000..390dc9a33 --- /dev/null +++ b/subprojects/glew.wrap @@ -0,0 +1,12 @@ +[wrap-file] +directory = glew-2.2.0 +source_url = http://downloads.sourceforge.net/glew/glew-2.2.0.tgz +source_filename = glew-2.2.0.tgz +source_hash = d4fc82893cfb00109578d0a1a2337fb8ca335b3ceccf97b97e5cc7f08e4353e1 +patch_filename = glew_2.2.0-2_patch.zip +patch_url = https://wrapdb.mesonbuild.com/v2/glew_2.2.0-2/get_patch +patch_hash = df7bc80456da53f83e93e89ca5035d04cdff19a836c956887a684b2bee16eb9b +wrapdb_version = 2.2.0-2 + +[provide] +glew = glew_dep diff --git a/targets/app/common/UI/Scenes/Frontend Menu screens/UIScene_CreateWorldMenu.cpp b/targets/app/common/UI/Scenes/Frontend Menu screens/UIScene_CreateWorldMenu.cpp index 6fb96d952..9c2fd82f9 100644 --- a/targets/app/common/UI/Scenes/Frontend Menu screens/UIScene_CreateWorldMenu.cpp +++ b/targets/app/common/UI/Scenes/Frontend Menu screens/UIScene_CreateWorldMenu.cpp @@ -38,13 +38,6 @@ #include "strings.h" #include "util/StringHelpers.h" -#if defined(_WINDOWS64) - -#include - -#include "../../../../../Windows64/Resource.h" -#endif - #define GAME_CREATE_ONLINE_TIMER_ID 0 #define GAME_CREATE_ONLINE_TIMER_TIME 100 diff --git a/targets/platform/renderer/gl/GLRenderer.cpp b/targets/platform/renderer/gl/GLRenderer.cpp index 3a509de6c..1e1ef038d 100644 --- a/targets/platform/renderer/gl/GLRenderer.cpp +++ b/targets/platform/renderer/gl/GLRenderer.cpp @@ -1403,40 +1403,6 @@ void glDeleteTextures_4J(int n, const unsigned int* textures) { ::glDeleteTextures(n, textures); } -// c hooks -#undef glFogfv -#undef glLightfv -#undef glLightModelfv -#undef glShadeModel -#undef glColorMaterial -#undef glNormal3f - -extern "C" { -void glFogfv(GLenum pname, const GLfloat* params) { - if (pname == 0x0B66) - PlatformRenderer.StateSetFogColour(params[0], params[1], params[2]); -} -void glLightfv(GLenum light, GLenum pname, const GLfloat* params) { - if (pname == 0x1203) - PlatformRenderer.StateSetLightDirection( - light == 0x4000 ? 0 : 1, params[0], params[1], params[2]); - else if (pname == 0x1200) - PlatformRenderer.StateSetLightAmbientColour(params[0], params[1], - params[2]); - else if (pname == 0x1201) - PlatformRenderer.StateSetLightColour(light == 0x4000 ? 0 : 1, params[0], - params[1], params[2]); -} -void glLightModelfv(GLenum pname, const GLfloat* params) { - if (pname == 0x0B53) - PlatformRenderer.StateSetLightAmbientColour(params[0], params[1], - params[2]); -} -void glShadeModel(GLenum) {} -void glColorMaterial(GLenum, GLenum) {} -void glNormal3f(GLfloat, GLfloat, GLfloat) {} -} - // MARK: LinuxStubs #ifdef GLES diff --git a/targets/platform/renderer/gl/gl3_loader.h b/targets/platform/renderer/gl/gl3_loader.h index bb03b3aad..0fcc9d096 100644 --- a/targets/platform/renderer/gl/gl3_loader.h +++ b/targets/platform/renderer/gl/gl3_loader.h @@ -2,18 +2,16 @@ // windows hack: Windows SDK OpenGL headers include WINGDIAPI in their declarations, // which can only be found in the Windows API -#ifdef _WIN32 +#if defined(_WIN32) #define WIN32_LEAN_AND_MEAN +#define NOMINMAX #include #endif -#ifndef GL_GLEXT_PROTOTYPES -#define GL_GLEXT_PROTOTYPES 1 -#endif -#include -// #include +#include #include + #ifndef GL_ARRAY_BUFFER #define GL_ARRAY_BUFFER 0x8892 #endif @@ -32,24 +30,21 @@ #ifndef GL_QUADS #define GL_QUADS 0x0007 #endif + static inline bool gl3_load() { - const char* ver = (const char*)glGetString(GL_VERSION); - if (!ver) { - fprintf(stderr, "[gl3_loader] ERROR: No active GL context found.\n"); + GLenum err = glewInit(); + if (err != GLEW_OK) { + fprintf(stderr, "[gl_loader] ERROR: glewInit failed: %s\n", + glewGetErrorString(err)); return false; } - int major = 0, minor = 0; - if (sscanf(ver, "%d.%d", &major, &minor) >= 2) { - if (major < 3 || (major == 3 && minor < 3)) { - fprintf(stderr, - "[gl3_loader] ERROR: Need GL 3.3, but system provides %s\n", - ver); - return false; - } + + if (!GLEW_VERSION_3_3) { + fprintf(stderr, "[gl_loader] ERROR: Need GL 3.3, not supported.\n"); + return false; } - fprintf( - stderr, - "[gl3_loader] GL Version: %s, it's all okay!! until android support.\n", - ver); + + fprintf(stderr, "[gl_loader] GL %s loaded successfully.\n", + (const char*)glewGetString(GLEW_VERSION)); return true; } \ No newline at end of file diff --git a/targets/platform/renderer/gl/gl_compat.h b/targets/platform/renderer/gl/gl_compat.h index df64fed1a..658c6cd6b 100644 --- a/targets/platform/renderer/gl/gl_compat.h +++ b/targets/platform/renderer/gl/gl_compat.h @@ -14,7 +14,7 @@ // rendering files compiling without dragging the concrete GLRenderer // class into every minecraft TU. -#include "gl3_loader.h" +// #include "gl3_loader.h" // NOTE: gl3_loader.h must be included before these two #include diff --git a/targets/platform/renderer/meson.build b/targets/platform/renderer/meson.build index cd9998ab1..5622bd7b9 100644 --- a/targets/platform/renderer/meson.build +++ b/targets/platform/renderer/meson.build @@ -10,7 +10,7 @@ if get_option('renderer') == 'gles' platform_renderer_gl_dependencies += dependency('glesv2') platform_renderer_gl_defs = ['-DGLES'] else - platform_renderer_gl_dependencies += dependency('gl') + platform_renderer_gl_dependencies += [dependency('gl'), dependency('glew')] platform_renderer_gl_defs = [] endif diff --git a/targets/platform/stubs.h b/targets/platform/stubs.h index 49dd2a7e9..0c4c87021 100644 --- a/targets/platform/stubs.h +++ b/targets/platform/stubs.h @@ -2,8 +2,9 @@ // windows hack: Windows SDK OpenGL headers include WINGDIAPI in their declarations, // which can only be found in the Windows API -#ifdef _WIN32 +#if defined(_WIN32) #define WIN32_LEAN_AND_MEAN +#define NOMINMAX #include #endif diff --git a/targets/platform/thread/C4JThread.cpp b/targets/platform/thread/C4JThread.cpp index c253cf7c9..36067b5d1 100644 --- a/targets/platform/thread/C4JThread.cpp +++ b/targets/platform/thread/C4JThread.cpp @@ -15,7 +15,9 @@ #include #if defined(_WIN32) -#include +#define WIN32_LEAN_AND_MEAN +#define NOMINMAX +#include #endif #if defined(__linux__) @@ -82,46 +84,46 @@ std::int64_t getNativeThreadId() { void setThreadNamePlatform([[maybe_unused]] std::uint32_t threadId, [[maybe_unused]] const char* name) { -#if defined(_WIN32) - // Try modern API first (Windows 10 1607+). - if (threadId == static_cast(-1) || - threadId == ::GetCurrentThreadId()) { - using SetThreadDescriptionFn = int32_t(WINAPI*)(void*, PCWSTR); - const HMODULE kernel = ::GetModuleHandleW("Kernel32.dll"); - if (kernel) { - const auto fn = reinterpret_cast( - ::GetProcAddress(kernel, "SetThreadDescription")); - if (fn) { - char wide[64]; - const auto n = std::strncpy( - wide, name, (sizeof(wide) / sizeof(wide[0])) - 1); - if (n != static_cast(-1)) { - wide[n] = '\0'; - (void)fn(::GetCurrentThread(), wide); - return; - } - } - } - } +// #if defined(_WIN32) +// // Try modern API first (Windows 10 1607+). +// if (threadId == static_cast(-1) || +// threadId == ::GetCurrentThreadId()) { +// using SetThreadDescriptionFn = int32_t(WINAPI*)(void*, PCWSTR); +// const HMODULE kernel = ::GetModuleHandleA("Kernel32.dll"); +// if (kernel) { +// const auto fn = reinterpret_cast( +// ::GetProcAddress(kernel, "SetThreadDescription")); +// if (fn) { +// char wide[64]; +// const auto n = std::strncpy( +// wide, name, (sizeof(wide) / sizeof(wide[0])) - 1); +// if (n != static_cast(-1)) { +// wide[n] = '\0'; +// (void)fn(::GetCurrentThread(), wide); +// return; +// } +// } +// } +// } - // Legacy fallback: raise exception 0x406D1388 for older MSVC debuggers. -#pragma pack(push, 8) - struct THREADNAME_INFO { - std::uint32_t dwType; - const char* szName; - std::uint32_t dwThreadID; - std::uint32_t dwFlags; - }; -#pragma pack(pop) +// // Legacy fallback: raise exception 0x406D1388 for older MSVC debuggers. +// #pragma pack(push, 8) +// struct THREADNAME_INFO { +// std::uint32_t dwType; +// const char* szName; +// std::uint32_t dwThreadID; +// std::uint32_t dwFlags; +// }; +// #pragma pack(pop) - THREADNAME_INFO info{0x1000, name, threadId, 0}; - __try { - ::RaiseException(0x406D1388, 0, sizeof(info) / sizeof(uintptr_t), - reinterpret_cast(&info)); - } __except (EXCEPTION_EXECUTE_HANDLER) { - } +// THREADNAME_INFO info{0x1000, name, threadId, 0}; +// __try { +// ::RaiseException(0x406D1388, 0, sizeof(info) / sizeof(uintptr_t), +// reinterpret_cast(&info)); +// } __except (EXCEPTION_EXECUTE_HANDLER) { +// } -#elif defined(__linux__) +#if defined(__linux__) // pthread_setname_np limit: 16 chars including null terminator. char truncated[16]; std::snprintf(truncated, sizeof(truncated), "%s", name);