changeset 522:f45a023f6690

core: doxygenize game
author David Demelier <markand@malikania.fr>
date Sat, 04 Mar 2023 16:47:54 +0100
parents 338a4436e255
children 81f7a432082f
files examples/example-action/example-action.c examples/example-animation/example-animation.c examples/example-audio/example-audio.c examples/example-cursor/example-cursor.c examples/example-debug/example-debug.c examples/example-drawable/example-drawable.c examples/example-font/example-font.c examples/example-gridmenu/example-gridmenu.c examples/example-label/example-label.c examples/example-message/example-message.c examples/example-notify/example-notify.c examples/example-sprite/example-sprite.c examples/example-trace/example-trace.c examples/example-ui/example-ui.c libmlk-core/CMakeLists.txt libmlk-core/mlk/core/game.c libmlk-core/mlk/core/game.h libmlk-core/mlk/core/inhibit.h tests/test-state.c
diffstat 19 files changed, 176 insertions(+), 118 deletions(-) [+]
line wrap: on
line diff
--- a/examples/example-action/example-action.c	Sat Mar 04 15:39:21 2023 +0100
+++ b/examples/example-action/example-action.c	Sat Mar 04 16:47:54 2023 +0100
@@ -43,8 +43,6 @@
 #include "chest.h"
 #include "dialog.h"
 
-static struct mlk_state *states[8];
-
 /*
  * Those are two chests shown on the script that the user can click. They will
  * fill up the script scene.
@@ -346,7 +344,7 @@
 		.draw = draw
 	};
 
-	mlk_game_init(states, MLK_UTIL_SIZE(states));
+	mlk_game_init();
 	mlk_game_push(&state);
 	mlk_game_loop();
 }
--- a/examples/example-animation/example-animation.c	Sat Mar 04 15:39:21 2023 +0100
+++ b/examples/example-animation/example-animation.c	Sat Mar 04 16:47:54 2023 +0100
@@ -41,7 +41,6 @@
 	.y = 10,
 };
 
-static struct mlk_state *states[8];
 static struct mlk_animation animation = {
 	.sprite = &mlk_registry_sprites[MLK_REGISTRY_TEXTURE_EXPLOSION],
 	.delay = 25
@@ -119,7 +118,7 @@
 		.draw = draw
 	};
 
-	mlk_game_init(states, MLK_UTIL_SIZE(states));
+	mlk_game_init();
 	mlk_game_push(&state);
 	mlk_game_loop();
 }
--- a/examples/example-audio/example-audio.c	Sat Mar 04 15:39:21 2023 +0100
+++ b/examples/example-audio/example-audio.c	Sat Mar 04 16:47:54 2023 +0100
@@ -34,7 +34,6 @@
 #include <mlk/example/example.h>
 #include <mlk/example/registry.h>
 
-static struct mlk_state *states[1];
 static struct mlk_music *music;
 static struct mlk_sound *sound;
 
@@ -129,7 +128,7 @@
 		.draw = draw
 	};
 
-	mlk_game_init(states, MLK_UTIL_SIZE(states));
+	mlk_game_init();
 	mlk_game_push(&state);
 	mlk_game_loop();
 
--- a/examples/example-cursor/example-cursor.c	Sat Mar 04 15:39:21 2023 +0100
+++ b/examples/example-cursor/example-cursor.c	Sat Mar 04 16:47:54 2023 +0100
@@ -35,7 +35,6 @@
 
 #include <mlk/example/example.h>
 
-static struct mlk_state *states[1];
 static char help_text[128];
 static enum mlk_window_cursor cursor = MLK_WINDOW_CURSOR_ARROW;
 
@@ -121,7 +120,7 @@
 
 	change(cursor);
 
-	mlk_game_init(states, MLK_UTIL_SIZE(states));
+	mlk_game_init();
 	mlk_game_push(&state);
 	mlk_game_loop();
 }
--- a/examples/example-debug/example-debug.c	Sat Mar 04 15:39:21 2023 +0100
+++ b/examples/example-debug/example-debug.c	Sat Mar 04 16:47:54 2023 +0100
@@ -31,7 +31,6 @@
 
 #include <mlk/example/example.h>
 
-static struct mlk_state *states[1];
 static int mouse_x;
 static int mouse_y;
 
@@ -86,7 +85,7 @@
 		.draw = draw
 	};
 
-	mlk_game_init(states, MLK_UTIL_SIZE(states));
+	mlk_game_init();
 	mlk_game_push(&state);
 	mlk_game_loop();
 }
@@ -107,4 +106,3 @@
 	run();
 	quit();
 }
-
--- a/examples/example-drawable/example-drawable.c	Sat Mar 04 15:39:21 2023 +0100
+++ b/examples/example-drawable/example-drawable.c	Sat Mar 04 16:47:54 2023 +0100
@@ -49,7 +49,6 @@
 	.text = "Keys: <Esc> to reset. Click anywhere to spawn a drawable."
 };
 
-static struct mlk_state *states[1];
 static struct mlk_drawable *drawables[64];
 static struct mlk_drawable_stack stack = {
 	.objects = drawables,
@@ -179,7 +178,7 @@
 
 	mlk_drawable_stack_init(&stack);
 
-	mlk_game_init(states, MLK_UTIL_SIZE(states));
+	mlk_game_init();
 	mlk_game_push(&state);
 	mlk_game_loop();
 }
--- a/examples/example-font/example-font.c	Sat Mar 04 15:39:21 2023 +0100
+++ b/examples/example-font/example-font.c	Sat Mar 04 16:47:54 2023 +0100
@@ -45,7 +45,6 @@
 	0xc7cfccff,     /* Christian Grey. */
 };
 
-static struct mlk_state *states[1];
 static int ci = 0;
 static enum mlk_font_style style = MLK_FONT_STYLE_ANTIALIASED;
 
@@ -123,7 +122,7 @@
 		.draw = draw
 	};
 
-	mlk_game_init(states, MLK_UTIL_SIZE(states));
+	mlk_game_init();
 	mlk_game_push(&state);
 	mlk_game_loop();
 }
--- a/examples/example-gridmenu/example-gridmenu.c	Sat Mar 04 15:39:21 2023 +0100
+++ b/examples/example-gridmenu/example-gridmenu.c	Sat Mar 04 16:47:54 2023 +0100
@@ -40,8 +40,6 @@
 
 static void menu_update(struct mlk_gridmenu_delegate *, struct mlk_gridmenu *, unsigned int);
 
-static struct mlk_state *states[8];
-
 static const char * const items[] = {
 	"Feu mineur",
 	"Feu majeur",
@@ -158,7 +156,7 @@
 	frame.x = menu.x;
 	frame.y = menu.y;
 
-	mlk_game_init(states, MLK_UTIL_SIZE(states));
+	mlk_game_init();
 	mlk_game_push(&state);
 	mlk_game_loop();
 }
--- a/examples/example-label/example-label.c	Sat Mar 04 15:39:21 2023 +0100
+++ b/examples/example-label/example-label.c	Sat Mar 04 16:47:54 2023 +0100
@@ -36,8 +36,6 @@
 #include <mlk/example/example.h>
 #include <mlk/example/glower.h>
 
-static struct mlk_state *states[8];
-
 static struct mlk_label_style style = {
 	.color = 0x005162ff
 };
@@ -213,7 +211,7 @@
 		.draw = draw
 	};
 
-	mlk_game_init(states, MLK_UTIL_SIZE(states));
+	mlk_game_init();
 	mlk_game_push(&state);
 	mlk_game_loop();
 }
--- a/examples/example-message/example-message.c	Sat Mar 04 15:39:21 2023 +0100
+++ b/examples/example-message/example-message.c	Sat Mar 04 16:47:54 2023 +0100
@@ -41,8 +41,6 @@
 #define MX      ((MLK_EXAMPLE_W / 2) - (MW / 2))
 #define MY      (100)
 
-static struct mlk_state *states[1];
-
 static void
 init(void)
 {
@@ -93,7 +91,7 @@
 
 	message_start(msg);
 
-	mlk_game_init(states, MLK_UTIL_SIZE(states));
+	mlk_game_init();
 	mlk_game_push(&state);
 	mlk_game_loop();
 }
--- a/examples/example-notify/example-notify.c	Sat Mar 04 15:39:21 2023 +0100
+++ b/examples/example-notify/example-notify.c	Sat Mar 04 16:47:54 2023 +0100
@@ -42,7 +42,6 @@
 	.y = 10
 };
 static struct mlk_texture *icon;
-static struct mlk_state *states[1];
 
 static void
 init(void)
@@ -102,7 +101,7 @@
 		.draw = draw
 	};
 
-	mlk_game_init(states, MLK_UTIL_SIZE(states));
+	mlk_game_init();
 	mlk_game_push(&state);
 	mlk_game_loop();
 }
--- a/examples/example-sprite/example-sprite.c	Sat Mar 04 15:39:21 2023 +0100
+++ b/examples/example-sprite/example-sprite.c	Sat Mar 04 16:47:54 2023 +0100
@@ -46,7 +46,6 @@
 static char msg[512];
 static struct mlk_sprite *sprite;
 static unsigned int row, column;
-static struct mlk_state *states[1];
 
 static struct mlk_label help = {
 	.x = 10,
@@ -134,7 +133,7 @@
 
 	changed();
 
-	mlk_game_init(states, MLK_UTIL_SIZE(states));
+	mlk_game_init();
 	mlk_game_push(&state);
 	mlk_game_loop();
 }
--- a/examples/example-trace/example-trace.c	Sat Mar 04 15:39:21 2023 +0100
+++ b/examples/example-trace/example-trace.c	Sat Mar 04 16:47:54 2023 +0100
@@ -33,8 +33,6 @@
 #include <mlk/example/example.h>
 #include <mlk/example/trace-hud.h>
 
-static struct mlk_state *states[1];
-
 static void
 init(void)
 {
@@ -101,7 +99,7 @@
 		.draw = draw
 	};
 
-	mlk_game_init(states, MLK_UTIL_SIZE(states));
+	mlk_game_init();
 	mlk_game_push(&state);
 	mlk_game_loop();
 }
--- a/examples/example-ui/example-ui.c	Sat Mar 04 15:39:21 2023 +0100
+++ b/examples/example-ui/example-ui.c	Sat Mar 04 16:47:54 2023 +0100
@@ -54,8 +54,6 @@
 
 #define PADDING         (10)
 
-static struct mlk_state *states[8];
-
 /*
  * We design a basic UI like this.
  *
@@ -344,7 +342,7 @@
 
 	resize();
 
-	mlk_game_init(states, MLK_UTIL_SIZE(states));
+	mlk_game_init();
 	mlk_game_push(&state);
 	mlk_game_loop();
 }
--- a/libmlk-core/CMakeLists.txt	Sat Mar 04 15:39:21 2023 +0100
+++ b/libmlk-core/CMakeLists.txt	Sat Mar 04 16:47:54 2023 +0100
@@ -49,7 +49,6 @@
 	${libmlk-core_SOURCE_DIR}/mlk/core/game.h
 	${libmlk-core_SOURCE_DIR}/mlk/core/image.c
 	${libmlk-core_SOURCE_DIR}/mlk/core/image.h
-	${libmlk-core_SOURCE_DIR}/mlk/core/inhibit.h
 	${libmlk-core_SOURCE_DIR}/mlk/core/key.h
 	${libmlk-core_SOURCE_DIR}/mlk/core/maths.c
 	${libmlk-core_SOURCE_DIR}/mlk/core/maths.h
--- a/libmlk-core/mlk/core/game.c	Sat Mar 04 15:39:21 2023 +0100
+++ b/libmlk-core/mlk/core/game.c	Sat Mar 04 16:47:54 2023 +0100
@@ -20,56 +20,60 @@
 #include <string.h>
 
 #include "clock.h"
+#include "err.h"
 #include "event.h"
 #include "game.h"
 #include "state.h"
 #include "util.h"
 #include "window.h"
 
-struct mlk_game game = {0};
+static struct mlk_state *states[8];
+
+struct mlk_game mlk_game = {
+	.states = states,
+	.statesz = MLK_UTIL_SIZE(states)
+};
 
 void
-mlk_game_init(struct mlk_state **states, size_t statesz)
+mlk_game_init(void)
 {
-	assert(states);
-	assert(statesz);
-
-	memset(&game, 0, sizeof (game));
-
-	game.states = states;
-	game.statesz = statesz;
-
-	for (size_t i = 0; i < game.statesz; ++i)
-		game.states[i] = NULL;
+	for (size_t i = 0; i < mlk_game.statesz; ++i)
+		mlk_game.states[i] = NULL;
 }
 
-void
+int
 mlk_game_push(struct mlk_state *state)
 {
 	assert(state);
-	assert(!game.state || game.state != &game.states[game.statesz - 1]);
+
+	if (!mlk_game.state) {
+		mlk_game.state = &mlk_game.states[0];
+		mlk_state_start(*mlk_game.state = state);
+		return 0;
+	}
 
-	if (!game.state) {
-		game.state = &game.states[0];
-		mlk_state_start(*game.state = state);
-	} else {
-		mlk_state_suspend(*game.state);
-		mlk_state_start(*(++game.state) = state);
-	}
+	if (mlk_game.state == &mlk_game.states[mlk_game.statesz - 1])
+		return MLK_ERR_NO_MEM;
+
+	mlk_state_suspend(*mlk_game.state);
+	mlk_state_start(*(++mlk_game.state) = state);
+
+	return 0;
 }
 
 void
 mlk_game_pop(void)
 {
-	assert(game.state);
-
-	mlk_state_end(*game.state);
-	mlk_state_finish(*game.state);
+	if (!mlk_game.state)
+		return;
 
-	if (game.state == game.states)
-		game.state = NULL;
+	mlk_state_end(*mlk_game.state);
+	mlk_state_finish(*mlk_game.state);
+
+	if (mlk_game.state == mlk_game.states)
+		mlk_game.state = NULL;
 	else
-		mlk_state_resume(*--game.state);
+		mlk_state_resume(*--mlk_game.state);
 }
 
 void
@@ -77,22 +81,22 @@
 {
 	assert(ev);
 
-	if (*game.state && !(game.inhibit & MLK_INHIBIT_STATE_INPUT))
-		mlk_state_handle(*game.state, ev);
+	if (mlk_game.state && !(mlk_game.inhibit & MLK_GAME_INHIBIT_INPUT))
+		mlk_state_handle(*mlk_game.state, ev);
 }
 
 void
 mlk_game_update(unsigned int ticks)
 {
-	if (*game.state && !(game.inhibit & MLK_INHIBIT_STATE_UPDATE))
-		mlk_state_update(*game.state, ticks);
+	if (mlk_game.state && !(mlk_game.inhibit & MLK_GAME_INHIBIT_UPDATE))
+		mlk_state_update(*mlk_game.state, ticks);
 }
 
 void
 mlk_game_draw(void)
 {
-	if (*game.state && !(game.inhibit & MLK_INHIBIT_STATE_DRAW))
-		mlk_state_draw(*game.state);
+	if (mlk_game.state && !(mlk_game.inhibit & MLK_GAME_INHIBIT_DRAW))
+		mlk_state_draw(*mlk_game.state);
 }
 
 void
@@ -108,7 +112,7 @@
 		/* Assuming 50.0 FPS. */
 		frametime = 1000.0 / 50.0;
 
-	while (*game.state) {
+	while (mlk_game.state) {
 		mlk_clock_start(&clock);
 
 		for (union mlk_event ev; mlk_event_poll(&ev); )
@@ -131,10 +135,10 @@
 void
 mlk_game_quit(void)
 {
-	for (size_t i = 0; i < game.statesz; ++i) {
-		if (game.states[i])
-			mlk_state_finish(game.states[i]);
+	for (size_t i = 0; i < mlk_game.statesz; ++i) {
+		if (mlk_game.states[i])
+			mlk_state_finish(mlk_game.states[i]);
 
-		game.states[i] = NULL;
+		mlk_game.states[i] = NULL;
 	}
 }
--- a/libmlk-core/mlk/core/game.h	Sat Mar 04 15:39:21 2023 +0100
+++ b/libmlk-core/mlk/core/game.h	Sat Mar 04 16:47:54 2023 +0100
@@ -19,48 +19,151 @@
 #ifndef MLK_CORE_GAME_H
 #define MLK_CORE_GAME_H
 
-#include <stddef.h>
+/**
+ * \file game.h
+ * \brief Main game object
+ *
+ * For convenience, the default game structure is already initialized with an
+ * array of 8 states usable.
+ */
 
-#include "inhibit.h"
+#include <stddef.h>
 
 struct mlk_state;
 
 union mlk_event;
 
-struct mlk_game {
-	enum mlk_inhibit inhibit;
-	struct mlk_state **states;
-	size_t statesz;
-	struct mlk_state **state;
+/**
+ * \enum mlk_game_inhibit
+ * \brief Inhibit game loop
+ */
+enum mlk_game_inhibit {
+	/**
+	 * Inhibit user input.
+	 */
+	MLK_GAME_INHIBIT_INPUT  = (1 << 0),
+
+	/**
+	 * Inhibit game update.
+	 */
+	MLK_GAME_INHIBIT_UPDATE = (1 << 1),
+
+	/**
+	 * Inhibit drawing.
+	 *
+	 * \note Turning off drawing can introduce weird results on some
+	 *       platforms.
+	 */
+	MLK_GAME_INHIBIT_DRAW   = (1 << 2)
 };
 
-extern struct mlk_game game;
+/**
+ * \struct mlk_game
+ * \brief Game structure
+ */
+struct mlk_game {
+	/**
+	 * (read-write)
+	 *
+	 * Inhibit a state function from the game loop.
+	 *
+	 * Enabling any flags on this field will skip according function from
+	 * the loop.
+	 */
+	enum mlk_game_inhibit inhibit;
+
+	/**
+	 * (read-write, borrowed)
+	 *
+	 * Array of non-owning states.
+	 */
+	struct mlk_state **states;
+
+	/**
+	 * Number of states in array ::mlk_game::states.
+	 *
+	 * \warning Changing this value must be kept in sync with the array
+	 *          dimension.
+	 */
+	size_t statesz;
+
+	/** \cond MLK_PRIVATE_DECLS */
+	struct mlk_state **state;
+	/** \endcond MLK_PRIVATE_DECLS */
+};
+
+/**
+ * \brief Main game loop structure.
+ */
+extern struct mlk_game mlk_game;
 
 #if defined(__cplusplus)
 extern "C" {
 #endif
 
+/**
+ * Initialize the main game loop.
+ */
 void
-mlk_game_init(struct mlk_state **, size_t);
+mlk_game_init(void);
 
-void
-mlk_game_push(struct mlk_state *);
+/**
+ * Try to append a new state into the game loop at the end unless the array is
+ * full.
+ *
+ * The state is inserted as-is and ownership is left to the caller.
+ *
+ * \pre state != NULL
+ * \param state
+ * \return 0 on success or ::MLK_ERR_NO_MEM if full.
+ */
+int
+mlk_game_push(struct mlk_state *state);
 
+/**
+ * Pop the current state if any and resume the previous one.
+ */
 void
 mlk_game_pop(void);
 
+/**
+ * Call the current state's mlk_state::handle function unless it is inhibited
+ * or NULL.
+ *
+ * \pre event != NULL
+ * \param event the event
+ */
 void
-mlk_game_handle(const union mlk_event *);
+mlk_game_handle(const union mlk_event *event);
 
+/**
+ * Call the current state's mlk_state::update function unless it is inhibited
+ * or NULL.
+ *
+ * \param ticks frame ticks
+ */
 void
-mlk_game_update(unsigned int);
+mlk_game_update(unsigned int ticks);
 
+/**
+ * Call the current state's mlk_state::draw function unless it is inhibited
+ * or NULL.
+ */
 void
 mlk_game_draw(void);
 
+/**
+ * Enter a game loop until there is no more states.
+ *
+ * The current implementation will perform a loop capped to a 60 FPS rate and
+ * update the states with the appropriate number of ticks.
+ */
 void
 mlk_game_loop(void);
 
+/**
+ * Request the game loop to stop by removing all states.
+ */
 void
 mlk_game_quit(void);
 
--- a/libmlk-core/mlk/core/inhibit.h	Sat Mar 04 15:39:21 2023 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,29 +0,0 @@
-/*
- * inhibit.h -- disable specific game behavior
- *
- * 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.
- */
-
-#ifndef MLK_CORE_INHIBIT_H
-#define MLK_CORE_INHIBIT_H
-
-enum mlk_inhibit {
-	MLK_INHIBIT_NONE,
-	MLK_INHIBIT_STATE_INPUT         = (1 << 0),
-	MLK_INHIBIT_STATE_UPDATE        = (1 << 1),
-	MLK_INHIBIT_STATE_DRAW          = (1 << 2)
-};
-
-#endif /* !MLK_CORE_INHIBIT_H */
--- a/tests/test-state.c	Sat Mar 04 15:39:21 2023 +0100
+++ b/tests/test-state.c	Sat Mar 04 16:47:54 2023 +0100
@@ -204,7 +204,9 @@
 	};
 
 	/* 0 becomes active and should start. */
-	mlk_game_init(mainstates, MLK_UTIL_SIZE(mainstates));
+	mlk_game.states = mainstates;
+	mlk_game.statesz = MLK_UTIL_SIZE(mainstates);
+	mlk_game_init();
 	mlk_game_push(&states[0].state);
 
 	DT_EQ_UINT(states[0].inv.start, 1U);