Add examples/glut/main.go

This commit is contained in:
Hajime Hoshi 2013-06-17 23:10:55 +09:00
parent b8a1a1806c
commit e9fa26142e
4 changed files with 182 additions and 112 deletions

73
examples/glut/main.go Normal file
View File

@ -0,0 +1,73 @@
package main
// #cgo LDFLAGS: -framework GLUT -framework OpenGL
//
// #include <stdlib.h>
// #include <GLUT/glut.h>
//
// void display(void);
//
// static void setDisplayFunc(void) {
// glutDisplayFunc(display);
// }
//
import "C"
import (
"image/color"
"os"
"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) {
g.Fill(&color.RGBA{R: 0, G: 0, B: 255, A: 255})
}
//export display
func display() {
device.Update()
}
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
screenHeight := 256
screenScale := 1
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()
game := &DemoGame{}
device = graphics.NewDevice(screenWidth, screenHeight, screenScale,
func(g *graphics.GraphicsContext, offscreen *graphics.Texture) {
game.Draw(g, offscreen)
})
C.glutMainLoop()
}

View File

@ -6,7 +6,7 @@ package graphics
// #include <stdlib.h>
import "C"
type device struct {
type Device struct {
screenWidth int
screenHeight int
screenScale int
@ -15,9 +15,9 @@ type device struct {
drawFunc func(*GraphicsContext, *Texture)
}
// This method should be called on the UI thread??
func newDevice(screenWidth, screenHeight, screenScale int, drawFunc func(*GraphicsContext, *Texture)) *device {
return &device{
func NewDevice(screenWidth, screenHeight, screenScale int,
drawFunc func(*GraphicsContext, *Texture)) *Device {
device := &Device{
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
@ -25,29 +25,29 @@ func newDevice(screenWidth, screenHeight, screenScale int, drawFunc func(*Graphi
offscreenTexture: NewTexture(screenWidth, screenHeight),
drawFunc: drawFunc,
}
return device
}
// This method should be called on the UI thread??
func (d *device) Update() {
g := d.graphicsContext
// g.initialize()
func (device *Device) Update() {
g := device.graphicsContext
C.glEnable(C.GL_TEXTURE_2D)
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_NEAREST)
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MAG_FILTER, C.GL_NEAREST)
g.SetOffscreen(d.offscreenTexture)
g.SetOffscreen(device.offscreenTexture)
g.Clear()
d.drawFunc(g, d.offscreenTexture)
// TODO: lock this!
device.drawFunc(g, device.offscreenTexture)
g.flush()
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_LINEAR)
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MAG_FILTER, C.GL_LINEAR)
g.resetOffscreen()
g.Clear()
geometryMatrix := IdentityGeometryMatrix()
geometryMatrix.SetA(AffineMatrixElement(g.screenScale))
geometryMatrix.SetD(AffineMatrixElement(g.screenScale))
g.DrawTexture(d.offscreenTexture,
0, 0, d.screenWidth, d.screenHeight,
g.DrawTexture(device.offscreenTexture,
0, 0, device.screenWidth, device.screenHeight,
geometryMatrix, IdentityColorMatrix())
g.flush()
}

View File

@ -2,14 +2,13 @@ package graphics
// #cgo LDFLAGS: -framework OpenGL
//
// #include <OpenGL/gl.h>
// #include <stdlib.h>
// #include <OpenGL/gl.h>
import "C"
import (
"image/color"
"math"
"unsafe"
"../ui"
)
type GraphicsContext struct {
@ -35,19 +34,19 @@ func newGraphicsContext(screenWidth, screenHeight, screenScale int) *GraphicsCon
mainFramebuffer := C.GLint(0)
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &mainFramebuffer)
context.mainFramebuffer = C.GLuint(mainFramebuffer)
initializeShaders()
return context
}
func (context *GraphicsContext) Clear() {
ui.ExecuteOnUIThread(func() {
C.glClearColor(0, 0, 0, 1)
C.glClear(C.GL_COLOR_BUFFER_BIT)
})
}
func (context *GraphicsContext) Fill(color color.Color) {
r, g, b, a := color.RGBA()
ui.ExecuteOnUIThread(func() {
max := 65535.0
C.glClearColor(
C.GLclampf(float64(r) / max),
@ -55,10 +54,10 @@ func (context *GraphicsContext) Fill(color color.Color) {
C.GLclampf(float64(b) / max),
C.GLclampf(float64(a) / max))
C.glClear(C.GL_COLOR_BUFFER_BIT)
})
}
func (context *GraphicsContext) DrawRect(x, y, width, height int, color color.Color) {
// TODO: implement!
}
func (context *GraphicsContext) DrawTexture(texture *Texture,
@ -67,7 +66,6 @@ func (context *GraphicsContext) DrawTexture(texture *Texture,
geometryMatrix = geometryMatrix.Clone()
colorMatrix = colorMatrix.Clone()
ui.ExecuteOnUIThread(func() {
context.setShaderProgram(geometryMatrix, colorMatrix)
C.glBindTexture(C.GL_TEXTURE_2D, texture.id)
@ -109,11 +107,9 @@ func (context *GraphicsContext) DrawTexture(texture *Texture,
C.glDisableVertexAttribArray(C.GLuint(vertexAttrLocation))
C.glDisableClientState(C.GL_TEXTURE_COORD_ARRAY)
C.glDisableClientState(C.GL_VERTEX_ARRAY)
})
}
func (context *GraphicsContext) SetOffscreen(texture *Texture) {
ui.ExecuteOnUIThread(func() {
framebuffer := C.GLuint(0)
if texture != nil {
framebuffer = context.getFramebuffer(texture)
@ -121,7 +117,9 @@ func (context *GraphicsContext) SetOffscreen(texture *Texture) {
framebuffer = context.mainFramebuffer
}
C.glBindFramebuffer(C.GL_FRAMEBUFFER, framebuffer)
// TODO: assert glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE
if C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER) != C.GL_FRAMEBUFFER_COMPLETE {
panic("glBindFramebuffer failed")
}
C.glEnable(C.GL_BLEND)
C.glBlendFunc(C.GL_SRC_ALPHA, C.GL_ONE_MINUS_SRC_ALPHA)
@ -140,8 +138,8 @@ func (context *GraphicsContext) SetOffscreen(texture *Texture) {
C.glViewport(0, 0,
C.GLsizei(math.Abs(float64(width))),
C.GLsizei(math.Abs(float64(height))))
e11 := float32(2.0 / width)
e22 := float32(2.0 / height)
e11 := float32(2.0) / float32(width)
e22 := float32(2.0) / float32(height)
e41 := float32(tx)
e42 := float32(ty)
context.projectionMatrix = [...]float32{
@ -150,7 +148,6 @@ func (context *GraphicsContext) SetOffscreen(texture *Texture) {
0, 0, 1, 0,
e41, e42, 0, 1,
}
})
}
func (context *GraphicsContext) resetOffscreen() {

View File

@ -7,7 +7,7 @@ import "C"
import (
"image"
"unsafe"
"../ui"
"github.com/hajimehoshi/go-ebiten/ui"
)
func Clp2(x uint64) uint64 {
@ -47,14 +47,18 @@ func createTexture(width, height int, pixels []uint8) *Texture{
}
ch := make(chan C.GLuint)
// TODO: should wait?
go func() {
texture.id = <-ch
}()
ui.ExecuteOnUIThread(func() {
textureID := C.GLuint(0)
C.glGenTextures(1, (*C.GLuint)(&textureID))
if textureID == 0 {
panic("glGenTexture failed")
}
C.glPixelStorei(C.GL_UNPACK_ALIGNMENT, 4)
C.glBindTexture(C.GL_TEXTURE_2D, C.GLuint(textureID))
if textureID != 0 {
panic("glBindTexture failed")
}
ptr := unsafe.Pointer(nil)
if pixels != nil {
@ -71,10 +75,6 @@ func createTexture(width, height int, pixels []uint8) *Texture{
ch<- textureID
close(ch)
})
// TODO: should wait?
go func() {
texture.id = <-ch
}()
return texture
}