ebiten/ui/cocoa/game_window.go

269 lines
5.0 KiB
Go
Raw Normal View History

2013-12-08 14:13:48 +01:00
package cocoa
// #include <stdlib.h>
//
2013-12-10 16:18:08 +01:00
// #include "input.h"
//
2014-01-03 17:47:05 +01:00
// @class EbitenGameWindow;
2013-12-30 19:17:39 +01:00
// @class NSOpenGLContext;
2013-12-08 14:13:48 +01:00
//
2014-01-03 17:47:05 +01:00
// typedef EbitenGameWindow* EbitenGameWindowPtr;
2013-12-30 19:17:39 +01:00
//
2014-01-03 17:47:05 +01:00
// EbitenGameWindow* CreateGameWindow(size_t width, size_t height, const char* title, NSOpenGLContext* glContext);
2013-12-30 19:17:39 +01:00
// NSOpenGLContext* CreateGLContext(NSOpenGLContext* sharedGLContext);
//
// void UseGLContext(NSOpenGLContext* glContext);
2013-12-08 14:13:48 +01:00
// void UnuseGLContext(void);
//
import "C"
import (
2013-12-09 14:40:54 +01:00
"github.com/hajimehoshi/go-ebiten/graphics"
2013-12-09 15:52:14 +01:00
"github.com/hajimehoshi/go-ebiten/graphics/opengl"
2013-12-10 16:18:08 +01:00
"github.com/hajimehoshi/go-ebiten/ui"
2013-12-08 14:13:48 +01:00
"runtime"
2014-05-11 14:24:37 +02:00
"sync"
2014-01-13 08:02:44 +01:00
"time"
2013-12-08 14:13:48 +01:00
"unsafe"
)
2014-05-11 19:16:23 +02:00
type Keys map[ui.Key]struct{}
func newKeys() Keys {
return Keys(map[ui.Key]struct{}{})
}
2014-05-12 03:04:28 +02:00
func (k Keys) clone() Keys {
n := newKeys()
for key, value := range k {
n[key] = value
}
return n
}
2014-05-11 19:16:23 +02:00
func (k Keys) add(key ui.Key) {
k[key] = struct{}{}
}
func (k Keys) remove(key ui.Key) {
delete(k, key)
}
func (k Keys) Includes(key ui.Key) bool {
_, ok := k[key]
return ok
}
2014-05-12 03:04:28 +02:00
type InputState struct {
pressedKeys Keys
mouseX int
mouseY int
}
func (i *InputState) PressedKeys() ui.Keys {
return i.pressedKeys
}
func (i *InputState) MouseX() int {
return i.mouseX
}
func (i *InputState) MouseY() int {
return i.mouseY
}
func (i *InputState) setMouseXY(x, y int) {
i.mouseX = x
i.mouseY = y
}
2014-01-03 17:47:05 +01:00
type GameWindow struct {
2014-05-12 03:04:28 +02:00
width int
height int
scale int
isClosed bool
inputState *InputState
title string
native *C.EbitenGameWindow
funcs chan func(*opengl.Context)
funcsDone chan struct{}
closed chan struct{}
2014-05-11 14:24:37 +02:00
sync.RWMutex
2013-12-08 14:13:48 +01:00
}
2014-01-03 17:47:05 +01:00
var windows = map[*C.EbitenGameWindow]*GameWindow{}
2013-12-10 16:18:08 +01:00
func newGameWindow(width, height, scale int, title string) *GameWindow {
2014-05-12 03:04:28 +02:00
inputState := &InputState{
pressedKeys: newKeys(),
mouseX: -1,
mouseY: -1,
2014-05-11 14:24:37 +02:00
}
return &GameWindow{
2014-05-12 03:04:28 +02:00
width: width,
height: height,
scale: scale,
inputState: inputState,
title: title,
funcs: make(chan func(*opengl.Context)),
funcsDone: make(chan struct{}),
closed: make(chan struct{}),
2013-12-08 14:13:48 +01:00
}
}
2013-12-08 14:13:48 +01:00
2014-05-12 03:04:28 +02:00
func (w *GameWindow) IsClosed() bool {
w.RLock()
defer w.RUnlock()
return w.isClosed
}
2014-05-02 17:06:20 +02:00
func (w *GameWindow) run(sharedGLContext *C.NSOpenGLContext) {
cTitle := C.CString(w.title)
2013-12-08 14:13:48 +01:00
defer C.free(unsafe.Pointer(cTitle))
ch := make(chan struct{})
go func() {
runtime.LockOSThread()
2014-01-11 02:34:38 +01:00
glContext := C.CreateGLContext(sharedGLContext)
2014-05-11 14:24:37 +02:00
w.native = C.CreateGameWindow(
2014-05-12 03:04:28 +02:00
C.size_t(w.width*w.scale),
C.size_t(w.height*w.scale),
2013-12-08 14:13:48 +01:00
cTitle,
glContext)
2013-12-10 16:18:08 +01:00
windows[w.native] = w
2013-12-08 14:13:48 +01:00
close(ch)
2014-01-11 03:42:23 +01:00
C.UseGLContext(glContext)
2014-05-03 06:58:18 +02:00
context := opengl.NewContext(
2014-05-12 03:04:28 +02:00
w.width, w.height, w.scale)
2014-01-11 03:42:23 +01:00
C.UnuseGLContext()
defer func() {
C.UseGLContext(glContext)
context.Dispose()
C.UnuseGLContext()
}()
w.loop(context, glContext)
}()
2014-01-11 03:42:23 +01:00
<-ch
}
2014-01-11 03:42:23 +01:00
func (w *GameWindow) loop(context *opengl.Context, glContext *C.NSOpenGLContext) {
2013-12-08 14:13:48 +01:00
for {
select {
case <-w.closed:
return
2013-12-08 14:13:48 +01:00
case f := <-w.funcs:
// Wait 10 millisecond at least to avoid busy loop.
after := time.After(time.Duration(int64(time.Millisecond) * 10))
2013-12-08 14:13:48 +01:00
C.UseGLContext(glContext)
f(context)
2013-12-08 14:13:48 +01:00
C.UnuseGLContext()
<-after
2013-12-08 14:13:48 +01:00
w.funcsDone <- struct{}{}
}
}
}
2014-01-03 17:47:05 +01:00
func (w *GameWindow) Draw(f func(graphics.Context)) {
select {
case <-w.closed:
return
default:
}
2014-01-13 07:26:20 +01:00
w.useGLContext(func(context *opengl.Context) {
context.Update(f)
})
2013-12-09 14:40:54 +01:00
}
func (w *GameWindow) useGLContext(f func(*opengl.Context)) {
2013-12-08 14:13:48 +01:00
w.funcs <- f
<-w.funcsDone
}
2013-12-10 16:18:08 +01:00
2014-05-12 03:04:28 +02:00
func (w *GameWindow) InputState() ui.InputState {
2014-05-11 14:24:37 +02:00
w.RLock()
defer w.RUnlock()
2014-05-12 03:04:28 +02:00
return &InputState{
pressedKeys: w.inputState.pressedKeys.clone(),
mouseX: w.inputState.mouseX,
mouseY: w.inputState.mouseY,
}
2013-12-16 02:02:15 +01:00
}
2013-12-15 17:13:18 +01:00
var cocoaKeyCodeToKey = map[int]ui.Key{
2013-12-18 10:05:28 +01:00
49: ui.KeySpace,
2013-12-15 17:13:18 +01:00
123: ui.KeyLeft,
124: ui.KeyRight,
2013-12-19 17:23:00 +01:00
125: ui.KeyDown,
126: ui.KeyUp,
2013-12-15 17:13:18 +01:00
}
//export ebiten_KeyDown
2014-01-03 17:47:05 +01:00
func ebiten_KeyDown(nativeWindow C.EbitenGameWindowPtr, keyCode int) {
2013-12-15 17:13:18 +01:00
key, ok := cocoaKeyCodeToKey[keyCode]
if !ok {
return
}
w := windows[nativeWindow]
2014-05-11 14:24:37 +02:00
w.Lock()
defer w.Unlock()
2014-05-12 03:04:28 +02:00
w.inputState.pressedKeys.add(key)
2013-12-15 17:13:18 +01:00
}
//export ebiten_KeyUp
2014-01-03 17:47:05 +01:00
func ebiten_KeyUp(nativeWindow C.EbitenGameWindowPtr, keyCode int) {
2013-12-15 17:13:18 +01:00
key, ok := cocoaKeyCodeToKey[keyCode]
if !ok {
return
}
w := windows[nativeWindow]
2014-05-11 14:24:37 +02:00
w.Lock()
defer w.Unlock()
2014-05-12 03:04:28 +02:00
w.inputState.pressedKeys.remove(key)
2013-12-15 17:13:18 +01:00
}
//export ebiten_MouseStateUpdated
2014-01-03 17:47:05 +01:00
func ebiten_MouseStateUpdated(nativeWindow C.EbitenGameWindowPtr, inputType C.InputType, cx, cy C.int) {
2013-12-10 16:18:08 +01:00
w := windows[nativeWindow]
if inputType == C.InputTypeMouseUp {
2014-05-11 14:24:37 +02:00
w.Lock()
defer w.Unlock()
2014-05-12 03:04:28 +02:00
w.inputState.setMouseXY(-1, -1)
2013-12-10 16:18:08 +01:00
return
}
x, y := int(cx), int(cy)
2014-05-12 03:04:28 +02:00
x /= w.scale
y /= w.scale
2013-12-10 16:18:08 +01:00
if x < 0 {
x = 0
2014-05-12 03:04:28 +02:00
} else if w.width <= x {
x = w.width - 1
2013-12-10 16:18:08 +01:00
}
if y < 0 {
y = 0
2014-05-12 03:04:28 +02:00
} else if w.height <= y {
y = w.height - 1
2013-12-10 16:18:08 +01:00
}
2014-05-11 14:24:37 +02:00
w.Lock()
defer w.Unlock()
2014-05-12 03:04:28 +02:00
w.inputState.setMouseXY(x, y)
2013-12-10 16:18:08 +01:00
}
2013-12-10 16:49:30 +01:00
//export ebiten_WindowClosed
2014-01-03 17:47:05 +01:00
func ebiten_WindowClosed(nativeWindow C.EbitenGameWindowPtr) {
2013-12-10 16:49:30 +01:00
w := windows[nativeWindow]
close(w.closed)
2014-05-11 14:24:37 +02:00
w.Lock()
defer w.Unlock()
2014-05-12 03:04:28 +02:00
w.isClosed = true
2014-05-11 19:16:23 +02:00
2013-12-11 14:31:13 +01:00
delete(windows, nativeWindow)
2013-12-10 16:49:30 +01:00
}