Skip to content

Commit a31db2b

Browse files
committed
pyportal_boing: update to use DrawBitmap
Replace DrawRGBBitmap8 with DrawBitmap, following the change in the previous commit. This improves performance from 86fps to 100fps! I didn't investigate why, but I suspect it's because it now needs only a single store instead of two to update a pixel.
1 parent 43c1f88 commit a31db2b

File tree

1 file changed

+23
-22
lines changed
  • examples/ili9341/pyportal_boing

1 file changed

+23
-22
lines changed

examples/ili9341/pyportal_boing/main.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ import (
88
"tinygo.org/x/drivers/examples/ili9341/initdisplay"
99
"tinygo.org/x/drivers/examples/ili9341/pyportal_boing/graphics"
1010
"tinygo.org/x/drivers/ili9341"
11+
"tinygo.org/x/drivers/pixel"
1112
)
1213

1314
const (
14-
BGCOLOR = 0xAD75
15-
GRIDCOLOR = 0xA815
16-
BGSHADOW = 0x5285
17-
GRIDSHADOW = 0x600C
18-
RED = 0xF800
19-
WHITE = 0xFFFF
15+
BGCOLOR = pixel.RGB565BE(0x75AD)
16+
GRIDCOLOR = pixel.RGB565BE(0x15A8)
17+
BGSHADOW = pixel.RGB565BE(0x8552)
18+
GRIDSHADOW = pixel.RGB565BE(0x0C60)
19+
RED = pixel.RGB565BE(0x00F8)
20+
WHITE = pixel.RGB565BE(0xFFFF)
2021

2122
YBOTTOM = 123 // Ball Y coord at bottom
2223
YBOUNCE = -3.5 // Upward velocity on ball bounce
@@ -25,7 +26,7 @@ const (
2526
)
2627

2728
var (
28-
frameBuffer = [(graphics.BALLHEIGHT + 8) * (graphics.BALLWIDTH + 8) * 2]uint8{}
29+
frameBuffer = pixel.NewImage[pixel.RGB565BE](graphics.BALLWIDTH+8, graphics.BALLHEIGHT+8)
2930

3031
startTime int64
3132
frame int64
@@ -41,7 +42,7 @@ var (
4142
balloldy float32
4243

4344
// Color table for ball rotation effect
44-
palette [16]uint16
45+
palette [16]pixel.RGB565BE
4546
)
4647

4748
var (
@@ -108,6 +109,7 @@ func main() {
108109

109110
width = maxx - minx + 1
110111
height = maxy - miny + 1
112+
buffer := frameBuffer.Rescale(int(width), int(height))
111113

112114
// Ball animation frame # is incremented opposite the ball's X velocity
113115
ballframe -= ballvx * 0.5
@@ -128,7 +130,7 @@ func main() {
128130
}
129131

130132
// Only the changed rectangle is drawn into the 'renderbuf' array...
131-
var c uint16 //, *destPtr;
133+
var c pixel.RGB565BE //, *destPtr;
132134
bx := minx - int16(ballx) // X relative to ball bitmap (can be negative)
133135
by := miny - int16(bally) // Y relative to ball bitmap (can be negative)
134136
bgx := minx // X relative to background bitmap (>= 0)
@@ -149,19 +151,20 @@ func main() {
149151
(by >= 0) && (by < graphics.BALLHEIGHT) { // inside the ball bitmap area?
150152
// Yes, do ball compositing math...
151153
p = graphics.Ball[int(by*(graphics.BALLWIDTH/2))+int(bx1/2)] // Get packed value (2 pixels)
154+
var nibble uint8
152155
if (bx1 & 1) != 0 {
153-
c = uint16(p & 0xF)
156+
nibble = p & 0xF
154157
} else {
155-
c = uint16(p >> 4)
158+
nibble = p >> 4
156159
} // Unpack high or low nybble
157-
if c == 0 { // Outside ball - just draw grid
160+
if nibble == 0 { // Outside ball - just draw grid
158161
if graphics.Background[bgidx]&(0x80>>(bgx1&7)) != 0 {
159162
c = GRIDCOLOR
160163
} else {
161164
c = BGCOLOR
162165
}
163-
} else if c > 1 { // In ball area...
164-
c = palette[c]
166+
} else if nibble > 1 { // In ball area...
167+
c = palette[nibble]
165168
} else { // In shadow area...
166169
if graphics.Background[bgidx]&(0x80>>(bgx1&7)) != 0 {
167170
c = GRIDSHADOW
@@ -176,8 +179,7 @@ func main() {
176179
c = BGCOLOR
177180
}
178181
}
179-
frameBuffer[(y*int(width)+x)*2] = byte(c >> 8)
180-
frameBuffer[(y*int(width)+x)*2+1] = byte(c)
182+
buffer.Set(x, y, c)
181183
bx1++ // Increment bitmap position counters (X axis)
182184
bgx1++
183185
}
@@ -188,7 +190,7 @@ func main() {
188190
bgy++
189191
}
190192

191-
display.DrawRGBBitmap8(minx, miny, frameBuffer[:width*height*2], width, height)
193+
display.DrawBitmap(minx, miny, buffer)
192194

193195
// Show approximate frame rate
194196
frame++
@@ -205,6 +207,7 @@ func DrawBackground() {
205207
w, h := display.Size()
206208
byteWidth := (w + 7) / 8 // Bitmap scanline pad = whole byte
207209
var b uint8
210+
buffer := frameBuffer.Rescale(int(w), 1)
208211
for j := int16(0); j < h; j++ {
209212
for k := int16(0); k < w; k++ {
210213
if k&7 > 0 {
@@ -213,13 +216,11 @@ func DrawBackground() {
213216
b = graphics.Background[j*byteWidth+k/8]
214217
}
215218
if b&0x80 == 0 {
216-
frameBuffer[2*k] = byte(BGCOLOR >> 8)
217-
frameBuffer[2*k+1] = byte(BGCOLOR & 0xFF)
219+
buffer.Set(int(k), 0, BGCOLOR)
218220
} else {
219-
frameBuffer[2*k] = byte(GRIDCOLOR >> 8)
220-
frameBuffer[2*k+1] = byte(GRIDCOLOR & 0xFF)
221+
buffer.Set(int(k), 0, GRIDCOLOR)
221222
}
222223
}
223-
display.DrawRGBBitmap8(0, j, frameBuffer[0:w*2], w, 1)
224+
display.DrawBitmap(0, j, buffer)
224225
}
225226
}

0 commit comments

Comments
 (0)