A game engine for top-down 2D RPG games.
rpg
game-engine
raylib
c99
1#include <keraforge.h>
2#include <raylib.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6
7
8static const char *HELP =
9"usage: newgame [options...]\n\n"
10"options:\n"
11"\t-w --width <int> specify width for the world (default: 1024)\n"
12"\t-h --height <int> specify height for the world (default: 1024)\n"
13"\t-s --size <int> specify width and height for the world\n"
14"\t-p --path <str> specify path to save the game in (default: path)\n"
15"\t-f --force create the new game even if the directory exists, this will delete data\n"
16"\t --no-force opposite of -f (default)\n"
17"\t-c --compress compress the world after creating it (recommended)\n"
18"\t --no-compress don't compress the world after creating it (default)\n"
19"\t --help display this message\n"
20;
21
22
23int main(int argc, char *argv[])
24{
25 char *path = "data";
26 int width = 1024, height = 1024;
27 bool compress = false;
28 bool force = false;
29
30 for (int i = 1 ; i < argc ; i++)
31 {
32 char *arg = argv[i];
33
34# define _checkshort(SHORT) (strncmp(arg, "-" SHORT, strlen("-" SHORT)) == 0)
35# define _checklong(LONG) (strncmp(arg, "--" LONG, strlen("--" LONG)) == 0)
36# define _check(SHORT, LONG) (_checkshort(SHORT) || _checklong(LONG))
37
38 if (_check("w", "width"))
39 width = atoi(argv[++i]);
40 else if (_check("h", "height"))
41 height = atoi(argv[++i]);
42 else if (_check("s", "size"))
43 width = height = atoi(argv[++i]);
44 else if (_check("p", "path"))
45 path = argv[++i];
46 else if (_check("c", "compress"))
47 compress = true;
48 else if (_checklong("no-compress"))
49 compress = false;
50 else if (_check("f", "force"))
51 force = true;
52 else if (_checklong("no-force"))
53 force = false;
54 else if (_checklong("help"))
55 {
56 kf_loginfo("%s", HELP);
57 exit(0);
58 }
59 else
60 {
61 kf_logerr("invalid argument: %s", arg);
62 exit(1);
63 }
64
65# undef _checkshort
66# undef _checklong
67# undef _check
68 }
69
70 if (!force && DirectoryExists(path))
71 KF_THROW("path exists: %s", path);
72
73 struct kf_world *world = NULL;
74
75 kf_loginfo("creating world");
76 world = kf_world_new(width, height, 2);
77
78 /* path for our map.bin */
79 char worldpath[4096] = {0};
80 strcpy(worldpath, path);
81 strcpy(&worldpath[0] + strlen(path), compress ? "/tmp/map.bin" : "/map.bin");
82 MakeDirectory(GetDirectoryPath(worldpath));
83
84 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);
89
90 if (compress)
91 {
92 char worldxzpath[4096] = {0};
93 strcpy(worldxzpath, path);
94 strcpy(&worldxzpath[0] + strlen(path), "/map.bin.xz");
95 if (!kf_compress(worldpath, worldxzpath))
96 KF_THROW("failed to compress %s", worldpath);
97 remove(worldpath); /* no longer needed */
98 }
99
100 free(world);
101}