ebiten/ui/cocoa/texture_factory.go

56 lines
1.1 KiB
Go
Raw Normal View History

2013-12-06 18:20:48 +01:00
package cocoa
2013-12-30 19:17:39 +01:00
// @class NSOpenGLContext;
//
// NSOpenGLContext* CreateGLContext(NSOpenGLContext* sharedGLContext);
// void UseGLContext(NSOpenGLContext* 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
)
type textureFactory struct {
2013-12-30 19:17:39 +01:00
sharedContext *C.NSOpenGLContext
2013-12-06 18:20:48 +01:00
funcs chan func()
funcsDone chan struct{}
}
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-30 19:17:39 +01:00
t.sharedContext = C.CreateGLContext(nil)
2013-12-06 18:20:48 +01:00
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-13 22:10:24 +01:00
func (t *textureFactory) useGLContext(f func()) {
2013-12-06 18:20:48 +01:00
t.funcs <- f
<-t.funcsDone
}
2014-01-03 17:47:05 +01:00
func (t *textureFactory) createGameWindow(ui *cocoaUI, width, height, scale int, title string) *GameWindow {
return runGameWindow(ui, width, height, scale, title, t.sharedContext)
2013-12-06 18:20:48 +01:00
}