Skip to content

Commit

Permalink
Feature: added ability to resize each sprite/tile in pixels.
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-wannacott committed Oct 13, 2023
1 parent 6b99564 commit b8df848
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/LeeWannacott/gontage

go 1.21

require github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // direct
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type spritesheet struct {
sprite_width int
amount_of_sprites []int
hframes int
sprite_resize_px int
}
type folderInfo struct {
sub_folder_path string
Expand All @@ -41,6 +42,7 @@ func main() {
}
sprite_source_folder := flag.String("f", "", "Folder name that contains sprites.")
hframes := flag.Int("hframes", 8, "Amount of horizontal sprites you want in your spritesheet: default 8.")
sprite_resize_px := flag.Int("sprite_resize", 0, "Resize each tile/sprite in spritesheet to the pixel value provided.")
parent_folder_path := flag.String("mf", "", "multiple folders: path should be parent folder containing sub folders that contain folders with sprites/images in them. Refer to test_multi for example structure.")
useMontage := flag.Bool("montage", false, "Use montage with -mf instead of gontage (if installed)")
help := flag.Bool("h", false, "Display help")
Expand All @@ -53,7 +55,7 @@ func main() {
}

if *sprite_source_folder != "" {
gontage.Gontage(*sprite_source_folder, hframes)
gontage.Gontage(*sprite_source_folder, hframes, *sprite_resize_px)
} else {
var wg sync.WaitGroup
if parent_folder_path != nil {
Expand All @@ -78,6 +80,7 @@ func main() {
sprite_width: sprite_width,
amount_of_sprites: amount_of_sprites,
hframes: *hframes,
sprite_resize_px: *sprite_resize_px,
}
cli := cliOptions{
useMontage: *useMontage,
Expand Down Expand Up @@ -117,7 +120,7 @@ func call_gontage_or_montage(i int, spritesheet spritesheet, folder folderInfo,
}
fmt.Println(string(out), filepath.Join(folder.sub_folder_path_gontage, folder.folder_name)+"/*", sprite_name)
} else {
gontage.Gontage(filepath.Join(folder.sub_folder_path_gontage, folder.folder_name), &spritesheet.hframes)
gontage.Gontage(filepath.Join(folder.sub_folder_path_gontage, folder.folder_name), &spritesheet.hframes, spritesheet.sprite_resize_px)
}
}

Expand Down
18 changes: 15 additions & 3 deletions src/gontage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"runtime"
"sync"
"time"

"github.com/nfnt/resize"
)

type drawingInfo struct {
Expand All @@ -24,7 +26,7 @@ type drawingInfo struct {
spritesheet draw.Image
}

func Gontage(sprite_source_folder string, hframes *int) {
func Gontage(sprite_source_folder string, hframes *int, sprite_resize_px int) {
start := time.Now()
pwd, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -94,10 +96,20 @@ func Gontage(sprite_source_folder string, hframes *int) {
if err != nil {
panic(err)
}

encoder := png.Encoder{CompressionLevel: png.BestSpeed}
if err = encoder.Encode(f, spritesheet); err != nil {
log.Printf("failed to encode: %v", err)
if sprite_resize_px != 0 {
resized_spritesheet := resize.Resize(uint(*hframes*sprite_resize_px), uint(int(vframes)*sprite_resize_px),
spritesheet, resize.Lanczos3)
if err = encoder.Encode(f, resized_spritesheet); err != nil {
log.Printf("failed to encode: %v", err)
}
} else {
if err = encoder.Encode(f, spritesheet); err != nil {
log.Printf("failed to encode: %v", err)
}
}

f.Close()
fmt.Println(spritesheet_name, ": ", time.Since(start))
}
Expand Down

0 comments on commit b8df848

Please sign in to comment.