changeset 629:fee7bd30725c

rpg: cleanup tileset-loader-file
author David Demelier <markand@malikania.fr>
date Sat, 26 Aug 2023 16:09:35 +0200
parents c2b62ff38224
children 8d8fe99b357c
files examples/example-map/example-map.c examples/example-tileset/example-tileset.c libmlk-core/mlk/core/util.h libmlk-rpg/mlk/rpg/map-loader-file.c libmlk-rpg/mlk/rpg/tileset-loader-file.c libmlk-rpg/mlk/rpg/tileset-loader-file.h tests/test-tileset.c
diffstat 7 files changed, 71 insertions(+), 79 deletions(-) [+]
line wrap: on
line diff
--- a/examples/example-map/example-map.c	Sat Aug 26 15:36:07 2023 +0200
+++ b/examples/example-map/example-map.c	Sat Aug 26 16:09:35 2023 +0200
@@ -56,7 +56,7 @@
 #include <assets/maps/world.h>
 #include <assets/tilesets/world.h>
 
-static struct mlk_tileset_loader tileset_loader;
+static struct mlk_tileset_loader_file tileset_loader;
 static struct mlk_tileset tileset;
 
 static struct mlk_map_loader map_loader;
@@ -140,7 +140,7 @@
 	 */
 	mlk_tracef("Searching tileset %s", ident);
 
-	if (mlk_tileset_loader_openmem(&tileset_loader, &tileset, assets_tilesets_world, sizeof (assets_tilesets_world)) < 0)
+	if (mlk_tileset_loader_openmem(&tileset_loader.iface, &tileset, assets_tilesets_world, sizeof (assets_tilesets_world)) < 0)
 		mlk_panic();
 
 	return &tileset;
@@ -158,7 +158,7 @@
 	 * mlk_tileset_loader_file.
 	 */
 	mlk_tileset_loader_file_init(&tileset_loader, "");
-	tileset_loader.new_texture = tileset_new_texture;
+	tileset_loader.iface.new_texture = tileset_new_texture;
 
 	/*
 	 * Create our map loader. It will also search for a tileset to be found
--- a/examples/example-tileset/example-tileset.c	Sat Aug 26 15:36:07 2023 +0200
+++ b/examples/example-tileset/example-tileset.c	Sat Aug 26 16:09:35 2023 +0200
@@ -49,7 +49,7 @@
 
 #include <assets/tilesets/world.h>
 
-static struct mlk_tileset_loader loader;
+static struct mlk_tileset_loader_file loader;
 static struct mlk_tileset tileset;
 
 /*
@@ -127,11 +127,11 @@
 	 * sprites animations are statically allocated.
 	 */
 	mlk_tileset_loader_file_init(&loader, "");
-	loader.new_texture = new_texture;
-	loader.new_sprite = new_sprite;
-	loader.new_animation = new_animation;
+	loader.iface.new_texture = new_texture;
+	loader.iface.new_sprite = new_sprite;
+	loader.iface.new_animation = new_animation;
 
-	if (mlk_tileset_loader_openmem(&loader, &tileset, assets_tilesets_world, sizeof (assets_tilesets_world)) < 0)
+	if (mlk_tileset_loader_openmem(&loader.iface, &tileset, assets_tilesets_world, sizeof (assets_tilesets_world)) < 0)
 		mlk_panic();
 }
 
--- a/libmlk-core/mlk/core/util.h	Sat Aug 26 15:36:07 2023 +0200
+++ b/libmlk-core/mlk/core/util.h	Sat Aug 26 16:09:35 2023 +0200
@@ -24,6 +24,7 @@
  * \brief libmlk-core utilities
  */
 
+#include <stddef.h>
 #include <stdint.h>
 
 /**
@@ -34,6 +35,12 @@
  */
 #define MLK_UTIL_SIZE(x) (sizeof ((x)) / sizeof ((x)[0]))
 
+/**
+ * Obtain parent container from a member field.
+ */
+#define MLK_CONTAINER_OF(ptr, type, member) \
+	((type*)((char*)(1 ? (ptr) : &((type*)0)->member) - offsetof(type, member)))
+
 #if defined(__cplusplus)
 extern "C" {
 #endif
--- a/libmlk-rpg/mlk/rpg/map-loader-file.c	Sat Aug 26 15:36:07 2023 +0200
+++ b/libmlk-rpg/mlk/rpg/map-loader-file.c	Sat Aug 26 16:09:35 2023 +0200
@@ -46,7 +46,7 @@
 	 * We use a tileset file loader if new_tileset function isn't present in
 	 * this map loader.
 	 */
-	struct mlk_tileset_loader tileset_loader;
+	struct mlk_tileset_loader_file tileset_loader;
 	struct mlk_tileset tileset;
 
 	/* Own allocated blocks. */
@@ -121,7 +121,7 @@
 	mlk_tileset_loader_file_finish(&self->tileset_loader);
 	mlk_tileset_loader_file_init(&self->tileset_loader, path);
 
-	if (mlk_tileset_loader_open(&self->tileset_loader, &self->tileset, path) < 0)
+	if (mlk_tileset_loader_open(&self->tileset_loader.iface, &self->tileset, path) < 0)
 		return NULL;
 
 	return &self->tileset;
--- a/libmlk-rpg/mlk/rpg/tileset-loader-file.c	Sat Aug 26 15:36:07 2023 +0200
+++ b/libmlk-rpg/mlk/rpg/tileset-loader-file.c	Sat Aug 26 16:09:35 2023 +0200
@@ -24,45 +24,15 @@
 #include <mlk/core/image.h>
 #include <mlk/core/sprite.h>
 #include <mlk/core/texture.h>
+#include <mlk/core/util.h>
 
 #include "loader-file_p.h"
 #include "tileset-loader-file.h"
 #include "tileset-loader.h"
 #include "tileset.h"
 
-struct self {
-	/* Resources allocator. */
-	struct mlk__loader_file *loader;
-
-	/* Arrays reallocated on purpose. */
-	struct mlk_tileset_collision *tilecollisions;
-	struct mlk_tileset_animation *tileanimations;
-};
-
-static struct self *
-self_new(const char *path)
-{
-	struct self *self;
-
-	if (!(self = mlk_alloc_new0(1, sizeof (*self))))
-		return NULL;
-	if (!(self->loader = mlk__loader_file_new(path))) {
-		mlk_alloc_free(self);
-		return NULL;
-	}
-
-	return self;
-}
-
-static void
-self_free(struct self *self)
-{
-	mlk__loader_file_free(self->loader);
-
-	/* Clear array of collisions/animations .*/
-	mlk_alloc_free(self->tilecollisions);
-	mlk_alloc_free(self->tileanimations);
-}
+#define THIS(loader) \
+	MLK_CONTAINER_OF(loader, struct mlk_tileset_loader_file, iface)
 
 static void *
 expand(void **array, size_t n, size_t w)
@@ -85,9 +55,9 @@
 {
 	(void)tileset;
 
-	struct self *self = loader->data;
+	struct mlk_tileset_loader_file *self = THIS(loader);
 
-	return mlk__loader_file_texture_open(self->loader, ident);
+	return mlk__loader_file_texture_open(self->lf, ident);
 }
 
 static struct mlk_sprite *
@@ -95,9 +65,9 @@
 {
 	(void)tileset;
 
-	struct self *self = loader->data;
+	struct mlk_tileset_loader_file *self = THIS(loader);
 
-	return mlk__loader_file_sprite_new(self->loader);
+	return mlk__loader_file_sprite_new(self->lf);
 }
 
 static struct mlk_animation *
@@ -105,9 +75,9 @@
 {
 	(void)tileset;
 
-	struct self *self = loader->data;
+	struct mlk_tileset_loader_file *self = THIS(loader);
 
-	return mlk__loader_file_animation_new(self->loader);
+	return mlk__loader_file_animation_new(self->lf);
 }
 
 struct mlk_tileset_collision *
@@ -119,7 +89,7 @@
 	(void)tileset;
 	(void)array;
 
-	struct self *self = loader->data;
+	struct mlk_tileset_loader_file *self = THIS(loader);
 
 	return expand((void **)&self->tilecollisions, arraysz, sizeof (struct mlk_tileset_collision));
 }
@@ -133,43 +103,40 @@
 	(void)tileset;
 	(void)array;
 
-	struct self *self = loader->data;
+	struct mlk_tileset_loader_file *self = THIS(loader);
 
 	return expand((void **)&self->tileanimations, arraysz, sizeof (struct mlk_tileset_animation));
 }
 
 int
-mlk_tileset_loader_file_init(struct mlk_tileset_loader *loader, const char *filename)
+mlk_tileset_loader_file_init(struct mlk_tileset_loader_file *file, const char *filename)
 {
-	assert(loader);
+	assert(file);
 	assert(filename);
 
-	struct self *self;
+	memset(file, 0, sizeof (*file));
 
-	memset(loader, 0, sizeof (*loader));
-
-	if (!(self = self_new(filename)))
+	if (!(file->lf = mlk__loader_file_new(filename)))
 		return -1;
 
-	loader->data = self;
-	loader->new_texture = new_texture;
-	loader->new_sprite = new_sprite;
-	loader->new_animation = new_animation;
-	loader->expand_collisions = expand_collisions;
-	loader->expand_animations = expand_animations;
+	file->iface.new_texture = new_texture;
+	file->iface.new_sprite = new_sprite;
+	file->iface.new_animation = new_animation;
+	file->iface.expand_collisions = expand_collisions;
+	file->iface.expand_animations = expand_animations;
 
 	return 0;
 }
 
 void
-mlk_tileset_loader_file_finish(struct mlk_tileset_loader *loader)
+mlk_tileset_loader_file_finish(struct mlk_tileset_loader_file *file)
 {
-	assert(loader);
+	assert(file);
 
-	struct self *self = loader->data;
+	mlk__loader_file_free(file->lf);
 
-	if (self)
-		self_free(self);
+	mlk_alloc_free(file->tilecollisions);
+	mlk_alloc_free(file->tileanimations);
 
-	memset(loader, 0, sizeof (*loader));
+	memset(file, 0, sizeof (*file));
 }
--- a/libmlk-rpg/mlk/rpg/tileset-loader-file.h	Sat Aug 26 15:36:07 2023 +0200
+++ b/libmlk-rpg/mlk/rpg/tileset-loader-file.h	Sat Aug 26 16:09:35 2023 +0200
@@ -31,7 +31,26 @@
  * the allocator functions can be changed.
  */
 
-struct mlk_tileset_loader;
+#include "tileset-loader.h"
+
+struct mlk_tileset_collision;
+struct mlk_tileset_animation;
+struct mlk__loader_file;
+
+struct mlk_tileset_loader_file {
+	/**
+	 * (read-write)
+	 *
+	 * Underlying tileset loader.
+	 */
+	struct mlk_tileset_loader iface;
+	
+	/** \cond MLK_PRIVATE_DECLS */
+	struct mlk_tileset_collision *tilecollisions;
+	struct mlk_tileset_animation *tileanimations;
+	struct mlk__loader_file *lf;
+	/** \endcond MLK_PRIVATE_DECLS */
+};
 
 #if defined(__cplusplus)
 extern "C" {
@@ -44,24 +63,23 @@
  * After loading the tileset with this underlying loader, it should be kept
  * until the tileset is no longer used.
  *
- * \pre loader != NULL
+ * \pre file != NULL
  * \pre filename != NULL
- * \param loader the abstract loader interface
+ * \param file the abstract loader interface
  * \param filename path to the tileset file
  * \return 0 on success or -1 on error
  */
 int
-mlk_tileset_loader_file_init(struct mlk_tileset_loader *loader,
-                             const char *filename);
+mlk_tileset_loader_file_init(struct mlk_tileset_loader_file *file, const char *filename);
 
 /**
  * Cleanup allocated resources by this file loader.
  *
- * \pre loader != NULL
- * \param loader the file loader
+ * \pre file != NULL
+ * \param file the file loader
  */
 void
-mlk_tileset_loader_file_finish(struct mlk_tileset_loader *loader);
+mlk_tileset_loader_file_finish(struct mlk_tileset_loader_file *file);
 
 #if defined(__cplusplus)
 }
--- a/tests/test-tileset.c	Sat Aug 26 15:36:07 2023 +0200
+++ b/tests/test-tileset.c	Sat Aug 26 16:09:35 2023 +0200
@@ -31,7 +31,7 @@
  * Convenient struct that pack all the required data.
  */
 struct tileset {
-	struct mlk_tileset_loader loader;
+	struct mlk_tileset_loader_file loader;
 	struct mlk_tileset tileset;
 };
 
@@ -40,7 +40,7 @@
 {
 	mlk_tileset_loader_file_init(&ts->loader, path);
 
-	return mlk_tileset_loader_open(&ts->loader, &ts->tileset, path);
+	return mlk_tileset_loader_open(&ts->loader.iface, &ts->tileset, path);
 }
 
 static inline void