ebiten/ui/cocoa/ui.go

103 lines
2.1 KiB
Go
Raw Normal View History

2013-10-12 20:17:51 +02:00
package cocoa
2013-11-22 18:56:18 +01:00
// #cgo CFLAGS: -x objective-c
2013-11-23 10:10:15 +01:00
// #cgo LDFLAGS: -framework Cocoa -framework OpenGL
//
2013-11-23 17:17:22 +01:00
// void StartApplication(void);
2013-11-23 10:10:15 +01:00
// void PollEvents(void);
2013-10-12 20:17:51 +02:00
//
import "C"
import (
2013-11-22 18:56:18 +01:00
"github.com/hajimehoshi/go-ebiten/graphics"
2013-10-14 04:34:58 +02:00
"github.com/hajimehoshi/go-ebiten/graphics/opengl"
"github.com/hajimehoshi/go-ebiten/ui"
2013-12-06 18:20:48 +01:00
"image"
2013-10-12 20:17:51 +02:00
)
2013-12-11 14:31:13 +01:00
type cocoaUI struct {
2013-12-18 10:05:28 +01:00
textureFactory *textureFactory
2013-12-16 01:39:49 +01:00
textureFactoryEvents chan interface{}
2013-12-18 10:05:28 +01:00
graphicsDevice *opengl.Device
2013-10-13 16:06:05 +02:00
}
2013-12-11 14:31:13 +01:00
var currentUI *cocoaUI
2013-10-13 16:06:05 +02:00
2013-12-11 14:31:13 +01:00
func getCurrentUI() *cocoaUI {
2013-10-14 17:49:30 +02:00
if currentUI != nil {
2013-12-11 14:31:13 +01:00
return currentUI
2013-10-14 17:49:30 +02:00
}
2013-12-11 14:31:13 +01:00
currentUI = &cocoaUI{}
2013-10-14 17:49:30 +02:00
2013-11-23 17:17:22 +01:00
C.StartApplication()
2013-12-11 14:31:13 +01:00
currentUI.textureFactory = runTextureFactory()
2013-12-13 22:10:24 +01:00
currentUI.textureFactory.useGLContext(func() {
2013-12-11 14:31:13 +01:00
currentUI.graphicsDevice = opengl.NewDevice()
2013-12-06 18:20:48 +01:00
})
2013-12-11 14:31:13 +01:00
return currentUI
}
func UI() ui.UI {
return getCurrentUI()
}
2013-11-29 19:21:10 +01:00
2013-12-11 14:31:13 +01:00
func TextureFactory() graphics.TextureFactory {
return getCurrentUI()
2013-10-14 17:49:30 +02:00
}
2013-12-30 19:17:39 +01:00
func (u *cocoaUI) CreateGameWindow(width, height, scale int, title string) ui.GameWindow {
2013-12-09 15:52:14 +01:00
return u.textureFactory.createWindow(u, width, height, scale, title)
}
2013-12-11 14:31:13 +01:00
func (u *cocoaUI) PollEvents() {
2013-11-23 10:10:15 +01:00
C.PollEvents()
2013-10-14 17:49:30 +02:00
}
2013-12-16 01:39:49 +01:00
func (u *cocoaUI) Events() <-chan interface{} {
if u.textureFactoryEvents != nil {
return u.textureFactoryEvents
}
u.textureFactoryEvents = make(chan interface{})
return u.textureFactoryEvents
2013-12-11 14:31:13 +01:00
}
2013-12-18 10:05:28 +01:00
func (u *cocoaUI) CreateTexture(tag interface{}, img image.Image, filter graphics.Filter) {
2013-12-07 17:35:24 +01:00
go func() {
var id graphics.TextureId
var err error
2013-12-13 22:10:24 +01:00
u.textureFactory.useGLContext(func() {
2013-12-18 10:05:28 +01:00
id, err = u.graphicsDevice.CreateTexture(img, filter)
2013-12-07 17:35:24 +01:00
})
if u.textureFactoryEvents == nil {
return
}
2013-12-07 17:35:24 +01:00
e := graphics.TextureCreatedEvent{
Tag: tag,
Id: id,
Error: err,
}
2013-12-16 01:39:49 +01:00
u.textureFactoryEvents <- e
2013-12-07 17:35:24 +01:00
}()
}
2013-12-11 14:31:13 +01:00
func (u *cocoaUI) CreateRenderTarget(tag interface{}, width, height int) {
2013-12-07 17:35:24 +01:00
go func() {
var id graphics.RenderTargetId
var err error
2013-12-13 22:10:24 +01:00
u.textureFactory.useGLContext(func() {
2013-12-09 01:45:40 +01:00
id, err = u.graphicsDevice.CreateRenderTarget(width, height)
2013-12-07 17:35:24 +01:00
})
if u.textureFactoryEvents == nil {
return
}
2013-12-07 17:35:24 +01:00
e := graphics.RenderTargetCreatedEvent{
Tag: tag,
Id: id,
Error: err,
}
2013-12-16 01:39:49 +01:00
u.textureFactoryEvents <- e
2013-12-07 17:35:24 +01:00
}()
2013-12-06 18:20:48 +01:00
}