From 1dece94cc8a33933c5ee796ae7639585fa0af92a Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 8 Jan 2014 00:45:53 +0900 Subject: [PATCH] Move functions from graphics/texture to graphics --- graphics/graphics.go | 11 ---- graphics/opengl/texture/texture.go | 6 +- graphics/texture/export_test.go | 3 - graphics/texture/render_target.go | 8 ++- graphics/texture/texture.go | 44 +-------------- graphics/texture_quad.go | 55 +++++++++++++++++++ .../texture_test.go => texture_quad_test.go} | 2 +- 7 files changed, 67 insertions(+), 62 deletions(-) delete mode 100644 graphics/texture/export_test.go create mode 100644 graphics/texture_quad.go rename graphics/{texture/texture_test.go => texture_quad_test.go} (94%) diff --git a/graphics/graphics.go b/graphics/graphics.go index 554d3fd9e..f211fa3fd 100644 --- a/graphics/graphics.go +++ b/graphics/graphics.go @@ -20,17 +20,6 @@ const ( FilterLinear ) -type TextureQuad struct { - VertexX1 float32 - VertexX2 float32 - VertexY1 float32 - VertexY2 float32 - TextureCoordU1 float32 - TextureCoordU2 float32 - TextureCoordV1 float32 - TextureCoordV2 float32 -} - type TextureId int // A render target is essentially same as a texture, but it is assumed that the diff --git a/graphics/opengl/texture/texture.go b/graphics/opengl/texture/texture.go index 8947bea97..f76c9a6fd 100644 --- a/graphics/opengl/texture/texture.go +++ b/graphics/opengl/texture/texture.go @@ -54,8 +54,8 @@ func create(textureWidth, textureHeight int, filter graphics.Filter) ( } func Create(width, height int, filter graphics.Filter) (*gtexture.Texture, error) { - native, err := create(gtexture.AdjustSize(width), - gtexture.AdjustSize(height), filter) + native, err := create(graphics.AdjustSizeForTexture(width), + graphics.AdjustSizeForTexture(height), filter) if err != nil { 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) { - adjustedImage := gtexture.AdjustImage(img) + adjustedImage := graphics.AdjustImageForTexture(img) size := adjustedImage.Bounds().Size() native := createNativeTexture(size.X, size.Y, adjustedImage.Pix, filter) return gtexture.New(native, size.X, size.Y), nil diff --git a/graphics/texture/export_test.go b/graphics/texture/export_test.go deleted file mode 100644 index e8da4e140..000000000 --- a/graphics/texture/export_test.go +++ /dev/null @@ -1,3 +0,0 @@ -package texture - -var NextPowerOf2 = nextPowerOf2 diff --git a/graphics/texture/render_target.go b/graphics/texture/render_target.go index 131767716..22408a5fe 100644 --- a/graphics/texture/render_target.go +++ b/graphics/texture/render_target.go @@ -1,5 +1,9 @@ package texture +import ( + "github.com/hajimehoshi/go-ebiten/graphics" +) + type RenderTarget struct { framebuffer interface{} offscreenWidth int @@ -9,8 +13,8 @@ type RenderTarget struct { func NewRenderTarget(framebuffer interface{}, width, height int) *RenderTarget { return &RenderTarget{ framebuffer: framebuffer, - offscreenWidth: AdjustSize(width), - offscreenHeight: AdjustSize(height), + offscreenWidth: graphics.AdjustSizeForTexture(width), + offscreenHeight: graphics.AdjustSizeForTexture(height), } } diff --git a/graphics/texture/texture.go b/graphics/texture/texture.go index 77ca4f1a0..b8b947318 100644 --- a/graphics/texture/texture.go +++ b/graphics/texture/texture.go @@ -2,64 +2,24 @@ package texture import ( "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 { native interface{} width 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 { return &Texture{native, width, height} } 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 { - return float32(y) / float32(AdjustSize(texture.height)) + return float32(y) / float32(graphics.AdjustSizeForTexture(texture.height)) } type Drawable interface { diff --git a/graphics/texture_quad.go b/graphics/texture_quad.go new file mode 100644 index 000000000..eb2420bf7 --- /dev/null +++ b/graphics/texture_quad.go @@ -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 +} diff --git a/graphics/texture/texture_test.go b/graphics/texture_quad_test.go similarity index 94% rename from graphics/texture/texture_test.go rename to graphics/texture_quad_test.go index 8321997c1..968b6f379 100644 --- a/graphics/texture/texture_test.go +++ b/graphics/texture_quad_test.go @@ -1,4 +1,4 @@ -package texture_test +package graphics_test import ( . "."