diff --git a/meson.build b/meson.build index c260da3..acf64a3 100644 --- a/meson.build +++ b/meson.build @@ -2,7 +2,10 @@ project('Cube', ['c', 'objcpp'], meson_version: '1.5.0') add_global_arguments( - ['-fobjc-arc', '-fobjc-arc-exceptions'], + [ + '-fobjc-arc', + '-fobjc-arc-exceptions' + ], language: 'objcpp') objfw_dep = dependency('objfw') @@ -39,6 +42,7 @@ executable('client', 'src/console.mm', 'src/editing.mm', 'src/entities.mm', + 'src/init.mm', 'src/main.mm', 'src/menus.mm', 'src/monster.mm', diff --git a/src/cube.h b/src/cube.h index 0485cf9..6b3e99d 100644 --- a/src/cube.h +++ b/src/cube.h @@ -1,5 +1,7 @@ // one big bad include file for the whole engine... nasty! +#import + #include "tools.h" enum // block types, order matters! @@ -390,21 +392,50 @@ enum // function signatures for script functions, see command.cpp // nasty macros for registering script functions, abuses globals to avoid // excessive infrastructure #define COMMANDN(name, fun, nargs) \ - static bool __dummy_##fun = addcommand(#name, (void (*)())fun, nargs) + OF_CONSTRUCTOR() \ + { \ + enqueueInit(#name, ^{ \ + addcommand(#name, (void (*)())fun, nargs); \ + }); \ + } #define COMMAND(name, nargs) COMMANDN(name, name, nargs) #define VARP(name, min, cur, max) \ - int name = variable(#name, min, cur, max, &name, NULL, true) + int name; \ + OF_CONSTRUCTOR() \ + { \ + enqueueInit(#name, ^{ \ + name = variable(#name, min, cur, max, &name, NULL, true); \ + }); \ + } #define VAR(name, min, cur, max) \ - int name = variable(#name, min, cur, max, &name, NULL, false) + int name; \ + OF_CONSTRUCTOR() \ + { \ + enqueueInit(#name, ^{ \ + name = variable(#name, min, cur, max, &name, NULL, false); \ + }); \ + } #define VARF(name, min, cur, max, body) \ void var_##name(); \ - static int name = \ - variable(#name, min, cur, max, &name, var_##name, false); \ + static int name; \ + OF_CONSTRUCTOR() \ + { \ + enqueueInit(#name, ^{ \ + name = variable( \ + #name, min, cur, max, &name, var_##name, false); \ + }); \ + } \ void var_##name() { body; } #define VARFP(name, min, cur, max, body) \ void var_##name(); \ - static int name = \ - variable(#name, min, cur, max, &name, var_##name, true); \ + static int name; \ + OF_CONSTRUCTOR() \ + { \ + enqueueInit(#name, ^{ \ + name = \ + variable(#name, min, cur, max, &name, var_##name, true); \ + }); \ + } \ void var_##name() { body; } #define ATOI(s) strtol(s, NULL, 0) // supports hexadecimal numbers diff --git a/src/init.mm b/src/init.mm new file mode 100644 index 0000000..e599ca8 --- /dev/null +++ b/src/init.mm @@ -0,0 +1,24 @@ +#include + +#import "cube.h" +#import "protos.h" + +static std::vector *queue; + +void +enqueueInit(const char *name, void (^init)(void)) +{ + if (queue == NULL) + queue = new std::vector(); + + queue->push_back(init); +} + +void +processInitQueue(void) +{ + for (auto &init : *queue) + init(); + + queue->clear(); +} diff --git a/src/main.mm b/src/main.mm index 1709126..25d4880 100644 --- a/src/main.mm +++ b/src/main.mm @@ -104,6 +104,8 @@ main(int argc, char **argv) char *sdesc = "", *ip = "", *master = NULL, *passwd = ""; islittleendian = *((char *)&islittleendian); + processInitQueue(); + #define log(s) conoutf("init: %s", s) log("sdl"); diff --git a/src/protos.h b/src/protos.h index 10cf9da..b759773 100644 --- a/src/protos.h +++ b/src/protos.h @@ -23,6 +23,10 @@ extern void conoutf(const char *s, ...); extern char *getcurcommand(); extern void writebinds(FILE *f); +// init +extern void enqueueInit(const char *name, void (^init)(void)); +extern void processInitQueue(void); + // menus extern bool rendermenu(); extern void menuset(int menu);