Remove graphics.Context.DrawRect

This commit is contained in:
Hajime Hoshi 2013-10-11 22:17:38 +09:00
parent 0ff5524911
commit 59921c5a34
4 changed files with 63 additions and 67 deletions

View File

@ -25,33 +25,41 @@ import (
"github.com/hajimehoshi/go.ebiten/graphics" "github.com/hajimehoshi/go.ebiten/graphics"
"github.com/hajimehoshi/go.ebiten/graphics/matrix" "github.com/hajimehoshi/go.ebiten/graphics/matrix"
"image/color" "image/color"
"math"
"math/rand" "math/rand"
"time" "time"
) )
type Rects struct { type Rects struct {
rectsTexture graphics.Texture rectTexture graphics.Texture
rect *graphics.Rect rectTextureInited bool
rectColor *color.RGBA rectsTexture graphics.Texture
rectsTextureInited bool
rectBounds *graphics.Rect
rectColor *color.RGBA
} }
func New() *Rects { func New() *Rects {
return &Rects{ return &Rects{
rect: &graphics.Rect{}, rectTextureInited: false,
rectColor: &color.RGBA{}, rectsTextureInited: false,
rectBounds: &graphics.Rect{},
rectColor: &color.RGBA{},
} }
} }
func (game *Rects) Init(tf graphics.TextureFactory) { func (game *Rects) Init(tf graphics.TextureFactory) {
// TODO: fix game.rectTexture = tf.NewTexture(16, 16)
game.rectsTexture = tf.NewTexture(256, 240) game.rectsTexture = tf.NewTexture(256, 240)
} }
func (game *Rects) Update(context ebiten.GameContext) { func (game *Rects) Update(context ebiten.GameContext) {
game.rect.X = rand.Intn(context.ScreenWidth()) game.rectBounds.X = rand.Intn(context.ScreenWidth())
game.rect.Y = rand.Intn(context.ScreenHeight()) game.rectBounds.Y = rand.Intn(context.ScreenHeight())
game.rect.Width = rand.Intn(context.ScreenWidth() - game.rect.X) game.rectBounds.Width =
game.rect.Height = rand.Intn(context.ScreenHeight() - game.rect.Y) 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.R = uint8(rand.Intn(256))
game.rectColor.G = 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)) game.rectColor.A = uint8(rand.Intn(256))
} }
func (game *Rects) Draw(g graphics.Context) { func (game *Rects) rectGeometryMatrix() matrix.Geometry {
g.SetOffscreen(game.rectsTexture.ID()) 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.SetOffscreen(g.Screen().ID())
g.DrawTexture(game.rectsTexture.ID(), g.DrawTexture(game.rectsTexture.ID(),

View File

@ -43,7 +43,6 @@ type Context interface {
Screen() Texture Screen() Texture
Clear() Clear()
Fill(clr color.Color) Fill(clr color.Color)
DrawRect(rect Rect, clr color.Color)
DrawTexture(textureID TextureID, DrawTexture(textureID TextureID,
geometryMatrix matrix.Geometry, geometryMatrix matrix.Geometry,
colorMatrix matrix.Color) colorMatrix matrix.Color)

View File

@ -67,12 +67,12 @@ func (matrix *Geometry) Translate(tx, ty float64) {
} }
func (matrix *Geometry) Scale(x, y float64) { func (matrix *Geometry) Scale(x, y float64) {
matrix.Elements[0][0] = x * matrix.Elements[0][0] matrix.Elements[0][0] *= x
matrix.Elements[0][1] = x * matrix.Elements[0][1] matrix.Elements[0][1] *= x
matrix.Elements[0][2] = x * matrix.Elements[0][2] matrix.Elements[0][2] *= x
matrix.Elements[1][0] = y * matrix.Elements[1][0] matrix.Elements[1][0] *= y
matrix.Elements[1][1] = y * matrix.Elements[1][1] matrix.Elements[1][1] *= y
matrix.Elements[1][2] = y * matrix.Elements[1][2] matrix.Elements[1][2] *= y
} }
func (matrix *Geometry) Rotate(theta float64) { func (matrix *Geometry) Rotate(theta float64) {

View File

@ -91,53 +91,6 @@ func (context *Context) Fill(clr color.Color) {
C.glClear(C.GL_COLOR_BUFFER_BIT) 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( func (context *Context) DrawTexture(
textureID graphics.TextureID, textureID graphics.TextureID,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {