-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
97 lines (80 loc) · 1.55 KB
/
main.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
package main
import (
_ "embed"
"image"
"os"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"github.com/dbut2/dialog"
"github.com/spf13/cobra"
"github.com/dbut2/sapphire/gba"
)
func main() {
c := cmd(run)
err := c.Execute()
if err != nil {
panic(err.Error())
}
}
func run(gamepak []byte) {
a := app.New()
win := window{
emu: gba.NewEmu(gamepak),
window: a.NewWindow("Sapphire"),
}
win.Start()
}
func cmd(run func(gamepak []byte)) *cobra.Command {
c := &cobra.Command{
Use: "sapphire",
RunE: func(cmd *cobra.Command, args []string) error {
game, err := cmd.Flags().GetString("game")
if err != nil {
return err
}
if game == "" {
game = selectGame()
}
gamepak, err := loadGame(game)
if err != nil {
return err
}
run(gamepak)
return nil
},
}
c.Flags().StringP("game", "g", "", "Game to load")
return c
}
type window struct {
emu *gba.Emulator
window fyne.Window
}
func (w *window) Start() {
img := image.NewRGBA(image.Rect(0, 0, 240, 160))
w.emu.LCD.SetImage(img)
cimg := canvas.NewImageFromImage(img)
cimg.ScaleMode = canvas.ImageScalePixels
w.emu.LCD.SetDraw(func() {
cimg.Refresh()
})
w.window.SetContent(cimg)
w.window.Resize(fyne.NewSize(240, 160))
go w.emu.Boot()
w.window.ShowAndRun()
}
func loadGame(game string) ([]byte, error) {
bytes, err := os.ReadFile(game)
if err != nil {
return nil, err
}
return bytes, nil
}
func selectGame() string {
filename, err := dialog.File().Load()
if err != nil {
panic(err.Error())
}
return filename
}