2013-12-08 14:13:48 +01:00
|
|
|
package cocoa
|
|
|
|
|
|
|
|
// #include <stdlib.h>
|
|
|
|
//
|
2013-12-10 16:18:08 +01:00
|
|
|
// #include "input.h"
|
|
|
|
//
|
2013-12-08 14:13:48 +01:00
|
|
|
// void* CreateWindow(size_t width, size_t height, const char* title, void* glContext);
|
|
|
|
// void* CreateGLContext(void* sharedGLContext);
|
|
|
|
//
|
|
|
|
// void* GetGLContext(void* window);
|
|
|
|
// void UseGLContext(void* glContext);
|
|
|
|
// 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"
|
|
|
|
)
|
|
|
|
|
2013-12-09 15:52:14 +01:00
|
|
|
type Window struct {
|
2013-12-11 14:31:13 +01:00
|
|
|
ui *cocoaUI
|
2013-12-10 16:18:08 +01:00
|
|
|
screenWidth int
|
|
|
|
screenHeight int
|
|
|
|
screenScale int
|
2013-12-10 17:01:01 +01:00
|
|
|
closed bool
|
2013-12-10 16:18:08 +01:00
|
|
|
native unsafe.Pointer
|
2013-12-15 17:13:18 +01:00
|
|
|
pressedKeys map[ui.Key]struct{}
|
2013-12-15 15:41:33 +01:00
|
|
|
context *opengl.Context
|
2013-12-10 16:18:08 +01:00
|
|
|
funcs chan func()
|
|
|
|
funcsDone chan struct{}
|
2013-12-16 01:39:49 +01:00
|
|
|
events chan interface{}
|
2013-12-08 14:13:48 +01:00
|
|
|
}
|
|
|
|
|
2013-12-10 16:18:08 +01:00
|
|
|
var windows = map[unsafe.Pointer]*Window{}
|
|
|
|
|
2013-12-15 17:13:18 +01:00
|
|
|
func runWindow(cocoaUI *cocoaUI, width, height, scale int, title string, sharedContext unsafe.Pointer) *Window {
|
2013-12-09 15:52:14 +01:00
|
|
|
w := &Window{
|
2013-12-15 17:13:18 +01:00
|
|
|
ui: cocoaUI,
|
2013-12-10 16:18:08 +01:00
|
|
|
screenWidth: width,
|
|
|
|
screenHeight: height,
|
|
|
|
screenScale: scale,
|
2013-12-10 17:01:01 +01:00
|
|
|
closed: false,
|
2013-12-15 17:13:18 +01:00
|
|
|
pressedKeys: map[ui.Key]struct{}{},
|
2013-12-10 16:18:08 +01:00
|
|
|
funcs: make(chan func()),
|
|
|
|
funcsDone: make(chan struct{}),
|
2013-12-08 14:13:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cTitle := C.CString(title)
|
|
|
|
defer C.free(unsafe.Pointer(cTitle))
|
|
|
|
|
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
glContext := C.CreateGLContext(sharedContext)
|
2013-12-09 15:52:14 +01:00
|
|
|
w.native = C.CreateWindow(C.size_t(width*scale),
|
|
|
|
C.size_t(height*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)
|
|
|
|
w.loop()
|
|
|
|
}()
|
|
|
|
<-ch
|
2013-12-13 22:10:24 +01:00
|
|
|
w.useGLContext(func() {
|
2013-12-15 17:13:18 +01:00
|
|
|
w.context = w.ui.graphicsDevice.CreateContext(width, height, scale)
|
2013-12-09 15:52:14 +01:00
|
|
|
})
|
2013-12-08 14:13:48 +01:00
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2013-12-09 15:52:14 +01:00
|
|
|
func (w *Window) loop() {
|
2013-12-08 14:13:48 +01:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case f := <-w.funcs:
|
|
|
|
glContext := C.GetGLContext(w.native)
|
|
|
|
C.UseGLContext(glContext)
|
|
|
|
f()
|
|
|
|
C.UnuseGLContext()
|
|
|
|
w.funcsDone <- struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 22:10:24 +01:00
|
|
|
func (w *Window) Draw(f func(graphics.Context)) {
|
2013-12-10 17:01:01 +01:00
|
|
|
if w.closed {
|
|
|
|
return
|
|
|
|
}
|
2013-12-13 22:10:24 +01:00
|
|
|
w.useGLContext(func() {
|
|
|
|
w.ui.graphicsDevice.Update(w.context, f)
|
2013-12-09 14:40:54 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-12-13 22:10:24 +01:00
|
|
|
func (w *Window) useGLContext(f func()) {
|
2013-12-08 14:13:48 +01:00
|
|
|
w.funcs <- f
|
|
|
|
<-w.funcsDone
|
|
|
|
}
|
2013-12-10 16:18:08 +01:00
|
|
|
|
2013-12-16 01:39:49 +01:00
|
|
|
func (w *Window) Events() <-chan interface{} {
|
|
|
|
if w.events != nil {
|
|
|
|
return w.events
|
|
|
|
}
|
|
|
|
w.events = make(chan interface{})
|
|
|
|
return w.events
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Window) notify(e interface{}) {
|
|
|
|
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
|
|
|
|
func ebiten_WindowSizeUpdated(nativeWindow unsafe.Pointer, width, height int) {
|
|
|
|
w := windows[nativeWindow]
|
|
|
|
e := ui.WindowSizeUpdatedEvent{width, height}
|
|
|
|
w.notify(e)
|
|
|
|
}
|
2013-12-10 16:18:08 +01:00
|
|
|
|
2013-12-16 02:02:15 +01:00
|
|
|
func (w *Window) keyStateUpdatedEvent() ui.KeyStateUpdatedEvent {
|
|
|
|
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
|
|
|
|
func ebiten_KeyDown(nativeWindow unsafe.Pointer, keyCode int) {
|
|
|
|
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
|
|
|
|
func ebiten_KeyUp(nativeWindow unsafe.Pointer, keyCode int) {
|
|
|
|
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
|
|
|
|
func ebiten_MouseStateUpdated(nativeWindow unsafe.Pointer, 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
|
|
|
|
func ebiten_WindowClosed(nativeWindow unsafe.Pointer) {
|
|
|
|
w := windows[nativeWindow]
|
2013-12-10 17:01:01 +01:00
|
|
|
w.closed = true
|
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
|
|
|
}
|