From e86a60e6c7475819f99bb042793a7ad568368e2f Mon Sep 17 00:00:00 2001 From: aura Date: Sun, 26 Jul 2026 02:09:41 +0200 Subject: better prop system + profiler trace --- src/game/object.h | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'src/game/object.h') 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* 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* 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* l = obj_proplist( classid ); + l->push( this ); } const char* name; @@ -38,20 +42,36 @@ struct OBJECT_PROP { U32 size; }; + +inline LIST* obj_proplist( U32 classid ) { + static struct LIST 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 children{}; - LIST props{}; OBJECT_DEINIT_FN deinit_fn{}; OBJVAR( VEC3, pos ); -- cgit v1.2.3