ebiten/ui/cocoa/cocoa.go

140 lines
3.2 KiB
Go
Raw Normal View History

2013-10-12 20:17:51 +02:00
package cocoa
// #cgo CFLAGS: -x objective-c -fobjc-arc
// #cgo LDFLAGS: -framework Cocoa -framework OpenGL -framework QuartzCore
//
// #include <stdlib.h>
2013-10-13 16:33:22 +02:00
// #include "input.h"
2013-10-12 20:17:51 +02:00
//
2013-10-13 10:36:18 +02:00
// void Run(size_t width, size_t height, size_t scale, const char* title);
2013-10-12 20:17:51 +02:00
//
import "C"
import (
2013-10-14 04:34:58 +02:00
"github.com/hajimehoshi/go-ebiten"
"github.com/hajimehoshi/go-ebiten/graphics/opengl"
2013-10-13 16:06:05 +02:00
"time"
2013-10-13 10:36:18 +02:00
"unsafe"
2013-10-12 20:17:51 +02:00
)
2013-10-13 16:06:05 +02:00
type UI struct {
screenWidth int
screenHeight int
screenScale int
graphicsDevice *opengl.Device
2013-10-13 19:04:26 +02:00
initializing chan ebiten.Game
initialized chan ebiten.Game
updating chan ebiten.Game
updated chan ebiten.Game
2013-10-13 16:06:05 +02:00
input chan ebiten.InputState
}
var currentUI *UI
//export ebiten_EbitenOpenGLView_Initialized
func ebiten_EbitenOpenGLView_Initialized() {
if currentUI.graphicsDevice != nil {
panic("The graphics device is already initialized")
}
currentUI.graphicsDevice = opengl.NewDevice(
currentUI.screenWidth,
currentUI.screenHeight,
currentUI.screenScale)
currentUI.graphicsDevice.Init()
2013-10-13 19:04:26 +02:00
game := <-currentUI.initializing
game.Init(currentUI.graphicsDevice.TextureFactory())
currentUI.initialized <- game
2013-10-13 16:06:05 +02:00
}
//export ebiten_EbitenOpenGLView_Updating
func ebiten_EbitenOpenGLView_Updating() {
2013-10-13 19:04:26 +02:00
game := <-currentUI.updating
currentUI.graphicsDevice.Update(game.Draw)
currentUI.updated <- game
2013-10-13 16:06:05 +02:00
}
//export ebiten_EbitenOpenGLView_InputUpdated
2013-10-13 16:39:10 +02:00
func ebiten_EbitenOpenGLView_InputUpdated(inputType C.InputType, cx, cy C.int) {
2013-10-13 16:33:22 +02:00
if inputType == C.InputTypeMouseUp {
currentUI.input <- ebiten.InputState{-1, -1}
return
}
x, y := int(cx), int(cy)
x /= currentUI.screenScale
y /= currentUI.screenScale
if x < 0 {
x = 0
} else if currentUI.screenWidth <= x {
x = currentUI.screenWidth - 1
}
if y < 0 {
y = 0
} else if currentUI.screenHeight <= y {
y = currentUI.screenHeight - 1
}
currentUI.input <- ebiten.InputState{x, y}
2013-10-13 16:06:05 +02:00
}
2013-10-12 20:17:51 +02:00
func Run(game ebiten.Game, screenWidth, screenHeight, screenScale int,
title string) {
2013-10-13 16:06:05 +02:00
currentUI = &UI{
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
2013-10-13 19:04:26 +02:00
initializing: make(chan ebiten.Game),
initialized: make(chan ebiten.Game),
updating: make(chan ebiten.Game),
updated: make(chan ebiten.Game),
2013-10-13 16:06:05 +02:00
input: make(chan ebiten.InputState),
}
go func() {
frameTime := time.Duration(
int64(time.Second) / int64(ebiten.FPS))
tick := time.Tick(frameTime)
gameContext := &GameContext{
screenWidth: screenWidth,
screenHeight: screenHeight,
inputState: ebiten.InputState{-1, -1},
}
2013-10-13 19:04:26 +02:00
currentUI.initializing <- game
game = <-currentUI.initialized
2013-10-13 16:06:05 +02:00
for {
select {
case gameContext.inputState = <-currentUI.input:
case <-tick:
game.Update(gameContext)
2013-10-13 19:04:26 +02:00
case currentUI.updating <- game:
game = <-currentUI.updated
2013-10-13 16:06:05 +02:00
}
}
}()
2013-10-13 19:04:26 +02:00
cTitle := C.CString(title)
defer C.free(unsafe.Pointer(cTitle))
2013-10-13 16:06:05 +02:00
C.Run(C.size_t(screenWidth),
C.size_t(screenHeight),
C.size_t(screenScale),
cTitle)
}
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-12 20:17:51 +02:00
}