forked from HFO4/gameboy.live
/
driver.go
284 lines (241 loc) · 6.37 KB
/
driver.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
284
package fyne
import (
"fmt"
"image"
"image/color"
"log"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/storage"
"github.com/andydotxyz/fynegameboy/util"
)
type LCD struct {
Open func(fyne.URIReadCloser)
Pause func()
Reset func()
Resume func()
DrawSignal chan bool
app fyne.App
pixels *[160][144][3]uint8
screen *image.RGBA
bg *canvas.Rectangle
buttons fyne.CanvasObject
frame fyne.CanvasObject
inputStatus *byte
interrupt bool
title string
paused bool
}
func NewDriver() *LCD {
a := app.NewWithID("xyz.andy.gameboy")
a.SetIcon(resourceIconPng)
return &LCD{app: a}
}
func (lcd *LCD) Init(pixels *[160][144][3]uint8, title string) {
lcd.pixels = pixels
lcd.title = title
log.Println("[Display] Initialize Fyne GUI display")
}
func (lcd *LCD) InitStatus(statusPointer *byte) {
lcd.inputStatus = statusPointer
}
func (lcd *LCD) UpdateInput() bool {
if lcd.interrupt {
lcd.interrupt = false
return true
}
return false
}
func (lcd *LCD) NewInput(b []byte) {
}
func (lcd *LCD) draw() image.Image {
i := 0
for y := 0; y < 144; y++ {
for x := 0; x < 160; x++ {
r, g, b := lcd.pixels[x][y][0], lcd.pixels[x][y][1], lcd.pixels[x][y][2]
if r == 0xFF && g == 0xFF && b == 0xFF {
lcd.screen.Pix[i] = 0x9b
lcd.screen.Pix[i+1] = 0xbc
lcd.screen.Pix[i+2] = 0x0f
} else if r == 0xCC && g == 0xCC && b == 0xCC {
lcd.screen.Pix[i] = 0x8b
lcd.screen.Pix[i+1] = 0xac
lcd.screen.Pix[i+2] = 0x0f
} else if r == 0x77 && g == 0x77 && b == 0x77 {
lcd.screen.Pix[i] = 0x30
lcd.screen.Pix[i+1] = 0x62
lcd.screen.Pix[i+2] = 0x30
} else {
lcd.screen.Pix[i] = 0x0f
lcd.screen.Pix[i+1] = 0x38
lcd.screen.Pix[i+2] = 0x0f
}
lcd.screen.Pix[i+3] = 0xff
i += 4
}
}
return lcd.screen
}
// Mapping from keys to GB index.
// Reference :https://github.com/Humpheh/goboy/blob/master/pkg/gbio/iopixel/pixels.go
var keyMap = map[fyne.KeyName]byte{
// A button
fyne.KeyX: 4,
// B button
fyne.KeyZ: 5,
// SELECT button
fyne.KeyBackspace: 6,
// START button
fyne.KeyReturn: 7,
// RIGHT button
fyne.KeyRight: 0,
// LEFT button
fyne.KeyLeft: 1,
// UP button
fyne.KeyUp: 2,
// DOWN button
fyne.KeyDown: 3,
}
func (lcd *LCD) downCode(num uint) {
statusCopy := *lcd.inputStatus
statusCopy = util.ClearBit(statusCopy, num)
lcd.interrupt = true
*lcd.inputStatus = statusCopy
}
func (lcd *LCD) buttonDown(ev *fyne.KeyEvent) {
if offset, ok := keyMap[ev.Name]; ok {
lcd.downCode(uint(offset))
}
}
func (lcd *LCD) upCode(num uint) {
statusCopy := *lcd.inputStatus
statusCopy = util.SetBit(statusCopy, num)
lcd.interrupt = true
*lcd.inputStatus = statusCopy
}
func (lcd *LCD) buttonUp(ev *fyne.KeyEvent) {
if offset, ok := keyMap[ev.Name]; ok {
lcd.upCode(uint(offset))
}
}
func (lcd *LCD) MinSize([]fyne.CanvasObject) fyne.Size {
if fyne.CurrentDevice().IsMobile() {
return fyne.NewSize(520, 800)
}
return fyne.NewSize(520, 400)
}
func (lcd *LCD) Layout(_ []fyne.CanvasObject, size fyne.Size) {
lcd.bg.Resize(size)
frameSpacePos := fyne.NewPos(0, 0)
frameSpaceSize := size
if fyne.CurrentDevice().IsMobile() {
buttonSpacePos := fyne.NewPos(0, 0)
buttonSpaceSize := size
if fyne.IsHorizontal(fyne.CurrentDevice().Orientation()) {
oldWidth := frameSpaceSize.Width
frameSpaceSize.Width *= 0.7
frameSpacePos.X = (oldWidth - frameSpaceSize.Width) / 2
} else {
frameSpaceSize.Height /= 2
buttonSpaceSize.Height = size.Height - frameSpaceSize.Height
buttonSpacePos.Y = size.Height - buttonSpaceSize.Height
}
lcd.buttons.Move(buttonSpacePos)
lcd.buttons.Resize(buttonSpaceSize)
}
frameSpaceRatio := frameSpaceSize.Width / frameSpaceSize.Height
frameRatio := float32(1.3)
frameSize := frameSpaceSize
framePos := frameSpacePos
if frameSpaceRatio > frameRatio {
frameSize = fyne.NewSize(frameSpaceSize.Height*frameRatio, frameSpaceSize.Height)
framePos = frameSpacePos.Add(fyne.NewPos((frameSpaceSize.Width-frameSize.Width)/2, 0))
} else if frameSpaceRatio < frameRatio {
frameSize = fyne.NewSize(frameSpaceSize.Width, frameSpaceSize.Width/frameRatio)
framePos = frameSpacePos.Add(fyne.NewPos(0, (frameSpaceSize.Height-frameSize.Height)/2))
}
lcd.frame.Move(framePos)
lcd.frame.Resize(frameSize)
}
func (lcd *LCD) Run(drawSignal chan bool, onQuit func()) {
win := lcd.app.NewWindow(fmt.Sprintf("GameBoy - %s", lcd.title))
lcd.app.Lifecycle().SetOnExitedForeground(func() {
if lcd.paused {
return
}
lcd.paused = true
lcd.Pause()
d := dialog.NewInformation("Paused", "Tap 'OK' to resume", win)
d.SetOnClosed(func() {
lcd.Resume()
lcd.paused = false
})
d.Show()
})
lcd.DrawSignal = drawSignal
lcd.screen = image.NewRGBA(image.Rect(0, 0, 160, 144))
output := canvas.NewImageFromImage(lcd.screen)
output.ScaleMode = canvas.ImageScalePixels
go func() {
for {
// drawSignal was sent by the emulator
<-lcd.DrawSignal
lcd.draw()
canvas.Refresh(output)
}
}()
frame := newFrame(output)
lcd.frame = frame.makeUI()
lcd.bg = canvas.NewRectangle(&color.Gray{Y: 0xbd})
objects := []fyne.CanvasObject{lcd.bg, lcd.frame}
if fyne.CurrentDevice().IsMobile() {
buttons := newButtons(lcd)
lcd.buttons = buttons.makeUI()
objects = append(objects, lcd.buttons)
}
content := container.New(lcd, objects...)
win.SetPadded(false)
win.SetContent(content)
if deskCanvas, ok := win.Canvas().(desktop.Canvas); ok {
deskCanvas.SetOnKeyDown(lcd.buttonDown)
deskCanvas.SetOnKeyUp(lcd.buttonUp)
}
if !fyne.CurrentDevice().IsMobile() {
win.Resize(fyne.NewSize(520, 400))
}
win.SetOnClosed(func() {
onQuit()
lcd.app.Quit()
})
win.SetMainMenu(fyne.NewMainMenu(fyne.NewMenu("File",
fyne.NewMenuItem("Open...", func() {
open := dialog.NewFileOpen(func(u fyne.URIReadCloser, err error) {
if u == nil {
return
}
if err != nil {
dialog.ShowError(err, win)
return
}
lcd.app.Preferences().SetString("RomURI", u.URI().String())
lcd.Open(u)
}, win)
if !fyne.CurrentDevice().IsMobile() {
open.SetFilter(storage.NewExtensionFileFilter([]string{".gb"}))
}
open.Show()
}),
fyne.NewMenuItem("Reset...", func() {
dialog.ShowConfirm("Reset game", "Are you sure you want to reset?", func(ok bool) {
if ok {
lcd.Reset()
}
}, win)
}),
)))
win.ShowAndRun()
}