summaryrefslogtreecommitdiff
path: root/src/game
diff options
context:
space:
mode:
Diffstat (limited to 'src/game')
-rw-r--r--src/game/object.h33
-rw-r--r--src/game/player.h2
-rw-r--r--src/game/world/world.h1
3 files changed, 29 insertions, 7 deletions
diff --git a/src/game/object.h b/src/game/object.h
index 80ade03..3f0592c 100644
--- a/src/game/object.h
+++ b/src/game/object.h
@@ -5,6 +5,7 @@
typedef void ( *OBJECT_DEINIT_FN )( struct OBJECT* obj );
inline const char* obj_classid_to_name( U32 classid );
+inline LIST<struct OBJECT_PROP*>* obj_proplist( U32 classid );
enum ObjectClass_t {
OBJCLASS_NONE = 0,
@@ -12,6 +13,7 @@ enum ObjectClass_t {
OBJCLASS_TRIGGER,
OBJCLASS_PLAYER,
OBJCLASS_BASENPC,
+ OBJCLASS_LAST
};
static const char* OBJCLASS_NAMES[] = {
@@ -22,14 +24,16 @@ static const char* OBJCLASS_NAMES[] = {
"base_npc",
};
+
// todo: come up with a good way to make these static
struct OBJECT_PROP {
- OBJECT_PROP( const char* name, void* value, U32 size, LIST<OBJECT_PROP*>* list, void* parent ) {
+ OBJECT_PROP( const char* name, U32 size, U32 classid, U32 offset ) {
this->name = name;
this->size = size;
this->hash = fnv1a( name );
- this->offset = (U32)( (U8*)value - (U8*)parent );
- list->push( this );
+ this->offset = offset;
+ LIST<OBJECT_PROP*>* l = obj_proplist( classid );
+ l->push( this );
}
const char* name;
@@ -38,20 +42,36 @@ struct OBJECT_PROP {
U32 size;
};
+
+inline LIST<OBJECT_PROP*>* obj_proplist( U32 classid ) {
+ static struct LIST<OBJECT_PROP*> prop_lists[OBJCLASS_LAST];
+
+ if( classid >= OBJCLASS_LAST ) {
+ dlog( "obj_classid_to_name() : invalid classid passed (%d)", classid );
+ return 0;
+ }
+
+ return &prop_lists[classid];
+}
+
#define OBJVAR( type, name ) \
type name; \
- OBJECT_PROP __##name##_props{ #name, (void*)&name, sizeof(type), &this->props, this }
+ constexpr static U32 __##name##_offset() { OBJ_SELF_TYPE t; return (U32)( (U64)&t.name - (U64)&t ); } \
+ static inline OBJECT_PROP __##name##_props{ #name, sizeof(type), OBJ_SELF_TYPE::CLASSID, (U32)( __##name##_offset() ) }
#define OBJVARV( type, name, val ) \
type name{ type( val ) }; \
- OBJECT_PROP __##name##_props{ #name, (void*)&name, sizeof(type), &this->props, this }
+ constexpr static U32 __##name##_offset() { OBJ_SELF_TYPE t; return (U32)( (U64)&t.name - (U64)&t ); } \
+ static inline OBJECT_PROP __##name##_props{ #name, sizeof(type), OBJ_SELF_TYPE::CLASSID, (U32)( __##name##_offset() ) }
#define OBJVAR_STR( name, len ) \
char name##[len]; \
- OBJECT_PROP __##name##_props{ #name, (void*)name, len, &this->props, this }
+ constexpr static U32 __##name##_offset() { OBJ_SELF_TYPE t; return (U32)( (U64)&t.name - (U64)&t ); } \
+ static inline OBJECT_PROP __##name##_props{ #name, sizeof(type), OBJ_SELF_TYPE::CLASSID, (U32)( __##name##_offset() ) }
struct OBJECT {
static const U32 CLASSID = OBJCLASS_NONE;
+ using OBJ_SELF_TYPE = OBJECT;
U32 classid;
U8 keeponlevel{};
@@ -59,7 +79,6 @@ struct OBJECT {
OBJECT* parent;
LIST<OBJECT*> children{};
- LIST<OBJECT_PROP*> props{};
OBJECT_DEINIT_FN deinit_fn{};
OBJVAR( VEC3, pos );
diff --git a/src/game/player.h b/src/game/player.h
index 27b423d..a2967bd 100644
--- a/src/game/player.h
+++ b/src/game/player.h
@@ -31,6 +31,8 @@ struct PLAYER_INPUT {
struct PLAYER : OBJECT {
static const U32 CLASSID = OBJCLASS_PLAYER;
+ using OBJ_SELF_TYPE = PLAYER;
+
OBJVAR( U32, health );
OBJVARV( F32, fov, 72.f );
OBJVARV( F32, eyeoffset, 40.f );
diff --git a/src/game/world/world.h b/src/game/world/world.h
index 229f235..11af15e 100644
--- a/src/game/world/world.h
+++ b/src/game/world/world.h
@@ -3,6 +3,7 @@
struct WORLD : OBJECT {
static const U32 CLASSID = OBJCLASS_WORLD;
+ using OBJ_SELF_TYPE = WORLD;
struct WORLD_MAP* map;
};