Remove opengl.Texture.id

This commit is contained in:
Hajime Hoshi 2013-10-20 01:52:18 +09:00
parent ec83c0ad5f
commit 6be8a2c691
3 changed files with 33 additions and 47 deletions

View File

@ -16,11 +16,13 @@ import (
type Context struct {
screen *RenderTarget
screenId graphics.RenderTargetID
screenWidth int
screenHeight int
screenScale int
textures map[graphics.TextureID]*Texture
renderTargets map[graphics.RenderTargetID]*RenderTarget
renderTargetToTexture map[graphics.RenderTargetID]graphics.TextureID
currentOffscreen *RenderTarget
mainFramebufferTexture *RenderTarget
}
@ -32,6 +34,7 @@ func newContext(screenWidth, screenHeight, screenScale int) *Context {
screenScale: screenScale,
textures: map[graphics.TextureID]*Texture{},
renderTargets: map[graphics.RenderTargetID]*RenderTarget{},
renderTargetToTexture: map[graphics.RenderTargetID]graphics.TextureID{},
}
return context
}
@ -49,14 +52,13 @@ func (context *Context) Init() {
initializeShaders()
screenID := context.NewRenderTarget(
context.screenId = context.NewRenderTarget(
context.screenWidth, context.screenHeight)
context.screen = context.renderTargets[screenID]
context.screen = context.renderTargets[context.screenId]
}
func (context *Context) ToTexture(renderTargetID graphics.RenderTargetID) graphics.TextureID {
renderTarget := context.renderTargets[renderTargetID]
return renderTarget.texture.ID()
return context.renderTargetToTexture[renderTargetID]
}
func (context *Context) Clear() {
@ -187,7 +189,7 @@ func (context *Context) projectionMatrix() [16]float32 {
e41 := float32(-1)
e42 := float32(-1)
if context.currentOffscreen.ID() == context.mainFramebufferTexture.ID() {
if context.currentOffscreen == context.mainFramebufferTexture {
e22 *= -1
e42 += float32(texture.height) / float32(texture.textureHeight) * 2
}
@ -264,14 +266,17 @@ func (context *Context) setShaderProgram(
func (context *Context) NewRenderTarget(width, height int) graphics.RenderTargetID {
renderTarget := newRenderTarget(width, height)
context.renderTargets[renderTarget.ID()] = renderTarget
context.textures[renderTarget.texture.ID()] = renderTarget.texture
renderTargetId := graphics.RenderTargetID(<-newId)
textureId := graphics.TextureID(<-newId)
context.renderTargets[renderTargetId] = renderTarget
context.textures[textureId] = renderTarget.texture
context.renderTargetToTexture[renderTargetId] = textureId
context.setOffscreen(renderTarget)
context.Clear()
context.setMainFramebufferOffscreen()
return renderTarget.ID()
return renderTargetId
}
func (context *Context) NewTextureFromImage(img image.Image) (
@ -280,6 +285,18 @@ func (context *Context) NewTextureFromImage(img image.Image) (
if err != nil {
return 0, err
}
context.textures[texture.ID()] = texture
return texture.ID(), nil
textureId := graphics.TextureID(<-newId)
context.textures[textureId] = texture
return textureId, nil
}
var newId chan int
func init() {
newId = make(chan int)
go func() {
for i := 0; ; i++ {
newId <- i
}
}()
}

View File

@ -53,7 +53,7 @@ func (device *Device) Update(draw func(graphics.Context)) {
{0, scale, 0},
},
}
context.DrawTexture(context.ToTexture(context.screen.ID()),
context.DrawTexture(context.ToTexture(context.screenId),
geometryMatrix, matrix.IdentityColor())
context.flush()
}

View File

@ -5,7 +5,6 @@ package opengl
// #include <OpenGL/gl.h>
import "C"
import (
"github.com/hajimehoshi/go-ebiten/graphics"
"image"
"unsafe"
)
@ -38,7 +37,6 @@ func adjustPixels(width, height int, pixels []uint8) []uint8 {
}
type Texture struct {
id graphics.TextureID
native C.GLuint
width int
height int
@ -47,15 +45,10 @@ type Texture struct {
}
type RenderTarget struct {
id graphics.RenderTargetID
texture *Texture
framebuffer C.GLuint
}
func (texture *Texture) ID() graphics.TextureID {
return texture.id
}
func createTexture(width, height int, pixels []uint8) *Texture {
if pixels != nil {
pixels = adjustPixels(width, height, pixels)
@ -63,7 +56,6 @@ func createTexture(width, height int, pixels []uint8) *Texture {
textureWidth := int(nextPowerOf2(uint64(width)))
textureHeight := int(nextPowerOf2(uint64(height)))
texture := &Texture{
id: 0,
width: width,
height: height,
textureWidth: textureWidth,
@ -92,8 +84,6 @@ func createTexture(width, height int, pixels []uint8) *Texture {
texture.native = nativeTexture
texture.id = graphics.TextureID(<-newID)
return texture
}
@ -101,20 +91,11 @@ func newRenderTarget(width, height int) *RenderTarget {
texture := createTexture(width, height, nil)
framebuffer := createFramebuffer(texture.native)
return &RenderTarget{
id: graphics.RenderTargetID(<-newID),
texture: texture,
framebuffer: framebuffer,
}
}
func (renderTarget *RenderTarget) ID() graphics.RenderTargetID {
return renderTarget.id
}
func (renderTarget *RenderTarget) Texture() *Texture {
return renderTarget.texture
}
type textureError string
func (err textureError) Error() string {
@ -138,7 +119,6 @@ func newTextureFromImage(img image.Image) (*Texture, error) {
func newRenderTargetWithFramebuffer(width, height int,
framebuffer C.GLuint) *RenderTarget {
texture := &Texture{
id: graphics.TextureID(<-newID),
width: width,
height: height,
textureWidth: int(nextPowerOf2(uint64(width))),
@ -167,14 +147,3 @@ func createFramebuffer(nativeTexture C.GLuint) C.GLuint {
return framebuffer
}
var newID chan int
func init() {
newID = make(chan int)
go func() {
for i := 0; ; i++ {
newID <- i
}
}()
}