Start moving objc code into actual .m file, starting with OpenGL context

This commit is contained in:
UnknownShadow200 2021-01-23 09:38:26 +11:00
parent d5226a023f
commit 7f0a23674c
5 changed files with 65 additions and 66 deletions

View file

@ -81,7 +81,7 @@ build_mac64() {
echo "Building mac64.."
cp $SOURCE_DIR/misc/CCicon_mac64 $SOURCE_DIR/src/CCicon_mac64.o
rm cc-osx64
$MAC64_CC *.c $ALL_FLAGS $MACOS_FLAGS CCicon_mac64.o -DCC_COMMIT_SHA=\"$LATEST\" -o cc-osx64 -framework Cocoa -framework OpenGL -framework IOKit -lobjc
$MAC64_CC *.c interop_cocoa.m $ALL_FLAGS $MACOS_FLAGS CCicon_mac64.o -DCC_COMMIT_SHA=\"$LATEST\" -o cc-osx64 -framework Cocoa -framework OpenGL -framework IOKit -lobjc
}
build_web() {

View file

@ -84,7 +84,7 @@ Although the regular linux compiliation flags will work fine, to take full advan
##### Using gcc/clang (64 bit)
```cc *.c -o ClassiCube -framework Cocoa -framework OpenGL -framework IOKit -lobjc```
```cc *.c interop_cocoa.m -o ClassiCube -framework Cocoa -framework OpenGL -framework IOKit -lobjc```
### Compiling - other desktop OSes

View file

@ -4326,7 +4326,6 @@ void Window_DisableRawMouse(void) { DefaultDisableRawMouse(); }
#ifdef CC_BUILD_GL
/* OpenGL contexts are heavily tied to the window, so for simplicitly are also included here */
/* SDL and EGL are platform agnostic, other OpenGL context backends are tied to one windowing system. */
#define GLCONTEXT_DEFAULT_DEPTH 24
#define GLContext_IsInvalidAddress(ptr) (ptr == (void*)0 || ptr == (void*)1 || ptr == (void*)-1 || ptr == (void*)2)
void GLContext_GetAll(const struct DynamicLibSym* syms, int count) {
@ -4894,62 +4893,9 @@ void GLContext_GetApiInfo(cc_string* info) { }
*--------------------------------------------------------NSOpenGL---------------------------------------------------------*
*#########################################################################################################################*/
#elif defined CC_BUILD_COCOA
#define NSOpenGLPFADoubleBuffer 5
#define NSOpenGLPFAColorSize 8
#define NSOpenGLPFADepthSize 12
#define NSOpenGLPFAFullScreen 54
#define NSOpenGLContextParameterSwapInterval 222
static id ctxHandle;
static id MakePixelFormat(struct GraphicsMode* mode, cc_bool fullscreen) {
id fmt;
uint32_t attribs[7] = {
NSOpenGLPFAColorSize, 0,
NSOpenGLPFADepthSize, GLCONTEXT_DEFAULT_DEPTH,
NSOpenGLPFADoubleBuffer, 0, 0
};
attribs[1] = mode->R + mode->G + mode->B + mode->A;
attribs[5] = fullscreen ? NSOpenGLPFAFullScreen : 0;
fmt = objc_msgSend((id)objc_getClass("NSOpenGLPixelFormat"), sel_registerName("alloc"));
return objc_msgSend(fmt, sel_registerName("initWithAttributes:"), attribs);
}
void GLContext_Create(void) {
struct GraphicsMode mode;
id view, fmt;
InitGraphicsMode(&mode);
fmt = MakePixelFormat(&mode, true);
if (!fmt) {
Platform_LogConst("Failed to create full screen pixel format.");
Platform_LogConst("Trying again to create a non-fullscreen pixel format.");
fmt = MakePixelFormat(&mode, false);
}
if (!fmt) Logger_Abort("Choosing pixel format");
ctxHandle = objc_msgSend((id)objc_getClass("NSOpenGLContext"), sel_registerName("alloc"));
ctxHandle = objc_msgSend(ctxHandle, sel_registerName("initWithFormat:shareContext:"), fmt, NULL);
if (!ctxHandle) Logger_Abort("Failed to create OpenGL context");
objc_msgSend(ctxHandle, sel_registerName("setView:"), viewHandle);
objc_msgSend(fmt, sel_registerName("release"));
objc_msgSend(ctxHandle, sel_registerName("makeCurrentContext"));
objc_msgSend(ctxHandle, selUpdate);
}
void GLContext_Update(void) {
// TODO: Why does this crash on resizing
objc_msgSend(ctxHandle, selUpdate);
}
/* NOTE: Mostly implemented in interop_cocoa.m */
cc_bool GLContext_TryRestore(void) { return true; }
void GLContext_Free(void) {
objc_msgSend((id)objc_getClass("NSOpenGLContext"), sel_registerName("clearCurrentContext"));
objc_msgSend(ctxHandle, sel_registerName("clearDrawable"));
objc_msgSend(ctxHandle, sel_registerName("release"));
}
void* GLContext_GetAddress(const char* function) {
static const cc_string glPath = String_FromConst("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL");
static void* lib;
@ -4960,15 +4906,6 @@ void* GLContext_GetAddress(const char* function) {
return GLContext_IsInvalidAddress(addr) ? NULL : addr;
}
cc_bool GLContext_SwapBuffers(void) {
objc_msgSend(ctxHandle, selFlushBuffer);
return true;
}
void GLContext_SetFpsLimit(cc_bool vsync, float minFrameMs) {
int value = vsync ? 1 : 0;
objc_msgSend(ctxHandle, sel_registerName("setValues:forParameter:"), &value, NSOpenGLContextParameterSwapInterval);
}
void GLContext_GetApiInfo(cc_string* info) { }

View file

@ -159,6 +159,7 @@ void Window_UpdateRawMouse(void);
void Window_DisableRawMouse(void);
#ifdef CC_BUILD_GL
#define GLCONTEXT_DEFAULT_DEPTH 24
/* Creates an OpenGL context, then makes it the active context. */
/* NOTE: You MUST have created the window beforehand, as the GL context is attached to the window. */
void GLContext_Create(void);

61
src/interop_cocoa.m Normal file
View file

@ -0,0 +1,61 @@
#include "Logger.h"
#include "Platform.h"
#include "Window.h"
#include <Cocoa/Cocoa.h>
#include <OpenGL/OpenGL.h>
static NSOpenGLContext* ctx;
extern id viewHandle;
static NSOpenGLPixelFormat* MakePixelFormat(cc_bool fullscreen) {
NSOpenGLPixelFormatAttribute attribs[7] = {
NSOpenGLPFAColorSize, 0,
NSOpenGLPFADepthSize, 24,
NSOpenGLPFADoubleBuffer, 0, 0
};
attribs[1] = DisplayInfo.Depth;
attribs[5] = fullscreen ? NSOpenGLPFAFullScreen : 0;
return [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
}
void GLContext_Create(void) {
NSOpenGLPixelFormat* fmt;
fmt = MakePixelFormat(true);
if (!fmt) {
Platform_LogConst("Failed to create full screen pixel format.");
Platform_LogConst("Trying again to create a non-fullscreen pixel format.");
fmt = MakePixelFormat(false);
}
if (!fmt) Logger_Abort("Choosing pixel format");
ctx = [NSOpenGLContext alloc];
ctx = [ctx initWithFormat:fmt shareContext:NULL];
if (!ctx) Logger_Abort("Failed to create OpenGL context");
[ctx setView:viewHandle];
[fmt release];
[ctx makeCurrentContext];
[ctx update];
}
void GLContext_Update(void) {
// TODO: Why does this crash on resizing
[ctx update];
}
void GLContext_Free(void) {
[NSOpenGLContext clearCurrentContext];
[ctx clearDrawable];
[ctx release];
}
cc_bool GLContext_SwapBuffers(void) {
[ctx flushBuffer];
return true;
}
void GLContext_SetFpsLimit(cc_bool vsync, float minFrameMs) {
int value = vsync ? 1 : 0;
[ctx setValues:&value forParameter: NSOpenGLContextParameterSwapInterval];
}