doodle/pkg/level/chunker_test.go

231 lines
5.0 KiB
Go
Raw 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"
"git.kirsle.net/apps/doodle/lib/render"
"git.kirsle.net/apps/doodle/pkg/level"
)
func TestWorldSize(t *testing.T) {
type TestCase struct {
Size int
Points []render.Point
Expect render.Rect
Zero render.Rect // expected WorldSizePositive
}
var tests = []TestCase{
{
Size: 1000,
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.
var ChunkSize int32 = 100
var Offset int32 = 50
c := level.NewChunker(int(ChunkSize))
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 := int32(-2); x <= 2; x++ {
for y := int32(-2); y <= 2; y++ {
point := render.NewPoint(
x*ChunkSize+Offset,
y*ChunkSize+Offset,
)
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,
)
}
}
}