mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
Refactoring: matrix initialization
This commit is contained in:
parent
c53695eb56
commit
ea98fefc4f
28
colorm.go
28
colorm.go
@ -21,16 +21,6 @@ import (
|
|||||||
// ColorMDim is a dimension of a ColorM.
|
// ColorMDim is a dimension of a ColorM.
|
||||||
const ColorMDim = 5
|
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 represents a matrix to transform coloring when rendering an image.
|
||||||
//
|
//
|
||||||
// A ColorM is applied to the source alpha color
|
// A ColorM is applied to the source alpha color
|
||||||
@ -48,6 +38,14 @@ func (c *ColorM) dim() int {
|
|||||||
return ColorMDim
|
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).
|
// Element returns a value of a matrix at (i, j).
|
||||||
func (c *ColorM) Element(i, j int) float64 {
|
func (c *ColorM) Element(i, j int) float64 {
|
||||||
if !c.initialized {
|
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.
|
// Concat multiplies a color matrix with the other color matrix.
|
||||||
func (c *ColorM) Concat(other ColorM) {
|
func (c *ColorM) Concat(other ColorM) {
|
||||||
if !c.initialized {
|
if !c.initialized {
|
||||||
*c = colorMI
|
c.initialize()
|
||||||
}
|
}
|
||||||
result := ColorM{}
|
result := ColorM{}
|
||||||
mul(&other, c, &result)
|
mul(&other, c, &result)
|
||||||
@ -72,7 +70,7 @@ func (c *ColorM) Concat(other ColorM) {
|
|||||||
// Add adds a color matrix with the other color matrix.
|
// Add adds a color matrix with the other color matrix.
|
||||||
func (c *ColorM) Add(other ColorM) {
|
func (c *ColorM) Add(other ColorM) {
|
||||||
if !c.initialized {
|
if !c.initialized {
|
||||||
*c = colorMI
|
c.initialize()
|
||||||
}
|
}
|
||||||
result := ColorM{}
|
result := ColorM{}
|
||||||
add(&other, c, &result)
|
add(&other, c, &result)
|
||||||
@ -81,7 +79,7 @@ func (c *ColorM) Add(other ColorM) {
|
|||||||
|
|
||||||
func (c *ColorM) Scale(r, g, b, a float64) {
|
func (c *ColorM) Scale(r, g, b, a float64) {
|
||||||
if !c.initialized {
|
if !c.initialized {
|
||||||
*c = colorMI
|
c.initialize()
|
||||||
}
|
}
|
||||||
for i := 0; i < ColorMDim; i++ {
|
for i := 0; i < ColorMDim; i++ {
|
||||||
c.es[0][i] *= r
|
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) {
|
func (c *ColorM) Translate(r, g, b, a float64) {
|
||||||
if !c.initialized {
|
if !c.initialized {
|
||||||
*c = colorMI
|
c.initialize()
|
||||||
}
|
}
|
||||||
c.es[0][4] += r
|
c.es[0][4] += r
|
||||||
c.es[1][4] += g
|
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).
|
// SetElement sets an element at (i, j).
|
||||||
func (c *ColorM) SetElement(i, j int, element float64) {
|
func (c *ColorM) SetElement(i, j int, element float64) {
|
||||||
if !c.initialized {
|
if !c.initialized {
|
||||||
*c = colorMI
|
c.initialize()
|
||||||
}
|
}
|
||||||
c.es[i][j] = element
|
c.es[i][j] = element
|
||||||
}
|
}
|
||||||
|
@ -109,22 +109,24 @@ func (s *GameScene) drawBackground(r *ebiten.Image) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
w, h := imageGameBG.Size()
|
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)
|
scaleW := ScreenWidth / float64(w)
|
||||||
scaleH := ScreenHeight / float64(h)
|
scaleH := ScreenHeight / float64(h)
|
||||||
scale := scaleW
|
scale := scaleW
|
||||||
if scale < scaleH {
|
if scale < scaleH {
|
||||||
scale = scaleH
|
scale = scaleH
|
||||||
}
|
}
|
||||||
geo.Concat(ebiten.ScaleGeo(scale, scale))
|
geo.Scale(scale, scale)
|
||||||
geo.Concat(ebiten.TranslateGeo(ScreenWidth/2, ScreenHeight/2))
|
geo.Translate(ScreenWidth/2, ScreenHeight/2)
|
||||||
|
|
||||||
a := 0.7
|
a := 0.7
|
||||||
m := ebiten.Monochrome()
|
m := ebiten.Monochrome()
|
||||||
m.Concat(ebiten.ScaleColor(a, a, a, a))
|
m.Scale(a, a, a, a)
|
||||||
clr := ebiten.ScaleColor(1-a, 1-a, 1-a, 1-a)
|
var clr ebiten.ColorM
|
||||||
|
clr.Scale(1-a, 1-a, 1-a, 1-a)
|
||||||
clr.Add(m)
|
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{
|
return r.DrawImage(imageGameBG, &ebiten.DrawImageOptions{
|
||||||
GeoM: geo,
|
GeoM: geo,
|
||||||
ColorM: clr,
|
ColorM: clr,
|
||||||
|
24
geom.go
24
geom.go
@ -21,14 +21,6 @@ import (
|
|||||||
// GeoMDim is a dimension of a GeoM.
|
// GeoMDim is a dimension of a GeoM.
|
||||||
const GeoMDim = 3
|
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.
|
// A GeoM represents a matrix to transform geometry when rendering an image.
|
||||||
//
|
//
|
||||||
// The initial value is identity.
|
// The initial value is identity.
|
||||||
@ -41,6 +33,12 @@ func (g *GeoM) dim() int {
|
|||||||
return GeoMDim
|
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).
|
// Element returns a value of a matrix at (i, j).
|
||||||
func (g *GeoM) Element(i, j int) float64 {
|
func (g *GeoM) Element(i, j int) float64 {
|
||||||
if !g.initialized {
|
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.
|
// Concat multiplies a geometry matrix with the other geometry matrix.
|
||||||
func (g *GeoM) Concat(other GeoM) {
|
func (g *GeoM) Concat(other GeoM) {
|
||||||
if !g.initialized {
|
if !g.initialized {
|
||||||
*g = geoMI
|
g.initialize()
|
||||||
}
|
}
|
||||||
result := GeoM{}
|
result := GeoM{}
|
||||||
mul(&other, g, &result)
|
mul(&other, g, &result)
|
||||||
@ -65,7 +63,7 @@ func (g *GeoM) Concat(other GeoM) {
|
|||||||
// Add adds a geometry matrix with the other geometry matrix.
|
// Add adds a geometry matrix with the other geometry matrix.
|
||||||
func (g *GeoM) Add(other GeoM) {
|
func (g *GeoM) Add(other GeoM) {
|
||||||
if !g.initialized {
|
if !g.initialized {
|
||||||
*g = geoMI
|
g.initialize()
|
||||||
}
|
}
|
||||||
result := GeoM{}
|
result := GeoM{}
|
||||||
add(&other, g, &result)
|
add(&other, g, &result)
|
||||||
@ -74,7 +72,7 @@ func (g *GeoM) Add(other GeoM) {
|
|||||||
|
|
||||||
func (g *GeoM) Scale(x, y float64) {
|
func (g *GeoM) Scale(x, y float64) {
|
||||||
if !g.initialized {
|
if !g.initialized {
|
||||||
*g = geoMI
|
g.initialize()
|
||||||
}
|
}
|
||||||
for i := 0; i < GeoMDim; i++ {
|
for i := 0; i < GeoMDim; i++ {
|
||||||
g.es[0][i] *= x
|
g.es[0][i] *= x
|
||||||
@ -84,7 +82,7 @@ func (g *GeoM) Scale(x, y float64) {
|
|||||||
|
|
||||||
func (g *GeoM) Translate(tx, ty float64) {
|
func (g *GeoM) Translate(tx, ty float64) {
|
||||||
if !g.initialized {
|
if !g.initialized {
|
||||||
*g = geoMI
|
g.initialize()
|
||||||
}
|
}
|
||||||
g.es[0][2] += tx
|
g.es[0][2] += tx
|
||||||
g.es[1][2] += ty
|
g.es[1][2] += ty
|
||||||
@ -93,7 +91,7 @@ func (g *GeoM) Translate(tx, ty float64) {
|
|||||||
// SetElement sets an element at (i, j).
|
// SetElement sets an element at (i, j).
|
||||||
func (g *GeoM) SetElement(i, j int, element float64) {
|
func (g *GeoM) SetElement(i, j int, element float64) {
|
||||||
if !g.initialized {
|
if !g.initialized {
|
||||||
*g = geoMI
|
g.initialize()
|
||||||
}
|
}
|
||||||
g.es[i][j] = element
|
g.es[i][j] = element
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user