From d40d588f6d7cf866f7de41db9efffdd6c1a05135 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Wed, 29 Dec 2021 18:14:01 +0000 Subject: Make convar init macro type-generic This removes the need to call atof() on each cvar on plugin load; now all that's required is the memory allocations for the string values. The syntax is also a bit nicer since numbers can just be numbers rather than quoted strings. Minor issue: specifying a string value that also happens to be numeric will break this since the numeric representation will be zero, but I can't see a reason this would ever happen. Also, add a DEF_CVAR_MAX just for completeness. --- src/con_.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/con_.h') diff --git a/src/con_.h b/src/con_.h index f6ef161..bfc8c47 100644 --- a/src/con_.h +++ b/src/con_.h @@ -225,10 +225,15 @@ extern void *_con_vtab_iconvar[]; }, \ .vtable_iconvar = _con_vtab_iconvar, \ .parent = &_cvar_##name_, /* bizarre, but how the engine does it */ \ - .defaultval = value, .strlen = sizeof("" value), \ + .defaultval = _Generic(value, char *: value, int: #value, \ + float: #value), \ + .strlen = _Generic(value, char *: sizeof(value), \ + default: sizeof(#value)), \ + .fval = _Generic(value, char *: 0, int: value, float: value), \ + .ival = _Generic(value, char *: 0, int: value, float: (int)value), \ .hasmin = hasmin_, .minval = (min), .hasmax = hasmax_, .maxval = (max) \ }; \ - struct con_var *name_ = (struct con_var *)&_cvar_##name_; + struct con_var *name_ = &_cvar_##name_; /* Defines a console variable with no min/max values. */ #define DEF_CVAR(name, desc, value, flags) \ @@ -238,6 +243,10 @@ extern void *_con_vtab_iconvar[]; #define DEF_CVAR_MIN(name_, desc, value, min, flags_) \ _DEF_CVAR(name, desc, value, true, min, false, 0, flags) +/* Defines a console variable with a given maximum numeric value. */ +#define DEF_CVAR_MAX(name_, desc, value, max, flags_) \ + _DEF_CVAR(name, desc, value, false, 0, true, max, flags) + /* Defines a console variable in the given numeric value range. */ #define DEF_CVAR_MINMAX(name_, desc, value, min, max, flags_) \ _DEF_CVAR(name, desc, value, true, min, true, max, flags) -- cgit v1.2.3