summaryrefslogtreecommitdiffhomepage
path: root/src/con_.h
diff options
context:
space:
mode:
authorMichael Smith <mikesmiffy128@gmail.com>2021-12-29 18:14:01 +0000
committerMichael Smith <mikesmiffy128@gmail.com>2021-12-29 18:18:42 +0000
commitd40d588f6d7cf866f7de41db9efffdd6c1a05135 (patch)
treecc1ff1eaecf3680815d7b30776f8b6264b0242e3 /src/con_.h
parent8eecc029568bbe8e2f3c0d9af218ad3f957251c9 (diff)
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.
Diffstat (limited to 'src/con_.h')
-rw-r--r--src/con_.h13
1 files changed, 11 insertions, 2 deletions
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)