Move functions from graphics/texture to graphics

This commit is contained in:
Hajime Hoshi 2014-01-08 00:45:53 +09:00
parent 0cbf6a58aa
commit 1dece94cc8
7 changed files with 67 additions and 62 deletions

View File

@ -20,17 +20,6 @@ const (
FilterLinear FilterLinear
) )
type TextureQuad struct {
VertexX1 float32
VertexX2 float32
VertexY1 float32
VertexY2 float32
TextureCoordU1 float32
TextureCoordU2 float32
TextureCoordV1 float32
TextureCoordV2 float32
}
type TextureId int type TextureId int
// 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

View File

@ -54,8 +54,8 @@ func create(textureWidth, textureHeight int, filter graphics.Filter) (
} }
func Create(width, height int, filter graphics.Filter) (*gtexture.Texture, error) { func Create(width, height int, filter graphics.Filter) (*gtexture.Texture, error) {
native, err := create(gtexture.AdjustSize(width), native, err := create(graphics.AdjustSizeForTexture(width),
gtexture.AdjustSize(height), filter) graphics.AdjustSizeForTexture(height), filter)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -63,7 +63,7 @@ func Create(width, height int, filter graphics.Filter) (*gtexture.Texture, error
} }
func CreateFromImage(img image.Image, filter graphics.Filter) (*gtexture.Texture, error) { func CreateFromImage(img image.Image, filter graphics.Filter) (*gtexture.Texture, error) {
adjustedImage := gtexture.AdjustImage(img) adjustedImage := graphics.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 gtexture.New(native, size.X, size.Y), nil return gtexture.New(native, size.X, size.Y), nil

View File

@ -1,3 +0,0 @@
package texture
var NextPowerOf2 = nextPowerOf2

View File

@ -1,5 +1,9 @@
package texture package texture
import (
"github.com/hajimehoshi/go-ebiten/graphics"
)
type RenderTarget struct { type RenderTarget struct {
framebuffer interface{} framebuffer interface{}
offscreenWidth int offscreenWidth int
@ -9,8 +13,8 @@ type RenderTarget struct {
func NewRenderTarget(framebuffer interface{}, width, height int) *RenderTarget { func NewRenderTarget(framebuffer interface{}, width, height int) *RenderTarget {
return &RenderTarget{ return &RenderTarget{
framebuffer: framebuffer, framebuffer: framebuffer,
offscreenWidth: AdjustSize(width), offscreenWidth: graphics.AdjustSizeForTexture(width),
offscreenHeight: AdjustSize(height), offscreenHeight: graphics.AdjustSizeForTexture(height),
} }
} }

View File

@ -2,64 +2,24 @@ package texture
import ( import (
"github.com/hajimehoshi/go-ebiten/graphics" "github.com/hajimehoshi/go-ebiten/graphics"
"image"
"image/draw"
) )
func nextPowerOf2(x uint64) uint64 {
x -= 1
x |= (x >> 1)
x |= (x >> 2)
x |= (x >> 4)
x |= (x >> 8)
x |= (x >> 16)
x |= (x >> 32)
return x + 1
}
type Texture struct { type Texture struct {
native interface{} native interface{}
width int width int
height int height int
} }
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{
image.ZP,
image.Point{
AdjustSize(width),
AdjustSize(height),
},
}
if nrgba, ok := img.(*image.NRGBA); ok &&
img.Bounds() == adjustedImageBounds {
return nrgba
}
adjustedImage := image.NewNRGBA(adjustedImageBounds)
dstBounds := image.Rectangle{
image.ZP,
img.Bounds().Size(),
}
draw.Draw(adjustedImage, dstBounds, img, image.ZP, draw.Src)
return adjustedImage
}
func New(native interface{}, width, height int) *Texture { func New(native interface{}, width, height int) *Texture {
return &Texture{native, width, height} return &Texture{native, width, height}
} }
func (texture *Texture) u(x int) float32 { func (texture *Texture) u(x int) float32 {
return float32(x) / float32(AdjustSize(texture.width)) return float32(x) / float32(graphics.AdjustSizeForTexture(texture.width))
} }
func (texture *Texture) v(y int) float32 { func (texture *Texture) v(y int) float32 {
return float32(y) / float32(AdjustSize(texture.height)) return float32(y) / float32(graphics.AdjustSizeForTexture(texture.height))
} }
type Drawable interface { type Drawable interface {

55
graphics/texture_quad.go Normal file
View File

@ -0,0 +1,55 @@
package graphics
import (
"image"
"image/draw"
)
type TextureQuad struct {
VertexX1 float32
VertexX2 float32
VertexY1 float32
VertexY2 float32
TextureCoordU1 float32
TextureCoordU2 float32
TextureCoordV1 float32
TextureCoordV2 float32
}
func NextPowerOf2(x uint64) uint64 {
x -= 1
x |= (x >> 1)
x |= (x >> 2)
x |= (x >> 4)
x |= (x >> 8)
x |= (x >> 16)
x |= (x >> 32)
return x + 1
}
func AdjustSizeForTexture(size int) int {
return int(NextPowerOf2(uint64(size)))
}
func AdjustImageForTexture(img image.Image) *image.NRGBA {
width, height := img.Bounds().Size().X, img.Bounds().Size().Y
adjustedImageBounds := image.Rectangle{
image.ZP,
image.Point{
AdjustSizeForTexture(width),
AdjustSizeForTexture(height),
},
}
if nrgba, ok := img.(*image.NRGBA); ok &&
img.Bounds() == adjustedImageBounds {
return nrgba
}
adjustedImage := image.NewNRGBA(adjustedImageBounds)
dstBounds := image.Rectangle{
image.ZP,
img.Bounds().Size(),
}
draw.Draw(adjustedImage, dstBounds, img, image.ZP, draw.Src)
return adjustedImage
}

View File

@ -1,4 +1,4 @@
package texture_test package graphics_test
import ( import (
. "." . "."