-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (73 loc) · 1.96 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
package main
import (
"flag"
"image"
"image/color"
"image/gif"
"log"
"os"
"github.com/kunalpowar/gochip8/pkg/chip8"
)
var rom = flag.String("rom", "roms/Chip8 Picture.ch8", "-rom path_to_rom")
func main() {
flag.Parse()
d := GIFDisplay{currentFrame: newPalettedImage()}
c := chip8.New(&d, nil, nil)
f, err := os.Open(*rom)
if err != nil {
log.Fatalf("could not open rom: %v", err)
}
defer f.Close()
c.LoadROM(f)
c.RunCycles(1000)
out, err := os.Create("out.gif")
if err != nil {
log.Fatalf("could not create gif: %v", err)
}
defer out.Close()
if err := gif.EncodeAll(out, &gif.GIF{Image: d.images, Delay: d.delays}); err != nil {
log.Fatalf("could not encode to gif: %v", err)
}
}
// GIFDisplay creates a gif frame for every display update
// At the end of limited cycles, a gif file will be encoded
// with all the frames.
type GIFDisplay struct {
images []*image.Paletted
delays []int
currentFrame *image.Paletted
}
func newPalettedImage() *image.Paletted {
palette := []color.Color{color.Black, color.White}
rect := image.Rect(0, 0, 64, 32)
return image.NewPaletted(rect, palette)
}
// DrawFrame will add the currentframe as an image to the list
// to be encoded as gif the end.
func (d *GIFDisplay) DrawFrame() {
if d.currentFrame == nil {
return
}
d.images = append(d.images, d.currentFrame)
d.delays = append(d.delays, 0)
img := newPalettedImage()
rect := img.Bounds()
for row := 0; row < rect.Dy(); row++ {
for col := 0; col < rect.Dx(); col++ {
img.Set(col, row, d.currentFrame.At(col, row))
}
}
d.currentFrame = img
}
// ClearPixel will set the pixel at location to black.
func (d *GIFDisplay) ClearPixel(x, y int) {
d.currentFrame.Set(x, y, color.Black)
}
// SetPixel will set the pixel at location to white.
func (d *GIFDisplay) SetPixel(x, y int) {
d.currentFrame.Set(x, y, color.White)
}
// ClearAll will clear all pixels by using a new paletted image.
func (d *GIFDisplay) ClearAll() {
d.currentFrame = newPalettedImage()
}