summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go192
1 files changed, 192 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..abde00f
--- /dev/null
+++ b/main.go
@@ -0,0 +1,192 @@
+package main
+
+import (
+ "encoding/json"
+ "fmt"
+ "io/ioutil"
+ "math"
+ "os"
+)
+
+type Root struct {
+ Path string
+ Name string
+ NName string
+ ImageWidth float64
+ ImageHeight float64
+ TileWidth float64
+ TileHeight float64
+ Margin float64
+ Columns float64
+ TileCount float64
+ Tiles []Tile
+}
+
+type Tile struct {
+ Id float64
+ Type string
+ Probability float64
+ Frames []Frame
+}
+
+type Frame struct {
+ Duration float64
+ TileId float64
+}
+
+func main() {
+ out := "#include \"tsj2c.h\"\n"
+ names := []string{}
+ for i := 1; i < len(os.Args); i++ {
+ result, err := process(os.Args[i])
+ if err != nil {
+ panic(err)
+ }
+ out += fmt.Sprint(result)
+ names = append(names, result.Name)
+ }
+ out += "const Tsj2cSet *tsj2c_sets[]={"
+ for i := 0; i < len(names); i++ {
+ out += "&" + names[i] + ","
+ }
+ out += "0};\n"
+ fmt.Print(out)
+}
+
+func process(path string) (Root, error) {
+ content, err := ioutil.ReadFile(path)
+ if err != nil {
+ panic(err)
+ }
+
+ var data map[string]interface{}
+ err = json.Unmarshal(content, &data)
+ if err != nil {
+ panic(err)
+ }
+
+ var root = Root{
+ Path: path,
+ Name: "",
+ NName: data["name"].(string),
+ ImageWidth: data["imagewidth"].(float64),
+ ImageHeight: data["imageheight"].(float64),
+ TileWidth: data["tilewidth"].(float64),
+ TileHeight: data["tileheight"].(float64),
+ Margin: data["margin"].(float64),
+ Columns: data["columns"].(float64),
+ TileCount: data["tilecount"].(float64),
+ Tiles: []Tile{},
+ }
+ for i := 0; i < len(root.Path); i++ {
+ chr := root.Path[i]
+ if (chr >= '0' && chr <= '9') || (chr >= 'A' && chr <= 'Z') ||
+ (chr >= 'a' && chr <= 'z') {
+ root.Name += string(chr)
+ } else {
+ root.Name += "_"
+ }
+ }
+
+ tiles := data["tiles"].([]interface{})
+ for i := 0; i < len(tiles); i++ {
+ root.Tiles = append(root.Tiles, NewTile(tiles[i].(map[string]interface{})))
+ }
+ return root, nil
+}
+
+func (this Root) String() string {
+ out := "static const Tsj2cSet " + this.Name + "={"
+ out += "\"" + this.NName + "\","
+ out += "\"" + this.Path + "\","
+ out += fmt.Sprint(this.ImageWidth) + ","
+ out += fmt.Sprint(this.ImageHeight) + ","
+ out += fmt.Sprint(this.TileWidth) + ","
+ out += fmt.Sprint(this.TileHeight) + ","
+ out += fmt.Sprint(this.Margin) + ","
+ out += fmt.Sprint(this.Columns) + ","
+ out += fmt.Sprint(this.TileCount) + ","
+ out += fmt.Sprint(len(this.Tiles)) + ","
+
+ out += "(const Tsj2cTile[]){"
+ for i := 0; i < len(this.Tiles); i++ {
+ out += this.Tiles[i].String() + ","
+ }
+ out += "}"
+
+ out += "};\n"
+ return out
+}
+
+func (this Tile) String() string {
+ out := "{"
+ out += fmt.Sprint(this.Id) + ","
+ if this.Type != "" {
+ out += "\"" + this.Type + "\","
+ } else {
+ out += "0,"
+ }
+ if math.IsNaN(this.Probability) {
+ out += "1,"
+ } else {
+ out += fmt.Sprint(this.Probability) + ","
+ }
+
+ out += fmt.Sprint(len(this.Frames)) + ","
+ if len(this.Frames) > 0 {
+ out += "(const Tsj2cFrame[]){"
+ for i := 0; i < len(this.Frames); i++ {
+ if i > 0 {
+ out += ","
+ }
+ out += this.Frames[i].String()
+ }
+ out += "}"
+ } else {
+ out += "0"
+ }
+
+ out += "}"
+ return out
+}
+
+func (this Frame) String() string {
+ out := "{"
+ out += fmt.Sprint(this.Duration) + ","
+ out += fmt.Sprint(this.TileId)
+ out += "}"
+ return out
+}
+
+func NewTile(jtile map[string]interface{}) Tile {
+ tile := Tile{
+ Id: jtile["id"].(float64),
+ Type: "",
+ Probability: math.NaN(),
+ Frames: []Frame{},
+ }
+ v, ok := jtile["type"]
+ if ok {
+ tile.Type = v.(string)
+ }
+ v, ok = jtile["probability"]
+ if ok {
+ tile.Probability = v.(float64)
+ }
+ v, ok = jtile["animation"]
+ if ok {
+ frames := v.([]interface{})
+ for i := 0; i < len(frames); i++ {
+ frame := frames[i].(map[string]interface{})
+ tile.Frames = append(tile.Frames, NewFrame(frame))
+ }
+ }
+ return tile
+}
+
+func NewFrame(src map[string]interface{}) Frame {
+ return Frame{
+ Duration: src["duration"].(float64),
+ TileId: src["tileid"].(float64),
+ }
+}