A game engine for top-down 2D RPG games.
rpg game-engine raylib c99

(temp) progress

Changed files
+84 -18
include
keraforge
src
tools
+17
include/keraforge/time.h
··· 6 6 #include <time.h> 7 7 8 8 9 + #define kf_timeit(_msg, ...) \ 10 + do \ 11 + { \ 12 + struct kf_timer __timer = { \ 13 + .mode = kf_timermode_stopwatch, \ 14 + }; \ 15 + kf_timer_start(&__timer); \ 16 + __VA_ARGS__; \ 17 + kf_timer_stop(&__timer); \ 18 + char __timerstr[BUFSIZ] = {0}; \ 19 + kf_timer_snprint(&__timer, __timerstr, sizeof(__timerstr)); \ 20 + kf_loginfo("took [%s] to %s", __timerstr, _msg); \ 21 + } \ 22 + while (0) 23 + 24 + 9 25 enum kf_timermode 10 26 { 11 27 /* Manually stopped timer. Upon stopping, age is ··· 53 69 void kf_timer_start(struct kf_timer *timer); 54 70 void kf_timer_tick(struct kf_timer *timer); 55 71 void kf_timer_stop(struct kf_timer *timer); 72 + size_t kf_timer_snprint(struct kf_timer *timer, char *buf, size_t bufsiz); 56 73 57 74 58 75 #endif
+4 -4
include/keraforge/world.h
··· 118 118 /* Draw the part of the world visible to the given camera. */ 119 119 void kf_world_draw(struct kf_world *world, Camera2D camera); 120 120 121 - /* Save a world to map.bin(.xz). */ 122 - int kf_world_save(struct kf_world *world, bool compress); 123 - /* Load a world from a map.bin(.xz). */ 124 - int kf_world_load(struct kf_world **world, bool compressed); 121 + /* Save a world to map.bin(.xz). Set outfile to NULL to use the default. */ 122 + int kf_world_save(struct kf_world *world, bool compress, char *outfile); 123 + /* Load a world from a map.bin(.xz). Set infile to NULL to use the default. */ 124 + int kf_world_load(struct kf_world **world, bool compressed, char *infile); 125 125 126 126 #endif
+1 -1
src/editor.c
··· 21 21 kf_logdbg("exiting editor, world is %s", d ? "dirty" : "not dirty"); 22 22 23 23 if (d) 24 - kf_world_save(kf_window.room, false); 24 + kf_timeit("save world", kf_world_save(kf_window.room, true, NULL)); 25 25 26 26 free(kf_window.modal->data); 27 27 kf_window.modal->data = NULL;
+1 -1
src/graphics.c
··· 47 47 kf_window.state = state; 48 48 49 49 struct kf_world *world = NULL; 50 - kf_world_load(&world, false); 50 + kf_timeit("load world", kf_world_load(&world, true, NULL)); 51 51 kf_window.room = world; 52 52 53 53 kf_actorregistry.id[0] = "player";
+46 -1
src/time.c
··· 1 + #include "keraforge/time.h" 1 2 #include <keraforge.h> 3 + #include <time.h> 2 4 3 5 4 6 void kf_timer_start(struct kf_timer *timer) ··· 13 15 14 16 void kf_timer_tick(struct kf_timer *timer) 15 17 { 18 + if (timer->stop) 19 + return; 20 + 16 21 time_t now = time(NULL); 17 22 time_t dif = now - timer->now; 18 23 timer->age += dif; ··· 23 28 case kf_timermode_stopwatch: 24 29 break; 25 30 case kf_timermode_repeat: 31 + if (timer->age >= timer->length) 32 + { 33 + timer->age = 0; 34 + if (timer->callback) 35 + timer->callback(timer); 36 + } 37 + break; 26 38 case kf_timermode_oneshot: 39 + if (timer->age >= timer->length) 40 + { 41 + timer->stop = now; 42 + timer->age = 0; 43 + if (timer->callback) 44 + timer->callback(timer); 45 + } 27 46 break; 28 47 } 29 48 30 49 } 31 50 32 - void kf_timer_stop(struct kf_timer *timer); 51 + void kf_timer_stop(struct kf_timer *timer) 52 + { 53 + time(&timer->stop); 54 + if (timer->mode == kf_timermode_stopwatch) 55 + { 56 + timer->length = timer->stop - timer->start; 57 + if (timer->callback) 58 + timer->callback(timer); 59 + } 60 + } 61 + 62 + size_t kf_timer_snprint(struct kf_timer *timer, char *buf, size_t bufsiz) 63 + { 64 + size_t n = 0; 65 + switch (timer->mode) 66 + { 67 + case kf_timermode_stopwatch: 68 + n += strftime(buf, bufsiz, "%Hh:%Mm:%Ss", gmtime(&timer->length)); 69 + break; 70 + case kf_timermode_repeat: 71 + case kf_timermode_oneshot: 72 + n += strftime(buf, bufsiz, "%Hh:%Mm:%Ss/", gmtime(&timer->age)); 73 + n += strftime(buf + n, bufsiz - n, "%Hh:%Mm:%Ss", gmtime(&timer->length)); 74 + break; 75 + } 76 + return n; 77 + }
+4 -4
src/world.c
··· 240 240 } 241 241 } 242 242 243 - int kf_world_save(struct kf_world *world, bool compress) 243 + int kf_world_save(struct kf_world *world, bool compress, char *outfile) 244 244 { 245 - char *outfile = compress ? _KF_MAPFILE_TMP : _KF_MAPFILE; 245 + outfile = outfile ? outfile : (compress ? _KF_MAPFILE_TMP : _KF_MAPFILE); 246 246 struct bini_stream *bs = bini_new(); 247 247 _kf_world_save_bs(world, bs); 248 248 if (!kf_writebin(outfile, bs->buffer, bs->len)) ··· 284 284 *pworld = world; 285 285 } 286 286 287 - int kf_world_load(struct kf_world **pworld, bool compressed) 287 + int kf_world_load(struct kf_world **pworld, bool compressed, char *infile) 288 288 { 289 289 if (compressed) /* decompress before loading */ 290 290 { ··· 302 302 } 303 303 } 304 304 305 - char *infile = compressed ? _KF_MAPFILE_TMP : _KF_MAPFILE; 305 + infile = infile ? infile : (compressed ? _KF_MAPFILE_TMP : _KF_MAPFILE); 306 306 kf_logdbg("loading world: %s", infile); 307 307 308 308 size_t len = 0;
+11 -7
tools/newgame.c
··· 73 73 struct kf_world *world = NULL; 74 74 75 75 kf_loginfo("creating world"); 76 - world = kf_world_new(width, height, 2); 76 + kf_timeit("create world", { 77 + world = kf_world_new(width, height, 2); 78 + }); 77 79 78 80 /* path for our map.bin */ 79 81 char worldpath[4096] = {0}; ··· 82 84 MakeDirectory(GetDirectoryPath(worldpath)); 83 85 84 86 size_t len = kf_world_getsize(world); 85 - kf_loginfo("saving world (%lu bytes uncompressed)", len); 86 - kf_world_save(world, compress); 87 - // if (!kf_writebin(worldpath, (u8 *)world, len)) 88 - // KF_THROW("failed to save %s", worldpath); 87 + kf_loginfo("saving world to %s (%lu bytes uncompressed)", worldpath, len); 88 + kf_timeit("save world", kf_world_save(world, compress, worldpath)); 89 89 90 90 if (compress) 91 91 { 92 92 char worldxzpath[4096] = {0}; 93 93 strcpy(worldxzpath, path); 94 94 strcpy(&worldxzpath[0] + strlen(path), "/map.bin.xz"); 95 - if (!kf_compress(worldpath, worldxzpath)) 96 - KF_THROW("failed to compress %s", worldpath); 95 + kf_timeit("compress world", { 96 + if (!kf_compress(worldpath, worldxzpath)) 97 + { 98 + KF_THROW("failed to compress %s", worldpath); 99 + } 100 + }); 97 101 remove(worldpath); /* no longer needed */ 98 102 } 99 103