From a374565e3713c21b39ebc03bb5bd184d98944e8a Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 22 Jun 2013 00:03:45 +0900 Subject: [PATCH] Add graphics.Rectangle --- example/game/rotating_image.go | 2 +- example/game/sprites.go | 2 +- graphics/graphics.go | 9 ++++++++- graphics/opengl/device.go | 2 +- graphics/opengl/graphics_context.go | 15 +++++++-------- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/example/game/rotating_image.go b/example/game/rotating_image.go index 83b834954..2a2fd78ab 100644 --- a/example/game/rotating_image.go +++ b/example/game/rotating_image.go @@ -57,7 +57,7 @@ func (game *RotatingImage) Draw(g graphics.GraphicsContext, offscreen graphics.T geometryMatrix.Translate(centerX-tx/2, centerY-ty/2) g.DrawTexture(game.ebitenTexture.ID, - 0, 0, int(tx), int(ty), + graphics.Rectangle{0, 0, int(tx), int(ty)}, geometryMatrix, matrix.IdentityColor()) } diff --git a/example/game/sprites.go b/example/game/sprites.go index b0959edc3..f7994c218 100644 --- a/example/game/sprites.go +++ b/example/game/sprites.go @@ -62,7 +62,7 @@ func (sprite *Sprite) Draw(g graphics.GraphicsContext) { geometryMatrix.Translate(float64(sprite.x), float64(sprite.y)) g.DrawTexture(sprite.texture.ID, - 0, 0, sprite.texture.Width, sprite.texture.Height, + graphics.Rectangle{0, 0, sprite.texture.Width, sprite.texture.Height}, geometryMatrix, matrix.IdentityColor()) } diff --git a/graphics/graphics.go b/graphics/graphics.go index 3e954cd59..8412bf832 100644 --- a/graphics/graphics.go +++ b/graphics/graphics.go @@ -11,11 +11,18 @@ type Device interface { TextureFactory() TextureFactory } +type Rectangle struct { + X int + Y int + Width int + Height int +} + type GraphicsContext interface { Clear() Fill(color color.Color) DrawTexture(textureId TextureID, - srcX, srcY, srcWidth, srcHeight int, + src Rectangle, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) SetOffscreen(textureId TextureID) } diff --git a/graphics/opengl/device.go b/graphics/opengl/device.go index e8488abda..2cf95384a 100644 --- a/graphics/opengl/device.go +++ b/graphics/opengl/device.go @@ -56,7 +56,7 @@ func (device *Device) Update() { }, } g.DrawTexture(device.offscreenTexture.ID, - 0, 0, device.screenWidth, device.screenHeight, + graphics.Rectangle{0, 0, device.screenWidth, device.screenHeight}, geometryMatrix, matrix.IdentityColor()) g.flush() } diff --git a/graphics/opengl/graphics_context.go b/graphics/opengl/graphics_context.go index c4f36e8fe..2e096417a 100644 --- a/graphics/opengl/graphics_context.go +++ b/graphics/opengl/graphics_context.go @@ -66,8 +66,7 @@ func (context *GraphicsContext) DrawRect(x, y, width, height int, clr color.Colo } func (context *GraphicsContext) DrawTexture( - textureID graphics.TextureID, - srcX, srcY, srcWidth, srcHeight int, + textureID graphics.TextureID, src graphics.Rectangle, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { texture := context.textures[textureID] @@ -76,9 +75,9 @@ func (context *GraphicsContext) DrawTexture( C.glBindTexture(C.GL_TEXTURE_2D, texture.id) x1 := float32(0) - x2 := float32(srcWidth) + x2 := float32(src.Width) y1 := float32(0) - y2 := float32(srcHeight) + y2 := float32(src.Height) vertex := [...]float32{ x1, y1, x2, y1, @@ -86,10 +85,10 @@ func (context *GraphicsContext) DrawTexture( x2, y2, } - tu1 := float32(srcX) / float32(texture.textureWidth) - tu2 := float32(srcX+srcWidth) / float32(texture.textureWidth) - tv1 := float32(srcY) / float32(texture.textureHeight) - tv2 := float32(srcY+srcHeight) / float32(texture.textureHeight) + tu1 := float32(src.X) / float32(texture.textureWidth) + tu2 := float32(src.X+src.Width) / float32(texture.textureWidth) + tv1 := float32(src.Y) / float32(texture.textureHeight) + tv2 := float32(src.Y+src.Height) / float32(texture.textureHeight) texCoord := [...]float32{ tu1, tv1, tu2, tv1,