From aa68d8e2aa0698da5964b62d05102158d7527083 Mon Sep 17 00:00:00 2001 From: Jacek Olszak Date: Sun, 13 Aug 2023 08:24:36 +0200 Subject: [PATCH] Rename pi.Pset and pi.Pget to pi.Set and pi.Get. Remove pi.Sset and pi.Sget pi.Sset and pi.Sget is used very rarely. And programmer could run pi.SprSheet().Set() instead. The effect will be the same. pi.Pset and pi.Pget have misleading names. They should propably have names using 2 capital letters eg. PSet. After pi.Sset is removed, then I can safely use pi.Set which is great name to remember. --- devtools/internal/icons/icons.go | 2 +- devtools/internal/inspector/draw.go | 2 +- devtools/internal/inspector/pset.go | 16 ++--- devtools/internal/inspector/tool.go | 4 +- devtools/internal/inspector/toolbar.go | 2 +- .../internal/lib/github_com-elgopher-pi.go | 6 +- docs/ROADMAP.md | 2 +- examples/controller/main.go | 2 +- examples/trigonometry/main.go | 4 +- internal/bench/screen_bench_test.go | 12 ++-- internal/bench/sprite_sheet_bench_test.go | 26 ------- palette.go | 2 +- pi_test.go | 4 +- screen.go | 8 +-- screen_test.go | 22 +++--- snap/snap_test.go | 2 +- sprite_sheet.go | 10 --- sprite_sheet_test.go | 69 ------------------- 18 files changed, 44 insertions(+), 151 deletions(-) delete mode 100644 internal/bench/sprite_sheet_bench_test.go diff --git a/devtools/internal/icons/icons.go b/devtools/internal/icons/icons.go index 9512428..ae8037b 100644 --- a/devtools/internal/icons/icons.go +++ b/devtools/internal/icons/icons.go @@ -14,7 +14,7 @@ import ( const ( Pointer = 0 MeasureTool = 1 - PsetTool = 2 + SetTool = 2 LineTool = 3 RectTool = 4 RectFillTool = 5 diff --git a/devtools/internal/inspector/draw.go b/devtools/internal/inspector/draw.go index 89f2b9b..0e32cc6 100644 --- a/devtools/internal/inspector/draw.go +++ b/devtools/internal/inspector/draw.go @@ -18,7 +18,7 @@ var pixelColorAtMouseCoords byte func Draw() { snapshot.Draw() - pixelColorAtMouseCoords = pi.Pget(pi.MousePos.X, pi.MousePos.Y) + pixelColorAtMouseCoords = pi.Get(pi.MousePos.X, pi.MousePos.Y) handleScreenshot() moveBarIfNeeded() diff --git a/devtools/internal/inspector/pset.go b/devtools/internal/inspector/pset.go index d60a392..aeacea1 100644 --- a/devtools/internal/inspector/pset.go +++ b/devtools/internal/inspector/pset.go @@ -11,27 +11,27 @@ import ( "github.com/elgopher/pi/devtools/internal/snapshot" ) -type Pset struct { +type Set struct { running bool } -func (p *Pset) Update() { +func (p *Set) Update() { x, y := pi.MousePos.X, pi.MousePos.Y leftp := pi.MouseBtnp(pi.MouseLeft) && !p.running left := pi.MouseBtn(pi.MouseLeft) && p.running if (leftp || left) && pixelColorAtMouseCoords != FgColor { p.running = true snapshot.Draw() - pi.Pset(x, y, FgColor) - fmt.Printf("pi.Pset(%d, %d, %d)\n", x, y, FgColor) + pi.Set(x, y, FgColor) + fmt.Printf("pi.Set(%d, %d, %d)\n", x, y, FgColor) snapshot.Take() } } -func (p *Pset) Draw() { - pi.Pset(pi.MousePos.X, pi.MousePos.Y, FgColor) +func (p *Set) Draw() { + pi.Set(pi.MousePos.X, pi.MousePos.Y, FgColor) } -func (p *Pset) Icon() byte { - return icons.PsetTool +func (p *Set) Icon() byte { + return icons.SetTool } diff --git a/devtools/internal/inspector/tool.go b/devtools/internal/inspector/tool.go index db69c67..d8dbae1 100644 --- a/devtools/internal/inspector/tool.go +++ b/devtools/internal/inspector/tool.go @@ -23,8 +23,8 @@ func selectTool(icon byte) { switch icon { case icons.MeasureTool: tool = &Measure{} - case icons.PsetTool: - tool = &Pset{} + case icons.SetTool: + tool = &Set{} case icons.LineTool: tool = &Shape{ draw: drawShape("Line", pi.Line), diff --git a/devtools/internal/inspector/toolbar.go b/devtools/internal/inspector/toolbar.go index ae282ac..c5865c7 100644 --- a/devtools/internal/inspector/toolbar.go +++ b/devtools/internal/inspector/toolbar.go @@ -73,7 +73,7 @@ func (t *Toolbar) draw() { y := t.pos.Y pi.RectFill(x, y, x+toolbarWidth, y+toolbarHeight, BgColor) icons.Draw(x+1, y+1, FgColor, - icons.MeasureTool, icons.PsetTool, icons.LineTool, icons.RectTool, icons.RectFillTool, icons.CircTool, icons.CircFillTool) + icons.MeasureTool, icons.SetTool, icons.LineTool, icons.RectTool, icons.RectFillTool, icons.CircTool, icons.CircFillTool) if t.toolHighlighted > 0 { toolX := x + int((t.toolHighlighted-1)*4) diff --git a/devtools/internal/lib/github_com-elgopher-pi.go b/devtools/internal/lib/github_com-elgopher-pi.go index 52c53c0..16b336e 100644 --- a/devtools/internal/lib/github_com-elgopher-pi.go +++ b/devtools/internal/lib/github_com-elgopher-pi.go @@ -54,9 +54,9 @@ func init() { "NewPixMapWithPixels": reflect.ValueOf(pi.NewPixMapWithPixels), "O": reflect.ValueOf(pi.O), "Palette": reflect.ValueOf(&pi.Palette).Elem(), - "Pget": reflect.ValueOf(pi.Pget), + "Get": reflect.ValueOf(pi.Get), "Print": reflect.ValueOf(pi.Print), - "Pset": reflect.ValueOf(pi.Pset), + "Set": reflect.ValueOf(pi.Set), "Rect": reflect.ValueOf(pi.Rect), "RectFill": reflect.ValueOf(pi.RectFill), "Reset": reflect.ValueOf(pi.Reset), @@ -67,7 +67,6 @@ func init() { "SetCustomFontSpecialWidth": reflect.ValueOf(pi.SetCustomFontSpecialWidth), "SetCustomFontWidth": reflect.ValueOf(pi.SetCustomFontWidth), "SetScreenSize": reflect.ValueOf(pi.SetScreenSize), - "Sget": reflect.ValueOf(pi.Sget), "Sin": reflect.ValueOf(pi.Sin), "Spr": reflect.ValueOf(pi.Spr), "SprSheet": reflect.ValueOf(pi.SprSheet), @@ -75,7 +74,6 @@ func init() { "SprSizeFlip": reflect.ValueOf(pi.SprSizeFlip), "SpriteHeight": reflect.ValueOf(constant.MakeFromLiteral("8", token.INT, 0)), "SpriteWidth": reflect.ValueOf(constant.MakeFromLiteral("8", token.INT, 0)), - "Sset": reflect.ValueOf(pi.Sset), "Stop": reflect.ValueOf(pi.Stop), "SystemFont": reflect.ValueOf(pi.SystemFont), "Time": reflect.ValueOf(&pi.Time).Elem(), diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 51f4883..e6dd083 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -40,7 +40,7 @@ * [x] presenting pixel coords and color * [ ] zoom-in, zoom-out * [ ] drawing on the screen using Pi functions - * [x] Pset, shapes + * [x] Set, shapes * [ ] Spr, Print * [x] **scripting/REPL** - write Go code **live** when the game is running * [ ] palette inspector diff --git a/examples/controller/main.go b/examples/controller/main.go index 8f789d9..24fc713 100644 --- a/examples/controller/main.go +++ b/examples/controller/main.go @@ -76,6 +76,6 @@ func drawPlayerController(player, x, y int) { func drawPlayerNumber(x int, y int, player int) { pi.Pal.Reset() for i := 0; i <= player; i++ { - pi.Pset(x+50-i*2, y+8, yellow) + pi.Set(x+50-i*2, y+8, yellow) } } diff --git a/examples/trigonometry/main.go b/examples/trigonometry/main.go index 4f11a10..1cae4cf 100644 --- a/examples/trigonometry/main.go +++ b/examples/trigonometry/main.go @@ -30,12 +30,12 @@ func draw(line int, color byte, f func(x float64) float64) { for x := 0.0; x < 128; x++ { angle := (x + start) / 128 dy := math.Round(f(angle) * 16) - pi.Pset(int(x), line+int(dy), color) + pi.Set(int(x), line+int(dy), color) } } func drawHorizontalAxis(line int) { for x := 0; x < 128; x++ { - pi.Pset(x, line, 1) + pi.Set(x, line, 1) } } diff --git a/internal/bench/screen_bench_test.go b/internal/bench/screen_bench_test.go index 103a195..3653fea 100644 --- a/internal/bench/screen_bench_test.go +++ b/internal/bench/screen_bench_test.go @@ -24,20 +24,20 @@ func BenchmarkClsCol(b *testing.B) { }) } -func BenchmarkPset(b *testing.B) { +func BenchmarkSet(b *testing.B) { runBenchmarks(b, func(res Resolution) { - for i := 0; i < 1000; i++ { // Pset is too fast - pi.Pset(2, 2, color) + for i := 0; i < 1000; i++ { // Set is too fast + pi.Set(2, 2, color) } }) } var sink byte -func BenchmarkPget(b *testing.B) { +func BenchmarkPGet(b *testing.B) { runBenchmarks(b, func(res Resolution) { - for i := 0; i < 1000; i++ { // Pget is too fast - sink = pi.Pget(2, 2) + for i := 0; i < 1000; i++ { // Get is too fast + sink = pi.Get(2, 2) } }) } diff --git a/internal/bench/sprite_sheet_bench_test.go b/internal/bench/sprite_sheet_bench_test.go deleted file mode 100644 index 8390515..0000000 --- a/internal/bench/sprite_sheet_bench_test.go +++ /dev/null @@ -1,26 +0,0 @@ -// (c) 2022 Jacek Olszak -// This code is licensed under MIT license (see LICENSE for details) - -package bench_test - -import ( - "testing" - - "github.com/elgopher/pi" -) - -func BenchmarkSset(b *testing.B) { - runBenchmarks(b, func(res Resolution) { - for i := 0; i < 1000; i++ { // Sset is too fast - pi.Sset(2, 2, color) - } - }) -} - -func BenchmarkSget(b *testing.B) { - runBenchmarks(b, func(res Resolution) { - for i := 0; i < 1000; i++ { // Sget is too fast - sink = pi.Sget(2, 2) - } - }) -} diff --git a/palette.go b/palette.go index 306687e..a75b7ce 100644 --- a/palette.go +++ b/palette.go @@ -8,7 +8,7 @@ var ( // Pal (draw palette) contains mapping of colors used to replace color with // another one for all subsequent drawings. // - // Affected functions are Pset, Spr, SprSize, SprSizeFlip, Circ, CircFill, Line, Rect and RectFill. + // Affected functions are Set, Spr, SprSize, SprSizeFlip, Circ, CircFill, Line, Rect and RectFill. // // The index of array is original color, the value is the color replacement: // To change color 7 to 0 write following code: diff --git a/pi_test.go b/pi_test.go index af2db70..7e51d86 100644 --- a/pi_test.go +++ b/pi_test.go @@ -208,8 +208,8 @@ func TestLoad(t *testing.T) { pi.Spr(4, 0, 0) // sprite-sheet.png has only 4 sprites (from 0 to 3) pi.SprSize(4, 0, 0, 1.0, 1.0) pi.SprSizeFlip(4, 0, 0, 1.0, 1.0, false, false) - pi.Pset(16, 16, color) // sprite-sheet.png is only 16x16 pixels (0..15) - pi.Pget(16, 16) + pi.Set(16, 16, color) // sprite-sheet.png is only 16x16 pixels (0..15) + pi.Get(16, 16) }) }) diff --git a/screen.go b/screen.go index 75c50d0..9cf46aa 100644 --- a/screen.go +++ b/screen.go @@ -17,13 +17,13 @@ func ClsCol(col byte) { screen.ClearCol(col) } -// Pset sets a pixel color on the screen. It takes into account camera and draw palette. -func Pset(x, y int, color byte) { +// Set sets a pixel color on the screen. It takes into account camera and draw palette. +func Set(x, y int, color byte) { screen.Set(x-Camera.X, y-Camera.Y, Pal[color]) } -// Pget gets a pixel color on the screen. -func Pget(x, y int) byte { +// Get gets a pixel color on the screen. +func Get(x, y int) byte { x -= Camera.X y -= Camera.Y diff --git a/screen_test.go b/screen_test.go index e7bf347..c5ddbf4 100644 --- a/screen_test.go +++ b/screen_test.go @@ -129,7 +129,7 @@ func TestPixMap_Set(t *testing.T) { }) } -func TestPset(t *testing.T) { +func TestSet(t *testing.T) { const col byte = 2 t.Run("should set pixel taking camera position into account", func(t *testing.T) { @@ -137,7 +137,7 @@ func TestPset(t *testing.T) { pi.SetScreenSize(2, 2) pi.Camera.Set(1, 2) // when - pi.Pset(1, 2, 8) + pi.Set(1, 2, 8) // then expected := make([]byte, 4) expected[0] = 8 @@ -167,7 +167,7 @@ func TestPset(t *testing.T) { pi.SetScreenSize(2, 2) // when pi.Camera.Set(1, 1) - pi.Pset(coords.X, coords.Y, col) + pi.Set(coords.X, coords.Y, col) // then assert.Equal(t, emptyScreen, pi.Scr().Pix()) }) @@ -179,7 +179,7 @@ func TestPset(t *testing.T) { pi.SetScreenSize(1, 1) pi.Pal[1] = 2 // when - pi.Pset(0, 0, 1) + pi.Set(0, 0, 1) // then assert.Equal(t, []byte{2}, pi.Scr().Pix()) }) @@ -190,7 +190,7 @@ func TestPset(t *testing.T) { pi.Pal[1] = 2 pi.Pal.Reset() // when - pi.Pset(0, 0, 1) + pi.Set(0, 0, 1) // then assert.Equal(t, []byte{1}, pi.Scr().Pix()) }) @@ -277,15 +277,15 @@ func TestPixMap_Get(t *testing.T) { }) } -func TestPget(t *testing.T) { +func TestPGet(t *testing.T) { t.Run("should get pixel taking camera position into consideration", func(t *testing.T) { pi.Reset() pi.SetScreenSize(2, 2) pi.Camera.Set(1, 2) const color byte = 8 - pi.Pset(1, 2, color) + pi.Set(1, 2, color) // when - actual := pi.Pget(1, 2) + actual := pi.Get(1, 2) // then assert.Equal(t, color, actual) }) @@ -313,7 +313,7 @@ func TestPget(t *testing.T) { pi.ClsCol(7) pi.Camera.Set(1, 1) // when - actual := pi.Pget(coords.X, coords.Y) + actual := pi.Get(coords.X, coords.Y) // then assert.Zero(t, actual) }) @@ -528,12 +528,12 @@ func testSpr(t *testing.T, spr func(spriteNo int, x int, y int)) { pi.SetScreenSize(8, 8) const originalColor byte = 7 const replacementColor byte = 15 - pi.Sset(5, 5, originalColor) + pi.SprSheet().Set(5, 5, originalColor) pi.Pal[originalColor] = replacementColor // when spr(0, 0, 0) // then - actual := pi.Pget(5, 5) + actual := pi.Get(5, 5) assert.Equal(t, replacementColor, actual) }) diff --git a/snap/snap_test.go b/snap/snap_test.go index be773a6..9b0068b 100644 --- a/snap/snap_test.go +++ b/snap/snap_test.go @@ -45,7 +45,7 @@ func TestSnap(t *testing.T) { pi.SetScreenSize(1, 1) original, replacement := byte(1), byte(2) pi.Pald[original] = replacement // replace 1 by 2 - pi.Pset(0, 0, original) + pi.Set(0, 0, original) screenshot, err := snap.Take() // then require.NoError(t, err) diff --git a/sprite_sheet.go b/sprite_sheet.go index a4d10ac..d6faae0 100644 --- a/sprite_sheet.go +++ b/sprite_sheet.go @@ -18,16 +18,6 @@ const ( var sprSheet = newSpriteSheet(defaultSpriteSheetWidth, defaultSpriteSheetHeight) -// Sset sets the pixel color on the sprite sheet. -func Sset(x, y int, color byte) { - sprSheet.Set(x, y, color) -} - -// Sget gets the pixel color on the sprite sheet. -func Sget(x, y int) byte { - return sprSheet.Get(x, y) -} - func loadSpriteSheet(resources fs.ReadFileFS) error { fileContents, err := resources.ReadFile("sprite-sheet.png") if errors.Is(err, fs.ErrNotExist) { diff --git a/sprite_sheet_test.go b/sprite_sheet_test.go index d4b931b..ccf19d6 100644 --- a/sprite_sheet_test.go +++ b/sprite_sheet_test.go @@ -4,7 +4,6 @@ package pi_test import ( - "fmt" "strconv" "testing" @@ -49,71 +48,3 @@ func TestUseEmptySpriteSheet(t *testing.T) { assert.Equal(t, expectedSpriteSheetData, pi.SprSheet().Pix()) }) } - -func TestSset(t *testing.T) { - col := byte(2) - - t.Run("should set color of pixel in sprite sheet", func(t *testing.T) { - pi.UseEmptySpriteSheet(8, 8) - // when - pi.Sset(2, 1, col) - // then - assert.Equal(t, col, pi.SprSheet().Pix()[10]) - }) - - t.Run("should not set pixel outside the sprite sheet", func(t *testing.T) { - pi.UseEmptySpriteSheet(8, 8) - - emptySheet := make([]byte, len(pi.SprSheet().Pix())) - - tests := []struct{ X, Y int }{ - {-1, 0}, - {0, -1}, - {8, 0}, - {0, 8}, - } - for _, coords := range tests { - name := fmt.Sprintf("%+v", coords) - t.Run(name, func(t *testing.T) { - // when - pi.Sset(coords.X, coords.Y, col) - // then - assert.Equal(t, emptySheet, pi.SprSheet().Pix()) - }) - } - }) -} - -func TestSget(t *testing.T) { - t.Run("should get color of pixel", func(t *testing.T) { - pi.UseEmptySpriteSheet(8, 8) - col := byte(7) - pi.Sset(1, 1, col) - // expect - assert.Equal(t, col, pi.Sget(1, 1)) - }) - - t.Run("should get color 0 if outside the sprite sheet", func(t *testing.T) { - pi.UseEmptySpriteSheet(8, 8) - pixels := pi.SprSheet().Pix() - for i := 0; i < len(pixels); i++ { - pixels[i] = 7 - } - - tests := []struct{ X, Y int }{ - {-1, 0}, - {0, -1}, - {8, 0}, - {0, 8}, - } - for _, coords := range tests { - name := fmt.Sprintf("%+v", coords) - t.Run(name, func(t *testing.T) { - // when - actual := pi.Sget(coords.X, coords.Y) - // then - assert.Zero(t, actual) - }) - } - }) -}