aboutsummaryrefslogtreecommitdiff
path: root/src/terrain
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2021-07-03 00:49:03 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-03-30 08:20:20 +0200
commit6b9f686913efc3725b2690033cd4f398e07076ba (patch)
treee9aa91a6b9f617d78123ebe7ad272fc42a60d306 /src/terrain
parentc9e6ae44a9aeb89b3f48f3443d6baa80103f7445 (diff)
downloadjancity-6b9f686913efc3725b2690033cd4f398e07076ba.tar.gz
Add project source code
Diffstat (limited to 'src/terrain')
-rw-r--r--src/terrain/CMakeLists.txt3
-rw-r--r--src/terrain/inc/terrain.h41
-rw-r--r--src/terrain/src/terrain.c67
3 files changed, 111 insertions, 0 deletions
diff --git a/src/terrain/CMakeLists.txt b/src/terrain/CMakeLists.txt
new file mode 100644
index 0000000..c74de94
--- /dev/null
+++ b/src/terrain/CMakeLists.txt
@@ -0,0 +1,3 @@
+add_library(terrain "src/terrain.c")
+target_include_directories(terrain PUBLIC "inc")
+target_link_libraries(terrain PUBLIC container camera gfx)
diff --git a/src/terrain/inc/terrain.h b/src/terrain/inc/terrain.h
new file mode 100644
index 0000000..9552555
--- /dev/null
+++ b/src/terrain/inc/terrain.h
@@ -0,0 +1,41 @@
+#ifndef TERRAIN_H
+#define TERRAIN_H
+
+#include <camera.h>
+#include <gfx.h>
+#include <stddef.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+enum
+{
+ MAP_TILES = 64,
+ TERRAIN_SZ = 32,
+ MAP_X = MAP_TILES * TERRAIN_SZ,
+ MAP_Y = MAP_TILES * TERRAIN_SZ
+};
+
+enum terrain_type
+{
+ TERRAIN_TYPE_GRASS
+};
+
+struct terrain_map
+{
+ enum terrain_type m[MAP_TILES][MAP_TILES];
+ int nx, ny;
+};
+
+void terrain_init(struct terrain_map *map);
+int terrain_render(const struct terrain_map *map, const struct camera *cam);
+
+extern struct sprite grass_sprite;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* TERRAIN_H */
diff --git a/src/terrain/src/terrain.c b/src/terrain/src/terrain.c
new file mode 100644
index 0000000..04df6e6
--- /dev/null
+++ b/src/terrain/src/terrain.c
@@ -0,0 +1,67 @@
+#include <terrain.h>
+#include <camera.h>
+#include <gfx.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+
+struct sprite grass_sprite;
+
+int terrain_render(const struct terrain_map *const map,
+ const struct camera *const cam)
+{
+ const int start_x = abs(cam->x / TERRAIN_SZ),
+ start_y = abs(cam->y / TERRAIN_SZ);
+
+ const int remx = cam->x % TERRAIN_SZ,
+ remy = cam->y % TERRAIN_SZ;
+
+ int nx = map->nx, ny = map->ny;
+
+ if (abs(remx) >= TERRAIN_SZ / 2)
+ nx++;
+
+ if (abs(remy) >= TERRAIN_SZ / 2)
+ ny++;
+
+ struct m
+ {
+ size_t i;
+ int p;
+ };
+
+ for (struct m x = {.i = start_x, .p = remx};
+ x.i < nx + start_x; x.i++, x.p += TERRAIN_SZ)
+ for (struct m y = {.i = start_y, .p = remy};
+ y.i < ny + start_y; y.i++, y.p += TERRAIN_SZ)
+ {
+ struct sprite *const s = sprite_get();
+
+ if (!s)
+ return -1;
+
+ switch (map->m[y.i][x.i])
+ {
+ case TERRAIN_TYPE_GRASS:
+ if (sprite_clone(&grass_sprite, s))
+ return -1;
+
+ break;
+ }
+
+ s->x = x.p;
+ s->y = y.p;
+ sprite_sort(s);
+ }
+
+ return 0;
+}
+
+void terrain_init(struct terrain_map *const map)
+{
+ const int extra = !!(screen_w % TERRAIN_SZ);
+
+ memset(map, 0, sizeof *map);
+ map->nx = screen_w / TERRAIN_SZ + extra;
+ map->ny = screen_h / TERRAIN_SZ + extra;
+}