Skip to content

Commit

Permalink
update osmesa and opengl linking (x11)
Browse files Browse the repository at this point in the history
  • Loading branch information
ColleagueRiley committed Jan 9, 2025
1 parent 09f8b93 commit 81065f7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 53 deletions.
20 changes: 14 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ CFLAGS =

DX11_LIBS = -static -lgdi32 -ldxgi -ld3d11 -luuid -ld3dcompiler
VULKAN_LIBS = -lgdi32 -I $(VULKAN_SDK)\Include -L $(VULKAN_SDK)\Lib -lvulkan-1
LIBS := -static -lgdi32 -lopengl32 -ggdb
LIBS := -static -lgdi32 -ggdb
LINK_GL1 = -lopengl32
EXT = .exe
LIB_EXT = .dll

Expand All @@ -29,20 +30,23 @@ detected_OS = windows

OBJ_FILE = .o


# not using a cross compiler
ifeq (,$(filter $(CC),x86_64-w64-mingw32-gcc i686-w64-mingw32-gcc x86_64-w64-mingw32-g++ /opt/msvc/bin/x64/cl.exe /opt/msvc/bin/x86/cl.exe))
detected_OS := $(shell uname 2>/dev/null || echo Unknown)

ifeq ($(detected_OS),Darwin) # Mac OS X
LIBS := -framework Cocoa -framework OpenGL -framework IOKit
LINK_GL1 = -framework OpenGL
VULKAN_LIBS =
EXT =
LIB_EXT = .dylib
OS_DIR = /
NO_VULKAN = 1
endif
ifeq ($(detected_OS),Linux)
LIBS := -lXrandr -lX11 -lGL -ldl -lpthread
LINK_GL1 = -lGL
LIBS := -lXrandr -lX11 -ldl -lpthread
VULKAN_LIBS = -lX11 -lXrandr -ldl -lpthread -lvulkan
EXT =
LIB_EXT = .so
Expand All @@ -62,7 +66,6 @@ ifeq ($(RGFW_WAYLAND),1)
LIBS = -D RGFW_WAYLAND relative-pointer-unstable-v1-client-protocol.c xdg-decoration-unstable-v1.c xdg-shell.c -lwayland-cursor -lwayland-client -lEGL -lxkbcommon -lGL -lwayland-egl
endif

LINK_GL1 =
LINK_GL3 =
LINK_GL2 =

Expand Down Expand Up @@ -90,13 +93,13 @@ endif
EXAMPLE_OUTPUTS = \
examples/basic/basic \
examples/buffer/buffer \
examples/silk/silk \
examples/events/events \
examples/callbacks/callbacks


EXAMPLE_OUTPUTS_CUSTOM = \
examples/gamepad/gamepad \
examples/silk/silk \
examples/first-person-camera/camera \
examples/microui_demo/microui_demo \
examples/gl33/gl33 \
Expand All @@ -122,7 +125,7 @@ endif

examples/gles2/gles2: examples/gles2/gles2.c RGFW.h
ifneq ($(NO_GLES), 1)
$(CC) $(CFLAGS) -I. $< $(LIBS) $(LINK_GL2) -lEGL -o $@$(EXT)
$(CC) $(CFLAGS) -I. $< $(LIBS) $(LINK_GL2) -lEGL -lGL -o $@$(EXT)
else
@echo gles has been disabled
endif
Expand All @@ -134,6 +137,7 @@ else
@echo osmesa has been disabled
endif


examples/vk10/vk10: examples/vk10/vk10.c RGFW.h
ifneq ($(NO_VULKAN), 1)
glslangValidator -V examples/vk10/shaders/vert.vert -o examples/vk10/shaders/vert.h --vn vert_code
Expand Down Expand Up @@ -198,11 +202,14 @@ endif

examples/microui_demo/microui_demo: examples/microui_demo/microui_demo.c RGFW.h
ifneq ($(CC), emcc)
$(CC) $(CFLAGS) -I. $< examples/microui_demo/microui.c $(LIBS) -o $@$(EXT)
$(CC) $(CFLAGS) -I. $< examples/microui_demo/microui.c $(LINK_GL1) $(LIBS) -o $@$(EXT)
else
$(CC) $(CFLAGS) -I. $< examples/microui_demo/microui.c -s USE_WEBGL2 $(LIBS) $(LINK_GL1) -o $@$(EXT)
endif

examples/silk/silk: examples/silk/silk.c RGFW.h
$(CC) $(CFLAGS) $(WARNINGS) -I. $< $(LIBS) -lm $(LINK_GL1) -o $@$(EXT)

examples/gamepad/gamepad: examples/gamepad/gamepad.c RGFW.h
$(CC) $(CFLAGS) $(WARNINGS) -I. $< $(LIBS) -lm $(LINK_GL1) -o $@$(EXT)

Expand Down Expand Up @@ -230,6 +237,7 @@ debug: all
.$(OS_DIR)$$exe$(EXT); \
done

./examples/silk/silk
./examples/gamepad/gamepad
./examples/first-person-camera/camera
./examples/portableGL/pgl$(EXT)
Expand Down
53 changes: 7 additions & 46 deletions RGFW.h
Original file line number Diff line number Diff line change
Expand Up @@ -1672,6 +1672,10 @@ RGFW_window* RGFW_createWindow(const char* name, RGFW_rect rect, RGFW_windowFlag
return RGFW_createWindowPtr(name, rect, flags, win);
}

#if defined(RGFW_USE_XDL) && defined(RGFW_X11)
#define XDL_IMPLEMENTATION
#include "XDL.h"
#endif

RGFWDEF void RGFW_window_basic_init(RGFW_window* win, RGFW_rect rect, RGFW_windowFlags flags);

Expand Down Expand Up @@ -1975,7 +1979,8 @@ void RGFW_window_setDND(RGFW_window* win, b8 allow) {
OpenGL defines start here (Normal, EGL, OSMesa)
*/

#if defined(RGFW_OPENGL) || defined(RGFW_EGL) || defined(RGFW_OSMESA)
#if defined(RGFW_OPENGL) || defined(RGFW_EGL)

#ifdef RGFW_WINDOWS
#define WIN32_LEAN_AND_MEAN
#define OEMRESOURCE
Expand All @@ -1993,7 +1998,6 @@ void RGFW_window_setDND(RGFW_window* win, b8 allow) {
#endif

/* EGL, normal OpenGL only */
#if !defined(RGFW_OSMESA)
i32 RGFW_majorVersion = 0, RGFW_minorVersion = 0;
b8 RGFW_profile = RGFW_glCore;

Expand All @@ -2003,7 +2007,6 @@ i32 RGFW_STENCIL = 8, RGFW_SAMPLES = 4, RGFW_STEREO = 0, RGFW_AUX_BUFFERS = 0, R
i32 RGFW_STENCIL = 0, RGFW_SAMPLES = 0, RGFW_STEREO = 0, RGFW_AUX_BUFFERS = 0, RGFW_DOUBLE_BUFFER = 1;
#endif


void RGFW_setGLStencil(i32 stencil) { RGFW_STENCIL = stencil; }
void RGFW_setGLSamples(i32 samples) { RGFW_SAMPLES = samples; }
void RGFW_setGLStereo(i32 stereo) { RGFW_STEREO = stereo; }
Expand Down Expand Up @@ -2341,32 +2344,6 @@ void RGFW_window_swapInterval(RGFW_window* win, i32 swapInterval) {
/*
end of RGFW_EGL defines
*/

/* OPENGL Normal / EGL defines only (no OS MESA) Ends here */

#elif defined(RGFW_OSMESA) /* OSmesa only */
RGFWDEF void RGFW_OSMesa_reorganize(RGFW_window* win);

/* reorganize buffer for osmesa */
void RGFW_OSMesa_reorganize(RGFW_window* win) {
u8* row = (u8*) RGFW_alloc(RGFW_bufferSize.w * 3);

i32 half_height = RGFW_bufferSize.h / 2;
i32 stride = RGFW_bufferSize.w * 3;

i32 y;
for (y = 0; y < half_height; ++y) {
i32 top_offset = y * stride;
i32 bottom_offset = (RGFW_bufferSize.h - y - 1) * stride;
RGFW_MEMCPY(row, win->buffer + top_offset, stride);
RGFW_MEMCPY(win->buffer + top_offset, win->buffer + bottom_offset, stride);
RGFW_MEMCPY(win->buffer + bottom_offset, row, stride);
}

RGFW_free(row);
}
#endif /* RGFW_OSMesa */

#endif /* RGFW_GL (OpenGL, EGL, OSMesa )*/

/*
Expand Down Expand Up @@ -2513,11 +2490,6 @@ Start of Linux / Unix defines
#include <X11/Xcursor/Xcursor.h>
#endif

#ifdef RGFW_USE_XDL
#define XDL_IMPLEMENTATION
#include "XDL.h"
#endif

#include <dlfcn.h>

#ifndef RGFW_NO_DPI
Expand Down Expand Up @@ -2711,7 +2683,7 @@ RGFW_window* RGFW_createWindowPtr(const char* name, RGFW_rect rect, RGFW_windowF
u64 event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask | FocusChangeMask | LeaveWindowMask | EnterWindowMask | ExposureMask; /*!< X11 events accepted*/

#ifdef RGFW_OPENGL
u32* visual_attribs = RGFW_initFormatAttribs(flags & RGFW_windowOpenglSoftware);
u32* visual_attribs = (u32*)RGFW_initFormatAttribs(flags & RGFW_windowOpenglSoftware);
i32 fbcount;
GLXFBConfig* fbc = glXChooseFBConfig((Display*) win->src.display, DefaultScreen(win->src.display), (i32*) visual_attribs, &fbcount);

Expand Down Expand Up @@ -4167,10 +4139,6 @@ void RGFW_window_swapBuffers(RGFW_window* win) {
if (!(win->_flags & RGFW_NO_CPU_RENDER)) {
#if defined(RGFW_OSMESA) || defined(RGFW_BUFFER)
RGFW_area area = RGFW_bufferSize;
#ifdef RGFW_OSMESA
RGFW_OSMesa_reorganize(win);
#endif

win->src.bitmap->data = (char*) win->buffer;
#if !defined(RGFW_X11_DONT_CONVERT_BGR) && !defined(RGFW_OSMESA)
u32 x, y;
Expand Down Expand Up @@ -6995,9 +6963,6 @@ void RGFW_window_swapBuffers(RGFW_window* win) {

if (!(win->_flags & RGFW_NO_CPU_RENDER)) {
#if defined(RGFW_OSMESA) || defined(RGFW_BUFFER)
#ifdef RGFW_OSMESA
RGFW_OSMesa_reorganize(win);
#endif
HGDIOBJ oldbmp = SelectObject(win->src.hdcMem, win->src.bitmap);
BitBlt(win->src.hdc, 0, 0, win->r.w, win->r.h, win->src.hdcMem, 0, 0, SRCCOPY);
SelectObject(win->src.hdcMem, oldbmp);
Expand Down Expand Up @@ -8832,10 +8797,6 @@ void RGFW_window_swapBuffers(RGFW_window* win) {

if (!(win->_flags & RGFW_NO_CPU_RENDER)) {
#if defined(RGFW_OSMESA) || defined(RGFW_BUFFER)
#ifdef RGFW_OSMESA
RGFW_OSMesa_reorganize(win);
#endif

id view = NSWindow_contentView((id)win->src.window);
id layer = objc_msgSend_id(view, sel_registerName("layer"));

Expand Down
4 changes: 4 additions & 0 deletions XDL.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ typedef PFNGLXGETVISUALFROMFBCONFIGPROC PFN_glXGetVisualFromFBConfig;
typedef PFNGLXGETFBCONFIGATTRIBPROC PFN_glXGetFBConfigAttrib;
typedef __GLXextFuncPtr (*PFN_glXGetProcAddressARB)(const GLubyte *);
typedef PFNGLXCHOOSEFBCONFIGPROC PFN_glXChooseFBConfig;
typedef void (*PFN_glXDestroyContext)(Display *dpy, GLXContext ctx);
#endif

/* Src vars for reciving the functions */
Expand Down Expand Up @@ -329,6 +330,7 @@ PFN_glXGetVisualFromFBConfig glXGetVisualFromFBConfigSrc;
PFN_glXGetFBConfigAttrib glXGetFBConfigAttribSrc;
PFN_glXGetProcAddressARB glXGetProcAddressARBSrc;
PFN_glXChooseFBConfig glXChooseFBConfigSrc;
PFN_glXDestroyContext glXDestroyContextSrc;
#endif

/* Function to source defines */
Expand Down Expand Up @@ -455,6 +457,7 @@ PFN_glXChooseFBConfig glXChooseFBConfigSrc;
#define glXGetFBConfigAttrib glXGetFBConfigAttribSrc
#define glXGetProcAddressARB glXGetProcAddressARBSrc
#define glXChooseFBConfig glXChooseFBConfigSrc
#define glXDestroyContext glXDestroyContextSrc
#define glXSwapIntervalEXT glXSwapIntervalEXTSrc
#endif

Expand Down Expand Up @@ -614,6 +617,7 @@ void XDL_init(void) {
XDL_PROC_DEF(1, glXGetFBConfigAttrib);
XDL_PROC_DEF(1, glXGetProcAddressARB);
XDL_PROC_DEF(1, glXChooseFBConfig);
XDL_PROC_DEF(1, glXDestroyContext);
#endif
}

Expand Down
3 changes: 2 additions & 1 deletion examples/osmesa/osmesa.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ int main(void) {
while (RGFW_window_checkEvent(win) != NULL);

RGFW_window_makeCurrent(win);


glViewport(0, 0, win->r.w, win->r.h);
glClearColor(255, 255, 255, 255);

glClear(GL_COLOR_BUFFER_BIT);
Expand Down

0 comments on commit 81065f7

Please sign in to comment.