changeset 516:6af0524913b3

misc: fix
author David Demelier <markand@malikania.fr>
date Sat, 04 Mar 2023 11:23:10 +0100
parents 2e05c1804b25
children 6e8f6640e05b
files examples/example-battle/example-battle.c examples/example-drawable/example-drawable.c libmlk-core/mlk/core/drawable-stack.c libmlk-core/mlk/core/drawable-stack.h libmlk-core/mlk/core/sprite.c libmlk-example/mlk/example/registry.c libmlk-rpg/mlk/rpg/tileset-file.c tests/test-drawable.c
diffstat 8 files changed, 60 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/examples/example-battle/example-battle.c	Sat Mar 04 11:02:48 2023 +0100
+++ b/examples/example-battle/example-battle.c	Sat Mar 04 11:23:10 2023 +0100
@@ -185,7 +185,10 @@
 };
 
 static struct mlk_drawable *drawables[16];
-static struct mlk_drawable_stack drawable_stack;
+static struct mlk_drawable_stack drawable_stack = {
+	.objects = drawables,
+	.objectsz = MLK_UTIL_SIZE(drawables)
+};
 
 static struct mlk_action *actions[16];
 static struct mlk_action_stack action_stack = {
@@ -203,7 +206,7 @@
 prepare_to_fight(void)
 {
 	mlk_action_stack_init(&action_stack);
-	mlk_drawable_stack_init(&drawable_stack, drawables, MLK_UTIL_SIZE(drawables));
+	mlk_drawable_stack_init(&drawable_stack);
 
 	battle_init(&bt);
 	battle_bar_default_init(&default_bar);
--- a/examples/example-drawable/example-drawable.c	Sat Mar 04 11:02:48 2023 +0100
+++ b/examples/example-drawable/example-drawable.c	Sat Mar 04 11:23:10 2023 +0100
@@ -51,7 +51,10 @@
 
 static struct mlk_state *states[1];
 static struct mlk_drawable *drawables[64];
-static struct mlk_drawable_stack stack;
+static struct mlk_drawable_stack stack = {
+	.objects = drawables,
+	.objectsz = MLK_UTIL_SIZE(drawables)
+};
 
 /*
  * List of drawables for this example.
@@ -108,7 +111,7 @@
 
 	ex->dw.data = ex;
 	ex->anim.sprite = explosion_sprite;
-	ex->anim.delay = 18;
+	ex->anim.delay = 1000 / 60;
 	ex->dw.x = x - (int)(explosion_sprite->cellw / 2);
 	ex->dw.y = y - (int)(explosion_sprite->cellh / 2);
 	ex->dw.update = explosion_update;
@@ -174,7 +177,7 @@
 		.draw = draw
 	};
 
-	mlk_drawable_stack_init(&stack, drawables, MLK_UTIL_SIZE(drawables));
+	mlk_drawable_stack_init(&stack);
 
 	mlk_game_init(states, MLK_UTIL_SIZE(states));
 	mlk_game_push(&state);
--- a/libmlk-core/mlk/core/drawable-stack.c	Sat Mar 04 11:02:48 2023 +0100
+++ b/libmlk-core/mlk/core/drawable-stack.c	Sat Mar 04 11:23:10 2023 +0100
@@ -27,13 +27,10 @@
 	for (size_t i = 0; i < (st)->objectsz && ((iter) = (st)->objects[i], 1); ++i)
 
 void
-mlk_drawable_stack_init(struct mlk_drawable_stack *st, struct mlk_drawable **objects, size_t objectsz)
+mlk_drawable_stack_init(struct mlk_drawable_stack *st)
 {
 	assert(st);
 
-	st->objects = objects;
-	st->objectsz = objectsz;
-
 	for (size_t i = 0; i < st->objectsz; ++i)
 		st->objects[i] = NULL;
 }
--- a/libmlk-core/mlk/core/drawable-stack.h	Sat Mar 04 11:02:48 2023 +0100
+++ b/libmlk-core/mlk/core/drawable-stack.h	Sat Mar 04 11:23:10 2023 +0100
@@ -31,7 +31,7 @@
 MLK_CORE_BEGIN_DECLS
 
 void
-mlk_drawable_stack_init(struct mlk_drawable_stack *, struct mlk_drawable **, size_t);
+mlk_drawable_stack_init(struct mlk_drawable_stack *);
 
 int
 mlk_drawable_stack_add(struct mlk_drawable_stack *, struct mlk_drawable *);
--- a/libmlk-core/mlk/core/sprite.c	Sat Mar 04 11:02:48 2023 +0100
+++ b/libmlk-core/mlk/core/sprite.c	Sat Mar 04 11:23:10 2023 +0100
@@ -24,10 +24,11 @@
 void
 mlk_sprite_init(struct mlk_sprite *sprite)
 {
-	assert(mlk_sprite_ok(sprite));
+	assert(sprite);
+	assert(mlk_texture_ok(sprite->texture));
 
-	sprite->nrows = tex->h / cellh;
-	sprite->ncols = tex->w / cellw;
+	sprite->nrows = sprite->texture->h / sprite->cellh;
+	sprite->ncols = sprite->texture->w / sprite->cellw;
 }
 
 int
--- a/libmlk-example/mlk/example/registry.c	Sat Mar 04 11:02:48 2023 +0100
+++ b/libmlk-example/mlk/example/registry.c	Sat Mar 04 11:23:10 2023 +0100
@@ -130,10 +130,16 @@
 		if ((err = mlk_image_openmem(texture, textures[i].data, textures[i].datasz)) < 0)
 			mlk_panic(err);
 
-		if (textures[i].cellw == 0 || textures[i].cellh == 0)
-			mlk_sprite_init(sprite, texture, texture->w, texture->h);
-		else
-			mlk_sprite_init(sprite, texture, textures[i].cellw, textures[i].cellh);
+		if (textures[i].cellw == 0 || textures[i].cellh == 0) {
+			sprite->cellw = texture->w;
+			sprite->cellh = texture->h;
+		} else {
+			sprite->cellw = textures[i].cellw;
+			sprite->cellh = textures[i].cellh;
+		}
+
+		sprite->texture = texture;
+		mlk_sprite_init(sprite);
 	}
 }
 
--- a/libmlk-rpg/mlk/rpg/tileset-file.c	Sat Mar 04 11:02:48 2023 +0100
+++ b/libmlk-rpg/mlk/rpg/tileset-file.c	Sat Mar 04 11:23:10 2023 +0100
@@ -195,10 +195,14 @@
 		if (mlk_image_open(&anim->texture, mlk_util_pathf("%s/%s", ctx->basedir, filename)) < 0)
 			return -1;
 
-		mlk_sprite_init(&anim->sprite, &anim->texture, ctx->tilewidth, ctx->tileheight);
+		anim->sprite.texture = &anim->texture;
+		anim->sprite.cellw = ctx->tilewidth;
+		anim->sprite.cellh = ctx->tileheight;
 
 		anim->animation.sprite = &anim->sprite;
 		anim->animation.delay = delay;
+
+		mlk_sprite_init(&anim->sprite);
 	}
 
 	/*
@@ -240,7 +244,11 @@
 	if ((err = mlk_image_open(&ctx->tf->image, mlk_util_pathf("%s/%s", ctx->basedir, p + 1))) < 0)
 		return err;
 
-	mlk_sprite_init(&ctx->tf->sprite, &ctx->tf->image, ctx->tilewidth, ctx->tileheight);
+	ctx->tf->sprite.texture = &ctx->tf->image;
+	ctx->tf->sprite.cellw = ctx->tilewidth;
+	ctx->tf->sprite.cellh = ctx->tileheight;
+	mlk_sprite_init(&ctx->tf->sprite);
+
 	ctx->tileset->sprite = &ctx->tf->sprite;
 
 	return 0;
--- a/tests/test-drawable.c	Sat Mar 04 11:02:48 2023 +0100
+++ b/tests/test-drawable.c	Sat Mar 04 11:23:10 2023 +0100
@@ -16,6 +16,8 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <mlk/core/util.h>
+
 #include <mlk/core/drawable-stack.h>
 #include <mlk/core/drawable.h>
 #include <mlk/core/err.h>
@@ -147,11 +149,14 @@
 static void
 test_stack_add(void)
 {
+	struct mlk_drawable dw = {0};
 	struct mlk_drawable *drawables[10];
-	struct mlk_drawable_stack st = {0};
-	struct mlk_drawable dw = {0};
+	struct mlk_drawable_stack st = {
+		.objects = drawables,
+		.objectsz = MLK_UTIL_SIZE(drawables)
+	};
 
-	mlk_drawable_stack_init(&st, drawables, 10);
+	mlk_drawable_stack_init(&st);
 
 	DT_EQ_INT(mlk_drawable_stack_add(&st, &dw), 0);
 
@@ -180,9 +185,12 @@
 	};
 
 	struct mlk_drawable *drawables[10];
-	struct mlk_drawable_stack st = {0};
+	struct mlk_drawable_stack st = {
+		.objects = drawables,
+		.objectsz = MLK_UTIL_SIZE(drawables)
+	};
 
-	mlk_drawable_stack_init(&st, drawables, 10);
+	mlk_drawable_stack_init(&st);
 	mlk_drawable_stack_add(&st, &table[0].dw);
 	mlk_drawable_stack_add(&st, &table[1].dw);
 	mlk_drawable_stack_add(&st, &table[2].dw);
@@ -271,9 +279,12 @@
 	};
 
 	struct mlk_drawable *drawables[10];
-	struct mlk_drawable_stack st = {0};
+	struct mlk_drawable_stack st = {
+		.objects = drawables,
+		.objectsz = MLK_UTIL_SIZE(drawables)
+	};
 
-	mlk_drawable_stack_init(&st, drawables, 10);
+	mlk_drawable_stack_init(&st);
 	mlk_drawable_stack_add(&st, &table[0].dw);
 	mlk_drawable_stack_add(&st, &table[1].dw);
 	mlk_drawable_stack_add(&st, &table[2].dw);
@@ -328,9 +339,12 @@
 	};
 
 	struct mlk_drawable *drawables[10];
-	struct mlk_drawable_stack st = {0};
+	struct mlk_drawable_stack st = {
+		.objects = drawables,
+		.objectsz = MLK_UTIL_SIZE(drawables)
+	};
 
-	mlk_drawable_stack_init(&st, drawables, 10);
+	mlk_drawable_stack_init(&st);
 	mlk_drawable_stack_add(&st, &table[0].dw);
 	mlk_drawable_stack_add(&st, &table[1].dw);
 	mlk_drawable_stack_finish(&st);