ebiten/examples/glut/main.go

102 lines
1.8 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);
2013-06-17 16:51:15 +02:00
// void idle(void);
2013-06-17 16:10:55 +02:00
//
// static void setDisplayFunc(void) {
// glutDisplayFunc(display);
// }
//
2013-06-17 16:51:15 +02:00
// static void setIdleFunc(void) {
// glutIdleFunc(idle);
// }
//
2013-06-17 16:10:55 +02:00
import "C"
import (
"image/color"
"os"
2013-06-17 18:26:46 +02:00
"time"
2013-06-17 16:10:55 +02:00
"unsafe"
"github.com/hajimehoshi/go-ebiten/graphics"
)
var device *graphics.Device
type DemoGame struct {
}
func (game *DemoGame) Update() {
}
func (game *DemoGame) Draw(g *graphics.GraphicsContext, offscreen *graphics.Texture) {
2013-06-17 18:26:46 +02:00
g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255})
2013-06-17 16:10:55 +02:00
}
//export display
func display() {
device.Update()
2013-06-17 16:51:15 +02:00
C.glutSwapBuffers()
}
//export idle
func idle() {
C.glutPostRedisplay()
2013-06-17 16:10:55 +02:00
}
func main() {
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))
screenWidth := 256
2013-06-17 16:39:44 +02:00
screenHeight := 240
screenScale := 2
2013-06-17 16:10:55 +02:00
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.setDisplayFunc()
2013-06-17 16:51:15 +02:00
C.setIdleFunc()
2013-06-17 16:10:55 +02:00
2013-06-17 18:26:46 +02:00
ch := make(chan bool, 1)
2013-06-17 16:10:55 +02:00
game := &DemoGame{}
device = graphics.NewDevice(screenWidth, screenHeight, screenScale,
func(g *graphics.GraphicsContext, offscreen *graphics.Texture) {
2013-06-17 18:26:46 +02:00
ch<- true
2013-06-17 16:10:55 +02:00
game.Draw(g, offscreen)
2013-06-17 18:26:46 +02:00
<-ch
2013-06-17 16:10:55 +02:00
})
2013-06-17 18:26:46 +02:00
go func() {
const frameTime = time.Second / 60
lastTime := time.Now()
for {
ch<- true
game.Update()
<-ch
time.Sleep(frameTime - time.Since(lastTime))
lastTime = time.Now()
}
}()
2013-06-17 16:10:55 +02:00
C.glutMainLoop()
}