Refactoring: matrix initialization

This commit is contained in:
Hajime Hoshi 2015-01-03 22:54:01 +09:00
parent c53695eb56
commit ea98fefc4f
3 changed files with 32 additions and 34 deletions

View File

@ -21,16 +21,6 @@ import (
// ColorMDim is a dimension of a ColorM.
const ColorMDim = 5
var colorMI = ColorM{
initialized: true,
es: [ColorMDim - 1][ColorMDim]float64{
{1, 0, 0, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
},
}
// A ColorM represents a matrix to transform coloring when rendering an image.
//
// A ColorM is applied to the source alpha color
@ -48,6 +38,14 @@ func (c *ColorM) dim() int {
return ColorMDim
}
func (c *ColorM) initialize() {
c.initialized = true
c.es[0][0] = 1
c.es[1][1] = 1
c.es[2][2] = 1
c.es[3][3] = 1
}
// Element returns a value of a matrix at (i, j).
func (c *ColorM) Element(i, j int) float64 {
if !c.initialized {
@ -62,7 +60,7 @@ func (c *ColorM) Element(i, j int) float64 {
// Concat multiplies a color matrix with the other color matrix.
func (c *ColorM) Concat(other ColorM) {
if !c.initialized {
*c = colorMI
c.initialize()
}
result := ColorM{}
mul(&other, c, &result)
@ -72,7 +70,7 @@ func (c *ColorM) Concat(other ColorM) {
// Add adds a color matrix with the other color matrix.
func (c *ColorM) Add(other ColorM) {
if !c.initialized {
*c = colorMI
c.initialize()
}
result := ColorM{}
add(&other, c, &result)
@ -81,7 +79,7 @@ func (c *ColorM) Add(other ColorM) {
func (c *ColorM) Scale(r, g, b, a float64) {
if !c.initialized {
*c = colorMI
c.initialize()
}
for i := 0; i < ColorMDim; i++ {
c.es[0][i] *= r
@ -93,7 +91,7 @@ func (c *ColorM) Scale(r, g, b, a float64) {
func (c *ColorM) Translate(r, g, b, a float64) {
if !c.initialized {
*c = colorMI
c.initialize()
}
c.es[0][4] += r
c.es[1][4] += g
@ -104,7 +102,7 @@ func (c *ColorM) Translate(r, g, b, a float64) {
// SetElement sets an element at (i, j).
func (c *ColorM) SetElement(i, j int, element float64) {
if !c.initialized {
*c = colorMI
c.initialize()
}
c.es[i][j] = element
}

View File

@ -109,22 +109,24 @@ func (s *GameScene) drawBackground(r *ebiten.Image) error {
}
w, h := imageGameBG.Size()
geo := ebiten.TranslateGeo(-float64(w)/2, -float64(h)/2)
var geo ebiten.GeoM
geo.Translate(-float64(w)/2, -float64(h)/2)
scaleW := ScreenWidth / float64(w)
scaleH := ScreenHeight / float64(h)
scale := scaleW
if scale < scaleH {
scale = scaleH
}
geo.Concat(ebiten.ScaleGeo(scale, scale))
geo.Concat(ebiten.TranslateGeo(ScreenWidth/2, ScreenHeight/2))
geo.Scale(scale, scale)
geo.Translate(ScreenWidth/2, ScreenHeight/2)
a := 0.7
m := ebiten.Monochrome()
m.Concat(ebiten.ScaleColor(a, a, a, a))
clr := ebiten.ScaleColor(1-a, 1-a, 1-a, 1-a)
m.Scale(a, a, a, a)
var clr ebiten.ColorM
clr.Scale(1-a, 1-a, 1-a, 1-a)
clr.Add(m)
clr.Concat(ebiten.TranslateColor(0.3, 0.3, 0.3, 0))
clr.Translate(0.3, 0.3, 0.3, 0)
return r.DrawImage(imageGameBG, &ebiten.DrawImageOptions{
GeoM: geo,
ColorM: clr,

24
geom.go
View File

@ -21,14 +21,6 @@ import (
// GeoMDim is a dimension of a GeoM.
const GeoMDim = 3
var geoMI = GeoM{
initialized: true,
es: [2][3]float64{
{1, 0, 0},
{0, 1, 0},
},
}
// A GeoM represents a matrix to transform geometry when rendering an image.
//
// The initial value is identity.
@ -41,6 +33,12 @@ func (g *GeoM) dim() int {
return GeoMDim
}
func (g *GeoM) initialize() {
g.initialized = true
g.es[0][0] = 1
g.es[1][1] = 1
}
// Element returns a value of a matrix at (i, j).
func (g *GeoM) Element(i, j int) float64 {
if !g.initialized {
@ -55,7 +53,7 @@ func (g *GeoM) Element(i, j int) float64 {
// Concat multiplies a geometry matrix with the other geometry matrix.
func (g *GeoM) Concat(other GeoM) {
if !g.initialized {
*g = geoMI
g.initialize()
}
result := GeoM{}
mul(&other, g, &result)
@ -65,7 +63,7 @@ func (g *GeoM) Concat(other GeoM) {
// Add adds a geometry matrix with the other geometry matrix.
func (g *GeoM) Add(other GeoM) {
if !g.initialized {
*g = geoMI
g.initialize()
}
result := GeoM{}
add(&other, g, &result)
@ -74,7 +72,7 @@ func (g *GeoM) Add(other GeoM) {
func (g *GeoM) Scale(x, y float64) {
if !g.initialized {
*g = geoMI
g.initialize()
}
for i := 0; i < GeoMDim; i++ {
g.es[0][i] *= x
@ -84,7 +82,7 @@ func (g *GeoM) Scale(x, y float64) {
func (g *GeoM) Translate(tx, ty float64) {
if !g.initialized {
*g = geoMI
g.initialize()
}
g.es[0][2] += tx
g.es[1][2] += ty
@ -93,7 +91,7 @@ func (g *GeoM) Translate(tx, ty float64) {
// SetElement sets an element at (i, j).
func (g *GeoM) SetElement(i, j int, element float64) {
if !g.initialized {
*g = geoMI
g.initialize()
}
g.es[i][j] = element
}