-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (158 loc) · 3.22 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
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
package main
import (
"errors"
"fmt"
"gochip8/internal"
"os"
"github.com/veandco/go-sdl2/sdl"
)
const (
SCALE = 15
WINDOW_WIDTH = internal.SCREEN_WIDTH * SCALE
WINDOW_HEIGHT = internal.SCREEN_HEIGHT * SCALE
TICKS_PER_FRAME = 10
FRAMERATE_LIMIT = 60
MILLISECONDS_PER_FRAME = 1000 / FRAMERATE_LIMIT
)
func main() {
if err := emulatorMain(); err != nil {
sdl.ShowSimpleMessageBox(sdl.MESSAGEBOX_ERROR, "Error", err.Error(), nil)
}
}
func emulatorMain() (out error) {
if len(os.Args) != 2 {
return fmt.Errorf("usage: chip8-emulator path/to/game")
}
emulator := internal.NewEmulator()
rom, err := os.ReadFile(os.Args[1])
if err != nil {
return err
}
emulator.Load(rom)
if err = sdl.Init(sdl.INIT_EVERYTHING); err != nil {
return err
}
defer sdl.Quit()
window, renderer, err := sdl.CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, sdl.WINDOW_HIDDEN)
if err != nil {
return err
}
defer func() {
if err := renderer.Destroy(); err != nil {
out = err
return
}
if err = window.Destroy(); err != nil {
out = err
return
}
}()
window.SetTitle("CHIP8 Emulator Go")
window.Show()
gameloop:
for {
start := sdl.GetTicks64()
event := sdl.PollEvent()
for event != nil {
switch event.GetType() {
case sdl.QUIT:
break gameloop
case sdl.KEYDOWN:
keyevent, ok := event.(*sdl.KeyboardEvent)
if !ok {
return errors.New("invalid keyboard event")
}
button := key2Button(keyevent.Keysym.Sym)
if button > 0 {
emulator.Input(button, true)
}
case sdl.KEYUP:
keyevent, ok := event.(*sdl.KeyboardEvent)
if !ok {
return errors.New("invalid keyboard event")
}
button := key2Button(keyevent.Keysym.Sym)
if button > 0 {
emulator.Input(button, false)
}
}
event = sdl.PollEvent()
}
for range TICKS_PER_FRAME {
emulator.Tick()
}
emulator.TickTimers()
drawScreen(emulator, renderer)
elapsed := sdl.GetTicks64() - start
if MILLISECONDS_PER_FRAME > elapsed {
sdl.Delay(uint32(MILLISECONDS_PER_FRAME - elapsed))
}
}
return out
}
func drawScreen(emulator *internal.Emulator, renderer *sdl.Renderer) error {
if err := renderer.SetDrawColor(0, 0, 0, 255); err != nil {
return err
}
if err := renderer.Clear(); err != nil {
return err
}
screenBuffer := emulator.ScreenBuffer()
if err := renderer.SetDrawColor(255, 255, 255, 255); err != nil {
return err
}
for i, pixel := range screenBuffer {
if pixel {
x := i % internal.SCREEN_WIDTH
y := i / internal.SCREEN_WIDTH
rect := sdl.Rect{
X: int32(x) * SCALE,
Y: int32(y) * SCALE,
W: SCALE,
H: SCALE,
}
if err := renderer.FillRect(&rect); err != nil {
return err
}
}
}
renderer.Present()
return nil
}
func key2Button(key sdl.Keycode) int {
switch key {
case sdl.K_1:
return 0x1
case sdl.K_2:
return 0x2
case sdl.K_3:
return 0x3
case sdl.K_4:
return 0xC
case sdl.K_q:
return 0x4
case sdl.K_w:
return 0x5
case sdl.K_e:
return 0x6
case sdl.K_r:
return 0xD
case sdl.K_a:
return 0x7
case sdl.K_s:
return 0x8
case sdl.K_d:
return 0x9
case sdl.K_f:
return 0xE
case sdl.K_z:
return 0xA
case sdl.K_x:
return 0x0
case sdl.K_c:
return 0xB
case sdl.K_v:
return 0xF
}
return -1
}