From 790a840389eeb2b1e18d5033afc1b876d9fdf686 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 26 Oct 2013 23:18:23 +0900 Subject: [PATCH] Refactoring --- graphics/opengl/context.go | 32 +++++++++++--------------------- graphics/opengl/shader/shader.go | 12 +++++++++++- graphics/opengl/texture.go | 10 ++++------ graphics/texture/texture.go | 12 ++++-------- 4 files changed, 30 insertions(+), 36 deletions(-) diff --git a/graphics/opengl/context.go b/graphics/opengl/context.go index 0f8254513..ae2ea5cc8 100644 --- a/graphics/opengl/context.go +++ b/graphics/opengl/context.go @@ -56,8 +56,6 @@ func (context *Context) Init() { panic("creating main framebuffer failed: " + err.Error()) } - shader.Init() - context.screenId, err = context.NewRenderTarget( context.screenWidth, context.screenHeight) if err != nil { @@ -83,36 +81,30 @@ func (context *Context) Fill(r, g, b uint8) { C.glClear(C.GL_COLOR_BUFFER_BIT) } -type TextureDrawing struct { - projectionMatrix [16]float32 - geometryMatrix matrix.Geometry - colorMatrix matrix.Color -} - -func (t *TextureDrawing) Draw(native interface{}, quads []texture.Quad) { - shader.DrawTexture(uint(native.(C.GLuint)), t.projectionMatrix, quads, t.geometryMatrix, t.colorMatrix) -} - func (context *Context) DrawTexture( textureId graphics.TextureId, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { - texture, ok := context.textures[textureId] + tex, ok := context.textures[textureId] if !ok { panic("invalid texture ID") } - drawing := &TextureDrawing{context.projectionMatrix, geometryMatrix, colorMatrix} - texture.Draw(drawing.Draw) + tex.Draw(func(native interface{}, quads []texture.Quad) { + shader.DrawTexture(uint(native.(C.GLuint)), context.projectionMatrix, quads, + geometryMatrix, colorMatrix) + }) } func (context *Context) DrawTextureParts( textureId graphics.TextureId, parts []graphics.TexturePart, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { - texture, ok := context.textures[textureId] + tex, ok := context.textures[textureId] if !ok { panic("invalid texture ID") } - drawing := &TextureDrawing{context.projectionMatrix, geometryMatrix, colorMatrix} - texture.DrawParts(parts, drawing.Draw) + tex.DrawParts(parts, func(native interface{}, quads []texture.Quad) { + shader.DrawTexture(uint(native.(C.GLuint)), context.projectionMatrix, quads, + geometryMatrix, colorMatrix) + }) } func (context *Context) ResetOffscreen() { @@ -170,8 +162,6 @@ func (v *viewportSetter) Set(x, y, width, height int) { v.context.projectionMatrix[i+j*4] = float32(matrix[i][j]) } } - - // TODO: call 'setShaderProgram' here? } func (context *Context) setMainFramebufferOffscreen() { @@ -204,7 +194,7 @@ func (context *Context) NewRenderTarget(width, height int) ( func (context *Context) NewTextureFromImage(img image.Image) ( graphics.TextureId, error) { - texture, err := texture.NewFromImage(img, &NativeTextureCreator{}) + texture, err := texture.NewFromImage(img, createFromImage) if err != nil { return 0, err } diff --git a/graphics/opengl/shader/shader.go b/graphics/opengl/shader/shader.go index 83755b31e..ff66c7138 100644 --- a/graphics/opengl/shader/shader.go +++ b/graphics/opengl/shader/shader.go @@ -114,7 +114,11 @@ func createProgram(shaders ...*shader) C.GLuint { return program } -func Init() { +var ( + initialized = false +) + +func initialize() { // TODO: when should this function be called? vertexShader.id = C.glCreateShader(C.GL_VERTEX_SHADER) if vertexShader.id == 0 { @@ -139,6 +143,8 @@ func Init() { C.glDeleteShader(vertexShader.id) C.glDeleteShader(fragmentShader.id) C.glDeleteShader(colorMatrixShader.id) + + initialized = true } const ( @@ -246,6 +252,10 @@ func use(projectionMatrix [16]float32, geometryMatrix matrix.Geometry, colorMatr func DrawTexture(native uint, projectionMatrix [16]float32, quads []texture.Quad, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { + if !initialized { + initialize() + } + if len(quads) == 0 { return } diff --git a/graphics/opengl/texture.go b/graphics/opengl/texture.go index 0142f8fd3..4c8d909b4 100644 --- a/graphics/opengl/texture.go +++ b/graphics/opengl/texture.go @@ -36,19 +36,17 @@ func createNativeTexture(textureWidth, textureHeight int, pixels []uint8) C.GLui return nativeTexture } -type NativeTextureCreator struct{} - -func (creator *NativeTextureCreator) Create(textureWidth, textureHeight int) (interface{}, error) { +func create(textureWidth, textureHeight int) (interface{}, error) { return createNativeTexture(textureWidth, textureHeight, nil), nil } -func (creator *NativeTextureCreator) CreateFromImage(img *image.NRGBA) (interface{}, error) { +func createFromImage(img *image.NRGBA) (interface{}, error) { size := img.Bounds().Size() return createNativeTexture(size.X, size.Y, img.Pix), nil } func newRenderTarget(width, height int) (*rendertarget.RenderTarget, error) { - texture, err := texture.New(width, height, &NativeTextureCreator{}) + texture, err := texture.New(width, height, create) if err != nil { return nil, err } @@ -57,7 +55,7 @@ func newRenderTarget(width, height int) (*rendertarget.RenderTarget, error) { } func newRenderTargetWithFramebuffer(width, height int, framebuffer C.GLuint) (*rendertarget.RenderTarget, error) { - texture, err := texture.New(width, height, &NativeTextureCreator{}) + texture, err := texture.New(width, height, create) if err != nil { return nil, err } diff --git a/graphics/texture/texture.go b/graphics/texture/texture.go index 5ad084b22..c2ca60952 100644 --- a/graphics/texture/texture.go +++ b/graphics/texture/texture.go @@ -23,24 +23,20 @@ type Texture struct { height int } -func New(width, height int, creator interface { - Create(textureWidth, textureHeight int) (interface{}, error) -}) (*Texture, error) { +func New(width, height int, create func(textureWidth, textureHeight int) (interface{}, error)) (*Texture, error) { texture := &Texture{ width: width, height: height, } var err error - texture.native, err = creator.Create(texture.textureWidth(), texture.textureHeight()) + texture.native, err = create(texture.textureWidth(), texture.textureHeight()) if err != nil { return nil, err } return texture, nil } -func NewFromImage(img image.Image, creator interface { - CreateFromImage(img *image.NRGBA) (interface{}, error) -}) (*Texture, error) { +func NewFromImage(img image.Image, create func(img *image.NRGBA) (interface{}, error)) (*Texture, error) { size := img.Bounds().Size() width, height := size.X, size.Y texture := &Texture{ @@ -58,7 +54,7 @@ func NewFromImage(img image.Image, creator interface { } draw.Draw(adjustedImage, dstBound, img, image.ZP, draw.Src) var err error - texture.native, err = creator.CreateFromImage(adjustedImage) + texture.native, err = create(adjustedImage) if err != nil { return nil, err }