ebiten/ui/cocoa/window.go

79 lines
1.6 KiB
Go
Raw Normal View History

2013-12-08 14:13:48 +01:00
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 (
2013-12-09 14:40:54 +01:00
"github.com/hajimehoshi/go-ebiten/graphics"
2013-12-09 15:52:14 +01:00
"github.com/hajimehoshi/go-ebiten/graphics/opengl"
2013-12-08 14:13:48 +01:00
"runtime"
"unsafe"
)
2013-12-09 15:52:14 +01:00
type Window struct {
2013-12-09 14:40:54 +01:00
ui *UI
2013-12-08 14:13:48 +01:00
native unsafe.Pointer
2013-12-09 15:52:14 +01:00
canvas *opengl.Canvas
2013-12-08 14:13:48 +01:00
funcs chan func()
funcsDone chan struct{}
}
2013-12-09 15:52:14 +01:00
func runWindow(ui *UI, width, height, scale int, title string, sharedContext unsafe.Pointer) *Window {
w := &Window{
2013-12-09 14:40:54 +01:00
ui: ui,
2013-12-08 14:13:48 +01:00
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)
2013-12-09 15:52:14 +01:00
w.native = C.CreateWindow(C.size_t(width*scale),
C.size_t(height*scale),
2013-12-08 14:13:48 +01:00
cTitle,
glContext)
close(ch)
w.loop()
}()
<-ch
2013-12-09 15:52:14 +01:00
w.useContext(func() {
w.canvas = ui.graphicsDevice.CreateCanvas(width, height, scale)
})
2013-12-08 14:13:48 +01:00
return w
}
2013-12-09 15:52:14 +01:00
func (w *Window) loop() {
2013-12-08 14:13:48 +01:00
for {
select {
case f := <-w.funcs:
glContext := C.GetGLContext(w.native)
C.UseGLContext(glContext)
f()
C.UnuseGLContext()
w.funcsDone <- struct{}{}
}
}
}
2013-12-09 15:52:14 +01:00
func (w *Window) Draw(f func(graphics.Canvas)) {
2013-12-09 14:40:54 +01:00
w.useContext(func() {
2013-12-09 15:52:14 +01:00
w.ui.graphicsDevice.Update(w.canvas, f)
2013-12-09 14:40:54 +01:00
})
}
2013-12-09 15:52:14 +01:00
func (w *Window) useContext(f func()) {
2013-12-08 14:13:48 +01:00
w.funcs <- f
<-w.funcsDone
}