add a dual grid toggle

This commit is contained in:
Sven Balzer
2025-03-13 05:55:17 +01:00
parent 57f6926d6f
commit b9a52da265
5 changed files with 434 additions and 407 deletions
+25 -20
View File
@@ -89,6 +89,7 @@ struct PerFrame {
float camera_distance;
float camera_tilt;
Uint32 map_width;
Uint32 use_dual_grid;
};
PerFrame per_frame = { 1.0f, 45.0f, 0.0f, 0.0f, 10.0f, 45.0f };
@@ -603,6 +604,22 @@ V3 Unproject(V3 screen_pos) {
return result.xyz;
}
V2 get_floor_intersection_of_mouse(V2 mouse_pos) {
V2 mouse = remap(V2{ 0, 0 }, V2{ (float)window_width, (float)window_height }, V2{ -1, 1 }, V2{ 1, -1 }, V2{ mouse_pos.x, mouse_pos.y });
V3 camera_position = (inverse_view_matrix * V4_(0, 0, 0, 1)).xyz;
V3 probe = Unproject(V3_(mouse, .5));
V3 ray_dir = normalize(probe - camera_position);
float t = -camera_position.z / ray_dir.z;
V3 floor_intersection = camera_position + (t * ray_dir);
if (per_frame.use_dual_grid)
floor_intersection.xy += V2_(0.5f, 0.5f);
return floor_intersection.xy;
}
int main(int argc, char **argv) {
load_map();
@@ -1053,6 +1070,8 @@ int main(int argc, char **argv) {
ImGui::DragFloat("fovy", &per_frame.fovy_degrees);
ImGui::DragFloat("camera_distance", &per_frame.camera_distance, 0.25f, 1.0f, INFINITY);
ImGui::DragFloat("camera_tilt", &per_frame.camera_tilt, 0.25f, 0.0f, 89.0f);
ImGui::CheckboxFlags("use dual grid", &per_frame.use_dual_grid, 0xffffffff);
}
ImGui::End();
@@ -1097,19 +1116,19 @@ int main(int argc, char **argv) {
continue;
if (event.key.key == SDLK_UP || event.key.key == SDLK_W) {
player.pos_y = clamp(0, player.pos_y + 1, map_height - 1);
player.pos_y = clamp(0, player.pos_y + 1, map_height - 1 - (per_frame.use_dual_grid ? 1 : 0));
}
if (event.key.key == SDLK_LEFT || event.key.key == SDLK_A) {
player.pos_x = clamp(0, player.pos_x - 1, map_width - 1);
player.pos_x = clamp(0, player.pos_x - 1, map_width - 1 - (per_frame.use_dual_grid ? 1 : 0));
}
if (event.key.key == SDLK_DOWN || event.key.key == SDLK_S) {
player.pos_y = clamp(0, player.pos_y - 1, map_height - 1);
player.pos_y = clamp(0, player.pos_y - 1, map_height - 1 - (per_frame.use_dual_grid ? 1 : 0));
}
if (event.key.key == SDLK_RIGHT || event.key.key == SDLK_D) {
player.pos_x = clamp(0, player.pos_x + 1, map_width - 1);
player.pos_x = clamp(0, player.pos_x + 1, map_width - 1 - (per_frame.use_dual_grid ? 1 : 0));
}
if (event.key.key == SDLK_F1) {
@@ -1125,14 +1144,7 @@ int main(int argc, char **argv) {
if (io.WantCaptureMouse)
continue;
V2 mouse = remap(V2{ 0, 0 }, V2{ (float)window_width, (float)window_height }, V2{ -1, 1 }, V2{ 1, -1 }, V2{ event.button.x, event.button.y });
V3 camera_position = (inverse_view_matrix * V4_(0, 0, 0, 1)).xyz;
V3 probe = Unproject(V3_(mouse, .5));
V3 ray_dir = normalize(probe - camera_position);
float t = -camera_position.z / ray_dir.z;
V3 floor_intersection = camera_position + (t * ray_dir);
V2 floor_intersection = get_floor_intersection_of_mouse(V2_(event.button.x, event.button.y));
Sint32 tile_x = roundf(floor_intersection.x);
Sint32 tile_y = roundf(floor_intersection.y);
@@ -1179,14 +1191,7 @@ int main(int argc, char **argv) {
continue;
if (selected_tile != -1 && dragging_tile_change) {
V2 mouse = remap(V2{ 0, 0 }, V2{ (float)window_width, (float)window_height }, V2{ -1, 1 }, V2{ 1, -1 }, V2{ event.button.x, event.button.y });
V3 camera_position = (inverse_view_matrix * V4_(0, 0, 0, 1)).xyz;
V3 probe = Unproject(V3_(mouse, .5));
V3 ray_dir = normalize(probe - camera_position);
float t = -camera_position.z / ray_dir.z;
V3 floor_intersection = camera_position + (t * ray_dir);
V2 floor_intersection = get_floor_intersection_of_mouse(V2_(event.button.x, event.button.y));
Sint32 tile_x = clamp(0, (Sint32)roundf(floor_intersection.x), map_width - 1);
Sint32 tile_y = clamp(0, (Sint32)roundf(floor_intersection.y), map_height - 1);