diff options
author | kdx <kikoodx@paranoici.org> | 2023-06-23 16:53:29 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-06-23 16:53:29 +0200 |
commit | 626d5acda19d9bcc9c4e13e90fbe1f605826ba93 (patch) | |
tree | cd6e2ad055ffd01b673ab283e6a040828e7dbba9 | |
parent | 6bcea1db383111e7dcff9c1a6eabfb526e6f45d0 (diff) | |
download | hmle-626d5acda19d9bcc9c4e13e90fbe1f605826ba93.tar.gz |
layer code
-rw-r--r-- | src/layer.c | 18 | ||||
-rw-r--r-- | src/layer.h | 5 | ||||
-rw-r--r-- | src/layerimage.c | 25 | ||||
-rw-r--r-- | src/layerimage.h | 8 | ||||
-rw-r--r-- | src/layertiles.c | 28 |
5 files changed, 73 insertions, 11 deletions
diff --git a/src/layer.c b/src/layer.c index 076c694..f5732c3 100644 --- a/src/layer.c +++ b/src/layer.c @@ -3,20 +3,26 @@ #include <string.h> #include <assert.h> -int -layer_init_tiles(Layer *this, Tileset *tset, int width, int height) +static void +_layer_init(Layer *this, Window *window) { memset(this, 0, sizeof(*this)); this->type = LAYER_TILES; + this->win = window; +} + +int +layer_init_tiles(Layer *this, Window *window, Tileset *tset, int width, int height) +{ + _layer_init(this, window); return layertiles_init(&this->layertiles, tset, width, height); } int -layer_init_image(Layer *this, const char *path) +layer_init_image(Layer *this, Window *window, const char *path) { - memset(this, 0, sizeof(*this)); - this->type = LAYER_IMAGE; - return layerimage_init(&this->layerimage, path); + _layer_init(this, window); + return layerimage_init(this, path); } void diff --git a/src/layer.h b/src/layer.h index 590023d..201e765 100644 --- a/src/layer.h +++ b/src/layer.h @@ -11,6 +11,7 @@ typedef enum LayerType { typedef struct Layer Layer; struct Layer { LayerType type; + Window *win; union { LayerTiles layertiles; LayerImage layerimage; @@ -18,9 +19,9 @@ struct Layer { }; [[nodiscard]] -int layer_init_tiles(Layer *this, Tileset *tset, int width, int height); +int layer_init_tiles(Layer *this, Window *window, Tileset *tset, int width, int height); [[nodiscard]] -int layer_init_image(Layer *this, const char *path); +int layer_init_image(Layer *this, Window *window, const char *path); void layer_deinit(Layer *this); diff --git a/src/layerimage.c b/src/layerimage.c new file mode 100644 index 0000000..87f6166 --- /dev/null +++ b/src/layerimage.c @@ -0,0 +1,25 @@ +#include "layerimage.h" +#include "layer.h" +#include "log.h" + +int +layerimage_init(Layer *this, const char *path) +{ + LayerImage *const lay = &this->layerimage; + memset(lay, 0, sizeof(*lay)); + + if (texture_load(&lay->tex, this->win, path)) { + log_error("texture_load failed"); + return -1; + } + + return 0; +} + +void +layerimage_deinit(Layer *this) +{ + LayerImage *const lay = &this->layerimage; + + texture_deinit(&lay->tex); +} diff --git a/src/layerimage.h b/src/layerimage.h index 06396dd..ba64c62 100644 --- a/src/layerimage.h +++ b/src/layerimage.h @@ -2,10 +2,12 @@ #include "texture.h" typedef struct LayerImage { - Texture *tex; + Texture tex; } LayerImage; +struct Layer; + [[nodiscard]] -int layerimage_init(LayerImage *this, const char *path); +int layerimage_init(struct Layer *this, const char *path); -void layerimage_deinit(LayerImage *this); +void layerimage_deinit(struct Layer *this); diff --git a/src/layertiles.c b/src/layertiles.c new file mode 100644 index 0000000..9dc8225 --- /dev/null +++ b/src/layertiles.c @@ -0,0 +1,28 @@ +#include "layertiles.h" +#include "log.h" + +int +layertiles_init(LayerTiles *this, Tileset *tset, int width, int height) +{ + memset(this, 0, sizeof(*this)); + + this->tset = tset; + this->width = width; + this->height = height; + this->data = calloc(width * height, 1); + if (this->data == NULL) { + log_error("calloc: %m"); + return -1; + } + + return 0; +} + +void +layertiles_deinit(LayerTiles *this) +{ + if (this->data) { + free(this->data); + this->data = NULL; + } +} |