Refactoring

This commit is contained in:
Hajime Hoshi 2013-11-26 21:07:37 +09:00
parent e7c7c69dcc
commit a109c11594
2 changed files with 26 additions and 51 deletions

View File

@ -66,19 +66,22 @@ func createFromImage(img *image.NRGBA) (interface{}, error) {
} }
func New(width, height int, filter Filter) (*gtexture.Texture, error) { func New(width, height int, filter Filter) (*gtexture.Texture, error) {
f := func(textureWidth, textureHeight int) (interface{}, error) { native, err := create(gtexture.AdjustSize(width), gtexture.AdjustSize(height), filter)
return create(textureWidth, textureHeight, filter) if err != nil {
return nil, err
} }
return gtexture.New(width, height, f) return gtexture.New(native, width, height), nil
} }
func NewEmpty(width, height int) (*gtexture.Texture, error) { func NewEmpty(width, height int) (*gtexture.Texture, error) {
f := func(textureWidth, textureHeight int) (interface{}, error) { return gtexture.New(nil, width, height), nil
return nil, nil
}
return gtexture.New(width, height, f)
} }
func NewFromImage(img image.Image) (*gtexture.Texture, error) { func NewFromImage(img image.Image) (*gtexture.Texture, error) {
return gtexture.NewFromImage(img, createFromImage) native, err := createFromImage(gtexture.AdjustImage(img))
if err != nil {
return nil, err
}
size := img.Bounds().Size()
return gtexture.New(native, size.X, size.Y), nil
} }

View File

@ -23,10 +23,18 @@ type Texture struct {
height int height int
} }
func AdjustImage(img image.Image, width, height int) *image.NRGBA { func AdjustSize(size int) int {
return int(nextPowerOf2(uint64(size)))
}
func AdjustImage(img image.Image) *image.NRGBA {
width, height := img.Bounds().Size().X, img.Bounds().Size().Y
adjustedImageBounds := image.Rectangle{ adjustedImageBounds := image.Rectangle{
image.ZP, image.ZP,
image.Point{width, height}, image.Point{
AdjustSize(width),
AdjustSize(height),
},
} }
if nrgba := img.(*image.NRGBA); nrgba != nil && if nrgba := img.(*image.NRGBA); nrgba != nil &&
img.Bounds() == adjustedImageBounds { img.Bounds() == adjustedImageBounds {
@ -42,56 +50,20 @@ func AdjustImage(img image.Image, width, height int) *image.NRGBA {
return adjustedImage return adjustedImage
} }
func New(width, height int, create func(textureWidth, textureHeight int) ( func New(native interface{}, width, height int) *Texture {
interface{}, error)) (*Texture, error) { return &Texture{native, width, height}
texture := &Texture{
width: width,
height: height,
}
var err error
texture.native, err = create(texture.textureWidth(),
texture.textureHeight())
if err != nil {
return nil, err
}
return texture, nil
}
func NewFromImage(img image.Image, create func(img *image.NRGBA) (
interface{}, error)) (*Texture, error) {
size := img.Bounds().Size()
width, height := size.X, size.Y
texture := &Texture{
width: width,
height: height,
}
adjustedImage := AdjustImage(img, texture.textureWidth(), texture.textureHeight())
var err error
texture.native, err = create(adjustedImage)
if err != nil {
return nil, err
}
return texture, nil
}
func (texture *Texture) textureWidth() int {
return int(nextPowerOf2(uint64(texture.width)))
}
func (texture *Texture) textureHeight() int {
return int(nextPowerOf2(uint64(texture.height)))
} }
func (texture *Texture) u(x int) float32 { func (texture *Texture) u(x int) float32 {
return float32(x) / float32(texture.textureWidth()) return float32(x) / float32(AdjustSize(texture.width))
} }
func (texture *Texture) v(y int) float32 { func (texture *Texture) v(y int) float32 {
return float32(y) / float32(texture.textureHeight()) return float32(y) / float32(AdjustSize(texture.height))
} }
func (texture *Texture) SetAsViewport(setter func(x, y, width, height int)) { func (texture *Texture) SetAsViewport(setter func(x, y, width, height int)) {
setter(0, 0, texture.textureWidth(), texture.textureHeight()) setter(0, 0, AdjustSize(texture.width), AdjustSize(texture.height))
} }
type Quad struct { type Quad struct {