2013-10-25 02:30:39 +02:00
|
|
|
package texture
|
|
|
|
|
|
|
|
import (
|
2013-10-25 21:15:27 +02:00
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics"
|
2013-10-25 02:30:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Texture struct {
|
|
|
|
native interface{}
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
}
|
|
|
|
|
2013-11-26 13:07:37 +01:00
|
|
|
func New(native interface{}, width, height int) *Texture {
|
|
|
|
return &Texture{native, width, height}
|
2013-10-25 02:30:39 +02:00
|
|
|
}
|
|
|
|
|
2013-10-25 21:15:27 +02:00
|
|
|
func (texture *Texture) u(x int) float32 {
|
2014-01-07 16:45:53 +01:00
|
|
|
return float32(x) / float32(graphics.AdjustSizeForTexture(texture.width))
|
2013-10-25 21:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (texture *Texture) v(y int) float32 {
|
2014-01-07 16:45:53 +01:00
|
|
|
return float32(y) / float32(graphics.AdjustSizeForTexture(texture.height))
|
2013-10-25 02:30:39 +02:00
|
|
|
}
|
|
|
|
|
2013-11-28 01:36:31 +01:00
|
|
|
type Drawable interface {
|
2014-01-07 15:33:53 +01:00
|
|
|
Draw(native interface{}, quads []graphics.TextureQuad)
|
2013-11-28 01:36:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (texture *Texture) Draw(drawable Drawable) {
|
2013-10-25 21:15:27 +02:00
|
|
|
x1 := float32(0)
|
|
|
|
x2 := float32(texture.width)
|
|
|
|
y1 := float32(0)
|
|
|
|
y2 := float32(texture.height)
|
|
|
|
u1 := texture.u(0)
|
|
|
|
u2 := texture.u(texture.width)
|
|
|
|
v1 := texture.v(0)
|
|
|
|
v2 := texture.v(texture.height)
|
2014-01-07 15:33:53 +01:00
|
|
|
quad := graphics.TextureQuad{x1, x2, y1, y2, u1, u2, v1, v2}
|
|
|
|
drawable.Draw(texture.native, []graphics.TextureQuad{quad})
|
2013-10-25 21:15:27 +02:00
|
|
|
}
|
|
|
|
|
2013-11-28 01:36:31 +01:00
|
|
|
func (texture *Texture) DrawParts(parts []graphics.TexturePart, drawable Drawable) {
|
2014-01-07 15:33:53 +01:00
|
|
|
quads := []graphics.TextureQuad{}
|
2013-10-25 21:15:27 +02:00
|
|
|
for _, part := range parts {
|
|
|
|
x1 := float32(part.LocationX)
|
|
|
|
x2 := float32(part.LocationX + part.Source.Width)
|
|
|
|
y1 := float32(part.LocationY)
|
|
|
|
y2 := float32(part.LocationY + part.Source.Height)
|
|
|
|
u1 := texture.u(part.Source.X)
|
|
|
|
u2 := texture.u(part.Source.X + part.Source.Width)
|
|
|
|
v1 := texture.v(part.Source.Y)
|
|
|
|
v2 := texture.v(part.Source.Y + part.Source.Height)
|
2014-01-07 15:33:53 +01:00
|
|
|
quad := graphics.TextureQuad{x1, x2, y1, y2, u1, u2, v1, v2}
|
2013-10-25 21:15:27 +02:00
|
|
|
quads = append(quads, quad)
|
|
|
|
}
|
2013-11-28 01:36:31 +01:00
|
|
|
drawable.Draw(texture.native, quads)
|
2013-10-25 02:30:39 +02:00
|
|
|
}
|
2013-11-28 18:38:18 +01:00
|
|
|
|
|
|
|
type FramebufferCreator interface {
|
|
|
|
Create(native interface{}) interface{}
|
|
|
|
}
|
|
|
|
|
2013-11-28 19:21:53 +01:00
|
|
|
func (texture *Texture) CreateRenderTarget(creator FramebufferCreator) *RenderTarget {
|
2013-11-28 18:38:18 +01:00
|
|
|
return NewRenderTarget(creator.Create(texture.native), texture.width, texture.height)
|
|
|
|
}
|