From: TimePath Date: Tue, 11 Aug 2015 03:04:45 +0000 (+1000) Subject: Extract registry functionality X-Git-Tag: xonotic-v0.8.1~13 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=367e29dae1e4e500e95743064bcd1db66ab62ca9;p=xonotic%2Fxonotic-data.pk3dir.git Extract registry functionality --- diff --git a/qcsrc/common/items/all.qh b/qcsrc/common/items/all.qh index 1786b4261..647958628 100644 --- a/qcsrc/common/items/all.qh +++ b/qcsrc/common/items/all.qh @@ -1,8 +1,14 @@ +#include "../registry.qh" + #ifndef ALL_H #define ALL_H +void RegisterItems(); const int MAX_ITEMS = 24; entity ITEMS[MAX_ITEMS]; +int ITEM_COUNT; +/** If you register a new item, make sure to add it to all.inc */ +#define REGISTER_ITEM(id, class) REGISTER(RegisterItems, ITEM, ITEMS, ITEM_COUNT, id, class) #define ITEMS_FOREACH(pred, body) do { \ for (int i = 0; i < ITEM_COUNT; i++) { \ @@ -11,7 +17,6 @@ entity ITEMS[MAX_ITEMS]; } \ } while(0) -void RegisterItems(); void Dump_Items(); #endif diff --git a/qcsrc/common/items/item.qh b/qcsrc/common/items/item.qh index 507ca93e0..6ecd6d7f7 100644 --- a/qcsrc/common/items/item.qh +++ b/qcsrc/common/items/item.qh @@ -2,6 +2,7 @@ #define GAMEITEM_H #include "../oo.qh" #define ITEM_HANDLE(signal, ...) __Item_Send_##signal(__VA_ARGS__) +/** If you register a new item, make sure to add it to all.inc */ CLASS(GameItem, Object) ATTRIB(GameItem, m_id, int, 0) METHOD(GameItem, show, void(entity this)) @@ -9,19 +10,4 @@ CLASS(GameItem, Object) void ITEM_HANDLE(Show, entity this) { this.show(this); } ENDCLASS(GameItem) - -int ITEM_COUNT; -/** If you register a new item, make sure to add it to all.inc */ -#define REGISTER_ITEM(id, class) \ - entity ITEM_##id; \ - void RegisterItems_init_##id(entity this) {} \ - void RegisterItems_##id() { \ - entity this = NEW(class); \ - ITEM_##id = this; \ - this.m_id = ITEM_COUNT; \ - ITEMS[ITEM_COUNT++] = this; \ - RegisterItems_init_##id(this); \ - } \ - ACCUMULATE_FUNCTION(RegisterItems, RegisterItems_##id) \ - [[accumulate]] void RegisterItems_init_##id(entity this) #endif diff --git a/qcsrc/common/registry.qh b/qcsrc/common/registry.qh new file mode 100644 index 000000000..d6f884c31 --- /dev/null +++ b/qcsrc/common/registry.qh @@ -0,0 +1,17 @@ +#ifndef REGISTRY_H +#define REGISTRY_H + +#define REGISTER(initfunc, ns, array, counter, id, class) \ + entity ns##_##id; \ + void Register_##ns##_##id##_init(entity this) { } \ + void Register_##ns##_##id() { \ + entity this = NEW(class); \ + ns##_##id = this; \ + this.m_id = ns##_COUNT; \ + array[counter++] = this; \ + Register_##ns##_##id##_init(this); \ + } \ + ACCUMULATE_FUNCTION(initfunc, Register_##ns##_##id) \ + [[accumulate]] void Register_##ns##_##id##_init(entity this) + +#endif