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"
|
2013-12-02 16:15:01 +01:00
|
|
|
"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-10 16:18:08 +01:00
|
|
|
textureFactory *textureFactory
|
|
|
|
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-11 14:31:13 +01:00
|
|
|
func (u *cocoaUI) CreateWindow(width, height, scale int, title string) ui.Window {
|
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-11 14:31:13 +01:00
|
|
|
func (u *cocoaUI) TextureCreated() <-chan graphics.TextureCreatedEvent {
|
|
|
|
return u.textureFactory.TextureCreated()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *cocoaUI) RenderTargetCreated() <-chan graphics.RenderTargetCreatedEvent {
|
|
|
|
return u.textureFactory.RenderTargetCreated()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *cocoaUI) CreateTexture(tag interface{}, img image.Image) {
|
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-09 01:45:40 +01:00
|
|
|
id, err = u.graphicsDevice.CreateTexture(img)
|
2013-12-07 17:35:24 +01:00
|
|
|
})
|
|
|
|
e := graphics.TextureCreatedEvent{
|
|
|
|
Tag: tag,
|
|
|
|
Id: id,
|
|
|
|
Error: err,
|
|
|
|
}
|
|
|
|
u.textureFactory.notifyTextureCreated(e)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
})
|
|
|
|
e := graphics.RenderTargetCreatedEvent{
|
|
|
|
Tag: tag,
|
|
|
|
Id: id,
|
|
|
|
Error: err,
|
|
|
|
}
|
|
|
|
u.textureFactory.notifyRenderTargetCreated(e)
|
|
|
|
}()
|
2013-12-06 18:20:48 +01:00
|
|
|
}
|