Add CreateGLContext

This commit is contained in:
Hajime Hoshi 2013-11-25 02:12:07 +09:00
parent 638da2ed2c
commit 0628d1342f
2 changed files with 21 additions and 12 deletions

View File

@ -7,7 +7,9 @@ package cocoa
// #include "input.h" // #include "input.h"
// //
// void StartApplication(void); // void StartApplication(void);
// void* CreateWindow(size_t width, size_t height, const char* title); // void* CreateGLContext(void* sharedGLContext);
// void SetCurrentGLContext(void* glContext);
// void* CreateWindow(size_t width, size_t height, const char* title, void* glContext);
// void PollEvents(void); // void PollEvents(void);
// void BeginDrawing(void* window); // void BeginDrawing(void* window);
// void EndDrawing(void* window); // void EndDrawing(void* window);
@ -77,14 +79,17 @@ func New(screenWidth, screenHeight, screenScale int, title string) *UI {
C.StartApplication() C.StartApplication()
ui.window = C.CreateWindow(C.size_t(ui.screenWidth * ui.screenScale), context := C.CreateGLContext(unsafe.Pointer(nil))
C.size_t(ui.screenHeight * ui.screenScale), C.SetCurrentGLContext(context);
cTitle)
ui.graphicsDevice = opengl.NewDevice( ui.graphicsDevice = opengl.NewDevice(
ui.screenWidth, ui.screenWidth,
ui.screenHeight, ui.screenHeight,
ui.screenScale) ui.screenScale)
ui.window = C.CreateWindow(C.size_t(ui.screenWidth * ui.screenScale),
C.size_t(ui.screenHeight * ui.screenScale),
cTitle,
context)
currentUI = ui currentUI = ui
return ui return ui
} }

View File

@ -15,7 +15,7 @@ void StartApplication() {
[app activateIgnoringOtherApps:YES]; [app activateIgnoringOtherApps:YES];
} }
void* CreateGLContext() { void* CreateGLContext(void* sharedGLContext) {
NSOpenGLPixelFormatAttribute attributes[] = { NSOpenGLPixelFormatAttribute attributes[] = {
NSOpenGLPFAWindow, NSOpenGLPFAWindow,
NSOpenGLPFADoubleBuffer, NSOpenGLPFADoubleBuffer,
@ -25,24 +25,27 @@ void* CreateGLContext() {
}; };
NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc]
initWithAttributes:attributes]; initWithAttributes:attributes];
NSOpenGLContext* glContext = [[NSOpenGLContext alloc] initWithFormat:format NSOpenGLContext* glContext =
shareContext:nil]; [[NSOpenGLContext alloc] initWithFormat:format
shareContext:(NSOpenGLContext*)sharedGLContext];
[format release]; [format release];
return glContext; return glContext;
} }
void* CreateWindow(size_t width, size_t height, const char* title) { void SetCurrentGLContext(void* glContext) {
NSOpenGLContext* glContext = CreateGLContext(); [(NSOpenGLContext*)glContext makeCurrentContext];
[glContext makeCurrentContext]; }
// This takes the ownership of glContext.
void* CreateWindow(size_t width, size_t height, const char* title, void* glContext) {
NSSize size = NSMakeSize(width, height); NSSize size = NSMakeSize(width, height);
EbitenWindow* window = [[EbitenWindow alloc] EbitenWindow* window = [[EbitenWindow alloc]
initWithSize:size initWithSize:size
glContext:glContext]; glContext:(NSOpenGLContext*)glContext];
[window setTitle: [[NSString alloc] initWithUTF8String:title]]; [window setTitle: [[NSString alloc] initWithUTF8String:title]];
[window makeKeyAndOrderFront:nil]; [window makeKeyAndOrderFront:nil];
[glContext setView:[window contentView]]; [(NSOpenGLContext*)glContext setView:[window contentView]];
return window; return window;
} }
@ -61,6 +64,7 @@ void PollEvents(void) {
} }
void BeginDrawing(void* window) { void BeginDrawing(void* window) {
[[(EbitenWindow*)window glContext] makeCurrentContext];
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
} }