ebiten/example/glut/main.go

178 lines
3.5 KiB
Go
Raw Normal View History

2013-06-17 16:10:55 +02:00
package main
// #cgo LDFLAGS: -framework GLUT -framework OpenGL
//
// #include <stdlib.h>
// #include <GLUT/glut.h>
//
// void display(void);
// void mouse(int button, int state, 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);
// glutMouseFunc(mouse);
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"
2013-06-24 15:27:43 +02:00
"github.com/hajimehoshi/go.ebiten/example/game/blank"
2013-06-23 16:43:44 +02:00
"github.com/hajimehoshi/go.ebiten/example/game/monochrome"
2013-06-22 19:01:26 +02:00
"github.com/hajimehoshi/go.ebiten/example/game/rects"
2013-06-22 16:16:01 +02:00
"github.com/hajimehoshi/go.ebiten/example/game/rotating"
"github.com/hajimehoshi/go.ebiten/example/game/sprites"
2013-06-20 16:29:51 +02:00
"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-19 16:49:24 +02:00
"runtime"
2013-06-17 16:10:55 +02:00
"unsafe"
)
2013-06-25 03:27:32 +02:00
type GlutInputEventState int
const (
GlutInputEventStateUp GlutInputEventState = iota
GlutInputEventStateDown
)
type GlutInputEvent struct {
State GlutInputEventState
X int
Y int
}
2013-06-19 16:51:41 +02:00
type GlutUI struct {
2013-06-25 03:27:32 +02:00
screenScale int
device graphics.Device
glutInputEventCh chan GlutInputEvent
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-06-26 18:13:09 +02:00
// TODO: Use channels?
2013-06-18 17:48:41 +02:00
currentUI.device.Update()
2013-06-17 16:51:15 +02:00
C.glutSwapBuffers()
}
//export mouse
2013-06-25 03:27:32 +02:00
func mouse(button, glutState, x, y C.int) {
var state GlutInputEventState
switch glutState {
case C.GLUT_UP:
state = GlutInputEventStateUp
case C.GLUT_DOWN:
state = GlutInputEventStateDown
default:
panic("invalid glutState")
}
currentUI.glutInputEventCh <- GlutInputEvent{
State: state,
X: int(x),
Y: int(y),
}
}
2013-06-17 16:51:15 +02:00
//export idle
func idle() {
C.glutPostRedisplay()
2013-06-17 16:10:55 +02:00
}
2013-06-26 18:13:09 +02:00
func NewGlutUI(screenWidth, screenHeight, screenScale int) *GlutUI{
ui := &GlutUI{
screenScale: screenScale,
glutInputEventCh: make(chan GlutInputEvent, 10),
}
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))
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
title := C.CString("Ebiten Demo")
defer C.free(unsafe.Pointer(title))
C.glutCreateWindow(title)
2013-06-17 19:01:59 +02:00
C.setGlutFuncs()
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-06-19 01:49:54 +02:00
func (ui *GlutUI) Run(device graphics.Device) {
2013-06-18 17:48:41 +02:00
ui.device = device
2013-06-17 16:10:55 +02:00
C.glutMainLoop()
}
2013-06-18 17:48:41 +02:00
func main() {
2013-06-19 16:49:24 +02:00
runtime.GOMAXPROCS(runtime.NumCPU())
2013-06-21 04:02:49 +02:00
gameName := ""
if 2 <= len(os.Args) {
gameName = os.Args[1]
}
var gm ebiten.Game
switch gameName {
2013-06-24 15:27:43 +02:00
case "blank":
gm = blank.New()
2013-06-23 16:43:44 +02:00
case "monochrome":
gm = monochrome.New()
2013-06-22 19:01:26 +02:00
case "rects":
gm = rects.New()
2013-06-22 16:16:01 +02:00
case "rotating":
gm = rotating.New()
2013-06-22 19:01:26 +02:00
case "sprites":
gm = sprites.New()
2013-06-21 04:02:49 +02:00
default:
2013-06-22 16:16:01 +02:00
gm = rotating.New()
2013-06-21 04:02:49 +02:00
}
2013-06-21 16:16:52 +02:00
screenScale := 2
2013-06-26 18:13:09 +02:00
currentUI = NewGlutUI(gm.ScreenWidth(), gm.ScreenHeight(), screenScale)
2013-06-18 17:48:41 +02:00
2013-06-25 03:27:32 +02:00
input := make(chan ebiten.InputState)
go func() {
ch := currentUI.glutInputEventCh
var inputState ebiten.InputState
for {
select {
case event := <-ch:
switch event.State {
case GlutInputEventStateUp:
inputState.IsTapped = false
inputState.X = 0
inputState.Y = 0
case GlutInputEventStateDown:
inputState.IsTapped = true
inputState.X = event.X
inputState.Y = event.Y
}
default:
// do nothing
}
input <- inputState
}
}()
2013-06-26 18:13:09 +02:00
graphicsDevice := opengl.NewDevice(
gm.ScreenWidth(), gm.ScreenHeight(), screenScale)
gm.Init(graphicsDevice.TextureFactory())
ebiten.Run(gm, currentUI, screenScale, graphicsDevice, input)
2013-06-18 17:48:41 +02:00
}