ebiten/ui/glut/glut.go

178 lines
3.6 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
2013-10-25 02:30:39 +02:00
// #cgo CFLAGS: -Wno-deprecated-declarations
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-10-14 04:34:58 +02:00
"github.com/hajimehoshi/go-ebiten"
"github.com/hajimehoshi/go-ebiten/graphics/opengl"
2013-06-17 16:10:55 +02:00
"os"
"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-10-15 02:12:43 +02:00
type UI struct {
screenWidth int
screenHeight int
2013-10-05 18:47:19 +02:00
screenScale int
2013-10-15 02:12:43 +02:00
title string
initializing chan ebiten.Game
initialized chan ebiten.Game
updating chan ebiten.Game
updated chan ebiten.Game
input chan ebiten.InputState
2013-10-05 18:47:19 +02:00
graphicsDevice *opengl.Device
2013-10-15 02:12:43 +02:00
glutInputting chan glutInputEvent
2013-06-17 16:10:55 +02:00
}
2013-10-15 02:12:43 +02:00
var currentUI *UI
2013-06-17 16:51:15 +02:00
2013-10-15 02:12:43 +02:00
func New(screenWidth, screenHeight, screenScale int, title string) *UI {
if currentUI != nil {
panic("UI can't be duplicated.")
2013-07-05 14:39:50 +02:00
}
2013-10-15 02:12:43 +02:00
ui := &UI{
2013-10-15 18:12:29 +02:00
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
title: title,
initializing: make(chan ebiten.Game),
initialized: make(chan ebiten.Game),
updating: make(chan ebiten.Game),
updated: make(chan ebiten.Game),
input: make(chan ebiten.InputState),
2013-10-15 02:12:43 +02:00
glutInputting: make(chan glutInputEvent),
2013-06-25 03:27:32 +02:00
}
2013-10-15 02:12:43 +02:00
currentUI = ui
return ui
}
2013-10-15 02:12:43 +02:00
func (ui *UI) MainLoop() {
cargs := []*C.char{C.CString(os.Args[0])}
2013-06-17 16:10:55 +02:00
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-10-15 02:12:43 +02:00
C.int(ui.screenWidth*ui.screenScale),
C.int(ui.screenHeight*ui.screenScale))
2013-06-17 16:10:55 +02:00
2013-10-15 02:12:43 +02:00
cTitle := C.CString(ui.title)
2013-07-06 18:45:19 +02:00
defer C.free(unsafe.Pointer(cTitle))
C.glutCreateWindow(cTitle)
2013-06-17 16:10:55 +02:00
2013-10-15 02:12:43 +02:00
ui.graphicsDevice = opengl.NewDevice(
ui.screenWidth, ui.screenHeight, ui.screenScale)
ui.graphicsDevice.Init()
game := <-ui.initializing
2013-10-20 08:51:26 +02:00
game.InitTextures(ui.graphicsDevice.TextureFactory())
2013-10-15 02:12:43 +02:00
ui.initialized <- game
// Set the callbacks
C.setGlutFuncs()
C.glutMainLoop()
2013-06-18 17:48:41 +02:00
}
2013-06-17 16:10:55 +02:00
2013-10-15 02:12:43 +02:00
func (ui *UI) ScreenWidth() int {
return ui.screenWidth
}
2013-07-05 17:27:36 +02:00
2013-10-15 02:12:43 +02:00
func (ui *UI) ScreenHeight() int {
return ui.screenHeight
}
2013-10-13 16:33:22 +02:00
2013-10-15 02:12:43 +02:00
func (ui *UI) Initializing() chan<- ebiten.Game {
return ui.initializing
}
2013-06-18 17:48:41 +02:00
2013-10-15 02:12:43 +02:00
func (ui *UI) Initialized() <-chan ebiten.Game {
return ui.initialized
}
2013-06-25 03:27:32 +02:00
2013-10-15 02:12:43 +02:00
func (ui *UI) Updating() chan<- ebiten.Game {
return ui.updating
}
2013-06-26 18:46:13 +02:00
2013-10-15 02:12:43 +02:00
func (ui *UI) Updated() <-chan ebiten.Game {
return ui.updated
}
2013-10-13 16:33:22 +02:00
2013-10-15 02:12:43 +02:00
func (ui *UI) Input() <-chan ebiten.InputState {
return ui.input
2013-07-05 15:25:45 +02:00
}
2013-10-15 02:12:43 +02:00
func (ui *UI) normalizePoint(x, y int) (newX, newY int) {
x /= ui.screenScale
y /= ui.screenScale
if x < 0 {
x = 0
} else if ui.screenWidth <= x {
x = ui.screenWidth - 1
}
if y < 0 {
y = 0
} else if ui.screenHeight <= y {
y = ui.screenHeight - 1
}
return x, y
2013-10-09 16:34:11 +02:00
}
2013-10-15 02:12:43 +02:00
//export display
func display() {
game := <-currentUI.updating
currentUI.graphicsDevice.Update(game.Draw)
currentUI.updated <- game
C.glutSwapBuffers()
2013-10-09 16:34:11 +02:00
}
2013-10-15 02:12:43 +02:00
//export mouse
func mouse(button, state, x, y C.int) {
if state != C.GLUT_DOWN {
currentUI.input <- ebiten.InputState{-1, -1}
return
}
newX, newY := currentUI.normalizePoint(int(x), int(y))
currentUI.input <- ebiten.InputState{newX, newY}
2013-07-05 15:25:45 +02:00
}
2013-10-15 02:12:43 +02:00
//export motion
func motion(x, y C.int) {
newX, newY := currentUI.normalizePoint(int(x), int(y))
currentUI.input <- ebiten.InputState{newX, newY}
}
//export idle
func idle() {
C.glutPostRedisplay()
2013-06-26 18:46:13 +02:00
}