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,22 +16,25 @@ import (
type Context struct { type Context struct {
screen *RenderTarget screen *RenderTarget
screenId graphics.RenderTargetID
screenWidth int screenWidth int
screenHeight int screenHeight int
screenScale int screenScale int
textures map[graphics.TextureID]*Texture textures map[graphics.TextureID]*Texture
renderTargets map[graphics.RenderTargetID]*RenderTarget renderTargets map[graphics.RenderTargetID]*RenderTarget
renderTargetToTexture map[graphics.RenderTargetID]graphics.TextureID
currentOffscreen *RenderTarget currentOffscreen *RenderTarget
mainFramebufferTexture *RenderTarget mainFramebufferTexture *RenderTarget
} }
func newContext(screenWidth, screenHeight, screenScale int) *Context { func newContext(screenWidth, screenHeight, screenScale int) *Context {
context := &Context{ context := &Context{
screenWidth: screenWidth, screenWidth: screenWidth,
screenHeight: screenHeight, screenHeight: screenHeight,
screenScale: screenScale, screenScale: screenScale,
textures: map[graphics.TextureID]*Texture{}, textures: map[graphics.TextureID]*Texture{},
renderTargets: map[graphics.RenderTargetID]*RenderTarget{}, renderTargets: map[graphics.RenderTargetID]*RenderTarget{},
renderTargetToTexture: map[graphics.RenderTargetID]graphics.TextureID{},
} }
return context return context
} }
@ -49,14 +52,13 @@ func (context *Context) Init() {
initializeShaders() initializeShaders()
screenID := context.NewRenderTarget( context.screenId = context.NewRenderTarget(
context.screenWidth, context.screenHeight) context.screenWidth, context.screenHeight)
context.screen = context.renderTargets[screenID] context.screen = context.renderTargets[context.screenId]
} }
func (context *Context) ToTexture(renderTargetID graphics.RenderTargetID) graphics.TextureID { func (context *Context) ToTexture(renderTargetID graphics.RenderTargetID) graphics.TextureID {
renderTarget := context.renderTargets[renderTargetID] return context.renderTargetToTexture[renderTargetID]
return renderTarget.texture.ID()
} }
func (context *Context) Clear() { func (context *Context) Clear() {
@ -187,7 +189,7 @@ func (context *Context) projectionMatrix() [16]float32 {
e41 := float32(-1) e41 := float32(-1)
e42 := float32(-1) e42 := float32(-1)
if context.currentOffscreen.ID() == context.mainFramebufferTexture.ID() { if context.currentOffscreen == context.mainFramebufferTexture {
e22 *= -1 e22 *= -1
e42 += float32(texture.height) / float32(texture.textureHeight) * 2 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 { func (context *Context) NewRenderTarget(width, height int) graphics.RenderTargetID {
renderTarget := newRenderTarget(width, height) renderTarget := newRenderTarget(width, height)
context.renderTargets[renderTarget.ID()] = renderTarget renderTargetId := graphics.RenderTargetID(<-newId)
context.textures[renderTarget.texture.ID()] = renderTarget.texture textureId := graphics.TextureID(<-newId)
context.renderTargets[renderTargetId] = renderTarget
context.textures[textureId] = renderTarget.texture
context.renderTargetToTexture[renderTargetId] = textureId
context.setOffscreen(renderTarget) context.setOffscreen(renderTarget)
context.Clear() context.Clear()
context.setMainFramebufferOffscreen() context.setMainFramebufferOffscreen()
return renderTarget.ID() return renderTargetId
} }
func (context *Context) NewTextureFromImage(img image.Image) ( func (context *Context) NewTextureFromImage(img image.Image) (
@ -280,6 +285,18 @@ func (context *Context) NewTextureFromImage(img image.Image) (
if err != nil { if err != nil {
return 0, err return 0, err
} }
context.textures[texture.ID()] = texture textureId := graphics.TextureID(<-newId)
return texture.ID(), nil 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}, {0, scale, 0},
}, },
} }
context.DrawTexture(context.ToTexture(context.screen.ID()), context.DrawTexture(context.ToTexture(context.screenId),
geometryMatrix, matrix.IdentityColor()) geometryMatrix, matrix.IdentityColor())
context.flush() context.flush()
} }

View File

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