math: Added point in rect tester. Added tests for tile to screen and
authorLukas Krickl <lukas@krickl.dev>
Fri, 27 Feb 2026 07:28:27 +0000 (08:28 +0100)
committerLukas Krickl <lukas@krickl.dev>
Fri, 27 Feb 2026 07:28:27 +0000 (08:28 +0100)
point in rect.

src/test.c
src/tests/t_math.c [new file with mode: 0644]
src/u_math.c
src/u_math.h

index 605d52ebf73975bec7ab5666415f71c06eea73d8..842bad7b71c7ae51395c05ea5ae56c030ea33700 100644 (file)
@@ -9,6 +9,7 @@
 #ifdef LRTS_IMPL 
 #include "tests/t_args.c"
 #include "tests/t_arena.c"
+#include "tests/t_math.c"
 #endif
 
 int main(int argc, char **argv) {
@@ -17,8 +18,10 @@ int main(int argc, char **argv) {
   T_TESTBEGIN("lrts test");
   lrts_getopt(argc, argv, lrts_cfg());
        
-       T_TESTCASE("test-argv", t_test_argv);
-       T_TESTCASE("test-arena", t_test_arena);
+       T_TESTCASE("argv", t_test_argv);
+       T_TESTCASE("arena", t_test_arena);
+       T_TESTCASE("tile-to-screen", t_test_tile_to_screen);
+       T_TESTCASE("point-in-rect", t_test_pointinrect);
 
   T_TESTEND("lrts test");
 
diff --git a/src/tests/t_math.c b/src/tests/t_math.c
new file mode 100644 (file)
index 0000000..9772227
--- /dev/null
@@ -0,0 +1,22 @@
+#include "../u_arena.h"
+#include "t_defs.h"
+#include "../u_math.h"
+
+int t_test_pointinrect(void) {
+       T_ASSERT(u_point_in_rect(5, 5, 4, 2, 4, 5), ("point in rect\n"));
+       T_ASSERT(u_point_in_rect(1, 1, 4, 2, 4, 5) == LRTS_FALSE, ("point not in rect\n"));
+
+       return 0;
+}
+
+int t_test_tile_to_screen(void) {
+       struct u_vec2 res = u_tile_to_screen(0, 0);
+       T_ASSERT(res.x == 0 && res.y == 0, ("tile to screen %d/%d\n", res.x, res.y));
+
+       res = u_tile_to_screen(1, 1);
+       T_ASSERT(res.x == 96 && res.y == 0, ("tile to screen %d/%d\n", res.x, res.y));
+
+       res = u_tile_to_screen(1, 2);
+       T_ASSERT(res.x == 144 && res.y == 24, ("tile to screen %d/%d\n", res.x, res.y));
+       return 0;
+}
index c661829a0148a7c9dcec1b605f4f30197e78381b..679ae4463614c9e69b0b542a48e9918176bd2fca 100644 (file)
@@ -63,3 +63,8 @@ struct u_vec2 u_tile_to_screen(i32 x, i32 y) {
 
        return r;
 }
+
+lrts_bool u_point_in_rect(i32 x, i32 y, i32 rx, i32 ry, i32 rw, i32 rh) {
+       return x >= rx && x <= rx + rw
+                       && y >= ry && y <= ry + rh;
+}
index b5838f44b6a2169a26764bd83b67e64afff8586f..4403907e1ae4106182d9762311ccb3db58914094 100644 (file)
@@ -6,6 +6,9 @@
 #define U_FP(n, f) {n, f}
 #define U_V2(x, y) {x, y}
 
+/* Clamps an integer value between min and max */
+#define U_CLAMP(v, min, max) if (v < min) { v = min; } if (v > max) { v = max; }
+
 /* fixed point number 
  * n: whole part
  * f: fractional part
@@ -48,7 +51,7 @@ struct u_vec2 u_vec2_mul(i32 n, struct u_vec2 v);
  */
 struct u_vec2 u_tile_to_screen(i32 x, i32 y);
 
-/* Clamps an integer value between min and max */
-#define U_CLAMP(v, min, max) if (v < min) { v = min; } if (v > max) { v = max; }
+/* tests if a point is contained inside a rectangle */
+lrts_bool u_point_in_rect(i32 x, i32 y, i32 rx, i32 ry, i32 rw, i32 rh);
 
 #endif