]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Extract registry functionality
authorTimePath <andrew.hardaker1995@gmail.com>
Tue, 11 Aug 2015 03:04:45 +0000 (13:04 +1000)
committerTimePath <andrew.hardaker1995@gmail.com>
Tue, 11 Aug 2015 03:04:45 +0000 (13:04 +1000)
qcsrc/common/items/all.qh
qcsrc/common/items/item.qh
qcsrc/common/registry.qh [new file with mode: 0644]

index 1786b4261131d020fa92996027dee78c44c25a8d..647958628069ba633346e94c9b8fcafe3f4ea9d4 100644 (file)
@@ -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
index 507ca93e029c993f00fccf6132e19213f02d48c7..6ecd6d7f7a49fdba67f0271a30d4259dc78e1293 100644 (file)
@@ -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 (file)
index 0000000..d6f884c
--- /dev/null
@@ -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