ebiten/ui/cocoa/cocoa.go

199 lines
4.7 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-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-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
inputStateUpdatedChs chan chan ui.InputStateUpdatedEvent
inputStateUpdatedNotified chan ui.InputStateUpdatedEvent
screenSizeUpdatedChs chan chan ui.ScreenSizeUpdatedEvent
screenSizeUpdatedNotified chan ui.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.")
}
u := &UI{
2013-12-01 13:42:41 +01:00
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
2013-11-29 20:24:52 +01:00
initialEventSent: false,
inputStateUpdatedChs: make(chan chan ui.InputStateUpdatedEvent),
inputStateUpdatedNotified: make(chan ui.InputStateUpdatedEvent),
screenSizeUpdatedChs: make(chan chan ui.ScreenSizeUpdatedEvent),
screenSizeUpdatedNotified: make(chan ui.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)
u.graphicsDevice = opengl.NewDevice(
u.screenWidth,
u.screenHeight,
u.screenScale)
2013-11-23 17:17:22 +01:00
u.window = C.CreateWindow(C.size_t(u.screenWidth*u.screenScale),
C.size_t(u.screenHeight*u.screenScale),
2013-11-24 18:12:07 +01:00
cTitle,
context)
currentUI = u
2013-11-29 19:21:10 +01:00
u.eventLoop()
2013-11-29 19:21:10 +01:00
return u
2013-10-14 17:49:30 +02:00
}
func (u *UI) eventLoop() {
2013-12-02 13:27:06 +01:00
go func() {
inputStateUpdated := []chan ui.InputStateUpdatedEvent{}
2013-12-02 13:27:06 +01:00
for {
select {
case ch := <-u.inputStateUpdatedChs:
2013-12-02 13:27:06 +01:00
inputStateUpdated = append(inputStateUpdated, ch)
case e := <-u.inputStateUpdatedNotified:
2013-12-02 13:27:06 +01:00
for _, ch := range inputStateUpdated {
ch <- e
close(ch)
}
inputStateUpdated = inputStateUpdated[0:0]
2013-11-29 19:21:10 +01:00
}
2013-12-02 13:27:06 +01:00
}
}()
go func() {
screenSizeUpdated := []chan ui.ScreenSizeUpdatedEvent{}
2013-12-02 13:27:06 +01:00
for {
select {
case ch := <-u.screenSizeUpdatedChs:
2013-12-02 13:27:06 +01:00
screenSizeUpdated = append(screenSizeUpdated, ch)
case e := <-u.screenSizeUpdatedNotified:
2013-12-02 13:27:06 +01:00
for _, ch := range screenSizeUpdated {
ch <- e
close(ch)
}
screenSizeUpdated = []chan ui.ScreenSizeUpdatedEvent{}
2013-11-29 20:24:52 +01:00
}
2013-11-29 19:21:10 +01:00
}
2013-12-02 13:27:06 +01:00
}()
2013-11-29 19:21:10 +01:00
}
func (u *UI) PollEvents() {
2013-11-23 10:10:15 +01:00
C.PollEvents()
if !u.initialEventSent {
e := ui.ScreenSizeUpdatedEvent{u.screenWidth, u.screenHeight}
u.notifyScreenSizeUpdated(e)
u.initialEventSent = true
2013-11-29 20:24:52 +01:00
}
2013-10-14 17:49:30 +02:00
}
func (u *UI) LoadResources(f func(graphics.TextureFactory)) {
C.BeginDrawing(u.window)
f(u.graphicsDevice.TextureFactory())
C.EndDrawing(u.window)
2013-11-22 18:56:18 +01:00
}
func (u *UI) Draw(f func(graphics.Canvas)) {
C.BeginDrawing(u.window)
u.graphicsDevice.Update(f)
C.EndDrawing(u.window)
2013-10-14 17:49:30 +02:00
}
func (u *UI) ObserveInputStateUpdated() <-chan ui.InputStateUpdatedEvent {
ch := make(chan ui.InputStateUpdatedEvent)
2013-11-29 19:21:10 +01:00
go func() {
u.inputStateUpdatedChs <- ch
2013-11-29 19:21:10 +01:00
}()
return ch
}
func (u *UI) notifyInputStateUpdated(e ui.InputStateUpdatedEvent) {
2013-11-29 19:21:10 +01:00
go func() {
u.inputStateUpdatedNotified <- e
2013-11-29 19:21:10 +01:00
}()
}
func (u *UI) ObserveScreenSizeUpdated() <-chan ui.ScreenSizeUpdatedEvent {
ch := make(chan ui.ScreenSizeUpdatedEvent)
2013-11-29 20:24:52 +01:00
go func() {
u.screenSizeUpdatedChs <- ch
2013-11-29 20:24:52 +01:00
}()
return ch
}
func (u *UI) notifyScreenSizeUpdated(e ui.ScreenSizeUpdatedEvent) {
2013-11-29 20:24:52 +01:00
go func() {
u.screenSizeUpdatedNotified <- e
2013-11-29 20:24:52 +01:00
}()
}
//export ebiten_ScreenSizeUpdated
func ebiten_ScreenSizeUpdated(width, height int) {
u := currentUI
e := ui.ScreenSizeUpdatedEvent{width, height}
u.notifyScreenSizeUpdated(e)
2013-11-29 20:24:52 +01:00
}
2013-11-23 10:15:39 +01:00
//export ebiten_InputUpdated
func ebiten_InputUpdated(inputType C.InputType, cx, cy C.int) {
u := currentUI
2013-11-23 11:31:10 +01:00
2013-10-13 16:33:22 +02:00
if inputType == C.InputTypeMouseUp {
e := ui.InputStateUpdatedEvent{-1, -1}
u.notifyInputStateUpdated(e)
2013-10-13 16:33:22 +02:00
return
}
x, y := int(cx), int(cy)
x /= u.screenScale
y /= u.screenScale
2013-10-13 16:33:22 +02:00
if x < 0 {
x = 0
} else if u.screenWidth <= x {
x = u.screenWidth - 1
2013-10-13 16:33:22 +02:00
}
if y < 0 {
y = 0
} else if u.screenHeight <= y {
y = u.screenHeight - 1
2013-10-13 16:33:22 +02:00
}
e := ui.InputStateUpdatedEvent{x, y}
u.notifyInputStateUpdated(e)
2013-10-13 16:06:05 +02:00
}