Skip to content

Commit 090b7a9

Browse files
committed
Make Color a function
For multiple reasons: * In Pico-8 this is also a function, so this change will make Pi more compatible with Pico-8 * If this is a function, then we can add some code inside it (mainly for optimization reasons)
1 parent 41575cf commit 090b7a9

File tree

5 files changed

+70
-23
lines changed

5 files changed

+70
-23
lines changed

examples/trigonometry/trigonometry.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func main() {
1818

1919
pi.Draw = func() {
2020
pi.Cls()
21-
pi.Color = 1
21+
pi.Color(1)
2222

2323
draw(32, 8, pi.Sin)
2424
draw(96, 11, pi.Cos)
@@ -28,10 +28,10 @@ func main() {
2828
}
2929

3030
func draw(line int, color byte, f func(x float64) float64) {
31-
pi.Color = 1
31+
pi.Color(1)
3232
drawHorizontalAxis(line)
3333

34-
pi.Color = color
34+
pi.Color(color)
3535
for x := 0.0; x < 128; x++ {
3636
angle := float64(x+start*speed) / 128
3737
dy := math.Round(f(angle) * 16)

pi.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const (
2323
defaultSpriteSheetHeight = 128
2424
defaultScreenWidth = 128
2525
defaultScreenHeight = 128
26+
defaultColor = byte(6)
2627
)
2728

2829
// User parameters. Will be used during Boot (and Run).
@@ -83,7 +84,6 @@ func Reset() {
8384
SpriteSheetHeight = defaultSpriteSheetHeight
8485
ScreenWidth = defaultScreenWidth
8586
ScreenHeight = defaultScreenHeight
86-
Color = 6
8787
Palette = defaultPalette
8888
}
8989

@@ -126,6 +126,7 @@ func Boot() error {
126126
Camera(0, 0)
127127
PaltReset()
128128
PalReset()
129+
Color(defaultColor)
129130

130131
return nil
131132
}

pi_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func TestBoot(t *testing.T) {
110110
t.Run("should reset draw state", func(t *testing.T) {
111111
pi.Reset()
112112
require.NoError(t, pi.Boot())
113-
pi.Color = 14
113+
pi.Color(14)
114114
pi.Camera(1, 2)
115115
pi.Clip(1, 2, 3, 4)
116116
// when
@@ -125,7 +125,7 @@ func TestBoot(t *testing.T) {
125125
assert.Zero(t, y)
126126
assert.Equal(t, pi.ScreenWidth, w)
127127
assert.Equal(t, pi.ScreenHeight, h)
128-
assert.Equal(t, byte(14), pi.Color)
128+
assert.Equal(t, byte(6), pi.ColorReset())
129129
})
130130

131131
t.Run("changing the user parameters after Boot should not ends up in a panic", func(t *testing.T) {

screen.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ package pi
55

66
// Screen-specific data
77
var (
8-
Color byte = 6 // Color is a currently used color in draw state. Used by Pset.
9-
108
// ScreenData contains pixel colors for the screen visible by the player.
119
// Each pixel is one byte. It is initialized during pi.Boot.
1210
//
@@ -24,6 +22,7 @@ var (
2422
lineOfScreenWidth []byte
2523
zeroScreenData []byte
2624
clippingRegion rect
25+
color = defaultColor // Color is a currently used color in draw state. Used by Pset.
2726
)
2827

2928
// Cls cleans the entire screen with color 0. It does not take into account any draw state parameters such as clipping region or camera.
@@ -44,6 +43,22 @@ func ClsCol(col byte) {
4443
}
4544
}
4645

46+
// Color sets the color used by Pset.
47+
//
48+
// Color returns previously used color.
49+
func Color(col byte) (prevCol byte) {
50+
prevCol = color
51+
color = col
52+
return
53+
}
54+
55+
// ColorReset resets the color to default value which is 6.
56+
//
57+
// ColorReset returns previously used color.
58+
func ColorReset() (prevCol byte) {
59+
return Color(defaultColor)
60+
}
61+
4762
// Pset sets a pixel color on the screen to Color.
4863
func Pset(x, y int) {
4964
x -= camera.x
@@ -74,7 +89,7 @@ func Pset(x, y int) {
7489
return
7590
}
7691

77-
ScreenData[y*scrWidth+x] = drawPalette[Color]
92+
ScreenData[y*scrWidth+x] = drawPalette[color]
7893
}
7994

8095
// Pget gets a pixel color on the screen.

screen_test.go

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,42 @@ import (
1717
"github.com/stretchr/testify/require"
1818
)
1919

20-
//go:embed internal/testimage/sprite-sheet-16x16.png
21-
var spriteSheet16x16 []byte
20+
var (
21+
//go:embed internal/testimage/sprite-sheet-16x16.png
22+
spriteSheet16x16 []byte
23+
//go:embed internal/testimage/*.png
24+
images embed.FS
25+
)
26+
27+
func TestColor(t *testing.T) {
28+
t.Run("should return default color", func(t *testing.T) {
29+
pi.BootOrPanic()
30+
assert.Equal(t, byte(6), pi.Color(2))
31+
})
32+
33+
t.Run("should return previous color", func(t *testing.T) {
34+
pi.BootOrPanic()
35+
prev := byte(3)
36+
pi.Color(prev)
37+
assert.Equal(t, prev, pi.Color(4))
38+
})
39+
}
40+
41+
func TestColorReset(t *testing.T) {
42+
t.Run("should reset color to default", func(t *testing.T) {
43+
pi.BootOrPanic()
44+
pi.Color(3)
45+
pi.ColorReset()
46+
assert.Equal(t, byte(6), pi.Color(5))
47+
})
2248

23-
//go:embed internal/testimage/*.png
24-
var images embed.FS
49+
t.Run("should return previous color", func(t *testing.T) {
50+
pi.BootOrPanic()
51+
prev := byte(5)
52+
pi.Color(prev)
53+
assert.Equal(t, prev, pi.ColorReset())
54+
})
55+
}
2556

2657
func TestCls(t *testing.T) {
2758
t.Run("should clean screen using color 0", func(t *testing.T) {
@@ -58,7 +89,7 @@ func TestPset(t *testing.T) {
5889
pi.ScreenHeight = 2
5990
pi.BootOrPanic()
6091
// when
61-
pi.Color = col
92+
pi.Color(col)
6293
pi.Pset(1, 1)
6394
// then
6495
assert.Equal(t, col, pi.ScreenData[3])
@@ -81,7 +112,7 @@ func TestPset(t *testing.T) {
81112
name := fmt.Sprintf("%+v", coords)
82113
t.Run(name, func(t *testing.T) {
83114
// when
84-
pi.Color = col
115+
pi.Color(col)
85116
pi.Pset(coords.X, coords.Y)
86117
// then
87118
assert.Equal(t, emptyScreen, pi.ScreenData)
@@ -105,7 +136,7 @@ func TestPset(t *testing.T) {
105136
pi.BootOrPanic()
106137
pi.Clip(1, 1, 1, 1)
107138
// when
108-
pi.Color = col
139+
pi.Color(col)
109140
pi.Pset(coords.X, coords.Y)
110141
// then
111142
assert.Equal(t, emptyScreen, pi.ScreenData)
@@ -118,7 +149,7 @@ func TestPset(t *testing.T) {
118149
pi.ScreenHeight = 2
119150
pi.BootOrPanic()
120151
pi.Camera(1, 2)
121-
pi.Color = 8
152+
pi.Color(8)
122153
// when
123154
pi.Pset(1, 2)
124155
// then
@@ -149,7 +180,7 @@ func TestPset(t *testing.T) {
149180
name := fmt.Sprintf("%+v", coords)
150181
t.Run(name, func(t *testing.T) {
151182
pi.BootOrPanic()
152-
pi.Color = col
183+
pi.Color(col)
153184
// when
154185
pi.Camera(1, 1)
155186
pi.Pset(coords.X, coords.Y)
@@ -163,7 +194,7 @@ func TestPset(t *testing.T) {
163194
pi.ScreenWidth = 1
164195
pi.ScreenHeight = 1
165196
pi.BootOrPanic()
166-
pi.Color = 1
197+
pi.Color(1)
167198
pi.Pal(1, 2)
168199
// when
169200
pi.Pset(0, 0)
@@ -175,7 +206,7 @@ func TestPset(t *testing.T) {
175206
pi.ScreenWidth = 1
176207
pi.ScreenHeight = 1
177208
pi.BootOrPanic()
178-
pi.Color = 1
209+
pi.Color(1)
179210
pi.Pal(1, 2)
180211
pi.PalReset()
181212
// when
@@ -191,7 +222,7 @@ func TestPget(t *testing.T) {
191222
pi.ScreenHeight = 2
192223
pi.BootOrPanic()
193224
col := byte(7)
194-
pi.Color = col
225+
pi.Color(col)
195226
pi.Pset(1, 1)
196227
// expect
197228
assert.Equal(t, col, pi.Pget(1, 1))
@@ -249,12 +280,12 @@ func TestPget(t *testing.T) {
249280
pi.ScreenHeight = 2
250281
pi.BootOrPanic()
251282
pi.Camera(1, 2)
252-
pi.Color = 8
283+
pi.Color(8)
253284
pi.Pset(1, 2)
254285
// when
255286
actual := pi.Pget(1, 2)
256287
// then
257-
assert.Equal(t, pi.Color, actual)
288+
assert.Equal(t, pi.ColorReset(), actual)
258289
})
259290

260291
t.Run("should get color 0 for pixels outside the screen when camera is set", func(t *testing.T) {

0 commit comments

Comments
 (0)