Refactoring

This commit is contained in:
Hajime Hoshi 2014-01-08 18:47:38 +09:00
parent cd6db439e4
commit 92b2e0d948
4 changed files with 60 additions and 67 deletions

View File

@ -65,17 +65,20 @@ func (i *ids) CreateTexture(img image.Image, filter graphics.Filter) (
func (i *ids) CreateRenderTarget(width, height int, filter graphics.Filter) (
graphics.RenderTargetId, error) {
renderTarget, texture, err := rendertarget.Create(width, height, filter)
texture, err := texture.Create(width, height, filter)
if err != nil {
return 0, err
}
renderTargetId := graphics.RenderTargetId(<-i.counts)
renderTarget := texture.CreateRenderTarget()
textureId := graphics.TextureId(<-i.counts)
renderTargetId := graphics.RenderTargetId(<-i.counts)
i.lock.Lock()
defer i.lock.Unlock()
i.renderTargets[renderTargetId] = renderTarget
i.textures[textureId] = texture
i.renderTargets[renderTargetId] = renderTarget
i.renderTargetToTexture[renderTargetId] = textureId
return renderTargetId, nil
@ -84,5 +87,11 @@ func (i *ids) CreateRenderTarget(width, height int, filter graphics.Filter) (
func (i *ids) DeleteRenderTarget(id graphics.RenderTargetId) {
renderTarget := i.renderTargets[id]
renderTarget.Dispose()
i.lock.Lock()
defer i.lock.Unlock()
delete(i.renderTargets, id)
// TODO: Remove the related texture
}

View File

@ -1,10 +1,5 @@
package offscreen
// #cgo LDFLAGS: -framework OpenGL
//
// #include <stdlib.h>
// #include <OpenGL/gl.h>
import "C"
import (
"github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
@ -25,24 +20,14 @@ func New(screenWidth, screenHeight, screenScale int) *Offscreen {
screenScale: screenScale,
}
mainFramebuffer := C.GLint(0)
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &mainFramebuffer)
var err error
offscreen.mainFramebufferTexture, err = rendertarget.CreateWithFramebuffer(
offscreen.mainFramebufferTexture = rendertarget.NewWithCurrentFramebuffer(
screenWidth*screenScale,
screenHeight*screenScale,
texture.Framebuffer(mainFramebuffer))
if err != nil {
panic("creating main framebuffer failed: " + err.Error())
}
offscreen.currentRenderTarget = offscreen.mainFramebufferTexture
screenHeight*screenScale)
return offscreen
}
func (o *Offscreen) Set(rt *rendertarget.RenderTarget) {
C.glFlush()
o.currentRenderTarget = rt
rt.SetAsViewport()
}

View File

@ -7,31 +7,55 @@ import "C"
import (
"fmt"
"github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
)
type NativeTexture C.GLuint
type RenderTarget struct {
framebuffer texture.Framebuffer
framebuffer C.GLuint
width int
height int
}
func Create(width, height int, filter graphics.Filter) (
*RenderTarget, *texture.Texture, error) {
tex, err := texture.Create(width, height, filter)
if err != nil {
return nil, nil, err
}
framebuffer := tex.CreateFramebuffer()
return &RenderTarget{framebuffer, width, height}, tex, nil
func NewWithCurrentFramebuffer(width, height int) *RenderTarget {
framebuffer := C.GLint(0)
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &framebuffer)
return &RenderTarget{C.GLuint(framebuffer), width, height}
}
func CreateWithFramebuffer(width, height int, framebuffer texture.Framebuffer) (
*RenderTarget, error) {
return &RenderTarget{framebuffer, width, height}, nil
func createFramebuffer(nativeTexture C.GLuint) C.GLuint {
framebuffer := C.GLuint(0)
C.glGenFramebuffers(1, &framebuffer)
origFramebuffer := C.GLint(0)
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &origFramebuffer)
C.glBindFramebuffer(C.GL_FRAMEBUFFER, framebuffer)
defer C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(origFramebuffer))
C.glFramebufferTexture2D(C.GL_FRAMEBUFFER, C.GL_COLOR_ATTACHMENT0,
C.GL_TEXTURE_2D, nativeTexture, 0)
if C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER) !=
C.GL_FRAMEBUFFER_COMPLETE {
panic("creating framebuffer failed")
}
// Set this framebuffer opaque because alpha values on a target might be
// confusing.
C.glClearColor(0, 0, 0, 1)
C.glClear(C.GL_COLOR_BUFFER_BIT)
return framebuffer
}
func CreateFromTexture(native NativeTexture, width, height int) *RenderTarget {
framebuffer := createFramebuffer(C.GLuint(native))
return &RenderTarget{framebuffer, width, height}
}
func (r *RenderTarget) SetAsViewport() {
C.glFlush()
C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(r.framebuffer))
err := C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER)
if err != C.GL_FRAMEBUFFER_COMPLETE {

View File

@ -7,21 +7,20 @@ import "C"
import (
"github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
"github.com/hajimehoshi/go-ebiten/graphics/opengl/rendertarget"
"github.com/hajimehoshi/go-ebiten/graphics/opengl/shader"
"image"
"unsafe"
)
type Framebuffer C.GLuint
type Texture struct {
native shader.NativeTexture
native C.GLuint
width int
height int
}
func createNativeTexture(textureWidth, textureHeight int, pixels []uint8,
filter graphics.Filter) shader.NativeTexture {
filter graphics.Filter) C.GLuint {
nativeTexture := C.GLuint(0)
C.glGenTextures(1, (*C.GLuint)(&nativeTexture))
@ -52,7 +51,7 @@ func createNativeTexture(textureWidth, textureHeight int, pixels []uint8,
C.GLsizei(textureWidth), C.GLsizei(textureHeight),
0, C.GL_RGBA, C.GL_UNSIGNED_BYTE, ptr)
return shader.NativeTexture(nativeTexture)
return nativeTexture
}
func Create(width, height int, filter graphics.Filter) (*Texture, error) {
@ -69,38 +68,14 @@ func CreateFromImage(img image.Image, filter graphics.Filter) (*Texture, error)
return &Texture{native, size.X, size.Y}, nil
}
func createFramebuffer(nativeTexture C.GLuint) Framebuffer {
framebuffer := C.GLuint(0)
C.glGenFramebuffers(1, &framebuffer)
origFramebuffer := C.GLint(0)
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &origFramebuffer)
C.glBindFramebuffer(C.GL_FRAMEBUFFER, framebuffer)
defer C.glBindFramebuffer(C.GL_FRAMEBUFFER, C.GLuint(origFramebuffer))
C.glFramebufferTexture2D(C.GL_FRAMEBUFFER, C.GL_COLOR_ATTACHMENT0,
C.GL_TEXTURE_2D, nativeTexture, 0)
if C.glCheckFramebufferStatus(C.GL_FRAMEBUFFER) !=
C.GL_FRAMEBUFFER_COMPLETE {
panic("creating framebuffer failed")
}
// Set this framebuffer opaque because alpha values on a target might be
// confusing.
C.glClearColor(0, 0, 0, 1)
C.glClear(C.GL_COLOR_BUFFER_BIT)
return Framebuffer(framebuffer)
}
func (t *Texture) CreateFramebuffer() Framebuffer {
return createFramebuffer(C.GLuint(t.native))
func (t *Texture) CreateRenderTarget() *rendertarget.RenderTarget {
return rendertarget.CreateFromTexture(
rendertarget.NativeTexture(t.native), t.width, t.height)
}
func (t *Texture) Draw(projectionMatrix [16]float32, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
quad := graphics.TextureQuadForTexture(t.width, t.height)
shader.DrawTexture(t.native,
shader.DrawTexture(shader.NativeTexture(t.native),
projectionMatrix, []graphics.TextureQuad{quad},
geometryMatrix, colorMatrix)
}
@ -108,7 +83,7 @@ func (t *Texture) Draw(projectionMatrix [16]float32, geometryMatrix matrix.Geome
func (t *Texture) DrawParts(parts []graphics.TexturePart, projectionMatrix [16]float32,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
quads := graphics.TextureQuadsForTextureParts(parts, t.width, t.height)
shader.DrawTexture(t.native,
shader.DrawTexture(shader.NativeTexture(t.native),
projectionMatrix, quads,
geometryMatrix, colorMatrix)
}