ebiten/ui/cocoa/game_window.go

204 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);
//
2014-01-03 17:47:05 +01:00
// NSOpenGLContext* GetGLContext(EbitenGameWindow* window);
2013-12-30 19:17:39 +01:00
// 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 {
graphicsDevice *opengl.Device
screenWidth int
screenHeight int
screenScale int
closed bool
native *C.EbitenGameWindow
pressedKeys map[ui.Key]struct{}
context *opengl.Context
funcs chan func()
funcsDone 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
func runGameWindow(graphicsDevice *opengl.Device, width, height, scale int, title string, sharedContext *C.NSOpenGLContext) *GameWindow {
2014-01-03 17:47:05 +01:00
w := &GameWindow{
graphicsDevice: graphicsDevice,
screenWidth: width,
screenHeight: height,
screenScale: scale,
closed: false,
pressedKeys: map[ui.Key]struct{}{},
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)
2014-01-03 17:47:05 +01:00
w.native = C.CreateGameWindow(C.size_t(width*scale),
2013-12-09 15:52:14 +01:00
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() {
w.context = w.graphicsDevice.CreateContext(width, height, scale)
2013-12-09 15:52:14 +01:00
})
2013-12-08 14:13:48 +01:00
return w
}
2014-01-03 17:47:05 +01:00
func (w *GameWindow) 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{}{}
}
}
}
2014-01-03 17:47:05 +01:00
func (w *GameWindow) Draw(f func(graphics.Context)) {
if w.closed {
return
}
2013-12-13 22:10:24 +01:00
w.useGLContext(func() {
w.graphicsDevice.Update(w.context, f)
2013-12-09 14:40:54 +01:00
})
}
2014-01-03 17:47:05 +01:00
func (w *GameWindow) useGLContext(f func()) {
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
}
//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 {
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
}
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]
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
}