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 (
"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)
}

View File

@ -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))
}

View File

@ -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) {

23
ids.go
View File

@ -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

View File

@ -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() {

2
ui.go
View File

@ -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
}