Add cocoa.window

This commit is contained in:
Hajime Hoshi 2013-12-08 22:13:48 +09:00
parent e10bac5a8a
commit c36d8c5512
4 changed files with 80 additions and 24 deletions

View File

@ -7,8 +7,6 @@ package cocoa
// //
// void StartApplication(void); // void StartApplication(void);
// void PollEvents(void); // void PollEvents(void);
// void BeginDrawing(void* window);
// void EndDrawing(void* window);
// //
import "C" import "C"
import ( import (
@ -16,14 +14,13 @@ import (
"github.com/hajimehoshi/go-ebiten/graphics/opengl" "github.com/hajimehoshi/go-ebiten/graphics/opengl"
"github.com/hajimehoshi/go-ebiten/ui" "github.com/hajimehoshi/go-ebiten/ui"
"image" "image"
"unsafe"
) )
type UI struct { type UI struct {
screenWidth int screenWidth int
screenHeight int screenHeight int
screenScale int screenScale int
window unsafe.Pointer window *window
initialEventSent bool initialEventSent bool
textureFactory *textureFactory textureFactory *textureFactory
graphicsDevice *opengl.Device graphicsDevice *opengl.Device
@ -114,10 +111,9 @@ func (u *UI) RenderTargetCreated() <-chan graphics.RenderTargetCreatedEvent {
} }
func (u *UI) Draw(f func(graphics.Canvas)) { func (u *UI) Draw(f func(graphics.Canvas)) {
// TODO: Use UseContext instead u.window.UseContext(func() {
C.BeginDrawing(u.window) u.graphicsDevice.Update(f)
u.graphicsDevice.Update(f) })
C.EndDrawing(u.window)
} }
//export ebiten_ScreenSizeUpdated //export ebiten_ScreenSizeUpdated

View File

@ -50,9 +50,8 @@ void* CreateGLContext(void* sharedGLContext) {
return glContext; return glContext;
} }
void* CreateWindow(size_t width, size_t height, const char* title, void* sharedGLContext) { void* CreateWindow(size_t width, size_t height, const char* title, void* glContext_) {
NSOpenGLContext* glContext = CreateGLContext(sharedGLContext); NSOpenGLContext* glContext = (NSOpenGLContext*)glContext_;
[glContext makeCurrentContext];
NSSize size = NSMakeSize(width, height); NSSize size = NSMakeSize(width, height);
EbitenWindow* window = [[EbitenWindow alloc] EbitenWindow* window = [[EbitenWindow alloc]
@ -95,6 +94,10 @@ void UnuseGLContext(void) {
CGLUnlockContext(cglContext); CGLUnlockContext(cglContext);
} }
void* GetGLContext(void* window) {
return [(EbitenWindow*)window glContext];
}
void BeginDrawing(void* window) { void BeginDrawing(void* window) {
// TODO: CGLLock // TODO: CGLLock
[[(EbitenWindow*)window glContext] makeCurrentContext]; [[(EbitenWindow*)window glContext] makeCurrentContext];

View File

@ -1,15 +1,12 @@
package cocoa package cocoa
// #include <stdlib.h>
//
// void* CreateGLContext(void* sharedGLContext); // void* CreateGLContext(void* sharedGLContext);
// void* CreateWindow(size_t width, size_t height, const char* title, void* sharedGLContext);
// void UseGLContext(void* glContext); // void UseGLContext(void* glContext);
// void UnuseGLContext(void); // void UnuseGLContext(void);
// //
import "C" import "C"
import ( import (
//"github.com/hajimehoshi/go-ebiten/graphics" "runtime"
"unsafe" "unsafe"
) )
@ -22,11 +19,12 @@ type textureFactory struct {
func runTextureFactory() *textureFactory { func runTextureFactory() *textureFactory {
t := &textureFactory{ t := &textureFactory{
funcs: make(chan func()), funcs: make(chan func()),
funcsDone: make(chan struct{}), funcsDone: make(chan struct{}),
} }
ch := make(chan struct{}) ch := make(chan struct{})
go func() { go func() {
runtime.LockOSThread()
t.sharedContext = C.CreateGLContext(unsafe.Pointer(nil)) t.sharedContext = C.CreateGLContext(unsafe.Pointer(nil))
close(ch) close(ch)
t.loop() t.loop()
@ -52,12 +50,6 @@ func (t *textureFactory) UseContext(f func()) {
<-t.funcsDone <-t.funcsDone
} }
func (t *textureFactory) CreateWindow(width, height int, title string) unsafe.Pointer { func (t *textureFactory) CreateWindow(width, height int, title string) *window {
cTitle := C.CString(title) return runWindow(width, height, title, t.sharedContext)
defer C.free(unsafe.Pointer(cTitle))
return C.CreateWindow(C.size_t(width),
C.size_t(height),
cTitle,
t.sharedContext)
} }

65
ui/cocoa/window.go Normal file
View File

@ -0,0 +1,65 @@
package cocoa
// #include <stdlib.h>
//
// void* CreateWindow(size_t width, size_t height, const char* title, void* glContext);
// void* CreateGLContext(void* sharedGLContext);
//
// void* GetGLContext(void* window);
// void UseGLContext(void* glContext);
// void UnuseGLContext(void);
//
import "C"
import (
"runtime"
"unsafe"
)
type window struct {
native unsafe.Pointer
funcs chan func()
funcsDone chan struct{}
}
func runWindow(width, height int, title string, sharedContext unsafe.Pointer) *window {
w := &window{
funcs: make(chan func()),
funcsDone: make(chan struct{}),
}
cTitle := C.CString(title)
defer C.free(unsafe.Pointer(cTitle))
ch := make(chan struct{})
go func() {
runtime.LockOSThread()
glContext := C.CreateGLContext(sharedContext)
w.native = C.CreateWindow(C.size_t(width),
C.size_t(height),
cTitle,
glContext)
close(ch)
w.loop()
}()
<-ch
// TODO: Activate here?
return w
}
func (w *window) loop() {
for {
select {
case f := <-w.funcs:
glContext := C.GetGLContext(w.native)
C.UseGLContext(glContext)
f()
C.UnuseGLContext()
w.funcsDone <- struct{}{}
}
}
}
func (w *window) UseContext(f func()) {
w.funcs <- f
<-w.funcsDone
}