Skip to content

Commit

Permalink
Merge pull request #222 from oakmound/feat/spritesheetbounds
Browse files Browse the repository at this point in the history
Feat/spritesheetbounds
  • Loading branch information
Implausiblyfun authored Jan 14, 2024
2 parents b55e474 + f889262 commit d140a61
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
9 changes: 8 additions & 1 deletion render/loadsheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,15 @@ func MakeSheet(rgba *image.RGBA, cellSize intgeom.Point2) (*Sheet, error) {
bounds := rgba.Bounds()

sheetW := bounds.Max.X / w
wMod := bounds.Max.X % w
if wMod != 0 {
return nil, oakerr.InvalidInput{InputName: "cellSize.X"}
}
sheetH := bounds.Max.Y / h

hMod := bounds.Max.Y % h
if hMod != 0 {
return nil, oakerr.InvalidInput{InputName: "cellSize.Y"}
}
if sheetW < 1 || sheetH < 1 {
return nil, oakerr.InvalidInput{InputName: "cellSize"}
}
Expand Down
24 changes: 24 additions & 0 deletions render/loadsheet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package render

import (
"errors"
"image"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -72,3 +73,26 @@ func TestMakeSheet_BadDimensions(t *testing.T) {
t.Fatalf("expected invalid height, got %v", expected.InputName)
}
}

func TestMakeSheet_BadMod(t *testing.T) {
rgba := &image.RGBA{
Rect: image.Rect(0, 0, 10, 10),
}
_, err := MakeSheet(rgba, intgeom.Point2{4, 5})
expected := &oakerr.InvalidInput{}
if !errors.As(err, expected) {
t.Fatalf("expected invalid input error, got %v", err)
}
if expected.InputName != "cellSize.X" {
t.Fatalf("expected invalid width, got %v", expected.InputName)
}

_, err = MakeSheet(rgba, intgeom.Point2{5, 4})
expected = &oakerr.InvalidInput{}
if !errors.As(err, expected) {
t.Fatalf("expected invalid input error, got %v", err)
}
if expected.InputName != "cellSize.Y" {
t.Fatalf("expected invalid height, got %v", expected.InputName)
}
}

0 comments on commit d140a61

Please sign in to comment.