-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (56 loc) · 1.52 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
package main
import (
"os"
"log"
"fmt"
"strconv"
"path/filepath"
nes "github.com/kazzmir/nes/lib"
"github.com/kazzmir/nes/test/screenshot"
"image/png"
)
func removeExtension(path string) string {
extension := filepath.Ext(path)
return path[0:len(path)-len(extension)]
}
func saveScreen(rom string, maxCycles int64, screen nes.VirtualScreen) error {
romName := removeExtension(filepath.Base(rom))
imagePath := fmt.Sprintf("images/%v-%v.png", romName, maxCycles)
out, err := os.Create(imagePath)
if err != nil {
return err
}
defer out.Close()
err = png.Encode(out, screenshot.ScreenToImage(screen))
if err != nil {
return err
}
log.Printf("Saved screenshot to %v", imagePath)
return nil
}
func main(){
log.SetFlags(log.Lshortfile | log.Lmicroseconds)
if len(os.Args) <= 2 {
log.Printf("Give a .nes file and a number of cycles to run for")
return
}
path := os.Args[1]
cycles := os.Args[2]
parsed, err := strconv.ParseInt(cycles, 10, 64)
if err != nil {
log.Printf("Could not parse cycles as an integer '%v': %v", cycles, err)
return
}
if parsed <= 0 {
log.Printf("Give a positive number of cycles: %v", parsed)
return
}
buffer, err := screenshot.Run(path, parsed)
if err != nil {
log.Printf("Error: %v", err)
}
err = saveScreen(path, parsed, buffer)
if err != nil {
log.Printf("Could not save screenshot: %v", err)
}
}