2026-03-05 19:29:43 -07:00
|
|
|
#include "Mw/BaseTypes.h"
|
2026-03-04 22:56:04 -07:00
|
|
|
#include "Mw/Core.h"
|
2026-03-08 20:03:47 -07:00
|
|
|
#include "Mw/LowLevel/Cocoa.h"
|
2026-03-04 22:56:04 -07:00
|
|
|
#include "Mw/StringDefs.h"
|
|
|
|
|
#include <Mw/Milsko.h>
|
|
|
|
|
#include <Mw/Widget/OpenGL.h>
|
|
|
|
|
#import <mach-o/dyld.h>
|
|
|
|
|
|
|
|
|
|
@interface MacOpenGLWidget : NSObject {
|
2026-03-24 04:26:26 +09:00
|
|
|
NSOpenGLPixelFormat* pixelFormat;
|
|
|
|
|
NSOpenGLContext* glc;
|
|
|
|
|
MilskoCocoa* _win;
|
2026-03-04 22:56:04 -07:00
|
|
|
}
|
|
|
|
|
|
2026-03-24 04:26:26 +09:00
|
|
|
- (MacOpenGLWidget*)initWithWindow:(MilskoCocoa*)w;
|
2026-03-04 22:56:04 -07:00
|
|
|
- (void)destroy;
|
|
|
|
|
- (void)makeCurrent;
|
|
|
|
|
- (void)swapBuffer;
|
2026-03-24 04:26:26 +09:00
|
|
|
- (void*)getProcAddressWithName:(const char*)name;
|
2026-03-04 22:56:04 -07:00
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
static int create(MwWidget handle) {
|
2026-03-24 04:26:26 +09:00
|
|
|
void* r = NULL;
|
|
|
|
|
MwWidget w = handle;
|
2026-03-04 22:56:04 -07:00
|
|
|
|
2026-03-24 04:26:26 +09:00
|
|
|
int x = MwGetInteger(w, MwNx);
|
|
|
|
|
int y = MwGetInteger(w, MwNy);
|
|
|
|
|
int width = MwGetInteger(w, MwNwidth);
|
|
|
|
|
int height = MwGetInteger(w, MwNheight);
|
2026-03-04 22:56:04 -07:00
|
|
|
|
2026-03-24 04:26:26 +09:00
|
|
|
printf("%d %d\n", width, height);
|
2026-03-04 22:56:04 -07:00
|
|
|
|
2026-03-24 04:26:26 +09:00
|
|
|
handle->internal =
|
|
|
|
|
[[MacOpenGLWidget alloc] initWithWindow:handle->lowlevel->cocoa.real];
|
|
|
|
|
handle->lowlevel->common.copy_buffer = 0;
|
2026-03-04 22:56:04 -07:00
|
|
|
|
2026-03-24 04:26:26 +09:00
|
|
|
MwSetDefault(handle);
|
2026-03-04 22:56:04 -07:00
|
|
|
|
2026-03-24 04:26:26 +09:00
|
|
|
while(w->parent != NULL)
|
|
|
|
|
w = w->parent;
|
2026-03-04 22:56:04 -07:00
|
|
|
|
2026-03-24 04:26:26 +09:00
|
|
|
w->berserk++;
|
2026-03-04 22:56:04 -07:00
|
|
|
|
2026-03-24 04:26:26 +09:00
|
|
|
return 0;
|
2026-03-04 22:56:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void destroy(MwWidget handle) {
|
2026-03-24 04:26:26 +09:00
|
|
|
[(MacOpenGLWidget*)handle->internal destroy];
|
2026-03-04 22:56:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void mwOpenGLMakeCurrentImpl(MwWidget handle) {
|
2026-03-24 04:26:26 +09:00
|
|
|
[(MacOpenGLWidget*)handle->internal makeCurrent];
|
2026-03-04 22:56:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void mwOpenGLSwapBufferImpl(MwWidget handle) {
|
2026-03-24 04:26:26 +09:00
|
|
|
[(MacOpenGLWidget*)handle->internal swapBuffer];
|
2026-03-04 22:56:04 -07:00
|
|
|
}
|
|
|
|
|
|
2026-03-24 04:26:26 +09:00
|
|
|
static void* mwOpenGLGetProcAddressImpl(MwWidget handle, const char* name) {
|
|
|
|
|
return [(MacOpenGLWidget*)handle->internal getProcAddressWithName:name];
|
2026-03-04 22:56:04 -07:00
|
|
|
}
|
|
|
|
|
|
2026-03-24 04:26:26 +09:00
|
|
|
static void func_handler(MwWidget handle, const char* name, void* out,
|
|
|
|
|
va_list va) {
|
|
|
|
|
if(strcmp(name, "mwOpenGLMakeCurrent") == 0) {
|
|
|
|
|
mwOpenGLMakeCurrentImpl(handle);
|
|
|
|
|
}
|
|
|
|
|
if(strcmp(name, "mwOpenGLSwapBuffer") == 0) {
|
|
|
|
|
mwOpenGLSwapBufferImpl(handle);
|
|
|
|
|
}
|
|
|
|
|
if(strcmp(name, "mwOpenGLGetProcAddress") == 0) {
|
|
|
|
|
const char* _name = va_arg(va, const char*);
|
|
|
|
|
*(void**)out = mwOpenGLGetProcAddressImpl(handle, _name);
|
|
|
|
|
}
|
2026-03-04 22:56:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@implementation MacOpenGLWidget
|
|
|
|
|
|
2026-03-24 04:26:26 +09:00
|
|
|
- (MacOpenGLWidget*)initWithWindow:(MilskoCocoa*)w {
|
|
|
|
|
NSOpenGLPixelFormatAttribute pixelFormatAttributes[] = {
|
|
|
|
|
NSOpenGLPFAColorSize, 24,
|
|
|
|
|
NSOpenGLPFAStencilSize, 8,
|
|
|
|
|
NSOpenGLPFAAlphaSize, 8,
|
|
|
|
|
NSOpenGLPFADoubleBuffer, NSOpenGLPFAAccelerated,
|
|
|
|
|
NSOpenGLPFANoRecovery, 0};
|
|
|
|
|
self->pixelFormat =
|
|
|
|
|
[[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes];
|
|
|
|
|
self->glc =
|
|
|
|
|
[[NSOpenGLContext alloc] initWithFormat:pixelFormat
|
|
|
|
|
shareContext:nil];
|
|
|
|
|
[self->glc setView:[w getView]];
|
|
|
|
|
self->_win = w;
|
|
|
|
|
return self;
|
2026-03-04 22:56:04 -07:00
|
|
|
};
|
|
|
|
|
- (void)destroy {
|
|
|
|
|
};
|
|
|
|
|
- (void)makeCurrent {
|
2026-03-24 04:26:26 +09:00
|
|
|
[self->glc makeCurrentContext];
|
2026-03-04 22:56:04 -07:00
|
|
|
};
|
|
|
|
|
- (void)swapBuffer {
|
2026-03-24 04:26:26 +09:00
|
|
|
[self->glc flushBuffer];
|
2026-03-04 22:56:04 -07:00
|
|
|
};
|
2026-03-24 04:26:26 +09:00
|
|
|
- (void*)getProcAddressWithName:(const char*)name {
|
|
|
|
|
if(NSIsSymbolNameDefined(name)) {
|
|
|
|
|
return NSAddressOfSymbol(NSLookupAndBindSymbol(name));
|
|
|
|
|
} else {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2026-03-04 22:56:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
2026-03-24 04:26:26 +09:00
|
|
|
MwClassRec MwOpenGLClassRec = {create, /* create */
|
|
|
|
|
destroy, /* destroy */
|
|
|
|
|
NULL, /* draw */
|
|
|
|
|
NULL, /* click */
|
|
|
|
|
NULL, /* parent_resize */
|
|
|
|
|
NULL, /* prop_change */
|
|
|
|
|
NULL, /* mouse_move */
|
|
|
|
|
NULL, /* mouse_up */
|
|
|
|
|
NULL, /* mouse_down */
|
|
|
|
|
NULL, /* key */
|
|
|
|
|
func_handler, /* execute */
|
|
|
|
|
NULL, /* tick */
|
|
|
|
|
NULL, /* resize */
|
|
|
|
|
NULL, /* children_update */
|
|
|
|
|
NULL, /* children_prop_change */
|
|
|
|
|
NULL, /* clipboard */
|
|
|
|
|
NULL, NULL, NULL, NULL};
|
|
|
|
|
MwClass MwOpenGLClass = &MwOpenGLClassRec;
|