diff --git a/example/game/rects/rects.go b/example/game/rects/rects.go index 3b574a3e7..27185e7cb 100644 --- a/example/game/rects/rects.go +++ b/example/game/rects/rects.go @@ -25,33 +25,41 @@ import ( "github.com/hajimehoshi/go.ebiten/graphics" "github.com/hajimehoshi/go.ebiten/graphics/matrix" "image/color" + "math" "math/rand" "time" ) type Rects struct { - rectsTexture graphics.Texture - rect *graphics.Rect - rectColor *color.RGBA + rectTexture graphics.Texture + rectTextureInited bool + rectsTexture graphics.Texture + rectsTextureInited bool + rectBounds *graphics.Rect + rectColor *color.RGBA } func New() *Rects { return &Rects{ - rect: &graphics.Rect{}, - rectColor: &color.RGBA{}, + rectTextureInited: false, + rectsTextureInited: false, + rectBounds: &graphics.Rect{}, + rectColor: &color.RGBA{}, } } func (game *Rects) Init(tf graphics.TextureFactory) { - // TODO: fix + game.rectTexture = tf.NewTexture(16, 16) game.rectsTexture = tf.NewTexture(256, 240) } func (game *Rects) Update(context ebiten.GameContext) { - game.rect.X = rand.Intn(context.ScreenWidth()) - game.rect.Y = rand.Intn(context.ScreenHeight()) - game.rect.Width = rand.Intn(context.ScreenWidth() - game.rect.X) - game.rect.Height = rand.Intn(context.ScreenHeight() - game.rect.Y) + game.rectBounds.X = rand.Intn(context.ScreenWidth()) + game.rectBounds.Y = rand.Intn(context.ScreenHeight()) + game.rectBounds.Width = + rand.Intn(context.ScreenWidth() - game.rectBounds.X) + game.rectBounds.Height = + rand.Intn(context.ScreenHeight() - game.rectBounds.Y) game.rectColor.R = uint8(rand.Intn(256)) game.rectColor.G = uint8(rand.Intn(256)) @@ -59,10 +67,46 @@ func (game *Rects) Update(context ebiten.GameContext) { game.rectColor.A = uint8(rand.Intn(256)) } -func (game *Rects) Draw(g graphics.Context) { - g.SetOffscreen(game.rectsTexture.ID()) +func (game *Rects) rectGeometryMatrix() matrix.Geometry { + geometryMatrix := matrix.IdentityGeometry() + scaleX := float64(game.rectBounds.Width) / + float64(game.rectTexture.Width()) + scaleY := float64(game.rectBounds.Height) / + float64(game.rectTexture.Height()) + geometryMatrix.Scale(scaleX, scaleY) + geometryMatrix.Translate( + float64(game.rectBounds.X), float64(game.rectBounds.Y)) + return geometryMatrix +} - g.DrawRect(*game.rect, game.rectColor) +func (game *Rects) rectColorMatrix() matrix.Color { + colorMatrix := matrix.IdentityColor() + colorMatrix.Elements[0][0] = + float64(game.rectColor.R) / float64(math.MaxUint8) + colorMatrix.Elements[1][1] = + float64(game.rectColor.G) / float64(math.MaxUint8) + colorMatrix.Elements[2][2] = + float64(game.rectColor.B) / float64(math.MaxUint8) + colorMatrix.Elements[3][3] = + float64(game.rectColor.A) / float64(math.MaxUint8) + return colorMatrix +} + +func (game *Rects) Draw(g graphics.Context) { + if !game.rectTextureInited { + g.SetOffscreen(game.rectTexture.ID()) + g.Fill(&color.White) + game.rectTextureInited = true + } + + g.SetOffscreen(game.rectsTexture.ID()) + if !game.rectsTextureInited { + g.Fill(&color.RGBA{0, 0, 0, 255}) + game.rectsTextureInited = true + } + g.DrawTexture(game.rectTexture.ID(), + game.rectGeometryMatrix(), + game.rectColorMatrix()) g.SetOffscreen(g.Screen().ID()) g.DrawTexture(game.rectsTexture.ID(), diff --git a/graphics/graphics.go b/graphics/graphics.go index 663331f33..9cf8c86bd 100644 --- a/graphics/graphics.go +++ b/graphics/graphics.go @@ -43,7 +43,6 @@ type Context interface { Screen() Texture Clear() Fill(clr color.Color) - DrawRect(rect Rect, clr color.Color) DrawTexture(textureID TextureID, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) diff --git a/graphics/matrix/geometry.go b/graphics/matrix/geometry.go index abce34711..3754fde6e 100644 --- a/graphics/matrix/geometry.go +++ b/graphics/matrix/geometry.go @@ -67,12 +67,12 @@ func (matrix *Geometry) Translate(tx, ty float64) { } func (matrix *Geometry) Scale(x, y float64) { - matrix.Elements[0][0] = x * matrix.Elements[0][0] - matrix.Elements[0][1] = x * matrix.Elements[0][1] - matrix.Elements[0][2] = x * matrix.Elements[0][2] - matrix.Elements[1][0] = y * matrix.Elements[1][0] - matrix.Elements[1][1] = y * matrix.Elements[1][1] - matrix.Elements[1][2] = y * matrix.Elements[1][2] + matrix.Elements[0][0] *= x + matrix.Elements[0][1] *= x + matrix.Elements[0][2] *= x + matrix.Elements[1][0] *= y + matrix.Elements[1][1] *= y + matrix.Elements[1][2] *= y } func (matrix *Geometry) Rotate(theta float64) { diff --git a/graphics/opengl/context.go b/graphics/opengl/context.go index a451ed387..6b2b5f159 100644 --- a/graphics/opengl/context.go +++ b/graphics/opengl/context.go @@ -91,53 +91,6 @@ func (context *Context) Fill(clr color.Color) { C.glClear(C.GL_COLOR_BUFFER_BIT) } -func (context *Context) DrawRect(rect graphics.Rect, clr color.Color) { - width := float32(context.currentOffscreen.Width()) - height := float32(context.currentOffscreen.Height()) - textureWidth := float32(clp2(uint64(width))) - textureHeight := float32(clp2(uint64(height))) - - // Normalize the coord between -1.0 and 1.0. - x1 := float32(rect.X)/textureWidth*2.0 - 1.0 - x2 := float32(rect.X+rect.Width)/textureHeight*2.0 - 1.0 - y1 := float32(rect.Y)/textureHeight*2.0 - 1.0 - y2 := float32(rect.Y+rect.Height)/textureHeight*2.0 - 1.0 - vertex := [...]float32{ - x1, y1, - x2, y1, - x1, y2, - x2, y2, - } - - origR, origG, origB, origA := clr.RGBA() - max := float32(math.MaxUint16) - r := float32(origR) / max - g := float32(origG) / max - b := float32(origB) / max - a := float32(origA) / max - color := [...]float32{ - r, g, b, a, - r, g, b, a, - r, g, b, a, - r, g, b, a, - } - - C.glUseProgram(0) - C.glDisable(C.GL_TEXTURE_2D) - C.glEnableClientState(C.GL_VERTEX_ARRAY) - C.glEnableClientState(C.GL_COLOR_ARRAY) - C.glVertexPointer(2, C.GL_FLOAT, C.GL_FALSE, unsafe.Pointer(&vertex[0])) - C.glColorPointer(4, C.GL_FLOAT, C.GL_FALSE, unsafe.Pointer(&color[0])) - C.glDrawArrays(C.GL_TRIANGLE_STRIP, 0, 4) - C.glDisableClientState(C.GL_COLOR_ARRAY) - C.glDisableClientState(C.GL_VERTEX_ARRAY) - C.glEnable(C.GL_TEXTURE_2D) - - if glError := C.glGetError(); glError != C.GL_NO_ERROR { - panic("OpenGL error") - } -} - func (context *Context) DrawTexture( textureID graphics.TextureID, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {