-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (60 loc) · 1.59 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
package main
import (
"flag"
"fmt"
"os"
"time"
"github.com/cbott/GoEmulate/cartridges"
"github.com/cbott/GoEmulate/gameboy"
"github.com/gopxl/pixel/v2"
"github.com/gopxl/pixel/v2/backends/opengl"
)
// initial size of the Game Boy window, overridden by command line flag
const DefaultScale = 4
// Maximum allowable speed multiplier for emulation
const MaximumSpeed = 10
func run() {
// Parse cmd line args
runBootROM := flag.Bool("bootrom", false, "run boot ROM prior to cartridge")
useDebugColors := flag.Bool("debug", false, "use debug colors (color sprites red, window green, background blue)")
scaleflag := flag.Int("scale", DefaultScale, "window scale factor")
flag.Parse()
romFile := flag.Arg(0)
if romFile == "" {
fmt.Println("ROM file must be specified")
os.Exit(1)
}
// Construct Pixel window
var scale float64 = float64(*scaleflag)
cfg := opengl.WindowConfig{
Title: "Game Boy Emulator",
Bounds: pixel.R(0, 0, gameboy.ScreenWidth*scale, gameboy.ScreenHeight*scale),
VSync: true,
Resizable: true,
}
win, err := opengl.NewWindow(cfg)
if err != nil {
panic(err)
}
// Construct Game Boy emulator
gb := gameboy.NewGameBoy(!*runBootROM, *useDebugColors)
gb.LoadCartridge(cartridges.Make(romFile))
emulator := Emulator{
console: gb,
window: win,
speed: 1,
}
// Ticker will execute once per Game Boy frame
var factor float64 = gameboy.FramesPerSecond
ticker := time.NewTicker(time.Nanosecond * time.Duration(int64(1e9/factor)))
for !win.Closed() {
select {
case <-ticker.C:
update(&emulator)
default:
}
}
}
func main() {
opengl.Run(run)
}