Refactoring

This commit is contained in:
Hajime Hoshi 2014-05-01 21:42:57 +09:00
parent 24430719ff
commit 2d6818c81e
4 changed files with 19 additions and 41 deletions

View File

@ -3,7 +3,6 @@ package opengl
import (
"github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
"github.com/hajimehoshi/go-ebiten/graphics/opengl/rendertarget"
)
type Context struct {
@ -19,7 +18,7 @@ func newContext(ids *ids, screenWidth, screenHeight, screenScale int) *Context {
ids: ids,
screenScale: screenScale,
}
mainRenderTarget := rendertarget.NewWithCurrentFramebuffer(
mainRenderTarget := newRTWithCurrentFramebuffer(
screenWidth*screenScale,
screenHeight*screenScale)
context.mainId = context.ids.AddRenderTarget(mainRenderTarget)

View File

@ -3,24 +3,22 @@ package opengl
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/texture"
"image"
"sync"
)
type ids struct {
lock sync.RWMutex
textures map[graphics.TextureId]*texture.Texture
renderTargets map[graphics.RenderTargetId]*rendertarget.RenderTarget
textures map[graphics.TextureId]*Texture
renderTargets map[graphics.RenderTargetId]*RenderTarget
renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId
counts chan int
}
func newIds() *ids {
ids := &ids{
textures: map[graphics.TextureId]*texture.Texture{},
renderTargets: map[graphics.RenderTargetId]*rendertarget.RenderTarget{},
textures: map[graphics.TextureId]*Texture{},
renderTargets: map[graphics.RenderTargetId]*RenderTarget{},
renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{},
counts: make(chan int),
}
@ -32,13 +30,13 @@ func newIds() *ids {
return ids
}
func (i *ids) textureAt(id graphics.TextureId) *texture.Texture {
func (i *ids) textureAt(id graphics.TextureId) *Texture {
i.lock.RLock()
defer i.lock.RUnlock()
return i.textures[id]
}
func (i *ids) renderTargetAt(id graphics.RenderTargetId) *rendertarget.RenderTarget {
func (i *ids) renderTargetAt(id graphics.RenderTargetId) *RenderTarget {
i.lock.RLock()
defer i.lock.RUnlock()
return i.renderTargets[id]
@ -52,7 +50,7 @@ func (i *ids) toTexture(id graphics.RenderTargetId) graphics.TextureId {
func (i *ids) CreateTexture(img image.Image, filter graphics.Filter) (
graphics.TextureId, error) {
texture, err := texture.CreateFromImage(img, filter)
texture, err := createTextureFromImage(img, filter)
if err != nil {
return 0, err
}
@ -67,11 +65,12 @@ func (i *ids) CreateTexture(img image.Image, filter graphics.Filter) (
func (i *ids) CreateRenderTarget(width, height int, filter graphics.Filter) (
graphics.RenderTargetId, error) {
texture, err := texture.Create(width, height, filter)
texture, err := createTexture(width, height, filter)
if err != nil {
return 0, err
}
renderTarget := texture.CreateRenderTarget()
framebuffer := createFramebuffer(texture.native)
renderTarget := &RenderTarget{framebuffer, texture.width, texture.height, false}
textureId := graphics.TextureId(<-i.counts)
renderTargetId := graphics.RenderTargetId(<-i.counts)
@ -86,7 +85,7 @@ func (i *ids) CreateRenderTarget(width, height int, filter graphics.Filter) (
}
// NOTE: renderTarget can't be used as a texture.
func (i *ids) AddRenderTarget(renderTarget *rendertarget.RenderTarget) graphics.RenderTargetId {
func (i *ids) AddRenderTarget(renderTarget *RenderTarget) graphics.RenderTargetId {
id := graphics.RenderTargetId(<-i.counts)
i.lock.Lock()

View File

@ -1,4 +1,4 @@
package rendertarget
package opengl
// #cgo LDFLAGS: -framework OpenGL
//
@ -11,15 +11,6 @@ import (
"math"
)
type Texture interface {
Draw(projectionMatrix [4][4]float64,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color)
DrawParts(parts []graphics.TexturePart, projectionMatrix [4][4]float64,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color)
}
type NativeTexture C.GLuint
type RenderTarget struct {
framebuffer C.GLuint
width int
@ -27,7 +18,7 @@ type RenderTarget struct {
flipY bool
}
func NewWithCurrentFramebuffer(width, height int) *RenderTarget {
func newRTWithCurrentFramebuffer(width, height int) *RenderTarget {
framebuffer := C.GLint(0)
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &framebuffer)
return &RenderTarget{C.GLuint(framebuffer), width, height, true}
@ -58,11 +49,6 @@ func createFramebuffer(nativeTexture C.GLuint) C.GLuint {
return framebuffer
}
func CreateFromTexture(native NativeTexture, width, height int) *RenderTarget {
framebuffer := createFramebuffer(C.GLuint(native))
return &RenderTarget{framebuffer, width, height, false}
}
func (r *RenderTarget) setAsViewport() {
current := C.GLint(0)
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &current)
@ -113,14 +99,14 @@ func (r *RenderTarget) Fill(red, green, blue uint8) {
C.glClear(C.GL_COLOR_BUFFER_BIT)
}
func (r *RenderTarget) DrawTexture(texture Texture,
func (r *RenderTarget) DrawTexture(texture *Texture,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
r.setAsViewport()
projectionMatrix := r.projectionMatrix()
texture.Draw(projectionMatrix, geometryMatrix, colorMatrix)
}
func (r *RenderTarget) DrawTextureParts(texture Texture,
func (r *RenderTarget) DrawTextureParts(texture *Texture,
parts []graphics.TexturePart,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
r.setAsViewport()

View File

@ -1,4 +1,4 @@
package texture
package opengl
// #cgo LDFLAGS: -framework OpenGL
//
@ -7,7 +7,6 @@ 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"
@ -64,25 +63,20 @@ func createNativeTexture(textureWidth, textureHeight int, pixels []uint8,
return nativeTexture
}
func Create(width, height int, filter graphics.Filter) (*Texture, error) {
func createTexture(width, height int, filter graphics.Filter) (*Texture, error) {
native := createNativeTexture(
graphics.AdjustSizeForTexture(width),
graphics.AdjustSizeForTexture(height), nil, filter)
return &Texture{native, width, height}, nil
}
func CreateFromImage(img image.Image, filter graphics.Filter) (*Texture, error) {
func createTextureFromImage(img image.Image, filter graphics.Filter) (*Texture, error) {
adjustedImage := graphics.AdjustImageForTexture(img)
size := adjustedImage.Bounds().Size()
native := createNativeTexture(size.X, size.Y, adjustedImage.Pix, filter)
return &Texture{native, size.X, size.Y}, nil
}
func (t *Texture) CreateRenderTarget() *rendertarget.RenderTarget {
return rendertarget.CreateFromTexture(
rendertarget.NativeTexture(t.native), t.width, t.height)
}
func (t *Texture) Draw(projectionMatrix [4][4]float64, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
quad := graphics.TextureQuadForTexture(t.width, t.height)
shader.DrawTexture(shader.NativeTexture(t.native),