summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoraura <nw@moneybot.cc>2026-07-26 02:09:41 +0200
committeraura <nw@moneybot.cc>2026-07-26 02:09:41 +0200
commite86a60e6c7475819f99bb042793a7ad568368e2f (patch)
tree329a18d729a3db550bb0204cf8ff98ab625621d2 /src
parente827fd28abfacee5784b5a86548ebd668c84a070 (diff)
better prop system + profiler trace
Diffstat (limited to 'src')
-rw-r--r--src/game/object.h33
-rw-r--r--src/game/player.h2
-rw-r--r--src/game/world/world.h1
-rw-r--r--src/util/mem.h6
-rw-r--r--src/util/profiler.h18
5 files changed, 51 insertions, 9 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;
};
diff --git a/src/util/mem.h b/src/util/mem.h
new file mode 100644
index 0000000..852e844
--- /dev/null
+++ b/src/util/mem.h
@@ -0,0 +1,6 @@
+#include "typedef.h"
+
+[[maybe_unused, clang::noinline]]
+static void* __cur_retaddr() {
+ return __builtin_extract_return_addr( __builtin_return_address(0) );
+}
diff --git a/src/util/profiler.h b/src/util/profiler.h
index 7caac96..334ad62 100644
--- a/src/util/profiler.h
+++ b/src/util/profiler.h
@@ -4,6 +4,7 @@
#include "allocator.h"
#include "time.h"
#include "thread.h"
+#include "mem.h"
#include "fnv.h"
static THREAD_MUTEX __profiler_intern_mutex;
@@ -13,7 +14,7 @@ static THREAD_MUTEX __profiler_intern_mutex;
#endif
#define PROFILE( x ) \
- PROFILER_LIST_ENTRY* __profiler_intern_id = __profiler_intern_start( x ); \
+ PROFILER_LIST_ENTRY* __profiler_intern_id = __profiler_intern_start( x, (U64)__cur_retaddr() ); \
defer( { __profiler_intern_end( __profiler_intern_id ); } )
#define _profiled PROFILE( __func__ );
@@ -24,6 +25,7 @@ struct PROFILER_LIST_ENTRY {
const char* name;
U64 duration;
U64 start;
+ U64 ptr;
FNV1A hash;
PROFILER_LIST_ENTRY* parent;
@@ -76,7 +78,7 @@ inline void __profiler_intern_new_frame( PROFILER_LIST_ENTRY* entry ) {
}
}
-inline PROFILER_LIST_ENTRY* __profiler_intern_start( const char* name ) {
+inline PROFILER_LIST_ENTRY* __profiler_intern_start( const char* name, U64 ptr ) {
PROFILER_LIST_ENTRY e;
FNV1A fnv = fnv1a( name );
U64 tick = u_tick();
@@ -100,6 +102,7 @@ inline PROFILER_LIST_ENTRY* __profiler_intern_start( const char* name ) {
e.children = {};
e.parent = gprof.current;
e.hash = fnv;
+ e.ptr = ptr;
if( gprof.current )
ep = gprof.current->children.push( e );
@@ -124,14 +127,25 @@ inline void __profiler_intern_init() {
thread_mutex_init( &__profiler_intern_mutex );
}
+inline void __profiler_intern_trace() {
+ thread_mutex_lock( &__profiler_intern_mutex );
+ PROFILER_LIST_ENTRY* entry = gprof.current;
+ dlog( "======== [ profiler trace ] ==========\n" );
+ while( entry ) {
+ dlog( "%s [0x%llx]\n", entry->name, entry->ptr );
+ }
+}
+
extern void __profiler_intern_draw_overlay( struct GL_FONT* font );
#define profiler_init() __profiler_intern_init()
#define profiler_draw_tree( font ) __profiler_intern_draw_overlay( font )
+#define profiler_trace() __profiler_intern_trace()
#else
#define PROFILE( x )
#define _profiled
+#define profiler_trace()
#define profiler_init()
#define profiler_draw_tree( x )
#endif