Skip to content

Commit

Permalink
Rename pi.Pset and pi.Pget to pi.Set and pi.Get. Remove pi.Sset and p…
Browse files Browse the repository at this point in the history
…i.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.
  • Loading branch information
elgopher committed Aug 13, 2023
1 parent eb6c4a2 commit aa68d8e
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 151 deletions.
2 changes: 1 addition & 1 deletion devtools/internal/icons/icons.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
const (
Pointer = 0
MeasureTool = 1
PsetTool = 2
SetTool = 2
LineTool = 3
RectTool = 4
RectFillTool = 5
Expand Down
2 changes: 1 addition & 1 deletion devtools/internal/inspector/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 8 additions & 8 deletions devtools/internal/inspector/pset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions devtools/internal/inspector/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion devtools/internal/inspector/toolbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions devtools/internal/lib/github_com-elgopher-pi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
4 changes: 2 additions & 2 deletions examples/trigonometry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
12 changes: 6 additions & 6 deletions internal/bench/screen_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
Expand Down
26 changes: 0 additions & 26 deletions internal/bench/sprite_sheet_bench_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion palette.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions pi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

Expand Down
8 changes: 4 additions & 4 deletions screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 11 additions & 11 deletions screen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ 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) {
pi.Reset()
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
Expand Down Expand Up @@ -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())
})
Expand All @@ -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())
})
Expand All @@ -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())
})
Expand Down Expand Up @@ -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)
})
Expand Down Expand Up @@ -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)
})
Expand Down Expand Up @@ -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)
})

Expand Down
2 changes: 1 addition & 1 deletion snap/snap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 0 additions & 10 deletions sprite_sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit aa68d8e

Please sign in to comment.