ebiten/ui/cocoa/texture_factory.go

56 lines
1.0 KiB
Go
Raw Normal View History

2013-12-06 18:20:48 +01:00
package cocoa
// void* CreateGLContext(void* sharedGLContext);
// void UseGLContext(void* glContext);
2013-12-07 17:35:24 +01:00
// void UnuseGLContext(void);
2013-12-06 18:20:48 +01:00
//
import "C"
import (
2013-12-08 14:13:48 +01:00
"runtime"
2013-12-06 18:20:48 +01:00
"unsafe"
)
type textureFactory struct {
sharedContext unsafe.Pointer
funcs chan func()
funcsDone chan struct{}
textureFactoryEvents
}
func runTextureFactory() *textureFactory {
t := &textureFactory{
2013-12-08 14:13:48 +01:00
funcs: make(chan func()),
2013-12-06 18:20:48 +01:00
funcsDone: make(chan struct{}),
}
ch := make(chan struct{})
go func() {
2013-12-08 14:13:48 +01:00
runtime.LockOSThread()
2013-12-06 18:20:48 +01:00
t.sharedContext = C.CreateGLContext(unsafe.Pointer(nil))
close(ch)
t.loop()
}()
<-ch
return t
}
func (t *textureFactory) loop() {
for {
select {
case f := <-t.funcs:
C.UseGLContext(t.sharedContext)
f()
2013-12-07 17:35:24 +01:00
C.UnuseGLContext()
2013-12-06 18:20:48 +01:00
t.funcsDone <- struct{}{}
}
}
}
2013-12-09 14:40:54 +01:00
func (t *textureFactory) useContext(f func()) {
2013-12-06 18:20:48 +01:00
t.funcs <- f
<-t.funcsDone
}
2013-12-11 14:31:13 +01:00
func (t *textureFactory) createWindow(ui *cocoaUI, width, height, scale int, title string) *Window {
2013-12-09 15:52:14 +01:00
return runWindow(ui, width, height, scale, title, t.sharedContext)
2013-12-06 18:20:48 +01:00
}