ebiten/ui/glut/glut.go

193 lines
3.8 KiB
Go
Raw Normal View History

2013-07-05 15:25:45 +02:00
// This package is experimental.
package glut
2013-06-17 16:10:55 +02:00
// #cgo LDFLAGS: -framework GLUT -framework OpenGL
//
// #include <stdlib.h>
// #include <GLUT/glut.h>
//
// void display(void);
2013-07-05 14:39:50 +02:00
// void mouse(int button, int state, int x, int y);
2013-07-02 18:27:04 +02:00
// void motion(int x, int y);
2013-06-17 16:51:15 +02:00
// void idle(void);
2013-06-17 16:10:55 +02:00
//
2013-06-17 19:01:59 +02:00
// static void setGlutFuncs(void) {
2013-06-17 16:10:55 +02:00
// glutDisplayFunc(display);
2013-07-05 14:39:50 +02:00
// glutMouseFunc(mouse);
2013-07-02 18:27:04 +02:00
// glutMotionFunc(motion);
2013-06-17 16:51:15 +02:00
// glutIdleFunc(idle);
// }
//
2013-06-17 16:10:55 +02:00
import "C"
import (
2013-06-20 16:29:51 +02:00
"github.com/hajimehoshi/go.ebiten"
"github.com/hajimehoshi/go.ebiten/graphics"
2013-06-26 18:13:09 +02:00
"github.com/hajimehoshi/go.ebiten/graphics/opengl"
2013-06-17 16:10:55 +02:00
"os"
2013-06-26 18:46:13 +02:00
"time"
2013-06-17 16:10:55 +02:00
"unsafe"
)
2013-07-05 15:25:45 +02:00
type glutInputEvent struct {
2013-07-05 14:39:50 +02:00
IsActive bool
2013-07-06 15:21:23 +02:00
X int
Y int
2013-06-25 03:27:32 +02:00
}
2013-06-19 16:51:41 +02:00
type GlutUI struct {
2013-10-05 18:47:19 +02:00
screenScale int
glutInputting chan glutInputEvent
graphicsDevice *opengl.Device
2013-10-06 11:22:45 +02:00
updating chan func(graphics.Context)
updated chan bool
2013-06-17 16:10:55 +02:00
}
2013-06-18 17:48:41 +02:00
var currentUI *GlutUI
2013-06-17 16:10:55 +02:00
//export display
func display() {
2013-10-06 11:22:45 +02:00
draw := <-currentUI.updating
currentUI.graphicsDevice.Update(draw)
currentUI.updated <- true
2013-06-17 16:51:15 +02:00
C.glutSwapBuffers()
}
2013-07-05 14:39:50 +02:00
//export mouse
func mouse(button, state, x, y C.int) {
2013-10-11 14:31:23 +02:00
event := glutInputEvent{false, 0, 0}
2013-07-05 14:46:52 +02:00
if state == C.GLUT_DOWN {
event.IsActive = true
event.X = int(x)
event.Y = int(y)
2013-07-05 14:39:50 +02:00
}
2013-07-05 14:46:52 +02:00
currentUI.glutInputting <- event
2013-07-05 14:39:50 +02:00
}
2013-07-02 18:27:04 +02:00
//export motion
func motion(x, y C.int) {
2013-07-05 15:25:45 +02:00
currentUI.glutInputting <- glutInputEvent{
2013-07-05 14:39:50 +02:00
IsActive: true,
2013-07-06 15:21:23 +02:00
X: int(x),
Y: int(y),
2013-06-25 03:27:32 +02:00
}
}
2013-06-17 16:51:15 +02:00
//export idle
func idle() {
C.glutPostRedisplay()
2013-06-17 16:10:55 +02:00
}
2013-07-06 18:45:19 +02:00
func new(screenWidth, screenHeight, screenScale int, title string) *GlutUI {
2013-06-26 18:13:09 +02:00
ui := &GlutUI{
2013-10-13 16:06:05 +02:00
glutInputting: make(chan glutInputEvent, 10),
updating: make(chan func(graphics.Context)),
updated: make(chan bool),
2013-06-26 18:13:09 +02:00
}
2013-06-25 03:27:32 +02:00
2013-06-17 16:10:55 +02:00
cargs := []*C.char{}
for _, arg := range os.Args {
cargs = append(cargs, C.CString(arg))
}
defer func() {
for _, carg := range cargs {
C.free(unsafe.Pointer(carg))
}
}()
cargc := C.int(len(cargs))
2013-10-11 14:31:23 +02:00
// Initialize OpenGL
2013-06-17 16:10:55 +02:00
C.glutInit(&cargc, &cargs[0])
2013-06-19 16:51:41 +02:00
C.glutInitDisplayMode(C.GLUT_RGBA)
2013-06-18 17:48:41 +02:00
C.glutInitWindowSize(
2013-06-21 16:16:52 +02:00
C.int(screenWidth*screenScale),
C.int(screenHeight*screenScale))
2013-06-17 16:10:55 +02:00
2013-07-06 18:45:19 +02:00
cTitle := C.CString(title)
defer C.free(unsafe.Pointer(cTitle))
C.glutCreateWindow(cTitle)
2013-06-17 16:10:55 +02:00
2013-06-26 18:13:09 +02:00
return ui
2013-06-18 17:48:41 +02:00
}
2013-06-17 16:10:55 +02:00
2013-10-09 16:34:11 +02:00
func Run(game ebiten.Game, screenWidth, screenHeight, screenScale int, title string) {
2013-07-06 18:45:19 +02:00
ui := new(screenWidth, screenHeight, screenScale, title)
2013-07-05 17:27:36 +02:00
currentUI = ui
2013-10-13 16:06:05 +02:00
graphicsDevice := opengl.NewDevice(
screenWidth, screenHeight, screenScale)
ui.graphicsDevice = graphicsDevice
graphicsDevice.Init()
2013-10-13 16:33:22 +02:00
2013-10-05 18:47:19 +02:00
game.Init(ui.graphicsDevice.TextureFactory())
2013-06-18 17:48:41 +02:00
2013-06-25 03:27:32 +02:00
input := make(chan ebiten.InputState)
go func() {
2013-07-05 17:27:36 +02:00
ch := ui.glutInputting
2013-06-25 03:27:32 +02:00
for {
2013-06-26 19:10:56 +02:00
event := <-ch
2013-07-05 14:46:52 +02:00
inputState := ebiten.InputState{-1, -1}
2013-07-05 14:39:50 +02:00
if event.IsActive {
x := event.X / screenScale
y := event.Y / screenScale
if x < 0 {
x = 0
} else if screenWidth <= x {
x = screenWidth - 1
}
if y < 0 {
y = 0
} else if screenHeight <= y {
y = screenHeight - 1
}
2013-07-05 14:46:52 +02:00
inputState.X = x
inputState.Y = y
2013-06-25 03:27:32 +02:00
}
2013-07-05 14:46:52 +02:00
input <- inputState
2013-06-25 03:27:32 +02:00
}
}()
2013-06-26 18:46:13 +02:00
go func() {
2013-07-01 17:42:47 +02:00
frameTime := time.Duration(
2013-10-06 11:44:15 +02:00
int64(time.Second) / int64(ebiten.FPS))
2013-10-06 11:22:45 +02:00
tick := time.Tick(frameTime)
2013-07-05 14:02:17 +02:00
gameContext := &GameContext{
2013-10-09 16:34:11 +02:00
screenWidth: screenWidth,
screenHeight: screenHeight,
inputState: ebiten.InputState{-1, -1},
2013-07-05 14:02:17 +02:00
}
2013-06-26 18:46:13 +02:00
for {
select {
2013-07-05 14:02:17 +02:00
case gameContext.inputState = <-input:
2013-10-06 11:22:45 +02:00
case <-tick:
2013-07-05 14:02:17 +02:00
game.Update(gameContext)
2013-10-09 15:35:08 +02:00
case ui.updating <- game.Draw:
2013-10-06 11:22:45 +02:00
<-ui.updated
2013-06-26 18:46:13 +02:00
}
}
2013-07-16 18:42:53 +02:00
os.Exit(0)
2013-06-26 18:46:13 +02:00
}()
2013-10-13 16:06:05 +02:00
// Set the callbacks
C.setGlutFuncs()
2013-10-13 16:33:22 +02:00
2013-07-05 15:25:45 +02:00
C.glutMainLoop()
}
type GameContext struct {
2013-10-09 16:34:11 +02:00
screenWidth int
screenHeight int
inputState ebiten.InputState
}
func (context *GameContext) ScreenWidth() int {
return context.screenWidth
}
func (context *GameContext) ScreenHeight() int {
return context.screenHeight
2013-07-05 15:25:45 +02:00
}
func (context *GameContext) InputState() ebiten.InputState {
return context.inputState
2013-06-26 18:46:13 +02:00
}