Skip to content

Commit

Permalink
use char in map for memory savings
Browse files Browse the repository at this point in the history
  • Loading branch information
fogleman committed Jan 31, 2014
1 parent 9d8ac0b commit 42e6dbc
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 55 deletions.
71 changes: 37 additions & 34 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,9 +591,9 @@ int highest_block(float x, float z) {
Chunk *chunk = find_chunk(p, q);
if (chunk) {
Map *map = &chunk->map;
MAP_FOR_EACH(map, e) {
if (is_obstacle(e->w) && e->x == nx && e->z == nz) {
result = MAX(result, e->y);
MAP_FOR_EACH(map, ex, ey, ez, ew) {
if (is_obstacle(ew) && ex == nx && ez == nz) {
result = MAX(result, ey);
}
} END_MAP_FOR_EACH;
}
Expand Down Expand Up @@ -984,11 +984,11 @@ void compute_chunk(WorkerItem *item) {
if (!map) {
continue;
}
MAP_FOR_EACH(map, e) {
int x = e->x - ox;
int y = e->y - oy;
int z = e->z - oz;
int w = e->w;
MAP_FOR_EACH(map, ex, ey, ez, ew) {
int x = ex - ox;
int y = ey - oy;
int z = ez - oz;
int w = ew;
// TODO: this should be unnecessary
if (x < 0 || y < 0 || z < 0) {
continue;
Expand All @@ -1013,11 +1013,11 @@ void compute_chunk(WorkerItem *item) {
if (!map) {
continue;
}
MAP_FOR_EACH(map, e) {
int x = e->x - ox;
int y = e->y - oy;
int z = e->z - oz;
light_fill(opaque, light, x, y, z, e->w, 1);
MAP_FOR_EACH(map, ex, ey, ez, ew) {
int x = ex - ox;
int y = ey - oy;
int z = ez - oz;
light_fill(opaque, light, x, y, z, ew, 1);
} END_MAP_FOR_EACH;
}
}
Expand All @@ -1029,45 +1029,45 @@ void compute_chunk(WorkerItem *item) {
int miny = 256;
int maxy = 0;
int faces = 0;
MAP_FOR_EACH(map, e) {
if (e->w <= 0) {
MAP_FOR_EACH(map, ex, ey, ez, ew) {
if (ew <= 0) {
continue;
}
int x = e->x - ox;
int y = e->y - oy;
int z = e->z - oz;
int x = ex - ox;
int y = ey - oy;
int z = ez - oz;
int f1 = !opaque[XYZ(x - 1, y, z)];
int f2 = !opaque[XYZ(x + 1, y, z)];
int f3 = !opaque[XYZ(x, y + 1, z)];
int f4 = !opaque[XYZ(x, y - 1, z)] && (e->y > 0);
int f4 = !opaque[XYZ(x, y - 1, z)] && (ey > 0);
int f5 = !opaque[XYZ(x, y, z - 1)];
int f6 = !opaque[XYZ(x, y, z + 1)];
int total = f1 + f2 + f3 + f4 + f5 + f6;
if (total == 0) {
continue;
}
if (is_plant(e->w)) {
if (is_plant(ew)) {
total = 4;
}
miny = MIN(miny, e->y);
maxy = MAX(maxy, e->y);
miny = MIN(miny, ey);
maxy = MAX(maxy, ey);
faces += total;
} END_MAP_FOR_EACH;

// generate geometry
GLfloat *data = malloc_faces(10, faces);
int offset = 0;
MAP_FOR_EACH(map, e) {
if (e->w <= 0) {
MAP_FOR_EACH(map, ex, ey, ez, ew) {
if (ew <= 0) {
continue;
}
int x = e->x - ox;
int y = e->y - oy;
int z = e->z - oz;
int x = ex - ox;
int y = ey - oy;
int z = ez - oz;
int f1 = !opaque[XYZ(x - 1, y, z)];
int f2 = !opaque[XYZ(x + 1, y, z)];
int f3 = !opaque[XYZ(x, y + 1, z)];
int f4 = !opaque[XYZ(x, y - 1, z)] && (e->y > 0);
int f4 = !opaque[XYZ(x, y - 1, z)] && (ey > 0);
int f5 = !opaque[XYZ(x, y, z - 1)];
int f6 = !opaque[XYZ(x, y, z + 1)];
int total = f1 + f2 + f3 + f4 + f5 + f6;
Expand Down Expand Up @@ -1099,7 +1099,7 @@ void compute_chunk(WorkerItem *item) {
float ao[6][4];
float light[6][4];
occlusion(neighbors, lights, shades, ao, light);
if (is_plant(e->w)) {
if (is_plant(ew)) {
total = 4;
float min_ao = 1;
float max_light = 0;
Expand All @@ -1109,16 +1109,16 @@ void compute_chunk(WorkerItem *item) {
max_light = MAX(max_light, light[a][b]);
}
}
float rotation = simplex2(e->x, e->z, 4, 0.5, 2) * 360;
float rotation = simplex2(ex, ez, 4, 0.5, 2) * 360;
make_plant(
data + offset, min_ao, max_light,
e->x, e->y, e->z, 0.5, e->w, rotation);
ex, ey, ez, 0.5, ew, rotation);
}
else {
make_cube(
data + offset, ao, light,
f1, f2, f3, f4, f5, f6,
e->x, e->y, e->z, 0.5, e->w);
ex, ey, ez, 0.5, ew);
}
offset += total * 60;
} END_MAP_FOR_EACH;
Expand Down Expand Up @@ -1201,8 +1201,11 @@ void init_chunk(Chunk *chunk, int p, int q) {
db_load_signs(signs, p, q);
Map *block_map = &chunk->map;
Map *light_map = &chunk->lights;
map_alloc(block_map, 0x7fff);
map_alloc(light_map, 0xf);
int dx = p * CHUNK_SIZE - 1;
int dy = 0;
int dz = q * CHUNK_SIZE - 1;
map_alloc(block_map, dx, dy, dz, 0x7fff);
map_alloc(light_map, dx, dy, dz, 0xf);
}

void create_chunk(Chunk *chunk, int p, int q) {
Expand Down
42 changes: 30 additions & 12 deletions src/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ int hash(int x, int y, int z) {
return x ^ y ^ z;
}

void map_alloc(Map *map, int mask) {
void map_alloc(Map *map, int dx, int dy, int dz, int mask) {
map->dx = dx;
map->dy = dy;
map->dz = dz;
map->mask = mask;
map->size = 0;
map->data = (MapEntry *)calloc(map->mask + 1, sizeof(MapEntry));
Expand All @@ -30,6 +33,9 @@ void map_free(Map *map) {
}

void map_copy(Map *dst, Map *src) {
dst->dx = src->dx;
dst->dy = src->dy;
dst->dz = src->dz;
dst->mask = src->mask;
dst->size = src->size;
dst->data = (MapEntry *)calloc(dst->mask + 1, sizeof(MapEntry));
Expand All @@ -38,27 +44,30 @@ void map_copy(Map *dst, Map *src) {

int map_set(Map *map, int x, int y, int z, int w) {
unsigned int index = hash(x, y, z) & map->mask;
x -= map->dx;
y -= map->dy;
z -= map->dz;
MapEntry *entry = map->data + index;
int overwrite = 0;
while (!EMPTY_ENTRY(entry)) {
if (entry->x == x && entry->y == y && entry->z == z) {
if (entry->e.x == x && entry->e.y == y && entry->e.z == z) {
overwrite = 1;
break;
}
index = (index + 1) & map->mask;
entry = map->data + index;
}
if (overwrite) {
if (entry->w != w) {
entry->w = w;
if (entry->e.w != w) {
entry->e.w = w;
return 1;
}
}
else if (w) {
entry->x = x;
entry->y = y;
entry->z = z;
entry->w = w;
entry->e.x = x;
entry->e.y = y;
entry->e.z = z;
entry->e.w = w;
map->size++;
if (map->size * 2 > map->mask) {
map_grow(map);
Expand All @@ -70,10 +79,16 @@ int map_set(Map *map, int x, int y, int z, int w) {

int map_get(Map *map, int x, int y, int z) {
unsigned int index = hash(x, y, z) & map->mask;
x -= map->dx;
y -= map->dy;
z -= map->dz;
if (x < 0 || x > 255) return 0;
if (y < 0 || y > 255) return 0;
if (z < 0 || z > 255) return 0;
MapEntry *entry = map->data + index;
while (!EMPTY_ENTRY(entry)) {
if (entry->x == x && entry->y == y && entry->z == z) {
return entry->w;
if (entry->e.x == x && entry->e.y == y && entry->e.z == z) {
return entry->e.w;
}
index = (index + 1) & map->mask;
entry = map->data + index;
Expand All @@ -83,11 +98,14 @@ int map_get(Map *map, int x, int y, int z) {

void map_grow(Map *map) {
Map new_map;
new_map.dx = map->dx;
new_map.dy = map->dy;
new_map.dz = map->dz;
new_map.mask = (map->mask << 1) | 1;
new_map.size = 0;
new_map.data = (MapEntry *)calloc(new_map.mask + 1, sizeof(MapEntry));
MAP_FOR_EACH(map, entry) {
map_set(&new_map, entry->x, entry->y, entry->z, entry->w);
MAP_FOR_EACH(map, ex, ey, ez, ew) {
map_set(&new_map, ex, ey, ez, ew);
} END_MAP_FOR_EACH;
free(map->data);
map->mask = new_map.mask;
Expand Down
28 changes: 19 additions & 9 deletions src/map.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
#ifndef _map_h_
#define _map_h_

#define EMPTY_ENTRY(e) (!(e)->x && !(e)->y && !(e)->z && !(e)->w)
#define EMPTY_ENTRY(entry) ((entry)->value == 0)

#define MAP_FOR_EACH(map, entry) \
#define MAP_FOR_EACH(map, ex, ey, ez, ew) \
for (unsigned int i = 0; i <= map->mask; i++) { \
MapEntry *entry = map->data + i; \
if (EMPTY_ENTRY(entry)) { \
continue; \
}
} \
int ex = entry->e.x + map->dx; \
int ey = entry->e.y + map->dy; \
int ez = entry->e.z + map->dz; \
int ew = entry->e.w;

#define END_MAP_FOR_EACH }

typedef struct {
int x;
int y;
int z;
int w;
typedef union {
unsigned int value;
struct {
unsigned char x;
unsigned char y;
unsigned char z;
char w;
} e;
} MapEntry;

typedef struct {
int dx;
int dy;
int dz;
unsigned int mask;
unsigned int size;
MapEntry *data;
} Map;

void map_alloc(Map *map, int mask);
void map_alloc(Map *map, int dx, int dy, int dz, int mask);
void map_free(Map *map);
void map_copy(Map *dst, Map *src);
void map_grow(Map *map);
Expand Down

0 comments on commit 42e6dbc

Please sign in to comment.