summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanny Holman <dholman@gymli.org>2025-03-20 14:15:38 -0500
committerDanny Holman <dholman@gymli.org>2025-03-20 14:18:49 -0500
commitadaecb65eaa1abf437a24c93a08b2b7c2266c5dc (patch)
tree8b70c3f85648a12c6e6b7308489033653235aa64
parent7281fe8fdbb64a83bc0f569ef01db33a1f26ac98 (diff)
core: refactor core API to be in proper style
Bring the rest of the core API to be in line with the project coding style. This commit also makes the coding style change formal by updating the coding style document in the documentation. Signed-off-by: Danny Holman <dholman@gymli.org>
-rw-r--r--core/abort.c22
-rw-r--r--core/callbacks.c21
-rw-r--r--core/mod.c27
-rw-r--r--core/thread.c31
-rw-r--r--doc/coding-style.rst19
-rw-r--r--include/rune/core/mod.h4
-rw-r--r--include/rune/core/thread.h8
-rw-r--r--include/rune/util/list.h18
8 files changed, 115 insertions, 35 deletions
diff --git a/core/abort.c b/core/abort.c
index cd66ab1..5ee42f6 100644
--- a/core/abort.c
+++ b/core/abort.c
@@ -1,4 +1,26 @@
+/*
+ * Rune Game Engine
+ * Copyright 2024 Danny Holman <dholman@gymli.org>
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ */
+
#include <rune/core/abort.h>
+#include <rune/core/alloc.h>
#include <rune/core/init.h>
#include <rune/core/logging.h>
#include <rune/util/exits.h>
diff --git a/core/callbacks.c b/core/callbacks.c
index 03b7c46..ea7f50e 100644
--- a/core/callbacks.c
+++ b/core/callbacks.c
@@ -1,3 +1,24 @@
+/*
+ * Rune Game Engine
+ * Copyright 2024 Danny Holman <dholman@gymli.org>
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ */
+
#include <rune/core/callbacks.h>
#include <rune/core/logging.h>
diff --git a/core/mod.c b/core/mod.c
index 4a4eac8..3d9e710 100644
--- a/core/mod.c
+++ b/core/mod.c
@@ -1,3 +1,24 @@
+/*
+ * Rune Game Engine
+ * Copyright 2024 Danny Holman <dholman@gymli.org>
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ */
+
#include <rune/core/mod.h>
#include <rune/core/logging.h>
#include <rune/core/alloc.h>
@@ -6,7 +27,7 @@
#include <dirent.h>
#include <dlfcn.h>
-struct list_head *mods = NULL;
+list_head_t *mods = NULL;
void _load_mod(const char *filename) {
char mod_path[4096];
@@ -31,7 +52,7 @@ void rune_load_mods(void) {
}
void rune_init_mods(void) {
- struct list_head *temp = mods;
+ list_head_t *temp = mods;
struct mod *mod;
while (temp != NULL) {
mod = (struct mod*)((void*)temp - offsetof(struct mod, list));
@@ -44,7 +65,7 @@ void rune_close_mods(void) {
if (mods == NULL)
return;
- struct list_head *temp = mods;
+ list_head_t *temp = mods;
struct mod *mod;
while (temp != NULL) {
mod = (struct mod*)((void*)temp - offsetof(struct mod, list));
diff --git a/core/thread.c b/core/thread.c
index 45de463..3c5db8c 100644
--- a/core/thread.c
+++ b/core/thread.c
@@ -1,3 +1,24 @@
+/*
+ * Rune Game Engine
+ * Copyright 2024 Danny Holman <dholman@gymli.org>
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ * 3. This notice may not be removed or altered from any source distribution.
+ */
+
#include <rune/core/thread.h>
#include <rune/core/logging.h>
#include <rune/core/alloc.h>
@@ -5,8 +26,8 @@
#include <string.h>
#include <stdatomic.h>
-static struct list_head *threads = NULL;
-static struct list_head *mutexes = NULL;
+static list_head_t *threads = NULL;
+static list_head_t *mutexes = NULL;
static int next_tid = 0;
static int next_mid = 0;
@@ -20,7 +41,7 @@ static struct thread* _find_thread_by_handle(void *handle) {
if (threads == NULL)
return NULL;
- struct list_head *temp = threads;
+ list_head_t *temp = threads;
struct thread *ret;
while (temp != NULL) {
ret = (struct thread*)((void*)temp - offsetof(struct thread, list));
@@ -35,7 +56,7 @@ static struct thread* _find_thread_by_id(int ID) {
if (threads == NULL)
return NULL;
- struct list_head *temp = threads;
+ list_head_t *temp = threads;
struct thread *ret;
while (temp != NULL) {
ret = (struct thread*)((void*)temp - offsetof(struct thread, list));
@@ -50,7 +71,7 @@ static struct mutex* _find_mutex_by_id(int ID) {
if (mutexes == NULL)
return NULL;
- struct list_head *temp = mutexes;
+ list_head_t *temp = mutexes;
struct mutex *ret;
while (temp != NULL) {
ret = (struct mutex*)((void*)temp - offsetof(struct mutex, list));
diff --git a/doc/coding-style.rst b/doc/coding-style.rst
index 68c2240..8908eef 100644
--- a/doc/coding-style.rst
+++ b/doc/coding-style.rst
@@ -135,20 +135,15 @@ should have a name that reflects their purpose and scope. For instance:
Typedefs
--------
-Typedefs should be avoided at the global or API level, as it can hide
-information about a struct or pointer. Things such as ``type_t`` are prone to
-create confusion and are heavily frowned upon. The rationale is that when a
-programmer sees ``type_t var``, they have no context as to how the type needs
-to be used. Instead, leave the backing type information in full view:
-
-.. code-block:: c
-
- struct type *var;
+Typedefs should generally be avoided at the local level, however, at the global
+or API level, a bare structure could become a bit too wordy for general use.
+Instead, use a typedef to avoid hitting the 80-column limit unless it is
+readable as it is.
The engine has already defined the standard C23 integer types for use in
-engine code, so there is no need to do any kind of typedef. Even if the backing
-type information is platform-dependant, a simple ``void*`` is almost always
-preferable to creating a new type wholesale.
+engine code, so there is no need to do any kind of typedef for standard types.
+Even if the backing type information is platform-dependant, a simple ``void*``
+is almost always preferable to creating a new type wholesale.
Functions
---------
diff --git a/include/rune/core/mod.h b/include/rune/core/mod.h
index f0940ce..32d1465 100644
--- a/include/rune/core/mod.h
+++ b/include/rune/core/mod.h
@@ -53,13 +53,13 @@ typedef void (*mod_func)(void);
/**
* Class-like definition for in-game mod
*/
-struct mod {
+typedef struct mod {
const char *name; ///< Name of the mod
mod_func init_func; ///< Mod initialization function, called by rune_init_mods
mod_func exit_func; ///< Mod exit function, called by rune_close_mods
mod_func update_func; ///< Mod update function, called at every frame
struct list_head list; ///< Linked list of all mod structs, used internally
-};
+} mod_t;
/**
* \brief Load all the mods from the mod folder, mods must be either DLLs on Windows,
diff --git a/include/rune/core/thread.h b/include/rune/core/thread.h
index 976b7d3..a397b0c 100644
--- a/include/rune/core/thread.h
+++ b/include/rune/core/thread.h
@@ -28,21 +28,21 @@
/**
* Platform-agnostic thread handle
*/
-struct thread {
+typedef struct thread {
int ID; ///< In-engine thread ID
int detached; ///< 1 if thread has been detached, 0 otherwise
void *thread_handle; ///< System-defined thread handle, usually a pthread_t
struct list_head list; ///< Linked list of all threads, used internally
-};
+} thread_t;
/**
* Platform-agnostic mutex handle
*/
-struct mutex {
+typedef struct mutex {
int ID; ///< In-engine mutex ID
void *mutex_handle; ///< System-defined mutex handle, usually a pthread_mutex_t
struct list_head list; ///< Linked list of all mutexes, used internally
-};
+} mutex_t;
/**
* \brief Initializes the engine's thread API, must be called before using any
diff --git a/include/rune/util/list.h b/include/rune/util/list.h
index 556e085..c011cb3 100644
--- a/include/rune/util/list.h
+++ b/include/rune/util/list.h
@@ -27,18 +27,18 @@
/**
* Linux-kernel combatible linked list implementation
*/
-struct list_head {
+typedef struct list_head {
struct list_head *next; ///< Next list element
struct list_head *prev; ///< Previous list element
-};
+} list_head_t;
/**
* \brief Add element to the end of a list
- * \param[in] new Pointer to struct list_head, part of another struct
+ * \param[in] new Pointer to list_head_t, part of another struct
* \param[in] head Start point of the list to be added to
*/
-static inline void list_add(struct list_head *new, struct list_head *head) {
- struct list_head *temp = head;
+static inline void list_add(list_head_t *new, list_head_t *head) {
+ list_head_t *temp = head;
while (temp->next != NULL)
temp = temp->next;
@@ -49,11 +49,11 @@ static inline void list_add(struct list_head *new, struct list_head *head) {
/**
* \brief Remove element from a list
- * \param[in] item Pointer to struct list_head, part of another struct
+ * \param[in] item Pointer to list_head_t, part of another struct
*/
-static inline void list_del(struct list_head *item) {
- struct list_head *next = item->next;
- struct list_head *prev = item->prev;
+static inline void list_del(list_head_t *item) {
+ list_head_t *next = item->next;
+ list_head_t *prev = item->prev;
if (next != NULL)
next->prev = prev;
if (prev != NULL)