ebiten/ui/cocoa/cocoa.go

146 lines
3.1 KiB
Go
Raw Normal View History

2013-10-12 20:17:51 +02:00
package cocoa
2013-11-22 18:56:18 +01:00
// #cgo CFLAGS: -x objective-c
2013-11-23 10:10:15 +01:00
// #cgo LDFLAGS: -framework Cocoa -framework OpenGL
//
2013-10-12 20:17:51 +02:00
// #include <stdlib.h>
2013-10-13 16:33:22 +02:00
// #include "input.h"
2013-10-12 20:17:51 +02:00
//
2013-11-23 17:17:22 +01:00
// void StartApplication(void);
2013-11-24 18:12:07 +01:00
// void* CreateGLContext(void* sharedGLContext);
// void SetCurrentGLContext(void* glContext);
2013-11-25 14:27:59 +01:00
// void* CreateWindow(size_t width, size_t height, const char* title, void* sharedGLContext);
2013-11-23 10:10:15 +01:00
// void PollEvents(void);
2013-11-23 17:17:22 +01:00
// void BeginDrawing(void* window);
// void EndDrawing(void* window);
2013-10-12 20:17:51 +02:00
//
import "C"
import (
2013-10-14 04:34:58 +02:00
"github.com/hajimehoshi/go-ebiten"
2013-11-22 18:56:18 +01:00
"github.com/hajimehoshi/go-ebiten/graphics"
2013-10-14 04:34:58 +02:00
"github.com/hajimehoshi/go-ebiten/graphics/opengl"
2013-11-22 18:56:18 +01:00
"runtime"
2013-11-17 14:42:12 +01:00
"sync"
2013-10-13 10:36:18 +02:00
"unsafe"
2013-10-12 20:17:51 +02:00
)
2013-11-20 16:05:57 +01:00
func init() {
runtime.LockOSThread()
}
2013-11-17 14:42:12 +01:00
type GameContext struct {
screenWidth int
screenHeight int
inputState ebiten.InputState
}
func (context *GameContext) ScreenWidth() int {
return context.screenWidth
}
func (context *GameContext) ScreenHeight() int {
return context.screenHeight
}
func (context *GameContext) InputState() ebiten.InputState {
return context.inputState
}
2013-10-13 16:06:05 +02:00
type UI struct {
2013-11-23 11:31:10 +01:00
screenWidth int
screenHeight int
screenScale int
graphicsDevice *opengl.Device
gameContext *GameContext
gameContextLock sync.Mutex
2013-11-23 17:17:22 +01:00
window unsafe.Pointer
2013-10-13 16:06:05 +02:00
}
var currentUI *UI
2013-10-14 17:49:30 +02:00
func New(screenWidth, screenHeight, screenScale int, title string) *UI {
if currentUI != nil {
panic("UI can't be duplicated.")
}
ui := &UI{
2013-11-23 10:10:15 +01:00
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
2013-11-22 18:56:18 +01:00
gameContext: &GameContext{
screenWidth: screenWidth,
screenHeight: screenHeight,
inputState: ebiten.InputState{-1, -1},
},
2013-11-23 11:31:10 +01:00
gameContextLock: sync.Mutex{},
2013-10-14 17:49:30 +02:00
}
2013-11-23 17:17:22 +01:00
cTitle := C.CString(title)
2013-10-14 17:49:30 +02:00
defer C.free(unsafe.Pointer(cTitle))
2013-11-23 17:17:22 +01:00
C.StartApplication()
2013-11-24 18:12:07 +01:00
context := C.CreateGLContext(unsafe.Pointer(nil))
C.SetCurrentGLContext(context);
2013-11-23 11:31:10 +01:00
ui.graphicsDevice = opengl.NewDevice(
ui.screenWidth,
ui.screenHeight,
ui.screenScale)
2013-11-23 17:17:22 +01:00
2013-11-24 18:12:07 +01:00
ui.window = C.CreateWindow(C.size_t(ui.screenWidth * ui.screenScale),
C.size_t(ui.screenHeight * ui.screenScale),
cTitle,
context)
2013-11-23 17:17:22 +01:00
currentUI = ui
return ui
2013-10-14 17:49:30 +02:00
}
2013-11-23 10:10:15 +01:00
func (ui *UI) PollEvents() {
C.PollEvents()
2013-10-14 17:49:30 +02:00
}
2013-11-23 10:10:15 +01:00
func (ui *UI) InitTextures(f func(graphics.TextureFactory)) {
2013-11-23 17:17:22 +01:00
C.BeginDrawing(ui.window)
2013-11-23 10:10:15 +01:00
f(ui.graphicsDevice.TextureFactory())
2013-11-23 17:17:22 +01:00
C.EndDrawing(ui.window)
2013-11-22 18:56:18 +01:00
}
2013-11-23 10:10:15 +01:00
func (ui *UI) Update(f func(ebiten.GameContext)) {
2013-11-23 11:31:10 +01:00
ui.gameContextLock.Lock()
defer ui.gameContextLock.Unlock()
2013-11-23 10:10:15 +01:00
f(ui.gameContext)
}
2013-11-22 18:56:18 +01:00
func (ui *UI) Draw(f func(graphics.Context)) {
2013-11-23 17:17:22 +01:00
C.BeginDrawing(ui.window)
2013-11-22 18:56:18 +01:00
ui.graphicsDevice.Update(f)
2013-11-23 17:17:22 +01:00
C.EndDrawing(ui.window)
2013-10-14 17:49:30 +02:00
}
2013-11-23 10:15:39 +01:00
//export ebiten_InputUpdated
func ebiten_InputUpdated(inputType C.InputType, cx, cy C.int) {
2013-11-23 11:31:10 +01:00
ui := currentUI
ui.gameContextLock.Lock()
defer ui.gameContextLock.Unlock()
2013-10-13 16:33:22 +02:00
if inputType == C.InputTypeMouseUp {
2013-11-23 11:31:10 +01:00
ui.gameContext.inputState = ebiten.InputState{-1, -1}
2013-10-13 16:33:22 +02:00
return
}
x, y := int(cx), int(cy)
2013-11-23 11:31:10 +01:00
x /= ui.screenScale
y /= ui.screenScale
2013-10-13 16:33:22 +02:00
if x < 0 {
x = 0
2013-11-23 11:31:10 +01:00
} else if ui.screenWidth <= x {
x = ui.screenWidth - 1
2013-10-13 16:33:22 +02:00
}
if y < 0 {
y = 0
2013-11-23 11:31:10 +01:00
} else if ui.screenHeight <= y {
y = ui.screenHeight - 1
2013-10-13 16:33:22 +02:00
}
2013-11-23 11:31:10 +01:00
ui.gameContext.inputState = ebiten.InputState{x, y}
2013-10-13 16:06:05 +02:00
}