doodle/pkg/level/chunker_test.go

326 lines
7.1 KiB
Go
Raw Permalink Normal View History

package level_test
import (
WIP Texture Caching NOTICE: Chunk size set to 100 for visual testing! NOTICE: guitest references a bmp file that isn't checked in! BUGS REMAINING: - When scrolling the level in Edit Mode, some of the chunks will pop out of existence randomly. - When clicking-dragging to draw in Edit Mode, if the scroll position is not at 0,0 then the pixels drawn will be offset from the cursor. - These are to do with the Scroll position and chunk coordinate calc functions probably. Implements a texture caching interface to stop redrawing everything pixel by pixel on every frame. The texture caching workflow is briefly: - The uix.Canvas widget's Present() function iterates over the list of Chunk Coordinates that are visible inside of the current viewport (i.e. viewable on screen) - For each Chunk: - Make it render and/or return its cached Texture object. - Work out how much of the Chunk will be visible and how to crop the boxes for the Copy() - Copy the cached Texture instead of drawing all the pixels every time like we were doing before. - The Chunk.Texture() function that returns said Texture: - It calls Chunk.ToBitmap() to save a bitmap on disk. - It calls Engine.NewBitmap() to get a Texture it can hang onto. - It hangs onto the Texture and returns it on future calls. - Any call to Set() or Delete() a pixel will invalidate the cache (mark the Chunk "dirty") and Texture() will rebuild next call. The interface `render.Texturer` provides a way for rendering backends (SDL2, OpenGL) to transport a "texture" of their own kind without exposing the type details to the user. The interface `render.Engine` adds two new methods: * NewBitmap(filename string) (Texturer, error) * Copy(t Texturer, src, dst Rect) NewBitmap should open a bitmap image on disk and return it wrapped in a Texturer (really it's an SDL2 Texture). This is for caching purposes. Next the Copy() function blits the texture onto the screen renderer using the source and destination rectangles. The uix.Canvas widget orchestrates the caching for the drawing it's responsible for. It queries which chunks are viewable in the Canvas viewport (scroll and bounding boxes), has each chunk render out their entire bitmap image to then cache them as SDL textures and then only _those_ need to be copied out to the renderer each frame. The frame rate now sits at a decent 60 FPS even when the drawing gets messy and full of lines. Each unique version of each chunk needs to render only one time and then it's a fast copy operation for future ticks. Other changes: - Chunker now assigns each Chunk what their coordinate and size are, so that the chunk can self reference that information. This info is considered read-only but that isn't really enforced. - Add Chunker.IterViewportChunks() that returns a channel of Chunk Coordinates that are visible in your viewport, rather than iterating over all of the pixels in all of those chunks. - Add Chunk.ToBitmap(filename) that causes a Chunk to render its pixels to a bitmap image on disk. SDL2 can natively speak Bitmaps for texture caching. Currently these go to files in /tmp but will soon go into your $XDG_CACHE_FOLDER instead. - Add Chunk.Texture() that causes a Chunk to render and then return a cached bitmap texture of the pixels it's responsible for. The texture is cached until the Chunk is next modified with Set() or Delete(). - UI: add an Image widget that currently just shows a bitmap image. It was the first test for caching bitmap images for efficiency. Can show any *.bmp file on disk! - Editor UI: make the StatusBar boxes dynamically build from an array of string pointers to make it SUPER EASY to add/remove labels.
2018-10-18 03:52:14 +00:00
"fmt"
"testing"
2022-09-24 22:17:25 +00:00
"git.kirsle.net/SketchyMaze/doodle/pkg/level"
"git.kirsle.net/go/render"
)
func TestWorldSize(t *testing.T) {
type TestCase struct {
2023-12-02 20:33:14 +00:00
Size uint8
Points []render.Point
Expect render.Rect
Zero render.Rect // expected WorldSizePositive
}
var tests = []TestCase{
{
2023-12-02 20:33:14 +00:00
Size: 200,
Points: []render.Point{
render.NewPoint(0, 0), // chunk 0,0
render.NewPoint(512, 788), // 0,0
render.NewPoint(1002, 500), // 1,0
render.NewPoint(2005, 2006), // 2,2
render.NewPoint(-5, -5), // -1,-1
},
Expect: render.Rect{
X: -1000,
Y: -1000,
W: 2999,
H: 2999,
},
Zero: render.NewRect(3999, 3999),
},
{
Size: 128,
Points: []render.Point{
render.NewPoint(5, 5),
},
Expect: render.Rect{
X: 0,
Y: 0,
W: 127,
H: 127,
},
Zero: render.NewRect(127, 127),
},
{
Size: 200,
Points: []render.Point{
render.NewPoint(-6000, -38556),
render.NewPoint(12345, 1288000),
},
Expect: render.Rect{
X: -6000,
Y: -38600,
W: 12399,
H: 1288199,
},
Zero: render.NewRect(18399, 1326799),
},
}
for _, test := range tests {
c := level.NewChunker(test.Size)
sw := &level.Swatch{
Name: "solid",
Color: render.Black,
}
for _, pt := range test.Points {
c.Set(pt, sw)
}
size := c.WorldSize()
if size != test.Expect {
t.Errorf("WorldSize not as expected: %s <> %s", size, test.Expect)
}
zero := c.WorldSizePositive()
if zero != test.Zero {
t.Errorf("WorldSizePositive not as expected: %s <> %s", zero, test.Expect)
}
}
WIP Texture Caching NOTICE: Chunk size set to 100 for visual testing! NOTICE: guitest references a bmp file that isn't checked in! BUGS REMAINING: - When scrolling the level in Edit Mode, some of the chunks will pop out of existence randomly. - When clicking-dragging to draw in Edit Mode, if the scroll position is not at 0,0 then the pixels drawn will be offset from the cursor. - These are to do with the Scroll position and chunk coordinate calc functions probably. Implements a texture caching interface to stop redrawing everything pixel by pixel on every frame. The texture caching workflow is briefly: - The uix.Canvas widget's Present() function iterates over the list of Chunk Coordinates that are visible inside of the current viewport (i.e. viewable on screen) - For each Chunk: - Make it render and/or return its cached Texture object. - Work out how much of the Chunk will be visible and how to crop the boxes for the Copy() - Copy the cached Texture instead of drawing all the pixels every time like we were doing before. - The Chunk.Texture() function that returns said Texture: - It calls Chunk.ToBitmap() to save a bitmap on disk. - It calls Engine.NewBitmap() to get a Texture it can hang onto. - It hangs onto the Texture and returns it on future calls. - Any call to Set() or Delete() a pixel will invalidate the cache (mark the Chunk "dirty") and Texture() will rebuild next call. The interface `render.Texturer` provides a way for rendering backends (SDL2, OpenGL) to transport a "texture" of their own kind without exposing the type details to the user. The interface `render.Engine` adds two new methods: * NewBitmap(filename string) (Texturer, error) * Copy(t Texturer, src, dst Rect) NewBitmap should open a bitmap image on disk and return it wrapped in a Texturer (really it's an SDL2 Texture). This is for caching purposes. Next the Copy() function blits the texture onto the screen renderer using the source and destination rectangles. The uix.Canvas widget orchestrates the caching for the drawing it's responsible for. It queries which chunks are viewable in the Canvas viewport (scroll and bounding boxes), has each chunk render out their entire bitmap image to then cache them as SDL textures and then only _those_ need to be copied out to the renderer each frame. The frame rate now sits at a decent 60 FPS even when the drawing gets messy and full of lines. Each unique version of each chunk needs to render only one time and then it's a fast copy operation for future ticks. Other changes: - Chunker now assigns each Chunk what their coordinate and size are, so that the chunk can self reference that information. This info is considered read-only but that isn't really enforced. - Add Chunker.IterViewportChunks() that returns a channel of Chunk Coordinates that are visible in your viewport, rather than iterating over all of the pixels in all of those chunks. - Add Chunk.ToBitmap(filename) that causes a Chunk to render its pixels to a bitmap image on disk. SDL2 can natively speak Bitmaps for texture caching. Currently these go to files in /tmp but will soon go into your $XDG_CACHE_FOLDER instead. - Add Chunk.Texture() that causes a Chunk to render and then return a cached bitmap texture of the pixels it's responsible for. The texture is cached until the Chunk is next modified with Set() or Delete(). - UI: add an Image widget that currently just shows a bitmap image. It was the first test for caching bitmap images for efficiency. Can show any *.bmp file on disk! - Editor UI: make the StatusBar boxes dynamically build from an array of string pointers to make it SUPER EASY to add/remove labels.
2018-10-18 03:52:14 +00:00
}
func TestViewportChunks(t *testing.T) {
// Initialize a 100 chunk image with 5x5 chunks.
2023-12-02 20:33:14 +00:00
var ChunkSize uint8 = 100
var Offset int = 50
2023-12-02 20:33:14 +00:00
c := level.NewChunker(ChunkSize)
WIP Texture Caching NOTICE: Chunk size set to 100 for visual testing! NOTICE: guitest references a bmp file that isn't checked in! BUGS REMAINING: - When scrolling the level in Edit Mode, some of the chunks will pop out of existence randomly. - When clicking-dragging to draw in Edit Mode, if the scroll position is not at 0,0 then the pixels drawn will be offset from the cursor. - These are to do with the Scroll position and chunk coordinate calc functions probably. Implements a texture caching interface to stop redrawing everything pixel by pixel on every frame. The texture caching workflow is briefly: - The uix.Canvas widget's Present() function iterates over the list of Chunk Coordinates that are visible inside of the current viewport (i.e. viewable on screen) - For each Chunk: - Make it render and/or return its cached Texture object. - Work out how much of the Chunk will be visible and how to crop the boxes for the Copy() - Copy the cached Texture instead of drawing all the pixels every time like we were doing before. - The Chunk.Texture() function that returns said Texture: - It calls Chunk.ToBitmap() to save a bitmap on disk. - It calls Engine.NewBitmap() to get a Texture it can hang onto. - It hangs onto the Texture and returns it on future calls. - Any call to Set() or Delete() a pixel will invalidate the cache (mark the Chunk "dirty") and Texture() will rebuild next call. The interface `render.Texturer` provides a way for rendering backends (SDL2, OpenGL) to transport a "texture" of their own kind without exposing the type details to the user. The interface `render.Engine` adds two new methods: * NewBitmap(filename string) (Texturer, error) * Copy(t Texturer, src, dst Rect) NewBitmap should open a bitmap image on disk and return it wrapped in a Texturer (really it's an SDL2 Texture). This is for caching purposes. Next the Copy() function blits the texture onto the screen renderer using the source and destination rectangles. The uix.Canvas widget orchestrates the caching for the drawing it's responsible for. It queries which chunks are viewable in the Canvas viewport (scroll and bounding boxes), has each chunk render out their entire bitmap image to then cache them as SDL textures and then only _those_ need to be copied out to the renderer each frame. The frame rate now sits at a decent 60 FPS even when the drawing gets messy and full of lines. Each unique version of each chunk needs to render only one time and then it's a fast copy operation for future ticks. Other changes: - Chunker now assigns each Chunk what their coordinate and size are, so that the chunk can self reference that information. This info is considered read-only but that isn't really enforced. - Add Chunker.IterViewportChunks() that returns a channel of Chunk Coordinates that are visible in your viewport, rather than iterating over all of the pixels in all of those chunks. - Add Chunk.ToBitmap(filename) that causes a Chunk to render its pixels to a bitmap image on disk. SDL2 can natively speak Bitmaps for texture caching. Currently these go to files in /tmp but will soon go into your $XDG_CACHE_FOLDER instead. - Add Chunk.Texture() that causes a Chunk to render and then return a cached bitmap texture of the pixels it's responsible for. The texture is cached until the Chunk is next modified with Set() or Delete(). - UI: add an Image widget that currently just shows a bitmap image. It was the first test for caching bitmap images for efficiency. Can show any *.bmp file on disk! - Editor UI: make the StatusBar boxes dynamically build from an array of string pointers to make it SUPER EASY to add/remove labels.
2018-10-18 03:52:14 +00:00
sw := &level.Swatch{
Name: "solid",
Color: render.Black,
}
// The 5x5 chunks are expected to be (diagonally)
// -2,-2
// -1,-1
// 0,0
// 1,1
// 2,2
// The chunk size is 100px so place a single pixel in each
// 100px quadrant.
fmt.Printf("size=%d offset=%d\n", ChunkSize, Offset)
for x := -2; x <= 2; x++ {
for y := -2; y <= 2; y++ {
WIP Texture Caching NOTICE: Chunk size set to 100 for visual testing! NOTICE: guitest references a bmp file that isn't checked in! BUGS REMAINING: - When scrolling the level in Edit Mode, some of the chunks will pop out of existence randomly. - When clicking-dragging to draw in Edit Mode, if the scroll position is not at 0,0 then the pixels drawn will be offset from the cursor. - These are to do with the Scroll position and chunk coordinate calc functions probably. Implements a texture caching interface to stop redrawing everything pixel by pixel on every frame. The texture caching workflow is briefly: - The uix.Canvas widget's Present() function iterates over the list of Chunk Coordinates that are visible inside of the current viewport (i.e. viewable on screen) - For each Chunk: - Make it render and/or return its cached Texture object. - Work out how much of the Chunk will be visible and how to crop the boxes for the Copy() - Copy the cached Texture instead of drawing all the pixels every time like we were doing before. - The Chunk.Texture() function that returns said Texture: - It calls Chunk.ToBitmap() to save a bitmap on disk. - It calls Engine.NewBitmap() to get a Texture it can hang onto. - It hangs onto the Texture and returns it on future calls. - Any call to Set() or Delete() a pixel will invalidate the cache (mark the Chunk "dirty") and Texture() will rebuild next call. The interface `render.Texturer` provides a way for rendering backends (SDL2, OpenGL) to transport a "texture" of their own kind without exposing the type details to the user. The interface `render.Engine` adds two new methods: * NewBitmap(filename string) (Texturer, error) * Copy(t Texturer, src, dst Rect) NewBitmap should open a bitmap image on disk and return it wrapped in a Texturer (really it's an SDL2 Texture). This is for caching purposes. Next the Copy() function blits the texture onto the screen renderer using the source and destination rectangles. The uix.Canvas widget orchestrates the caching for the drawing it's responsible for. It queries which chunks are viewable in the Canvas viewport (scroll and bounding boxes), has each chunk render out their entire bitmap image to then cache them as SDL textures and then only _those_ need to be copied out to the renderer each frame. The frame rate now sits at a decent 60 FPS even when the drawing gets messy and full of lines. Each unique version of each chunk needs to render only one time and then it's a fast copy operation for future ticks. Other changes: - Chunker now assigns each Chunk what their coordinate and size are, so that the chunk can self reference that information. This info is considered read-only but that isn't really enforced. - Add Chunker.IterViewportChunks() that returns a channel of Chunk Coordinates that are visible in your viewport, rather than iterating over all of the pixels in all of those chunks. - Add Chunk.ToBitmap(filename) that causes a Chunk to render its pixels to a bitmap image on disk. SDL2 can natively speak Bitmaps for texture caching. Currently these go to files in /tmp but will soon go into your $XDG_CACHE_FOLDER instead. - Add Chunk.Texture() that causes a Chunk to render and then return a cached bitmap texture of the pixels it's responsible for. The texture is cached until the Chunk is next modified with Set() or Delete(). - UI: add an Image widget that currently just shows a bitmap image. It was the first test for caching bitmap images for efficiency. Can show any *.bmp file on disk! - Editor UI: make the StatusBar boxes dynamically build from an array of string pointers to make it SUPER EASY to add/remove labels.
2018-10-18 03:52:14 +00:00
point := render.NewPoint(
2023-12-02 20:33:14 +00:00
x*int(ChunkSize)+Offset,
y*int(ChunkSize)+Offset,
WIP Texture Caching NOTICE: Chunk size set to 100 for visual testing! NOTICE: guitest references a bmp file that isn't checked in! BUGS REMAINING: - When scrolling the level in Edit Mode, some of the chunks will pop out of existence randomly. - When clicking-dragging to draw in Edit Mode, if the scroll position is not at 0,0 then the pixels drawn will be offset from the cursor. - These are to do with the Scroll position and chunk coordinate calc functions probably. Implements a texture caching interface to stop redrawing everything pixel by pixel on every frame. The texture caching workflow is briefly: - The uix.Canvas widget's Present() function iterates over the list of Chunk Coordinates that are visible inside of the current viewport (i.e. viewable on screen) - For each Chunk: - Make it render and/or return its cached Texture object. - Work out how much of the Chunk will be visible and how to crop the boxes for the Copy() - Copy the cached Texture instead of drawing all the pixels every time like we were doing before. - The Chunk.Texture() function that returns said Texture: - It calls Chunk.ToBitmap() to save a bitmap on disk. - It calls Engine.NewBitmap() to get a Texture it can hang onto. - It hangs onto the Texture and returns it on future calls. - Any call to Set() or Delete() a pixel will invalidate the cache (mark the Chunk "dirty") and Texture() will rebuild next call. The interface `render.Texturer` provides a way for rendering backends (SDL2, OpenGL) to transport a "texture" of their own kind without exposing the type details to the user. The interface `render.Engine` adds two new methods: * NewBitmap(filename string) (Texturer, error) * Copy(t Texturer, src, dst Rect) NewBitmap should open a bitmap image on disk and return it wrapped in a Texturer (really it's an SDL2 Texture). This is for caching purposes. Next the Copy() function blits the texture onto the screen renderer using the source and destination rectangles. The uix.Canvas widget orchestrates the caching for the drawing it's responsible for. It queries which chunks are viewable in the Canvas viewport (scroll and bounding boxes), has each chunk render out their entire bitmap image to then cache them as SDL textures and then only _those_ need to be copied out to the renderer each frame. The frame rate now sits at a decent 60 FPS even when the drawing gets messy and full of lines. Each unique version of each chunk needs to render only one time and then it's a fast copy operation for future ticks. Other changes: - Chunker now assigns each Chunk what their coordinate and size are, so that the chunk can self reference that information. This info is considered read-only but that isn't really enforced. - Add Chunker.IterViewportChunks() that returns a channel of Chunk Coordinates that are visible in your viewport, rather than iterating over all of the pixels in all of those chunks. - Add Chunk.ToBitmap(filename) that causes a Chunk to render its pixels to a bitmap image on disk. SDL2 can natively speak Bitmaps for texture caching. Currently these go to files in /tmp but will soon go into your $XDG_CACHE_FOLDER instead. - Add Chunk.Texture() that causes a Chunk to render and then return a cached bitmap texture of the pixels it's responsible for. The texture is cached until the Chunk is next modified with Set() or Delete(). - UI: add an Image widget that currently just shows a bitmap image. It was the first test for caching bitmap images for efficiency. Can show any *.bmp file on disk! - Editor UI: make the StatusBar boxes dynamically build from an array of string pointers to make it SUPER EASY to add/remove labels.
2018-10-18 03:52:14 +00:00
)
fmt.Printf("in chunk: %d,%d set pt: %s\n",
x, y, point,
)
c.Set(point, sw)
}
}
// Sanity check the test canvas was created correctly.
worldSize := c.WorldSize()
expectSize := render.Rect{
X: -200,
Y: -200,
W: 299,
H: 299,
}
if worldSize != expectSize {
t.Errorf(
"Test canvas world size wasn't as expected:\n"+
"Expected: %s\n"+
" Actual: %s\n",
expectSize,
worldSize,
)
}
if len(c.Chunks) != 25 {
t.Errorf(
"Test canvas chunk count wasn't as expected:\n"+
"Expected: 25\n"+
" Actual: %d\n",
len(c.Chunks),
)
}
type TestCase struct {
Viewport render.Rect
Expect map[render.Point]interface{}
}
var tests = []TestCase{
{
Viewport: render.Rect{X: -10000, Y: -10000, W: 10000, H: 10000},
Expect: map[render.Point]interface{}{
render.NewPoint(-2, -2): nil,
render.NewPoint(-2, -1): nil,
render.NewPoint(-2, 0): nil,
render.NewPoint(-2, 1): nil,
render.NewPoint(-2, 2): nil,
render.NewPoint(-1, -2): nil,
render.NewPoint(-1, -1): nil,
render.NewPoint(-1, 0): nil,
render.NewPoint(-1, 1): nil,
render.NewPoint(-1, 2): nil,
render.NewPoint(0, -2): nil,
render.NewPoint(0, -1): nil,
render.NewPoint(0, 0): nil,
render.NewPoint(0, 1): nil,
render.NewPoint(0, 2): nil,
render.NewPoint(1, -2): nil,
render.NewPoint(1, -1): nil,
render.NewPoint(1, 0): nil,
render.NewPoint(1, 1): nil,
render.NewPoint(1, 2): nil,
render.NewPoint(2, -2): nil,
render.NewPoint(2, -1): nil,
render.NewPoint(2, 0): nil,
render.NewPoint(2, 1): nil,
render.NewPoint(2, 2): nil,
},
},
{
Viewport: render.Rect{X: 0, Y: 0, W: 200, H: 200},
Expect: map[render.Point]interface{}{
render.NewPoint(0, 0): nil,
render.NewPoint(0, 1): nil,
render.NewPoint(1, 0): nil,
render.NewPoint(1, 1): nil,
},
},
// {
// Viewport: render.Rect{X: -5, Y: 0, W: 200, H: 200},
// Expect: map[render.Point]interface{}{
// render.NewPoint(-1, 0): nil,
// render.NewPoint(0, 0): nil,
// render.NewPoint(1, 1): nil,
// },
// },
}
WIP Texture Caching NOTICE: Chunk size set to 100 for visual testing! NOTICE: guitest references a bmp file that isn't checked in! BUGS REMAINING: - When scrolling the level in Edit Mode, some of the chunks will pop out of existence randomly. - When clicking-dragging to draw in Edit Mode, if the scroll position is not at 0,0 then the pixels drawn will be offset from the cursor. - These are to do with the Scroll position and chunk coordinate calc functions probably. Implements a texture caching interface to stop redrawing everything pixel by pixel on every frame. The texture caching workflow is briefly: - The uix.Canvas widget's Present() function iterates over the list of Chunk Coordinates that are visible inside of the current viewport (i.e. viewable on screen) - For each Chunk: - Make it render and/or return its cached Texture object. - Work out how much of the Chunk will be visible and how to crop the boxes for the Copy() - Copy the cached Texture instead of drawing all the pixels every time like we were doing before. - The Chunk.Texture() function that returns said Texture: - It calls Chunk.ToBitmap() to save a bitmap on disk. - It calls Engine.NewBitmap() to get a Texture it can hang onto. - It hangs onto the Texture and returns it on future calls. - Any call to Set() or Delete() a pixel will invalidate the cache (mark the Chunk "dirty") and Texture() will rebuild next call. The interface `render.Texturer` provides a way for rendering backends (SDL2, OpenGL) to transport a "texture" of their own kind without exposing the type details to the user. The interface `render.Engine` adds two new methods: * NewBitmap(filename string) (Texturer, error) * Copy(t Texturer, src, dst Rect) NewBitmap should open a bitmap image on disk and return it wrapped in a Texturer (really it's an SDL2 Texture). This is for caching purposes. Next the Copy() function blits the texture onto the screen renderer using the source and destination rectangles. The uix.Canvas widget orchestrates the caching for the drawing it's responsible for. It queries which chunks are viewable in the Canvas viewport (scroll and bounding boxes), has each chunk render out their entire bitmap image to then cache them as SDL textures and then only _those_ need to be copied out to the renderer each frame. The frame rate now sits at a decent 60 FPS even when the drawing gets messy and full of lines. Each unique version of each chunk needs to render only one time and then it's a fast copy operation for future ticks. Other changes: - Chunker now assigns each Chunk what their coordinate and size are, so that the chunk can self reference that information. This info is considered read-only but that isn't really enforced. - Add Chunker.IterViewportChunks() that returns a channel of Chunk Coordinates that are visible in your viewport, rather than iterating over all of the pixels in all of those chunks. - Add Chunk.ToBitmap(filename) that causes a Chunk to render its pixels to a bitmap image on disk. SDL2 can natively speak Bitmaps for texture caching. Currently these go to files in /tmp but will soon go into your $XDG_CACHE_FOLDER instead. - Add Chunk.Texture() that causes a Chunk to render and then return a cached bitmap texture of the pixels it's responsible for. The texture is cached until the Chunk is next modified with Set() or Delete(). - UI: add an Image widget that currently just shows a bitmap image. It was the first test for caching bitmap images for efficiency. Can show any *.bmp file on disk! - Editor UI: make the StatusBar boxes dynamically build from an array of string pointers to make it SUPER EASY to add/remove labels.
2018-10-18 03:52:14 +00:00
for _, test := range tests {
chunks := []render.Point{}
for chunk := range c.IterViewportChunks(test.Viewport) {
chunks = append(chunks, chunk)
}
if len(chunks) != len(test.Expect) {
t.Errorf("%s: chunk count mismatch: expected %d, got %d",
test.Viewport,
len(test.Expect),
len(chunks),
)
}
for _, actual := range chunks {
if _, ok := test.Expect[actual]; !ok {
t.Errorf("%s: got chunk coord %d but did not expect to",
test.Viewport,
actual,
)
}
delete(test.Expect, actual)
}
if len(test.Expect) > 0 {
t.Errorf("%s: failed to see these coords: %+v",
test.Viewport,
test.Expect,
)
}
}
}
(Experimental) Run Length Encoding for Levels Finally add a second option for Chunk MapAccessor implementation besides the MapAccessor. The RLEAccessor is basically a MapAccessor that will compress your drawing with Run Length Encoding (RLE) in the on-disk format in the ZIP file. This slashes the file sizes of most levels: * Shapeshifter: 21.8 MB -> 8.1 MB * Jungle: 10.4 MB -> 4.1 MB * Zoo: 2.8 MB -> 1.3 MB Implementation details: * The RLE binary format for Chunks is a stream of Uvarint pairs storing the palette index number and the number of pixels to repeat it (along the Y,X axis of the chunk). * Null colors are represented by a Uvarint that decodes to 0xFFFF or 65535 in decimal. * Gameplay logic currently limits maps to 256 colors. * The default for newly created chunks in-game will be RLE by default. * Its in-memory representation is still a MapAccessor (a map of absolute world coordinates to palette index). * The game can still open and play legacy MapAccessor maps. * On save in the editor, the game will upgrade/convert MapAccessor chunks over to RLEAccessors, improving on your level's file size with a simple re-save. Current Bugs * On every re-save to RLE, one pixel is lost in the bottom-right corner of each chunk. Each subsequent re-save loses one more pixel to the left, so what starts as a single pixel per chunk slowly evolves into a horizontal line. * Some pixels smear vertically as well. * Off-by-negative-one errors when some chunks Iter() their pixels but compute a relative coordinate of (-1,0)! Some mismatch between the stored world coords of a pixel inside the chunk vs. the chunk's assigned coordinate by the Chunker: certain combinations of chunk coord/abs coord. To Do * The `doodad touch` command should re-save existing levels to upgrade them.
2024-05-24 06:02:01 +00:00
func TestRelativeCoordinates(t *testing.T) {
var (
chunker = level.NewChunker(128)
)
type TestCase struct {
WorldCoord render.Point
ChunkCoord render.Point
ExpectRelative render.Point
}
var tests = []TestCase{
{
WorldCoord: render.NewPoint(4, 8),
ExpectRelative: render.NewPoint(4, 8),
},
{
WorldCoord: render.NewPoint(128, 128),
ExpectRelative: render.NewPoint(0, 0),
},
{
WorldCoord: render.NewPoint(143, 144),
ExpectRelative: render.NewPoint(15, 16),
},
{
WorldCoord: render.NewPoint(-105, -86),
ExpectRelative: render.NewPoint(23, 42),
},
{
WorldCoord: render.NewPoint(-252, 264),
ExpectRelative: render.NewPoint(4, 8),
},
// These were seen breaking actual levels, at the corners of the chunk
{
WorldCoord: render.NewPoint(511, 256),
ExpectRelative: render.NewPoint(127, 0), // was getting -1,0 in game
},
{
WorldCoord: render.NewPoint(511, 512),
ChunkCoord: render.NewPoint(4, 4),
ExpectRelative: render.NewPoint(127, 0), // was getting -1,0 in game
},
{
WorldCoord: render.NewPoint(127, 384),
ChunkCoord: render.NewPoint(1, 3),
ExpectRelative: render.NewPoint(-1, 0),
},
}
for i, test := range tests {
var (
chunkCoord = test.ChunkCoord
actualRelative = level.RelativeCoordinate(
test.WorldCoord,
chunkCoord,
chunker.Size,
)
roundTrip = level.FromRelativeCoordinate(
actualRelative,
chunkCoord,
chunker.Size,
)
)
// compute expected chunk coord automatically?
if chunkCoord == render.Origin {
chunkCoord = chunker.ChunkCoordinate(test.WorldCoord)
}
if actualRelative != test.ExpectRelative {
t.Errorf("Test %d: world coord %s in chunk %s\n"+
"Expected RelativeCoordinate() to be: %s\n"+
"But it was: %s",
i,
test.WorldCoord,
chunkCoord,
test.ExpectRelative,
actualRelative,
)
}
if roundTrip != test.WorldCoord {
t.Errorf("Test %d: world coord %s in chunk %s\n"+
"Did not survive round trip! Expected: %s\n"+
"But it was: %s",
i,
test.WorldCoord,
chunkCoord,
test.WorldCoord,
roundTrip,
)
}
}
}