Add Texture.Width, Height and RenderTarget.Width, Height

This commit is contained in:
Hajime Hoshi 2014-12-17 22:50:44 +09:00
parent 7c56ac268e
commit f3e4d1703d
6 changed files with 33 additions and 39 deletions

View File

@ -19,7 +19,6 @@ package main
import ( import (
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil" "github.com/hajimehoshi/ebiten/ebitenutil"
"image"
_ "image/jpeg" _ "image/jpeg"
"log" "log"
"math" "math"
@ -33,31 +32,27 @@ const (
type Game struct { type Game struct {
count int count int
gophersTexture *ebiten.Texture gophersTexture *ebiten.Texture
gophersTextureWidth int
gophersTextureHeight int
} }
func (g *Game) Update(gr ebiten.GraphicsContext) error { func (g *Game) Update(gr ebiten.GraphicsContext) error {
g.count++ 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.ScaleGeometry(0.5, 0.5))
geo.Concat(ebiten.TranslateGeometry(screenWidth/2, screenHeight/2)) geo.Concat(ebiten.TranslateGeometry(screenWidth/2, screenHeight/2))
clr := ebiten.RotateHue(float64(g.count%180) * 2 * math.Pi / 180) 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 return nil
} }
func main() { func main() {
g := new(Game) g := new(Game)
var img image.Image
var err error 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 { if err != nil {
log.Fatal(err) 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 { if err := ebiten.Run(g.Update, screenWidth, screenHeight, 2, "Image (Ebiten Demo)"); err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -75,6 +75,11 @@ type Texture struct {
glTexture *opengl.Texture 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. // RenderTarget represents a render target.
// A render target is essentially same as a texture, but it is assumed that the // A render target is essentially same as a texture, but it is assumed that the
// all alpha values of a render target is maximum. // all alpha values of a render target is maximum.
@ -83,6 +88,11 @@ type RenderTarget struct {
texture *Texture 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 { func u(x int, width int) float32 {
return float32(x) / float32(opengl.AdjustSizeForTexture(width)) return float32(x) / float32(opengl.AdjustSizeForTexture(width))
} }

View File

@ -32,7 +32,7 @@ func newGraphicsContext(screenWidth, screenHeight, screenScale int) (*graphicsCo
c := &graphicsContext{ c := &graphicsContext{
currents: make([]*RenderTarget, 1), currents: make([]*RenderTarget, 1),
defaultR: idsInstance.addRenderTarget(r), defaultR: &RenderTarget{r, nil},
screen: screen, screen: screen,
screenWidth: screenWidth, screenWidth: screenWidth,
screenHeight: screenHeight, screenHeight: screenHeight,
@ -57,7 +57,12 @@ var _ GraphicsContext = new(graphicsContext)
func (c *graphicsContext) dispose() { func (c *graphicsContext) dispose() {
// NOTE: Now this method is not used anywhere. // 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 { func (c *graphicsContext) Clear() error {
@ -72,8 +77,8 @@ func (c *graphicsContext) Texture(texture *Texture) Drawer {
return &textureWithContext{texture, c} return &textureWithContext{texture, c}
} }
func (c *graphicsContext) RenderTarget(id *RenderTarget) Drawer { func (c *graphicsContext) RenderTarget(renderTarget *RenderTarget) Drawer {
return &textureWithContext{idsInstance.toTexture(id), c} return &textureWithContext{renderTarget.texture, c}
} }
func (c *graphicsContext) PushRenderTarget(renderTarget *RenderTarget) { func (c *graphicsContext) PushRenderTarget(renderTarget *RenderTarget) {

23
ids.go
View File

@ -23,18 +23,16 @@ import (
"math" "math"
) )
// ids manages the current render target to be used.
// TODO: Change this name. `ids` is not appropriate for now.
type ids struct { type ids struct {
currentRenderTarget *RenderTarget currentRenderTarget *RenderTarget
} }
var idsInstance = &ids{} var idsInstance = &ids{}
func (i *ids) toTexture(renderTarget *RenderTarget) *Texture {
return renderTarget.texture
}
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.NewTexture(width, height, filter)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -52,21 +50,6 @@ func (i *ids) createRenderTarget(width, height int, filter int) (*RenderTarget,
return renderTarget, nil 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 { func (i *ids) fillRenderTarget(renderTarget *RenderTarget, r, g, b uint8) error {
if err := i.setViewportIfNeeded(renderTarget); err != nil { if err := i.setViewportIfNeeded(renderTarget); err != nil {
return err return err

View File

@ -94,18 +94,19 @@ func createNativeTexture(textureWidth, textureHeight int, pixels []uint8, filter
return nativeTexture return nativeTexture
} }
func CreateTexture(width, height int, filter int) (*Texture, error) { func NewTexture(width, height int, filter int) (*Texture, error) {
w := AdjustSizeForTexture(width) w := AdjustSizeForTexture(width)
h := AdjustSizeForTexture(height) h := AdjustSizeForTexture(height)
native := createNativeTexture(w, h, nil, filter) native := createNativeTexture(w, h, nil, filter)
return &Texture{native, width, height}, nil 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) adjustedImage := adjustImageForTexture(img)
size := adjustedImage.Bounds().Size() size := adjustedImage.Bounds().Size()
native := createNativeTexture(size.X, size.Y, adjustedImage.Pix, filter) 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() { func (t *Texture) Dispose() {

2
ui.go
View File

@ -132,7 +132,7 @@ 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() {
glTexture, err := opengl.CreateTextureFromImage(img, filter) glTexture, err := opengl.NewTextureFromImage(img, filter)
if err != nil { if err != nil {
return return
} }