/* draw a tile texture from origin */
for (i = 0; i < R_TILE_W; i++) {
- for (j = 0; j < R_TILE_H-1; j++) {
+ for (j = 0; j < R_TILE_H; j++) {
final_color = r_asset_fallback_tile[i + j * R_TILE_W] & color;
r_draw_pixel(&r_framebuffer, t.x + i, t.y + j, final_color);
}
}
lrts_bool t_camera_is_visible_world(struct t_camera *c, i32 x, i32 y) {
- struct u_vec2 t;
- t.x = c->x;
- t.y = c->y;
+ struct u_vec2 t = u_point_to_screen(c->x, c->y);
return u_point_in_rect(x, y, t.x, t.y, t.x + c->viewport_x, t.y + c->viewport_y);
}
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-to-screen", t_test_point_to_world_world_to_point);
T_TESTCASE("point-in-rect", t_test_pointinrect);
T_TESTCASE("fixed-point-macro", t_test_fp_macros);
T_TESTCASE("fixed-point-sqrt", t_test_fp_sqrt);
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));
+ T_ASSERT(res.x == 0 && res.y == 48, ("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));
+ T_ASSERT(res.x == -48 && res.y == 72, ("tile to screen %d/%d\n", res.x, res.y));
+ return 0;
+}
+
+int t_test_point_to_world_world_to_point(void) {
+ struct u_vec2 res = u_point_to_screen(1, 2);
+ T_ASSERT(res.x == -2 && res.y == 1, ("point to screen %d/%d\n", res.x, res.y));
+
+ res = u_point_to_world(res.x, res.y);
+ T_ASSERT(res.x == 1 && res.y == 2, ("point to world %d/%d\n", res.x, res.y));
+
+
+ res = u_point_to_screen(32, 64);
+ T_ASSERT(res.x == -48 && res.y == 40, ("point to screen %d/%d\n", res.x, res.y));
+
+ res = u_point_to_world(res.x, res.y);
+ T_ASSERT(res.x == 32 && res.y == 64, ("point to world %d/%d\n", res.x, res.y));
+
return 0;
}
}
struct u_vec2 u_tile_to_screen(i32 x, i32 y) {
+ return u_point_to_screen(x * R_TILE_W, y * R_TILE_H);
+}
+
+struct u_vec2 u_point_to_screen(i32 x, i32 y) {
struct u_vec2 r;
- r.x = (x * R_TILE_W / 2) + (y * R_TILE_W / 2);
- r.y = (y * R_TILE_H / 2) - (x * R_TILE_H / 2);
+ r.x = (x / 2 - y);
+ r.y = (y + x / 2) / 2;
return r;
}
-struct u_vec2 u_point_to_screen(i32 x, i32 y) {
+struct u_vec2 u_point_to_world(i32 x, i32 y) {
struct u_vec2 r;
- r.x = (x) + (y);
- r.y = (y) - (x);
+ r.x = (y + x);
+ r.y = (2 * y - x) / 2;
return r;
}
struct u_vec2 u_vec2_mul(i32 n, struct u_vec2 v);
/*
- * converts a tile position to
+ * converts a world tile position to
* a screen position
*/
struct u_vec2 u_tile_to_screen(i32 x, i32 y);
-/* convers a point to screen space */
+/* convers a world point to screen space */
struct u_vec2 u_point_to_screen(i32 x, i32 y);
+/* converts a screen point to world space */
+struct u_vec2 u_point_to_world(i32 x, i32 y);
+
/* 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);