From efab3695126a0aa5835acca7ba2d3c23ac941033 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 15 Mar 2026 17:45:28 +0900 Subject: [PATCH] all: go fix --- audio/audio_test.go | 4 +- audio/internal/convert/float32.go | 2 +- audio/internal/convert/float32_test.go | 2 +- audio/internal/convert/resampling.go | 5 +- audio/internal/convert/resampling_test.go | 2 +- audio/internal/convert/stereof32_test.go | 2 +- audio/internal/convert/stereoi16_test.go | 6 +- audio/loop.go | 5 +- audio/vorbis/float32.go | 2 +- audio/vorbis/int16.go | 2 +- cmd/ebitenmobile/gomobile.go | 4 +- cmd/ebitenmobile/main.go | 2 +- colorm/colorm_test.go | 32 +-- colorm/draw_test.go | 10 +- examples/2048/2048/board.go | 2 +- examples/2048/2048/tile.go | 2 +- examples/2048/2048/tile_test.go | 4 +- examples/airship/main.go | 6 +- examples/blocks/blocks/field.go | 14 +- examples/blocks/blocks/gamepad.go | 6 +- examples/blocks/blocks/gamescene.go | 4 +- examples/blocks/blocks/piece.go | 12 +- examples/blocks/blocks/titlescene.go | 8 +- examples/chipmunk/main.go | 4 +- examples/drag/main.go | 2 +- examples/flappy/main.go | 2 +- examples/font/main.go | 4 +- examples/gamepad/main.go | 10 +- examples/infinitescroll/main.go | 4 +- examples/lasttouch/main.go | 8 +- examples/life/main.go | 6 +- examples/paint/main.go | 8 +- examples/pcm/main.go | 4 +- examples/perspective/main.go | 2 +- examples/piano/main.go | 6 +- examples/polygons/main.go | 2 +- examples/squiral/main.go | 8 +- examples/stars/main.go | 6 +- examples/subimage/main.go | 4 +- examples/ui/main.go | 12 +- examples/video/mpegplayer.go | 4 +- examples/windowsize/main.go | 4 +- image_test.go | 48 ++-- inpututil/inpututil.go | 9 +- internal/affine/affine.go | 6 +- internal/affine/colorm.go | 2 +- internal/affine/colorm_test.go | 8 +- internal/atlas/image.go | 2 +- internal/atlas/image_test.go | 86 ++++---- internal/atlas/set.go | 6 +- internal/graphics/shader.go | 30 +-- internal/graphicscommand/image_test.go | 8 +- internal/graphicsdriver/opengl/program.go | 2 +- internal/mipmap/mipmap.go | 2 +- internal/shader/shader.go | 2 +- internal/shader/syntax_test.go | 28 +-- internal/testing/shader.go | 10 +- internal/ui/monitor_glfw.go | 2 +- internal/ui/shader.go | 8 +- internal/ui/ui_darwin.go | 4 +- internal/ui/ui_glfw.go | 2 +- internal/vettools/imageimportcheck.go | 2 +- shader_test.go | 254 +++++++++++----------- text/text.go | 2 +- text/text_test.go | 10 +- text/v2/text.go | 2 +- text/v2/text_test.go | 14 +- vector/path_test.go | 10 +- 68 files changed, 385 insertions(+), 412 deletions(-) diff --git a/audio/audio_test.go b/audio/audio_test.go index acc06e56e..42fbc5ea8 100644 --- a/audio/audio_test.go +++ b/audio/audio_test.go @@ -56,7 +56,7 @@ func TestGC(t *testing.T) { p = nil runtime.GC() - for i := 0; i < 10; i++ { + for range 10 { got = audio.PlayersCountForTesting() if want := 0; got == want { return @@ -103,7 +103,7 @@ func TestSameSourcePlayers(t *testing.T) { p0.Play() p1.Play() - for i := 0; i < 10; i++ { + for range 10 { if err := audio.UpdateForTesting(); err != nil { // An error is expected. return diff --git a/audio/internal/convert/float32.go b/audio/internal/convert/float32.go index 08ff57350..dbb5b18d9 100644 --- a/audio/internal/convert/float32.go +++ b/audio/internal/convert/float32.go @@ -59,7 +59,7 @@ func (r *float32BytesReader) Read(buf []byte) (int, error) { // Convert int16 bytes to float32 bytes and fill buf. samplesToFill := min(len(r.i16Buf)/2, len(buf)/4) - for i := 0; i < samplesToFill; i++ { + for i := range samplesToFill { vi16l := r.i16Buf[2*i] vi16h := r.i16Buf[2*i+1] v := float32(int16(vi16l)|int16(vi16h)<<8) / (1 << 15) diff --git a/audio/internal/convert/float32_test.go b/audio/internal/convert/float32_test.go index 1c124d030..9a32b4327 100644 --- a/audio/internal/convert/float32_test.go +++ b/audio/internal/convert/float32_test.go @@ -90,7 +90,7 @@ func TestFloat32(t *testing.T) { } if seek { // Shifting by incomplete bytes should not affect the result. - for i := 0; i < 4; i++ { + for i := range 4 { if _, err := r.Seek(int64(i), io.SeekCurrent); err != nil { if err != io.EOF { t.Fatal(err) diff --git a/audio/internal/convert/resampling.go b/audio/internal/convert/resampling.go index 274eced60..cbffde773 100644 --- a/audio/internal/convert/resampling.go +++ b/audio/internal/convert/resampling.go @@ -211,10 +211,7 @@ func (r *Resampling) src(i int64) (float64, float64, error) { func (r *Resampling) at(t int64) (float64, float64, error) { windowSize := 8.0 tInSrc := float64(t) * float64(r.from) / float64(r.to) - startN := int64(tInSrc - windowSize) - if startN < 0 { - startN = 0 - } + startN := max(int64(tInSrc-windowSize), 0) endN := int64(tInSrc + windowSize) var lv, rv float64 var eof bool diff --git a/audio/internal/convert/resampling_test.go b/audio/internal/convert/resampling_test.go index cbb1dab86..5b0581824 100644 --- a/audio/internal/convert/resampling_test.go +++ b/audio/internal/convert/resampling_test.go @@ -29,7 +29,7 @@ func soundAt(timeInSecond float64) float64 { amp := []float64{1.0, 0.8, 0.6, 0.4, 0.2} v := 0.0 - for j := 0; j < len(amp); j++ { + for j := range amp { v += amp[j] * math.Sin(2.0*math.Pi*timeInSecond*freq*float64(j+1)) / 2 } if v > 1 { diff --git a/audio/internal/convert/stereof32_test.go b/audio/internal/convert/stereof32_test.go index 1a2b51aeb..b2325d727 100644 --- a/audio/internal/convert/stereof32_test.go +++ b/audio/internal/convert/stereof32_test.go @@ -87,7 +87,7 @@ func TestStereoF32(t *testing.T) { break } // Shifting by incomplete bytes should not affect the result. - for i := 0; i < 4*2; i++ { + for i := range 4 * 2 { if _, err := s.Seek(int64(i), io.SeekCurrent); err != nil { if err != io.EOF { t.Fatal(err) diff --git a/audio/internal/convert/stereoi16_test.go b/audio/internal/convert/stereoi16_test.go index 49b1c0eb2..863ecd1af 100644 --- a/audio/internal/convert/stereoi16_test.go +++ b/audio/internal/convert/stereoi16_test.go @@ -77,7 +77,7 @@ func TestStereoI16FromSigned16Bits(t *testing.T) { break } // Shifting by incomplete bytes should not affect the result. - for i := 0; i < 2*2; i++ { + for i := range 2 * 2 { if _, err := s.Seek(int64(i), io.SeekCurrent); err != nil { if err != io.EOF { t.Fatal(err) @@ -158,7 +158,7 @@ func TestStereoI16FromUnsigned8Bits(t *testing.T) { break } // Shifting by incomplete bytes should not affect the result. - for i := 0; i < 2*2; i++ { + for i := range 2 * 2 { if _, err := s.Seek(int64(i), io.SeekCurrent); err != nil { if err != io.EOF { t.Fatal(err) @@ -238,7 +238,7 @@ func TestStereoI16FromSigned24Bits(t *testing.T) { break } // Shifting by incomplete bytes should not affect the result. - for i := 0; i < 2*2; i++ { + for i := range 2 * 2 { if _, err := s.Seek(int64(i), io.SeekCurrent); err != nil { if err != io.EOF { t.Fatal(err) diff --git a/audio/loop.go b/audio/loop.go index ee9d911bf..3c4c58dd1 100644 --- a/audio/loop.go +++ b/audio/loop.go @@ -209,10 +209,7 @@ func (i *InfiniteLoop) Read(b []byte) (int, error) { // Read the afterLoop part if necessary. if i.pos == i.length() && err == nil { if i.afterLoop == nil { - buflen := int64(256 * i.bytesPerSample) - if buflen > i.length() { - buflen = i.length() - } + buflen := min(int64(256*i.bytesPerSample), i.length()) buf := make([]byte, buflen) pos := 0 diff --git a/audio/vorbis/float32.go b/audio/vorbis/float32.go index 0ac5b847c..01cf623a0 100644 --- a/audio/vorbis/float32.go +++ b/audio/vorbis/float32.go @@ -54,7 +54,7 @@ func (r *float32BytesReadSeeker) Read(buf []byte) (int, error) { return 0, err } - for i := 0; i < n; i++ { + for i := range n { v := math.Float32bits(r.fbuf[i]) buf[4*i] = byte(v) buf[4*i+1] = byte(v >> 8) diff --git a/audio/vorbis/int16.go b/audio/vorbis/int16.go index eca609797..808f45f23 100644 --- a/audio/vorbis/int16.go +++ b/audio/vorbis/int16.go @@ -64,7 +64,7 @@ func (r *int16BytesReader) Read(buf []byte) (int, error) { if len(buf) == 1 && n > 0 { b = make([]byte, 2) } - for i := 0; i < n; i++ { + for i := range n { f := r.fbuf[i] s := int16(f * (1<<15 - 1)) b[2*i] = byte(s) diff --git a/cmd/ebitenmobile/gomobile.go b/cmd/ebitenmobile/gomobile.go index 2623b45a2..ccbd212e0 100644 --- a/cmd/ebitenmobile/gomobile.go +++ b/cmd/ebitenmobile/gomobile.go @@ -131,7 +131,7 @@ func prepareGomobileCommands() (string, error) { if err := runGo("mod", "init", modname); err != nil { return tmp, err } - if err := os.WriteFile("tools.go", []byte(fmt.Sprintf(`%s + if err := os.WriteFile("tools.go", fmt.Appendf(nil, `%s package %s @@ -139,7 +139,7 @@ import ( _ "github.com/ebitengine/gomobile/cmd/gobind" _ "github.com/ebitengine/gomobile/cmd/gomobile" ) -`, buildtags, modname)), 0644); err != nil { +`, buildtags, modname), 0644); err != nil { return tmp, err } diff --git a/cmd/ebitenmobile/main.go b/cmd/ebitenmobile/main.go index 3e516b5db..6e709b0e8 100644 --- a/cmd/ebitenmobile/main.go +++ b/cmd/ebitenmobile/main.go @@ -309,7 +309,7 @@ func isValidJavaPackageName(name string) bool { return false } // A Java package name consists of one or more Java identifiers separated by dots. - for _, token := range strings.Split(name, ".") { + for token := range strings.SplitSeq(name, ".") { if !isValidJavaIdentifier(token) { return false } diff --git a/colorm/colorm_test.go b/colorm/colorm_test.go index b8b14f6a5..c7e094869 100644 --- a/colorm/colorm_test.go +++ b/colorm/colorm_test.go @@ -24,8 +24,8 @@ import ( func TestColorMInit(t *testing.T) { var m colorm.ColorM - for i := 0; i < colorm.Dim-1; i++ { - for j := 0; j < colorm.Dim; j++ { + for i := range colorm.Dim - 1 { + for j := range colorm.Dim { got := m.Element(i, j) want := 0.0 if i == j { @@ -38,8 +38,8 @@ func TestColorMInit(t *testing.T) { } m.SetElement(0, 0, 1) - for i := 0; i < colorm.Dim-1; i++ { - for j := 0; j < colorm.Dim; j++ { + for i := range colorm.Dim - 1 { + for j := range colorm.Dim { got := m.Element(i, j) want := 0.0 if i == j { @@ -73,8 +73,8 @@ func TestColorMTranslate(t *testing.T) { } m := colorm.ColorM{} m.Translate(0.5, 1.5, 2.5, 3.5) - for i := 0; i < 4; i++ { - for j := 0; j < 5; j++ { + for i := range 4 { + for j := range 5 { got := m.Element(i, j) want := expected[i][j] if want != got { @@ -93,8 +93,8 @@ func TestColorMScale(t *testing.T) { } m := colorm.ColorM{} m.Scale(0.5, 1.5, 2.5, 3.5) - for i := 0; i < 4; i++ { - for j := 0; j < 5; j++ { + for i := range 4 { + for j := range 5 { got := m.Element(i, j) want := expected[i][j] if want != got { @@ -114,8 +114,8 @@ func TestColorMTranslateAndScale(t *testing.T) { m := colorm.ColorM{} m.Translate(0, 0, 0, 1) m.Scale(1, 1, 1, 0.5) - for i := 0; i < 4; i++ { - for j := 0; j < 5; j++ { + for i := range 4 { + for j := range 5 { got := m.Element(i, j) want := expected[i][j] if want != got { @@ -134,8 +134,8 @@ func TestColorMMonochrome(t *testing.T) { } m := colorm.ColorM{} m.ChangeHSV(0, 0, 1) - for i := 0; i < 4; i++ { - for j := 0; j < 5; j++ { + for i := range 4 { + for j := range 5 { got := m.Element(i, j) want := expected[i][j] if math.Abs(want-got) > 0.0001 { @@ -153,14 +153,14 @@ func TestColorMConcatSelf(t *testing.T) { {25, 37, 39, 46, 36}, } m := colorm.ColorM{} - for i := 0; i < 4; i++ { - for j := 0; j < 5; j++ { + for i := range 4 { + for j := range 5 { m.SetElement(i, j, float64((i+j)%5+1)) } } m.Concat(m) - for i := 0; i < 4; i++ { - for j := 0; j < 5; j++ { + for i := range 4 { + for j := range 5 { got := m.Element(i, j) want := expected[i][j] if want != got { diff --git a/colorm/draw_test.go b/colorm/draw_test.go index e25b6bb1f..e43794a9a 100644 --- a/colorm/draw_test.go +++ b/colorm/draw_test.go @@ -172,8 +172,8 @@ func TestDrawTrianglesWithColorM(t *testing.T) { op.ColorScaleMode = format dst1.DrawTriangles(vs1, is, src, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst0.At(i, j) want := dst1.At(i, j) if got != want { @@ -279,15 +279,15 @@ func TestColorMCopy(t *testing.T) { dst := ebiten.NewImage(w, h) src := ebiten.NewImage(w, h) - for k := 0; k < 256; k++ { + for k := range 256 { var cm colorm.ColorM cm.Translate(1, 1, 1, float64(k)/0xff) op := &colorm.DrawImageOptions{} op.Blend = ebiten.BlendCopy colorm.DrawImage(dst, src, cm, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: byte(k), G: byte(k), B: byte(k), A: byte(k)} if !sameColors(got, want, 1) { diff --git a/examples/2048/2048/board.go b/examples/2048/2048/board.go index a4df2ae26..3a44dfdc5 100644 --- a/examples/2048/2048/board.go +++ b/examples/2048/2048/board.go @@ -37,7 +37,7 @@ func NewBoard(size int) (*Board, error) { size: size, tiles: map[*Tile]struct{}{}, } - for i := 0; i < 2; i++ { + for range 2 { if err := addRandomTile(b.tiles, b.size); err != nil { return nil, err } diff --git a/examples/2048/2048/tile.go b/examples/2048/2048/tile.go index 0c1d04881..996f670bf 100644 --- a/examples/2048/2048/tile.go +++ b/examples/2048/2048/tile.go @@ -158,7 +158,7 @@ func MoveTiles(tiles map[*Tile]struct{}, size int, dir Dir) bool { vx, vy := dir.Vector() tx := []int{} ty := []int{} - for i := 0; i < size; i++ { + for i := range size { tx = append(tx, i) ty = append(ty, i) } diff --git a/examples/2048/2048/tile_test.go b/examples/2048/2048/tile_test.go index 8f668acc0..9ebd18fdf 100644 --- a/examples/2048/2048/tile_test.go +++ b/examples/2048/2048/tile_test.go @@ -23,8 +23,8 @@ import ( func cellsToTiles(cells []int, size int) map[*twenty48.Tile]struct{} { tiles := map[*twenty48.Tile]struct{}{} - for j := 0; j < size; j++ { - for i := 0; i < size; i++ { + for j := range size { + for i := range size { c := cells[i+j*size] if c == 0 { continue diff --git a/examples/airship/main.go b/examples/airship/main.go index 2ebc6e635..4fce3eeb7 100644 --- a/examples/airship/main.go +++ b/examples/airship/main.go @@ -172,7 +172,7 @@ func (g *Game) drawGroundImage(screen *ebiten.Image, ground *ebiten.Image) { g.perspectiveGroundImage.Clear() gw := ground.Bounds().Dx() pw, ph := g.perspectiveGroundImage.Bounds().Dx(), g.perspectiveGroundImage.Bounds().Dy() - for j := 0; j < ph; j++ { + for j := range ph { // z is in [2, -1] rate := float64(j) / float64(ph) z := (1-rate)*2 + rate*-1 @@ -219,7 +219,7 @@ func NewGame() *Game { const fogHeight = 16 w := g.perspectiveGroundImage.Bounds().Dx() fogRGBA := image.NewRGBA(image.Rect(0, 0, w, fogHeight)) - for j := 0; j < fogHeight; j++ { + for j := range fogHeight { a := uint32(float64(fogHeight-1-j) * 0xff / (fogHeight - 1)) clr := skyColor r, g, b, oa := uint32(clr.R), uint32(clr.G), uint32(clr.B), uint32(clr.A) @@ -227,7 +227,7 @@ func NewGame() *Game { clr.G = uint8(g * a / oa) clr.B = uint8(b * a / oa) clr.A = uint8(a) - for i := 0; i < w; i++ { + for i := range w { fogRGBA.SetRGBA(i, j, clr) } } diff --git a/examples/blocks/blocks/field.go b/examples/blocks/blocks/field.go index e1c51d7a0..b6118b3aa 100644 --- a/examples/blocks/blocks/field.go +++ b/examples/blocks/blocks/field.go @@ -128,7 +128,7 @@ func (f *Field) flushable() bool { // flushableLine returns a boolean value indicating whether // the line j is flushable or not. func (f *Field) flushableLine(j int) bool { - for i := 0; i < fieldBlockCountX; i++ { + for i := range fieldBlockCountX { if f.blocks[i][j] == BlockTypeNone { return false } @@ -156,17 +156,17 @@ func (f *Field) endFlushAnimating() int { // flushLine returns a boolean value indicating whether // the line is actually flushed. func (f *Field) flushLine(j int) bool { - for i := 0; i < fieldBlockCountX; i++ { + for i := range fieldBlockCountX { if f.blocks[i][j] == BlockTypeNone { return false } } for j2 := j; 1 <= j2; j2-- { - for i := 0; i < fieldBlockCountX; i++ { + for i := range fieldBlockCountX { f.blocks[i][j2] = f.blocks[i][j2-1] } } - for i := 0; i < fieldBlockCountX; i++ { + for i := range fieldBlockCountX { f.blocks[i][0] = BlockTypeNone } return true @@ -198,13 +198,13 @@ func flushingColor(rate float64) colorm.ColorM { func (f *Field) Draw(r *ebiten.Image, x, y int) { fc := flushingColor(float64(f.flushCount) / maxFlushCount) - for j := 0; j < fieldBlockCountY; j++ { + for j := range fieldBlockCountY { if f.flushableLine(j) { - for i := 0; i < fieldBlockCountX; i++ { + for i := range fieldBlockCountX { drawBlock(r, f.blocks[i][j], i*blockWidth+x, j*blockHeight+y, fc) } } else { - for i := 0; i < fieldBlockCountX; i++ { + for i := range fieldBlockCountX { drawBlock(r, f.blocks[i][j], i*blockWidth+x, j*blockHeight+y, colorm.ColorM{}) } } diff --git a/examples/blocks/blocks/gamepad.go b/examples/blocks/blocks/gamepad.go index 8fba580d8..4557e0367 100644 --- a/examples/blocks/blocks/gamepad.go +++ b/examples/blocks/blocks/gamepad.go @@ -124,7 +124,7 @@ func (c *gamepadConfig) initializeIfNeeded() { if c.defaultAxesValues == nil { c.defaultAxesValues = map[ebiten.GamepadAxisType]float64{} na := ebiten.GamepadAxisType(ebiten.GamepadAxisCount(c.gamepadID)) - for a := ebiten.GamepadAxisType(0); a < na; a++ { + for a := range na { c.defaultAxesValues[a] = ebiten.GamepadAxisValue(c.gamepadID, a) } } @@ -150,7 +150,7 @@ func (c *gamepadConfig) Scan(b virtualGamepadButton) bool { delete(c.axes, b) ebn := ebiten.GamepadButton(ebiten.GamepadButtonCount(c.gamepadID)) - for eb := ebiten.GamepadButton(0); eb < ebn; eb++ { + for eb := range ebn { if _, ok := c.assignedButtons[eb]; ok { continue } @@ -162,7 +162,7 @@ func (c *gamepadConfig) Scan(b virtualGamepadButton) bool { } na := ebiten.GamepadAxisType(ebiten.GamepadAxisCount(c.gamepadID)) - for a := ebiten.GamepadAxisType(0); a < na; a++ { + for a := range na { v := ebiten.GamepadAxisValue(c.gamepadID, a) const delta = 0.25 diff --git a/examples/blocks/blocks/gamescene.go b/examples/blocks/blocks/gamescene.go index df8e7f2a4..86ea0fc6b 100644 --- a/examples/blocks/blocks/gamescene.go +++ b/examples/blocks/blocks/gamescene.go @@ -154,8 +154,8 @@ func init() { var mono colorm.ColorM mono.ChangeHSV(0, 0, 1) - for j := 0; j < colorm.Dim-1; j++ { - for i := 0; i < colorm.Dim-1; i++ { + for j := range colorm.Dim - 1 { + for i := range colorm.Dim - 1 { lightGray.SetElement(i, j, mono.Element(i, j)*0.7+id.Element(i, j)*0.3) } } diff --git a/examples/blocks/blocks/piece.go b/examples/blocks/blocks/piece.go index 7548d9af0..fb5e4daa6 100644 --- a/examples/blocks/blocks/piece.go +++ b/examples/blocks/blocks/piece.go @@ -183,8 +183,8 @@ func (p *Piece) InitialPosition() (int, int) { x := (fieldBlockCountX - size) / 2 y := 0 Loop: - for j := 0; j < size; j++ { - for i := 0; i < size; i++ { + for j := range size { + for i := range size { if p.blocks[i][j] { break Loop } @@ -218,8 +218,8 @@ func (p *Piece) isBlocked(i, j int, angle Angle) bool { // the piece at (x, y) with the given angle would collide with the field's blocks. func (p *Piece) collides(field *Field, x, y int, angle Angle) bool { size := len(p.blocks) - for i := 0; i < size; i++ { - for j := 0; j < size; j++ { + for i := range size { + for j := range size { if field.IsBlocked(x+i, y+j) && p.isBlocked(i, j, angle) { return true } @@ -230,8 +230,8 @@ func (p *Piece) collides(field *Field, x, y int, angle Angle) bool { func (p *Piece) AbsorbInto(field *Field, x, y int, angle Angle) { size := len(p.blocks) - for i := 0; i < size; i++ { - for j := 0; j < size; j++ { + for i := range size { + for j := range size { if p.isBlocked(i, j, angle) { field.setBlock(x+i, y+j, p.blockType) } diff --git a/examples/blocks/blocks/titlescene.go b/examples/blocks/blocks/titlescene.go index 78aa930a3..18976e528 100644 --- a/examples/blocks/blocks/titlescene.go +++ b/examples/blocks/blocks/titlescene.go @@ -19,6 +19,7 @@ import ( "image" "image/color" _ "image/png" + "slices" "github.com/hajimehoshi/ebiten/v2" rblocks "github.com/hajimehoshi/ebiten/v2/examples/resources/images/blocks" @@ -45,12 +46,7 @@ func anyGamepadVirtualButtonJustPressed(i *Input) bool { return false } - for _, b := range virtualGamepadButtons { - if i.gamepadConfig.IsButtonJustPressed(b) { - return true - } - } - return false + return slices.ContainsFunc(virtualGamepadButtons, i.gamepadConfig.IsButtonJustPressed) } func (s *TitleScene) Update(state *GameState) error { diff --git a/examples/chipmunk/main.go b/examples/chipmunk/main.go index f734b6a5b..a7ad26d83 100644 --- a/examples/chipmunk/main.go +++ b/examples/chipmunk/main.go @@ -59,8 +59,8 @@ func NewGame() *Game { var body *cp.Body var shape *cp.Shape - for y := 0; y < imageHeight; y++ { - for x := 0; x < imageWidth; x++ { + for y := range imageHeight { + for x := range imageWidth { if getPixel(uint(x), uint(y)) == 0 { continue } diff --git a/examples/drag/main.go b/examples/drag/main.go index e2475e88c..24989d705 100644 --- a/examples/drag/main.go +++ b/examples/drag/main.go @@ -187,7 +187,7 @@ func NewGame() *Game { // Initialize the sprites. sprites := []*Sprite{} w, h := ebitenImage.Bounds().Dx(), ebitenImage.Bounds().Dy() - for i := 0; i < 50; i++ { + for range 50 { s := &Sprite{ image: ebitenImage, alphaImage: ebitenAlphaImage, diff --git a/examples/flappy/main.go b/examples/flappy/main.go index 6b8ab9436..f37f7bd4a 100644 --- a/examples/flappy/main.go +++ b/examples/flappy/main.go @@ -398,7 +398,7 @@ func (g *Game) drawTiles(screen *ebiten.Image) { // pipe if tileY, ok := g.pipeAt(floorDiv(g.cameraX, tileSize) + i); ok { - for j := 0; j < tileY; j++ { + for j := range tileY { op.GeoM.Reset() op.GeoM.Scale(1, -1) op.GeoM.Translate(float64(i*tileSize-floorMod(g.cameraX, tileSize)), diff --git a/examples/font/main.go b/examples/font/main.go index 374447466..874864b94 100644 --- a/examples/font/main.go +++ b/examples/font/main.go @@ -103,8 +103,8 @@ func (g *Game) Update() error { // Change the text color for each second. if g.counter%ebiten.TPS() == 0 { g.kanjiText = "" - for j := 0; j < 6; j++ { - for i := 0; i < 12; i++ { + for range 6 { + for range 12 { g.kanjiText += string(jaKanjis[rand.IntN(len(jaKanjis))]) } g.kanjiText += "\n" diff --git a/examples/gamepad/main.go b/examples/gamepad/main.go index 64628a288..e103a954f 100644 --- a/examples/gamepad/main.go +++ b/examples/gamepad/main.go @@ -17,7 +17,7 @@ package main import ( "fmt" "log" - "sort" + "slices" "strconv" "strings" "time" @@ -61,13 +61,13 @@ func (g *Game) Update() error { g.pressedButtons = map[ebiten.GamepadID][]string{} for id := range g.gamepadIDs { maxAxis := ebiten.GamepadAxisType(ebiten.GamepadAxisCount(id)) - for a := ebiten.GamepadAxisType(0); a < maxAxis; a++ { + for a := range maxAxis { v := ebiten.GamepadAxisValue(id, a) g.axes[id] = append(g.axes[id], fmt.Sprintf("%d:%+0.2f", a, v)) } maxButton := ebiten.GamepadButton(ebiten.GamepadButtonCount(id)) - for b := ebiten.GamepadButton(0); b < maxButton; b++ { + for b := range maxButton { if ebiten.IsGamepadButtonPressed(id, b) { g.pressedButtons[id] = append(g.pressedButtons[id], strconv.Itoa(int(b))) } @@ -178,9 +178,7 @@ func (g *Game) Draw(screen *ebiten.Image) { for id := range g.gamepadIDs { ids = append(ids, id) } - sort.Slice(ids, func(a, b int) bool { - return ids[a] < ids[b] - }) + slices.Sort(ids) for _, id := range ids { var standard string if ebiten.IsStandardGamepadLayoutAvailable(id) { diff --git a/examples/infinitescroll/main.go b/examples/infinitescroll/main.go index 408abe39f..546d9b462 100644 --- a/examples/infinitescroll/main.go +++ b/examples/infinitescroll/main.go @@ -80,8 +80,8 @@ func (g *Game) Draw(screen *ebiten.Image) { // Draw bgImage on the screen repeatedly. const repeat = 3 w, h := bgImage.Bounds().Dx(), bgImage.Bounds().Dy() - for j := 0; j < repeat; j++ { - for i := 0; i < repeat; i++ { + for j := range repeat { + for i := range repeat { op := &ebiten.DrawImageOptions{} op.GeoM.Translate(float64(w*i), float64(h*j)) op.GeoM.Translate(offsetX, offsetY) diff --git a/examples/lasttouch/main.go b/examples/lasttouch/main.go index 21a1f3c2a..4f969cb99 100644 --- a/examples/lasttouch/main.go +++ b/examples/lasttouch/main.go @@ -17,6 +17,7 @@ package main import ( "fmt" "log" + "strings" "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/ebitenutil" @@ -64,11 +65,12 @@ func (g *Game) Update() error { } func (g *Game) Draw(screen *ebiten.Image) { - msg := "Touch the screen and release your finger from it.\n\nLast Positions:\n" + var msg strings.Builder + msg.WriteString("Touch the screen and release your finger from it.\n\nLast Positions:\n") for _, p := range g.lastTouchPositions { - msg += fmt.Sprintf(" (%d, %d)\n", p.x, p.y) + msg.WriteString(fmt.Sprintf(" (%d, %d)\n", p.x, p.y)) } - ebitenutil.DebugPrint(screen, msg) + ebitenutil.DebugPrint(screen, msg.String()) } func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) { diff --git a/examples/life/main.go b/examples/life/main.go index bdc94de35..d7b2045e8 100644 --- a/examples/life/main.go +++ b/examples/life/main.go @@ -33,7 +33,7 @@ func NewWorld(width, height int, maxInitLiveCells int) *World { // init inits world with a random state. func (w *World) init(maxLiveCells int) { - for i := 0; i < maxLiveCells; i++ { + for range maxLiveCells { x := rand.IntN(w.width) y := rand.IntN(w.height) w.area[y*w.width+x] = true @@ -45,8 +45,8 @@ func (w *World) Update() { width := w.width height := w.height next := make([]bool, width*height) - for y := 0; y < height; y++ { - for x := 0; x < width; x++ { + for y := range height { + for x := range width { pop := neighbourCount(w.area, width, height, x, y) switch { case pop < 2: diff --git a/examples/paint/main.go b/examples/paint/main.go index de3b5df13..5d716d586 100644 --- a/examples/paint/main.go +++ b/examples/paint/main.go @@ -20,6 +20,7 @@ import ( "image/color" "log" "math" + "strings" "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/colorm" @@ -130,11 +131,12 @@ func (g *Game) paint(canvas *ebiten.Image, x, y int) { func (g *Game) Draw(screen *ebiten.Image) { screen.DrawImage(g.canvasImage, nil) - msg := fmt.Sprintf("(%d, %d)", g.cursor.x, g.cursor.y) + var msg strings.Builder + msg.WriteString(fmt.Sprintf("(%d, %d)", g.cursor.x, g.cursor.y)) for _, t := range g.touches { - msg += fmt.Sprintf("\n(%d, %d) touch %d", t.pos.x, t.pos.y, t.id) + msg.WriteString(fmt.Sprintf("\n(%d, %d) touch %d", t.pos.x, t.pos.y, t.id)) } - ebitenutil.DebugPrint(screen, msg) + ebitenutil.DebugPrint(screen, msg.String()) } func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) { diff --git a/examples/pcm/main.go b/examples/pcm/main.go index a4da0fdab..69e4d12c5 100644 --- a/examples/pcm/main.go +++ b/examples/pcm/main.go @@ -53,7 +53,7 @@ var score = strings.Replace( // square fills out with square wave values with the specified volume, frequency and sequence. func square(out []float32, volume float32, freq float32, sequence float32) { if freq == 0 { - for i := 0; i < len(out); i++ { + for i := range out { out[i] = 0 } return @@ -62,7 +62,7 @@ func square(out []float32, volume float32, freq float32, sequence float32) { if length == 0 { panic("invalid freq") } - for i := 0; i < len(out); i++ { + for i := range out { a := volume if i%length < int(float32(length)*sequence) { a = -a diff --git a/examples/perspective/main.go b/examples/perspective/main.go index 0fdc574ff..d4e83e814 100644 --- a/examples/perspective/main.go +++ b/examples/perspective/main.go @@ -43,7 +43,7 @@ func (g *Game) Draw(screen *ebiten.Image) { // Split the image into horizontal lines and draw them with different scales. op := &ebiten.DrawImageOptions{} w, h := gophersImage.Bounds().Dx(), gophersImage.Bounds().Dy() - for i := 0; i < h; i++ { + for i := range h { op.GeoM.Reset() // Move the image's center to the upper-left corner. diff --git a/examples/piano/main.go b/examples/piano/main.go index 118b9bbfc..33c5987ba 100644 --- a/examples/piano/main.go +++ b/examples/piano/main.go @@ -59,7 +59,7 @@ func pianoAt(i int, freq float32) float32 { amp := []float32{1.0, 0.8, 0.6, 0.4, 0.2} x := []float32{4.0, 2.0, 1.0, 0.5, 0.25} var v float32 - for j := 0; j < len(amp); j++ { + for j := range amp { // Decay a := amp[j] * float32(math.Exp(float64(-5*float32(i)*freq/baseFreq/(x[j]*sampleRate)))) v += a * float32(math.Sin(2.0*math.Pi*float64(i)*float64(freq)*float64(j+1)/sampleRate)) @@ -103,7 +103,7 @@ func init() { const refFreq = 110 length := 4 * sampleRate * baseFreq / refFreq refData := make([]float32, length) - for i := 0; i < length; i++ { + for i := range length { refData[i] = pianoAt(i, refFreq) } @@ -114,7 +114,7 @@ func init() { length := 4 * sampleRate * baseFreq / int(freq) l := make([]float32, length) r := make([]float32, length) - for i := 0; i < length; i++ { + for i := range length { idx := int(float64(i) * freq / refFreq) if len(refData) <= idx { break diff --git a/examples/polygons/main.go b/examples/polygons/main.go index cc28752a4..bb7c50309 100644 --- a/examples/polygons/main.go +++ b/examples/polygons/main.go @@ -47,7 +47,7 @@ func genVertices(num int) []ebiten.Vertex { ) vs := []ebiten.Vertex{} - for i := 0; i < num; i++ { + for i := range num { rate := float64(i) / float64(num) cr := 0.0 cg := 0.0 diff --git a/examples/squiral/main.go b/examples/squiral/main.go index 5ce28bbab..3d4a327fb 100644 --- a/examples/squiral/main.go +++ b/examples/squiral/main.go @@ -241,8 +241,8 @@ func (au *automaton) init(game *Game) { // Init the test grid with color (0,0,0,0) and the borders of // it with color(0,0,0,254) as a blocker color, so the squirals // cannot escape the scene. - for x := 0; x < width; x++ { - for y := 0; y < height; y++ { + for x := range width { + for y := range height { if x == 0 || x == width-1 || y == 0 || y == height-1 { au.colorMap[x][y] = blocker } else { @@ -251,13 +251,13 @@ func (au *automaton) init(game *Game) { } } - for i := 0; i < numOfSquirals; i++ { + for i := range numOfSquirals { au.squirals[i].spawn(game) } } func (a *automaton) step(game *Game) { - for i := 0; i < numOfSquirals; i++ { + for i := range numOfSquirals { for s := 0; s < a.squirals[i].speed; s++ { a.squirals[i].step(game) if a.squirals[i].dead { diff --git a/examples/stars/main.go b/examples/stars/main.go index 1f611e37b..7fe7e6c50 100644 --- a/examples/stars/main.go +++ b/examples/stars/main.go @@ -71,7 +71,7 @@ type Game struct { func NewGame() *Game { g := &Game{} - for i := 0; i < starsCount; i++ { + for i := range starsCount { g.stars[i].Init() } return g @@ -79,14 +79,14 @@ func NewGame() *Game { func (g *Game) Update() error { x, y := ebiten.CursorPosition() - for i := 0; i < starsCount; i++ { + for i := range starsCount { g.stars[i].Update(float32(x*scale), float32(y*scale)) } return nil } func (g *Game) Draw(screen *ebiten.Image) { - for i := 0; i < starsCount; i++ { + for i := range starsCount { g.stars[i].Draw(screen) } } diff --git a/examples/subimage/main.go b/examples/subimage/main.go index 2dac8eb5c..47f122790 100644 --- a/examples/subimage/main.go +++ b/examples/subimage/main.go @@ -48,8 +48,8 @@ func (g *Game) Draw(screen *ebiten.Image) { sw, sh := screen.Bounds().Dx(), screen.Bounds().Dy() cw := sw / cx ch := sh / cy - for j := 0; j < cy; j++ { - for i := 0; i < cx; i++ { + for j := range cy { + for i := range cx { r := image.Rect(cw*i, ch*j, cw*(i+1), ch*(j+1)) img := g.offscreen.SubImage(r).(*ebiten.Image) diff --git a/examples/ui/main.go b/examples/ui/main.go index aa30d0411..bf4b59810 100644 --- a/examples/ui/main.go +++ b/examples/ui/main.go @@ -102,8 +102,8 @@ func drawNinePatches(dst *ebiten.Image, dstRect image.Rectangle, srcRect image.R dstH := dstRect.Dy() op := &ebiten.DrawImageOptions{} - for j := 0; j < 3; j++ { - for i := 0; i < 3; i++ { + for j := range 3 { + for i := range 3 { op.GeoM.Reset() sx := srcX @@ -254,13 +254,7 @@ func (v *VScrollBar) Update(contentHeight int) { if v.dragging { if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { _, y := ebiten.CursorPosition() - v.thumbOffset = v.draggingStartOffset + (y - v.draggingStartY) - if v.thumbOffset < 0 { - v.thumbOffset = 0 - } - if v.thumbOffset > v.maxThumbOffset() { - v.thumbOffset = v.maxThumbOffset() - } + v.thumbOffset = min(max(v.draggingStartOffset+(y-v.draggingStartY), 0), v.maxThumbOffset()) } else { v.dragging = false } diff --git a/examples/video/mpegplayer.go b/examples/video/mpegplayer.go index 5aed7cfef..549db0e3e 100644 --- a/examples/video/mpegplayer.go +++ b/examples/video/mpegplayer.go @@ -170,14 +170,14 @@ func (p *mpegPlayer) updateFrame() error { return fmt.Errorf("video: subsample ratio must be 4:2:0") } w, h := p.mpg.Width(), p.mpg.Height() - for j := 0; j < h; j++ { + for j := range h { yi := j * img.YStride ci := (j / 2) * img.CStride // Create temporary slices to encourage BCE (boundary-checking elimination). ys := img.Y[yi : yi+w] cbs := img.Cb[ci : ci+w/2] crs := img.Cr[ci : ci+w/2] - for i := 0; i < w; i++ { + for i := range w { idx := 4 * (j*w + i) buf := p.yCbCrBytes[idx : idx+3] buf[0] = ys[i] diff --git a/examples/windowsize/main.go b/examples/windowsize/main.go index 22d7b6e72..41c59c8b4 100644 --- a/examples/windowsize/main.go +++ b/examples/windowsize/main.go @@ -74,8 +74,8 @@ func createRandomIconImage() image.Image { bf := float64(rand.IntN(0x100)) img := ebiten.NewImage(size, size) pix := make([]byte, 4*size*size) - for j := 0; j < size; j++ { - for i := 0; i < size; i++ { + for j := range size { + for i := range size { af := float64(i+j) / float64(2*size) if af > 0 { pix[4*(j*size+i)] = byte(rf * af) diff --git a/image_test.go b/image_test.go index 5bcfb4fd4..fe8704712 100644 --- a/image_test.go +++ b/image_test.go @@ -399,8 +399,8 @@ func TestNewImageFromSubImage(t *testing.T) { if h2 != sh { t.Errorf("eimg Width: got %v; want %v", h2, sh) } - for j := 0; j < h2; j++ { - for i := 0; i < w2; i++ { + for j := range h2 { + for i := range w2 { got := eimg.At(i, j) want := color.RGBAModel.Convert(img.At(i+1, j+1)) if got != want { @@ -495,7 +495,7 @@ func TestImageEdge(t *testing.T) { transparent := color.RGBA{} angles := []float64{} - for a := 0; a < 1440; a++ { + for a := range 1440 { angles = append(angles, float64(a)/1440*2*math.Pi) } for a := 0; a < 4096; a += 3 { @@ -573,8 +573,8 @@ func TestImageEdge(t *testing.T) { img1.DrawTriangles(vs, is, img0, op) } allTransparent := true - for j := 0; j < img1Height; j++ { - for i := 0; i < img1Width; i++ { + for j := range img1Height { + for i := range img1Width { c := img1.At(i, j) if c == transparent { continue @@ -2447,7 +2447,7 @@ func TestImageColorMCopy(t *testing.T) { dst := ebiten.NewImage(w, h) src := ebiten.NewImage(w, h) - for k := 0; k < 256; k++ { + for k := range 256 { op := &ebiten.DrawImageOptions{} op.ColorM.Translate(1, 1, 1, float64(k)/0xff) op.Blend = ebiten.BlendCopy @@ -4819,27 +4819,23 @@ func TestSubImageRaceConditionWithFill(t *testing.T) { subImages := make(chan *ebiten.Image) var wg sync.WaitGroup - wg.Add(1) // Create a goroutine to create sub-images. - go func() { - for i := 0; i < h; i++ { - for j := 0; j < w; j++ { + wg.Go(func() { + for i := range h { + for j := range w { subImages <- img.SubImage(image.Rect(i, j, i+1, j+1)).(*ebiten.Image) } } close(subImages) - wg.Done() - }() + }) // Create goroutines to use the sub-images. for img := range subImages { - wg.Add(1) - go func() { - for j := 0; j < 1000; j++ { + wg.Go(func() { + for range 1000 { img.Fill(color.RGBA{0xff, 0xff, 0xff, 0xff}) } - wg.Done() - }() + }) } wg.Wait() } @@ -4851,27 +4847,23 @@ func TestSubImageRaceConditionWithSubImage(t *testing.T) { subImages := make(chan *ebiten.Image) var wg sync.WaitGroup - wg.Add(1) // Create a goroutine to create sub-images. - go func() { - for i := 0; i < h; i++ { - for j := 0; j < w; j++ { + wg.Go(func() { + for i := range h { + for j := range w { subImages <- img.SubImage(image.Rect(i, j, i+1, j+1)).(*ebiten.Image) } } close(subImages) - wg.Done() - }() + }) // Create goroutines to use the sub-images. for img := range subImages { - wg.Add(1) - go func() { - for j := 0; j < 1000; j++ { + wg.Go(func() { + for range 1000 { img.SubImage(img.Bounds()) } - wg.Done() - }() + }) } wg.Wait() } diff --git a/inpututil/inpututil.go b/inpututil/inpututil.go index a4e259f57..b002ad51a 100644 --- a/inpututil/inpututil.go +++ b/inpututil/inpututil.go @@ -16,6 +16,7 @@ package inpututil import ( + "maps" "slices" "sync" @@ -71,9 +72,7 @@ func (i *inputState) update() { // Copy the gamepad states. clear(i.prevGamepadStates) - for id, s := range i.gamepadStates { - i.prevGamepadStates[id] = s - } + maps.Copy(i.prevGamepadStates, i.gamepadStates) i.gamepadIDsBuf = ebiten.AppendGamepadIDs(i.gamepadIDsBuf[:0]) for _, id := range i.gamepadIDsBuf { @@ -109,9 +108,7 @@ func (i *inputState) update() { // Copy the touch durations and positions. clear(i.prevTouchStates) - for id, state := range i.touchStates { - i.prevTouchStates[id] = state - } + maps.Copy(i.prevTouchStates, i.touchStates) i.touchIDsBuf = ebiten.AppendTouchIDs(i.touchIDsBuf[:0]) for _, id := range i.touchIDsBuf { diff --git a/internal/affine/affine.go b/internal/affine/affine.go index b988766d0..3cfbd1e40 100644 --- a/internal/affine/affine.go +++ b/internal/affine/affine.go @@ -16,10 +16,10 @@ package affine func mulSquare(lhs, rhs *[16]float32, dim int) [16]float32 { result := [16]float32{} - for i := 0; i < dim; i++ { - for j := 0; j < dim; j++ { + for i := range dim { + for j := range dim { e := float32(0.0) - for k := 0; k < dim; k++ { + for k := range dim { e += lhs[i*dim+k] * rhs[k*dim+j] } result[i*dim+j] = e diff --git a/internal/affine/colorm.go b/internal/affine/colorm.go index be390697c..d3132a471 100644 --- a/internal/affine/colorm.go +++ b/internal/affine/colorm.go @@ -667,7 +667,7 @@ func (c *colorMImplBodyTranslate) Scale(r, g, b, a float32) ColorM { } eb := c.body - for i := 0; i < ColorMDim-1; i++ { + for i := range ColorMDim - 1 { eb[i*(ColorMDim-1)] *= r eb[i*(ColorMDim-1)+1] *= g eb[i*(ColorMDim-1)+2] *= b diff --git a/internal/affine/colorm_test.go b/internal/affine/colorm_test.go index 9257f0769..80761869b 100644 --- a/internal/affine/colorm_test.go +++ b/internal/affine/colorm_test.go @@ -151,8 +151,8 @@ func TestColorMIsInvertible(t *testing.T) { func arrayToColorM(es [4][5]float32) affine.ColorM { var a affine.ColorM = affine.ColorMIdentity{} - for j := 0; j < 5; j++ { - for i := 0; i < 4; i++ { + for j := range 5 { + for i := range 4 { a = affine.ColorMSetElement(a, i, j, es[i][j]) } } @@ -167,8 +167,8 @@ func abs(x float32) float32 { } func equalWithDelta(a, b affine.ColorM, delta float32) bool { - for j := 0; j < 5; j++ { - for i := 0; i < 4; i++ { + for j := range 5 { + for i := range 4 { ea := a.At(i, j) eb := b.At(i, j) if abs(ea-eb) > delta { diff --git a/internal/atlas/image.go b/internal/atlas/image.go index cad384990..271b29e29 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -574,7 +574,7 @@ func (i *Image) writePixels(pix []byte, region image.Rectangle) { pixb := graphics.NewManagedBytes(4*r.Dx()*r.Dy(), func(bs []byte) { // Clear the edges. bs might not be zero-cleared. rowPixels := 4 * r.Dx() - for i := 0; i < rowPixels; i++ { + for i := range rowPixels { bs[rowPixels*(r.Dy()-1)+i] = 0 } for j := 1; j < r.Dy(); j++ { diff --git a/internal/atlas/image_test.go b/internal/atlas/image_test.go index deab5a8ef..df53936a1 100644 --- a/internal/atlas/image_test.go +++ b/internal/atlas/image_test.go @@ -82,8 +82,8 @@ func TestEnsureIsolatedFromSourceBackend(t *testing.T) { defer img3.Deallocate() pix := make([]byte, size*size*4) - for j := 0; j < size; j++ { - for i := 0; i < size; i++ { + for j := range size { + for i := range size { pix[4*(i+j*size)] = byte(i + j) pix[4*(i+j*size)+1] = byte(i + j) pix[4*(i+j*size)+2] = byte(i + j) @@ -125,8 +125,8 @@ func TestEnsureIsolatedFromSourceBackend(t *testing.T) { if !ok { t.Fatal("ReadPixels failed") } - for j := 0; j < size; j++ { - for i := 0; i < size; i++ { + for j := range size { + for i := range size { r := pix[4*(size*j+i)] g := pix[4*(size*j+i)+1] b := pix[4*(size*j+i)+2] @@ -168,8 +168,8 @@ func TestReputOnSourceBackend(t *testing.T) { img2 := atlas.NewImage(size, size, atlas.ImageTypeRegular) defer img2.Deallocate() pix := make([]byte, 4*size*size) - for j := 0; j < size; j++ { - for i := 0; i < size; i++ { + for j := range size { + for i := range size { pix[4*(i+j*size)] = byte(i + j) pix[4*(i+j*size)+1] = byte(i + j) pix[4*(i+j*size)+2] = byte(i + j) @@ -191,7 +191,7 @@ func TestReputOnSourceBackend(t *testing.T) { dr := image.Rect(0, 0, size, size) sr := image.Rect(0, 0, size, size) // Render onto img1. The count should not matter. - for i := 0; i < 5; i++ { + for range 5 { vs := quadVertices(size, size, 0, 0, 1) img1.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{img2}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{sr}, atlas.NearestFilterShader, nil) if got, want := img1.IsOnSourceBackendForTesting(), false; got != want { @@ -202,7 +202,7 @@ func TestReputOnSourceBackend(t *testing.T) { // Use img1 as a render source. // Use the doubled count since img1 was on a texture atlas and became an isolated image once. // Then, img1 requires longer time to recover to be on a texture atlas again. - for i := 0; i < atlas.BaseCountToPutOnSourceBackend*2; i++ { + for range atlas.BaseCountToPutOnSourceBackend * 2 { atlas.PutImagesOnSourceBackendForTesting() vs := quadVertices(size, size, 0, 0, 1) img0.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{sr}, atlas.NearestFilterShader, nil) @@ -227,8 +227,8 @@ func TestReputOnSourceBackend(t *testing.T) { if !ok { t.Fatal("ReadPixels failed") } - for j := 0; j < size; j++ { - for i := 0; i < size; i++ { + for j := range size { + for i := range size { want := color.RGBA{R: byte(i + j), G: byte(i + j), B: byte(i + j), A: byte(i + j)} r := pix[4*(size*j+i)] g := pix[4*(size*j+i)+1] @@ -255,8 +255,8 @@ func TestReputOnSourceBackend(t *testing.T) { if !ok { t.Fatal("ReadPixels failed") } - for j := 0; j < size; j++ { - for i := 0; i < size; i++ { + for j := range size { + for i := range size { want := color.RGBA{R: byte(i + j), G: byte(i + j), B: byte(i + j), A: byte(i + j)} r := pix[4*(size*j+i)] g := pix[4*(size*j+i)+1] @@ -270,7 +270,7 @@ func TestReputOnSourceBackend(t *testing.T) { } // Use img1 as a render target again. The count should not matter. - for i := 0; i < 5; i++ { + for range 5 { vs := quadVertices(size, size, 0, 0, 1) img1.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{img2}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{sr}, atlas.NearestFilterShader, nil) if got, want := img1.IsOnSourceBackendForTesting(), false; got != want { @@ -280,7 +280,7 @@ func TestReputOnSourceBackend(t *testing.T) { // Use img1 as a render source, but call WritePixels. // Now use 4x count as img1 became an isolated image again. - for i := 0; i < atlas.BaseCountToPutOnSourceBackend*4; i++ { + for range atlas.BaseCountToPutOnSourceBackend * 4 { atlas.PutImagesOnSourceBackendForTesting() img1.WritePixels(make([]byte, 4*size*size), image.Rect(0, 0, size, size)) vs := quadVertices(size, size, 0, 0, 1) @@ -299,7 +299,7 @@ func TestReputOnSourceBackend(t *testing.T) { } // Use img3 as a render source. As img3 is unmanaged, img3 is never on an atlas. - for i := 0; i < atlas.BaseCountToPutOnSourceBackend*2; i++ { + for range atlas.BaseCountToPutOnSourceBackend * 2 { atlas.PutImagesOnSourceBackendForTesting() vs := quadVertices(size, size, 0, 0, 1) img0.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{img3}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{sr}, atlas.NearestFilterShader, nil) @@ -317,7 +317,7 @@ func TestExtend(t *testing.T) { defer img0.Deallocate() p0 := make([]byte, 4*w0*h0) - for i := 0; i < w0*h0; i++ { + for i := range w0 * h0 { p0[4*i] = byte(i) p0[4*i+1] = byte(i) p0[4*i+2] = byte(i) @@ -330,7 +330,7 @@ func TestExtend(t *testing.T) { defer img1.Deallocate() p1 := make([]byte, 4*w1*h1) - for i := 0; i < w1*h1; i++ { + for i := range w1 * h1 { p1[4*i] = byte(i) p1[4*i+1] = byte(i) p1[4*i+2] = byte(i) @@ -347,8 +347,8 @@ func TestExtend(t *testing.T) { if !ok { t.Fatal("ReadPixels failed") } - for j := 0; j < h0; j++ { - for i := 0; i < w0; i++ { + for j := range h0 { + for i := range w0 { r := pix0[4*(w0*j+i)] g := pix0[4*(w0*j+i)+1] b := pix0[4*(w0*j+i)+2] @@ -370,8 +370,8 @@ func TestExtend(t *testing.T) { if !ok { t.Fatal("ReadPixels failed") } - for j := 0; j < h1; j++ { - for i := 0; i < w1; i++ { + for j := range h1 { + for i := range w1 { r := pix1[4*(w1*j+i)] g := pix1[4*(w1*j+i)+1] b := pix1[4*(w1*j+i)+2] @@ -394,7 +394,7 @@ func TestWritePixelsAfterDrawTriangles(t *testing.T) { defer dst.Deallocate() pix := make([]byte, 4*w*h) - for i := 0; i < w*h; i++ { + for i := range w * h { pix[4*i] = byte(i) pix[4*i+1] = byte(i) pix[4*i+2] = byte(i) @@ -417,8 +417,8 @@ func TestWritePixelsAfterDrawTriangles(t *testing.T) { if !ok { t.Fatal("ReadPixels failed") } - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { r := pix[4*(w*j+i)] g := pix[4*(w*j+i)+1] b := pix[4*(w*j+i)+2] @@ -442,7 +442,7 @@ func TestSmallImages(t *testing.T) { defer dst.Deallocate() pix := make([]byte, 4*w*h) - for i := 0; i < w*h; i++ { + for i := range w * h { pix[4*i] = 0xff pix[4*i+1] = 0xff pix[4*i+2] = 0xff @@ -464,8 +464,8 @@ func TestSmallImages(t *testing.T) { if !ok { t.Fatal("ReadPixels failed") } - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { r := pix[4*(w*j+i)] a := pix[4*(w*j+i)+3] if got, want := r, byte(0xff); got != want { @@ -489,7 +489,7 @@ func TestLongImages(t *testing.T) { defer dst.Deallocate() pix := make([]byte, 4*w*h) - for i := 0; i < w*h; i++ { + for i := range w * h { pix[4*i] = 0xff pix[4*i+1] = 0xff pix[4*i+2] = 0xff @@ -512,8 +512,8 @@ func TestLongImages(t *testing.T) { if !ok { t.Fatal("ReadPixels failed") } - for j := 0; j < h; j++ { - for i := 0; i < w*scale; i++ { + for j := range h { + for i := range w * scale { r := pix[4*(dstW*j+i)] a := pix[4*(dstW*j+i)+3] if got, want := r, byte(0xff); got != want { @@ -625,7 +625,7 @@ func TestDeallocatedAndReputOnSourceBackend(t *testing.T) { } // Use src as a render source. - for i := 0; i < atlas.BaseCountToPutOnSourceBackend/2; i++ { + for range atlas.BaseCountToPutOnSourceBackend / 2 { atlas.PutImagesOnSourceBackendForTesting() vs := quadVertices(size, size, 0, 0, 1) dst.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{sr}, atlas.NearestFilterShader, nil) @@ -662,7 +662,7 @@ func TestImageIsNotReputOnSourceBackendWithoutUsingAsSource(t *testing.T) { // // Call DrawTriangles multiple times. // The number of DrawTriangles doesn't matter as long as these are called in one frame. - for i := 0; i < 2; i++ { + for range 2 { src2.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{sr}, atlas.NearestFilterShader, nil) } if got, want := src2.IsOnSourceBackendForTesting(), false; got != want { @@ -671,7 +671,7 @@ func TestImageIsNotReputOnSourceBackendWithoutUsingAsSource(t *testing.T) { // Update the count without using src2 as a rendering source. // This should not affect whether src2 is on an atlas or not. - for i := 0; i < atlas.BaseCountToPutOnSourceBackend; i++ { + for range atlas.BaseCountToPutOnSourceBackend { atlas.PutImagesOnSourceBackendForTesting() if got, want := src2.IsOnSourceBackendForTesting(), false; got != want { t.Errorf("got: %v, want: %v", got, want) @@ -679,7 +679,7 @@ func TestImageIsNotReputOnSourceBackendWithoutUsingAsSource(t *testing.T) { } // Update the count with using src2 as a rendering source. - for i := 0; i < atlas.BaseCountToPutOnSourceBackend; i++ { + for range atlas.BaseCountToPutOnSourceBackend { atlas.PutImagesOnSourceBackendForTesting() vs := quadVertices(size, size, 0, 0, 1) dst.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src2}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{sr}, atlas.NearestFilterShader, nil) @@ -700,8 +700,8 @@ func TestImageWritePixelsModify(t *testing.T) { img := atlas.NewImage(size, size, typ) defer img.Deallocate() pix := make([]byte, 4*size*size) - for j := 0; j < size; j++ { - for i := 0; i < size; i++ { + for j := range size { + for i := range size { pix[4*(i+j*size)] = byte(i + j) pix[4*(i+j*size)+1] = byte(i + j) pix[4*(i+j*size)+2] = byte(i + j) @@ -711,8 +711,8 @@ func TestImageWritePixelsModify(t *testing.T) { img.WritePixels(pix, image.Rect(0, 0, size, size)) // Modify pix after WritePixels. - for j := 0; j < size; j++ { - for i := 0; i < size; i++ { + for j := range size { + for i := range size { pix[4*(i+j*size)] = 0 pix[4*(i+j*size)+1] = 0 pix[4*(i+j*size)+2] = 0 @@ -729,8 +729,8 @@ func TestImageWritePixelsModify(t *testing.T) { if !ok { t.Fatal("ReadPixels failed") } - for j := 0; j < size; j++ { - for i := 0; i < size; i++ { + for j := range size { + for i := range size { want := color.RGBA{R: byte(i + j), G: byte(i + j), B: byte(i + j), A: byte(i + j)} r := pix[4*(size*j+i)] g := pix[4*(size*j+i)+1] @@ -808,14 +808,14 @@ func TestDestinationCountOverflow(t *testing.T) { sr := image.Rect(0, 0, w, h) // Use dst0 as a destination for a while. - for i := 0; i < 31; i++ { + for range 31 { dst0.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{sr}, atlas.NearestFilterShader, nil) atlas.PutImagesOnSourceBackendForTesting() } // Use dst0 as a source for a while. // As dst0 is used as a destination too many times (31 is a maximum), dst0's backend should never be a source backend. - for i := 0; i < 100; i++ { + for i := range 100 { dst1.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{dst0}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{sr}, atlas.NearestFilterShader, nil) atlas.PutImagesOnSourceBackendForTesting() if dst0.IsOnSourceBackendForTesting() { @@ -849,7 +849,7 @@ func TestIteratingImagesToPutOnSourceBackend(t *testing.T) { // Use srcs as sources. This will register an image to imagesToPutOnSourceBackend. // Check iterating the registered image works correctly. - for i := 0; i < 100; i++ { + for range 100 { for _, src := range srcs { dst.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{sr}, atlas.NearestFilterShader, nil) } diff --git a/internal/atlas/set.go b/internal/atlas/set.go index 3bc23cccc..ed82e6bfa 100644 --- a/internal/atlas/set.go +++ b/internal/atlas/set.go @@ -29,10 +29,8 @@ func (s *imageSmallSet) add(image *Image) { if image == nil { panic("atlas: nil image cannot be added") } - for _, img := range s.s { - if img == image { - return - } + if slices.Contains(s.s, image) { + return } s.s = append(s.s, image) } diff --git a/internal/graphics/shader.go b/internal/graphics/shader.go index 7a5686f92..e81035ff0 100644 --- a/internal/graphics/shader.go +++ b/internal/graphics/shader.go @@ -17,13 +17,15 @@ package graphics import ( "bytes" "fmt" + "strings" "github.com/hajimehoshi/ebiten/v2/internal/shader" "github.com/hajimehoshi/ebiten/v2/internal/shaderir" ) func shaderSuffix(unit shaderir.Unit) (string, error) { - shaderSuffix := fmt.Sprintf(` + var shaderSuffix strings.Builder + shaderSuffix.WriteString(fmt.Sprintf(` var __imageDstTextureSize vec2 // imageDstTextureSize returns the destination image's texture size in pixels. @@ -89,10 +91,10 @@ var __imageSrcRegionSizes [%[1]d]vec2 func imageSrcRegionOnTexture() (vec2, vec2) { return __imageSrcRegionOrigins[0], __imageSrcRegionSizes[0] } -`, ShaderSrcImageCount) +`, ShaderSrcImageCount)) - for i := 0; i < ShaderSrcImageCount; i++ { - shaderSuffix += fmt.Sprintf(` + for i := range ShaderSrcImageCount { + shaderSuffix.WriteString(fmt.Sprintf(` // imageSrc%[1]dOrigin returns the source image's region origin on its texture. // The unit is the source texture's pixel or texel. // @@ -106,7 +108,7 @@ func imageSrc%[1]dOrigin() vec2 { func imageSrc%[1]dSize() vec2 { return __imageSrcRegionSizes[%[1]d] } -`, i) +`, i)) pos := "pos" if i >= 1 { @@ -121,24 +123,24 @@ func imageSrc%[1]dSize() vec2 { } } // __t%d is a special variable for a texture variable. - shaderSuffix += fmt.Sprintf(` + shaderSuffix.WriteString(fmt.Sprintf(` func imageSrc%[1]dUnsafeAt(pos vec2) vec4 { // pos is the position in positions of the source texture (= 0th image's texture). return __texelAt(__t%[1]d, %[2]s) } -`, i, pos) +`, i, pos)) switch unit { case shaderir.Pixels: - shaderSuffix += fmt.Sprintf(` + shaderSuffix.WriteString(fmt.Sprintf(` func imageSrc%[1]dAt(pos vec2) vec4 { // pos is the position of the source texture (= 0th image's texture). // If pos is in the region, the result is (1, 1). Otherwise, either element is 0. in := step(__imageSrcRegionOrigins[0], pos) - step(__imageSrcRegionOrigins[0] + __imageSrcRegionSizes[%[1]d], pos) return __texelAt(__t%[1]d, %[2]s) * in.x * in.y } -`, i, pos) +`, i, pos)) case shaderir.Texels: - shaderSuffix += fmt.Sprintf(` + shaderSuffix.WriteString(fmt.Sprintf(` func imageSrc%[1]dAt(pos vec2) vec4 { // pos is the position of the source texture (= 0th image's texture). // If pos is in the region, the result is (1, 1). Otherwise, either element is 0. @@ -147,18 +149,18 @@ func imageSrc%[1]dAt(pos vec2) vec4 { in := step(__imageSrcRegionOrigins[0], pos) - step(__imageSrcRegionOrigins[0] + __imageSrcRegionSizes[0], pos) return __texelAt(__t%[1]d, %[2]s) * in.x * in.y } -`, i, pos) +`, i, pos)) } } - shaderSuffix += ` + shaderSuffix.WriteString(` var __projectionMatrix mat4 func __vertex(dstPos vec2, srcPos vec2, color vec4, custom vec4) (vec4, vec2, vec4, vec4) { return __projectionMatrix * vec4(dstPos, 0, 1), srcPos, color, custom } -` - return shaderSuffix, nil +`) + return shaderSuffix.String(), nil } func completeShaderSource(fragmentSrc []byte) ([]byte, error) { diff --git a/internal/graphicscommand/image_test.go b/internal/graphicscommand/image_test.go index 20089cf38..2ebc3f418 100644 --- a/internal/graphicscommand/image_test.go +++ b/internal/graphicscommand/image_test.go @@ -68,8 +68,8 @@ func TestClear(t *testing.T) { }); err != nil { t.Fatal(err) } - for j := 0; j < h/2; j++ { - for i := 0; i < w/2; i++ { + for j := range h / 2 { + for i := range w / 2 { idx := 4 * (i + w*j) got := color.RGBA{R: pix[idx], G: pix[idx+1], B: pix[idx+2], A: pix[idx+3]} want := color.RGBA{} @@ -125,8 +125,8 @@ func TestShader(t *testing.T) { }); err != nil { t.Fatal(err) } - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { idx := 4 * (i + w*j) got := color.RGBA{R: pix[idx], G: pix[idx+1], B: pix[idx+2], A: pix[idx+3]} want := color.RGBA{R: 0xff, A: 0xff} diff --git a/internal/graphicsdriver/opengl/program.go b/internal/graphicsdriver/opengl/program.go index 20fffaedf..09dfd770e 100644 --- a/internal/graphicsdriver/opengl/program.go +++ b/internal/graphicsdriver/opengl/program.go @@ -243,7 +243,7 @@ func areSameUint32Array(a, b []uint32) bool { if len(a) != len(b) { return false } - for i := 0; i < len(a); i++ { + for i := range a { if a[i] != b[i] { return false } diff --git a/internal/mipmap/mipmap.go b/internal/mipmap/mipmap.go index 464701d3f..18fe9c96b 100644 --- a/internal/mipmap/mipmap.go +++ b/internal/mipmap/mipmap.go @@ -229,7 +229,7 @@ func (m *Mipmap) level(level int) *buffered.Image { } func sizeForLevel(x int, level int) int { - for i := 0; i < level; i++ { + for range level { x /= 2 if x == 0 { return 0 diff --git a/internal/shader/shader.go b/internal/shader/shader.go index 31619299f..cb5b9885c 100644 --- a/internal/shader/shader.go +++ b/internal/shader/shader.go @@ -831,7 +831,7 @@ func (cs *compileState) parseFunc(block *block, d *ast.FuncDecl) (function, bool if diff := len(cs.ir.Varyings) - (len(inParams) - 1); diff > 0 { // inParams is not enough when the vertex shader has more returning values than the fragment shader's arguments. orig := len(inParams) - 1 - for i := 0; i < diff; i++ { + for i := range diff { inParams = append(inParams, variable{ name: "_", typ: cs.ir.Varyings[orig+i], diff --git a/internal/shader/syntax_test.go b/internal/shader/syntax_test.go index f29202e33..e8141d483 100644 --- a/internal/shader/syntax_test.go +++ b/internal/shader/syntax_test.go @@ -1300,12 +1300,12 @@ func TestSyntaxOperatorMultiply(t *testing.T) { } for _, c := range cases { - _, err := compileToIR([]byte(fmt.Sprintf(`package main + _, err := compileToIR(fmt.Appendf(nil, `package main func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { %s return dstPos -}`, c.stmt))) +}`, c.stmt)) if err == nil && c.err { t.Errorf("%s must return an error but does not", c.stmt) } else if err != nil && !c.err { @@ -1382,12 +1382,12 @@ func TestSyntaxOperatorShift(t *testing.T) { } for _, c := range cases { - _, err := compileToIR([]byte(fmt.Sprintf(`package main + _, err := compileToIR(fmt.Appendf(nil, `package main func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { %s return dstPos -}`, c.stmt))) +}`, c.stmt)) if err == nil && c.err { t.Errorf("%s must return an error but does not", c.stmt) } else if err != nil && !c.err { @@ -1463,12 +1463,12 @@ func TestSyntaxOperatorShiftAssign(t *testing.T) { } for _, c := range cases { - _, err := compileToIR([]byte(fmt.Sprintf(`package main + _, err := compileToIR(fmt.Appendf(nil, `package main func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { %s return dstPos -}`, c.stmt))) +}`, c.stmt)) if err == nil && c.err { t.Errorf("%s must return an error but does not", c.stmt) } else if err != nil && !c.err { @@ -1585,12 +1585,12 @@ func TestSyntaxOperatorMultiplyAssign(t *testing.T) { } for _, c := range cases { - _, err := compileToIR([]byte(fmt.Sprintf(`package main + _, err := compileToIR(fmt.Appendf(nil, `package main func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { %s return dstPos -}`, c.stmt))) +}`, c.stmt)) if err == nil && c.err { t.Errorf("%s must return an error but does not", c.stmt) } else if err != nil && !c.err { @@ -1691,12 +1691,12 @@ func TestSyntaxBitwiseOperatorAssign(t *testing.T) { } for _, c := range cases { - _, err := compileToIR([]byte(fmt.Sprintf(`package main + _, err := compileToIR(fmt.Appendf(nil, `package main func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { %s return dstPos -}`, c.stmt))) +}`, c.stmt)) if err == nil && c.err { t.Errorf("%s must return an error but does not", c.stmt) } else if err != nil && !c.err { @@ -1720,12 +1720,12 @@ func TestSyntaxAtan(t *testing.T) { } for _, c := range cases { - _, err := compileToIR([]byte(fmt.Sprintf(`package main + _, err := compileToIR(fmt.Appendf(nil, `package main func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { %s return dstPos -}`, c.stmt))) +}`, c.stmt)) if err == nil && c.err { t.Errorf("%s must return an error but does not", c.stmt) } else if err != nil && !c.err { @@ -2187,12 +2187,12 @@ func TestSyntaxConstructorFuncType(t *testing.T) { } for _, c := range cases { - _, err := compileToIR([]byte(fmt.Sprintf(`package main + _, err := compileToIR(fmt.Appendf(nil, `package main func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { %s return dstPos -}`, c.stmt))) +}`, c.stmt)) if err == nil && c.err { t.Errorf("%s must return an error but does not", c.stmt) } else if err != nil && !c.err { diff --git a/internal/testing/shader.go b/internal/testing/shader.go index 5c6ae3f7c..558be2aa0 100644 --- a/internal/testing/shader.go +++ b/internal/testing/shader.go @@ -24,14 +24,14 @@ import ( // ShaderProgramFill returns a shader source to fill the frambuffer. func ShaderProgramFill(r, g, b, a byte) *shaderir.Program { - ir, err := graphics.CompileShader([]byte(fmt.Sprintf(`//kage:unit pixels + ir, err := graphics.CompileShader(fmt.Appendf(nil, `//kage:unit pixels package main func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { return vec4(%0.9f, %0.9f, %0.9f, %0.9f) } -`, float64(r)/0xff, float64(g)/0xff, float64(b)/0xff, float64(a)/0xff))) +`, float64(r)/0xff, float64(g)/0xff, float64(b)/0xff, float64(a)/0xff)) if err != nil { panic(err) } @@ -45,18 +45,18 @@ func ShaderProgramImages(numImages int) *shaderir.Program { } var exprs []string - for i := 0; i < numImages; i++ { + for i := range numImages { exprs = append(exprs, fmt.Sprintf("imageSrc%dUnsafeAt(srcPos)", i)) } - ir, err := graphics.CompileShader([]byte(fmt.Sprintf(`//kage:unit pixels + ir, err := graphics.CompileShader(fmt.Appendf(nil, `//kage:unit pixels package main func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { return %s } -`, strings.Join(exprs, " + ")))) +`, strings.Join(exprs, " + "))) if err != nil { panic(err) } diff --git a/internal/ui/monitor_glfw.go b/internal/ui/monitor_glfw.go index 3b4543ea1..ed3f78871 100644 --- a/internal/ui/monitor_glfw.go +++ b/internal/ui/monitor_glfw.go @@ -142,7 +142,7 @@ func (m *monitors) update() error { // Keep calling GetContentScale until the returned scale is 0 (#2051). // Retry this at most 5 times to avoid an infinite loop. - for i := 0; i < 5; i++ { + for range 5 { // An error can happen e.g. when entering a screensaver on Windows (#2488). sx, _, err := m.GetContentScale() if err != nil { diff --git a/internal/ui/shader.go b/internal/ui/shader.go index fdd0d157c..ab3a2c8c1 100644 --- a/internal/ui/shader.go +++ b/internal/ui/shader.go @@ -101,7 +101,7 @@ func (s *Shader) AppendUniforms(dst []uint32, uniforms map[string]any) []uint32 } switch t.Elem().Kind() { case reflect.Bool: - for i := 0; i < l; i++ { + for i := range l { if v.Index(i).Bool() { dst[idx+i] = 1 } else { @@ -109,15 +109,15 @@ func (s *Shader) AppendUniforms(dst []uint32, uniforms map[string]any) []uint32 } } case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - for i := 0; i < l; i++ { + for i := range l { dst[idx+i] = uint32(v.Index(i).Int()) } case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: - for i := 0; i < l; i++ { + for i := range l { dst[idx+i] = uint32(v.Index(i).Uint()) } case reflect.Float32, reflect.Float64: - for i := 0; i < l; i++ { + for i := range l { dst[idx+i] = math.Float32bits(float32(v.Index(i).Float())) } default: diff --git a/internal/ui/ui_darwin.go b/internal/ui/ui_darwin.go index ee73c8c5b..82fc7e729 100644 --- a/internal/ui/ui_darwin.go +++ b/internal/ui/ui_darwin.go @@ -56,12 +56,12 @@ func (u *UserInterface) initializePlatform() error { []objc.FieldDef{ { Name: "origDelegate", - Type: reflect.TypeOf(objc.ID(0)), + Type: reflect.TypeFor[objc.ID](), Attribute: objc.ReadWrite, }, { Name: "origResizable", - Type: reflect.TypeOf(true), + Type: reflect.TypeFor[bool](), Attribute: objc.ReadWrite, }, }, diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index bd62f1cf6..7b81062ee 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -326,7 +326,7 @@ func (u *UserInterface) setWindowMonitor(monitor *Monitor) error { // Just after exiting fullscreen, the window state seems very unstable (#2758). // Wait for a while with polling events. if runtime.GOOS == "darwin" { - for i := 0; i < 60; i++ { + for range 60 { if err := glfw.PollEvents(); err != nil { return err } diff --git a/internal/vettools/imageimportcheck.go b/internal/vettools/imageimportcheck.go index fbc1ae7b1..fbd6a3483 100644 --- a/internal/vettools/imageimportcheck.go +++ b/internal/vettools/imageimportcheck.go @@ -31,7 +31,7 @@ var imageImportCheckAnalyzer = &analysis.Analyzer{ Name: "imageimportcheck", Doc: "check importing image/gif, image/jpeg, and image/png packages", Run: runImageImportCheck, - ResultType: reflect.TypeOf(imageImportCheckResult{}), + ResultType: reflect.TypeFor[imageImportCheckResult](), } type imageImportCheckResult struct { diff --git a/shader_test.go b/shader_test.go index 59c05ad82..bb83442db 100644 --- a/shader_test.go +++ b/shader_test.go @@ -43,8 +43,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawRectShader(w/2, h/2, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) var want color.RGBA if i < w/2 && j < h/2 { @@ -78,8 +78,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { op.Images[0] = src dst.DrawRectShader(w/2, h/2, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) var want color.RGBA if i < w/2 && j < h/2 { @@ -130,14 +130,14 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { // Demonstrate the bug with a write to src1, which will actually end up on src0. // Validated later. var buf []byte - for i := 0; i < w*h; i++ { + for range w * h { buf = append(buf, 2, 5, 2, 5) } src1.WritePixels(buf) // Verify that src1 was copied to dst. - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0xff, A: 0xff} if got != want { @@ -153,8 +153,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawRectShader(w, h, s, op) // Verify that src0 was copied to dst and not overwritten above. - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 25, G: 0xff, B: 25, A: 0xff} if got != want { @@ -230,8 +230,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawTrianglesShader(vs, is, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0xff, A: 0xff} if got != want { @@ -263,8 +263,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawRectShader(w, h, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0xff, A: 0xff} if got != want { @@ -294,8 +294,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawRectShader(w, h, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) var want color.RGBA if got != want { @@ -335,8 +335,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { op.Images[0] = src dst.DrawRectShader(w, h, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 87, G: 82, B: 71, A: 255} if got != want { @@ -365,8 +365,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { src0 := ebiten.NewImage(w, h) pix0 := make([]byte, 4*w*h) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { if 2 <= i && i < 10 && 3 <= j && j < 11 { pix0[4*(j*w+i)] = 0xff pix0[4*(j*w+i)+1] = 0 @@ -380,8 +380,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { src1 := ebiten.NewImage(w, h) pix1 := make([]byte, 4*w*h) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { if 6 <= i && i < 14 && 8 <= j && j < 16 { pix1[4*(j*w+i)] = 0 pix1[4*(j*w+i)+1] = 0xff @@ -394,8 +394,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { src1 = src1.SubImage(image.Rect(6, 8, 14, 16)).(*ebiten.Image) testPixels := func(testname string, dst *ebiten.Image) { - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) var want color.RGBA if i < w/2 && j < h/2 { @@ -493,8 +493,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst := ebiten.NewImage(w, h) src := ebiten.NewImage(w, h) pix := make([]byte, 4*w*h) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { if i < w/2 { pix[4*(j*w+i)] = 0xff } @@ -555,8 +555,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst := ebiten.NewImage(w, h) src := ebiten.NewImage(w, h) pix := make([]byte, 4*w*h) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { if i < w/2 { pix[4*(j*w+i)] = 0xff } @@ -687,8 +687,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawRectShader(w/2, h/2, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) var want color.RGBA if i < w/2 && j < h/2 { @@ -724,8 +724,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { op.Images[0] = src dst.DrawRectShader(w, h, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0x20, G: 0x40, B: 0x60, A: 0xff} if !sameColors(got, want, 2) { @@ -755,8 +755,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawRectShader(w, h, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0x40, B: 0x40, A: 0xff} if !sameColors(got, want, 2) { @@ -793,8 +793,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { op.Images[0] = src dst.DrawRectShader(w, h, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0x10, G: 0x20, B: 0x30, A: 0xff} if !sameColors(got, want, 2) { @@ -829,8 +829,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { op.Images[0] = src dst.DrawRectShader(w, h, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) v := byte(math.Floor(0xff * math.Pi / 4)) want := color.RGBA{R: v, G: v, B: v, A: v} @@ -870,8 +870,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { } dst.DrawRectShader(w, h, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 8, G: 12, B: 0xff, A: 0xff} if !sameColors(got, want, 2) { @@ -912,8 +912,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { } dst.DrawRectShader(w, h, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 54, G: 80, B: 0xff, A: 0xff} if !sameColors(got, want, 2) { @@ -953,8 +953,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { } dst.DrawRectShader(w, h, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 24, G: 30, B: 36, A: 0xff} if !sameColors(got, want, 2) { @@ -997,8 +997,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { } dst.DrawRectShader(w, h, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 6, G: 8, B: 9, A: 0xff} if !sameColors(got, want, 2) { @@ -1039,8 +1039,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { } dst.DrawRectShader(w, h, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 112, G: 128, B: 143, A: 159} if !sameColors(got, want, 2) { @@ -1085,8 +1085,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { } dst.DrawRectShader(w, h, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 44, G: 50, B: 56, A: 62} if !sameColors(got, want, 2) { @@ -1125,8 +1125,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { } dst.DrawRectShader(w, h, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 7, G: 7, B: 7, A: 7} if !sameColors(got, want, 2) { @@ -1156,8 +1156,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { const offset0 = -4 src0 := ebiten.NewImageWithOptions(image.Rect(offset0, offset0, w+offset0, h+offset0), nil) pix0 := make([]byte, 4*w*h) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { if 2 <= i && i < 10 && 3 <= j && j < 11 { pix0[4*(j*w+i)] = 0xff pix0[4*(j*w+i)+1] = 0 @@ -1172,8 +1172,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { const offset1 = -6 src1 := ebiten.NewImageWithOptions(image.Rect(offset1, offset1, w+offset1, h+offset1), nil) pix1 := make([]byte, 4*w*h) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { if 6 <= i && i < 14 && 8 <= j && j < 16 { pix1[4*(j*w+i)] = 0 pix1[4*(j*w+i)+1] = 0xff @@ -1289,8 +1289,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawRectShader(w, h, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0xff, A: 0xff} if !sameColors(got, want, 2) { @@ -1309,8 +1309,8 @@ func TestShaderDiscard(t *testing.T) { src := ebiten.NewImage(w, h) pix := make([]byte, 4*w*h) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { if i >= w/2 || j >= h/2 { continue } @@ -1340,8 +1340,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { op.Images[0] = src dst.DrawRectShader(w, h, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{G: 0xff, A: 0xff} if i >= w/2 || j >= h/2 { @@ -1392,8 +1392,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { op.GeoM.Translate(offsetX, offsetY) op.Images[0] = src dst.DrawRectShader(srcW, srcH, s, op) - for j := 0; j < dstH; j++ { - for i := 0; i < dstW; i++ { + for j := range dstH { + for i := range dstW { got := dst.At(i, j).(color.RGBA) var want color.RGBA if offsetX <= i && i < offsetX+srcW && offsetY <= j && j < offsetY+srcH { @@ -1434,8 +1434,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { op.ColorScale.ScaleWithColor(color.RGBA{R: 0x40, G: 0x80, B: 0xc0, A: 0xff}) dst.DrawRectShader(w, h, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0x20, G: 0x50, B: 0x90, A: 0xe0} if !sameColors(got, want, 1) { @@ -1754,7 +1754,7 @@ func TestShaderTexelAndPixel(t *testing.T) { dstPixel := ebiten.NewImage(dstW, dstH) src := ebiten.NewImage(srcW, srcH) - shaderTexel, err := ebiten.NewShader([]byte(fmt.Sprintf(`//kage:unit texels + shaderTexel, err := ebiten.NewShader(fmt.Appendf(nil, `//kage:unit texels package main @@ -1764,7 +1764,7 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { pos /= 255 return vec4(pos.x, pos.y, 0, 1) } -`, srcW, srcH))) +`, srcW, srcH)) if err != nil { t.Fatal(err) } @@ -1787,8 +1787,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dstTexel.DrawRectShader(src.Bounds().Dx(), src.Bounds().Dy(), shaderTexel, op) dstPixel.DrawRectShader(src.Bounds().Dx(), src.Bounds().Dy(), shaderPixel, op) - for j := 0; j < dstH; j++ { - for i := 0; i < dstW; i++ { + for j := range dstH { + for i := range dstW { c0 := dstTexel.At(i, j).(color.RGBA) c1 := dstPixel.At(i, j).(color.RGBA) if !sameColors(c0, c1, 1) { @@ -1814,14 +1814,14 @@ func TestShaderDifferentTextureSizes(t *testing.T) { for _, unit := range []string{"texels", "pixels"} { t.Run(fmt.Sprintf("unit %s", unit), func(t *testing.T) { - shader, err := ebiten.NewShader([]byte(fmt.Sprintf(`//kage:unit %s + shader, err := ebiten.NewShader(fmt.Appendf(nil, `//kage:unit %s package main func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { return imageSrc0At(srcPos) + imageSrc1At(srcPos) } -`, unit))) +`, unit)) if err != nil { t.Fatal(err) } @@ -1835,8 +1835,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { op.Images[1] = src1 dst.DrawRectShader(2, 3, shader, op) - for j := 0; j < 3; j++ { - for i := 0; i < 2; i++ { + for j := range 3 { + for i := range 2 { got := dst.At(i, j).(color.RGBA) want := color.RGBA{0x40, 0x40, 0x40, 0xff} if !sameColors(got, want, 1) { @@ -1854,8 +1854,8 @@ func TestShaderIVec(t *testing.T) { src := ebiten.NewImage(w, h) pix := make([]byte, 4*w*h) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { pix[4*(j*w+i)] = byte(i) pix[4*(j*w+i)+1] = byte(j) pix[4*(j*w+i)+3] = 0xff @@ -2020,8 +2020,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { } dst.DrawRectShader(w, h, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{0xff, 0xff, 0xff, 0xff} if got != want { @@ -2035,8 +2035,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { op.Uniforms = nil dst.DrawRectShader(w, h, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{0, 0, 0, 0} if got != want { @@ -2058,7 +2058,7 @@ func TestShaderDrawRectWithoutSource(t *testing.T) { src := ebiten.NewImage(srcW, srcH) for _, unit := range []string{"pixels", "texels"} { - s, err := ebiten.NewShader([]byte(fmt.Sprintf(`//kage:unit %s + s, err := ebiten.NewShader(fmt.Appendf(nil, `//kage:unit %s package main @@ -2086,7 +2086,7 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { } return vec4(0, 1, 0, 1) } -`, unit))) +`, unit)) if err != nil { t.Fatal(err) } @@ -2108,8 +2108,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { op.Images[0] = src } dst.DrawRectShader(srcW, srcH, s, op) - for j := 0; j < dstH; j++ { - for i := 0; i < dstW; i++ { + for j := range dstH { + for i := range dstW { got := dst.At(i, j).(color.RGBA) var want color.RGBA if offsetX <= i && i < offsetX+srcW && offsetY <= j && j < offsetY+srcH { @@ -2158,8 +2158,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { op.Images[0] = src dst.DrawRectShader(w, h, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0x18, G: 0x30, B: 0x48, A: 0xff} if !sameColors(got, want, 2) { @@ -2192,14 +2192,14 @@ func TestShaderDifferentSourceSizes(t *testing.T) { } }() } - shader, err := ebiten.NewShader([]byte(fmt.Sprintf(`//kage:unit %s + shader, err := ebiten.NewShader(fmt.Appendf(nil, `//kage:unit %s package main func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { return imageSrc0At(srcPos) + imageSrc1At(srcPos) } -`, unit))) +`, unit)) if err != nil { t.Fatal(err) } @@ -2260,8 +2260,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { t.Fatal("not reached") } - for j := 0; j < 4; j++ { - for i := 0; i < 3; i++ { + for j := range 4 { + for i := range 3 { got := dst.At(i, j).(color.RGBA) var want color.RGBA if i < 2 && j < 3 { @@ -2302,7 +2302,7 @@ func TestShaderBitwiseOperator(t *testing.T) { v.rgb = v.rgb ^ 0x8d` } - s, err := ebiten.NewShader([]byte(fmt.Sprintf(`//kage:unit pixels + s, err := ebiten.NewShader(fmt.Appendf(nil, `//kage:unit pixels package main @@ -2311,7 +2311,7 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { %s return vec4(v) / 0xff; } -`, code))) +`, code)) if err != nil { t.Fatal(err) } @@ -2321,8 +2321,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { op.Images[0] = src dst.DrawRectShader(w, h, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0xbd, G: 0xb7, B: 0xf7, A: 0xff} if !sameColors(got, want, 2) { @@ -2352,8 +2352,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawRectShader(w/2, h/2, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) var want color.RGBA if i < w/2 && j < h/2 { @@ -2396,8 +2396,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawRectShader(w/2, h/2, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) var want color.RGBA if i < w/2 && j < h/2 { @@ -2415,8 +2415,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.Clear() dst.DrawRectShader(w/2, h/2, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) var want color.RGBA if i < w/2 && j < h/2 { @@ -2453,8 +2453,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawRectShader(w, h, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0x40, G: 0x80, B: 0xc0, A: 0xff} if !sameColors(got, want, 2) { @@ -2486,8 +2486,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawRectShader(w, h, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0xff, G: 0xc0, B: 0x80, A: 0x40} if !sameColors(got, want, 2) { @@ -2511,8 +2511,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawRectShader(w, h, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0xff, G: 0xc0, B: 0x80, A: 0} if !sameColors(got, want, 2) { @@ -2549,8 +2549,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawRectShader(w, h, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0xff, G: 0x80, B: 0xff, A: 0xc0} if !sameColors(got, want, 2) { @@ -2583,8 +2583,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawRectShader(w, h, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0x00, G: 0x80, B: 0x00, A: 0x40} if !sameColors(got, want, 2) { @@ -2619,8 +2619,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawRectShader(w, h, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff} if !sameColors(got, want, 2) { @@ -2706,8 +2706,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4, custom vec4) vec4 { }, }, []uint16{0, 1, 2, 1, 2, 3}, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := clr if !sameColors(got, want, 2) { @@ -2800,8 +2800,8 @@ func Fragment(dstPos vec4, srcPos vec2) vec4 { }, }, []uint16{0, 1, 2, 1, 2, 3}, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) var want color.RGBA switch idx { @@ -2842,8 +2842,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawRectShader(w, h, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0x00, G: 0x00, B: 0x00, A: 0xff} if !sameColors(got, want, 2) { @@ -2883,8 +2883,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawRectShader(w, h, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0x80, G: 0x40, B: 0xc0, A: 0xff} if !sameColors(got, want, 2) { @@ -2912,8 +2912,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawRectShader(w, h, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0x40, G: 0x40, B: 0xc0, A: 0xc0} if !sameColors(got, want, 2) { @@ -2941,8 +2941,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawRectShader(w, h, s, nil) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0x40, G: 0xc0, B: 0xc0, A: 0xff} if !sameColors(got, want, 2) { @@ -2993,8 +2993,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { } dst.DrawRectShader(w, h, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0xff, G: 0, B: 0xff, A: 0xff} if !sameColors(got, want, 2) { @@ -3046,8 +3046,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { dst.DrawTrianglesShader32(vs, []uint32{0, 1, 2, 1, 2, 3}, s, op) dst.DrawTrianglesShader32(vs, []uint32{2, 1, 0, 3, 2, 1}, s, op) - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := dst.At(i, j).(color.RGBA) want := color.RGBA{R: 0x80, G: 0x80, B: 0x00, A: 0xff} if !sameColors(got, want, 2) { diff --git a/text/text.go b/text/text.go index 140ee5d6d..fd77abbcd 100644 --- a/text/text.go +++ b/text/text.go @@ -366,7 +366,7 @@ func CacheGlyphs(face font.Face, text string) { b, a, _ := fc.GlyphBounds(r) // Cache all 4 variations for one rune (#2528). - for i := 0; i < 4; i++ { + for i := range 4 { offset := fixed.Point26_6{ X: (fixed.Int26_6(i*(1<<4)) + b.Min.X) & ((1 << 6) - 1), Y: b.Min.Y & ((1 << 6) - 1), diff --git a/text/text_test.go b/text/text_test.go index 4e7ec1cf4..bdc4db2b2 100644 --- a/text/text_test.go +++ b/text/text_test.go @@ -39,8 +39,8 @@ func TestTextColor(t *testing.T) { w, h := img.Bounds().Dx(), img.Bounds().Dy() allTransparent := true - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := img.At(i, j) want1 := color.RGBA{R: 0x80, G: 0x80, B: 0x80, A: 0x80} want2 := color.RGBA{} @@ -125,8 +125,8 @@ func TestNegativeKern(t *testing.T) { // With testFace, 'b' is rendered at the previous position as 0xff. // 'a' is rendered at the current position as 0x80. text.Draw(dst, "ab", f, 0, 0, color.White) - for j := 0; j < testFaceSize; j++ { - for i := 0; i < testFaceSize; i++ { + for j := range testFaceSize { + for i := range testFaceSize { got := dst.At(i, j) want := color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff} if got != want { @@ -137,7 +137,7 @@ func TestNegativeKern(t *testing.T) { // The glyph 'a' should be treated correctly. text.Draw(dst, "a", f, testFaceSize, 0, color.White) - for j := 0; j < testFaceSize; j++ { + for j := range testFaceSize { for i := testFaceSize; i < testFaceSize*2; i++ { got := dst.At(i, j) want := color.RGBA{R: 0x80, G: 0x80, B: 0x80, A: 0x80} diff --git a/text/v2/text.go b/text/v2/text.go index 88488d5ee..1f522ccb8 100644 --- a/text/v2/text.go +++ b/text/v2/text.go @@ -257,7 +257,7 @@ func CacheGlyphs(text string, face Face) { var buf []Glyph // Create all the possible variations (#2528). - for i := 0; i < c; i++ { + for range c { buf = appendGlyphs(buf, text, face, x, y, nil) buf = buf[:0] diff --git a/text/v2/text_test.go b/text/v2/text_test.go index 6d46a960f..36a25de6b 100644 --- a/text/v2/text_test.go +++ b/text/v2/text_test.go @@ -66,8 +66,8 @@ func TestTextColor(t *testing.T) { w, h := img.Bounds().Dx(), img.Bounds().Dy() allTransparent := true - for j := 0; j < h; j++ { - for i := 0; i < w; i++ { + for j := range h { + for i := range w { got := img.At(i, j) want1 := color.RGBA{R: 0x80, G: 0x80, B: 0x80, A: 0x80} want2 := color.RGBA{} @@ -154,8 +154,8 @@ func TestNegativeKern(t *testing.T) { op := &text.DrawOptions{} op.GeoM.Translate(0, 0) text.Draw(dst, "ab", f, op) - for j := 0; j < testGoXFaceSize; j++ { - for i := 0; i < testGoXFaceSize; i++ { + for j := range testGoXFaceSize { + for i := range testGoXFaceSize { got := dst.At(i, j) want := color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff} if got != want { @@ -168,7 +168,7 @@ func TestNegativeKern(t *testing.T) { op = &text.DrawOptions{} op.GeoM.Translate(testGoXFaceSize, 0) text.Draw(dst, "a", f, op) - for j := 0; j < testGoXFaceSize; j++ { + for j := range testGoXFaceSize { for i := testGoXFaceSize; i < testGoXFaceSize*2; i++ { got := dst.At(i, j) want := color.RGBA{R: 0x80, G: 0x80, B: 0x80, A: 0x80} @@ -234,8 +234,8 @@ func TestUnhashableFace(t *testing.T) { dst := ebiten.NewImage(unhashableGoXFaceSize*2, unhashableGoXFaceSize*2) text.Draw(dst, "a", f, nil) - for j := 0; j < unhashableGoXFaceSize*2; j++ { - for i := 0; i < unhashableGoXFaceSize*2; i++ { + for j := range unhashableGoXFaceSize * 2 { + for i := range unhashableGoXFaceSize * 2 { got := dst.At(i, j) var want color.RGBA if i < unhashableGoXFaceSize && j < unhashableGoXFaceSize { diff --git a/vector/path_test.go b/vector/path_test.go index c3e844142..ef069d3c3 100644 --- a/vector/path_test.go +++ b/vector/path_test.go @@ -283,8 +283,8 @@ func TestArcAndGeoM(t *testing.T) { vector.StrokePath(origDst, &path, strokeOp, nil) vector.StrokePath(refDst, &tc.refPath, strokeOp, nil) - for j := 0; j < 16; j++ { - for i := 0; i < 16; i++ { + for j := range 16 { + for i := range 16 { got := origDst.At(i, j) want := refDst.At(i, j) if got != want { @@ -346,8 +346,7 @@ func TestRaceConditionWithSubImage(t *testing.T) { var wg sync.WaitGroup for i := range h { for j := range w { - wg.Add(1) - go func() { + wg.Go(func() { subImg := src.SubImage(image.Rect(i, j, i+1, j+1)).(*ebiten.Image) var p vector.Path p.MoveTo(0, 0) @@ -361,8 +360,7 @@ func TestRaceConditionWithSubImage(t *testing.T) { vector.FillPath(subImg, &p, nil, op) dst := ebiten.NewImage(w, h) dst.DrawImage(subImg, nil) - wg.Done() - }() + }) } } wg.Wait()