func main() { if len(os.Args) != 2 { fmt.Println("please provide an XCF as the first parameter") return } canvas, err := xcf.LoadFromFile(os.Args[1]) if err != nil { panic(err) } for _, layer := range canvas.Layers { saveImageAsPng(layer, "./"+layer.Name+"_layer.png") } saveImageAsPng(composeLayers(canvas), "./composed_layer.png") }
func main() { resources := blob.New() textureAtlas := atlas.New(2048) gophette, err := xcf.LoadFromFile("./gophette.xcf") check(err) barney, err := xcf.LoadFromFile("./barney.xcf") check(err) // create the collision information for Gophette and Barney addCollisionInfo := func(canvas xcf.Canvas, id string) { collision := canvas.GetLayerByName("collision") left, top := findTopLeftNonTransparentPixel(collision) right, bottom := findBottomRightNonTransparentPixel(collision) // scale the collision rect just like the images left = int(0.5 + scale*float64(left)) top = int(0.5 + scale*float64(top)) right = int(0.5 + scale*float64(right)) bottom = int(0.5 + scale*float64(bottom)) width, height := right-left+1, bottom-top+1 r := rect{int32(left), int32(top), int32(width), int32(height)} buffer := bytes.NewBuffer(nil) check(binary.Write(buffer, byteOrder, &r)) resources.Append(id, buffer.Bytes()) } addCollisionInfo(gophette, "hero collision") addCollisionInfo(barney, "barney collision") addImage := func(img image.Image, id string) { _, err := textureAtlas.Add(id, img) check(err) } // create the image resources for _, layer := range []string{ "jump", "run1", "run2", "run3", } { small := scaleImage(gophette.GetLayerByName(layer)) addImage(small, "gophette_left_"+layer) addImage(imaging.FlipH(small), "gophette_right_"+layer) } for _, layer := range []string{ "stand", "jump", "run1", "run2", "run3", "run4", "run5", "run6", } { smallLeft := scaleImage(barney.GetLayerByName("left_" + layer)) smallRight := scaleImage(barney.GetLayerByName("right_" + layer)) addImage(smallLeft, "barney_left_"+layer) addImage(smallRight, "barney_right_"+layer) } grass, err := xcf.LoadFromFile("./grass.xcf") check(err) for _, layer := range []string{ "grass left", "grass right", "grass center 1", "grass center 2", "grass center 3", } { addImage(grass.GetLayerByName(layer), layer) } grassLong, err := xcf.LoadFromFile("./grass_long.xcf") check(err) for _, layer := range []string{ "grass long 1", "grass long 2", "grass long 3", } { addImage(grassLong.GetLayerByName(layer), layer) } ground, err := xcf.LoadFromFile("./ground.xcf") check(err) for _, layer := range []string{ "ground left", "ground right", "ground center 1", "ground center 2", "ground center 3", } { addImage(ground.GetLayerByName(layer), layer) } groundLong, err := xcf.LoadFromFile("./ground_long.xcf") check(err) for _, layer := range []string{ "ground long 1", "ground long 2", } { addImage(groundLong.GetLayerByName(layer), layer) } rock, err := xcf.LoadFromFile("./rock.xcf") check(err) addImage(scaleImage(rock.GetLayerByName("rock")), "square rock") tree, err := xcf.LoadFromFile("./tree.xcf") check(err) smallTree := scaleImage(tree.GetLayerByName("small")) addImage(smallTree, "small tree") tree, err = xcf.LoadFromFile("./tree_big.xcf") check(err) bigTree := scaleImage(tree.GetLayerByName("big")) addImage(bigTree, "big tree") tree, err = xcf.LoadFromFile("./tree_huge.xcf") check(err) hugeTree := scaleImage(tree.GetLayerByName("huge")) addImage(hugeTree, "huge tree") cave, err := xcf.LoadFromFile("./cave.xcf") check(err) addImage(scaleImage(cave.GetLayerByName("cave back")), "cave back") addImage(scaleImage(cave.GetLayerByName("cave front")), "cave front") intro, err := xcf.LoadFromFile("./intro.xcf") check(err) addImage(scaleImageToFactor(intro.GetLayerByName("pc 1"), 0.67), "intro pc 1") addImage(scaleImageToFactor(intro.GetLayerByName("pc 2"), 0.67), "intro pc 2") addImage(scaleImageToFactor(intro.GetLayerByName("gophette"), 0.67), "intro gophette") { music, err := ioutil.ReadFile("./background_music.ogg") check(err) resources.Append("music", music) } { music, err := ioutil.ReadFile("./background_music.wav") check(err) resources.Append("music_wav", music) } for _, sound := range []string{ "win", "lose", "fall", "barney wins", "barney intro text", "whistle", "instructions", } { data, err := ioutil.ReadFile(sound + ".wav") check(err) resources.Append(sound, data) } resources.Append("atlas", imageToBytes(textureAtlas)) for _, sub := range textureAtlas.SubImages { resources.Append( sub.ID, toRectData(sub.Bounds().Sub(textureAtlas.Bounds().Min)), ) } resourceFile, err := os.Create("../resource/resources.blob") check(err) defer resourceFile.Close() resources.Write(resourceFile) }
func main() { resources := make(ResourceMap) constants := bytes.NewBuffer(nil) gophette, err := xcf.LoadFromFile("./gophette.xcf") check(err) barney, err := xcf.LoadFromFile("./barney.xcf") check(err) // create the collision information for Gophette and Barney addCollisionInfo := func(canvas xcf.Canvas, variable string) { collision := canvas.GetLayerByName("collision") left, top := findTopLeftNonTransparentPixel(collision) right, bottom := findBottomRightNonTransparentPixel(collision) // scale the collision rect just like the images left = int(0.5 + scale*float64(left)) top = int(0.5 + scale*float64(top)) right = int(0.5 + scale*float64(right)) bottom = int(0.5 + scale*float64(bottom)) width, height := right-left+1, bottom-top+1 line := fmt.Sprintf( "var %v = Rectangle{%v, %v, %v, %v}\n", variable, left, top, width, height, ) constants.WriteString(line) } addCollisionInfo(gophette, "HeroCollisionRect") addCollisionInfo(barney, "BarneyCollisionRect") // create the image resources for _, layer := range []string{ "jump", "run1", "run2", "run3", } { small := scaleImage(gophette.GetLayerByName(layer)) resources["gophette_left_"+layer] = imageToBytes(small) resources["gophette_right_"+layer] = imageToBytes(imaging.FlipH(small)) } for _, layer := range []string{ "stand", "jump", "run1", "run2", "run3", "run4", "run5", "run6", } { smallLeft := scaleImage(barney.GetLayerByName("left_" + layer)) smallRight := scaleImage(barney.GetLayerByName("right_" + layer)) resources["barney_left_"+layer] = imageToBytes(smallLeft) resources["barney_right_"+layer] = imageToBytes(smallRight) } grass, err := xcf.LoadFromFile("./grass.xcf") check(err) for _, layer := range []string{ "grass left", "grass right", "grass center 1", "grass center 2", "grass center 3", } { resources[layer] = imageToBytes(grass.GetLayerByName(layer)) } grassLong, err := xcf.LoadFromFile("./grass_long.xcf") check(err) for _, layer := range []string{ "grass long 1", "grass long 2", "grass long 3", } { resources[layer] = imageToBytes(grassLong.GetLayerByName(layer)) } ground, err := xcf.LoadFromFile("./ground.xcf") check(err) for _, layer := range []string{ "ground left", "ground right", "ground center 1", "ground center 2", "ground center 3", } { resources[layer] = imageToBytes(ground.GetLayerByName(layer)) } groundLong, err := xcf.LoadFromFile("./ground_long.xcf") check(err) for _, layer := range []string{ "ground long 1", "ground long 2", } { resources[layer] = imageToBytes(groundLong.GetLayerByName(layer)) } rock, err := xcf.LoadFromFile("./rock.xcf") check(err) resources["square rock"] = imageToBytes(scaleImage(rock.GetLayerByName("rock"))) tree, err := xcf.LoadFromFile("./tree.xcf") check(err) smallTree := scaleImage(tree.GetLayerByName("small")) resources["small tree"] = imageToBytes(smallTree) tree, err = xcf.LoadFromFile("./tree_big.xcf") check(err) bigTree := scaleImage(tree.GetLayerByName("big")) resources["big tree"] = imageToBytes(bigTree) tree, err = xcf.LoadFromFile("./tree_huge.xcf") check(err) hugeTree := scaleImage(tree.GetLayerByName("huge")) resources["huge tree"] = imageToBytes(hugeTree) cave, err := xcf.LoadFromFile("./cave.xcf") check(err) resources["cave back"] = imageToBytes(scaleImage(cave.GetLayerByName("cave back"))) resources["cave front"] = imageToBytes(scaleImage(cave.GetLayerByName("cave front"))) intro, err := xcf.LoadFromFile("./intro.xcf") check(err) resources["intro pc 1"] = imageToBytes(scaleImageToFactor(intro.GetLayerByName("pc 1"), 0.67)) resources["intro pc 2"] = imageToBytes(scaleImageToFactor(intro.GetLayerByName("pc 2"), 0.67)) resources["intro gophette"] = imageToBytes(scaleImageToFactor(intro.GetLayerByName("gophette"), 0.67)) // the music file is too big, breaks IDE /* music, err := ioutil.ReadFile("./background_music.ogg") check(err) resources["music"] = music */ for _, sound := range []string{ "win", "lose", "fall", "barney wins", "barney intro text", "whistle", "instructions", } { data, err := ioutil.ReadFile(sound + ".wav") check(err) resources[sound] = data } content := toGoFile(resources, string(constants.Bytes())) ioutil.WriteFile("../resource/resources.go", content, 0777) }