-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
166 lines (131 loc) · 3.05 KB
/
main.go
File metadata and controls
166 lines (131 loc) · 3.05 KB
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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
"os"
"strings"
)
type Options struct {
Format string
}
type Bitsy struct {
Name string
Description string
Comments []string
Tiles []Tile
}
type Tile struct {
Name string
Data []string
}
type Std struct {
in io.Reader
out io.Writer
err io.Writer
args []string
}
func main() {
std := Std{
in: os.Stdin,
out: os.Stdout,
err: os.Stderr,
args: os.Args,
}
if err := run(std); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
func run(std Std) error {
o, err := parseOptions(std.err, std.args)
if err != nil {
return err
}
b, err := parseBitsy(std.in)
if err != nil {
return err
}
switch o.Format {
case "json":
return json.NewEncoder(std.out).Encode(b)
case "w4":
return w4(std.out, b)
default:
return fmt.Errorf("unhandled output format: %q", o.Format)
}
}
// Output Zig code, hopefully compatible with w4
func w4(w io.Writer, b Bitsy) error {
fmt.Fprintf(w, "// Code generated by bitsy-to-w4-sprites; DO NOT EDIT.\n\n")
for _, c := range b.Comments {
fmt.Fprintf(w, "// %s\n", c)
}
fmt.Fprintf(w, "\npub const %s = enum(u8) {\n", b.Name)
var tname string
for _, t := range b.Tiles {
fmt.Fprintln(w, " "+t.Name+",")
tname = t.Name
}
fmt.Fprintln(w, "")
fmt.Fprintln(w, " pub const Count: i32 = @intCast(@intFromEnum("+b.Name+"."+tname+") + 1);")
fmt.Fprintln(w, "")
fmt.Fprintf(w, " pub fn sprite(self: %s) [8]u8 {\n", b.Name)
fmt.Fprintf(w, " return switch (self) {\n")
for _, t := range b.Tiles {
fmt.Fprintf(w, " .%s => [8]u8{\n", t.Name)
for _, d := range t.Data {
fmt.Fprintf(w, " 0b%s,\n", d)
}
fmt.Fprintln(w, " },")
}
fmt.Fprintln(w, " };")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, "};")
return nil
}
func parseOptions(output io.Writer, args []string) (Options, error) {
var o Options
name := args[0]
rest := args[1:]
flags := flag.NewFlagSet(name, flag.ContinueOnError)
flags.SetOutput(output)
flags.StringVar(&o.Format, "format", "w4", "Format of output [w4 or json]")
if err := flags.Parse(rest); err != nil {
return o, err
}
return o, nil
}
func parseBitsy(r io.Reader) (Bitsy, error) {
var lines []string
s := bufio.NewScanner(r)
for s.Scan() {
if t := s.Text(); t != "" {
lines = append(lines, s.Text())
}
}
var b Bitsy
n := -1
for _, line := range lines {
switch {
case strings.HasPrefix(line, "#"):
b.Comments = append(b.Comments, strings.TrimPrefix(line, "# "))
case strings.HasPrefix(line, "TIL"):
n++
b.Tiles = append(b.Tiles, Tile{})
case strings.HasPrefix(line, "NAME"):
b.Tiles[n].Name = strings.TrimPrefix(line, "NAME ")
case strings.HasPrefix(line, "1") || strings.HasPrefix(line, "0"):
b.Tiles[n].Data = append(b.Tiles[n].Data, line)
default:
return Bitsy{}, fmt.Errorf("UNHANDLED: %q\n", line)
}
}
if len(b.Comments) == 0 {
return Bitsy{}, fmt.Errorf("no comments in Bitsy")
}
b.Name, b.Description, _ = strings.Cut(b.Comments[0], " - ")
return b, nil
}