summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-03-17 10:51:17 +0100
committerkdx <kikoodx@paranoici.org>2023-03-17 10:51:17 +0100
commitba8cb4fcea09190bbe6e80c45ef59314feea8600 (patch)
tree991862671f887760883507159be6539a7915bbae
parent7ef2f09cfdd85b1427e89a50a173fc2b67221b88 (diff)
downloadhyperultra-ba8cb4fcea09190bbe6e80c45ef59314feea8600.tar.gz
simple entity system
-rw-r--r--CMakeLists.txt4
-rw-r--r--src/entity.h28
-rw-r--r--src/game.c49
-rw-r--r--src/game.h15
-rw-r--r--src/main.c15
-rw-r--r--src/player.c25
-rw-r--r--src/player.h10
7 files changed, 145 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 74aa891..28424ad 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -10,8 +10,10 @@ include(Fxconv)
find_package(Gint 2.9 REQUIRED)
set(SOURCES
- src/main.c
+ src/game.c
src/lzy.c
+ src/main.c
+ src/player.c
)
set(ASSETS
diff --git a/src/entity.h b/src/entity.h
new file mode 100644
index 0000000..9f5861c
--- /dev/null
+++ b/src/entity.h
@@ -0,0 +1,28 @@
+#pragma once
+#include "player.h"
+#include <stdbool.h>
+
+struct Game;
+
+typedef enum {
+ ET_NONE,
+ ET_PLAYER
+} EntityType;
+
+typedef struct Entity Entity;
+struct Entity {
+ unsigned long uuid;
+ void (*update)(Entity *this, struct Game *g);
+ void (*draw)(Entity *this, struct Game *g);
+ unsigned int type;
+ int id;
+ int pos[2];
+ double vel[2];
+ double rem[2];
+ int width;
+ int height;
+ bool solid;
+ union {
+ Player player;
+ };
+};
diff --git a/src/game.c b/src/game.c
new file mode 100644
index 0000000..2a3ceeb
--- /dev/null
+++ b/src/game.c
@@ -0,0 +1,49 @@
+#include "game.h"
+#include <string.h>
+
+void
+game_init(Game *this)
+{
+ memset(this, 0, sizeof(*this));
+}
+
+void
+game_deinit(Game *this)
+{
+ (void)this;
+}
+
+void
+game_update(Game *this)
+{
+ for (int i = 0; i < MAX_ENTITIES; i++) {
+ Entity *const e = &this->entities[i];
+ if (e->type != ET_NONE && e->update != NULL)
+ e->update(e, this);
+ }
+}
+
+void
+game_draw(Game *this)
+{
+ for (int i = 0; i < MAX_ENTITIES; i++) {
+ Entity *const e = &this->entities[i];
+ if (e->type != ET_NONE && e->draw != NULL)
+ e->draw(e, this);
+ }
+}
+
+Entity *
+game_create_entity(Game *this)
+{
+ Entity *e = &this->entities[MAX_ENTITIES - 1];
+ for (int i = 0; i < MAX_ENTITIES; i++)
+ if (this->entities[i].type == ET_NONE) {
+ e = &this->entities[i];
+ break;
+ }
+ e->type = ET_NONE;
+ e->uuid = this->uuid;
+ this->uuid += 1;
+ return e;
+}
diff --git a/src/game.h b/src/game.h
new file mode 100644
index 0000000..185c89b
--- /dev/null
+++ b/src/game.h
@@ -0,0 +1,15 @@
+#pragma once
+#include "entity.h"
+
+enum { MAX_ENTITIES = 64 };
+
+typedef struct Game {
+ unsigned int uuid;
+ Entity entities[MAX_ENTITIES];
+} Game;
+
+void game_init(Game *this);
+void game_deinit(Game *this);
+void game_update(Game *this);
+void game_draw(Game *this);
+Entity *game_create_entity(Game *this);
diff --git a/src/main.c b/src/main.c
index 03b612f..744c96b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,4 +1,8 @@
#include "lzy.h"
+#include "game.h"
+#include "player.h"
+#include <stdio.h>
+#include <stdlib.h>
int main(void)
{
@@ -8,6 +12,15 @@ int main(void)
return 1;
}
+ Game *const game = malloc(sizeof(Game));
+ if (game == NULL) {
+ LZY_Log("malloc failed");
+ LZY_Quit();
+ return 1;
+ }
+ game_init(game);
+ player_init(game_create_entity(game));
+
while (!LZY_ShouldQuit()) {
LZY_CycleEvents();
@@ -17,6 +30,8 @@ int main(void)
LZY_DrawEnd();
}
+ game_deinit(game);
+ free(game);
LZY_Quit();
return 0;
}
diff --git a/src/player.c b/src/player.c
new file mode 100644
index 0000000..d751857
--- /dev/null
+++ b/src/player.c
@@ -0,0 +1,25 @@
+#include "player.h"
+#include "entity.h"
+#include "game.h"
+#include <string.h>
+
+static void
+player_update(Entity *this, Game *g)
+{
+ (void)this, (void)g;
+}
+
+static void
+player_draw(Entity *this, Game *g)
+{
+ (void)this, (void)g;
+}
+
+void
+player_init(Entity *this)
+{
+ memset(this, 0, sizeof(*this));
+ this->type = ET_PLAYER;
+ this->update = player_update;
+ this->draw = player_draw;
+}
diff --git a/src/player.h b/src/player.h
new file mode 100644
index 0000000..a6fe6c9
--- /dev/null
+++ b/src/player.h
@@ -0,0 +1,10 @@
+#pragma once
+
+typedef struct {
+ int _;
+} Player;
+
+struct Entity;
+struct Game;
+
+void player_init(struct Entity *this);