From f3e4d1703d420ff50f360e781cd86de9731e4474 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 17 Dec 2014 22:50:44 +0900 Subject: [PATCH] Add Texture.Width, Height and RenderTarget.Width, Height --- example/image/main.go | 17 ++++++----------- graphics.go | 10 ++++++++++ graphicscontext.go | 13 +++++++++---- ids.go | 23 +++-------------------- internal/opengl/texture.go | 7 ++++--- ui.go | 2 +- 6 files changed, 33 insertions(+), 39 deletions(-) diff --git a/example/image/main.go b/example/image/main.go index 89fafb5c3..d5cea3ea3 100644 --- a/example/image/main.go +++ b/example/image/main.go @@ -19,7 +19,6 @@ package main import ( "github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten/ebitenutil" - "image" _ "image/jpeg" "log" "math" @@ -31,33 +30,29 @@ const ( ) type Game struct { - count int - gophersTexture *ebiten.Texture - gophersTextureWidth int - gophersTextureHeight int + count int + gophersTexture *ebiten.Texture } func (g *Game) Update(gr ebiten.GraphicsContext) error { g.count++ - geo := ebiten.TranslateGeometry(-float64(g.gophersTextureWidth)/2, -float64(g.gophersTextureHeight)/2) + w, h := g.gophersTexture.Size() + geo := ebiten.TranslateGeometry(-float64(w)/2, -float64(h)/2) geo.Concat(ebiten.ScaleGeometry(0.5, 0.5)) geo.Concat(ebiten.TranslateGeometry(screenWidth/2, screenHeight/2)) clr := ebiten.RotateHue(float64(g.count%180) * 2 * math.Pi / 180) - ebiten.DrawWhole(gr.Texture(g.gophersTexture), g.gophersTextureWidth, g.gophersTextureHeight, geo, clr) + ebiten.DrawWhole(gr.Texture(g.gophersTexture), w, h, geo, clr) return nil } func main() { g := new(Game) - var img image.Image var err error - g.gophersTexture, img, err = ebitenutil.NewTextureFromFile("images/gophers.jpg", ebiten.FilterLinear) + g.gophersTexture, _, err = ebitenutil.NewTextureFromFile("images/gophers.jpg", ebiten.FilterLinear) if err != nil { log.Fatal(err) } - g.gophersTextureWidth = img.Bounds().Size().X - g.gophersTextureHeight = img.Bounds().Size().Y if err := ebiten.Run(g.Update, screenWidth, screenHeight, 2, "Image (Ebiten Demo)"); err != nil { log.Fatal(err) } diff --git a/graphics.go b/graphics.go index b7d517a7e..57f7034a7 100644 --- a/graphics.go +++ b/graphics.go @@ -75,6 +75,11 @@ type Texture struct { glTexture *opengl.Texture } +// Size returns the size of the texture. +func (t *Texture) Size() (width int, height int) { + return t.glTexture.Width(), t.glTexture.Height() +} + // RenderTarget represents a render target. // A render target is essentially same as a texture, but it is assumed that the // all alpha values of a render target is maximum. @@ -83,6 +88,11 @@ type RenderTarget struct { texture *Texture } +// Size returns the size of the render target. +func (r *RenderTarget) Size() (width int, height int) { + return r.glRenderTarget.Width(), r.glRenderTarget.Height() +} + func u(x int, width int) float32 { return float32(x) / float32(opengl.AdjustSizeForTexture(width)) } diff --git a/graphicscontext.go b/graphicscontext.go index 0d11a820f..44cc1d6c4 100644 --- a/graphicscontext.go +++ b/graphicscontext.go @@ -32,7 +32,7 @@ func newGraphicsContext(screenWidth, screenHeight, screenScale int) (*graphicsCo c := &graphicsContext{ currents: make([]*RenderTarget, 1), - defaultR: idsInstance.addRenderTarget(r), + defaultR: &RenderTarget{r, nil}, screen: screen, screenWidth: screenWidth, screenHeight: screenHeight, @@ -57,7 +57,12 @@ var _ GraphicsContext = new(graphicsContext) func (c *graphicsContext) dispose() { // NOTE: Now this method is not used anywhere. - idsInstance.deleteRenderTarget(c.screen) + glRenderTarget := c.screen.glRenderTarget + texture := c.screen.texture + glTexture := texture.glTexture + + glRenderTarget.Dispose() + glTexture.Dispose() } func (c *graphicsContext) Clear() error { @@ -72,8 +77,8 @@ func (c *graphicsContext) Texture(texture *Texture) Drawer { return &textureWithContext{texture, c} } -func (c *graphicsContext) RenderTarget(id *RenderTarget) Drawer { - return &textureWithContext{idsInstance.toTexture(id), c} +func (c *graphicsContext) RenderTarget(renderTarget *RenderTarget) Drawer { + return &textureWithContext{renderTarget.texture, c} } func (c *graphicsContext) PushRenderTarget(renderTarget *RenderTarget) { diff --git a/ids.go b/ids.go index 9b52559c8..df9ca0056 100644 --- a/ids.go +++ b/ids.go @@ -23,18 +23,16 @@ import ( "math" ) +// ids manages the current render target to be used. +// TODO: Change this name. `ids` is not appropriate for now. type ids struct { currentRenderTarget *RenderTarget } var idsInstance = &ids{} -func (i *ids) toTexture(renderTarget *RenderTarget) *Texture { - return renderTarget.texture -} - func (i *ids) createRenderTarget(width, height int, filter int) (*RenderTarget, error) { - glTexture, err := opengl.CreateTexture(width, height, filter) + glTexture, err := opengl.NewTexture(width, height, filter) if err != nil { return nil, err } @@ -52,21 +50,6 @@ func (i *ids) createRenderTarget(width, height int, filter int) (*RenderTarget, return renderTarget, nil } -// NOTE: renderTarget can't be used as a texture. -func (i *ids) addRenderTarget(glRenderTarget *opengl.RenderTarget) *RenderTarget { - return &RenderTarget{glRenderTarget, nil} -} - -func (i *ids) deleteRenderTarget(renderTarget *RenderTarget) { - - glRenderTarget := renderTarget.glRenderTarget - texture := renderTarget.texture - glTexture := texture.glTexture - - glRenderTarget.Dispose() - glTexture.Dispose() -} - func (i *ids) fillRenderTarget(renderTarget *RenderTarget, r, g, b uint8) error { if err := i.setViewportIfNeeded(renderTarget); err != nil { return err diff --git a/internal/opengl/texture.go b/internal/opengl/texture.go index b7ae7ebe0..5d94f2524 100644 --- a/internal/opengl/texture.go +++ b/internal/opengl/texture.go @@ -94,18 +94,19 @@ func createNativeTexture(textureWidth, textureHeight int, pixels []uint8, filter return nativeTexture } -func CreateTexture(width, height int, filter int) (*Texture, error) { +func NewTexture(width, height int, filter int) (*Texture, error) { w := AdjustSizeForTexture(width) h := AdjustSizeForTexture(height) native := createNativeTexture(w, h, nil, filter) return &Texture{native, width, height}, nil } -func CreateTextureFromImage(img image.Image, filter int) (*Texture, error) { +func NewTextureFromImage(img image.Image, filter int) (*Texture, error) { + origSize := img.Bounds().Size() adjustedImage := adjustImageForTexture(img) size := adjustedImage.Bounds().Size() native := createNativeTexture(size.X, size.Y, adjustedImage.Pix, filter) - return &Texture{native, size.X, size.Y}, nil + return &Texture{native, origSize.X, origSize.Y}, nil } func (t *Texture) Dispose() { diff --git a/ui.go b/ui.go index 1d65265cb..27b84f4c9 100644 --- a/ui.go +++ b/ui.go @@ -132,7 +132,7 @@ func (u *ui) newTexture(img image.Image, filter int) (*Texture, error) { var texture *Texture var err error u.use(func() { - glTexture, err := opengl.CreateTextureFromImage(img, filter) + glTexture, err := opengl.NewTextureFromImage(img, filter) if err != nil { return }