summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-06-23 14:45:27 +0200
committerkdx <kikoodx@paranoici.org>2023-06-23 14:45:27 +0200
commit6bcea1db383111e7dcff9c1a6eabfb526e6f45d0 (patch)
treed81bc089ac68c4ae17fa0c5ac25a79488b2021d5
parentb3ad8b4d238c7fbf9581e849cbcd532c4d70094a (diff)
downloadhmle-6bcea1db383111e7dcff9c1a6eabfb526e6f45d0.tar.gz
starting work on map
-rw-r--r--src/layer.c39
-rw-r--r--src/layer.h26
-rw-r--r--src/layerimage.h11
-rw-r--r--src/layertiles.h15
-rw-r--r--src/main.c8
-rw-r--r--src/map.c26
-rw-r--r--src/map.h14
7 files changed, 139 insertions, 0 deletions
diff --git a/src/layer.c b/src/layer.c
new file mode 100644
index 0000000..076c694
--- /dev/null
+++ b/src/layer.c
@@ -0,0 +1,39 @@
+#include "layer.h"
+#include "log.h"
+#include <string.h>
+#include <assert.h>
+
+int
+layer_init_tiles(Layer *this, Tileset *tset, int width, int height)
+{
+ memset(this, 0, sizeof(*this));
+ this->type = LAYER_TILES;
+ return layertiles_init(&this->layertiles, tset, width, height);
+}
+
+int
+layer_init_image(Layer *this, const char *path)
+{
+ memset(this, 0, sizeof(*this));
+ this->type = LAYER_IMAGE;
+ return layerimage_init(&this->layerimage, path);
+}
+
+void
+layer_deinit(Layer *this)
+{
+ switch (this->type) {
+ case LAYER_NONE:
+ break;
+ case LAYER_TILES:
+ layertiles_deinit(&this->layertiles);
+ break;
+ case LAYER_IMAGE:
+ layertiles_deinit(&this->layertiles);
+ break;
+ default:
+ assert(0);
+ break;
+ }
+ this->type = LAYER_NONE;
+}
diff --git a/src/layer.h b/src/layer.h
new file mode 100644
index 0000000..590023d
--- /dev/null
+++ b/src/layer.h
@@ -0,0 +1,26 @@
+#pragma once
+#include "layertiles.h"
+#include "layerimage.h"
+
+typedef enum LayerType {
+ LAYER_NONE,
+ LAYER_TILES,
+ LAYER_IMAGE,
+} LayerType;
+
+typedef struct Layer Layer;
+struct Layer {
+ LayerType type;
+ union {
+ LayerTiles layertiles;
+ LayerImage layerimage;
+ };
+};
+
+[[nodiscard]]
+int layer_init_tiles(Layer *this, Tileset *tset, int width, int height);
+
+[[nodiscard]]
+int layer_init_image(Layer *this, const char *path);
+
+void layer_deinit(Layer *this);
diff --git a/src/layerimage.h b/src/layerimage.h
new file mode 100644
index 0000000..06396dd
--- /dev/null
+++ b/src/layerimage.h
@@ -0,0 +1,11 @@
+#pragma once
+#include "texture.h"
+
+typedef struct LayerImage {
+ Texture *tex;
+} LayerImage;
+
+[[nodiscard]]
+int layerimage_init(LayerImage *this, const char *path);
+
+void layerimage_deinit(LayerImage *this);
diff --git a/src/layertiles.h b/src/layertiles.h
new file mode 100644
index 0000000..2cd6781
--- /dev/null
+++ b/src/layertiles.h
@@ -0,0 +1,15 @@
+#pragma once
+#include "tileset.h"
+#include <stdint.h>
+
+typedef struct LayerTiles {
+ Tileset *tset;
+ int width;
+ int height;
+ uint32_t *data;
+} LayerTiles;
+
+[[nodiscard]]
+int layertiles_init(LayerTiles *this, Tileset *tset, int width, int height);
+
+void layertiles_deinit(LayerTiles *this);
diff --git a/src/main.c b/src/main.c
index 4ca065b..d47b6a7 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2,11 +2,13 @@
#include "sdl.h"
#include "window.h"
#include "tileset.h"
+#include "map.h"
#include <errno.h>
#include <SDL2/SDL.h>
Window window = {};
Tileset tileset = {};
+Map map = {};
[[nodiscard]] static int init(const char *tset_path);
static void deinit(void);
@@ -39,12 +41,18 @@ int init(const char *tset_path)
return deinit(), -1;
}
+ if (map_init(&map)) {
+ log_fatal("map_init failed");
+ return deinit(), -1;
+ }
+
return 0;
}
static void
deinit(void)
{
+ map_deinit(&map);
tileset_deinit(&tileset);
window_deinit(&window);
sdl_deinit();
diff --git a/src/map.c b/src/map.c
new file mode 100644
index 0000000..db2317d
--- /dev/null
+++ b/src/map.c
@@ -0,0 +1,26 @@
+#include "map.h"
+#include <stdlib.h>
+#include <string.h>
+
+int
+map_init(Map *this)
+{
+ memset(this, 0, sizeof(*this));
+
+ return 0;
+}
+
+void
+map_deinit(Map *this)
+{
+ for (size_t i = 0; i < this->layers_size; i++) {
+ layer_deinit(this->layers[i]);
+ free(this->layers[i]);
+ }
+ if (this->layers) {
+ free(this->layers);
+ this->layers = NULL;
+ this->layers_size = 0;
+ this->layers_capacity = 0;
+ }
+}
diff --git a/src/map.h b/src/map.h
new file mode 100644
index 0000000..70b34d2
--- /dev/null
+++ b/src/map.h
@@ -0,0 +1,14 @@
+#pragma once
+#include "layer.h"
+#include <stddef.h>
+
+typedef struct Map {
+ Layer **layers;
+ size_t layers_size;
+ size_t layers_capacity;
+} Map;
+
+[[nodiscard]]
+int map_init(Map *this);
+
+void map_deinit(Map *this);