2018-09-23 22:20:45 +00:00
|
|
|
package level
|
|
|
|
|
|
|
|
import (
|
2022-04-30 03:34:59 +00:00
|
|
|
"archive/zip"
|
2018-09-23 22:20:45 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
2022-04-30 03:34:59 +00:00
|
|
|
"sync"
|
2018-09-23 22:20:45 +00:00
|
|
|
|
2022-09-24 22:17:25 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/balance"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/log"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/shmem"
|
2019-12-28 03:16:34 +00:00
|
|
|
"git.kirsle.net/go/render"
|
2018-09-23 22:20:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Chunker is the data structure that manages the chunks of a level, and
|
|
|
|
// provides the API to interact with the pixels using their absolute coordinates
|
|
|
|
// while abstracting away the underlying details.
|
|
|
|
type Chunker struct {
|
2022-04-30 03:34:59 +00:00
|
|
|
// Layer is optional for the caller, levels use only 0 and
|
|
|
|
// doodads use them for frames. When chunks are exported to
|
|
|
|
// zipfile the Layer keeps them from overlapping.
|
2023-02-17 05:47:18 +00:00
|
|
|
Layer int `json:"-"` // internal use only
|
|
|
|
Size uint8 `json:"size"`
|
2022-04-30 03:34:59 +00:00
|
|
|
|
|
|
|
// A Zipfile reference for new-style levels and doodads which
|
|
|
|
// keep their chunks in external parts of a zip file.
|
|
|
|
Zipfile *zip.Reader `json:"-"`
|
|
|
|
|
|
|
|
// Chunks, oh boy.
|
|
|
|
// The v1 drawing format had all the chunks in the JSON file.
|
|
|
|
// New drawings write them to zips. Legacy drawings can be converted
|
|
|
|
// simply by loading and resaving: their Chunks loads from JSON and
|
|
|
|
// is committed to zipfile on save. This makes Chunks also a good
|
|
|
|
// cache even when we have a zipfile to fall back on.
|
|
|
|
Chunks ChunkMap `json:"chunks"`
|
|
|
|
chunkMu sync.RWMutex
|
|
|
|
|
|
|
|
// If we have a zipfile, only keep chunks warm in memory if they
|
|
|
|
// are actively wanted by the game.
|
|
|
|
lastTick uint64 // NOTE: tracks from shmem.Tick
|
|
|
|
chunkRequestsThisTick map[render.Point]interface{}
|
2022-05-08 00:16:03 +00:00
|
|
|
requestsN1 map[render.Point]interface{} // chunks accessed last tick
|
|
|
|
requestsN2 map[render.Point]interface{} // 2 ticks ago (to free soon)
|
|
|
|
chunksToFree map[render.Point]uint64 // chopping block (free after X ticks)
|
2022-05-08 00:42:38 +00:00
|
|
|
ctfMu sync.Mutex // lock for chunksToFree
|
2022-04-30 03:34:59 +00:00
|
|
|
requestMu sync.Mutex
|
|
|
|
|
|
|
|
// The palette reference from first call to Inflate()
|
|
|
|
pal *Palette
|
2018-09-23 22:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewChunker creates a new chunk manager with a given chunk size.
|
2023-02-17 05:47:18 +00:00
|
|
|
func NewChunker(size uint8) *Chunker {
|
2018-09-23 22:20:45 +00:00
|
|
|
return &Chunker{
|
|
|
|
Size: size,
|
|
|
|
Chunks: ChunkMap{},
|
2022-04-30 03:34:59 +00:00
|
|
|
|
|
|
|
chunkRequestsThisTick: map[render.Point]interface{}{},
|
|
|
|
requestsN1: map[render.Point]interface{}{},
|
|
|
|
requestsN2: map[render.Point]interface{}{},
|
2022-05-08 00:16:03 +00:00
|
|
|
chunksToFree: map[render.Point]uint64{},
|
2018-09-23 22:20:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inflate iterates over the pixels in the (loaded) chunks and expands any
|
|
|
|
// Sparse Swatches (which have only their palette index, from the file format
|
|
|
|
// on disk) to connect references to the swatches in the palette.
|
|
|
|
func (c *Chunker) Inflate(pal *Palette) error {
|
2022-04-30 03:34:59 +00:00
|
|
|
c.pal = pal
|
|
|
|
|
|
|
|
c.chunkMu.RLock()
|
|
|
|
defer c.chunkMu.RUnlock()
|
2018-09-23 22:20:45 +00:00
|
|
|
for coord, chunk := range c.Chunks {
|
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
|
|
|
chunk.Point = coord
|
|
|
|
chunk.Size = c.Size
|
2018-09-23 22:20:45 +00:00
|
|
|
chunk.Inflate(pal)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IterViewport returns a channel to iterate every point that exists within
|
|
|
|
// the viewport rect.
|
|
|
|
func (c *Chunker) IterViewport(viewport render.Rect) <-chan Pixel {
|
|
|
|
pipe := make(chan Pixel)
|
|
|
|
go func() {
|
|
|
|
// Get the chunk box coordinates.
|
|
|
|
var (
|
|
|
|
topLeft = c.ChunkCoordinate(render.NewPoint(viewport.X, viewport.Y))
|
|
|
|
bottomRight = c.ChunkCoordinate(render.Point{
|
|
|
|
X: viewport.X + viewport.W,
|
|
|
|
Y: viewport.Y + viewport.H,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
for cx := topLeft.X; cx <= bottomRight.X; cx++ {
|
|
|
|
for cy := topLeft.Y; cy <= bottomRight.Y; cy++ {
|
|
|
|
if chunk, ok := c.GetChunk(render.NewPoint(cx, cy)); ok {
|
|
|
|
for px := range chunk.Iter() {
|
2018-09-25 16:40:34 +00:00
|
|
|
|
|
|
|
// Verify this pixel is also in range.
|
|
|
|
if px.Point().Inside(viewport) {
|
|
|
|
pipe <- px
|
|
|
|
}
|
2018-09-23 22:20:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(pipe)
|
|
|
|
}()
|
|
|
|
return pipe
|
|
|
|
}
|
|
|
|
|
2022-04-09 21:41:24 +00:00
|
|
|
// IterChunks returns a channel to iterate over all chunks in the drawing.
|
|
|
|
func (c *Chunker) IterChunks() <-chan render.Point {
|
2022-04-30 03:34:59 +00:00
|
|
|
var (
|
|
|
|
pipe = make(chan render.Point)
|
|
|
|
sent = map[render.Point]interface{}{}
|
|
|
|
)
|
|
|
|
|
2022-04-09 21:41:24 +00:00
|
|
|
go func() {
|
2022-04-30 03:34:59 +00:00
|
|
|
c.chunkMu.RLock()
|
|
|
|
|
|
|
|
// Send the chunk coords we have in working memory.
|
|
|
|
// v1 levels: had all their chunks there in their JSON data
|
|
|
|
// v2 levels: chunks are in zipfile, cached ones are here
|
2022-04-09 21:41:24 +00:00
|
|
|
for point := range c.Chunks {
|
2022-04-30 03:34:59 +00:00
|
|
|
sent[point] = nil
|
2022-04-09 21:41:24 +00:00
|
|
|
pipe <- point
|
|
|
|
}
|
2022-04-30 03:34:59 +00:00
|
|
|
|
|
|
|
c.chunkMu.RUnlock()
|
|
|
|
|
|
|
|
// If we have a zipfile, send any remaining chunks that are
|
|
|
|
// in colder storage.
|
|
|
|
if c.Zipfile != nil {
|
|
|
|
for _, point := range ChunksInZipfile(c.Zipfile, c.Layer) {
|
|
|
|
if _, ok := sent[point]; ok {
|
|
|
|
continue // Already sent from active memory
|
|
|
|
}
|
|
|
|
pipe <- point
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(pipe)
|
|
|
|
}()
|
|
|
|
return pipe
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
IterChunksThemselves iterates all chunks in the drawing rather than coords.
|
|
|
|
|
|
|
|
Note: this will mark every chunk as "touched" this frame, so in a zipfile
|
|
|
|
level will load ALL chunks into memory.
|
|
|
|
*/
|
|
|
|
func (c *Chunker) IterChunksThemselves() <-chan *Chunk {
|
|
|
|
pipe := make(chan *Chunk)
|
|
|
|
go func() {
|
|
|
|
for coord := range c.IterChunks() {
|
|
|
|
if chunk, ok := c.GetChunk(coord); ok {
|
|
|
|
pipe <- chunk
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(pipe)
|
|
|
|
}()
|
|
|
|
return pipe
|
|
|
|
}
|
|
|
|
|
|
|
|
// IterCachedChunks iterates ONLY over the chunks currently cached in memory,
|
|
|
|
// e.g. so they can be torn down without loading extra chunks by looping normally.
|
|
|
|
func (c *Chunker) IterCachedChunks() <-chan *Chunk {
|
|
|
|
pipe := make(chan *Chunk)
|
|
|
|
go func() {
|
|
|
|
c.chunkMu.RLock()
|
|
|
|
defer c.chunkMu.RUnlock()
|
|
|
|
|
|
|
|
for _, chunk := range c.Chunks {
|
|
|
|
pipe <- chunk
|
|
|
|
}
|
2022-04-09 21:41:24 +00:00
|
|
|
close(pipe)
|
|
|
|
}()
|
|
|
|
return pipe
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// IterViewportChunks returns a channel to iterate over the Chunk objects that
|
|
|
|
// appear within the viewport rect, instead of the pixels in each chunk.
|
|
|
|
func (c *Chunker) IterViewportChunks(viewport render.Rect) <-chan render.Point {
|
|
|
|
pipe := make(chan render.Point)
|
|
|
|
go func() {
|
2023-02-17 05:47:18 +00:00
|
|
|
var (
|
|
|
|
sent = make(map[render.Point]interface{})
|
|
|
|
size = int(c.Size)
|
|
|
|
)
|
2018-10-18 06:01:21 +00:00
|
|
|
|
2024-02-08 06:14:48 +00:00
|
|
|
for x := viewport.X; x < viewport.W+size; x += (size / 4) {
|
|
|
|
for y := viewport.Y; y < viewport.H+size; y += (size / 4) {
|
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
|
|
|
|
|
|
|
// Constrain this chunksize step to a point within the bounds
|
|
|
|
// of the viewport. This can yield partial chunks on the edges
|
|
|
|
// of the viewport.
|
|
|
|
point := render.NewPoint(x, y)
|
|
|
|
if point.X < viewport.X {
|
|
|
|
point.X = viewport.X
|
|
|
|
} else if point.X > viewport.X+viewport.W {
|
|
|
|
point.X = viewport.X + viewport.W
|
|
|
|
}
|
|
|
|
if point.Y < viewport.Y {
|
|
|
|
point.Y = viewport.Y
|
|
|
|
} else if point.Y > viewport.Y+viewport.H {
|
|
|
|
point.Y = viewport.Y + viewport.H
|
|
|
|
}
|
|
|
|
|
|
|
|
// Translate to a chunk coordinate, dedupe and send it.
|
|
|
|
coord := c.ChunkCoordinate(render.NewPoint(x, y))
|
|
|
|
if _, ok := sent[coord]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sent[coord] = nil
|
|
|
|
|
|
|
|
if _, ok := c.GetChunk(coord); ok {
|
|
|
|
pipe <- coord
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(pipe)
|
|
|
|
}()
|
|
|
|
return pipe
|
|
|
|
}
|
|
|
|
|
2018-09-23 22:20:45 +00:00
|
|
|
// IterPixels returns a channel to iterate over every pixel in the entire
|
|
|
|
// chunker.
|
|
|
|
func (c *Chunker) IterPixels() <-chan Pixel {
|
|
|
|
pipe := make(chan Pixel)
|
|
|
|
go func() {
|
2022-04-30 03:34:59 +00:00
|
|
|
for chunk := range c.IterChunksThemselves() {
|
2018-09-23 22:20:45 +00:00
|
|
|
for px := range chunk.Iter() {
|
|
|
|
pipe <- px
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(pipe)
|
|
|
|
}()
|
|
|
|
return pipe
|
|
|
|
}
|
|
|
|
|
2018-10-16 16:20:25 +00:00
|
|
|
// WorldSize returns the bounding coordinates that the Chunker has chunks to
|
|
|
|
// manage: the lowest pixels from the lowest chunks to the highest pixels of
|
|
|
|
// the highest chunks.
|
|
|
|
func (c *Chunker) WorldSize() render.Rect {
|
2023-02-17 05:47:18 +00:00
|
|
|
var (
|
|
|
|
size = int(c.Size)
|
|
|
|
chunkLowest, chunkHighest = c.Bounds()
|
|
|
|
)
|
|
|
|
|
2018-10-16 16:20:25 +00:00
|
|
|
return render.Rect{
|
2023-02-17 05:47:18 +00:00
|
|
|
X: chunkLowest.X * size,
|
|
|
|
Y: chunkLowest.Y * size,
|
|
|
|
W: (chunkHighest.X * size) + (size - 1),
|
|
|
|
H: (chunkHighest.Y * size) + (size - 1),
|
2018-10-16 16:20:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WorldSizePositive returns the WorldSize anchored to 0,0 with only positive
|
|
|
|
// coordinates.
|
|
|
|
func (c *Chunker) WorldSizePositive() render.Rect {
|
|
|
|
S := c.WorldSize()
|
|
|
|
return render.Rect{
|
|
|
|
X: 0,
|
|
|
|
Y: 0,
|
2019-12-28 03:16:34 +00:00
|
|
|
W: int(math.Abs(float64(S.X))) + S.W,
|
|
|
|
H: int(math.Abs(float64(S.Y))) + S.H,
|
2018-10-16 16:20:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 00:21:17 +00:00
|
|
|
// Bounds returns the boundary points of the lowest and highest chunk which
|
|
|
|
// have any data in them.
|
|
|
|
func (c *Chunker) Bounds() (low, high render.Point) {
|
2022-04-30 03:34:59 +00:00
|
|
|
for coord := range c.IterChunks() {
|
2021-10-04 00:21:17 +00:00
|
|
|
if coord.X < low.X {
|
|
|
|
low.X = coord.X
|
|
|
|
}
|
|
|
|
if coord.Y < low.Y {
|
|
|
|
low.Y = coord.Y
|
|
|
|
}
|
|
|
|
|
|
|
|
if coord.X > high.X {
|
|
|
|
high.X = coord.X
|
|
|
|
}
|
|
|
|
if coord.Y > high.Y {
|
|
|
|
high.Y = coord.Y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return low, high
|
|
|
|
}
|
|
|
|
|
2022-04-30 03:34:59 +00:00
|
|
|
/*
|
|
|
|
GetChunk gets a chunk at a certain position. Returns false if not found.
|
|
|
|
|
|
|
|
This should be the centralized function to request a Chunk from the Chunker
|
|
|
|
(or IterChunksThemselves). On old-style levels all of the chunks were just
|
|
|
|
in memory as part of the JSON struct, in Zip files we can load/unload them
|
|
|
|
at will from external files.
|
|
|
|
*/
|
2018-09-23 22:20:45 +00:00
|
|
|
func (c *Chunker) GetChunk(p render.Point) (*Chunk, bool) {
|
2022-04-30 03:34:59 +00:00
|
|
|
// It's currently cached in memory?
|
|
|
|
c.chunkMu.RLock()
|
2018-09-23 22:20:45 +00:00
|
|
|
chunk, ok := c.Chunks[p]
|
2022-04-30 03:34:59 +00:00
|
|
|
c.chunkMu.RUnlock()
|
|
|
|
|
2022-05-08 00:42:38 +00:00
|
|
|
// Was it on the chopping block for garbage collection?
|
|
|
|
c.ctfMu.Lock()
|
|
|
|
delete(c.chunksToFree, p)
|
|
|
|
c.ctfMu.Unlock()
|
|
|
|
|
2022-04-30 03:34:59 +00:00
|
|
|
if ok {
|
|
|
|
// An empty chunk? We hang onto these until save time to commit
|
|
|
|
// the empty chunk to ZIP.
|
|
|
|
if chunk.Len() == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
c.logChunkAccess(p, chunk) // for the LRU cache
|
|
|
|
return chunk, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hit the zipfile for it.
|
|
|
|
if c.Zipfile != nil {
|
2024-05-24 06:02:01 +00:00
|
|
|
if chunk, err := c.ChunkFromZipfile(p); err == nil {
|
2022-05-01 00:59:55 +00:00
|
|
|
// log.Debug("GetChunk(%s) cache miss, read from zip", p)
|
2022-04-30 03:34:59 +00:00
|
|
|
c.SetChunk(p, chunk) // cache it
|
|
|
|
c.logChunkAccess(p, chunk) // for the LRU cache
|
|
|
|
if c.pal != nil {
|
|
|
|
chunk.Point = p
|
|
|
|
chunk.Size = c.Size
|
|
|
|
chunk.Inflate(c.pal)
|
|
|
|
}
|
|
|
|
return chunk, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is our chunk cache getting too full? e.g. on full level
|
|
|
|
// sweeps where a whole zip file's worth of chunks are scanned.
|
|
|
|
if balance.ChunkerLRUCacheMax > 0 && len(c.Chunks) > balance.ChunkerLRUCacheMax {
|
2023-12-02 20:33:14 +00:00
|
|
|
log.Error("Chunks in memory (%d) exceeds LRU cache cap of %d, freeing random chunks", len(c.Chunks), balance.ChunkerLRUCacheMax)
|
2022-04-30 03:34:59 +00:00
|
|
|
c.chunkMu.Lock()
|
|
|
|
defer c.chunkMu.Unlock()
|
|
|
|
|
|
|
|
var (
|
|
|
|
i = 0
|
|
|
|
limit = len(c.Chunks) - balance.ChunkerLRUCacheMax
|
|
|
|
)
|
|
|
|
for coord := range c.Chunks {
|
|
|
|
if i < limit {
|
|
|
|
delete(c.Chunks, coord)
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// LRU cache for chunks from zipfiles: log which chunks were accessed
|
|
|
|
// this tick, so they can be compared to the tick prior, and then freed
|
|
|
|
// up after that.
|
|
|
|
func (c *Chunker) logChunkAccess(p render.Point, chunk *Chunk) {
|
|
|
|
// Record this point.
|
|
|
|
c.requestMu.Lock()
|
|
|
|
if c.chunkRequestsThisTick == nil {
|
|
|
|
c.chunkRequestsThisTick = map[render.Point]interface{}{}
|
|
|
|
}
|
|
|
|
c.chunkRequestsThisTick[p] = nil
|
|
|
|
c.requestMu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FreeCaches unloads chunks that have not been requested in 2 frames.
|
|
|
|
//
|
|
|
|
// Only on chunkers that have zipfiles, old-style levels without zips
|
|
|
|
// wouldn't be able to restore their chunks otherwise! Returns -1 if
|
|
|
|
// no Zipfile, otherwise number of chunks freed.
|
|
|
|
func (c *Chunker) FreeCaches() int {
|
|
|
|
if c.Zipfile == nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
var thisTick = shmem.Tick
|
|
|
|
|
|
|
|
// Very first tick this chunker has seen?
|
|
|
|
if c.lastTick == 0 {
|
|
|
|
c.lastTick = thisTick
|
|
|
|
}
|
|
|
|
|
|
|
|
// A new tick?
|
|
|
|
if (thisTick-c.lastTick)%4 == 0 {
|
|
|
|
c.requestMu.Lock()
|
|
|
|
c.chunkMu.Lock()
|
|
|
|
defer c.requestMu.Unlock()
|
|
|
|
defer c.chunkMu.Unlock()
|
|
|
|
|
|
|
|
var (
|
|
|
|
requestsThisTick = c.chunkRequestsThisTick
|
|
|
|
requestsN2 = c.requestsN2
|
|
|
|
delete_coords = []render.Point{}
|
|
|
|
)
|
|
|
|
|
2022-05-08 00:16:03 +00:00
|
|
|
// Chunks requested 2 ticks ago but not this tick, put on the chopping
|
|
|
|
// block to free them later.
|
2022-05-08 00:42:38 +00:00
|
|
|
c.ctfMu.Lock()
|
2022-04-30 03:34:59 +00:00
|
|
|
for coord := range requestsN2 {
|
|
|
|
// Old point not requested recently?
|
|
|
|
if _, ok := requestsThisTick[coord]; !ok {
|
2022-05-08 00:16:03 +00:00
|
|
|
c.chunksToFree[coord] = shmem.Tick + balance.CanvasChunkFreeChoppingBlockTicks
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// From the chopping block, see if scheduled chunks to free are ready.
|
|
|
|
for coord, expireAt := range c.chunksToFree {
|
|
|
|
if shmem.Tick > expireAt {
|
2022-04-30 03:34:59 +00:00
|
|
|
delete_coords = append(delete_coords, coord)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-08 00:16:03 +00:00
|
|
|
// Free any eligible chunks NOW.
|
2022-04-30 03:34:59 +00:00
|
|
|
for _, coord := range delete_coords {
|
2022-05-08 00:16:03 +00:00
|
|
|
delete(c.chunksToFree, coord)
|
2022-04-30 03:34:59 +00:00
|
|
|
c.FreeChunk(coord)
|
|
|
|
}
|
2022-05-08 00:42:38 +00:00
|
|
|
c.ctfMu.Unlock()
|
2022-04-30 03:34:59 +00:00
|
|
|
|
|
|
|
// Rotate the cached ticks and clean the slate.
|
|
|
|
c.requestsN2 = c.requestsN1
|
|
|
|
c.requestsN1 = requestsThisTick
|
|
|
|
c.chunkRequestsThisTick = map[render.Point]interface{}{}
|
|
|
|
|
|
|
|
c.lastTick = thisTick
|
|
|
|
|
|
|
|
return len(delete_coords)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetChunk writes the chunk into the cache dict and nothing more.
|
|
|
|
//
|
|
|
|
// This function should be the singular writer to the chunk cache.
|
|
|
|
func (c *Chunker) SetChunk(p render.Point, chunk *Chunk) {
|
|
|
|
c.chunkMu.Lock()
|
|
|
|
c.Chunks[p] = chunk
|
|
|
|
c.chunkMu.Unlock()
|
|
|
|
|
|
|
|
c.logChunkAccess(p, chunk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FreeChunk unloads a chunk from active memory for zipfile-backed levels.
|
|
|
|
//
|
|
|
|
// Not thread safe: it is assumed the caller has the lock on c.Chunks.
|
|
|
|
func (c *Chunker) FreeChunk(p render.Point) bool {
|
|
|
|
if c.Zipfile == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-05-03 03:35:53 +00:00
|
|
|
// If this chunk has been modified since it was last loaded from ZIP, hang onto it
|
|
|
|
// in memory until the next save so we don't lose it.
|
|
|
|
if chunk, ok := c.Chunks[p]; ok {
|
|
|
|
if chunk.IsModified() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't delete empty chunks, hang on until next zipfile save.
|
|
|
|
if chunk, ok := c.Chunks[p]; ok && chunk.Len() == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2022-04-30 03:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete(c.Chunks, p)
|
|
|
|
return true
|
2018-09-23 22:20:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 02:38:37 +00:00
|
|
|
// Redraw marks every chunk as dirty and invalidates all their texture caches,
|
|
|
|
// forcing the drawing to re-generate from scratch.
|
|
|
|
func (c *Chunker) Redraw() {
|
2022-04-30 03:34:59 +00:00
|
|
|
for chunk := range c.IterChunksThemselves() {
|
2020-07-10 02:38:37 +00:00
|
|
|
chunk.SetDirty()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 03:04:24 +00:00
|
|
|
// Prerender visits every chunk and fetches its texture, in order to pre-load
|
|
|
|
// the whole drawing for smooth gameplay rather than chunks lazy rendering as
|
|
|
|
// they enter the screen.
|
|
|
|
func (c *Chunker) Prerender() {
|
2022-04-30 03:34:59 +00:00
|
|
|
for chunk := range c.IterChunksThemselves() {
|
2021-07-19 03:04:24 +00:00
|
|
|
_ = chunk.CachedBitmap(render.Invisible)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrerenderN will pre-render the texture for N number of chunks and then
|
|
|
|
// yield back to the caller. Returns the number of chunks that still need
|
|
|
|
// textures rendered; zero when the last chunk has been prerendered.
|
|
|
|
func (c *Chunker) PrerenderN(n int) (remaining int) {
|
|
|
|
var (
|
|
|
|
total int // total no. of chunks available
|
|
|
|
totalRendered int // no. of chunks with textures
|
|
|
|
modified int // number modified this call
|
|
|
|
)
|
|
|
|
|
2022-04-30 03:34:59 +00:00
|
|
|
for chunk := range c.IterChunksThemselves() {
|
2021-07-19 03:04:24 +00:00
|
|
|
total++
|
|
|
|
if chunk.bitmap != nil {
|
|
|
|
totalRendered++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if modified < n {
|
|
|
|
_ = chunk.CachedBitmap(render.Invisible)
|
|
|
|
totalRendered++
|
|
|
|
modified++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
remaining = total - totalRendered
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-23 22:20:45 +00:00
|
|
|
// Get a pixel at the given coordinate. Returns the Palette entry for that
|
|
|
|
// pixel or else returns an error if not found.
|
|
|
|
func (c *Chunker) Get(p render.Point) (*Swatch, error) {
|
|
|
|
// Compute the chunk coordinate.
|
|
|
|
coord := c.ChunkCoordinate(p)
|
2022-04-30 03:34:59 +00:00
|
|
|
if chunk, ok := c.GetChunk(coord); ok {
|
2018-09-23 22:20:45 +00:00
|
|
|
return chunk.Get(p)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("no chunk %s exists for point %s", coord, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a pixel at the given coordinate.
|
|
|
|
func (c *Chunker) Set(p render.Point, sw *Swatch) error {
|
|
|
|
coord := c.ChunkCoordinate(p)
|
2022-04-30 03:34:59 +00:00
|
|
|
chunk, ok := c.GetChunk(coord)
|
2018-09-23 22:20:45 +00:00
|
|
|
if !ok {
|
|
|
|
chunk = NewChunk()
|
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
|
|
|
chunk.Point = coord
|
|
|
|
chunk.Size = c.Size
|
2022-04-30 03:34:59 +00:00
|
|
|
c.SetChunk(coord, chunk)
|
2018-09-23 22:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return chunk.Set(p, sw)
|
|
|
|
}
|
|
|
|
|
Eraser Tool, Brush Sizes
* Implement Brush Sizes for drawtool.Stroke and add a UI to the tools panel
to control the brush size.
* Brush sizes: 1, 2, 4, 8, 16, 24, 32, 48, 64
* Add the Eraser Tool to editor mode. It uses a default brush size of 16
and a max size of 32 due to some performance issues.
* The Undo/Redo system now remembers the original color of pixels when
you change them, so that Undo will set them back how they were instead
of deleting the pixel entirely. Due to performance issues, this only
happens when your Brush Size is 0 (drawing single-pixel shapes).
* UI: Add an IntVariable option to ui.Label to bind showing the value of
an int reference.
Aforementioned performance issues:
* When we try to remember whole rects of pixels for drawing thick
shapes, it requires a ton of scanning for each step of the shape. Even
de-duplicating pixel checks, tons of extra reads are constantly
checked.
* The Eraser is the only tool that absolutely needs to be able to
remember wiped pixels AND have large brush sizes. The performance
sucks and lags a bit if you erase a lot all at once, but it's a
trade-off for now.
* So pixels aren't remembered when drawing lines in your level with
thick brushes, so the Undo action will simply delete your pixels and not
reset them. Only the Eraser can bring back pixels.
2019-07-12 02:07:46 +00:00
|
|
|
// SetRect sets a rectangle of pixels to a color all at once.
|
|
|
|
func (c *Chunker) SetRect(r render.Rect, sw *Swatch) error {
|
|
|
|
var (
|
|
|
|
xMin = r.X
|
|
|
|
yMin = r.Y
|
|
|
|
xMax = r.X + r.W
|
|
|
|
yMax = r.Y + r.H
|
|
|
|
)
|
|
|
|
for x := xMin; x < xMax; x++ {
|
|
|
|
for y := yMin; y < yMax; y++ {
|
|
|
|
c.Set(render.NewPoint(x, y), sw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-23 22:20:45 +00:00
|
|
|
// Delete a pixel at the given coordinate.
|
|
|
|
func (c *Chunker) Delete(p render.Point) error {
|
|
|
|
coord := c.ChunkCoordinate(p)
|
2019-07-17 05:10:18 +00:00
|
|
|
|
2022-04-30 03:34:59 +00:00
|
|
|
if chunk, ok := c.GetChunk(coord); ok {
|
2018-09-23 22:20:45 +00:00
|
|
|
return chunk.Delete(p)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("no chunk %s exists for point %s", coord, p)
|
|
|
|
}
|
|
|
|
|
Eraser Tool, Brush Sizes
* Implement Brush Sizes for drawtool.Stroke and add a UI to the tools panel
to control the brush size.
* Brush sizes: 1, 2, 4, 8, 16, 24, 32, 48, 64
* Add the Eraser Tool to editor mode. It uses a default brush size of 16
and a max size of 32 due to some performance issues.
* The Undo/Redo system now remembers the original color of pixels when
you change them, so that Undo will set them back how they were instead
of deleting the pixel entirely. Due to performance issues, this only
happens when your Brush Size is 0 (drawing single-pixel shapes).
* UI: Add an IntVariable option to ui.Label to bind showing the value of
an int reference.
Aforementioned performance issues:
* When we try to remember whole rects of pixels for drawing thick
shapes, it requires a ton of scanning for each step of the shape. Even
de-duplicating pixel checks, tons of extra reads are constantly
checked.
* The Eraser is the only tool that absolutely needs to be able to
remember wiped pixels AND have large brush sizes. The performance
sucks and lags a bit if you erase a lot all at once, but it's a
trade-off for now.
* So pixels aren't remembered when drawing lines in your level with
thick brushes, so the Undo action will simply delete your pixels and not
reset them. Only the Eraser can bring back pixels.
2019-07-12 02:07:46 +00:00
|
|
|
// DeleteRect deletes a rectangle of pixels between two points.
|
|
|
|
// The rect is a relative one with a width and height, and the X,Y values are
|
|
|
|
// an absolute world coordinate.
|
|
|
|
func (c *Chunker) DeleteRect(r render.Rect) error {
|
|
|
|
var (
|
|
|
|
xMin = r.X
|
|
|
|
yMin = r.Y
|
|
|
|
xMax = r.X + r.W
|
|
|
|
yMax = r.Y + r.H
|
|
|
|
)
|
|
|
|
for x := xMin; x < xMax; x++ {
|
|
|
|
for y := yMin; y < yMax; y++ {
|
|
|
|
c.Delete(render.NewPoint(x, y))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-23 22:20:45 +00:00
|
|
|
// ChunkCoordinate computes a chunk coordinate from an absolute coordinate.
|
|
|
|
func (c *Chunker) ChunkCoordinate(abs render.Point) render.Point {
|
|
|
|
if c.Size == 0 {
|
|
|
|
return render.Point{}
|
|
|
|
}
|
|
|
|
|
|
|
|
size := float64(c.Size)
|
|
|
|
return render.NewPoint(
|
2019-12-28 03:16:34 +00:00
|
|
|
int(math.Floor(float64(abs.X)/size)),
|
|
|
|
int(math.Floor(float64(abs.Y)/size)),
|
2018-09-23 22:20:45 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-05-24 02:15:10 +00:00
|
|
|
// RelativeCoordinate will translate from an absolute world coordinate, into one that
|
|
|
|
// is relative to fit inside of the chunk with the given chunk coordinate and size.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// - With 128x128 chunks and a world coordinate of (280,-600)
|
|
|
|
// - The ChunkCoordinate would be (2,-4) which encompasses (256,-512) to (383,-639)
|
|
|
|
// - And relative inside that chunk, the pixel is at (24,)
|
|
|
|
func RelativeCoordinate(abs render.Point, chunkCoord render.Point, chunkSize uint8) render.Point {
|
|
|
|
// Pixel coordinate offset.
|
2024-05-24 06:02:01 +00:00
|
|
|
var (
|
|
|
|
size = int(chunkSize)
|
|
|
|
offset = render.Point{
|
|
|
|
X: chunkCoord.X * size,
|
|
|
|
Y: chunkCoord.Y * size,
|
|
|
|
}
|
|
|
|
point = render.Point{
|
|
|
|
X: abs.X - offset.X,
|
|
|
|
Y: abs.Y - offset.Y,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
if point.X < 0 || point.Y < 0 {
|
|
|
|
log.Error("RelativeCoordinate: X < 0! abs=%s rel=%s chunk=%s size=%d", abs, point, chunkCoord, chunkSize)
|
|
|
|
log.Error("RelativeCoordinate(2): size=%d offset=%s point=%s", size, offset, point)
|
|
|
|
}
|
|
|
|
|
|
|
|
return point
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromRelativeCoordinate is the inverse of RelativeCoordinate.
|
|
|
|
//
|
|
|
|
// With a chunk size of 128 and a relative coordinate like (8, 12),
|
|
|
|
// this function will return the absolute world coordinates based
|
|
|
|
// on your chunk.Point's placement in the level.
|
|
|
|
func FromRelativeCoordinate(rel render.Point, chunkCoord render.Point, chunkSize uint8) render.Point {
|
2024-05-24 02:15:10 +00:00
|
|
|
var (
|
|
|
|
size = int(chunkSize)
|
|
|
|
offset = render.Point{
|
|
|
|
X: chunkCoord.X * size,
|
|
|
|
Y: chunkCoord.Y * size,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return render.Point{
|
2024-05-24 06:02:01 +00:00
|
|
|
X: rel.X + offset.X,
|
|
|
|
Y: rel.Y + offset.Y,
|
2024-05-24 02:15:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 22:20:45 +00:00
|
|
|
// ChunkMap maps a chunk coordinate to its chunk data.
|
|
|
|
type ChunkMap map[render.Point]*Chunk
|
|
|
|
|
|
|
|
// MarshalJSON to convert the chunk map to JSON. This is needed for writing so
|
|
|
|
// the JSON encoder knows how to serializes a `map[Point]*Chunk` but the inverse
|
|
|
|
// is not necessary to implement.
|
|
|
|
func (c ChunkMap) MarshalJSON() ([]byte, error) {
|
|
|
|
dict := map[string]*Chunk{}
|
|
|
|
for point, chunk := range c {
|
|
|
|
dict[point.String()] = chunk
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := json.Marshal(dict)
|
|
|
|
return out, err
|
|
|
|
}
|