wasm f32_pow, f64_pow, lots of changes to math, ui animations start

This commit is contained in:
Krzosa Karol
2025-01-22 17:59:30 +01:00
parent b20a507834
commit de35c4a705
19 changed files with 775 additions and 544 deletions

View File

@@ -82,7 +82,7 @@ int main(int argc, char **argv) {
ok = os_systemf( ok = os_systemf(
"clang.exe ../src/wasm_app/main.c -o main.wasm" "clang.exe ../src/wasm_app/main.c -o main.wasm"
" -Oz -g -I../src" " -Oz -g -I../src"
" -Wall -Wno-missing-braces -Wno-single-bit-bitfield-constant-conversion" " -Wall -Wno-missing-braces -Wno-single-bit-bitfield-constant-conversion -Wno-unsequenced"
" -fdiagnostics-absolute-paths -fdiagnostics-format=msvc" " -fdiagnostics-absolute-paths -fdiagnostics-format=msvc"
" --target=wasm32 -nostdlib -mbulk-memory -msimd128" " --target=wasm32 -nostdlib -mbulk-memory -msimd128"
" -Wl,-export-dynamic,--allow-undefined,--import-memory,--no-entry,--initial-memory=131072000,--max-memory=4294967296" " -Wl,-export-dynamic,--allow-undefined,--import-memory,--no-entry,--initial-memory=131072000,--max-memory=4294967296"

View File

@@ -12,7 +12,7 @@
<canvas class="fill-screen" id="canvas"></canvas> <canvas class="fill-screen" id="canvas"></canvas>
</body> </body>
<style> <style>
@font-face { @font-face {
font-family: main_font; font-family: main_font;
src: url(FiraCode-Regular.ttf); src: url(FiraCode-Regular.ttf);
} }

View File

@@ -130,7 +130,7 @@ fn_wasm_export b32 wasm_update(f64 time, f32 width, f32 height, f32 dpr) {
wasm_frame.window_size = (v2f32_t){width, height}; wasm_frame.window_size = (v2f32_t){width, height};
wasm_frame.dpr = wasm_dpr; wasm_frame.dpr = wasm_dpr;
wasm_frame.mouse_pos = wasm_frame.last_event->mouse_pos; wasm_frame.mouse_pos = wasm_frame.last_event->mouse_pos;
wasm_frame.delta = wasm_delta_time; wasm_frame.delta = wasm_delta_time / 1000.0;
wasm_frame.frame = wasm_frame_counter; wasm_frame.frame = wasm_frame_counter;
b32 animating = app_update(&wasm_frame); b32 animating = app_update(&wasm_frame);

View File

@@ -12,7 +12,7 @@
<canvas class="fill-screen" id="canvas"></canvas> <canvas class="fill-screen" id="canvas"></canvas>
</body> </body>
<style> <style>
@font-face { @font-face {
font-family: main_font; font-family: main_font;
src: url(FiraCode-Regular.ttf); src: url(FiraCode-Regular.ttf);
} }

View File

@@ -35,10 +35,25 @@ typedef double f64;
#define fn_wasm_import #define fn_wasm_import
#endif #endif
#define U64_TO_F64(x) (((union { f64 f; u64 i; }) { .i = (x) }).f) typedef union convert_f64_u64_t convert_f64_u64_t;
#define U32_TO_F32(x) (((union { f32 f; u32 i; }) { .i = (x) }).f) union convert_f64_u64_t { f64 f; u64 i; };
#define F64_TO_U64(x) (((union { f64 f; u64 i; }) { .f = (x) }).i) typedef union convert_f64_i64_t convert_f64_i64_t;
#define F32_TO_U32(x) (((union { f32 f; u32 i; }) { .f = (x) }).i) union convert_f64_i64_t { f64 f; i64 i; };
typedef union convert_f32_u32_t convert_f32_u32_t;
union convert_f32_u32_t { f32 f; u32 i; };
typedef union convert_f32_i32_t convert_f32_i32_t;
union convert_f32_i32_t { f32 f; i32 i; };
#define U64_TO_F64(x) (((convert_f64_u64_t) { .i = (x) }).f)
#define U32_TO_F32(x) (((convert_f32_u32_t) { .i = (x) }).f)
#define F64_TO_U64(x) (((convert_f64_u64_t) { .f = (x) }).i)
#define F32_TO_U32(x) (((convert_f32_u32_t) { .f = (x) }).i)
#define I64_TO_F64(x) (((convert_f64_i64_t) { .i = (x) }).f)
#define I32_TO_F32(x) (((convert_f32_i32_t) { .i = (x) }).f)
#define F64_TO_I64(x) (((convert_f64_i64_t) { .f = (x) }).i)
#define F32_TO_I32(x) (((convert_f32_i32_t) { .f = (x) }).i)
#define MIN(x,y) ((x) > (y) ? (y) : (x)) #define MIN(x,y) ((x) > (y) ? (y) : (x))
#define MAX(x,y) ((x) > (y) ? (x) : (y)) #define MAX(x,y) ((x) > (y) ? (x) : (y))

View File

@@ -28,19 +28,142 @@ fn void memory_zero(void *dst, usize size) {
memory_set(dst, 0, size); memory_set(dst, 0, size);
} }
f32 f32_sqrt(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_sqrtf(x), sqrtf(x)); } fn_inline f32 f32_sqrt(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_sqrtf(x), sqrtf(x)); }
f64 f64_sqrt(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_sqrt(x), sqrt(x)); } fn_inline f64 f64_sqrt(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_sqrt(x), sqrt(x)); }
f32 f32_ceil(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_ceilf(x), ceilf(x)); } fn_inline f32 f32_ceil(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_ceilf(x), ceilf(x)); }
f64 f64_ceil(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_ceil(x), ceil(x)); } fn_inline f64 f64_ceil(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_ceil(x), ceil(x)); }
f32 f32_floor(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_floorf(x), floorf(x)); } fn_inline f32 f32_floor(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_floorf(x), floorf(x)); }
f64 f64_floor(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_floor(x), floor(x)); } fn_inline f64 f64_floor(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_floor(x), floor(x)); }
f32 f32_abs(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_fabsf(x), fabsf(x)); } fn_inline f32 f32_abs(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_fabsf(x), fabsf(x)); }
f64 f64_abs(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_fabs(x), fabs(x)); } fn_inline f64 f64_abs(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_fabs(x), fabs(x)); }
f32 f32_round(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_roundf(x), roundf(x)); } fn_inline f32 f32_round(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_roundf(x), roundf(x)); }
f64 f64_round(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_round(x), round(x)); } fn_inline f64 f64_round(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_round(x), round(x)); }
f64 f64_mod(f64 a, f64 b) { return IF_PLATFORM_CLANG_ELSE(__builtin_fmod(a, b), fmod(a, b)); } fn_inline f64 f64_mod(f64 a, f64 b) { return IF_PLATFORM_CLANG_ELSE(__builtin_fmod(a, b), fmod(a, b)); }
f32 f32_mod(f32 a, f32 b) { return IF_PLATFORM_CLANG_ELSE(__builtin_fmodf(a, b), fmodf(a, b)); } fn_inline f32 f32_mod(f32 a, f32 b) { return IF_PLATFORM_CLANG_ELSE(__builtin_fmodf(a, b), fmodf(a, b)); }
f32 f32_pow(f32 a, f32 b) { return IF_PLATFORM_CLANG_ELSE(__builtin_powf(a, b), powf(a, b)); }
f64 f64_pow(f64 a, f64 b) { return IF_PLATFORM_CLANG_ELSE(__builtin_pow(a, b), pow(a, b)); }
/*
https://gitlab.com/nakst/essence/-/blob/master/shared/math.cpp
*/
fn f64 f64_exp2(f64 x) {
f64 a = f64_floor(x * 8);
int64_t ai = (i64)a;
if (ai < -1024) {
return 0;
}
f64 b = x - a / 8;
f64 y = U64_TO_F64(0x3FF0000000000000) +
b * (U64_TO_F64(0x3FE62E42FEFA3A00) +
b * (U64_TO_F64(0x3FCEBFBDFF829140) +
b * (U64_TO_F64(0x3FAC6B08D73C4A40) +
b * (U64_TO_F64(0x3F83B2AB53873280) +
b * (U64_TO_F64(0x3F55D88F363C6C00) +
b * (U64_TO_F64(0x3F242C003E4A2000) +
b * U64_TO_F64(0x3EF0B291F6C00000)))))));
const f64 m[8] = {
U64_TO_F64(0x3FF0000000000000),
U64_TO_F64(0x3FF172B83C7D517B),
U64_TO_F64(0x3FF306FE0A31B715),
U64_TO_F64(0x3FF4BFDAD5362A27),
U64_TO_F64(0x3FF6A09E667F3BCD),
U64_TO_F64(0x3FF8ACE5422AA0DB),
U64_TO_F64(0x3FFAE89F995AD3AD),
U64_TO_F64(0x3FFD5818DCFBA487),
};
y *= m[ai & 7];
convert_f64_u64_t c;
c.f = y;
c.i += (ai >> 3) << 52;
return c.f;
}
fn f32 f32_exp2(f32 x) {
f32 a = f32_floor(x);
i32 ai = (i32)a;
if (ai < -128) {
return 0;
}
f32 b = x - a;
f32 y = U32_TO_F32(0x3F7FFFFE) + b * (U32_TO_F32(0x3F31729A) + b * (U32_TO_F32(0x3E75E700)
+ b * (U32_TO_F32(0x3D64D520) + b * (U32_TO_F32(0x3C128280) + b * U32_TO_F32(0x3AF89400)))));
convert_f32_u32_t c;
c.f = y;
c.i += ai << 23;
return c.f;
}
fn f64 f64_log2(f64 x) {
convert_f64_u64_t c;
c.f = x;
int64_t e = ((c.i >> 52) & 2047) - 0x3FF;
c.i = (c.i & ~((uint64_t) 0x7FF << 52)) + ((uint64_t) 0x3FF << 52);
x = c.f;
f64 a;
if (x < 1.125) {
a = 0;
} else if (x < 1.250) {
x *= 1.125 / 1.250;
a = U64_TO_F64(0xBFC374D65D9E608E);
} else if (x < 1.375) {
x *= 1.125 / 1.375;
a = U64_TO_F64(0xBFD28746C334FECB);
} else if (x < 1.500) {
x *= 1.125 / 1.500;
a = U64_TO_F64(0xBFDA8FF971810A5E);
} else if (x < 1.625) {
x *= 1.125 / 1.625;
a = U64_TO_F64(0xBFE0F9F9FFC8932A);
} else if (x < 1.750) {
x *= 1.125 / 1.750;
a = U64_TO_F64(0xBFE465D36ED11B11);
} else if (x < 1.875) {
x *= 1.125 / 1.875;
a = U64_TO_F64(0xBFE79538DEA712F5);
} else {
x *= 1.125 / 2.000;
a = U64_TO_F64(0xBFEA8FF971810A5E);
}
f64 y = U64_TO_F64(0xC00FF8445026AD97) + x * (U64_TO_F64(0x40287A7A02D9353F) + x * (U64_TO_F64(0xC03711C58D55CEE2)
+ x * (U64_TO_F64(0x4040E8263C321A26) + x * (U64_TO_F64(0xC041EB22EA691BB3) + x * (U64_TO_F64(0x403B00FB376D1F10)
+ x * (U64_TO_F64(0xC02C416ABE857241) + x * (U64_TO_F64(0x40138BA7FAA3523A) + x * (U64_TO_F64(0xBFF019731AF80316)
+ x * U64_TO_F64(0x3FB7F1CD3852C200)))))))));
return y - a + e;
}
fn f32 f32_log2(f32 x) {
// TODO f32_log2(0.9999971379999999f) gives 0.00000143051147460938 (should be negative!!).
convert_f32_u32_t c;
c.f = x;
i32 e = ((c.i >> 23) & 255) - 0x7F;
c.i = (c.i & ~(0xFF << 23)) + (0x7F << 23);
x = c.f;
f32 y = U32_TO_F32(0xC05B5154) + x * (U32_TO_F32(0x410297C6) + x * (U32_TO_F32(0xC1205CEB)
+ x * (U32_TO_F32(0x4114DF63) + x * (U32_TO_F32(0xC0C0DBBB) + x * (U32_TO_F32(0x402942C6)
+ x * (U32_TO_F32(0xBF3FF98A) + x * (U32_TO_F32(0x3DFE1050) + x * U32_TO_F32(0xBC151480))))))));
return y + e;
}
fn f64 f64_pow(f64 x, f64 y) {
return f64_exp2(y * f64_log2(x));
}
fn f32 f32_pow(f32 x, f32 y) {
return f32_exp2(y * f32_log2(x));
}

View File

@@ -4,7 +4,7 @@
* also https://gist.github.com/ciembor/1494530 * also https://gist.github.com/ciembor/1494530
* alpha is ignored * alpha is ignored
*/ */
v3f32_t v3f32_rgb_to_hsl(v3f32_t color) { fn v3f32_t v3f32_rgb_to_hsl(v3f32_t color) {
v3f32_t result; v3f32_t result;
f32 max = MAX(MAX(color.r, color.g), color.b); f32 max = MAX(MAX(color.r, color.g), color.b);
f32 min = MIN(MIN(color.r, color.g), color.b); f32 min = MIN(MIN(color.r, color.g), color.b);
@@ -35,7 +35,7 @@ v3f32_t v3f32_rgb_to_hsl(v3f32_t color) {
* Converts an HUE to r, g or b. * Converts an HUE to r, g or b.
* returns f32 in the set [0, 1]. * returns f32 in the set [0, 1].
*/ */
f32 f32_hue_to_rgb(f32 p, f32 q, f32 t) { fn f32 f32_hue_to_rgb(f32 p, f32 q, f32 t) {
if (t < 0.f) if (t < 0.f)
t += 1.f; t += 1.f;
if (t > 1.f) if (t > 1.f)
@@ -49,7 +49,7 @@ f32 f32_hue_to_rgb(f32 p, f32 q, f32 t) {
return p; return p;
} }
v3f32_t v3f32_hsl_to_rgb(v3f32_t color) { fn v3f32_t v3f32_hsl_to_rgb(v3f32_t color) {
v3f32_t result; v3f32_t result;
if (0 == color.s) { if (0 == color.s) {
@@ -65,12 +65,12 @@ v3f32_t v3f32_hsl_to_rgb(v3f32_t color) {
return result; return result;
} }
v4f32_t v4f32_rgba_to_hsla(v4f32_t rgba) { fn v4f32_t v4f32_rgba_to_hsla(v4f32_t rgba) {
v3f32_t result = v3f32_rgb_to_hsl(rgba.xyz); v3f32_t result = v3f32_rgb_to_hsl(rgba.xyz);
return (v4f32_t){result.r, result.g, result.b, rgba.a}; return (v4f32_t){result.r, result.g, result.b, rgba.a};
} }
v4f32_t v4f32_hsl_to_rgb(v4f32_t color) { fn v4f32_t v4f32_hsla_to_rgba(v4f32_t color) {
v3f32_t result = v3f32_hsl_to_rgb(color.xyz); v3f32_t result = v3f32_hsl_to_rgb(color.xyz);
return (v4f32_t){result.r, result.g, result.b, color.a}; return (v4f32_t){result.r, result.g, result.b, color.a};
} }
@@ -81,24 +81,29 @@ https://gitlab.com/nakst/essence/-/blob/master/shared/math.cpp
*/ */
fn f64 f64_clamp01(f64 a) { return CLAMP(a, 0, 1); }
fn f64 f64_lerp(f64 a, f64 b, f64 t) { return (1 - t) * a + t * b; }
fn f64 f64_smooth_step(f64 t) { return t*t*(3-2*t); }
fn f64 f64_ease_in_quint(f64 x) { return x * x * x * x * x; }
fn f64 f64_ease_in_cubic(f64 x) { return x * x * x; }
fn f64 f64_ping_pong(f64 x) { return 1.0 - f64_abs(1.0 - f64_mod(x,2)); }
f64 f64_lerp(f64 a, f64 b, f64 t) { return (1 - t) * a + t * b; } fn f32 f32_clamp01(f32 a) { return CLAMP(a, 0, 1); }
f64 f64_smooth_step(f64 t) { return t*t*(3-2*t); } fn f32 f32_lerp(f32 a, f32 b, f32 t) { return (1 - t) * a + t * b; }
f64 f64_ease_in_quint(f64 x) { return x * x * x * x * x; } fn f32 f32_smooth_step(f32 t) { return t*t*(3-2*t); }
f64 f64_ease_in_cubic(f64 x) { return x * x * x; } fn f32 f32_ease_in_quint(f32 x) { return x * x * x * x * x; }
f64 f64_ping_pong(f64 x) { return 1.0 - f64_abs(1.0 - f64_mod(x,2)); } fn f32 f32_ease_in_cubic(f32 x) { return x * x * x; }
fn f32 f32_ping_pong(f32 x) { return 1.0f - f32_abs(1.0f - f32_mod(x,2)); }
f32 f32_lerp(f32 a, f32 b, f32 t) { return (1 - t) * a + t * b; } fn v4f32_t v4f32_lerp(v4f32_t a, v4f32_t b, f32 t) {
f32 f32_smooth_step(f32 t) { return t*t*(3-2*t); }
f32 f32_ease_in_quint(f32 x) { return x * x * x * x * x; }
f32 f32_ease_in_cubic(f32 x) { return x * x * x; }
f32 f32_ping_pong(f32 x) { return 1.0f - f32_abs(1.0f - f32_mod(x,2)); }
v4f32_t v4f32_lerp(v4f32_t a, v4f32_t b, f32 t) {
return (v4f32_t){f32_lerp(a.x, a.y, t), f32_lerp(a.y, b.y, t), f32_lerp(a.z, b.z, t), f32_lerp(a.w, b.w, t)}; return (v4f32_t){f32_lerp(a.x, a.y, t), f32_lerp(a.y, b.y, t), f32_lerp(a.z, b.z, t), f32_lerp(a.w, b.w, t)};
} }
f64 _Sine(f64 x) { fn v4f32_t v4f32_lerp_noa(v4f32_t a, v4f32_t b, f32 t) {
return (v4f32_t){f32_lerp(a.x, a.y, t), f32_lerp(a.y, b.y, t), f32_lerp(a.z, b.z, t), a.w};
}
fn f64 _Sine(f64 x) {
// Calculates sin(x) for x in [0, pi/4]. // Calculates sin(x) for x in [0, pi/4].
f64 x2 = x * x; f64 x2 = x * x;
@@ -107,7 +112,7 @@ f64 _Sine(f64 x) {
+ x2 * (U64_TO_F64(0x3EC71DE349280000) + x2 * (U64_TO_F64(0xBE5AE5DC48000000) + x2 * U64_TO_F64(0x3DE5D68200000000))))))); + x2 * (U64_TO_F64(0x3EC71DE349280000) + x2 * (U64_TO_F64(0xBE5AE5DC48000000) + x2 * U64_TO_F64(0x3DE5D68200000000)))))));
} }
f32 _SineFloat(f32 x) { fn f32 _SineFloat(f32 x) {
// Calculates sin(x) for x in [0, pi/4]. // Calculates sin(x) for x in [0, pi/4].
f32 x2 = x * x; f32 x2 = x * x;
@@ -115,7 +120,7 @@ f32 _SineFloat(f32 x) {
return x * (U32_TO_F32(0x3F800000) + x2 * (U32_TO_F32(0xBE2AAAA0) + x2 * (U32_TO_F32(0x3C0882C0) + x2 * U32_TO_F32(0xB94C6000)))); return x * (U32_TO_F32(0x3F800000) + x2 * (U32_TO_F32(0xBE2AAAA0) + x2 * (U32_TO_F32(0x3C0882C0) + x2 * U32_TO_F32(0xB94C6000))));
} }
f64 _ArcSine(f64 x) { fn f64 _ArcSine(f64 x) {
// Calculates arcsin(x) for x in [0, 0.5]. // Calculates arcsin(x) for x in [0, 0.5].
f64 x2 = x * x; f64 x2 = x * x;
@@ -126,7 +131,7 @@ f64 _ArcSine(f64 x) {
+ x2 * (U64_TO_F64(0xBF8E849CA75B1E00) + x2 * U64_TO_F64(0x3FA146C2D37F2C60)))))))))))); + x2 * (U64_TO_F64(0xBF8E849CA75B1E00) + x2 * U64_TO_F64(0x3FA146C2D37F2C60))))))))))));
} }
f32 _ArcSineFloat(f32 x) { fn f32 _ArcSineFloat(f32 x) {
// Calculates arcsin(x) for x in [0, 0.5]. // Calculates arcsin(x) for x in [0, 0.5].
f32 x2 = x * x; f32 x2 = x * x;
@@ -134,7 +139,7 @@ f32 _ArcSineFloat(f32 x) {
return x * (U32_TO_F32(0x3F800004) + x2 * (U32_TO_F32(0x3E2AA130) + x2 * (U32_TO_F32(0x3D9B2C28) + x2 * (U32_TO_F32(0x3D1C1800) + x2 * U32_TO_F32(0x3D5A97C0))))); return x * (U32_TO_F32(0x3F800004) + x2 * (U32_TO_F32(0x3E2AA130) + x2 * (U32_TO_F32(0x3D9B2C28) + x2 * (U32_TO_F32(0x3D1C1800) + x2 * U32_TO_F32(0x3D5A97C0)))));
} }
f64 _ArcTangent(f64 x) { fn f64 _ArcTangent(f64 x) {
// Calculates arctan(x) for x in [0, 0.5]. // Calculates arctan(x) for x in [0, 0.5].
f64 x2 = x * x; f64 x2 = x * x;
@@ -145,7 +150,7 @@ f64 _ArcTangent(f64 x) {
+ x2 * (U64_TO_F64(0x3F9FC6B5E115F2C0) + x2 * U64_TO_F64(0xBF87DCA5AB25BF80)))))))))))); + x2 * (U64_TO_F64(0x3F9FC6B5E115F2C0) + x2 * U64_TO_F64(0xBF87DCA5AB25BF80))))))))))));
} }
f32 _ArcTangentFloat(f32 x) { fn f32 _ArcTangentFloat(f32 x) {
// Calculates arctan(x) for x in [0, 0.5]. // Calculates arctan(x) for x in [0, 0.5].
f32 x2 = x * x; f32 x2 = x * x;
@@ -153,7 +158,7 @@ f32 _ArcTangentFloat(f32 x) {
return x * (U32_TO_F32(0x3F7FFFF8) + x2 * (U32_TO_F32(0xBEAAA53C) + x2 * (U32_TO_F32(0x3E4BC990) + x2 * (U32_TO_F32(0xBE084A60) + x2 * U32_TO_F32(0x3D8864B0))))); return x * (U32_TO_F32(0x3F7FFFF8) + x2 * (U32_TO_F32(0xBEAAA53C) + x2 * (U32_TO_F32(0x3E4BC990) + x2 * (U32_TO_F32(0xBE084A60) + x2 * U32_TO_F32(0x3D8864B0)))));
} }
f64 _Cosine(f64 x) { fn f64 _Cosine(f64 x) {
// Calculates cos(x) for x in [0, pi/4]. // Calculates cos(x) for x in [0, pi/4].
f64 x2 = x * x; f64 x2 = x * x;
@@ -162,7 +167,7 @@ f64 _Cosine(f64 x) {
+ x2 * (U64_TO_F64(0x3EFA019F87490000) + x2 * (U64_TO_F64(0xBE927DF66B000000) + x2 * U64_TO_F64(0x3E21B949E0000000)))))); + x2 * (U64_TO_F64(0x3EFA019F87490000) + x2 * (U64_TO_F64(0xBE927DF66B000000) + x2 * U64_TO_F64(0x3E21B949E0000000))))));
} }
f32 _CosineFloat(f32 x) { fn f32 _CosineFloat(f32 x) {
// Calculates cos(x) for x in [0, pi/4]. // Calculates cos(x) for x in [0, pi/4].
f32 x2 = x * x; f32 x2 = x * x;
@@ -170,7 +175,7 @@ f32 _CosineFloat(f32 x) {
return U32_TO_F32(0x3F800000) + x2 * (U32_TO_F32(0xBEFFFFDA) + x2 * (U32_TO_F32(0x3D2A9F60) + x2 * U32_TO_F32(0xBAB22C00))); return U32_TO_F32(0x3F800000) + x2 * (U32_TO_F32(0xBEFFFFDA) + x2 * (U32_TO_F32(0x3D2A9F60) + x2 * U32_TO_F32(0xBAB22C00)));
} }
f64 _Tangent(f64 x) { fn f64 _Tangent(f64 x) {
// Calculates tan(x) for x in [0, pi/4]. // Calculates tan(x) for x in [0, pi/4].
f64 x2 = x * x; f64 x2 = x * x;
@@ -182,7 +187,7 @@ f64 _Tangent(f64 x) {
+ x2 * (U64_TO_F64(0x3F0980BDF11E8000))))))))))))))); + x2 * (U64_TO_F64(0x3F0980BDF11E8000)))))))))))))));
} }
f32 _TangentFloat(f32 x) { fn f32 _TangentFloat(f32 x) {
// Calculates tan(x) for x in [0, pi/4]. // Calculates tan(x) for x in [0, pi/4].
f32 x2 = x * x; f32 x2 = x * x;
@@ -191,8 +196,7 @@ f32 _TangentFloat(f32 x) {
+ x2 * (U32_TO_F32(0x3CD24840) + x2 * (U32_TO_F32(0x3AC3CA00) + x2 * U32_TO_F32(0x3C272F00))))))); + x2 * (U32_TO_F32(0x3CD24840) + x2 * (U32_TO_F32(0x3AC3CA00) + x2 * U32_TO_F32(0x3C272F00)))))));
} }
fn f64 f64_sin(f64 x) {
f64 f64_sin(f64 x) {
b32 negate = false; b32 negate = false;
// x in -infty, infty // x in -infty, infty
@@ -226,7 +230,7 @@ f64 f64_sin(f64 x) {
} }
f32 f32_sin(f32 x) { fn f32 f32_sin(f32 x) {
b32 negate = false; b32 negate = false;
// x in -infty, infty // x in -infty, infty
@@ -259,7 +263,7 @@ f32 f32_sin(f32 x) {
return negate ? -y : y; return negate ? -y : y;
} }
f64 f64_cos(f64 x) { fn f64 f64_cos(f64 x) {
b32 negate = false; b32 negate = false;
// x in -infty, infty // x in -infty, infty
@@ -291,7 +295,7 @@ f64 f64_cos(f64 x) {
return negate ? -y : y; return negate ? -y : y;
} }
f32 f32_cos(f32 x) { fn f32 f32_cos(f32 x) {
b32 negate = false; b32 negate = false;
// x in -infty, infty // x in -infty, infty
@@ -323,7 +327,7 @@ f32 f32_cos(f32 x) {
return negate ? -y : y; return negate ? -y : y;
} }
f64 f64_tan(f64 x) { fn f64 f64_tan(f64 x) {
b32 negate = false; b32 negate = false;
// x in -infty, infty // x in -infty, infty
@@ -350,7 +354,7 @@ f64 f64_tan(f64 x) {
return negate ? -y : y; return negate ? -y : y;
} }
f32 f32_tan(f32 x) { fn f32 f32_tan(f32 x) {
b32 negate = false; b32 negate = false;
// x in -infty, infty // x in -infty, infty
@@ -377,7 +381,7 @@ f32 f32_tan(f32 x) {
return negate ? -y : y; return negate ? -y : y;
} }
f64 f64_asin(f64 x) { fn f64 f64_asin(f64 x) {
b32 negate = false; b32 negate = false;
if (x < 0) { if (x < 0) {
@@ -396,7 +400,7 @@ f64 f64_asin(f64 x) {
return negate ? -y : y; return negate ? -y : y;
} }
f32 f32_asin(f32 x) { fn f32 f32_asin(f32 x) {
b32 negate = false; b32 negate = false;
if (x < 0) { if (x < 0) {
@@ -415,15 +419,15 @@ f32 f32_asin(f32 x) {
return negate ? -y : y; return negate ? -y : y;
} }
f64 f64_acos(f64 x) { fn f64 f64_acos(f64 x) {
return f64_asin(-x) + F64_PI / 2; return f64_asin(-x) + F64_PI / 2;
} }
f32 f32_acos(f32 x) { fn f32 f32_acos(f32 x) {
return f32_asin(-x) + F32_PI / 2; return f32_asin(-x) + F32_PI / 2;
} }
f64 f64_atan(f64 x) { fn f64 f64_atan(f64 x) {
b32 negate = false; b32 negate = false;
if (x < 0) { if (x < 0) {
@@ -453,7 +457,7 @@ f64 f64_atan(f64 x) {
return negate ? -y : y; return negate ? -y : y;
} }
f32 f32_atan(f32 x) { fn f32 f32_atan(f32 x) {
b32 negate = false; b32 negate = false;
if (x < 0) { if (x < 0) {
@@ -483,29 +487,54 @@ f32 f32_atan(f32 x) {
return negate ? -y : y; return negate ? -y : y;
} }
f64 f64_atan2(f64 y, f64 x) { fn f64 f64_atan2(f64 y, f64 x) {
if (x == 0) return y > 0 ? F64_PI / 2 : -F64_PI / 2; if (x == 0) return y > 0 ? F64_PI / 2 : -F64_PI / 2;
else if (x > 0) return f64_atan(y / x); else if (x > 0) return f64_atan(y / x);
else if (y >= 0) return F64_PI + f64_atan(y / x); else if (y >= 0) return F64_PI + f64_atan(y / x);
else return -F64_PI + f64_atan(y / x); else return -F64_PI + f64_atan(y / x);
} }
f32 f32_atan2(f32 y, f32 x) { fn f32 f32_atan2(f32 y, f32 x) {
if (x == 0) return y > 0 ? F32_PI / 2 : -F32_PI / 2; if (x == 0) return y > 0 ? F32_PI / 2 : -F32_PI / 2;
else if (x > 0) return f32_atan(y / x); else if (x > 0) return f32_atan(y / x);
else if (y >= 0) return F32_PI + f32_atan(y / x); else if (y >= 0) return F32_PI + f32_atan(y / x);
else return -F32_PI + f32_atan(y / x); else return -F32_PI + f32_atan(y / x);
} }
fn b32 f32_are_equal(f32 a, f32 b) {
b32 f32_are_equal(f32 a, f32 b) {
f32 diff = f32_abs(a - b); f32 diff = f32_abs(a - b);
b32 result = diff <= 0.00001f; b32 result = diff <= 0.00001f;
return result; return result;
} }
b32 f64_are_equal(f64 a, f64 b) { fn b32 f64_are_equal(f64 a, f64 b) {
f64 diff = f64_abs(a - b); f64 diff = f64_abs(a - b);
b32 result = diff <= 0.00001; b32 result = diff <= 0.00001;
return result; return result;
} }
fn f32 f32_ease_out_bounce(f32 x) {
f32 n1 = 7.5625f;
f32 d1 = 2.75f;
if (x < 1.f / d1) {
return n1 * x * x;
} else if (x < 2.f / d1) {
return n1 * (x -= 1.5f / d1) * x + 0.75f;
} else if (x < 2.5f / d1) {
return n1 * (x -= 2.25f / d1) * x + 0.9375f;
} else {
return n1 * (x -= 2.625f / d1) * x + 0.984375f;
}
}
fn f32 f32_ease_out_elastic(f32 x) {
f32 c4 = (2 * F32_PI) / 3;
return x == 0.f
? 0
: x == 1.f
? 1
: f32_pow(2.f, -10.f * x) * f32_sin((x * 10.f - 0.75f) * c4) + 1.f;
}

File diff suppressed because it is too large Load Diff

View File

@@ -162,10 +162,10 @@ for basic_type in basic_types:
result += ", " result += ", "
return result return result
print(f"""{type}_t {type}({members_as_func_params(members, basic_type)}) {{ return ({type}_t){{ {members_as_func_params(members, "")} }}; }}""") print(f"""fn_inline {type}_t {type}({members_as_func_params(members, basic_type)}) {{ return ({type}_t){{ {members_as_func_params(members, "")} }}; }}""")
print(f"""gb_read_only {type}_t {type}_null = {{0}};""") print(f"""gb_read_only {type}_t {type}_null = {{0}};""")
print(f"""b32 {type}_is_null({type}_t a) {{ return memory_equal(&a, &{type}_null, sizeof(a)); }}""") print(f"""fn_inline b32 {type}_is_null({type}_t a) {{ return memory_equal(&a, &{type}_null, sizeof(a)); }}""")
print(f"""b32 {type}_are_equal({type}_t a, {type}_t b) {{ return memory_equal(&a, &b, sizeof(a)); }}""") print(f"""fn_inline b32 {type}_are_equal({type}_t a, {type}_t b) {{ return memory_equal(&a, &b, sizeof(a)); }}""")
for sym in symbols: for sym in symbols:
op = sym[op_idx] op = sym[op_idx]
@@ -186,8 +186,8 @@ for basic_type in basic_types:
return result return result
print(f"{type}_t {type}_{opname}({type}_t a, {type}_t b) {{ return ({type}_t){{ {op_str(members, op)} }}; }}") print(f"fn_inline {type}_t {type}_{opname}({type}_t a, {type}_t b) {{ return ({type}_t){{ {op_str(members, op)} }}; }}")
print(f"{type}_t {type}_{opname}s({type}_t a, {basic_type} b) {{ return ({type}_t){{ {op_str(members, op, skip_right = True)} }}; }}") print(f"fn_inline {type}_t {type}_{opname}s({type}_t a, {basic_type} b) {{ return ({type}_t){{ {op_str(members, op, skip_right = True)} }}; }}")
for basic_type in basic_types: for basic_type in basic_types:
for i in range(len(rect_types)): for i in range(len(rect_types)):
@@ -199,120 +199,120 @@ for basic_type in basic_types:
rect_name = rect_type[name_idx] + basic_type rect_name = rect_type[name_idx] + basic_type
rect_members = rect_type[member_idx] rect_members = rect_type[member_idx]
print(f"{rect_name}_t {rect_name}_mindim({vec_name}_t pos, {vec_name}_t size) {{ return ({rect_name}_t){{ pos, {vec_name}_add(pos, size) }}; }}") print(f"fn_inline {rect_name}_t {rect_name}_mindim({vec_name}_t pos, {vec_name}_t size) {{ return ({rect_name}_t){{ pos, {vec_name}_add(pos, size) }}; }}")
print(f"{rect_name}_t {rect_name}_center_halfdim({vec_name}_t center, {vec_name}_t halfdim) {{ return ({rect_name}_t){{ {vec_name}_sub(center, halfdim), {vec_name}_add(center, halfdim) }}; }}") print(f"fn_inline {rect_name}_t {rect_name}_center_halfdim({vec_name}_t center, {vec_name}_t halfdim) {{ return ({rect_name}_t){{ {vec_name}_sub(center, halfdim), {vec_name}_add(center, halfdim) }}; }}")
for basic_type in basic_types: for basic_type in basic_types:
s = """ s = """
r2f64_t r2f64_cut_left(r2f64_t *r, f64 value) { fn r2f64_t r2f64_cut_left(r2f64_t *r, f64 value) {
f64 minx = r->min.x; f64 minx = r->min.x;
r->min.x = MIN(r->min.x + value, r->max.x); r->min.x = MIN(r->min.x + value, r->max.x);
return (r2f64_t){ .min = {.x = minx, .y = r->min.y}, .max = {.x = r->min.x, .y =r->max.y} }; return (r2f64_t){ .min = {.x = minx, .y = r->min.y}, .max = {.x = r->min.x, .y =r->max.y} };
} }
r2f64_t r2f64_cut_right(r2f64_t *r, f64 value) { fn r2f64_t r2f64_cut_right(r2f64_t *r, f64 value) {
f64 maxx = r->max.x; f64 maxx = r->max.x;
r->max.x = MAX(r->min.x, r->max.x - value); r->max.x = MAX(r->min.x, r->max.x - value);
return (r2f64_t){ .min = {.x = r->max.x, .y = r->min.y}, .max = {.x = maxx, .y = r->max.y} }; return (r2f64_t){ .min = {.x = r->max.x, .y = r->min.y}, .max = {.x = maxx, .y = r->max.y} };
} }
r2f64_t r2f64_cut_top(r2f64_t *r, f64 value) { /* Y is down */ fn r2f64_t r2f64_cut_top(r2f64_t *r, f64 value) { /* Y is down */
f64 miny = r->min.y; f64 miny = r->min.y;
r->min.y = MIN(r->max.y, r->min.y + value); r->min.y = MIN(r->max.y, r->min.y + value);
return (r2f64_t){ .min = {.x = r->min.x, .y = miny}, .max = {.x = r->max.x, .y = r->min.y} }; return (r2f64_t){ .min = {.x = r->min.x, .y = miny}, .max = {.x = r->max.x, .y = r->min.y} };
} }
r2f64_t r2f64_cut_bottom(r2f64_t *r, f64 value) { /* Y is down */ fn r2f64_t r2f64_cut_bottom(r2f64_t *r, f64 value) { /* Y is down */
f64 maxy = r->max.y; f64 maxy = r->max.y;
r->max.y = MAX(r->min.y, r->max.y - value); r->max.y = MAX(r->min.y, r->max.y - value);
return (r2f64_t){ .min = {.x = r->min.x, .y = r->max.y}, .max = {.x = r->max.x, .y = maxy} }; return (r2f64_t){ .min = {.x = r->min.x, .y = r->max.y}, .max = {.x = r->max.x, .y = maxy} };
} }
r2f64_t r2f64_cut_left_no_squash(r2f64_t *r, f64 value) { fn r2f64_t r2f64_cut_left_no_squash(r2f64_t *r, f64 value) {
f64 minx = r->min.x; f64 minx = r->min.x;
r->min.x = r->min.x + value; r->min.x = r->min.x + value;
return (r2f64_t){ .min = {.x = minx, .y = r->min.y}, .max = {.x = r->min.x, .y =r->max.y} }; return (r2f64_t){ .min = {.x = minx, .y = r->min.y}, .max = {.x = r->min.x, .y =r->max.y} };
} }
r2f64_t r2f64_cut_right_no_squash(r2f64_t *r, f64 value) { fn r2f64_t r2f64_cut_right_no_squash(r2f64_t *r, f64 value) {
f64 maxx = r->max.x; f64 maxx = r->max.x;
r->max.x = r->max.x - value; r->max.x = r->max.x - value;
return (r2f64_t){ .min = {.x = r->max.x, .y = r->min.y}, .max = {.x = maxx, .y = r->max.y} }; return (r2f64_t){ .min = {.x = r->max.x, .y = r->min.y}, .max = {.x = maxx, .y = r->max.y} };
} }
r2f64_t r2f64_cut_top_no_squash(r2f64_t *r, f64 value) { /* Y is down */ fn r2f64_t r2f64_cut_top_no_squash(r2f64_t *r, f64 value) { /* Y is down */
f64 miny = r->min.y; f64 miny = r->min.y;
r->min.y = r->min.y + value; r->min.y = r->min.y + value;
return (r2f64_t){ .min = {.x = r->min.x, .y = miny}, .max = {.x = r->max.x, .y = r->min.y} }; return (r2f64_t){ .min = {.x = r->min.x, .y = miny}, .max = {.x = r->max.x, .y = r->min.y} };
} }
r2f64_t r2f64_cut_bottom_no_squash(r2f64_t *r, f64 value) { /* Y is down */ fn r2f64_t r2f64_cut_bottom_no_squash(r2f64_t *r, f64 value) { /* Y is down */
f64 maxy = r->max.y; f64 maxy = r->max.y;
r->max.y = r->max.y - value; r->max.y = r->max.y - value;
return (r2f64_t){ .min = {.x = r->min.x, .y = r->max.y}, .max = {.x = r->max.x, .y = maxy} }; return (r2f64_t){ .min = {.x = r->min.x, .y = r->max.y}, .max = {.x = r->max.x, .y = maxy} };
} }
r2f64_t r2f64_add_left(r2f64_t *r, f64 value) { fn r2f64_t r2f64_add_left(r2f64_t *r, f64 value) {
r2f64_t result = { .min = {r->min.x - value, r->min.y}, .max = {r->min.x, r->max.y} }; r2f64_t result = { .min = {r->min.x - value, r->min.y}, .max = {r->min.x, r->max.y} };
r->min.x -= value; r->min.x -= value;
return result; return result;
} }
r2f64_t r2f64_add_right(r2f64_t *r, f64 value) { fn r2f64_t r2f64_add_right(r2f64_t *r, f64 value) {
r2f64_t result = { .min = {r->max.x, r->min.y}, .max = {r->max.x + value, r->max.y} }; r2f64_t result = { .min = {r->max.x, r->min.y}, .max = {r->max.x + value, r->max.y} };
r->max.x += value; r->max.x += value;
return result; return result;
} }
r2f64_t r2f64_add_top(r2f64_t *r, f64 value) { fn r2f64_t r2f64_add_top(r2f64_t *r, f64 value) {
r2f64_t result = { .min = {r->min.x, r->min.y - value}, .max = {r->max.x, r->min.y} }; r2f64_t result = { .min = {r->min.x, r->min.y - value}, .max = {r->max.x, r->min.y} };
r->min.y -= value; r->min.y -= value;
return result; return result;
} }
r2f64_t r2f64_add_bottom(r2f64_t *r, f64 value) { fn r2f64_t r2f64_add_bottom(r2f64_t *r, f64 value) {
r2f64_t result = { .min = {r->min.x, r->max.y}, .max = {r->max.x, r->max.y + value} }; r2f64_t result = { .min = {r->min.x, r->max.y}, .max = {r->max.x, r->max.y + value} };
r->max.y += value; r->max.y += value;
return result; return result;
} }
// get past left // get past left
r2f64_t r2f64_getp_left(const r2f64_t *rect, f64 value) { fn r2f64_t r2f64_getp_left(const r2f64_t *rect, f64 value) {
r2f64_t result = r2f64(rect->min.x - value, rect->min.y, rect->min.x, rect->max.y); r2f64_t result = r2f64(rect->min.x - value, rect->min.y, rect->min.x, rect->max.y);
return result; return result;
} }
r2f64_t r2f64_getp_right(const r2f64_t *rect, f64 value) { fn r2f64_t r2f64_getp_right(const r2f64_t *rect, f64 value) {
r2f64_t result = r2f64(rect->max.x, rect->min.y, rect->max.x + value, rect->max.y); r2f64_t result = r2f64(rect->max.x, rect->min.y, rect->max.x + value, rect->max.y);
return result; return result;
} }
r2f64_t r2f64_getp_bottom(const r2f64_t *rect, f64 value) { fn r2f64_t r2f64_getp_bottom(const r2f64_t *rect, f64 value) {
r2f64_t result = r2f64(rect->min.x, rect->max.y, rect->max.x, rect->max.y + value); r2f64_t result = r2f64(rect->min.x, rect->max.y, rect->max.x, rect->max.y + value);
return result; return result;
} }
r2f64_t r2f64_getp_top(const r2f64_t *rect, f64 value) { fn r2f64_t r2f64_getp_top(const r2f64_t *rect, f64 value) {
r2f64_t result = r2f64(rect->min.x, rect->min.y - value, rect->max.x, rect->min.y); r2f64_t result = r2f64(rect->min.x, rect->min.y - value, rect->max.x, rect->min.y);
return result; return result;
} }
r2f64_t r2f64_get_left(const r2f64_t *r, f64 value) { fn r2f64_t r2f64_get_left(const r2f64_t *r, f64 value) {
f64 minx = r->min.x; f64 minx = r->min.x;
r2f64_t result = (r2f64_t){ .min = {.x = minx, .y = r->min.y}, .max = {.x = MIN(r->min.x + value, r->max.x), .y =r->max.y} }; r2f64_t result = (r2f64_t){ .min = {.x = minx, .y = r->min.y}, .max = {.x = MIN(r->min.x + value, r->max.x), .y =r->max.y} };
return result; return result;
} }
r2f64_t r2f64_get_right(const r2f64_t *r, f64 value) { fn r2f64_t r2f64_get_right(const r2f64_t *r, f64 value) {
f64 maxx = r->max.x; f64 maxx = r->max.x;
r2f64_t result = (r2f64_t){ .min = {.x = MAX(r->min.x, r->max.x - value), .y = r->min.y}, .max = {.x = maxx, .y = r->max.y} }; r2f64_t result = (r2f64_t){ .min = {.x = MAX(r->min.x, r->max.x - value), .y = r->min.y}, .max = {.x = maxx, .y = r->max.y} };
return result; return result;
} }
r2f64_t r2f64_get_top(const r2f64_t *r, f64 value) { /* Y is down */ fn r2f64_t r2f64_get_top(const r2f64_t *r, f64 value) { /* Y is down */
f64 miny = r->min.y; f64 miny = r->min.y;
r2f64_t result = (r2f64_t){ .min = {.x = r->min.x, .y = miny}, .max = {.x = r->max.x, .y = MIN(r->max.y, r->min.y + value)} }; r2f64_t result = (r2f64_t){ .min = {.x = r->min.x, .y = miny}, .max = {.x = r->max.x, .y = MIN(r->max.y, r->min.y + value)} };
return result; return result;
} }
r2f64_t r2f64_get_bottom(const r2f64_t *r, f64 value) { /* Y is down */ fn r2f64_t r2f64_get_bottom(const r2f64_t *r, f64 value) { /* Y is down */
f64 maxy = r->max.y; f64 maxy = r->max.y;
r2f64_t result = (r2f64_t){ .min = {.x = r->min.x, .y = MAX(r->min.y, r->max.y - value)}, .max = {.x = r->max.x, .y = maxy} }; r2f64_t result = (r2f64_t){ .min = {.x = r->min.x, .y = MAX(r->min.y, r->max.y - value)}, .max = {.x = r->max.x, .y = maxy} };
return result; return result;
} }
r2f64_t r2f64_shrink(r2f64_t rect, v2f64_t value) { return (r2f64_t){ v2f64_add(rect.min, value), v2f64_sub(rect.max, value) }; } fn_inline r2f64_t r2f64_shrink(r2f64_t rect, v2f64_t value) { return (r2f64_t){ v2f64_add(rect.min, value), v2f64_sub(rect.max, value) }; }
r2f64_t r2f64_shrinks(r2f64_t rect, f64 value) { return (r2f64_t){ v2f64_adds(rect.min, value), v2f64_subs(rect.max, value) }; } fn_inline r2f64_t r2f64_shrinks(r2f64_t rect, f64 value) { return (r2f64_t){ v2f64_adds(rect.min, value), v2f64_subs(rect.max, value) }; }
v2f64_t r2f64_get_size(r2f64_t r) { return (v2f64_t){r.max.x - r.min.x, r.max.y - r.min.y}; } fn_inline v2f64_t r2f64_get_size(r2f64_t r) { return (v2f64_t){r.max.x - r.min.x, r.max.y - r.min.y}; }
v2f64_t r2f64_get_mid(r2f64_t r) { return v2f64_add(r.min, v2f64_divs(r2f64_get_size(r), 2)); } fn_inline v2f64_t r2f64_get_mid(r2f64_t r) { return v2f64_add(r.min, v2f64_divs(r2f64_get_size(r), 2)); }
b32 r2f64_contains(r2f64_t rec, v2f64_t point) { return (point.x >= rec.min.x) && (point.x < rec.max.x) && (point.y >= rec.min.y) && (point.y < rec.max.y); } fn_inline b32 r2f64_contains(r2f64_t rec, v2f64_t point) { return (point.x >= rec.min.x) && (point.x < rec.max.x) && (point.y >= rec.min.y) && (point.y < rec.max.y); }
r2f64_t r2f64_intersect(r2f64_t a, r2f64_t b) { return (r2f64_t){ .min.x = MAX(a.min.x, b.min.x), .min.y = MAX(a.min.y, b.min.y), .max.x = MIN(a.max.x, b.max.x), .max.y = MIN(a.max.y, b.max.y) }; } fn_inline r2f64_t r2f64_intersect(r2f64_t a, r2f64_t b) { return (r2f64_t){ .min.x = MAX(a.min.x, b.min.x), .min.y = MAX(a.min.y, b.min.y), .max.x = MIN(a.max.x, b.max.x), .max.y = MIN(a.max.y, b.max.y) }; }
r2f64_t r2f64_union(r2f64_t a, r2f64_t b) { return (r2f64_t){ .min.x = MIN(a.min.x, b.min.x), .min.y = MIN(a.min.y, b.min.y), .max.x = MAX(a.max.x, b.max.x), .max.y = MAX(a.max.y, b.max.y) }; } fn_inline r2f64_t r2f64_union(r2f64_t a, r2f64_t b) { return (r2f64_t){ .min.x = MIN(a.min.x, b.min.x), .min.y = MIN(a.min.y, b.min.y), .max.x = MAX(a.max.x, b.max.x), .max.y = MAX(a.max.y, b.max.y) }; }
""" """
s = s.replace("r2f64", f"r2{basic_type}") s = s.replace("r2f64", f"r2{basic_type}")
s = s.replace("f64", basic_type) s = s.replace("f64", basic_type)
@@ -335,7 +335,7 @@ for at in basic_types:
result += ", " result += ", "
return result return result
print(f"{btype}_t {atype}_to_{btype}({atype}_t v) {{ return ({btype}_t){{ {cast_members(members, bt)} }}; }}") print(f"fn_inline {btype}_t {atype}_to_{btype}({atype}_t v) {{ return ({btype}_t){{ {cast_members(members, bt)} }}; }}")
####### #######
@@ -344,8 +344,8 @@ for bt in basic_types:
for sym in symbols: for sym in symbols:
word_op = sym[1] word_op = sym[1]
s = """ s = """
r2f64_t r2f64_sub_v2f64(r2f64_t a, v2f64_t b) { return (r2f64_t){ v2f64_sub(a.min, b), v2f64_sub(a.max, b) }; } fn_inline r2f64_t r2f64_sub_v2f64(r2f64_t a, v2f64_t b) { return (r2f64_t){ v2f64_sub(a.min, b), v2f64_sub(a.max, b) }; }
""".strip() """.strip()
s = s.replace("f64", bt) s = s.replace("f64", bt)
s = s.replace("sub", word_op) s = s.replace("sub", word_op)
print(s) print(s)

View File

@@ -149,6 +149,7 @@ void test_intern_table(void) {
#include <stdio.h> #include <stdio.h>
int main(int argc, char **argv) { int main(int argc, char **argv) {
printf("PLATFORM_WASM = %d\n", PLATFORM_WASM); printf("PLATFORM_WASM = %d\n", PLATFORM_WASM);
printf("PLATFORM_WINDOWS = %d\n", PLATFORM_WINDOWS); printf("PLATFORM_WINDOWS = %d\n", PLATFORM_WINDOWS);
@@ -160,9 +161,11 @@ int main(int argc, char **argv) {
printf("PLATFORM_CL = %d\n", PLATFORM_CL); printf("PLATFORM_CL = %d\n", PLATFORM_CL);
printf("PLATFORM_TCC = %d\n", PLATFORM_TCC); printf("PLATFORM_TCC = %d\n", PLATFORM_TCC);
test_s8(); test_s8();
test_hash_table(); test_hash_table();
test_intern_table(); test_intern_table();
debugf("%f %f", f64_pow(2, 10.4f), powf(2, 10.4f));
printf("all done!\n"); printf("all done!\n");
} }

View File

@@ -127,7 +127,9 @@ fn ui_box_t *ui_build_box_from_id(ui_code_loc_t loc, ui_box_flags_t flags, ui_id
box->flags = flags; box->flags = flags;
box->text_align = ui->text_align_stack->value; box->text_align = ui->text_align_stack->value;
box->border_thickness = ui->border_thickness_stack->value; box->border_thickness = ui->border_thickness_stack->value;
if (ui->background_color_stack) box->background_color = ui->background_color_stack->value; box->background_color = ui->background_color_stack->value;
box->text_color = ui->text_color_stack->value;
box->border_color = ui->border_color_stack->value;
ui_push_box(ui->top, box); ui_push_box(ui->top, box);
return box; return box;
} }
@@ -262,6 +264,9 @@ fn void ui_begin_build(ui_code_loc_t loc, app_event_t *ev, r2f32_t window_rect)
ui_push_id(ui_idf("root")); ui_push_id(ui_idf("root"));
ui_push_border_thickness(1.0f); ui_push_border_thickness(1.0f);
ui_push_text_align(ui_text_align_left); ui_push_text_align(ui_text_align_left);
ui_push_background_color(primary_color_global);
ui_push_text_color(black_color_global);
ui_push_border_color(accent2_color_global);
zero_struct(&ui->root); zero_struct(&ui->root);
ui->root.full_rect = ui->root.rect = window_rect; ui->root.full_rect = ui->root.rect = window_rect;
@@ -274,9 +279,15 @@ fn void ui_begin_build(ui_code_loc_t loc, app_event_t *ev, r2f32_t window_rect)
fn void ui_end_build(void) { fn void ui_end_build(void) {
assert(ui->top == &ui->root); assert(ui->top == &ui->root);
SLLS_POP(ui->lop_stack);
SLLS_POP(ui->id_stack); SLLS_POP(ui->id_stack);
SLLS_POP(ui->border_thickness_stack); SLLS_POP(ui->border_thickness_stack);
SLLS_POP(ui->text_align_stack); SLLS_POP(ui->text_align_stack);
SLLS_POP(ui->background_color_stack);
SLLS_POP(ui->text_color_stack);
SLLS_POP(ui->border_color_stack);
ui_assert_stacks_are_null();
for (ui_box_t *box = ui->hash_first, *next = NULL; box; box = next) { for (ui_box_t *box = ui->hash_first, *next = NULL; box; box = next) {
next = box->hash_next; next = box->hash_next;
@@ -298,23 +309,36 @@ fn void ui__draw_box(app_frame_t *frame, ui_box_t *box) {
r2f32_t rect = box->full_rect; r2f32_t rect = box->full_rect;
box->final_rect = r2f32_intersect(box->full_rect, ui->clip_rect); box->final_rect = r2f32_intersect(box->full_rect, ui->clip_rect);
v4f32_t rect_color = primary_color_global; v4f32_t background_color = box->background_color;
if (!v4f32_is_null(box->background_color)) rect_color = box->background_color; v4f32_t text_color = box->text_color;
v4f32_t text_color = black_color_global;
v4f32_t border_color = box->border_color;
if (ui_is_hot_box(box)) { if (ui_is_hot_box(box)) {
rect_color = secondary_color_global; v4f32_t hsla_rect = v4f32_rgba_to_hsla(background_color);
text_color = accent2_color_global;
hsla_rect.s = f32_lerp(hsla_rect.s, 1.0f, f32_ease_out_elastic(f32_clamp01(box->hot_t)));
background_color = v4f32_hsla_to_rgba(hsla_rect);
// v4f32_t hsla_text = v4f32_rgba_to_hsla(text_color);
// hsla_text.l = f32_lerp(hsla_text.l, 1.0f, box->hot_t);
// text_color = v4f32_hsla_to_rgba(hsla_text);
} }
if (ui_is_active_box(box)) { if (ui_is_active_box(box)) {
rect_color = accent1_color_global; // v4f32_t hsla_rect = v4f32_rgba_to_hsla(background_color);
text_color = white_color_global; // hsla_rect.s = f32_lerp(hsla_rect.s, 1.0f, box->active_t);
// background_color = v4f32_hsla_to_rgba(hsla_rect);
// v4f32_t hsla_text = v4f32_rgba_to_hsla(text_color);
// hsla_text.l = f32_lerp(hsla_text.l, 1.0f, box->active_t);
// text_color = v4f32_hsla_to_rgba(hsla_text);
} }
if (box->flags.draw_rect) { if (box->flags.draw_rect) {
rn_draw_rect(rect, rect_color); rn_draw_rect(rect, background_color);
} }
if (box->flags.draw_border) { if (box->flags.draw_border) {
rn_draw_rect_border(rect, accent2_color_global, box->border_thickness); rn_draw_rect_border(rect, border_color, box->border_thickness);
} }
if (box->flags.draw_text) { if (box->flags.draw_text) {
v2f32_t string_size = rn_measure_string(rn_state.main_font, box->string); v2f32_t string_size = rn_measure_string(rn_state.main_font, box->string);
@@ -341,6 +365,7 @@ fn void ui__draw_box(app_frame_t *frame, ui_box_t *box) {
ui->clip_rect = prev_clip_rect; ui->clip_rect = prev_clip_rect;
rn_set_clip(ui->clip_rect); rn_set_clip(ui->clip_rect);
} }
} }
fn void ui_draw(void) { fn void ui_draw(void) {
@@ -354,6 +379,20 @@ fn void ui_begin_frame(app_frame_t *frame) {
} }
fn void ui_end_frame(void) { fn void ui_end_frame(void) {
for (ui_box_t *box = ui->hash_first, *next = NULL; box; box = next) {
next = box->hash_next;
if (ui_is_hot_box(box)) {
box->hot_t += (f32)ui->frame->delta;
} else {
box->hot_t = 0;
}
if (ui_is_active_box(box)) {
box->active_t += (f32)ui->frame->delta;
} else {
box->active_t = 0;
}
}
for (app_event_t *ev = ui->frame->first_event; ev; ev = ev->next) { for (app_event_t *ev = ui->frame->first_event; ev; ev = ev->next) {
if (ev_left_up(ev)) { if (ev_left_up(ev)) {
ui->active = ui_null_id; ui->active = ui_null_id;
@@ -561,15 +600,15 @@ fn void ui_demo_init(ma_arena_t *arena) {
gb i32 ui_g_panel = 1; gb i32 ui_g_panel = 1;
gb app_event_t ui_test_event; gb app_event_t ui_test_event;
fn void ui_demo_update(app_frame_t *frame) { fn void ui_demo_update(app_frame_t *frame, mt_tweak_t *tweak_table, i32 tweak_count) {
ui_begin_frame(frame); ui_begin_frame(frame);
for (app_event_t *ev = frame->first_event; ev; ev = ev->next) { for (app_event_t *ev = frame->first_event; ev; ev = ev->next) {
ui_begin_build(UILOC, ev, window_rect_from_frame(frame)); ui_begin_build(UILOC, ev, window_rect_from_frame(frame));
ui_box_t *top_box = ui_box0((ui_box_flags_t){.draw_rect = true, .clip_rect = true}); ui_box_t *top_box = ui_box0((ui_box_flags_t){.draw_rect = true, .clip_rect = true});
ui_set_rect(top_box, r2f32_cut_top(&ui->top->rect, ui_em(1))); ui_set_rect(top_box, r2f32_cut_top(&ui->top->rect, ui_em(1.5f)));
ui_set_padding(v2f32(50, 0)) ui_set_padding(v2f32(ui_em(3), 0))
ui_set_text_align(ui_text_align_center) ui_set_text_align(ui_text_align_center)
ui_set_lop(ui_lop_cut_left) ui_set_lop(ui_lop_cut_left)
ui_set_top(top_box) { ui_set_top(top_box) {
@@ -577,7 +616,7 @@ fn void ui_demo_update(app_frame_t *frame) {
ui_radio_button(&ui_g_panel, 2, "2"); ui_radio_button(&ui_g_panel, 2, "2");
} }
ui->top->rect = r2f32_shrinks(ui->top->rect, 100); ui->top->rect = r2f32_shrinks(ui->top->rect, ui_em(1));
if (ui_g_panel == 1) { if (ui_g_panel == 1) {
@@ -586,10 +625,12 @@ fn void ui_demo_update(app_frame_t *frame) {
ui_box_t *item_box = ui_box0((ui_box_flags_t){.draw_rect = true, .clip_rect = true}); ui_box_t *item_box = ui_box0((ui_box_flags_t){.draw_rect = true, .clip_rect = true});
ui_set_rect(item_box, r2f32_cut_left(&ui->top->rect, ui_max)); ui_set_rect(item_box, r2f32_cut_left(&ui->top->rect, ui_max));
item_box->rect = r2f32_shrinks(item_box->rect, ui_em(1));
ui_set_text_align(ui_text_align_left) ui_set_text_align(ui_text_align_left)
ui_set_top(item_box) { ui_set_top(item_box) {
for (i32 i = 0; i < lengthof(tweak_table); i += 1) { for (i32 i = 0; i < tweak_count; i += 1) {
mt_tweak_t *tweak = tweak_table + i; mt_tweak_t *tweak = tweak_table + i;
if (s8_starts_with(tweak->name, s8_lit("_"), false)) { if (s8_starts_with(tweak->name, s8_lit("_"), false)) {
continue; continue;

View File

@@ -23,6 +23,12 @@ fn void ui_pop_padding(void) { SLLS_POP(ui->padding_stack); }
fn void ui_push_background_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx.temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->background_color_stack, n); } fn void ui_push_background_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx.temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->background_color_stack, n); }
fn void ui_pop_background_color(void) { SLLS_POP(ui->background_color_stack); } fn void ui_pop_background_color(void) { SLLS_POP(ui->background_color_stack); }
#define ui_set_background_color(x) defer_block(ui_push_background_color(x), ui_pop_background_color()) #define ui_set_background_color(x) defer_block(ui_push_background_color(x), ui_pop_background_color())
fn void ui_push_border_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx.temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->border_color_stack, n); }
fn void ui_pop_border_color(void) { SLLS_POP(ui->border_color_stack); }
#define ui_set_border_color(x) defer_block(ui_push_border_color(x), ui_pop_border_color())
fn void ui_push_text_color(v4f32_t v) { ui_v4f32_node_t *n = ma_push_type(tcx.temp, ui_v4f32_node_t); n->value = v; SLLS_PUSH(ui->text_color_stack, n); }
fn void ui_pop_text_color(void) { SLLS_POP(ui->text_color_stack); }
#define ui_set_text_color(x) defer_block(ui_push_text_color(x), ui_pop_text_color())
fn void ui_assert_stacks_are_null(void) { fn void ui_assert_stacks_are_null(void) {
assert(ui->id_stack == NULL); assert(ui->id_stack == NULL);
@@ -33,4 +39,6 @@ assert(ui->required_width_stack == NULL);
assert(ui->required_height_stack == NULL); assert(ui->required_height_stack == NULL);
assert(ui->padding_stack == NULL); assert(ui->padding_stack == NULL);
assert(ui->background_color_stack == NULL); assert(ui->background_color_stack == NULL);
assert(ui->border_color_stack == NULL);
assert(ui->text_color_stack == NULL);
} }

View File

@@ -13,6 +13,8 @@ f32 required_width;\
f32 required_height;\ f32 required_height;\
v2f32_t padding;\ v2f32_t padding;\
v4f32_t background_color;\ v4f32_t background_color;\
v4f32_t border_color;\
v4f32_t text_color;\
#define UI_DECL_STACKS \ #define UI_DECL_STACKS \
ui_id_node_t *id_stack;\ ui_id_node_t *id_stack;\
@@ -23,3 +25,5 @@ ui_f32_node_t *required_width_stack;\
ui_f32_node_t *required_height_stack;\ ui_f32_node_t *required_height_stack;\
ui_v2f32_node_t *padding_stack;\ ui_v2f32_node_t *padding_stack;\
ui_v4f32_node_t *background_color_stack;\ ui_v4f32_node_t *background_color_stack;\
ui_v4f32_node_t *border_color_stack;\
ui_v4f32_node_t *text_color_stack;\

View File

@@ -58,6 +58,9 @@ struct ui_box_t {
ui_box_t *hash_prev; ui_box_t *hash_prev;
u64 last_touched_event_id; u64 last_touched_event_id;
f32 hot_t;
f32 active_t;
r2f32_t final_rect; r2f32_t final_rect;
b32 expanded; b32 expanded;
}; };

View File

@@ -9,6 +9,8 @@ void mt_ui(ma_arena_t *arena) {
{ f32 required_height 0 } { f32 required_height 0 }
{ v2f32_t padding 0 } { v2f32_t padding 0 }
{ v4f32_t background_color 0 } { v4f32_t background_color 0 }
{ v4f32_t border_color 0 }
{ v4f32_t text_color 0 }
)); ));
/////////////////////////////// ///////////////////////////////

3
src/ui/ui_inc.c Normal file
View File

@@ -0,0 +1,3 @@
#include "ui_iter.c"
#include "ui.gen.c"
#include "ui.c"

1
src/ui/ui_inc.h Normal file
View File

@@ -0,0 +1 @@
#include "ui.h"

View File

@@ -1,15 +1,13 @@
#include "core/core_inc.h" #include "core/core_inc.h"
#include "app/app.h" #include "app/app.h"
#include "ui/ui.h" #include "ui/ui_inc.h"
#include "core/core_inc.c" #include "core/core_inc.c"
#include "app/app.c" #include "app/app.c"
#include "render/render_inc.c" #include "render/render_inc.c"
#include "ui/ui_inc.c"
#include "wasm_app.gen.c" #include "wasm_app.gen.c"
#include "ui/ui_iter.c"
#include "ui/ui.gen.c"
#include "ui/ui.c"
fn void app_init(f32 dpr) { fn void app_init(f32 dpr) {
ma_arena_t *perm = &tcx._perm; ma_arena_t *perm = &tcx._perm;
@@ -25,6 +23,6 @@ fn b32 app_update(app_frame_t *frame) {
_font_size = font_size; _font_size = font_size;
rn_reload_font(font_size, frame->dpr); rn_reload_font(font_size, frame->dpr);
} }
ui_demo_update(frame); ui_demo_update(frame, tweak_table, lengthof(tweak_table));
return true; return true;
} }

View File

@@ -27,6 +27,7 @@
[ ] demo style, with different buttons and controls, headings [ ] demo style, with different buttons and controls, headings
[ ] palette idea, also push pop palette [ ] palette idea, also push pop palette
[ ] push pop flag [ ] push pop flag
[ ] replace padding with something more 'real'
[ ] color picker [ ] color picker
[ ] slider [ ] slider
[ ] draw image in box ui [ ] draw image in box ui