Remove textures from ids

This commit is contained in:
Hajime Hoshi 2014-12-17 22:12:32 +09:00
parent bac4317fc5
commit f8c6958a4a
3 changed files with 11 additions and 31 deletions

View File

@ -72,7 +72,7 @@ const (
// Texture represents a texture. // Texture represents a texture.
type Texture struct { type Texture struct {
id int glTexture *opengl.Texture
} }
// RenderTarget represents a render target. // RenderTarget represents a render target.

33
ids.go
View File

@ -20,13 +20,11 @@ import (
"github.com/go-gl/gl" "github.com/go-gl/gl"
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
"github.com/hajimehoshi/ebiten/internal/opengl/internal/shader" "github.com/hajimehoshi/ebiten/internal/opengl/internal/shader"
"image"
"math" "math"
"sync" "sync"
) )
type ids struct { type ids struct {
textures map[*Texture]*opengl.Texture
renderTargets map[*RenderTarget]*opengl.RenderTarget renderTargets map[*RenderTarget]*opengl.RenderTarget
renderTargetToTexture map[*RenderTarget]*Texture renderTargetToTexture map[*RenderTarget]*Texture
lastID int lastID int
@ -35,17 +33,10 @@ type ids struct {
} }
var idsInstance = &ids{ var idsInstance = &ids{
textures: map[*Texture]*opengl.Texture{},
renderTargets: map[*RenderTarget]*opengl.RenderTarget{}, renderTargets: map[*RenderTarget]*opengl.RenderTarget{},
renderTargetToTexture: map[*RenderTarget]*Texture{}, renderTargetToTexture: map[*RenderTarget]*Texture{},
} }
func (i *ids) textureAt(texture *Texture) *opengl.Texture {
i.RLock()
defer i.RUnlock()
return i.textures[texture]
}
func (i *ids) renderTargetAt(renderTarget *RenderTarget) *opengl.RenderTarget { func (i *ids) renderTargetAt(renderTarget *RenderTarget) *opengl.RenderTarget {
i.RLock() i.RLock()
defer i.RUnlock() defer i.RUnlock()
@ -58,20 +49,6 @@ func (i *ids) toTexture(renderTarget *RenderTarget) *Texture {
return i.renderTargetToTexture[renderTarget] return i.renderTargetToTexture[renderTarget]
} }
func (i *ids) createTexture(img image.Image, filter int) (*Texture, error) {
glTexture, err := opengl.CreateTextureFromImage(img, filter)
if err != nil {
return nil, err
}
i.Lock()
defer i.Unlock()
i.lastID++
texture := &Texture{i.lastID}
i.textures[texture] = glTexture
return texture, nil
}
func (i *ids) createRenderTarget(width, height int, filter int) (*RenderTarget, error) { func (i *ids) createRenderTarget(width, height int, filter int) (*RenderTarget, error) {
glTexture, err := opengl.CreateTexture(width, height, filter) glTexture, err := opengl.CreateTexture(width, height, filter)
if err != nil { if err != nil {
@ -87,12 +64,11 @@ func (i *ids) createRenderTarget(width, height int, filter int) (*RenderTarget,
i.Lock() i.Lock()
defer i.Unlock() defer i.Unlock()
i.lastID++ texture := &Texture{glTexture}
texture := &Texture{i.lastID}
i.lastID++ i.lastID++
renderTarget := &RenderTarget{i.lastID} renderTarget := &RenderTarget{i.lastID}
i.textures[texture] = glTexture //i.textures[texture] = glTexture
i.renderTargets[renderTarget] = r i.renderTargets[renderTarget] = r
i.renderTargetToTexture[renderTarget] = texture i.renderTargetToTexture[renderTarget] = texture
@ -116,14 +92,13 @@ func (i *ids) deleteRenderTarget(renderTarget *RenderTarget) {
glRenderTarget := i.renderTargets[renderTarget] glRenderTarget := i.renderTargets[renderTarget]
texture := i.renderTargetToTexture[renderTarget] texture := i.renderTargetToTexture[renderTarget]
glTexture := i.textures[texture] glTexture := texture.glTexture
glRenderTarget.Dispose() glRenderTarget.Dispose()
glTexture.Dispose() glTexture.Dispose()
delete(i.renderTargets, renderTarget) delete(i.renderTargets, renderTarget)
delete(i.renderTargetToTexture, renderTarget) delete(i.renderTargetToTexture, renderTarget)
delete(i.textures, texture)
} }
func (i *ids) fillRenderTarget(renderTarget *RenderTarget, r, g, b uint8) error { func (i *ids) fillRenderTarget(renderTarget *RenderTarget, r, g, b uint8) error {
@ -137,7 +112,7 @@ func (i *ids) fillRenderTarget(renderTarget *RenderTarget, r, g, b uint8) error
} }
func (i *ids) drawTexture(target *RenderTarget, texture *Texture, parts []TexturePart, geo GeometryMatrix, color ColorMatrix) error { func (i *ids) drawTexture(target *RenderTarget, texture *Texture, parts []TexturePart, geo GeometryMatrix, color ColorMatrix) error {
glTexture := i.textureAt(texture) glTexture := texture.glTexture
if err := i.setViewportIfNeeded(target); err != nil { if err := i.setViewportIfNeeded(target); err != nil {
return err return err
} }

7
ui.go
View File

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"github.com/go-gl/gl" "github.com/go-gl/gl"
glfw "github.com/go-gl/glfw3" glfw "github.com/go-gl/glfw3"
"github.com/hajimehoshi/ebiten/internal/opengl"
"image" "image"
"runtime" "runtime"
) )
@ -131,7 +132,11 @@ func (u *ui) newTexture(img image.Image, filter int) (*Texture, error) {
var texture *Texture var texture *Texture
var err error var err error
u.use(func() { u.use(func() {
texture, err = idsInstance.createTexture(img, filter) glTexture, err := opengl.CreateTextureFromImage(img, filter)
if err != nil {
return
}
texture = &Texture{glTexture}
}) })
return texture, err return texture, err
} }