@@ -8,15 +8,16 @@ import (
8
8
"tinygo.org/x/drivers/examples/ili9341/initdisplay"
9
9
"tinygo.org/x/drivers/examples/ili9341/pyportal_boing/graphics"
10
10
"tinygo.org/x/drivers/ili9341"
11
+ "tinygo.org/x/drivers/pixel"
11
12
)
12
13
13
14
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 )
20
21
21
22
YBOTTOM = 123 // Ball Y coord at bottom
22
23
YBOUNCE = - 3.5 // Upward velocity on ball bounce
@@ -25,7 +26,7 @@ const (
25
26
)
26
27
27
28
var (
28
- frameBuffer = [ (graphics .BALLHEIGHT + 8 ) * ( graphics .BALLWIDTH + 8 ) * 2 ] uint8 {}
29
+ frameBuffer = pixel . NewImage [pixel. RGB565BE ] (graphics .BALLWIDTH + 8 , graphics .BALLHEIGHT + 8 )
29
30
30
31
startTime int64
31
32
frame int64
41
42
balloldy float32
42
43
43
44
// Color table for ball rotation effect
44
- palette [16 ]uint16
45
+ palette [16 ]pixel. RGB565BE
45
46
)
46
47
47
48
var (
@@ -108,6 +109,7 @@ func main() {
108
109
109
110
width = maxx - minx + 1
110
111
height = maxy - miny + 1
112
+ buffer := frameBuffer .Rescale (int (width ), int (height ))
111
113
112
114
// Ball animation frame # is incremented opposite the ball's X velocity
113
115
ballframe -= ballvx * 0.5
@@ -128,7 +130,7 @@ func main() {
128
130
}
129
131
130
132
// Only the changed rectangle is drawn into the 'renderbuf' array...
131
- var c uint16 //, *destPtr;
133
+ var c pixel. RGB565BE //, *destPtr;
132
134
bx := minx - int16 (ballx ) // X relative to ball bitmap (can be negative)
133
135
by := miny - int16 (bally ) // Y relative to ball bitmap (can be negative)
134
136
bgx := minx // X relative to background bitmap (>= 0)
@@ -149,19 +151,20 @@ func main() {
149
151
(by >= 0 ) && (by < graphics .BALLHEIGHT ) { // inside the ball bitmap area?
150
152
// Yes, do ball compositing math...
151
153
p = graphics .Ball [int (by * (graphics .BALLWIDTH / 2 ))+ int (bx1 / 2 )] // Get packed value (2 pixels)
154
+ var nibble uint8
152
155
if (bx1 & 1 ) != 0 {
153
- c = uint16 ( p & 0xF )
156
+ nibble = p & 0xF
154
157
} else {
155
- c = uint16 ( p >> 4 )
158
+ nibble = p >> 4
156
159
} // Unpack high or low nybble
157
- if c == 0 { // Outside ball - just draw grid
160
+ if nibble == 0 { // Outside ball - just draw grid
158
161
if graphics .Background [bgidx ]& (0x80 >> (bgx1 & 7 )) != 0 {
159
162
c = GRIDCOLOR
160
163
} else {
161
164
c = BGCOLOR
162
165
}
163
- } else if c > 1 { // In ball area...
164
- c = palette [c ]
166
+ } else if nibble > 1 { // In ball area...
167
+ c = palette [nibble ]
165
168
} else { // In shadow area...
166
169
if graphics .Background [bgidx ]& (0x80 >> (bgx1 & 7 )) != 0 {
167
170
c = GRIDSHADOW
@@ -176,8 +179,7 @@ func main() {
176
179
c = BGCOLOR
177
180
}
178
181
}
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 )
181
183
bx1 ++ // Increment bitmap position counters (X axis)
182
184
bgx1 ++
183
185
}
@@ -188,7 +190,7 @@ func main() {
188
190
bgy ++
189
191
}
190
192
191
- display .DrawRGBBitmap8 (minx , miny , frameBuffer [: width * height * 2 ], width , height )
193
+ display .DrawBitmap (minx , miny , buffer )
192
194
193
195
// Show approximate frame rate
194
196
frame ++
@@ -205,6 +207,7 @@ func DrawBackground() {
205
207
w , h := display .Size ()
206
208
byteWidth := (w + 7 ) / 8 // Bitmap scanline pad = whole byte
207
209
var b uint8
210
+ buffer := frameBuffer .Rescale (int (w ), 1 )
208
211
for j := int16 (0 ); j < h ; j ++ {
209
212
for k := int16 (0 ); k < w ; k ++ {
210
213
if k & 7 > 0 {
@@ -213,13 +216,11 @@ func DrawBackground() {
213
216
b = graphics .Background [j * byteWidth + k / 8 ]
214
217
}
215
218
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 )
218
220
} else {
219
- frameBuffer [2 * k ] = byte (GRIDCOLOR >> 8 )
220
- frameBuffer [2 * k + 1 ] = byte (GRIDCOLOR & 0xFF )
221
+ buffer .Set (int (k ), 0 , GRIDCOLOR )
221
222
}
222
223
}
223
- display .DrawRGBBitmap8 (0 , j , frameBuffer [ 0 : w * 2 ], w , 1 )
224
+ display .DrawBitmap (0 , j , buffer )
224
225
}
225
226
}
0 commit comments