Skip to content

Commit 09393da

Browse files
committed
Limit the size of sprite-sheet to 64KB
Screen size is already limited. It makes sense to limit the sprite-sheet too. Creating sprite-sheets bigger than 64KB takes a lot of time, and really does not make sense to have bigger one to create low-res games. If game developer wants to create huge pictures,
1 parent 5e9bae5 commit 09393da

File tree

5 files changed

+24
-3
lines changed

5 files changed

+24
-3
lines changed

pi.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ var GameLoopStopped bool
7575
// form of mapping: index number->RGB color. Image must have an indexed color mode,
7676
// because Pi loads the palette from the sprite-sheet.png file itself. Please use a pixel-art editor
7777
// which supports indexed color mode, such as Aseprite (paid) or LibreSprite (free). Sprite-sheet
78-
// width and height must be multiplication of 8.
78+
// width and height must be multiplication of 8. Each sprite is 8x8 pixels. The maximum number of pixels
79+
// is 65536 (64KB).
7980
//
8081
// custom-font.png must also have an indexed color mode. Color with index 0 is treated as background.
8182
// Any other color as foreground. The size of the image is fixed. It must be 128x128. Each char is 8x8.

pi_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ func TestLoad(t *testing.T) {
225225
})
226226
})
227227
})
228+
229+
t.Run("should panic when sprite-sheet total number of pixels is higher than 65536", func(t *testing.T) {
230+
pi.Reset()
231+
assert.Panics(t, func() {
232+
pi.UseEmptySpriteSheet(264, 256)
233+
})
234+
})
228235
}
229236

230237
func TestTime(t *testing.T) {

screen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (p *Position) Reset() {
180180
p.Y = 0
181181
}
182182

183-
const maxScreenSize = 1024 * 64
183+
const maxScreenSize = 65536
184184

185185
// SetScreenSize sets the screen size to specified resolution. The maximum number of pixels is 65536 (64KB).
186186
// Will panic if screen size is too big or width/height are <= 0.

sprite_sheet.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ func SprSheet() PixMap {
6060
return sprSheet.PixMap
6161
}
6262

63+
const maxSpriteSheetSize = 65536
64+
6365
func newSpriteSheet(w int, h int) spriteSheet {
6466
if w%8 != 0 || w == 0 {
6567
panic(fmt.Sprintf("sprite sheet width %d is not a multiplication of 8", w))
@@ -70,6 +72,10 @@ func newSpriteSheet(w int, h int) spriteSheet {
7072

7173
size := w * h
7274

75+
if size > maxSpriteSheetSize {
76+
panic(fmt.Sprintf("number of pixels for sprite-sheet resolution %dx%d is higher than maximum %d. Please use a smaller one.", w, h, maxSpriteSheetSize))
77+
}
78+
7379
return spriteSheet{
7480
PixMap: NewPixMap(w, h),
7581
numberOfSprites: size / (SpriteWidth * SpriteHeight),
@@ -78,7 +84,7 @@ func newSpriteSheet(w int, h int) spriteSheet {
7884
}
7985

8086
// UseEmptySpriteSheet initializes empty sprite-sheet with given size. Could be used
81-
// when you don't have sprite-sheet.png in resources.
87+
// when you don't have sprite-sheet.png in resources. The maximum number of pixels is 65536 (64KB).
8288
func UseEmptySpriteSheet(w, h int) {
8389
sprSheet = newSpriteSheet(w, h)
8490
}

sprite_sheet_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,11 @@ func TestUseEmptySpriteSheet(t *testing.T) {
4747
expectedSpriteSheetData := make([]byte, 16*8)
4848
assert.Equal(t, expectedSpriteSheetData, pi.SprSheet().Pix())
4949
})
50+
51+
t.Run("should panic when total number of pixels is higher than 65536", func(t *testing.T) {
52+
pi.Reset()
53+
assert.Panics(t, func() {
54+
pi.UseEmptySpriteSheet(256, 264)
55+
})
56+
})
5057
}

0 commit comments

Comments
 (0)