Skip to content

Commit d48ca5d

Browse files
committed
Set screen size limit
Limit the maximum screen size to 65536 (64KB) for following reasons: * bigger screens are not good for pixel-perfect programming * the more pixels, the longer it takes to create game assets * PI uses CPU for screen manipulation, which means it is slow for high resolutions such as FullHD or 4K
1 parent 5cef81a commit d48ca5d

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

pi_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package pi_test
55

66
import (
77
_ "embed"
8+
"fmt"
89
"strconv"
910
"testing"
1011
"testing/fstest"
@@ -129,6 +130,39 @@ func TestSetScreenSize(t *testing.T) {
129130
}
130131
})
131132

133+
t.Run("should panic when total number of pixels is higher than 65536", func(t *testing.T) {
134+
tests := []struct{ w, h int }{
135+
{w: 65537, h: 1},
136+
{w: 1, h: 65537},
137+
}
138+
139+
for _, test := range tests {
140+
t.Run(fmt.Sprintf("%dx%d", test.w, test.h), func(t *testing.T) {
141+
pi.Reset()
142+
assert.Panics(t, func() {
143+
pi.SetScreenSize(test.w, test.h)
144+
})
145+
})
146+
}
147+
})
148+
149+
t.Run("should not panic when total number of pixels is lower/equal than 65536", func(t *testing.T) {
150+
tests := []struct{ w, h int }{
151+
{w: 1024, h: 64},
152+
{w: 256, h: 256},
153+
{w: 320, h: 200},
154+
}
155+
156+
for _, test := range tests {
157+
t.Run(fmt.Sprintf("%dx%d", test.w, test.h), func(t *testing.T) {
158+
pi.Reset()
159+
assert.NotPanics(t, func() {
160+
pi.SetScreenSize(test.w, test.h)
161+
})
162+
})
163+
}
164+
})
165+
132166
t.Run("should initialize screen data", func(t *testing.T) {
133167
pi.Reset()
134168
// when

screen.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,21 @@ type Position struct {
231231
X, Y int
232232
}
233233

234-
func SetScreenSize(w, h int) {
235-
if w <= 0 {
236-
panic(fmt.Sprintf("screen width %d is not greather than 0", w))
234+
const maxScreenSize = 1024 * 64
235+
236+
// SetScreenSize sets the screen size to specified resolution. The maximum number of pixels is 65536 (64KB).
237+
// Will panic if screen size is too big or width/height are <= 0.
238+
func SetScreenSize(width, height int) {
239+
if width <= 0 {
240+
panic(fmt.Sprintf("screen width %d is not greather than 0", width))
237241
}
238-
if h <= 0 {
239-
panic(fmt.Sprintf("screen height %d is not greather than 0", h))
242+
if height <= 0 {
243+
panic(fmt.Sprintf("screen height %d is not greather than 0", height))
244+
}
245+
246+
if width*height > maxScreenSize {
247+
panic(fmt.Sprintf("number of pixels for screen resolution %dx%d is higher than maximum %d. Please use smaller screen.", width, height, maxScreenSize))
240248
}
241249

242-
screen = NewPixMap(w, h)
250+
screen = NewPixMap(width, height)
243251
}

0 commit comments

Comments
 (0)