Remove Texture/RenderTarget

This commit is contained in:
Hajime Hoshi 2013-10-17 00:14:32 +09:00
parent fd0ba69f0b
commit db4e3fdc03
9 changed files with 41 additions and 44 deletions

View File

@ -10,7 +10,7 @@ import (
)
type Input struct {
textTexture graphics.Texture
textTextureID graphics.TextureID
inputState ebiten.InputState
}
@ -29,7 +29,7 @@ func (game *Input) Init(tf graphics.TextureFactory) {
if err != nil {
panic(err)
}
if game.textTexture, err = tf.NewTextureFromImage(img); err != nil {
if game.textTextureID, err = tf.NewTextureFromImage(img); err != nil {
panic(err)
}
}
@ -74,6 +74,6 @@ func (game *Input) drawText(g graphics.Context, text string, x, y int) {
geometryMatrix := matrix.IdentityGeometry()
geometryMatrix.Translate(float64(x), float64(y))
colorMatrix := matrix.IdentityColor()
g.DrawTextureParts(game.textTexture.ID(), parts,
g.DrawTextureParts(game.textTextureID, parts,
geometryMatrix, colorMatrix)
}

View File

@ -15,7 +15,7 @@ const (
)
type Monochrome struct {
ebitenTexture graphics.Texture
ebitenTextureID graphics.TextureID
ch chan bool
colorMatrix matrix.Color
geometryMatrix matrix.Geometry
@ -40,7 +40,7 @@ func (game *Monochrome) Init(tf graphics.TextureFactory) {
if err != nil {
panic(err)
}
if game.ebitenTexture, err = tf.NewTextureFromImage(img); err != nil {
if game.ebitenTextureID, err = tf.NewTextureFromImage(img); err != nil {
panic(err)
}
@ -102,6 +102,6 @@ func (game *Monochrome) Update(context ebiten.GameContext) {
func (game *Monochrome) Draw(g graphics.Context) {
g.Fill(128, 128, 255)
g.DrawTexture(game.ebitenTexture.ID(),
g.DrawTexture(game.ebitenTextureID,
game.geometryMatrix, game.colorMatrix)
}

View File

@ -11,9 +11,9 @@ import (
)
type Rects struct {
rectTexture graphics.RenderTarget
rectTextureID graphics.RenderTargetID
rectTextureInited bool
offscreen graphics.RenderTarget
offscreenID graphics.RenderTargetID
offscreenInited bool
rectBounds *graphics.Rect
rectColor *color.RGBA
@ -36,8 +36,8 @@ func New() *Rects {
}
func (game *Rects) Init(tf graphics.TextureFactory) {
game.rectTexture = tf.NewRenderTarget(rectTextureWidth, rectTextureHeight)
game.offscreen = tf.NewRenderTarget(offscreenWidth, offscreenHeight)
game.rectTextureID = tf.NewRenderTarget(rectTextureWidth, rectTextureHeight)
game.offscreenID = tf.NewRenderTarget(offscreenWidth, offscreenHeight)
}
func (game *Rects) Update(context ebiten.GameContext) {
@ -81,22 +81,22 @@ func (game *Rects) rectColorMatrix() matrix.Color {
func (game *Rects) Draw(g graphics.Context) {
if !game.rectTextureInited {
g.SetOffscreen(game.rectTexture.ID())
g.SetOffscreen(game.rectTextureID)
g.Fill(255, 255, 255)
game.rectTextureInited = true
}
g.SetOffscreen(game.offscreen.ID())
g.SetOffscreen(game.offscreenID)
if !game.offscreenInited {
g.Fill(0, 0, 0)
game.offscreenInited = true
}
g.DrawTexture(game.rectTexture.Texture().ID(),
g.DrawTexture(g.TextureID(game.rectTextureID),
game.rectGeometryMatrix(),
game.rectColorMatrix())
g.ResetOffscreen()
g.DrawTexture(game.offscreen.Texture().ID(),
g.DrawTexture(g.TextureID(game.offscreenID),
matrix.IdentityGeometry(),
matrix.IdentityColor())
}

View File

@ -16,7 +16,7 @@ const (
)
type Rotating struct {
ebitenTexture graphics.Texture
ebitenTextureID graphics.TextureID
x int
geometryMatrix matrix.Geometry
}
@ -36,7 +36,7 @@ func (game *Rotating) Init(tf graphics.TextureFactory) {
if err != nil {
panic(err)
}
if game.ebitenTexture, err = tf.NewTextureFromImage(img); err != nil {
if game.ebitenTextureID, err = tf.NewTextureFromImage(img); err != nil {
panic(err)
}
}
@ -56,7 +56,7 @@ func (game *Rotating) Update(context ebiten.GameContext) {
func (game *Rotating) Draw(g graphics.Context) {
g.Fill(128, 128, 255)
g.DrawTexture(game.ebitenTexture.ID(),
g.DrawTexture(game.ebitenTextureID,
game.geometryMatrix,
matrix.IdentityColor())
}

View File

@ -64,7 +64,7 @@ func (sprite *Sprite) Update() {
}
type Sprites struct {
ebitenTexture graphics.Texture
ebitenTextureID graphics.TextureID
sprites []*Sprite
}
@ -83,7 +83,7 @@ func (game *Sprites) Init(tf graphics.TextureFactory) {
if err != nil {
panic(err)
}
if game.ebitenTexture, err = tf.NewTextureFromImage(img); err != nil {
if game.ebitenTextureID, err = tf.NewTextureFromImage(img); err != nil {
panic(err)
}
}
@ -111,7 +111,6 @@ func (game *Sprites) Draw(g graphics.Context) {
// Draw the sprites
locations := make([]graphics.TexturePart, 0, len(game.sprites))
texture := game.ebitenTexture
for _, sprite := range game.sprites {
location := graphics.TexturePart{
LocationX: sprite.x,
@ -123,7 +122,7 @@ func (game *Sprites) Draw(g graphics.Context) {
locations = append(locations, location)
}
geometryMatrix := matrix.IdentityGeometry()
g.DrawTextureParts(texture.ID(), locations,
g.DrawTextureParts(game.ebitenTextureID, locations,
geometryMatrix, matrix.IdentityColor())
}

View File

@ -19,6 +19,8 @@ type TexturePart struct {
}
type Context interface {
TextureID(renderTargetID RenderTargetID) TextureID
Clear()
Fill(r, g, b uint8)
DrawTexture(textureID TextureID,
@ -33,21 +35,12 @@ type Context interface {
}
type TextureFactory interface {
NewRenderTarget(width, height int) RenderTarget
NewTextureFromImage(img image.Image) (Texture, error)
}
type Texture interface {
ID() TextureID
NewRenderTarget(width, height int) RenderTargetID
NewTextureFromImage(img image.Image) (TextureID, error)
}
type TextureID int
// The interface of a render target. This is essentially same as a texture, but
// it is assumed that the all alpha of a render target is maximum.
type RenderTarget interface {
Texture() Texture
ID() RenderTargetID
}
// A render target is essentially same as a texture, but it is assumed that the
// all alpha of a render target is maximum.
type RenderTargetID int

View File

@ -47,8 +47,13 @@ func (context *Context) Init() {
initializeShaders()
context.screen = context.NewRenderTarget(
context.screenWidth, context.screenHeight).(*RenderTarget)
screenID := context.NewRenderTarget(
context.screenWidth, context.screenHeight)
context.screen = (*RenderTarget)(context.textures[C.GLuint(screenID)])
}
func (context *Context) TextureID(renderTargetID graphics.RenderTargetID) graphics.TextureID {
return graphics.TextureID(renderTargetID)
}
func (context *Context) Clear() {
@ -284,7 +289,7 @@ func createFramebuffer(textureID C.GLuint) C.GLuint {
return framebuffer
}
func (context *Context) NewRenderTarget(width, height int) graphics.RenderTarget {
func (context *Context) NewRenderTarget(width, height int) graphics.RenderTargetID {
renderTarget := newRenderTarget(width, height)
context.textures[renderTarget.id] = (*Texture)(renderTarget)
@ -292,15 +297,15 @@ func (context *Context) NewRenderTarget(width, height int) graphics.RenderTarget
context.Clear()
context.resetOffscreen()
return renderTarget
return renderTarget.ID()
}
func (context *Context) NewTextureFromImage(img image.Image) (
graphics.Texture, error) {
graphics.TextureID, error) {
texture, err := newTextureFromImage(img)
if err != nil {
return nil, err
return 0, err
}
context.textures[texture.id] = texture
return texture, nil
return texture.ID(), nil
}

View File

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

View File

@ -137,8 +137,8 @@ func newRenderTargetWithFramebuffer(width, height int,
type RenderTarget Texture
func (renderTarget *RenderTarget) Texture() graphics.Texture {
return (*Texture)(renderTarget)
func (renderTarget *RenderTarget) TextureID() graphics.TextureID {
return graphics.TextureID(renderTarget.id)
}
func (renderTarget *RenderTarget) ID() graphics.RenderTargetID {