-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
koebiten.go
283 lines (244 loc) · 5.75 KB
/
koebiten.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//go:build tinygo
package koebiten
import (
"errors"
"fmt"
"image/color"
"io/fs"
"reflect"
"strings"
"time"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/image/png"
"tinygo.org/x/drivers/pixel"
"tinygo.org/x/tinydraw"
"tinygo.org/x/tinyfont"
)
type Image struct {
}
func (i *Image) Size() (x, y int16) {
return 0, 0
}
func (i *Image) SetPixel(x, y int16, c color.RGBA) {
}
func (i *Image) Display() error {
return nil
}
func (i *Image) ClearDisplay() {
}
func (i *Image) ClearBuffer() {
}
// Displayer interface for display operations.
type Displayer interface {
drivers.Displayer
ClearDisplay()
ClearBuffer()
}
var (
display Displayer
textY int16
)
var (
white = color.RGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF}
black = color.RGBA{R: 0x00, G: 0x00, B: 0x00, A: 0xFF}
)
var keyUpdate = func() error { return nil }
func init() {
pngBuffer = map[string]pixel.Image[pixel.Monochrome]{}
}
// Run starts the main loop for the application.
func Run(d func()) error {
tick := time.Tick(32 * time.Millisecond)
for {
<-tick
keyUpdate()
theInputState.update()
textY = 0
display.ClearBuffer()
d()
display.Display()
}
return nil
}
func RunGame(game Game) error {
tick := time.Tick(32 * time.Millisecond)
for {
<-tick
keyUpdate()
theInputState.update()
textY = 0
display.ClearBuffer()
err := game.Update()
if err != nil {
if errors.Is(err, Termination) {
return nil
}
return err
}
game.Draw(nil)
display.Display()
}
return nil
}
// SetWindowSize sets the size of the display window.
func SetWindowSize(w, h int) {
}
// SetWindowTitle sets the title of the display window.
func SetWindowTitle(title string) {
}
func SetHardware(h Hardware) error {
err := h.Init()
if err != nil {
return err
}
display = h.GetDisplay()
keyUpdate = h.KeyUpdate
return nil
}
// SetRotation sets the display rotation mode.
// If the display is already a RotatedDisplay, it updates the mode.
// Otherwise, it wraps the existing display in a new RotatedDisplay with the specified mode.
func SetRotation(mode int) {
d, ok := display.(*RotatedDisplay)
if ok {
d.mode = mode
} else {
display = &RotatedDisplay{
Displayer: display,
mode: mode,
}
}
}
// Println prints formatted output to the display.
func Println(args ...any) {
str := []string{}
for _, x := range args {
s, ok := x.(string)
if ok {
str = append(str, s)
continue
}
i, ok := x.(int)
if ok {
str = append(str, fmt.Sprintf("%d", i))
continue
}
}
textY += 8
tinyfont.WriteLine(display, &tinyfont.Org01, 2, textY, strings.Join(str, " "), white)
}
// DrawText draws text on the display.
func DrawText(dst Displayer, str string, font tinyfont.Fonter, x, y int16, c pixel.BaseColor) {
if isNil(dst) {
dst = display
}
if font == nil {
font = &tinyfont.Org01
}
tinyfont.WriteLine(dst, font, x, y, str, c.RGBA())
}
// DrawRect draws a rectangle on the display.
func DrawRect(dst Displayer, x, y, w, h int, c pixel.BaseColor) {
if isNil(dst) {
dst = display
}
tinydraw.Rectangle(dst, int16(x), int16(y), int16(w), int16(h), c.RGBA())
}
// DrawFilledRect draws a filled rectangle on the display.
func DrawFilledRect(dst Displayer, x, y, w, h int, c pixel.BaseColor) {
if isNil(dst) {
dst = display
}
tinydraw.FilledRectangle(dst, int16(x), int16(y), int16(w), int16(h), c.RGBA())
}
// DrawLine draws a line on the display.
func DrawLine(dst Displayer, x1, y1, x2, y2 int, c pixel.BaseColor) {
if isNil(dst) {
dst = display
}
tinydraw.Line(dst, int16(x1), int16(y1), int16(x2), int16(y2), c.RGBA())
}
// DrawCircle draws a circle on the display.
func DrawCircle(dst Displayer, x, y, r int, c pixel.BaseColor) {
if isNil(dst) {
dst = display
}
tinydraw.Circle(dst, int16(x), int16(y), int16(r), c.RGBA())
}
// DrawFilledCircle draws a filled circle on the display.
func DrawFilledCircle(dst Displayer, x, y, r int, c pixel.BaseColor) {
if isNil(dst) {
dst = display
}
tinydraw.FilledCircle(dst, int16(x), int16(y), int16(r), c.RGBA())
}
// DrawTriangle draws a triangle on the display.
func DrawTriangle(dst Displayer, x0, y0, x1, y1, x2, y2 int, c pixel.BaseColor) {
if isNil(dst) {
dst = display
}
tinydraw.Triangle(dst, int16(x0), int16(y0), int16(x1), int16(y1), int16(x2), int16(y2), c.RGBA())
}
// DrawFilledTriangle draws a filled triangle on the display.
func DrawFilledTriangle(dst Displayer, x0, y0, x1, y1, x2, y2 int, c pixel.BaseColor) {
if isNil(dst) {
dst = display
}
tinydraw.FilledTriangle(dst, int16(x0), int16(y0), int16(x1), int16(y1), int16(x2), int16(y2), c.RGBA())
}
var (
buffer [3 * 8 * 8 * 4]uint16
pngBuffer map[string]pixel.Image[pixel.Monochrome]
)
// DrawImageFS draws an image from the filesystem onto the display.
func DrawImageFS(dst Displayer, fsys fs.FS, path string, x, y int) {
if isNil(dst) {
dst = display
}
img, ok := pngBuffer[path]
if !ok {
p, err := fsys.Open(path)
if err != nil {
return
}
png.SetCallback(buffer[:], func(data []uint16, x, y, w, h, width, height int16) {
if img.Len() == 0 {
img = pixel.NewImage[pixel.Monochrome](int(width), int(height))
}
for yy := int16(0); yy < h; yy++ {
for xx := int16(0); xx < w; xx++ {
c := C565toRGBA(data[yy*w+xx])
cnt := 0
if c.R < 0x80 {
cnt++
}
if c.G < 0x80 {
cnt++
}
if c.B < 0x80 {
cnt++
}
if cnt >= 2 {
img.Set(int(x+xx), int(y+yy), true)
}
}
}
})
_, err = png.Decode(p)
if err != nil {
return
}
pngBuffer[path] = img
}
w, h := img.Size()
for yy := 0; yy < h; yy++ {
for xx := 0; xx < w; xx++ {
if img.Get(xx, yy) == true {
dst.SetPixel(int16(x)+int16(xx), int16(y)+int16(yy), white)
}
}
}
}
func isNil(d Displayer) bool {
return d == nil || (reflect.ValueOf(d).Kind() == reflect.Ptr && reflect.ValueOf(d).IsNil())
}