Skip to content

Commit

Permalink
updated RGFW
Browse files Browse the repository at this point in the history
  • Loading branch information
ColleagueRiley committed Mar 10, 2024
1 parent 3b8f8f3 commit 9472279
Showing 1 changed file with 40 additions and 34 deletions.
74 changes: 40 additions & 34 deletions deps/RGFW.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ extern "C" {

#if defined(__APPLE__) && !defined(RGFW_MACOS_X11)
#define RGFW_MACOS
#include <CoreVideo/CVDisplayLink.h>
#endif

#if defined(RGFW_OPENGL_ES) && !defined(RGFW_EGL)
Expand Down Expand Up @@ -387,6 +388,8 @@ typedef struct RGFW_window_src {
Window window; /*!< source window */
#endif
#ifdef RGFW_MACOS
u32 display;
CVDisplayLinkRef displayLink;
void* window;
#endif

Expand Down Expand Up @@ -4419,6 +4422,7 @@ RGFW_window* RGFW_createWindow(const char* name, RGFW_rect rect, u16 args) {
class_addMethod(WindowDelegateClass, sel_registerName("windowDidResize:"), (IMP)RGFW_windowDidResize, "v@:@");

NSApp = NSApplication_sharedApplication();
NSApplication_setActivationPolicy(NSApp, NSApplicationActivationPolicyRegular);

RGFW_window* win = RGFW_window_basic_init(rect, args);

Expand All @@ -4432,21 +4436,10 @@ RGFW_window* RGFW_createWindow(const char* name, RGFW_rect rect, u16 args) {
else
macArgs |= NSWindowStyleMaskBorderless;

win->src.window = NSWindow_init(windowRect, macArgs, NSBackingStoreBuffered, false);
NSWindow_contentView_setWantsLayer(win->src.window, true);
win->src.window = NSWindow_init(windowRect, macArgs, macArgs, false);
NSRetain(win->src.window);
NSWindow_setTitle(win->src.window, name);

if (args & RGFW_TRANSPARENT_WINDOW) {
#ifdef RGFW_OPENGL
i32 opacity = 0;
NSOpenGLContext_setValues(win->src.rSurf, &opacity, NSOpenGLContextParameterSurfaceOpacity);
#endif
NSWindow_setOpaque(win->src.window, false);
NSWindow_setBackgroundColor(win->src.window, NSColor_colorWithSRGB(0, 0, 0, 0));
NSWindow_setAlphaValue(win->src.window, 0x00);
}

#ifdef RGFW_OPENGL
NSOpenGLPixelFormatAttribute attributes[] = {
NSOpenGLPFAAccelerated,
Expand Down Expand Up @@ -4499,20 +4492,38 @@ RGFW_window* RGFW_createWindow(const char* name, RGFW_rect rect, u16 args) {
win->src.view = NSOpenGLView_initWithFrame(NSMakeRect(0, 0, win->r.w, win->r.h), format);
NSOpenGLView_prepareOpenGL(win->src.view);

win->src.rSurf = NSOpenGLView_openGLContext(win->src.view);
NSOpenGLContext_makeCurrentContext(win->src.rSurf);

#else
NSRect contentRect = NSMakeRect(0, 0, win->r.w, win->r.h);
win->src.view = NSView_initWithFrame(contentRect);
#endif

RGFW_init_buffer(win);
NSWindow_contentView_setWantsLayer(win->src.window, true);
NSWindow_setContentView(win->src.window, (NSView*)win->src.view);

win->src.rSurf = NSOpenGLView_openGLContext(win->src.view);
NSOpenGLContext_makeCurrentContext(win->src.rSurf);

if (args & RGFW_TRANSPARENT_WINDOW) {
#ifdef RGFW_OPENGL
i32 opacity = 0;
NSOpenGLContext_setValues(win->src.rSurf, &opacity, NSOpenGLContextParameterSurfaceOpacity);
#endif
NSWindow_setOpaque(win->src.window, false);
NSWindow_setBackgroundColor(win->src.window, NSColor_colorWithSRGB(0, 0, 0, 0));
NSWindow_setAlphaValue(win->src.window, 0x00);
}

win->src.display = CGMainDisplayID();
CVDisplayLinkCreateWithCGDisplay(win->src.display, &win->src.displayLink);
CVDisplayLinkSetOutputCallback(win->src.displayLink, displayCallback, win);
CVDisplayLinkStart(win->src.displayLink);

RGFW_init_buffer(win);

if (args & RGFW_SCALE_TO_MONITOR)
RGFW_window_scaleToMonitor(win);
NSWindow_setContentView(win->src.window, win->src.view);
if (args & RGFW_ALLOW_DND) {

if (args & RGFW_ALLOW_DND) {
win->src.winArgs |= RGFW_ALLOW_DND;

siArray(NSPasteboardType) array = si_array_init((NSPasteboardType[]){NSPasteboardTypeURL, NSPasteboardTypeFileURL, NSPasteboardTypeString}, sizeof(*array), 3);
Expand All @@ -4532,19 +4543,18 @@ RGFW_window* RGFW_createWindow(const char* name, RGFW_rect rect, u16 args) {
if (args & RGFW_COCOA_MOVE_TO_RESOURCE_DIR)
NSMoveToResourceDir();

// Show the window
NSWindow_makeKeyAndOrderFront(win->src.window, NULL);
// Show the window
NSWindow_makeKeyAndOrderFront(win->src.window, NULL);
NSWindow_setIsVisible(win->src.window, true);

NSApplication_setActivationPolicy(NSApp, NSApplicationActivationPolicyRegular);
NSApplication_finishLaunching(NSApp);

if (!RGFW_loaded) {
NSWindow_makeMainWindow(win->src.window);

RGFW_loaded = 1;
}

NSApplication_finishLaunching(NSApp);

RGFW_windows_size++;

RGFW_window** nWins = (RGFW_window**)malloc(sizeof(RGFW_window*) * RGFW_windows_size);
Expand Down Expand Up @@ -4594,6 +4604,10 @@ u32 RGFW_keysPressed[10]; /*10 keys at a time*/
RGFW_Event* RGFW_window_checkEvent(RGFW_window* win) {
assert(win != NULL);

NSEvent* e = NSApplication_nextEventMatchingMask(NSApp, NSEventMaskAny, NULL, NSDefaultRunLoopMode, true);
if (win->event.type == RGFW_quit || e == NULL || NSEvent_window(e) != win->src.window)
return NULL;

if (win->event.droppedFilesCount) {
i32 i;
for (i = 0; i < win->event.droppedFilesCount; i++)
Expand All @@ -4613,10 +4627,6 @@ RGFW_Event* RGFW_window_checkEvent(RGFW_window* win) {
NSCursor_set(win->src.cursor);
}

NSEvent* e = NSApplication_nextEventMatchingMask(NSApp, NSEventMaskAny, NULL, NSDefaultRunLoopMode, true);
if (win->event.type == RGFW_quit || e == NULL || NSEvent_window(e) != win->src.window)
return NULL;

switch (NSEvent_type(e)) {
case NSEventTypeKeyDown:
win->event.type = RGFW_keyPressed;
Expand Down Expand Up @@ -4706,8 +4716,6 @@ RGFW_Event* RGFW_window_checkEvent(RGFW_window* win) {

NSApplication_sendEvent(NSApp, e);

NSApplication_updateWindows(NSApp);

RGFW_vector mouse = RGFW_getGlobalMousePoint();
if (win->src.winArgs & RGFW_HOLD_MOUSE && win->event.inFocus &&
(mouse.x != win->r.x + (win->r.w / 2) || mouse.y != win->r.y + (win->r.h / 2))) {
Expand Down Expand Up @@ -4902,11 +4910,7 @@ RGFW_monitor RGFW_getPrimaryMonitor(void) {
}

RGFW_monitor RGFW_window_getMonitor(RGFW_window* win) {
static CGDirectDisplayID display = 0;
if (display == 0)
display = CGMainDisplayID();

return RGFW_NSCreateMonitor(display);
return RGFW_NSCreateMonitor(win->src.display);
}

u8 RGFW_isPressedI(RGFW_window* win, u32 key) {
Expand Down Expand Up @@ -4993,6 +4997,8 @@ void RGFW_window_close(RGFW_window* win){
RGFW_FREE(RGFW_windows);
}

CVDisplayLinkStop(win->src.displayLink);
CVDisplayLinkRelease(win->src.displayLink);

NSApplication_terminate(NSApp, (id)win->src.window);
}
Expand Down

0 comments on commit 9472279

Please sign in to comment.