ebiten/ui/cocoa/window.go

185 lines
3.8 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"
//
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
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{}
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,
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)) {
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-10 16:18:08 +01:00
/*//export ebiten_ScreenSizeUpdated
func ebiten_ScreenSizeUpdated(nativeWindow unsafe.Pointer, width, height int) {
u := currentUI
e := ui.ScreenSizeUpdatedEvent{width, height}
u.windowEvents.notifyScreenSizeUpdated(e)
}*/
2013-12-15 17:13:18 +01:00
var cocoaKeyCodeToKey = map[int]ui.Key{
123: ui.KeyLeft,
124: ui.KeyRight,
125: ui.KeyUp,
126: ui.KeyDown,
}
//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{}{}
}
//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)
}
//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 {
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
func ebiten_WindowClosed(nativeWindow unsafe.Pointer) {
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
}