-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement correct sprite priority resolution
- Loading branch information
Showing
6 changed files
with
138 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package gb | ||
|
||
import ( | ||
"fmt" | ||
"image" | ||
"image/color" | ||
"image/png" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestSpritePriority runs the mooneye sprite_priority.gb test rom and asserts that the | ||
// output frame matches the expected image. | ||
func TestSpritePriority(t *testing.T) { | ||
// Takes about 10 frames to render the sprite priority image | ||
const maxPPUIterations = 10 | ||
|
||
// Override the palette with the colours in the expected image | ||
Palettes[CurrentPalette] = [][]byte{ | ||
{3, 3, 3}, | ||
{2, 2, 3}, | ||
{1, 1, 1}, // not used in expected image | ||
{0, 0, 0}, | ||
} | ||
|
||
// Map of colours in the image to color in the palette | ||
var imageMap = map[color.Color]byte{ | ||
color.Gray{Y: 255}: 3, | ||
color.Gray{Y: 111}: 2, | ||
color.Gray{Y: 0}: 0, | ||
} | ||
|
||
// Load the test ROM and iterate a few frames to load the image | ||
gb, err := NewGameboy("./../../roms/mooneye/runnable/sprite_priority.gb") | ||
require.NoError(t, err, "error in init gb %v", err) | ||
for i := 0; i < maxPPUIterations; i++ { | ||
gb.Update() | ||
} | ||
|
||
// Load the expected output image | ||
img, err := loadImage("../../roms/mooneye/runnable/sprite_priority-expected.png") | ||
if err != nil { | ||
t.Fatalf("Could not open expected image: %v", err) | ||
} | ||
|
||
// Iterate over the image and assert each pixel matches the expected image | ||
for x := 0; x < ScreenWidth; x++ { | ||
for y := 0; y < ScreenHeight; y++ { | ||
actual := gb.PreparedData[x][y] | ||
expected, ok := imageMap[img.At(x, y)] | ||
require.True(t, ok, "unexpected colour in expected image: %v", img.At(x, y)) | ||
require.Equal(t, expected, actual[0], "incorrect pixel at X:%v Y:%x", x, y) | ||
} | ||
} | ||
} | ||
|
||
// Load a PNG image | ||
func loadImage(filename string) (image.Image, error) { | ||
file, err := os.Open(filename) | ||
if err != nil { | ||
return nil, fmt.Errorf("opening image: %v", err) | ||
} | ||
defer file.Close() | ||
|
||
img, err := png.Decode(file) | ||
if err != nil { | ||
return nil, fmt.Errorf("decoding image: %v", err) | ||
} | ||
return img, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.