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"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2014-01-03 17:47:05 +01:00
|
|
|
type GameWindow struct {
|
2014-01-11 03:42:23 +01:00
|
|
|
screenWidth int
|
|
|
|
screenHeight int
|
|
|
|
screenScale int
|
|
|
|
title string
|
|
|
|
native *C.EbitenGameWindow
|
|
|
|
pressedKeys map[ui.Key]struct{}
|
|
|
|
funcs chan func(*opengl.Context)
|
|
|
|
funcsDone chan struct{}
|
|
|
|
closed chan struct{}
|
|
|
|
events chan interface{}
|
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
|
|
|
|
2014-01-06 18:14:53 +01:00
|
|
|
func newGameWindow(width, height, scale int, title string) *GameWindow {
|
|
|
|
return &GameWindow{
|
|
|
|
screenWidth: width,
|
|
|
|
screenHeight: height,
|
|
|
|
screenScale: scale,
|
|
|
|
title: title,
|
|
|
|
pressedKeys: map[ui.Key]struct{}{},
|
2014-01-07 13:58:46 +01:00
|
|
|
funcs: make(chan func(*opengl.Context)),
|
2014-01-06 18:14:53 +01:00
|
|
|
funcsDone: make(chan struct{}),
|
2014-01-07 13:58:46 +01:00
|
|
|
closed: make(chan struct{}),
|
2013-12-08 14:13:48 +01:00
|
|
|
}
|
2014-01-06 18:14:53 +01:00
|
|
|
}
|
2013-12-08 14:13:48 +01:00
|
|
|
|
2014-01-11 03:42:23 +01:00
|
|
|
func (w *GameWindow) run(graphicsSharedContext *opengl.SharedContext, sharedGLContext *C.NSOpenGLContext) {
|
2014-01-06 18:14:53 +01:00
|
|
|
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-01-06 18:14:53 +01:00
|
|
|
w.native = C.CreateGameWindow(C.size_t(w.screenWidth*w.screenScale),
|
|
|
|
C.size_t(w.screenHeight*w.screenScale),
|
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)
|
|
|
|
context := graphicsSharedContext.CreateContext(
|
|
|
|
w.screenWidth, w.screenHeight, w.screenScale)
|
|
|
|
C.UnuseGLContext()
|
|
|
|
|
2014-01-11 06:48:43 +01:00
|
|
|
defer func() {
|
|
|
|
C.UseGLContext(glContext)
|
|
|
|
context.Dispose()
|
|
|
|
C.UnuseGLContext()
|
|
|
|
}()
|
2014-01-07 13:58:46 +01:00
|
|
|
|
2014-01-11 06:48:43 +01:00
|
|
|
w.loop(context, glContext)
|
2014-01-07 13:58:46 +01:00
|
|
|
}()
|
2014-01-11 03:42:23 +01:00
|
|
|
<-ch
|
|
|
|
}
|
2014-01-07 13:58:46 +01:00
|
|
|
|
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 {
|
2014-01-07 13:58:46 +01:00
|
|
|
case <-w.closed:
|
|
|
|
return
|
2013-12-08 14:13:48 +01:00
|
|
|
case f := <-w.funcs:
|
|
|
|
C.UseGLContext(glContext)
|
2014-01-07 13:58:46 +01:00
|
|
|
f(context)
|
2013-12-08 14:13:48 +01:00
|
|
|
C.UnuseGLContext()
|
|
|
|
w.funcsDone <- struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-03 17:47:05 +01:00
|
|
|
func (w *GameWindow) Draw(f func(graphics.Context)) {
|
2014-01-11 06:48:43 +01:00
|
|
|
select {
|
|
|
|
case <-w.closed:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
w.useGLContext(func(context *opengl.Context) {
|
|
|
|
context.Update(f)
|
|
|
|
})
|
|
|
|
}
|
2013-12-09 14:40:54 +01:00
|
|
|
}
|
|
|
|
|
2014-01-07 13:58:46 +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-01-03 17:47:05 +01:00
|
|
|
func (w *GameWindow) Events() <-chan interface{} {
|
2013-12-16 01:39:49 +01:00
|
|
|
if w.events != nil {
|
|
|
|
return w.events
|
|
|
|
}
|
|
|
|
w.events = make(chan interface{})
|
|
|
|
return w.events
|
|
|
|
}
|
|
|
|
|
2014-01-03 17:47:05 +01:00
|
|
|
func (w *GameWindow) notify(e interface{}) {
|
2013-12-16 01:39:49 +01:00
|
|
|
if w.events == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
w.events <- e
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2013-12-18 10:05:28 +01:00
|
|
|
// Now this function is not used anywhere.
|
|
|
|
//export ebiten_WindowSizeUpdated
|
2014-01-03 17:47:05 +01:00
|
|
|
func ebiten_WindowSizeUpdated(nativeWindow C.EbitenGameWindowPtr, width, height int) {
|
2013-12-18 10:05:28 +01:00
|
|
|
w := windows[nativeWindow]
|
|
|
|
e := ui.WindowSizeUpdatedEvent{width, height}
|
|
|
|
w.notify(e)
|
|
|
|
}
|
2013-12-10 16:18:08 +01:00
|
|
|
|
2014-01-03 17:47:05 +01:00
|
|
|
func (w *GameWindow) keyStateUpdatedEvent() ui.KeyStateUpdatedEvent {
|
2013-12-16 02:02:15 +01:00
|
|
|
keys := []ui.Key{}
|
|
|
|
for key, _ := range w.pressedKeys {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
return ui.KeyStateUpdatedEvent{
|
|
|
|
Keys: keys,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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]
|
|
|
|
w.pressedKeys[key] = struct{}{}
|
2013-12-16 02:02:15 +01:00
|
|
|
w.notify(w.keyStateUpdatedEvent())
|
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]
|
|
|
|
delete(w.pressedKeys, key)
|
2013-12-16 02:02:15 +01:00
|
|
|
w.notify(w.keyStateUpdatedEvent())
|
2013-12-15 17:13:18 +01:00
|
|
|
}
|
|
|
|
|
2013-12-15 15:41:33 +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 {
|
2013-12-15 15:41:33 +01:00
|
|
|
e := ui.MouseStateUpdatedEvent{-1, -1}
|
2013-12-15 18:42:54 +01:00
|
|
|
w.notify(e)
|
2013-12-10 16:18:08 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
x, y := int(cx), int(cy)
|
|
|
|
x /= w.screenScale
|
|
|
|
y /= w.screenScale
|
|
|
|
if x < 0 {
|
|
|
|
x = 0
|
|
|
|
} else if w.screenWidth <= x {
|
|
|
|
x = w.screenWidth - 1
|
|
|
|
}
|
|
|
|
if y < 0 {
|
|
|
|
y = 0
|
|
|
|
} else if w.screenHeight <= y {
|
|
|
|
y = w.screenHeight - 1
|
|
|
|
}
|
2013-12-15 15:41:33 +01:00
|
|
|
e := ui.MouseStateUpdatedEvent{x, y}
|
2013-12-15 18:42:54 +01:00
|
|
|
w.notify(e)
|
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]
|
2014-01-07 13:58:46 +01:00
|
|
|
close(w.closed)
|
2013-12-15 18:42:54 +01:00
|
|
|
w.notify(ui.WindowClosedEvent{})
|
2013-12-11 14:31:13 +01:00
|
|
|
delete(windows, nativeWindow)
|
2013-12-10 16:49:30 +01:00
|
|
|
}
|