ebiten/example/glut/main.go
2013-06-21 23:16:52 +09:00

95 lines
1.7 KiB
Go

package main
// #cgo LDFLAGS: -framework GLUT -framework OpenGL
//
// #include <stdlib.h>
// #include <GLUT/glut.h>
//
// void display(void);
// void idle(void);
//
// static void setGlutFuncs(void) {
// glutDisplayFunc(display);
// glutIdleFunc(idle);
// }
//
import "C"
import (
"github.com/hajimehoshi/go.ebiten"
"github.com/hajimehoshi/go.ebiten/example/game"
"github.com/hajimehoshi/go.ebiten/graphics"
"os"
"runtime"
"unsafe"
)
type GlutUI struct {
device graphics.Device
}
var currentUI *GlutUI
//export display
func display() {
currentUI.device.Update()
C.glutSwapBuffers()
}
//export idle
func idle() {
C.glutPostRedisplay()
}
func (ui *GlutUI) Init(screenWidth, screenHeight, screenScale int) {
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])
C.glutInitDisplayMode(C.GLUT_RGBA)
C.glutInitWindowSize(
C.int(screenWidth*screenScale),
C.int(screenHeight*screenScale))
title := C.CString("Ebiten Demo")
defer C.free(unsafe.Pointer(title))
C.glutCreateWindow(title)
C.setGlutFuncs()
}
func (ui *GlutUI) Run(device graphics.Device) {
ui.device = device
C.glutMainLoop()
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
gameName := ""
if 2 <= len(os.Args) {
gameName = os.Args[1]
}
var gm ebiten.Game
switch gameName {
case "sprites":
gm = game.NewSprites()
default:
gm = game.NewRotatingImage()
}
screenScale := 2
currentUI = &GlutUI{}
currentUI.Init(gm.ScreenWidth(), gm.ScreenHeight(), screenScale)
ebiten.OpenGLRun(gm, currentUI, screenScale)
}