-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
179 lines (148 loc) · 3.56 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
175
176
177
178
179
package main
import (
"errors"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"time"
"github.com/gdamore/tcell/v2"
"github.com/urfave/cli"
"github.com/valerio/go-jeebie/jeebie"
)
const (
// Game Boy screen dimensions
width = 160
height = 144
// Since terminal characters are taller than wide, we'll scale the width more
// to maintain approximate aspect ratio
scaleX = 2 // Each pixel becomes 2 characters wide
scaleY = 1 // Each pixel becomes 1 character tall
// Frame timing (Game Boy runs at ~59.7 FPS)
frameTime = time.Second / 60
)
// Characters to represent different shades of gray
// From darkest to lightest.
var shadeChars = []rune{'█', '▓', '▒', '░'}
type TerminalRenderer struct {
screen tcell.Screen
emulator *jeebie.Emulator
running bool
}
func NewTerminalRenderer(emu *jeebie.Emulator) (*TerminalRenderer, error) {
screen, err := tcell.NewScreen()
if err != nil {
return nil, fmt.Errorf("failed to initialize terminal: %v", err)
}
if err := screen.Init(); err != nil {
return nil, fmt.Errorf("failed to initialize terminal: %v", err)
}
return &TerminalRenderer{
screen: screen,
emulator: emu,
running: true,
}, nil
}
func (t *TerminalRenderer) Run() error {
defer func() {
slog.Info("Finishing terminal")
t.screen.Fini()
}()
// Set up screen
t.screen.SetStyle(tcell.StyleDefault.
Background(tcell.ColorBlack).
Foreground(tcell.ColorWhite))
t.screen.Clear()
// Handle input in a separate goroutine
go t.handleInput()
// Main render loop
ticker := time.NewTicker(frameTime)
defer ticker.Stop()
// catch SIGINT and SIGTERM
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
for t.running {
select {
case <-ticker.C:
t.emulator.RunUntilFrame()
t.render()
t.screen.Show()
case <-signals:
t.running = false
slog.Info("Received signal to stop")
return nil
}
}
return nil
}
func (t *TerminalRenderer) handleInput() {
for t.running {
ev := t.screen.PollEvent()
switch ev := ev.(type) {
case *tcell.EventKey:
switch ev.Key() {
case tcell.KeyEscape:
t.running = false
return
}
case *tcell.EventResize:
t.screen.Sync()
}
}
}
func (t *TerminalRenderer) render() {
fb := t.emulator.GetCurrentFrame()
frame := fb.ToSlice()
// Clear screen with background color
t.screen.Clear()
// Render each pixel
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
// Get pixel value (assuming it's a 32-bit color where higher values = lighter)
pixel := frame[x*height+y]
// Convert to shade index (4 shades, so divide by 64 to get 0-3)
shade := 3 - (pixel>>24)/64 // Invert so higher values = darker
if shade > 3 {
shade = 3
}
// Draw scaled pixel
style := tcell.StyleDefault.Foreground(tcell.ColorWhite)
char := shadeChars[shade]
// Draw the character repeated scaleX times
screenX := x * scaleX
screenY := y * scaleY
for sx := 0; sx < scaleX; sx++ {
t.screen.SetContent(screenX+sx, screenY, char, nil, style)
}
}
}
}
func main() {
app := cli.NewApp()
app.Name = "Jeebie"
app.Description = "A simple gameboy emulator"
app.Action = runEmulator
app.Run(os.Args)
}
func runEmulator(c *cli.Context) error {
path := ""
if c.NArg() > 0 {
path = c.Args().First()
}
var emu *jeebie.Emulator
var err error
if path == "" {
slog.Error("no ROM path provided")
return errors.New("no ROM path provided")
}
emu, err = jeebie.NewWithFile(path)
if err != nil {
return err
}
renderer, err := NewTerminalRenderer(emu)
if err != nil {
return err
}
return renderer.Run()
}