From 672eeb904cd183ded241d3f91e88fde3f2f02751 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 8 Sep 2019 18:02:31 +1000 Subject: [PATCH] Start on the incredibly ugly cocoa backend --- src/Window.c | 134 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 130 insertions(+), 4 deletions(-) diff --git a/src/Window.c b/src/Window.c index a9b47e4ec..9c603723b 100644 --- a/src/Window.c +++ b/src/Window.c @@ -284,7 +284,7 @@ static LRESULT CALLBACK Window_Procedure(HWND handle, UINT message, WPARAM wPara bool ext = (lParam & (1UL << 24)) != 0; bool lShiftDown, rShiftDown; - Key mappedKey; + Key key; switch (wParam) { case VK_SHIFT: @@ -295,7 +295,7 @@ static LRESULT CALLBACK Window_Procedure(HWND handle, UINT message, WPARAM wPara rShiftDown = ((USHORT)GetKeyState(VK_RSHIFT)) >> 15; if (!pressed || lShiftDown != rShiftDown) { - Input_SetPressed(KEY_LSHIFT, lShiftDown); + Input_SetPressed(KEY_LSHIFT, lShiftDown); Input_SetPressed(KEY_RSHIFT, rShiftDown); } return 0; @@ -311,8 +311,8 @@ static LRESULT CALLBACK Window_Procedure(HWND handle, UINT message, WPARAM wPara return 0; default: - mappedKey = Window_MapKey(wParam); - if (mappedKey) Input_SetPressed(mappedKey, pressed); + key = Window_MapKey(wParam); + if (key) Input_SetPressed(key, pressed); return 0; } } break; @@ -3652,3 +3652,129 @@ void GLContext_SetFpsLimit(bool vsync, float minFrameMs) { } #endif #endif + + + +#ifdef CC_BUILD_COCOA +#include +#include +#include +#include +static id appHandle; +static id winHandle; + +typedef struct FailRect_ { + // TODO: use CGFloat because that works with 32 bit + double x, y; + double width, height; +} FailRect; + +id Cocoa_SendIntPtr(id receiver, SEL op) { + IMP func = objc_msg_lookup(receiver, op); + return func(receiver, op); +} + +id Cocoa_SendIntPtr_I(id receiver, SEL op, int a1) { + IMP func = objc_msg_lookup(receiver, op); + return func(receiver, op, a1); +} + +id Cocoa_SendIntPtr_P(id receiver, SEL op, id a1) { + IMP func = objc_msg_lookup(receiver, op); + return func(receiver, op, a1); +} + +void Cocoa_SendIntPtr_R(id receiver, SEL op, FailRect* r) { + IMP func = objc_msg_lookup(receiver, op); + func(receiver, op, r); +} + +id cmacs_window_init_msgSend(id receiver, SEL op, FailRect a1, int a2, int a3, bool a4) { + IMP func = objc_msg_lookup(receiver, op); + return func(receiver, op, a1, a2, a3, a4); +} + +void Window_Init(void) { + id screen, screens; + int count; + FailRect rect = { 1, 2, 3, 4 }; + + Platform_LogConst("hi world"); + appHandle = Cocoa_SendIntPtr((id)objc_getClass("NSApplication"), sel_registerName("sharedApplication")); + Platform_LogConst("all gudd!!"); + + + // TODO: why's this bit of code completely stuffed + screens = Cocoa_SendIntPtr((id)objc_getClass("NSScreen"), sel_registerName("screens")); + count = Cocoa_SendIntPtr(screens, sel_registerName("count")); + Platform_Log1("COUNT: %i", &count); + + screen = Cocoa_SendIntPtr_I(screens, sel_registerName("objectAtIndex:"), 0); + Cocoa_SendIntPtr_R(screen, sel_registerName("frame"), &rect); + Platform_LogConst("GOT FRAME"); + printf("STUFF: %f,%f,%f,%f", rect.x, rect.y, rect.width, rect.height); +} + +#define NSTitledWindowMask (1 << 0) +#define NSClosableWindowMask (1 << 1) +#define NSMiniaturizableWindowMask (1 << 2) +#define NSResizableWindowMask (1 << 3) + +void Window_Create(int width, int height) { + id window; + Window_Width = width; + Window_Height = height; + Window_Exists = true; + + winHandle = Cocoa_SendIntPtr((id)objc_getClass("NSWindow"), sel_registerName("alloc")); + winHandle = cmacs_window_init_msgSend(winHandle, sel_getUid("initWithContentRect:styleMask:backing:defer:"), (FailRect) { 0, 0, 1024, 460 }, (NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask | NSMiniaturizableWindowMask), 0, false); + + // TODO: move to setVisible + Cocoa_SendIntPtr_P(winHandle, sel_registerName("makeKeyAndOrderFront:"), appHandle); + Platform_LogConst("made window"); +} + +void Window_SetTitle(const String* title) { } +void Clipboard_GetText(String* value) { } +void Clipboard_SetText(const String* value) { } + +void Window_SetVisible(bool visible) { } +int Window_GetWindowState(void) { return 0; } +void Window_EnterFullscreen(void) { } +void Window_ExitFullscreen(void) { } + +void Window_SetSize(int width, int height) { } +void Window_Close(void) { } +void Window_ProcessEvents(void) { } + +static void Cursor_GetRawPos(int* x, int* y) { *x = 0; *y = 0; } +void Cursor_SetPosition(int x, int y) { } +void Cursor_SetVisible(bool visible) { } +void Window_ShowDialog(const char* title, const char* msg) { } + +void Window_AllocFramebuffer(Bitmap* bmp) { + bmp->Scan0 = (cc_uint8*)Mem_Alloc(bmp->Width * bmp->Height, 4, "window pixels"); +} + +void Window_DrawFramebuffer(Rect2D r) { +} + +void Window_FreeFramebuffer(Bitmap* bmp) { + Mem_Free(bmp->Scan0); +} + +void Window_OpenKeyboard(void) { } +void Window_CloseKeyboard(void) { } + +void Window_EnableRawMouse(void) { } +void Window_UpdateRawMouse(void) { } +void Window_DisableRawMouse(void) { } + +void GLContext_Init(struct GraphicsMode* mode) { } +void GLContext_Update(void) { } +bool GLContext_TryRestore(void) { return true; } +void GLContext_Free(void) { } +void* GLContext_GetAddress(const char* function) { return NULL; } +bool GLContext_SwapBuffers(void) { return true; } +void GLContext_SetFpsLimit(bool vsync, float minFrameMs) { } +#endif