1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
// SPDX-FileCopyrightText: 2024 Lizzy Fleckenstein <lizzy@vlhl.dev>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
#include <stdlib.h>
#include <string.h>
#include "chunk.h"
node *chunk_col(chunk c, uint32_t x)
{
return &c.data[c.pitch*x];
}
node *chunk_index(chunk c, uvec2 v)
{
return &chunk_col(c, v.x)[v.y];
}
node *chunk_index_abs(chunk c, vec2 v)
{
if (box2_contains(c.bounds, v))
return chunk_index(c, CUVEC2(vec2_sub(v, c.bounds.pos)));
else
return NULL;
}
chunk chunk_sub(chunk c, box2 area)
{
return (chunk) {
.pitch = c.pitch,
.bounds = area,
.data = chunk_index(c, CUVEC2(vec2_sub(area.pos, c.bounds.pos))),
};
}
chunk chunk_alloc(box2 b)
{
return (chunk) {
.pitch = b.size.y,
.bounds = b,
.data = calloc(b.size.x * b.size.y, sizeof(node)),
};
}
void chunk_copy(chunk dst, chunk src)
{
box2 area = box2_overlap(dst.bounds, src.bounds);
if (box2_empty(area))
return;
chunk d = chunk_sub(dst, area);
chunk s = chunk_sub(src, area);
if (d.pitch == area.size.y && s.pitch == area.size.y)
memcpy(d.data, s.data, area.size.x * area.size.y * sizeof(node));
else for (uint32_t x = 0; x < area.size.x; x++)
memcpy(chunk_col(d, x), chunk_col(s, x), area.size.y * sizeof(node));
}
void chunk_clear(chunk c)
{
if (c.bounds.size.y == c.pitch)
memset(c.data, 0, c.bounds.size.x * c.bounds.size.y * sizeof(node));
else for (uint32_t x = 0; x < c.bounds.size.x; x++)
memset(chunk_col(c, x), 0, c.bounds.size.y * sizeof(node));
}
|