From ba4dafcb4d272640c7cb93eebea37d90209a74a0 Mon Sep 17 00:00:00 2001 From: jacob Date: Fri, 8 Mar 2024 11:50:28 -0600 Subject: [PATCH] convert entity rel_trs -> rel_xform --- src/common.h | 6 +- src/entity.h | 3 +- src/game.c | 101 +++++++++++++++++-------- src/intrinsics.h | 42 +++++------ src/math.h | 186 ++++++++++++++++++++++++++--------------------- src/mixer.c | 12 +-- src/settings.c | 8 +- src/sys_win32.c | 2 +- src/tick.h | 2 +- src/user.c | 41 ++++++----- 10 files changed, 228 insertions(+), 175 deletions(-) diff --git a/src/common.h b/src/common.h index 2186b54a..30667538 100644 --- a/src/common.h +++ b/src/common.h @@ -413,9 +413,9 @@ struct v4_array { struct xform { struct { - struct v2 bx; /* X basis vector */ - struct v2 by; /* Y basis vector */ - struct v2 tl; /* Translation vector */ + struct v2 bx; /* X basis vector (x axis) */ + struct v2 by; /* Y basis vector (y axis)*/ + struct v2 og; /* Translation vector (origin) */ }; }; diff --git a/src/entity.h b/src/entity.h index a155ac15..ebc1ed22 100644 --- a/src/entity.h +++ b/src/entity.h @@ -47,7 +47,7 @@ struct entity { /* ====================================================================== */ /* Translation, rotation, scale in relation to parent entity */ - struct trs rel_trs; + struct xform rel_xform; struct xform world_xform; /* Calculated post-physics */ /* ====================================================================== */ @@ -94,7 +94,6 @@ struct entity { /* ENTITY_PROP_CAMERA */ b32 camera_active; struct entity_handle camera_follow; - f32 camera_rot; f32 camera_zoom; }; diff --git a/src/game.c b/src/game.c index ea8bd2fd..417f4222 100644 --- a/src/game.c +++ b/src/game.c @@ -165,6 +165,9 @@ INTERNAL void game_update(void) /* Player ent */ struct entity *player_ent; { + struct v2 pos = V2(0, 0); + struct v2 size = V2(1, 1); + f32 r = 0; struct string sprite_name = STR("res/graphics/tim.ase"); struct string sprite_tag_name = STR("UNARMED"); @@ -174,12 +177,9 @@ INTERNAL void game_update(void) f32 meters_height = sheet->frame_size.y / PIXELS_PER_UNIT; struct v2 sprite_size = V2(meters_width, meters_height); - struct v2 pos = V2(0, 0); - struct entity *e = entity_alloc(); e->valid = true; - //e->rel_trs = TRS(.t = pos, .r = 0, .s = V2(3, 1)); - e->rel_trs = TRS(.t = pos, .r = 0, .s = V2(1, 1)); + e->rel_xform = XFORM_TRS(.t = pos, .r = r, .s = size); e->sprite_name = sprite_name; e->sprite_tag_name = sprite_tag_name; @@ -197,15 +197,52 @@ INTERNAL void game_update(void) player_ent = e; - entity_enable_prop(e, ENTITY_PROP_TEST); + //entity_enable_prop(e, ENTITY_PROP_TEST); //entity_enable_prop(e, ENTITY_PROP_TEST_FOLLOW_MOUSE); } + /* Child ent */ + { + struct v2 pos = V2(0.5, 0.5); + struct v2 size = V2(1, 1); + f32 r = PI / 2; + + struct string sprite_name = STR("res/graphics/tim.ase"); + struct string sprite_tag_name = STR("UNARMED"); + + struct sheet *sheet = sheet_load(sprite_name); + f32 meters_width = sheet->frame_size.x / PIXELS_PER_UNIT; + f32 meters_height = sheet->frame_size.y / PIXELS_PER_UNIT; + struct v2 sprite_size = V2(meters_width, meters_height); + + struct entity *e = entity_alloc(); + e->valid = true; + e->rel_xform = XFORM_TRS(.t = pos, .r = r, .s = size); + + e->sprite_name = sprite_name; + e->sprite_tag_name = sprite_tag_name; + e->sprite_trs = TRS(.s = sprite_size); + e->sprite_pivot_norm = V2(0, 0.65); + e->sprite_tint = RGBA_F(0.5, 0.5, 0.3, 0.5); + + //entity_enable_prop(e, ENTITY_PROP_PLAYER_CONTROLLED); + //e->player_max_speed = 5.f; + //e->player_acceleration = 15.0f; + + entity_enable_prop(e, ENTITY_PROP_ANIMATING); + e->animation_looping = true; + + //entity_enable_prop(e, ENTITY_PROP_TEST); + //entity_enable_prop(e, ENTITY_PROP_TEST_FOLLOW_MOUSE); + + entity_tree_attach(player_ent, e); + } + /* Camera ent */ { struct entity *e = entity_alloc(); e->valid = true; - e->rel_trs = TRS(); + e->rel_xform = XFORM_IDENT; entity_enable_prop(e, ENTITY_PROP_CAMERA); e->camera_active = true; @@ -219,6 +256,8 @@ INTERNAL void game_update(void) * Process game cmds * ========================== */ + L.tick.player_move_dir = V2(0, 0); + struct game_cmd_array game_cmds = pop_cmds(scratch.arena); for (u64 i = 0; i < game_cmds.count; ++i) { struct game_cmd cmd = game_cmds.cmds[i]; @@ -227,11 +266,8 @@ INTERNAL void game_update(void) /* Movement */ case GAME_CMD_KIND_PLAYER_MOVE: { struct v2 dir = cmd.dir; - if (v2_len(dir) > 1.f) { - /* Clamp magnitude */ - dir = v2_norm(dir); - } - L.tick.player_move = dir; + + L.tick.player_move_dir = v2_add(L.tick.player_move_dir, dir); } break; /* Focus */ @@ -243,6 +279,11 @@ INTERNAL void game_update(void) }; } + if (v2_len(L.tick.player_move_dir) > 1.f) { + /* Clamp movement magnitude */ + L.tick.player_move_dir = v2_norm(L.tick.player_move_dir); + } + /* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */ @@ -261,7 +302,7 @@ INTERNAL void game_update(void) /* ENTITY_PROP_TEST */ if (entity_has_prop(ent, ENTITY_PROP_TEST) && !ent->test_initialized) { ent->test_initialized = true; - ent->test_start_rel_trs = ent->rel_trs; + ent->test_start_rel_trs = trs_from_xform(ent->rel_xform); ent->test_start_sprite_trs = ent->sprite_trs; } @@ -309,18 +350,14 @@ INTERNAL void game_update(void) /* ENTITY_PROP_TEST */ if (entity_has_prop(ent, ENTITY_PROP_TEST)) { f32 t = ((f32)L.tick.time); - f32 r = t * 0.5f; - f32 s = 1 + (math_abs_f32(math_sin(t * 5)) * 3); + f32 r = t * 2.f; + f32 s = 1 + (math_fabs(math_sin(t * 5)) * 3); (UNUSED)r; (UNUSED)s; - ent->rel_trs.r = ent->test_start_rel_trs.r + (r * -5.f); - //ent->sprite_trs.r = ent->test_start_sprite_trs.r + (-r * 3.0f); - - //ent->rel_trs.s = v2_mul(ent->test_start_rel_trs.s, s); - ent->rel_trs.s.x = ent->test_start_rel_trs.s.x * s; - //ent->sprite_trs.s = v2_mul(ent->test_start_sprite_trs.s, s); + ent->rel_xform = xform_with_rotation(ent->rel_xform, r); + ent->rel_xform = xform_with_scale(ent->rel_xform, V2(s, 1)); } } @@ -341,7 +378,7 @@ INTERNAL void game_update(void) f32 max_speed = ent->player_max_speed; f32 acceleration_rate = ent->player_acceleration; acceleration_rate = clamp_f32(acceleration_rate, 0, GAME_FPS); /* Can't integrate acceleration rate higher than FPS */ - struct v2 target_velocity = v2_mul(L.tick.player_move, max_speed); + struct v2 target_velocity = v2_mul(L.tick.player_move_dir, max_speed); struct v2 target_acceleration = v2_sub(target_velocity, ent->velocity); ent->acceleration = v2_mul(target_acceleration, acceleration_rate); } @@ -358,7 +395,7 @@ INTERNAL void game_update(void) ent->velocity = v2_add(ent->velocity, a); /* Apply velocity to position */ - ent->rel_trs.t = v2_add(ent->rel_trs.t, v2_mul(ent->velocity, dt)); + ent->rel_xform.og = v2_add(ent->rel_xform.og, v2_mul(ent->velocity, dt)); } /* ========================== * @@ -366,9 +403,10 @@ INTERNAL void game_update(void) * ========================== */ if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) { - struct v2 ent_pos = ent->rel_trs.t; + struct v2 ent_pos = ent->rel_xform.og; struct v2 look_pos = L.tick.player_focus; - ent->rel_trs.r = v2_angle_to_point(ent_pos, look_pos); + f32 r = v2_angle_to_point(ent_pos, look_pos) + PI / 2; + ent->rel_xform = xform_with_rotation(ent->rel_xform, r); } /* ========================== * @@ -377,7 +415,7 @@ INTERNAL void game_update(void) /* ENTITY_PROP_TEST_FOLLOW_MOUSE */ if (entity_has_prop(ent, ENTITY_PROP_TEST_FOLLOW_MOUSE)) { - ent->rel_trs.t = L.tick.player_focus; + ent->rel_xform.og = L.tick.player_focus; ent->test_start_rel_trs.t = L.tick.player_focus; } @@ -385,12 +423,12 @@ INTERNAL void game_update(void) * Calculate xforms * ========================== */ - ent->world_xform = xform_from_trs(ent->rel_trs); + ent->world_xform = ent->rel_xform; struct entity *child = entity_from_handle(ent->first); struct xform parent_xform = ent->world_xform; while (child->valid) { - child->world_xform = xform_trs(parent_xform, child->rel_trs); + child->world_xform = xform_mul(parent_xform, child->rel_xform); /* Depth first iteration */ if (child->first.gen) { @@ -427,10 +465,9 @@ INTERNAL void game_update(void) if (entity_has_prop(ent, ENTITY_PROP_CAMERA)) { struct entity *follow = entity_from_handle(ent->camera_follow); - struct v2 pos = follow->rel_trs.t; - - ent->rel_trs.t = pos; - ent->world_xform = xform_from_trs(ent->rel_trs); + ent->world_xform = follow->world_xform; + ent->world_xform = xform_with_rotation(ent->world_xform, 0); + ent->world_xform = xform_with_scale(ent->world_xform, V2(1, 1)); } /* ========================== * @@ -440,7 +477,7 @@ INTERNAL void game_update(void) if (entity_has_prop(ent, ENTITY_PROP_TEST_SOUND_EMITTER)) { struct mixer_desc desc = ent->sound_desc; desc.speed = L.timescale; - desc.pos = xform_get_pos(ent->world_xform); + desc.pos = ent->world_xform.og; struct sound *sound = sound_load_async(ent->sound_name, 0); b32 played = ent->sound_handle.gen != 0; if (sound) { diff --git a/src/intrinsics.h b/src/intrinsics.h index 93dcdf45..ad306cfa 100644 --- a/src/intrinsics.h +++ b/src/intrinsics.h @@ -5,41 +5,39 @@ * Math * ========================== */ -#if 0 -INLINE f32 ix_sqrt_f(f32 f) +INLINE f32 ix_sqrt_f32(f32 f) { - return _mm_cvtss_f32(_mm_sqrt_ss(_mm_set_ss(f))); + __m128 n = _mm_set_ss(f); + n = _mm_sqrt_ss(n); + return _mm_cvtss_f32(n); +} + +INLINE f32 ix_rsqrt_f32(f32 f) +{ + __m128 n = _mm_set_ss(f); + n = _mm_rsqrt_ss(n); + return _mm_cvtss_f32(n); } -#endif INLINE i32 ix_round_f32_to_i32(f32 f) { - return _mm_cvtss_si32(_mm_round_ss(_mm_setzero_ps(), _mm_set_ss(f), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC)); -} - -INLINE i64 ix_round_f64_to_i64(f64 f) -{ - return _mm_cvtsd_si64(_mm_round_sd(_mm_setzero_pd(), _mm_set_sd(f), _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC)); + __m128 n = _mm_set_ss(f); + n = _mm_round_ss(_mm_setzero_ps(), n, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC); + return _mm_cvtss_si32(n); } INLINE i32 ix_floor_f32_to_i32(f32 f) { - return _mm_cvtss_si32(_mm_floor_ss(_mm_setzero_ps(), _mm_set_ss(f))); -} - -INLINE i64 ix_floor_f64_to_i64(f64 f) -{ - return _mm_cvtsd_si64(_mm_floor_sd(_mm_setzero_pd(), _mm_set_sd(f))); + __m128 n = _mm_set_ss(f); + n = _mm_floor_ss(_mm_setzero_ps(), n); + return _mm_cvtss_si32(n); } INLINE i32 ix_ceil_f32_to_i32(f32 f) { - return _mm_cvtss_si32(_mm_ceil_ss(_mm_setzero_ps(), _mm_set_ss(f))); -} - -INLINE i64 ix_ceil_f64_to_i64(f64 f) -{ - return _mm_cvtsd_si64(_mm_ceil_sd(_mm_setzero_pd(), _mm_set_sd(f))); + __m128 n = _mm_set_ss(f); + n = _mm_ceil_ss(_mm_setzero_ps(), n); + return _mm_cvtss_si32(n); } /* ========================== * diff --git a/src/math.h b/src/math.h index 252ec2a3..69c42b5c 100644 --- a/src/math.h +++ b/src/math.h @@ -12,67 +12,39 @@ /* TODO: Don't use intrinsics for these. */ -INLINE i32 math_round_f32(f32 f) +INLINE i32 math_round(f32 f) { return ix_round_f32_to_i32(f); } -INLINE i32 math_floor_f32(f32 f) +INLINE i32 math_floor(f32 f) { return ix_floor_f32_to_i32(f); } -INLINE i32 math_ceil_f32(f32 f) +INLINE i32 math_ceil(f32 f) { return ix_ceil_f32_to_i32(f); } -INLINE i64 math_round_f64(f64 f) -{ - return ix_round_f64_to_i64(f); -} - -INLINE i64 math_floor_f64(f64 f) -{ - return ix_floor_f64_to_i64(f); -} - -INLINE i64 math_ceil_f64(f64 f) -{ - return ix_ceil_f64_to_i64(f); -} - -INLINE f32 math_mod_f32(f32 x, f32 m) +INLINE f32 math_fmod(f32 x, f32 m) { return x - m * (i32)(x / m); } -INLINE f32 math_abs_f32(f32 f) +INLINE f32 math_fabs(f32 f) { u32 truncated = *(u32 *)&f & 0x7FFFFFFF; return *(f32 *)&truncated; } -INLINE f64 math_abs_f64(f64 f) -{ - u64 truncated = *(u64 *)&f & 0x7FFFFFFFFFFFFFFF; - return *(f64 *)&truncated; -} - -INLINE i32 math_sign_f32(f32 f) +INLINE i32 math_fsign(f32 f) { u32 bits = *(u32 *)&f; i32 sign_bit = bits & ((u32)1 << 31); return 1 + (sign_bit * -2); } -INLINE i32 math_sign_f64(f64 f) -{ - u64 bits = *(u64 *)&f; - i32 sign_bit = bits & ((u64)1 << 31); - return 1 + (sign_bit * -2); -} - /* ========================== * * Exponential * ========================== */ @@ -159,10 +131,16 @@ INLINE u64 math_pow_u64(u64 base, u8 exp) { } } + +INLINE f32 math_rsqrt(f32 x) +{ + return ix_rsqrt_f32(x); +} + /* From Quake III - * https://github.com/id-Software/Quake-III-Arena/blob/dbe4ddb10315479fc00086f08e25d968b4b43c49/code/game/q_math.c#L552 */ -INLINE f32 math_rsqrt(f32 x) +INLINE f32 math_rsqrt_fast(f32 x) { const f32 three_halfs = 1.5f; f32 x2 = x * 0.5f; @@ -176,27 +154,7 @@ INLINE f32 math_rsqrt(f32 x) INLINE f32 math_sqrt(f32 x) { - return x * math_rsqrt(x); -} - -/* ========================== * - * Lerp - * ========================== */ - -INLINE f32 math_lerp_f32(f32 val0, f32 val1, f32 t) -{ - return val0 + ((val1 - val0) * t); -} - -INLINE f64 math_lerp_f64(f64 val0, f64 val1, f64 t) -{ - return val0 + ((val1 - val0) * t); -} - -INLINE f32 math_lerp_angle(f32 a, f32 b, f32 t) { - f32 diff = math_mod_f32(b - a, TAU); - diff = math_mod_f32(2.0f * diff, TAU) - diff; - return a + diff * t; + return ix_sqrt_f32(x); } /* ========================== * @@ -215,10 +173,10 @@ INLINE f32 math_lerp_angle(f32 a, f32 b, f32 t) { INLINE f32 math_sin(f32 x) { const f32 c = 0.225; - x -= (TAU * (f32)math_floor_f32(x / TAU)); /* [0, TAU] */ + x -= (TAU * (f32)math_floor(x / TAU)); /* [0, TAU] */ x += (TAU * (x < -PI)) - (TAU * (x > PI)); /* [-PI, PI] */ - f32 y = (4.0f/PI) * x + (-4.0f/(PI*PI)) * x * math_abs_f32(x); - y = c * (y * math_abs_f32(y) - y) + y; + f32 y = (4.0f/PI) * x + (-4.0f/(PI*PI)) * x * math_fabs(x); + y = c * (y * math_fabs(y) - y) + y; return y; } @@ -237,7 +195,7 @@ INLINE f32 math_atan2(f32 y, f32 x) { const f32 a11 = -0.01172120f; /* Ensure input is in [-1, +1] */ - b32 swap = math_abs_f32(x) < math_abs_f32(y); + b32 swap = math_fabs(x) < math_fabs(y); f32 s = (swap ? x : y) / (swap ? y : x); /* Approximate atan */ @@ -265,6 +223,26 @@ INLINE f32 math_acos(f32 x) return math_atan2(x, math_sqrt(1.0f - (x*x))); } +/* ========================== * + * Lerp + * ========================== */ + +INLINE f32 math_lerp_f32(f32 val0, f32 val1, f32 t) +{ + return val0 + ((val1 - val0) * t); +} + +INLINE f64 math_lerp_f64(f64 val0, f64 val1, f64 t) +{ + return val0 + ((val1 - val0) * t); +} + +INLINE f32 math_lerp_angle(f32 a, f32 b, f32 t) { + f32 diff = math_fmod(b - a, TAU); + diff = math_fmod(2.0f * diff, TAU) - diff; + return a + diff * t; +} + /* ========================== * * V2 * ========================== */ @@ -322,8 +300,19 @@ INLINE struct v2 v2_perp(struct v2 a) INLINE struct v2 v2_norm(struct v2 a) { - f32 len_squared = v2_len_squared(a); - f32 r_sqrt = math_rsqrt(len_squared); + f32 l = v2_len_squared(a); + if (l != 0) { + l = math_sqrt(l); + a.x /= l; + a.y /= l; + } + return a; +} + +INLINE struct v2 v2_norm_fast(struct v2 a) +{ + f32 l = v2_len_squared(a); + f32 r_sqrt = math_rsqrt_fast(l); a.x *= r_sqrt; a.y *= r_sqrt; return a; @@ -332,8 +321,8 @@ INLINE struct v2 v2_norm(struct v2 a) INLINE struct v2 v2_round(struct v2 a) { return V2( - (f32)math_round_f32(a.x), - (f32)math_round_f32(a.y) + (f32)math_round(a.x), + (f32)math_round(a.y) ); } @@ -398,7 +387,7 @@ INLINE struct mat4x4 mat4x4_from_xform(struct xform xf) {xf.bx.x, xf.bx.y, 0, 0}, {xf.by.x, xf.by.y, 0, 0}, {0, 0, 1, 0}, - {xf.tl.x, xf.tl.y, 0, 1}, + {xf.og.x, xf.og.y, 0, 1}, } }; } @@ -470,7 +459,10 @@ INLINE struct xform xform_rotate(struct xform xf, f32 angle); INLINE struct xform xform_scale(struct xform xf, struct v2 v); INLINE struct xform xform_invert(struct xform xf); INLINE struct v2 xform_basis_mul_v2(struct xform xf, struct v2 v); +INLINE struct v2 xform_mul_v2(struct xform xf, struct v2 v); +INLINE struct xform xform_with_scale(struct xform xf, struct v2 s); INLINE f32 xform_get_determinant(struct xform xf); +INLINE struct v2 xform_get_scale(struct xform xf); INLINE struct trs trs_from_xform(struct xform m); INLINE struct trs trs_lerp(struct trs a, struct trs b, f32 t); @@ -479,7 +471,7 @@ INLINE struct xform xform_from_pos(struct v2 v) return (struct xform) { .bx = {1, 0}, .by = {0, 1}, - .tl = {v.x, v.y} + .og = {v.x, v.y} }; } @@ -493,10 +485,11 @@ INLINE struct xform xform_from_trs(struct trs trs) INLINE struct xform xform_translate(struct xform xf, struct v2 v) { - xf.tl = V2( - xf.bx.x * v.x + xf.by.x * v.y + xf.tl.x, - xf.bx.y * v.x + xf.by.y * v.y + xf.tl.y + xf.og = V2( + xf.bx.x * v.x + xf.by.x * v.y + xf.og.x, + xf.bx.y * v.x + xf.by.y * v.y + xf.og.y ); + return xf; } @@ -517,11 +510,28 @@ INLINE struct xform xform_rotate(struct xform xf, f32 angle) INLINE struct xform xform_scale(struct xform xf, struct v2 v) { - xf.bx = v2_mul(xf.bx, v.y); + xf.bx = v2_mul(xf.bx, v.x); xf.by = v2_mul(xf.by, v.y); return xf; } +INLINE struct xform xform_with_rotation(struct xform xf, f32 r) +{ + struct v2 scale = xform_get_scale(xf); + f32 c = math_cos(r); + f32 s = math_sin(r); + xf.bx = V2(c * scale.x, s * scale.x); + xf.by = V2(-s * scale.y, c * scale.y); + return xf; +} + +INLINE struct xform xform_with_scale(struct xform xf, struct v2 s) +{ + xf.bx = v2_mul(v2_norm(xf.bx), s.x); + xf.by = v2_mul(v2_norm(xf.by), s.y); + return xf; +} + INLINE struct xform xform_trs(struct xform xf, struct trs trs) { xf = xform_translate(xf, trs.t); @@ -570,11 +580,24 @@ INLINE struct xform xform_invert(struct xform xf) xf.bx = v2_mul_v2(xf.bx, V2(inv_det, -inv_det)); xf.by = v2_mul_v2(xf.by, V2(-inv_det, inv_det)); - xf.tl = xform_basis_mul_v2(xf, v2_neg(xf.tl)); + xf.og = xform_basis_mul_v2(xf, v2_neg(xf.og)); return xf; } +INLINE struct xform xform_mul(struct xform a, struct xform b) +{ + struct xform res; + + res.bx.x = a.bx.x * b.bx.x + a.by.x * b.bx.y; + res.bx.y = a.bx.y * b.bx.x + a.by.y * b.bx.y; + res.by.x = a.bx.x * b.by.x + a.by.x * b.by.y; + res.by.y = a.bx.y * b.by.x + a.by.y * b.by.y; + res.og = xform_mul_v2(a, b.og); + + return res; +} + INLINE struct v2 xform_basis_mul_v2(struct xform xf, struct v2 v) { return V2( @@ -586,15 +609,15 @@ INLINE struct v2 xform_basis_mul_v2(struct xform xf, struct v2 v) INLINE struct v2 xform_mul_v2(struct xform xf, struct v2 v) { struct v2 res = xform_basis_mul_v2(xf, v); - res = v2_add(res, xf.tl); + res = v2_add(res, xf.og); return res; } -INLINE struct v2 xform_mul_v2_invert(struct xform xf, struct v2 v) +INLINE struct v2 xform_invert_mul_v2(struct xform xf, struct v2 v) { struct xform inv = xform_invert(xf); struct v2 res = xform_basis_mul_v2(inv, v); - res = v2_add(res, inv.tl); + res = v2_add(res, inv.og); return res; } @@ -606,7 +629,7 @@ INLINE f32 xform_get_determinant(struct xform xf) INLINE f32 xform_get_skew(struct xform xf) { f32 det = xform_get_determinant(xf); - i32 det_sign = math_sign_f32(det); + i32 det_sign = math_fsign(det); struct v2 bx_norm = v2_norm(xf.bx); struct v2 by_norm = v2_norm(xf.by); @@ -637,19 +660,14 @@ INLINE struct v2 xform_get_down(struct xform xf) return xf.by; } -INLINE struct v2 xform_get_pos(struct xform xf) -{ - return xf.tl; -} - -INLINE f32 xform_get_rot(struct xform xf) +INLINE f32 xform_get_rotation(struct xform xf) { return v2_angle(xf.bx); } INLINE struct v2 xform_get_scale(struct xform xf) { - f32 det_sign = math_sign_f32(xform_get_determinant(xf)); + f32 det_sign = math_fsign(xform_get_determinant(xf)); return V2(v2_len(xf.bx), det_sign * v2_len(xf.by)); } @@ -669,8 +687,8 @@ INLINE struct trs trs_lerp(struct trs a, struct trs b, f32 t) INLINE struct trs trs_from_xform(struct xform xf) { struct trs trs = { 0 }; - trs.t = xform_get_pos(xf); - trs.r = xform_get_rot(xf); + trs.t = xf.og; + trs.r = xform_get_rotation(xf); trs.s = xform_get_scale(xf); return trs; } diff --git a/src/mixer.c b/src/mixer.c index 2fef44cb..59940c92 100644 --- a/src/mixer.c +++ b/src/mixer.c @@ -314,12 +314,12 @@ struct mixed_pcm_f32 mixer_update(struct arena *arena, u64 frame_count) if (source_is_stereo) { source_samples_count = frame_count * 2; /* Round to nearest frame boundary (nearest multiple of 2) */ - source_samples_count = (u64)math_ceil_f32((f32)source_samples_count * speed); + source_samples_count = (u64)math_ceil((f32)source_samples_count * speed); source_samples_count &= ~1; } else { source_samples_count = frame_count; /* Round to nearest sample */ - source_samples_count = (u64)math_round_f32((f32)source_samples_count * speed); + source_samples_count = (u64)math_round((f32)source_samples_count * speed); } u64 source_sample_pos_start = mix->source_pos; @@ -359,8 +359,8 @@ struct mixed_pcm_f32 mixer_update(struct arena *arena, u64 frame_count) /* 16 bit Stereo -> 32 bit Stereo */ for (u64 out_frame_pos = 0; out_frame_pos < out_frames_count; ++out_frame_pos) { f32 in_frame_pos_exact = source_frame_pos_start + (((f32)out_frame_pos / (f32)out_frames_count) * (f32)source_frames_count); - u32 in_frame_pos_prev = math_floor_f32(in_frame_pos_exact); - u32 in_frame_pos_next = math_ceil_f32(in_frame_pos_exact); + u32 in_frame_pos_prev = math_floor(in_frame_pos_exact); + u32 in_frame_pos_next = math_ceil(in_frame_pos_exact); /* Sample source */ f32 sample1_prev = sample_sound(source, (in_frame_pos_prev * 2) + 0, desc.looping) * (1.f / 32768.f); @@ -380,8 +380,8 @@ struct mixed_pcm_f32 mixer_update(struct arena *arena, u64 frame_count) /* 16 bit Mono -> 32 bit Stereo */ for (u64 out_frame_pos = 0; out_frame_pos < out_frames_count; ++out_frame_pos) { f32 in_frame_pos_exact = source_frame_pos_start + (((f32)out_frame_pos / (f32)out_frames_count) * (f32)source_frames_count); - u32 in_frame_pos_prev = math_floor_f32(in_frame_pos_exact); - u32 in_frame_pos_next = math_ceil_f32(in_frame_pos_exact); + u32 in_frame_pos_prev = math_floor(in_frame_pos_exact); + u32 in_frame_pos_next = math_ceil(in_frame_pos_exact); /* Sample source */ f32 sample_prev = sample_sound(source, in_frame_pos_prev, desc.looping) * (1.f / 32768.f); diff --git a/src/settings.c b/src/settings.c index 82954fd0..20968bd0 100644 --- a/src/settings.c +++ b/src/settings.c @@ -102,10 +102,10 @@ struct sys_window_settings settings_default_window_settings(struct sys_window *w struct v2 window_size = sys_window_get_size(window); struct v2 monitor_size = sys_window_get_monitor_size(window); - i32 window_width = math_round_f32(window_size.x); - i32 window_height = math_round_f32(window_size.y); - i32 monitor_width = math_round_f32(monitor_size.x); - i32 monitor_height = math_round_f32(monitor_size.y); + i32 window_width = math_round(window_size.x); + i32 window_height = math_round(window_size.y); + i32 monitor_width = math_round(monitor_size.x); + i32 monitor_height = math_round(monitor_size.y); i32 width = window_width / 2; i32 height = window_height / 2; diff --git a/src/sys_win32.c b/src/sys_win32.c index 2e96ab1a..d2ce57a6 100644 --- a/src/sys_win32.c +++ b/src/sys_win32.c @@ -1200,7 +1200,7 @@ void sys_condition_variable_wait_time(struct sys_condition_variable *cv, struct #if RTC atomic_inc_eval64(&cv->num_sleepers); #endif - u32 ms = (u32)math_round_f64(seconds * 1000.0); + u32 ms = (u32)math_round((f32)seconds * 1000.f); SleepConditionVariableSRW((PCONDITION_VARIABLE)cv->handle, (SRWLOCK *)&mutex->handle, ms, 0); #if RTC atomic_dec_eval64(&cv->num_sleepers); diff --git a/src/tick.h b/src/tick.h index 6651aec2..1e479434 100644 --- a/src/tick.h +++ b/src/tick.h @@ -10,7 +10,7 @@ struct tick { f64 dt; f64 time; - struct v2 player_move; /* Player movement direction */ + struct v2 player_move_dir; /* Player movement direction */ struct v2 player_focus; /* Mouse cursor pos in world coordinates */ u64 entities_count; /* Includes 'released' & non-active entities */ diff --git a/src/user.c b/src/user.c index bc5ce29c..28d34680 100644 --- a/src/user.c +++ b/src/user.c @@ -235,7 +235,7 @@ INTERNAL struct v2 view_xform_point(struct view view, struct v2 p) INTERNAL struct v2 view_inverse_xform_point(struct view view, struct v2 p) { - return xform_mul_v2_invert(view_get_xform(view), p); + return xform_invert_mul_v2(view_get_xform(view), p); } INTERNAL struct v2 view_mouse_pos(struct view view) @@ -248,19 +248,19 @@ INTERNAL struct v2 view_mouse_pos(struct view view) * ========================== */ /* TODO: remove this (testing) */ -INTERNAL void debug_draw_xform(struct xform mtx) +INTERNAL void debug_draw_xform(struct xform xf) { f32 thickness = 2.f / PIXELS_PER_UNIT / L.world_view.zoom; u32 color = RGBA_F(0, 1, 1, 0.3); u32 color_x = RGBA_F(1, 0, 0, 0.3); u32 color_y = RGBA_F(0, 1, 0, 0.3); - struct v2 pos = xform_get_pos(mtx); - struct v2 x_ray = xform_get_right(mtx); - struct v2 y_ray = xform_get_up(mtx); + struct v2 pos = xf.og; + struct v2 x_ray = xform_get_right(xf); + struct v2 y_ray = xform_get_up(xf); struct quad quad = quad_from_rect(RECT(0, 0, 1, -1)); - quad = quad_mul_xform(quad_scale(quad, 0.075), mtx); + quad = quad_mul_xform(quad_scale(quad, 0.075), xf); draw_solid_ray(L.world_canvas, pos, x_ray, thickness, color_x); draw_solid_ray(L.world_canvas, pos, y_ray, thickness, color_y); @@ -275,7 +275,7 @@ INTERNAL void debug_draw_movement(struct entity *ent) u32 color_vel = RGBA_F(1, 0.5, 0, 1); u32 color_acc = RGBA_F(1, 1, 0.5, 1); - struct v2 pos = xform_get_pos(ent->world_xform); + struct v2 pos = ent->world_xform.og; struct v2 vel_ray = ent->velocity; struct v2 acc_ray = ent->acceleration; @@ -298,10 +298,10 @@ INTERNAL void user_update(void) /* Get screen dimensions */ L.screen_size = sys_window_get_size(L.window); /* Make screen_size an even number */ - L.screen_size = V2(math_round_f32(L.screen_size.x), math_round_f32(L.screen_size.y)); + L.screen_size = V2(math_round(L.screen_size.x), math_round(L.screen_size.y)); L.screen_size = V2( - math_round_f32(L.screen_size.x) % 2 == 0 ? L.screen_size.x : L.screen_size.x + 1, - math_round_f32(L.screen_size.y) % 2 == 0 ? L.screen_size.y : L.screen_size.y + 1 + math_round(L.screen_size.x) % 2 == 0 ? L.screen_size.x : L.screen_size.x + 1, + math_round(L.screen_size.y) % 2 == 0 ? L.screen_size.y : L.screen_size.y + 1 ); L.screen_center = v2_mul(L.screen_size, 0.5); @@ -434,6 +434,8 @@ INTERNAL void user_update(void) if (L.debug_camera) { + //L.world_view.rot += 1 * (f32)L.dt; + /* Pan view */ if (L.debug_camera_panning) { struct v2 offset = v2_sub(L.debug_camera_panning_from, view_mouse_pos(L.world_view)); @@ -501,7 +503,7 @@ INTERNAL void user_update(void) struct entity *e0 = &t0->entities[i]; struct entity *e1 = &t1->entities[i]; if (e0->handle.gen == e1->handle.gen && e0->continuity_gen == e1->continuity_gen) { - e->rel_trs = trs_lerp(e0->rel_trs, e1->rel_trs, tick_blend); + e->rel_xform = xform_lerp(e0->rel_xform, e1->rel_xform, tick_blend); e->world_xform = xform_lerp(e0->world_xform, e1->world_xform, tick_blend); e->acceleration = v2_lerp(e0->acceleration, e1->acceleration, tick_blend); @@ -511,7 +513,6 @@ INTERNAL void user_update(void) e->sprite_trs = trs_lerp(e0->sprite_trs, e1->sprite_trs, tick_blend); e->sprite_pivot_norm = v2_lerp(e0->sprite_pivot_norm, e1->sprite_pivot_norm, tick_blend); - e->camera_rot = math_lerp_angle(e0->camera_rot, e1->camera_rot, tick_blend); e->camera_zoom = math_lerp_f32(e0->camera_zoom, e1->camera_zoom, tick_blend); } } @@ -530,8 +531,8 @@ INTERNAL void user_update(void) if (!ent->valid) continue; if (entity_has_prop(ent, ENTITY_PROP_CAMERA) && ent->camera_active && !L.debug_camera) { - struct v2 center = xform_get_pos(ent->world_xform); - f32 rot = ent->camera_rot; + struct v2 center = ent->world_xform.og; + f32 rot = xform_get_rotation(ent->world_xform); f32 zoom = ent->camera_zoom; zoom = zoom > 0 ? zoom : 1; L.world_view.center = center; @@ -663,10 +664,10 @@ INTERNAL void user_update(void) { u32 color = RGBA_F(1, 0, 0, 1); - struct xform mtx_pre_pivot = ent->world_xform; - mtx_pre_pivot = xform_trs(mtx_pre_pivot, ent->sprite_trs); + struct xform pre_pivot_xf = xform_trs(ent->world_xform, ent->sprite_trs); + struct v2 p = pre_pivot_xf.og; - draw_solid_circle(L.world_canvas, xform_get_pos(mtx_pre_pivot), 0.02, color, 20); + draw_solid_circle(L.world_canvas, p, 0.02, color, 20); } } } @@ -675,13 +676,13 @@ INTERNAL void user_update(void) if (L.debug_draw && !is_camera) { struct font *disp_font = font_load_async(STR("res/fonts/fixedsys.ttf"), 12.0f); if (disp_font) { - struct xform mtx = ent->world_xform; - struct trs trs = trs_from_xform(mtx); + struct xform xf = ent->world_xform; + struct trs trs = trs_from_xform(xf); struct v2 velocity = ent->velocity; struct v2 acceleration = ent->acceleration; f32 offset = 1; - struct v2 pos = v2_add(xform_get_pos(mtx), v2_mul(V2(0, -1), offset)); + struct v2 pos = v2_add(xf.og, v2_mul(V2(0, -1), offset)); pos = view_xform_point(L.world_view, pos); pos = v2_round(pos);