aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUneven Prankster <unevenprankster@protonmail.com>2023-07-25 17:37:40 -0300
committerUneven Prankster <unevenprankster@protonmail.com>2023-07-25 17:37:40 -0300
commit593e8163a2442546a459d98d8febcaa9c2cbceb9 (patch)
tree713cf2bc250a358ac7ff480ae4aeb443a0c5b837
parent6f4930e920b3f368d0fcb9acfd2824095b101681 (diff)
Added explanation for Fixed-point usage, + a useful macro
-rw-r--r--src/config.h16
-rw-r--r--src/controller.c4
-rw-r--r--src/rumble.c7
3 files changed, 23 insertions, 4 deletions
diff --git a/src/config.h b/src/config.h
index 5099409..18dc414 100644
--- a/src/config.h
+++ b/src/config.h
@@ -9,12 +9,28 @@ typedef unsigned long long u64;
typedef float f32;
typedef double f64;
+/*
+You may be wondering "why fixed point values???", and that's simple
+Floating Point was a mistake and has caused great suffering to countless.
+Time-keeping is specially burdened. Nobody should ever keep track of time
+with float values. Variable precision can and WILL throw off calculations
+no matter how much of a NERD you are.
+
+What I did was to just leverage the support for floats in order to make most
+things simply take them (that's what everyone expects) then convert them to
+fixed point for precise operations.
+
+They can still be happily used for other things, specially inside shaders.
+But don't @ me.
+*/
+
// 24.8 format
typedef i32 fx32;
#define FIXED_POINT_BITS 8
#define FIXED_POINT_ONE (1 << FIXED_POINT_BITS)
#define TO_FIXED(x) ((fx32)(x) << FIXED_POINT_BITS)
#define FROM_FIXED(x) ((x) >> FIXED_POINT_BITS)
+#define FP_TO_FIXED(x) (fx32)((x) * FIXED_POINT_BITS)
typedef struct{
fx32 x, y;
diff --git a/src/controller.c b/src/controller.c
index cacada3..aa8d4d9 100644
--- a/src/controller.c
+++ b/src/controller.c
@@ -19,8 +19,8 @@ b32 is_button_released(i32 btn)
vec2 get_dir_input(void)
{
- fx32 cont_x = (fx32)(FIXED_POINT_ONE * GetGamepadAxisMovement(0, 0));
- fx32 cont_y = (fx32)(FIXED_POINT_ONE * GetGamepadAxisMovement(0, 1));
+ fx32 cont_x = FP_TO_FIXED(GetGamepadAxisMovement(0, 0));
+ fx32 cont_y = FP_TO_FIXED(GetGamepadAxisMovement(0, 1));
fx32 key_w = is_button_held(KEY_W) ? FIXED_POINT_ONE : 0;
fx32 key_s = is_button_held(KEY_S) ? FIXED_POINT_ONE : 0;
diff --git a/src/rumble.c b/src/rumble.c
index 2313d68..2212761 100644
--- a/src/rumble.c
+++ b/src/rumble.c
@@ -2,6 +2,8 @@
I made this a separate source file because Raylib has some quirks
regarding Windows compilation. And I also suppose because it has
some state of its own.
+
+NOTE FOR SPELUNKERS: this requires linking to xinput1_4 AFAIK
*/
#include "controller.h"
@@ -23,16 +25,17 @@ void set_rumble(f32 duration, f32 strength)
state.wRightMotorSpeed = (unsigned)(strength * 65535.0f);
if (XInputSetState(0, &state) == 0){
- timer = FIXED_POINT_ONE * duration;
+ timer = FP_TO_FIXED(duration);
}
}
void poll_rumble(f32 dt)
{
+ // Early return
if(timer <= 0)
return;
- fx32 delta = FIXED_POINT_ONE * dt;
+ fx32 delta = FP_TO_FIXED(dt);
timer -= delta;
if(timer <= 0){