ebiten/ui/cocoa/game_window.go

227 lines
4.4 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{}{})
}
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-01-03 17:47:05 +01:00
type GameWindow struct {
2014-05-11 19:16:23 +02:00
state ui.CanvasState
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-11 14:24:37 +02:00
state := ui.CanvasState{
2014-05-11 19:16:23 +02:00
Width: width,
Height: height,
Scale: scale,
PressedKeys: newKeys(),
MouseX: -1,
MouseY: -1,
IsClosed: false,
2014-05-11 14:24:37 +02:00
}
return &GameWindow{
2014-05-11 19:16:23 +02:00
state: state,
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-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(
C.size_t(w.state.Width*w.state.Scale),
C.size_t(w.state.Height*w.state.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-11 14:24:37 +02:00
w.state.Width, w.state.Height, w.state.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-11 14:24:37 +02:00
func (w *GameWindow) State() ui.CanvasState {
w.RLock()
defer w.RUnlock()
return w.state
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-11 19:16:23 +02:00
w.state.PressedKeys.(Keys).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-11 19:16:23 +02:00
w.state.PressedKeys.(Keys).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()
w.state.MouseX = -1
w.state.MouseY = -1
2013-12-10 16:18:08 +01:00
return
}
x, y := int(cx), int(cy)
2014-05-11 14:24:37 +02:00
x /= w.state.Scale
y /= w.state.Scale
2013-12-10 16:18:08 +01:00
if x < 0 {
x = 0
2014-05-11 14:24:37 +02:00
} else if w.state.Width <= x {
x = w.state.Width - 1
2013-12-10 16:18:08 +01:00
}
if y < 0 {
y = 0
2014-05-11 14:24:37 +02:00
} else if w.state.Height <= y {
y = w.state.Height - 1
2013-12-10 16:18:08 +01:00
}
2014-05-11 14:24:37 +02:00
w.Lock()
defer w.Unlock()
w.state.MouseX = x
w.state.MouseY = 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()
w.state.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
}