+14
-10
include/keraforge/actor.h
+14
-10
include/keraforge/actor.h
···
20
struct kf_actorregistry
21
{
22
int count;
23
-
char *id[KF_MAX_ACTOR_TYPES];
24
void (*serialize[KF_MAX_ACTOR_TYPES])(struct kf_actor *self, struct bini_stream *bs);
25
void (*deserialize[KF_MAX_ACTOR_TYPES])(struct kf_actor *self, struct bini_stream *bs);
26
};
27
/* Global actor registry instance. */
28
extern struct kf_actorregistry kf_actorregistry;
···
31
struct kf_actor
32
{
33
/* Unique string identifier for this actor. */
34
-
char *id;
35
-
/* Spritesheet for the actor. */
36
-
struct kf_spritesheet sprites;
37
/* The actor's position. */
38
struct kf_vec2(f32) pos;
39
/* The actor's velocity. */
···
56
enum kf_direction pointing;
57
/* If the actor is running. This will not increase their speed, you are expected to yourself. */
58
bool running;
59
-
/* Called every frame to update the actor. Don't forget about deltatime (kf_dts, kf_dtms)! */
60
-
void (*tick)(struct kf_actor *self);
61
-
/* Called every frame to render the actor. */
62
-
void (*draw)(struct kf_actor *self);
63
/* Doubly-linked list of actors. */
64
struct kf_actor *prev, *next;
65
};
···
69
70
71
/* Create a new actor. */
72
-
struct kf_actor *kf_actor_new(char *id, struct kf_spritesheet sprites, f32 width, f32 height, bool collides);
73
74
/* Load a spritesheet, filling in the details for loading character spritesheets. */
75
struct kf_spritesheet kf_actor_loadspritesheet(char *filename);
···
85
86
/* Get the registry ID of a given actor ID.
87
This will perform a lot of string comparisons, use sparingly! */
88
-
int kf_actor_getregistryid(char *id);
89
90
/* Save actor data. */
91
int kf_saveactors(void);
···
20
struct kf_actorregistry
21
{
22
int count;
23
+
char *key[KF_MAX_ACTOR_TYPES];
24
+
/* Save the actor to a binary stream. */
25
void (*serialize[KF_MAX_ACTOR_TYPES])(struct kf_actor *self, struct bini_stream *bs);
26
+
/* Load the actor from a binary stream. */
27
void (*deserialize[KF_MAX_ACTOR_TYPES])(struct kf_actor *self, struct bini_stream *bs);
28
+
/* Called every frame to update the actor. Don't forget about deltatime (kf_dts, kf_dtms)! */
29
+
void (*tick[KF_MAX_ACTOR_TYPES])(struct kf_actor *self);
30
+
/* Called every frame to render the actor. */
31
+
void (*draw[KF_MAX_ACTOR_TYPES])(struct kf_actor *self);
32
+
/* Spritesheet for the actor. */
33
+
struct kf_spritesheet sprite[KF_MAX_ACTOR_TYPES];
34
};
35
/* Global actor registry instance. */
36
extern struct kf_actorregistry kf_actorregistry;
···
39
struct kf_actor
40
{
41
/* Unique string identifier for this actor. */
42
+
char *key;
43
+
/* Unique integer identifier for this actor, comes from kf_actorregistry. */
44
+
int id;
45
/* The actor's position. */
46
struct kf_vec2(f32) pos;
47
/* The actor's velocity. */
···
64
enum kf_direction pointing;
65
/* If the actor is running. This will not increase their speed, you are expected to yourself. */
66
bool running;
67
/* Doubly-linked list of actors. */
68
struct kf_actor *prev, *next;
69
};
···
73
74
75
/* Create a new actor. */
76
+
struct kf_actor *kf_actor_new(char *key);
77
78
/* Load a spritesheet, filling in the details for loading character spritesheets. */
79
struct kf_spritesheet kf_actor_loadspritesheet(char *filename);
···
89
90
/* Get the registry ID of a given actor ID.
91
This will perform a lot of string comparisons, use sparingly! */
92
+
int kf_actor_getregistryid(char *key);
93
94
/* Save actor data. */
95
int kf_saveactors(void);
+2
-2
include/keraforge/player.h
+2
-2
include/keraforge/player.h
+16
-16
src/actor.c
+16
-16
src/actor.c
···
1
-
#include "keraforge/bini.h"
2
#include <keraforge.h>
3
#include <stdlib.h>
4
#include <raymath.h>
···
10
u32 kf_actor_count = 0;
11
12
13
-
struct kf_actor *kf_actor_new(char *id, struct kf_spritesheet sprites, f32 width, f32 height, bool collides)
14
{
15
struct kf_actor *actor = calloc(1, sizeof(struct kf_actor));
16
kf_actor_count++;
···
26
kf_actors_last = actor;
27
}
28
29
-
actor->id = id;
30
-
actor->sprites = sprites;
31
-
actor->size.x = width;
32
-
actor->size.y = height;
33
-
actor->collide = collides;
34
35
actor->speed = 25;
36
actor->speedmod = 1;
···
174
175
x += (int)(kf_s * (frames+1)) % frames;
176
177
-
kf_drawsprite(&actor->sprites, actor->pos.x, actor->pos.y, x, y);
178
}
179
180
-
int kf_actor_getregistryid(char *id)
181
{
182
-
size_t l = strlen(id);
183
for (int i = 0 ; i < kf_actorregistry.count ; i++)
184
{
185
-
char *c = kf_actorregistry.id[i];
186
size_t cl = strlen(c);
187
-
if (strncmp(c, id, l>cl?l:cl) == 0)
188
{
189
return i;
190
}
···
209
210
for (struct kf_actor *actor = kf_actors ; actor != NULL ; actor = actor->next)
211
{
212
-
if (!actor->id)
213
continue;
214
-
int id = kf_actor_getregistryid(actor->id);
215
if (id == -1)
216
continue;
217
-
bini_wstr(bs, actor->id);
218
kf_actorregistry.serialize[id](actor, bs);
219
nactors++;
220
}
···
259
int id = kf_actor_getregistryid(key);
260
if (id == -1)
261
continue;
262
kf_actorregistry.deserialize[id](actor, &bs);
263
-
nactors++;
264
}
265
266
kf_logdbg("loaded %d actors", nactors);
···
1
+
#include "keraforge/actor.h"
2
#include <keraforge.h>
3
#include <stdlib.h>
4
#include <raymath.h>
···
10
u32 kf_actor_count = 0;
11
12
13
+
struct kf_actor *kf_actor_new(char *key)
14
{
15
struct kf_actor *actor = calloc(1, sizeof(struct kf_actor));
16
kf_actor_count++;
···
26
kf_actors_last = actor;
27
}
28
29
+
actor->id = kf_actor_getregistryid(key);
30
+
actor->key = key;
31
+
actor->size.x = 10;
32
+
actor->size.y = 10;
33
+
actor->collide = true;
34
35
actor->speed = 25;
36
actor->speedmod = 1;
···
174
175
x += (int)(kf_s * (frames+1)) % frames;
176
177
+
kf_drawsprite(&kf_actorregistry.sprite[actor->id], actor->pos.x, actor->pos.y, x, y);
178
}
179
180
+
int kf_actor_getregistryid(char *key)
181
{
182
+
size_t l = strlen(key);
183
for (int i = 0 ; i < kf_actorregistry.count ; i++)
184
{
185
+
char *c = kf_actorregistry.key[i];
186
size_t cl = strlen(c);
187
+
if (strncmp(c, key, l>cl?l:cl) == 0)
188
{
189
return i;
190
}
···
209
210
for (struct kf_actor *actor = kf_actors ; actor != NULL ; actor = actor->next)
211
{
212
+
if (!actor->key)
213
continue;
214
+
int id = kf_actor_getregistryid(actor->key);
215
if (id == -1)
216
continue;
217
+
bini_wstr(bs, actor->key);
218
kf_actorregistry.serialize[id](actor, bs);
219
nactors++;
220
}
···
259
int id = kf_actor_getregistryid(key);
260
if (id == -1)
261
continue;
262
+
struct kf_actor *actor = kf_actor_new(key, k, f32 width, f32 height, bool collides)
263
kf_actorregistry.deserialize[id](actor, &bs);
264
}
265
266
kf_logdbg("loaded %d actors", nactors);
+7
-15
src/player.c
+7
-15
src/player.c
···
1
#include <keraforge.h>
2
3
···
81
}
82
}
83
84
-
85
-
struct _kf_serialized_player
86
{
87
-
struct kf_vec2(f32) pos;
88
-
};
89
-
90
-
u8* kf_player_serialize(struct kf_actor *self, size_t *plen)
91
-
{
92
-
*plen = sizeof(struct _kf_serialized_player);
93
-
struct _kf_serialized_player *s = malloc(*plen);
94
-
s->pos = self->pos;
95
-
return (u8 *)s;
96
}
97
98
-
void kf_player_deserialize(struct kf_actor *self, u8 *data, size_t len)
99
{
100
-
(void)len;
101
-
struct _kf_serialized_player *s = (struct _kf_serialized_player *)data;
102
-
self->pos = s->pos;
103
}
···
1
+
#include "keraforge/bini.h"
2
#include <keraforge.h>
3
4
···
82
}
83
}
84
85
+
void kf_player_serialize(struct kf_actor *self, struct bini_stream *bs)
86
{
87
+
bini_wf(bs, self->pos.x);
88
+
bini_wf(bs, self->pos.y);
89
}
90
91
+
void kf_player_deserialize(struct kf_actor *self, struct bini_stream *bs)
92
{
93
+
self->pos.x = bini_rf(bs);
94
+
self->pos.y = bini_rf(bs);
95
}