changeset 599:cb8ca73f1aa4

rpg: walksprite -> mlk_walksprite
author David Demelier <markand@malikania.fr>
date Fri, 31 Mar 2023 20:01:00 +0200
parents 1742b5eaf0d4
children 23bbe3f71dbd
files cmake/MlkOptions.cmake cmake/MlkOptions.install.cmake libmlk-rpg/mlk/rpg/map.c libmlk-rpg/mlk/rpg/map.h libmlk-rpg/mlk/rpg/walksprite.c libmlk-rpg/mlk/rpg/walksprite.h
diffstat 6 files changed, 109 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/cmake/MlkOptions.cmake	Fri Mar 31 10:42:14 2023 +0200
+++ b/cmake/MlkOptions.cmake	Fri Mar 31 20:01:00 2023 +0200
@@ -16,15 +16,9 @@
 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 #
 
-mlk_option(MLK_WITH_DOXYGEN On
-    BOOL "Enable doxygen documentation")
-mlk_option(MLK_WITH_EXAMPLES On
-    BOOL "Enable examples")
-mlk_option(MLK_WITH_NLS On
-    BOOL "Enable NLS support")
-mlk_option(MLK_WITH_TESTS On
-    BOOL "Enable unit tests")
-mlk_option(MLK_WITH_TESTS_GRAPHICAL On
-    BOOL "Enable unit tests that requires graphical context")
-mlk_option(MLK_WITH_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake"
-    STRING "Destination for CMake files")
+mlk_option(DOXYGEN On BOOL "Enable doxygen documentation")
+mlk_option(EXAMPLES On BOOL "Enable examples")
+mlk_option(NLS On BOOL "Enable NLS support")
+mlk_option(TESTS On BOOL "Enable unit tests")
+mlk_option(TESTS_GRAPHICAL On BOOL "Enable unit tests that requires graphical context")
+mlk_option(CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake" STRING "Destination for CMake files")
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmake/MlkOptions.install.cmake	Fri Mar 31 20:01:00 2023 +0200
@@ -0,0 +1,20 @@
+#
+# CMakeLists.txt -- CMake build system for Molko's Engine
+#
+# Copyright (c) 2020-2023 David Demelier <markand@malikania.fr>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+set(MLK_WITH_NLS @MLK_WITH_NLS@)
+set(MLK_WITH_CMAKEDIR @MLK_WITH_CMAKEDIR@)
--- a/libmlk-rpg/mlk/rpg/map.c	Fri Mar 31 10:42:14 2023 +0200
+++ b/libmlk-rpg/mlk/rpg/map.c	Fri Mar 31 20:01:00 2023 +0200
@@ -174,7 +174,9 @@
 	center(map);
 
 	/* Final bits. */
-	walksprite_init(&map->player_ws, map->player_sprite, 150);
+	map->player_ws.sprite = map->player_sprite;
+	map->player_ws.delay = 150;
+	mlk_walksprite_init(&map->player_ws);
 }
 
 static void
@@ -467,7 +469,7 @@
 	if (dy)
 		move_y(map, dy * delta);
 
-	walksprite_update(&map->player_ws, ticks);
+	mlk_walksprite_update(&map->player_ws, ticks);
 }
 
 static inline void
@@ -641,7 +643,7 @@
 	draw_layer(map, &map->layers[MLK_MAP_LAYER_TYPE_BG]);
 	draw_layer(map, &map->layers[MLK_MAP_LAYER_TYPE_FG]);
 
-	walksprite_draw(
+	mlk_walksprite_draw(
 		&map->player_ws,
 		map->player_a,
 		map->player_x - map->view_x,
--- a/libmlk-rpg/mlk/rpg/map.h	Fri Mar 31 10:42:14 2023 +0200
+++ b/libmlk-rpg/mlk/rpg/map.h	Fri Mar 31 20:01:00 2023 +0200
@@ -79,7 +79,7 @@
 	int player_y;
 	int player_a;
 	unsigned int player_movement;
-	struct walksprite player_ws;
+	struct mlk_walksprite player_ws;
 
 	int view_x;
 	int view_y;
--- a/libmlk-rpg/mlk/rpg/walksprite.c	Fri Mar 31 10:42:14 2023 +0200
+++ b/libmlk-rpg/mlk/rpg/walksprite.c	Fri Mar 31 20:01:00 2023 +0200
@@ -17,33 +17,30 @@
  */
 
 #include <assert.h>
-#include <string.h>
 
 #include <mlk/core/sprite.h>
 
 #include "walksprite.h"
 
 void
-walksprite_init(struct walksprite *ws, struct mlk_sprite *sprite, unsigned int delay)
+mlk_walksprite_init(struct mlk_walksprite *ws)
 {
 	assert(ws);
-	assert(sprite);
 
-	memset(ws, 0, sizeof (*ws));
-	ws->sprite = sprite;
-	ws->delay = delay;
+	mlk_walksprite_reset(ws);
 }
 
 void
-walksprite_reset(struct walksprite *ws)
+mlk_walksprite_reset(struct mlk_walksprite *ws)
 {
 	assert(ws);
 
 	ws->index = 0;
+	ws->elapsed = 0;
 }
 
 void
-walksprite_update(struct walksprite *ws, unsigned int ticks)
+mlk_walksprite_update(struct mlk_walksprite *ws, unsigned int ticks)
 {
 	assert(ws);
 
@@ -60,7 +57,10 @@
 }
 
 void
-walksprite_draw(const struct walksprite *ws, unsigned int orientation, int x, int y)
+mlk_walksprite_draw(const struct mlk_walksprite *ws,
+                    unsigned int orientation,
+                    int x,
+                    int y)
 {
 	assert(ws);
 	assert(orientation < 8);
--- a/libmlk-rpg/mlk/rpg/walksprite.h	Fri Mar 31 10:42:14 2023 +0200
+++ b/libmlk-rpg/mlk/rpg/walksprite.h	Fri Mar 31 20:01:00 2023 +0200
@@ -19,13 +19,15 @@
 #ifndef MLK_RPG_WALKSPRITE_H
 #define MLK_RPG_WALKSPRITE_H
 
-struct mlk_sprite;
-
 /**
- * \brief Sprite designed for walking entities.
+ * \file mlk/rpg/walksprite.h
+ * \brief Sprite designed for walking entities
  *
- * This structure works with sprite images that are defined as using the
- * following conventions:
+ * This module provides a convenient animation for walking entities using a
+ * specific sprite texture.
+ *
+ * The entity can have up to 8 orientation including diagonals which are defined
+ * as following:
  *
  * ```
  * 7   0   1
@@ -50,29 +52,85 @@
  *  6 | ←←←←←←←←←←←←←←←←←←←←
  *  7 | ↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖
  * ```
+ *
+ * The first column in each row should be static position which means that the
+ * entity is not moving. Using ::mlk_walksprite_reset resets the column index to
+ * this position so that you can use this module even on non-moving entities.
  */
-struct walksprite {
+
+struct mlk_sprite;
+
+/**
+ * \struct mlk_walksprite
+ * \brief Walking sprite structure
+ */
+struct mlk_walksprite {
+	/**
+	 * (read-write, borrowed)
+	 *
+	 * Sprite file to use for animation.
+	 */
 	struct mlk_sprite *sprite;
+
+	/**
+	 * (read-write)
+	 *
+	 * Delay between each frame.
+	 */
 	unsigned int delay;
+
+	/** \cond MLK_PRIVATE_DECLS */
 	unsigned int index;
 	unsigned int elapsed;
+	/** \endcond MLK_PRIVATE_DECLS */
 };
 
 #if defined(__cplusplus)
 extern "C" {
 #endif
 
+/**
+ * Initialize the walking sprite.
+ *
+ * \pre ws != NULL
+ * \param ws walking sprite to initialize
+ */
 void
-walksprite_init(struct walksprite *, struct mlk_sprite *, unsigned int);
+mlk_walksprite_init(struct mlk_walksprite *ws);
 
+/**
+ * Reset the walking sprite to first column.
+ *
+ * \pre ws != NULL
+ * \param ws walking sprite to reset
+ */
 void
-walksprite_reset(struct walksprite *);
+mlk_walksprite_reset(struct mlk_walksprite *ws);
 
+/**
+ * Update the walking sprite, moving to the next animation if needed.
+ *
+ * \pre ws != NULL
+ * \param ws walking sprite to update
+ * \param ticks frame ticks
+ */
 void
-walksprite_update(struct walksprite *, unsigned int);
+mlk_walksprite_update(struct mlk_walksprite *ws, unsigned int ticks);
 
+/**
+ * Draw the current image frame at the given position.
+ *
+ * \pre ws != NULL
+ * \param ws walking sprite to draw
+ * \param orientation the orientation [0..7]
+ * \param x the x coordinate
+ * \param y the y coordinate
+ */
 void
-walksprite_draw(const struct walksprite *, unsigned int, int, int);
+mlk_walksprite_draw(const struct mlk_walksprite *ws,
+                    unsigned int orientation,
+                    int x,
+                    int y);
 
 #if defined(__cplusplus)
 }