Skip to content

Commit

Permalink
Set screen size limit
Browse files Browse the repository at this point in the history
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
  • Loading branch information
elgopher committed Dec 2, 2022
1 parent 5cef81a commit d48ca5d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
34 changes: 34 additions & 0 deletions pi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package pi_test

import (
_ "embed"
"fmt"
"strconv"
"testing"
"testing/fstest"
Expand Down Expand Up @@ -129,6 +130,39 @@ func TestSetScreenSize(t *testing.T) {
}
})

t.Run("should panic when total number of pixels is higher than 65536", func(t *testing.T) {
tests := []struct{ w, h int }{
{w: 65537, h: 1},
{w: 1, h: 65537},
}

for _, test := range tests {
t.Run(fmt.Sprintf("%dx%d", test.w, test.h), func(t *testing.T) {
pi.Reset()
assert.Panics(t, func() {
pi.SetScreenSize(test.w, test.h)
})
})
}
})

t.Run("should not panic when total number of pixels is lower/equal than 65536", func(t *testing.T) {
tests := []struct{ w, h int }{
{w: 1024, h: 64},
{w: 256, h: 256},
{w: 320, h: 200},
}

for _, test := range tests {
t.Run(fmt.Sprintf("%dx%d", test.w, test.h), func(t *testing.T) {
pi.Reset()
assert.NotPanics(t, func() {
pi.SetScreenSize(test.w, test.h)
})
})
}
})

t.Run("should initialize screen data", func(t *testing.T) {
pi.Reset()
// when
Expand Down
20 changes: 14 additions & 6 deletions screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,21 @@ type Position struct {
X, Y int
}

func SetScreenSize(w, h int) {
if w <= 0 {
panic(fmt.Sprintf("screen width %d is not greather than 0", w))
const maxScreenSize = 1024 * 64

// SetScreenSize sets the screen size to specified resolution. The maximum number of pixels is 65536 (64KB).
// Will panic if screen size is too big or width/height are <= 0.
func SetScreenSize(width, height int) {
if width <= 0 {
panic(fmt.Sprintf("screen width %d is not greather than 0", width))
}
if h <= 0 {
panic(fmt.Sprintf("screen height %d is not greather than 0", h))
if height <= 0 {
panic(fmt.Sprintf("screen height %d is not greather than 0", height))
}

if width*height > maxScreenSize {
panic(fmt.Sprintf("number of pixels for screen resolution %dx%d is higher than maximum %d. Please use smaller screen.", width, height, maxScreenSize))
}

screen = NewPixMap(w, h)
screen = NewPixMap(width, height)
}

0 comments on commit d48ca5d

Please sign in to comment.