diff --git a/colorm.go b/colorm.go index f76ba5946..a2bfd6ccd 100644 --- a/colorm.go +++ b/colorm.go @@ -79,6 +79,26 @@ func (c *ColorM) Add(other ColorM) { *c = result } +func (c *ColorM) Scale(r, g, b, a float64) { + if !c.initialized { + *c = colorMI + } + c.es[0][0] *= r + c.es[1][1] *= g + c.es[2][2] *= b + c.es[3][3] *= a +} + +func (c *ColorM) Translate(r, g, b, a float64) { + if !c.initialized { + *c = colorMI + } + c.es[0][4] += r + c.es[1][4] += g + c.es[2][4] += b + c.es[3][4] += a +} + // SetElement sets an element at (i, j). func (c *ColorM) SetElement(i, j int, element float64) { if !c.initialized { diff --git a/example/blocks/blocks/font.go b/example/blocks/blocks/font.go index 2095bb4bb..531b6acdb 100644 --- a/example/blocks/blocks/font.go +++ b/example/blocks/blocks/font.go @@ -20,6 +20,7 @@ import ( "image" "image/color" "math" + "strings" ) var imageFont *ebiten.Image @@ -41,9 +42,10 @@ func textWidth(str string) int { } func drawText(rt *ebiten.Image, str string, ox, oy, scale int, c color.Color) error { - parts := make([]ebiten.ImagePart, 0, len(str)) + parts := make([]ebiten.ImagePart, len(strings.Replace(str, "\n", "", -1))) locationX, locationY := 0, 0 + i := 0 for _, c := range str { if c == '\n' { locationX = 0 @@ -53,27 +55,27 @@ func drawText(rt *ebiten.Image, str string, ox, oy, scale int, c color.Color) er code := int(c) x := (code % 16) * charWidth y := ((code - 32) / 16) * charHeight - parts = append(parts, ebiten.ImagePart{ - Dst: image.Rect(locationX, locationY, locationX+charWidth, locationY+charHeight), - Src: image.Rect(x, y, x+charWidth, y+charHeight), - }) + parts[i].Dst = image.Rect(locationX, locationY, locationX+charWidth, locationY+charHeight) + parts[i].Src = image.Rect(x, y, x+charWidth, y+charHeight) + i++ locationX += charWidth } - geo := ebiten.ScaleGeo(float64(scale), float64(scale)) - geo.Concat(ebiten.TranslateGeo(float64(ox), float64(oy))) + options := &ebiten.DrawImageOptions{ + Parts: parts, + } + options.GeoM.Scale(float64(scale), float64(scale)) + options.GeoM.Translate(float64(ox), float64(oy)) + c2 := color.NRGBA64Model.Convert(c).(color.NRGBA64) const max = math.MaxUint16 r := float64(c2.R) / max g := float64(c2.G) / max b := float64(c2.B) / max a := float64(c2.A) / max - clr := ebiten.ScaleColor(r, g, b, a) - return rt.DrawImage(imageFont, &ebiten.DrawImageOptions{ - Parts: parts, - GeoM: geo, - ColorM: clr, - }) + options.ColorM.Scale(r, g, b, a) + + return rt.DrawImage(imageFont, options) } func drawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color.Color) error { diff --git a/geom.go b/geom.go index f97fa3415..2ec8d2294 100644 --- a/geom.go +++ b/geom.go @@ -72,6 +72,22 @@ func (g *GeoM) Add(other GeoM) { *g = result } +func (g *GeoM) Scale(x, y float64) { + if !g.initialized { + *g = geoMI + } + g.es[0][0] *= x + g.es[1][1] *= y +} + +func (g *GeoM) Translate(tx, ty float64) { + if !g.initialized { + *g = geoMI + } + g.es[0][2] += tx + g.es[1][2] += ty +} + // SetElement sets an element at (i, j). func (g *GeoM) SetElement(i, j int, element float64) { if !g.initialized {