This commit is contained in:
Hajime Hoshi 2013-06-19 23:51:41 +09:00
parent e91ab21d8d
commit 6ed9102f01
10 changed files with 90 additions and 91 deletions

View File

@ -1,9 +1,9 @@
package ebiten
import (
"time"
"github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/graphics/opengl"
"time"
)
type Game interface {
@ -26,7 +26,7 @@ func OpenGLRun(game Game, ui UI) {
func(g graphics.GraphicsContext, offscreen graphics.TextureID) {
ticket := <-ch
game.Draw(g, offscreen)
ch<- ticket
ch <- ticket
})
go func() {
@ -36,11 +36,11 @@ func OpenGLRun(game Game, ui UI) {
<-tick
ticket := <-ch
game.Update()
ch<- ticket
ch <- ticket
}
}()
game.Init(device.TextureFactory())
ch<- true
ch <- true
ui.Run(device)
}

View File

@ -15,21 +15,21 @@ package main
//
import "C"
import (
"github.com/hajimehoshi/go-ebiten"
"github.com/hajimehoshi/go-ebiten/graphics"
"image"
"image/color"
_ "image/png"
"os"
"runtime"
"unsafe"
"github.com/hajimehoshi/go-ebiten"
"github.com/hajimehoshi/go-ebiten/graphics"
)
type GlutUI struct{
screenWidth int
type GlutUI struct {
screenWidth int
screenHeight int
screenScale int
device graphics.Device
screenScale int
device graphics.Device
}
var currentUI *GlutUI
@ -57,15 +57,15 @@ func (ui *GlutUI) Init() {
}()
cargc := C.int(len(cargs))
ui.screenWidth = 256
ui.screenWidth = 256
ui.screenHeight = 240
ui.screenScale = 2
ui.screenScale = 2
C.glutInit(&cargc, &cargs[0])
C.glutInitDisplayMode(C.GLUT_RGBA);
C.glutInitDisplayMode(C.GLUT_RGBA)
C.glutInitWindowSize(
C.int(ui.screenWidth * ui.screenScale),
C.int(ui.screenHeight * ui.screenScale))
C.int(ui.screenWidth*ui.screenScale),
C.int(ui.screenHeight*ui.screenScale))
title := C.CString("Ebiten Demo")
defer C.free(unsafe.Pointer(title))
@ -93,7 +93,7 @@ func (ui *GlutUI) Run(device graphics.Device) {
type DemoGame struct {
ebitenTexture graphics.Texture
x int
x int
}
func (game *DemoGame) Init(tf graphics.TextureFactory) {

View File

@ -1,8 +1,8 @@
package graphics_test
import (
"testing"
. "."
"testing"
)
func setElements(matrix *AffineMatrix, elements [][]float64) {

View File

@ -1,8 +1,8 @@
package graphics_test
import (
"testing"
. "."
"testing"
)
func TestGeometryMatrixElements(t *testing.T) {

View File

@ -25,8 +25,8 @@ type TextureFactory interface {
}
type Texture struct {
ID TextureID
Width int
ID TextureID
Width int
Height int
}

View File

@ -10,22 +10,22 @@ import (
)
type Device struct {
screenWidth int
screenHeight int
screenScale int
graphicsContext *GraphicsContext
screenWidth int
screenHeight int
screenScale int
graphicsContext *GraphicsContext
offscreenTexture graphics.Texture
drawFunc func(graphics.GraphicsContext, graphics.TextureID)
drawFunc func(graphics.GraphicsContext, graphics.TextureID)
}
func NewDevice(screenWidth, screenHeight, screenScale int,
drawFunc func(graphics.GraphicsContext, graphics.TextureID)) *Device {
device := &Device{
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
graphicsContext: newGraphicsContext(screenWidth, screenHeight, screenScale),
drawFunc: drawFunc,
drawFunc: drawFunc,
}
device.offscreenTexture =
device.graphicsContext.NewTexture(screenWidth, screenHeight)

View File

@ -7,32 +7,32 @@ package opengl
import "C"
import (
"fmt"
"github.com/hajimehoshi/go-ebiten/graphics"
"image"
"image/color"
"unsafe"
"github.com/hajimehoshi/go-ebiten/graphics"
)
type GraphicsContext struct {
screenWidth int
screenHeight int
screenScale int
textures map[graphics.TextureID]*Texture
projectionMatrix [16]float32
screenWidth int
screenHeight int
screenScale int
textures map[graphics.TextureID]*Texture
projectionMatrix [16]float32
currentShaderProgram C.GLuint
mainFramebuffer C.GLuint
framebuffers map[C.GLuint]C.GLuint
mainFramebuffer C.GLuint
framebuffers map[C.GLuint]C.GLuint
}
// This method should be called on the UI thread.
func newGraphicsContext(screenWidth, screenHeight, screenScale int) *GraphicsContext {
context := &GraphicsContext{
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
textures: map[graphics.TextureID]*Texture{},
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
textures: map[graphics.TextureID]*Texture{},
mainFramebuffer: 0,
framebuffers: map[C.GLuint]C.GLuint{},
framebuffers: map[C.GLuint]C.GLuint{},
}
// main framebuffer should be created sooner than any other framebuffers!
mainFramebuffer := C.GLint(0)
@ -53,10 +53,10 @@ func (context *GraphicsContext) Fill(clr color.Color) {
r, g, b, a := clr.RGBA()
max := 65535.0
C.glClearColor(
C.GLclampf(float64(r) / max),
C.GLclampf(float64(g) / max),
C.GLclampf(float64(b) / max),
C.GLclampf(float64(a) / max))
C.GLclampf(float64(r)/max),
C.GLclampf(float64(g)/max),
C.GLclampf(float64(b)/max),
C.GLclampf(float64(a)/max))
C.glClear(C.GL_COLOR_BUFFER_BIT)
}
@ -69,7 +69,7 @@ func (context *GraphicsContext) DrawTexture(
srcX, srcY, srcWidth, srcHeight int,
geometryMatrix *graphics.GeometryMatrix, colorMatrix *graphics.ColorMatrix) {
geometryMatrix = geometryMatrix.Clone()
colorMatrix = colorMatrix.Clone()
colorMatrix = colorMatrix.Clone()
texture := context.textures[textureID]
@ -87,10 +87,10 @@ func (context *GraphicsContext) DrawTexture(
x2, y2,
}
tu1 := float32(srcX) / float32(texture.textureWidth)
tu2 := float32(srcX + srcWidth) / float32(texture.textureWidth)
tv1 := float32(srcY) / float32(texture.textureHeight)
tv2 := float32(srcY + srcHeight) / float32(texture.textureHeight)
tu1 := float32(srcX) / float32(texture.textureWidth)
tu2 := float32(srcX+srcWidth) / float32(texture.textureWidth)
tv1 := float32(srcY) / float32(texture.textureHeight)
tv2 := float32(srcY+srcHeight) / float32(texture.textureHeight)
texCoord := [...]float32{
tu1, tv1,
tu2, tv1,
@ -138,8 +138,7 @@ func (context *GraphicsContext) setOffscreenFramebuffer(framebuffer C.GLuint,
C.glFlush()
C.glBindFramebuffer(C.GL_FRAMEBUFFER, framebuffer)
if err := C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER);
err != C.GL_FRAMEBUFFER_COMPLETE {
if err := C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER); err != C.GL_FRAMEBUFFER_COMPLETE {
panic(fmt.Sprintf("glBindFramebuffer failed: %d", err))
}
C.glEnable(C.GL_BLEND)
@ -147,15 +146,15 @@ func (context *GraphicsContext) setOffscreenFramebuffer(framebuffer C.GLuint,
width, height, tx, ty := 0, 0, 0, 0
if framebuffer != context.mainFramebuffer {
width = textureWidth
width = textureWidth
height = textureHeight
tx = -1
ty = -1
tx = -1
ty = -1
} else {
width = context.screenWidth * context.screenScale
width = context.screenWidth * context.screenScale
height = -1 * context.screenHeight * context.screenScale
tx = -1
ty = 1
tx = -1
ty = 1
}
C.glViewport(0, 0, C.GLsizei(abs(width)), C.GLsizei(abs(height)))
e11 := float32(2.0) / float32(width)
@ -163,9 +162,9 @@ func (context *GraphicsContext) setOffscreenFramebuffer(framebuffer C.GLuint,
e41 := float32(tx)
e42 := float32(ty)
context.projectionMatrix = [...]float32{
e11, 0, 0, 0,
0, e22, 0, 0,
0, 0, 1, 0,
e11, 0, 0, 0,
0, e22, 0, 0,
0, 0, 1, 0,
e41, e42, 0, 1,
}
}
@ -196,16 +195,16 @@ func (context *GraphicsContext) setShaderProgram(
1, C.GL_FALSE,
(*C.GLfloat)(&context.projectionMatrix[0]))
a := float32(geometryMatrix.A())
b := float32(geometryMatrix.B())
c := float32(geometryMatrix.C())
d := float32(geometryMatrix.D())
a := float32(geometryMatrix.A())
b := float32(geometryMatrix.B())
c := float32(geometryMatrix.C())
d := float32(geometryMatrix.D())
tx := float32(geometryMatrix.Tx())
ty := float32(geometryMatrix.Ty())
glModelviewMatrix := [...]float32{
a, c, 0, 0,
b, d, 0, 0,
0, 0, 1, 0,
a, c, 0, 0,
b, d, 0, 0,
0, 0, 1, 0,
tx, ty, 0, 1,
}
C.glUniformMatrix4fv(getUniformLocation(program, "modelview_matrix"),
@ -241,7 +240,7 @@ func (context *GraphicsContext) setShaderProgram(
1, (*C.GLfloat)(&glColorMatrixTranslation[0]))
}
func (context *GraphicsContext) getFramebuffer(textureID C.GLuint) C.GLuint{
func (context *GraphicsContext) getFramebuffer(textureID C.GLuint) C.GLuint {
framebuffer, ok := context.framebuffers[textureID]
if ok {
return framebuffer

View File

@ -10,14 +10,14 @@ import (
)
type shader struct {
id C.GLuint
name string
id C.GLuint
name string
source string
}
var (
vertexShader = &shader{
id: 0,
id: 0,
name: "vertex_shader",
source: `
attribute /*highp*/ vec2 vertex;
@ -33,7 +33,7 @@ void main(void) {
`,
}
fragmentShader = &shader{
id: 0,
id: 0,
name: "fragment_shader",
source: `
uniform /*lowp*/ sampler2D texture;
@ -45,7 +45,7 @@ void main(void) {
`,
}
colorMatrixShader = &shader{
id: 0,
id: 0,
name: "color_matrix_shader",
source: `
uniform /*highp*/ sampler2D texture;
@ -62,7 +62,7 @@ void main(void) {
)
var (
regularShaderProgram = C.GLuint(0)
regularShaderProgram = C.GLuint(0)
colorMatrixShaderProgram = C.GLuint(0)
)

View File

@ -21,15 +21,15 @@ func Clp2(x uint64) uint64 {
}
type Texture struct {
id C.GLuint
width int
height int
textureWidth int
id C.GLuint
width int
height int
textureWidth int
textureHeight int
}
func createTexture(width, height int, pixels []uint8) *Texture {
textureWidth := int(Clp2(uint64(width)))
textureWidth := int(Clp2(uint64(width)))
textureHeight := int(Clp2(uint64(height)))
if pixels != nil {
if width != textureWidth {
@ -40,10 +40,10 @@ func createTexture(width, height int, pixels []uint8) *Texture {
}
}
texture := &Texture{
id: 0,
width: width,
height: height,
textureWidth: textureWidth,
id: 0,
width: width,
height: height,
textureWidth: textureWidth,
textureHeight: textureHeight,
}

View File

@ -1,14 +1,14 @@
package opengl_test
import (
"testing"
. "."
"testing"
)
func TestClp2(t *testing.T) {
testCases := []struct {
expected uint64
arg uint64
arg uint64
}{
{256, 255},
{256, 256},