GDraw_glfw trans ition to GDraw_sdl :) (#188)

Co-authored-by: Tropical <42101043+tropicaaal@users.noreply.github.com>
This commit is contained in:
JuiceyDev 2026-03-11 20:55:14 +01:00 committed by Tropical
parent b63f4171d7
commit 6fd48242ae
4 changed files with 120 additions and 122 deletions

View file

@ -1,45 +0,0 @@
#ifndef __RAD_INCLUDE_GDRAW_GLFW_H__
#define __RAD_INCLUDE_GDRAW_GLFW_H__
#include "../../../Windows64/Iggy/include/gdraw.h"
#include "../../../Windows64/Iggy/include/iggy.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum gdraw_gl_resourcetype
{
GDRAW_GL_RESOURCE_rendertarget,
GDRAW_GL_RESOURCE_texture,
GDRAW_GL_RESOURCE_vertexbuffer,
GDRAW_GL_RESOURCE__count,
} gdraw_gl_resourcetype;
struct IggyCustomDrawCallbackRegion;
extern int gdraw_GL_SetResourceLimits(gdraw_gl_resourcetype type, S32 num_handles, S32 num_bytes);
extern GDrawFunctions * gdraw_GL_CreateContext(S32 min_w, S32 min_h, S32 msaa_samples);
extern void gdraw_GL_DestroyContext(void);
extern void gdraw_GL_SetTileOrigin(S32 vx, S32 vy, unsigned int framebuffer); // framebuffer=FBO handle, or 0 for main frame buffer
extern void gdraw_GL_NoMoreGDrawThisFrame(void);
extern GDrawTexture *gdraw_GL_WrappedTextureCreate(S32 gl_texture_handle, S32 width, S32 height, int has_mipmaps);
extern void gdraw_GL_WrappedTextureChange(GDrawTexture *tex, S32 new_gl_texture_handle, S32 new_width, S32 new_height, int new_has_mipmaps);
extern void gdraw_GL_WrappedTextureDestroy(GDrawTexture *tex);
extern void gdraw_GL_BeginCustomDraw(struct IggyCustomDrawCallbackRegion *region, float *matrix);
extern void gdraw_GL_EndCustomDraw(struct IggyCustomDrawCallbackRegion *region);
extern void gdraw_GL_CalculateCustomDraw_4J(struct IggyCustomDrawCallbackRegion *region, float *matrix);
extern void gdraw_GL_BeginCustomDraw_4J(struct IggyCustomDrawCallbackRegion *region, float *matrix);
extern GDrawTexture * gdraw_GL_MakeTextureFromResource(unsigned char *resource_file, S32 resource_len, IggyFileTextureRaw *texture);
extern void gdraw_GL_DestroyTextureFromResource(GDrawTexture *tex);
#ifdef __cplusplus
}
#endif
#endif

View file

@ -1,12 +1,10 @@
// gdraw_glfw.c - Linux/GLFW port of gdraw_wgl.c
// Original: copyright 2011-2012 RAD Game Tools
// Port: Implements the Iggy graphics driver layer for GL on Linux via GLFW.
// Rewrite of gdraw_GLFW to gdraw_SDL
// I hope iggy gets fully implemented rrlllly quickly <3
#define GDRAW_ASSERTS
#include "../../../Windows64/Iggy/include/iggy.h"
#include "../../../Windows64/Iggy/include/gdraw.h"
#include "gdraw_glfw.h"
#include "gdraw_sdl.h"
#include <GL/gl.h>
#include <GL/glext.h>
@ -21,16 +19,37 @@
#define true 1
#define false 0
///////////////////////////////////////////////////////////////////////////////
//
// Extensions (we map to GL 2.0 function names for a uniform interface
// across platforms)
//
// NOTE: glActiveTexture and glCompressedTexImage2D are omitted here because
// on Linux they are core GL 1.3+ functions already declared in <GL/gl.h>.
// The shared code calls them by name and the real functions are used directly.
//
// Say hi to sdl
void *IggyGDrawMallocAnnotated(SINTa size, const char *file, int line)
{
(void)file; (void)line;
return malloc((size_t)size);
}
void IggyGDrawFree(void *ptr)
{
free(ptr);
}
void IggyGDrawSendWarning(Iggy *f, char const *message, ...)
{
(void)f;
va_list args;
va_start(args, message);
fprintf(stderr, "[Iggy GDraw Warning] ");
vfprintf(stderr, message, args);
fprintf(stderr, "\n");
va_end(args);
}
void IggyDiscardVertexBufferCallback(void *owner, void *buf)
{
(void)owner; (void)buf;
}
// glActiveTexture and glCompressedTexImage2D are core GL 1.3+ on Linux and
// are declared directly in <GL/gl.h>, so they are omitted from this list.
#define GDRAW_GL_EXTENSION_LIST \
/* identifier import procname */ \
/* GL_ARB_vertex_buffer_object */ \
@ -83,25 +102,25 @@ GLE(BlitFramebuffer, "BlitFramebufferEXT", BLITFRAM
GLE(RenderbufferStorageMultisample, "RenderbufferStorageMultisampleEXT",RENDERBUFFERSTORAGEMULTISAMPLEEXT) \
/* <end> */
#define gdraw_GLx_(id) gdraw_GL_##id
#define GDRAW_GLx_(id) GDRAW_GL_##id
#define GDRAW_SHADERS "gdraw_gl_shaders.inl"
#define gdraw_GLx_(id) gdraw_GL_##id
#define GDRAW_GLx_(id) GDRAW_GL_##id
#define GDRAW_SHADERS "gdraw_gl_shaders.inl"
// On Linux, GLhandleARB is void* (not GLuint) but the shader functions
// actually return/take GLuint values. Use GLuint as our handle type,
// matching the Mac pattern from gdraw_gl_shared.inl.
// On Linux, GLhandleARB is void* but shader functions use GLuint values.
// Use GLuint as our handle type, matching the Mac pattern from gdraw_gl_shared.inl.
#define GDrawGLProgram GLuint
typedef GLuint GLhandle;
typedef GLuint GLhandle;
typedef gdraw_gl_resourcetype gdraw_resourcetype;
// Extensions
// Declare extension function pointers
#define GLE(id, import, procname) static PFNGL##procname##PROC gl##id;
GDRAW_GL_EXTENSION_LIST
#undef GLE
static void load_extensions(void)
{
#define GLE(id, import, procname) gl##id = (PFNGL##procname##PROC) SDL_GL_GetProcAddress("gl" import);
#define GLE(id, import, procname) \
gl##id = (PFNGL##procname##PROC) SDL_GL_GetProcAddress("gl" import);
GDRAW_GL_EXTENSION_LIST
#undef GLE
}
@ -113,75 +132,68 @@ static void clear_renderstate_platform_specific(void)
static void error_msg_platform_specific(const char *msg)
{
fprintf(stderr, "[GDraw GL] %s\n", msg);
fprintf(stderr, "[GDraw SDL] %s\n", msg);
}
///////////////////////////////////////////////////////////////////////////////
//
// Shared code
//
#define GDRAW_MULTISAMPLING
// Override RR_BREAK() to avoid SIGTRAP from GL debug checks on Linux
// Suppress SIGTRAP from GL debug assertions on Linux
#ifdef RR_BREAK
#undef RR_BREAK
#endif
#define RR_BREAK() do { fprintf(stderr, "[GDraw] RR_BREAK suppressed (GL error)\n"); } while(0)
#define RR_BREAK() \
do { fprintf(stderr, "[GDraw] RR_BREAK suppressed (GL error)\n"); } while(0)
#include "../../../Windows64/Iggy/gdraw/gdraw_gl_shared.inl"
///////////////////////////////////////////////////////////////////////////////
//
// Initialization and platform-specific functionality
//
// Context creation and management
GDrawFunctions *gdraw_GL_CreateContext(S32 w, S32 h, S32 msaa_samples)
{
static const TextureFormatDesc tex_formats[] = {
{ IFT_FORMAT_rgba_8888, 1, 1, 4, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE },
{ IFT_FORMAT_rgba_4444_LE, 1, 1, 2, GL_RGBA4, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4 },
{ IFT_FORMAT_rgba_5551_LE, 1, 1, 2, GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1 },
{ IFT_FORMAT_la_88, 1, 1, 2, GL_LUMINANCE8_ALPHA8, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE },
{ IFT_FORMAT_la_44, 1, 1, 1, GL_LUMINANCE4_ALPHA4, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE },
{ IFT_FORMAT_i_8, 1, 1, 1, GL_INTENSITY8, GL_ALPHA, GL_UNSIGNED_BYTE },
{ IFT_FORMAT_i_4, 1, 1, 1, GL_INTENSITY4, GL_ALPHA, GL_UNSIGNED_BYTE },
{ IFT_FORMAT_l_8, 1, 1, 1, GL_LUMINANCE8, GL_LUMINANCE, GL_UNSIGNED_BYTE },
{ IFT_FORMAT_l_4, 1, 1, 1, GL_LUMINANCE4, GL_LUMINANCE, GL_UNSIGNED_BYTE },
{ IFT_FORMAT_DXT1, 4, 4, 8, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, 0, GL_UNSIGNED_BYTE },
{ IFT_FORMAT_DXT3, 4, 4, 16, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, 0, GL_UNSIGNED_BYTE },
{ IFT_FORMAT_DXT5, 4, 4, 16, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, 0, GL_UNSIGNED_BYTE },
{ 0, 0, 0, 0, 0, 0, 0 },
{ IFT_FORMAT_rgba_8888, 1, 1, 4, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE },
{ IFT_FORMAT_rgba_4444_LE, 1, 1, 2, GL_RGBA4, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4 },
{ IFT_FORMAT_rgba_5551_LE, 1, 1, 2, GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1 },
{ IFT_FORMAT_la_88, 1, 1, 2, GL_LUMINANCE8_ALPHA8, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE },
{ IFT_FORMAT_la_44, 1, 1, 1, GL_LUMINANCE4_ALPHA4, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE },
{ IFT_FORMAT_i_8, 1, 1, 1, GL_INTENSITY8, GL_ALPHA, GL_UNSIGNED_BYTE },
{ IFT_FORMAT_i_4, 1, 1, 1, GL_INTENSITY4, GL_ALPHA, GL_UNSIGNED_BYTE },
{ IFT_FORMAT_l_8, 1, 1, 1, GL_LUMINANCE8, GL_LUMINANCE, GL_UNSIGNED_BYTE },
{ IFT_FORMAT_l_4, 1, 1, 1, GL_LUMINANCE4, GL_LUMINANCE, GL_UNSIGNED_BYTE },
{ IFT_FORMAT_DXT1, 4, 4, 8, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, 0, GL_UNSIGNED_BYTE },
{ IFT_FORMAT_DXT3, 4, 4, 16, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, 0, GL_UNSIGNED_BYTE },
{ IFT_FORMAT_DXT5, 4, 4, 16, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, 0, GL_UNSIGNED_BYTE },
{ 0, 0, 0, 0, 0, 0, 0 },
};
GDrawFunctions *funcs;
const char *s;
GLint n;
// check for the extensions we need
// A current SDL2 GL context must be active before calling this, if it doesn't exist, just throw an warning-
s = (const char *) glGetString(GL_EXTENSIONS);
if (s == NULL) {
fprintf(stderr, "[GDraw GL] glGetString(GL_EXTENSIONS) returned NULL - GL context not current?\n");
if (!s) {
fprintf(stderr, "[GDraw SDL] glGetString(GL_EXTENSIONS) returned NULL - "
"SDL GL context not current?\n");
assert(s != NULL);
return NULL;
}
// check for the extensions we won't work without
if (!hasext(s, "GL_ARB_multitexture") ||
!hasext(s, "GL_ARB_texture_compression") ||
!hasext(s, "GL_ARB_texture_mirrored_repeat") ||
// Verify required extensions
if (!hasext(s, "GL_ARB_multitexture") ||
!hasext(s, "GL_ARB_texture_compression") ||
!hasext(s, "GL_ARB_texture_mirrored_repeat") ||
!hasext(s, "GL_ARB_texture_non_power_of_two") ||
!hasext(s, "GL_ARB_vertex_buffer_object") ||
!hasext(s, "GL_EXT_framebuffer_object") ||
!hasext(s, "GL_ARB_shader_objects") ||
!hasext(s, "GL_ARB_vertex_shader") ||
!hasext(s, "GL_ARB_vertex_buffer_object") ||
!hasext(s, "GL_EXT_framebuffer_object") ||
!hasext(s, "GL_ARB_shader_objects") ||
!hasext(s, "GL_ARB_vertex_shader") ||
!hasext(s, "GL_ARB_fragment_shader"))
{
fprintf(stderr, "[GDraw GL] Required GL extensions not available\n");
fprintf(stderr, "[GDraw SDL] Required GL extensions not available\n");
return NULL;
}
// if user requests multisampling and HW doesn't support it, bail
if (!hasext(s, "GL_EXT_framebuffer_multisample") && msaa_samples > 1)
return NULL;
@ -192,17 +204,16 @@ GDrawFunctions *gdraw_GL_CreateContext(S32 w, S32 h, S32 msaa_samples)
gdraw->tex_formats = tex_formats;
// check for optional extensions
gdraw->has_mapbuffer = true; // part of core VBO extension on regular GL
gdraw->has_depth24 = true; // we just assume.
gdraw->has_texture_max_level = true; // core on regular GL
gdraw->has_mapbuffer = true; // core in ARB_vertex_buffer_object
gdraw->has_depth24 = true;
gdraw->has_texture_max_level = true; // core GL
if (hasext(s, "GL_EXT_packed_depth_stencil")) gdraw->has_packed_depth_stencil = true;
if (hasext(s, "GL_EXT_packed_depth_stencil"))
gdraw->has_packed_depth_stencil = true;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &n);
gdraw->has_conditional_non_power_of_two = n < 8192;
gdraw->has_conditional_non_power_of_two = (n < 8192);
// clamp number of multisampling levels to max supported
if (msaa_samples > 1) {
glGetIntegerv(GL_MAX_SAMPLES, &n);
gdraw->multisampling = RR_MIN(msaa_samples, n);
@ -210,18 +221,13 @@ GDrawFunctions *gdraw_GL_CreateContext(S32 w, S32 h, S32 msaa_samples)
opengl_check();
fprintf(stderr, "[GDraw GL] Context created successfully (%dx%d, msaa=%d)\n", w, h, msaa_samples);
fprintf(stderr, "[GDraw SDL] Context created successfully (%dx%d, msaa=%d)\n",
w, h, msaa_samples);
return funcs;
}
///////////////////////////////////////////////////////////////////////////////
//
// 4J-specific custom draw functions
//
void gdraw_GL_BeginCustomDraw_4J(IggyCustomDrawCallbackRegion *region, F32 *matrix)
{
// Same as BeginCustomDraw but uses different depth param
clear_renderstate();
gdraw_GetObjectSpaceMatrix(matrix, region->o2w, gdraw->projection, depth_from_id(0), 1);
}
@ -229,4 +235,4 @@ void gdraw_GL_BeginCustomDraw_4J(IggyCustomDrawCallbackRegion *region, F32 *matr
void gdraw_GL_CalculateCustomDraw_4J(IggyCustomDrawCallbackRegion *region, F32 *matrix)
{
gdraw_GetObjectSpaceMatrix(matrix, region->o2w, gdraw->projection, 0.0f, 0);
}
}

View file

@ -0,0 +1,37 @@
#ifndef __RAD_INCLUDE_GDRAW_SDL_H__
#define __RAD_INCLUDE_GDRAW_SDL_H__
#include "../../../Windows64/Iggy/include/gdraw.h"
#include "../../../Windows64/Iggy/include/iggy.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum gdraw_gl_resourcetype
{
GDRAW_GL_RESOURCE_rendertarget,
GDRAW_GL_RESOURCE_texture,
GDRAW_GL_RESOURCE_vertexbuffer,
GDRAW_GL_RESOURCE__count,
} gdraw_gl_resourcetype;
struct IggyCustomDrawCallbackRegion;
extern int gdraw_GL_SetResourceLimits(gdraw_gl_resourcetype type, S32 num_handles, S32 num_bytes);
extern GDrawFunctions *gdraw_GL_CreateContext(S32 min_w, S32 min_h, S32 msaa_samples);
extern void gdraw_GL_DestroyContext(void);
extern void gdraw_GL_SetTileOrigin(S32 vx, S32 vy, unsigned int framebuffer);
extern void gdraw_GL_NoMoreGDrawThisFrame(void);
extern GDrawTexture *gdraw_GL_WrappedTextureCreate(S32 gl_texture_handle, S32 width, S32 height, int has_mipmaps);
extern void gdraw_GL_WrappedTextureChange(GDrawTexture *tex, S32 new_gl_texture_handle, S32 new_width, S32 new_height, int new_has_mipmaps);
extern void gdraw_GL_WrappedTextureDestroy(GDrawTexture *tex);
extern void gdraw_GL_BeginCustomDraw(struct IggyCustomDrawCallbackRegion *region, float *matrix);
extern void gdraw_GL_EndCustomDraw(struct IggyCustomDrawCallbackRegion *region);
extern void gdraw_GL_CalculateCustomDraw_4J(struct IggyCustomDrawCallbackRegion *region, float *matrix);
extern void gdraw_GL_BeginCustomDraw_4J(struct IggyCustomDrawCallbackRegion *region, float *matrix);
extern GDrawTexture *gdraw_GL_MakeTextureFromResource(unsigned char *resource_file, S32 resource_len, IggyFileTextureRaw *texture);
extern void gdraw_GL_DestroyTextureFromResource(GDrawTexture *tex);
#ifdef __cplusplus
}
#endif
#endif // __RAD_INCLUDE_GDRAW_SDL_H__

View file

@ -6,7 +6,7 @@
#include "../../Textures/Textures.h"
// GDraw GL backend for Linux
#include "Iggy/gdraw/gdraw_glfw.h"
#include "Iggy/gdraw/gdraw_sdl.h"
#define _ENABLEIGGY