ebiten/examples/glut/main.go

137 lines
2.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);
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-06-17 16:51:15 +02:00
// glutIdleFunc(idle);
// }
//
2013-06-17 16:10:55 +02:00
import "C"
import (
2013-06-19 16:51:41 +02:00
"github.com/hajimehoshi/go-ebiten"
"github.com/hajimehoshi/go-ebiten/graphics"
2013-06-18 17:48:41 +02:00
"image"
2013-06-17 16:10:55 +02:00
"image/color"
2013-06-18 17:48:41 +02:00
_ "image/png"
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-19 16:51:41 +02:00
type GlutUI struct {
screenWidth int
2013-06-18 17:48:41 +02:00
screenHeight int
2013-06-19 16:51:41 +02:00
screenScale int
device graphics.Device
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-18 17:48:41 +02:00
currentUI.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
}
2013-06-18 17:48:41 +02:00
func (ui *GlutUI) Init() {
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-06-19 16:51:41 +02:00
ui.screenWidth = 256
2013-06-18 17:48:41 +02:00
ui.screenHeight = 240
2013-06-19 16:51:41 +02:00
ui.screenScale = 2
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-19 16:51:41 +02:00
C.int(ui.screenWidth*ui.screenScale),
C.int(ui.screenHeight*ui.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-18 17:48:41 +02:00
}
2013-06-17 16:10:55 +02:00
2013-06-18 17:48:41 +02:00
func (ui *GlutUI) ScreenWidth() int {
return ui.screenWidth
}
2013-06-17 18:26:46 +02:00
2013-06-18 17:48:41 +02:00
func (ui *GlutUI) ScreenHeight() int {
return ui.screenHeight
}
func (ui *GlutUI) ScreenScale() int {
return ui.screenScale
}
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
type DemoGame struct {
2013-06-19 16:08:24 +02:00
ebitenTexture graphics.Texture
2013-06-19 16:51:41 +02:00
x int
2013-06-18 17:48:41 +02:00
}
2013-06-19 16:08:24 +02:00
func (game *DemoGame) Init(tf graphics.TextureFactory) {
file, err := os.Open("ebiten.png")
if err != nil {
panic(err)
}
defer file.Close()
2013-06-19 16:51:41 +02:00
2013-06-19 16:08:24 +02:00
img, _, err := image.Decode(file)
if err != nil {
panic(err)
2013-06-18 17:48:41 +02:00
}
2013-06-19 16:08:24 +02:00
game.ebitenTexture = tf.NewTextureFromImage(img)
}
func (game *DemoGame) Update() {
2013-06-18 18:14:55 +02:00
game.x++
2013-06-18 17:48:41 +02:00
}
2013-06-19 16:08:24 +02:00
func (game *DemoGame) Draw(g graphics.GraphicsContext, offscreen graphics.TextureID) {
2013-06-18 17:48:41 +02:00
g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255})
2013-06-18 18:14:55 +02:00
geometryMatrix := graphics.IdentityGeometryMatrix()
2013-06-19 02:29:17 +02:00
geometryMatrix.SetTx(float64(game.x))
geometryMatrix.SetTy(float64(game.x))
2013-06-19 16:08:24 +02:00
g.DrawTexture(game.ebitenTexture.ID,
2013-06-19 03:31:44 +02:00
0, 0, game.ebitenTexture.Width, game.ebitenTexture.Height,
2013-06-18 18:14:55 +02:00
geometryMatrix,
2013-06-18 17:48:41 +02:00
graphics.IdentityColorMatrix())
}
func main() {
2013-06-19 16:49:24 +02:00
runtime.GOMAXPROCS(runtime.NumCPU())
2013-06-18 17:48:41 +02:00
game := &DemoGame{}
currentUI = &GlutUI{}
currentUI.Init()
2013-06-19 01:55:07 +02:00
ebiten.OpenGLRun(game, currentUI)
2013-06-18 17:48:41 +02:00
}