ebiten/ui/cocoa/cocoa.go

188 lines
4.6 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-10-12 20:17:51 +02:00
// #include <stdlib.h>
2013-10-13 16:33:22 +02:00
// #include "input.h"
2013-10-12 20:17:51 +02:00
//
2013-11-23 17:17:22 +01:00
// void StartApplication(void);
2013-11-24 18:12:07 +01:00
// void* CreateGLContext(void* sharedGLContext);
// void SetCurrentGLContext(void* glContext);
2013-11-25 14:27:59 +01:00
// void* CreateWindow(size_t width, size_t height, const char* title, void* sharedGLContext);
2013-11-23 10:10:15 +01:00
// void PollEvents(void);
2013-11-23 17:17:22 +01:00
// void BeginDrawing(void* window);
// void EndDrawing(void* window);
2013-10-12 20:17:51 +02:00
//
import "C"
import (
2013-10-14 04:34:58 +02:00
"github.com/hajimehoshi/go-ebiten"
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-11-22 18:56:18 +01:00
"runtime"
2013-10-13 10:36:18 +02:00
"unsafe"
2013-10-12 20:17:51 +02:00
)
2013-11-20 16:05:57 +01:00
func init() {
runtime.LockOSThread()
}
2013-10-13 16:06:05 +02:00
type UI struct {
2013-11-29 19:21:10 +01:00
screenWidth int
screenHeight int
screenScale int
graphicsDevice *opengl.Device
window unsafe.Pointer
2013-11-29 20:24:52 +01:00
initialEventSent bool
2013-11-29 19:21:10 +01:00
inputStateUpdatedChs chan chan ebiten.InputStateUpdatedEvent
inputStateUpdatedNotified chan ebiten.InputStateUpdatedEvent
2013-11-29 20:24:52 +01:00
screenSizeUpdatedChs chan chan ebiten.ScreenSizeUpdatedEvent
screenSizeUpdatedNotified chan ebiten.ScreenSizeUpdatedEvent
2013-10-13 16:06:05 +02:00
}
var currentUI *UI
2013-10-14 17:49:30 +02:00
func New(screenWidth, screenHeight, screenScale int, title string) *UI {
if currentUI != nil {
panic("UI can't be duplicated.")
}
ui := &UI{
2013-11-23 10:10:15 +01:00
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
2013-11-29 20:24:52 +01:00
initialEventSent: false,
2013-11-29 19:21:10 +01:00
inputStateUpdatedChs: make(chan chan ebiten.InputStateUpdatedEvent),
inputStateUpdatedNotified: make(chan ebiten.InputStateUpdatedEvent),
2013-11-29 20:24:52 +01:00
screenSizeUpdatedChs: make(chan chan ebiten.ScreenSizeUpdatedEvent),
screenSizeUpdatedNotified: make(chan ebiten.ScreenSizeUpdatedEvent),
2013-10-14 17:49:30 +02:00
}
2013-11-23 17:17:22 +01:00
cTitle := C.CString(title)
2013-10-14 17:49:30 +02:00
defer C.free(unsafe.Pointer(cTitle))
2013-11-23 17:17:22 +01:00
C.StartApplication()
2013-11-24 18:12:07 +01:00
context := C.CreateGLContext(unsafe.Pointer(nil))
2013-11-26 17:46:07 +01:00
C.SetCurrentGLContext(context)
2013-11-23 11:31:10 +01:00
ui.graphicsDevice = opengl.NewDevice(
ui.screenWidth,
ui.screenHeight,
ui.screenScale)
2013-11-23 17:17:22 +01:00
2013-11-26 17:46:07 +01:00
ui.window = C.CreateWindow(C.size_t(ui.screenWidth*ui.screenScale),
C.size_t(ui.screenHeight*ui.screenScale),
2013-11-24 18:12:07 +01:00
cTitle,
context)
2013-11-23 17:17:22 +01:00
currentUI = ui
2013-11-29 19:21:10 +01:00
go ui.chLoop()
2013-11-23 17:17:22 +01:00
return ui
2013-10-14 17:49:30 +02:00
}
2013-11-29 19:21:10 +01:00
func (ui *UI) chLoop() {
inputStateUpdated := []chan ebiten.InputStateUpdatedEvent{}
2013-11-29 20:24:52 +01:00
screenSizeUpdated := []chan ebiten.ScreenSizeUpdatedEvent{}
2013-11-29 19:21:10 +01:00
for {
select {
case ch := <-ui.inputStateUpdatedChs:
inputStateUpdated = append(inputStateUpdated, ch)
case e := <-ui.inputStateUpdatedNotified:
for _, ch := range inputStateUpdated {
ch <- e
}
2013-11-29 20:24:52 +01:00
case ch := <-ui.screenSizeUpdatedChs:
screenSizeUpdated = append(screenSizeUpdated, ch)
case e := <-ui.screenSizeUpdatedNotified:
for _, ch := range screenSizeUpdated {
ch <- e
}
2013-11-29 19:21:10 +01:00
}
}
}
2013-11-23 10:10:15 +01:00
func (ui *UI) PollEvents() {
C.PollEvents()
2013-11-29 20:24:52 +01:00
if !ui.initialEventSent {
e := ebiten.ScreenSizeUpdatedEvent{ui.screenWidth, ui.screenHeight}
ui.notifyScreenSizeUpdated(e)
ui.initialEventSent = true
}
2013-10-14 17:49:30 +02:00
}
2013-11-23 10:10:15 +01:00
func (ui *UI) InitTextures(f func(graphics.TextureFactory)) {
2013-11-23 17:17:22 +01:00
C.BeginDrawing(ui.window)
2013-11-23 10:10:15 +01:00
f(ui.graphicsDevice.TextureFactory())
2013-11-23 17:17:22 +01:00
C.EndDrawing(ui.window)
2013-11-22 18:56:18 +01:00
}
func (ui *UI) Draw(f func(graphics.Canvas)) {
2013-11-23 17:17:22 +01:00
C.BeginDrawing(ui.window)
2013-11-22 18:56:18 +01:00
ui.graphicsDevice.Update(f)
2013-11-23 17:17:22 +01:00
C.EndDrawing(ui.window)
2013-10-14 17:49:30 +02:00
}
2013-12-01 13:23:03 +01:00
func (ui *UI) ObserveInputStateUpdated() <-chan ebiten.InputStateUpdatedEvent {
2013-11-29 19:21:10 +01:00
ch := make(chan ebiten.InputStateUpdatedEvent)
go func() {
ui.inputStateUpdatedChs <- ch
}()
return ch
}
func (ui *UI) notifyInputStateUpdated(e ebiten.InputStateUpdatedEvent) {
go func() {
2013-12-01 13:23:03 +01:00
e := e
2013-11-29 19:21:10 +01:00
ui.inputStateUpdatedNotified <- e
}()
}
2013-12-01 13:23:03 +01:00
func (ui *UI) ObserveScreenSizeUpdated() <-chan ebiten.ScreenSizeUpdatedEvent {
2013-11-29 20:24:52 +01:00
ch := make(chan ebiten.ScreenSizeUpdatedEvent)
go func() {
ui.screenSizeUpdatedChs <- ch
}()
return ch
}
func (ui *UI) notifyScreenSizeUpdated(e ebiten.ScreenSizeUpdatedEvent) {
go func() {
2013-12-01 13:23:03 +01:00
e := e
2013-11-29 20:24:52 +01:00
ui.screenSizeUpdatedNotified <- e
}()
}
//export ebiten_ScreenSizeUpdated
func ebiten_ScreenSizeUpdated(width, height int) {
ui := currentUI
e := ebiten.ScreenSizeUpdatedEvent{width, height}
ui.notifyScreenSizeUpdated(e)
}
2013-11-23 10:15:39 +01:00
//export ebiten_InputUpdated
func ebiten_InputUpdated(inputType C.InputType, cx, cy C.int) {
2013-11-23 11:31:10 +01:00
ui := currentUI
2013-10-13 16:33:22 +02:00
if inputType == C.InputTypeMouseUp {
2013-11-29 19:21:10 +01:00
e := ebiten.InputStateUpdatedEvent{-1, -1}
ui.notifyInputStateUpdated(e)
2013-10-13 16:33:22 +02:00
return
}
x, y := int(cx), int(cy)
2013-11-23 11:31:10 +01:00
x /= ui.screenScale
y /= ui.screenScale
2013-10-13 16:33:22 +02:00
if x < 0 {
x = 0
2013-11-23 11:31:10 +01:00
} else if ui.screenWidth <= x {
x = ui.screenWidth - 1
2013-10-13 16:33:22 +02:00
}
if y < 0 {
y = 0
2013-11-23 11:31:10 +01:00
} else if ui.screenHeight <= y {
y = ui.screenHeight - 1
2013-10-13 16:33:22 +02:00
}
2013-11-29 19:21:10 +01:00
e := ebiten.InputStateUpdatedEvent{x, y}
ui.notifyInputStateUpdated(e)
2013-10-13 16:06:05 +02:00
}