make editor camera independent of player camera

This commit is contained in:
Sven Balzer 2026-04-18 13:33:01 +02:00
parent 6305d0b096
commit 53a7ed7890

View File

@ -101,6 +101,9 @@ static vec2 mouse_pos;
static bool in_editor; static bool in_editor;
static vec2 editor_camera_position;
static float editor_camera_distance = 30.0f;
static bool show_demo_window = false; static bool show_demo_window = false;
static bool show_tile_picker = false; static bool show_tile_picker = false;
static bool show_settings = false; static bool show_settings = false;
@ -325,7 +328,8 @@ static Sint32 selected_tile_kind = -1;
static Sint32 selected_tile = -1; static Sint32 selected_tile = -1;
static Sint32 selected_rotation = 0; static Sint32 selected_rotation = 0;
static bool dragging_tile_change = false; static bool dragging_tile_change;
static bool dragging_camera_change;
static vec2 drag_start_pos; static vec2 drag_start_pos;
static bool update_buffer(WGPUBuffer buffer, Uint32 offset, Uint32 num_bytes, void *data) { static bool update_buffer(WGPUBuffer buffer, Uint32 offset, Uint32 num_bytes, void *data) {
@ -1666,35 +1670,6 @@ static void process_event_editor(SDL_Event event) {
SDL_Keymod modifiers = SDL_GetModState(); SDL_Keymod modifiers = SDL_GetModState();
if (event.key.key == SDLK_F1) {
save_map(current_map);
}
if (event.key.key == SDLK_F4) {
char *map_path = SDL_strdup(current_map.name);
unload_map(&current_map);
load_map(map_path, &current_map);
SDL_free(map_path);
}
if (event.key.key == SDLK_F5) {
recreate_tile_textures();
WGPUBindGroupEntry world_bind_group_entries[] = {
{ .binding = 0, .textureView = tile_textures_atlas_view },
{ .binding = 1, .sampler = pixel_sampler },
{ .binding = 2, .buffer = tint_color_buffer, .offset = 0, .size = WGPU_WHOLE_SIZE },
{ .binding = 3, .buffer = tile_uvs_buffer, .offset = 0, .size = WGPU_WHOLE_SIZE },
};
WGPUBindGroupDescriptor world_bind_group_descriptor = {
.label = { .data = "world_bind_group", .length = WGPU_STRLEN },
.layout = wgpuRenderPipelineGetBindGroupLayout(world_render_pipeline, 1),
.entryCount = SDL_arraysize(world_bind_group_entries),
.entries = world_bind_group_entries,
};
world_bind_group = wgpuDeviceCreateBindGroup(device, &world_bind_group_descriptor);
}
if (event.key.key == SDLK_R) { if (event.key.key == SDLK_R) {
if (selected_tile != -1 && selected_rotation != -1) { if (selected_tile != -1 && selected_rotation != -1) {
if (modifiers & SDL_KMOD_SHIFT) { if (modifiers & SDL_KMOD_SHIFT) {
@ -1715,6 +1690,11 @@ static void process_event_editor(SDL_Event event) {
drag_start_pos = floor_intersection; drag_start_pos = floor_intersection;
if (event.button.button == SDL_BUTTON_RIGHT) {
dragging_camera_change = true;
}
if (event.button.button == SDL_BUTTON_LEFT) {
if (selected_tile_kind != -1) { if (selected_tile_kind != -1) {
change_map_tile(tile_pos.x, tile_pos.y, (TileKind)selected_tile_kind); change_map_tile(tile_pos.x, tile_pos.y, (TileKind)selected_tile_kind);
@ -1755,12 +1735,18 @@ static void process_event_editor(SDL_Event event) {
else else
change_map_size(&current_map, 'S', 1); change_map_size(&current_map, 'S', 1);
} }
}
} break; } break;
case SDL_EVENT_MOUSE_BUTTON_UP: { case SDL_EVENT_MOUSE_BUTTON_UP: {
if (io.WantCaptureMouse) if (io.WantCaptureMouse)
return; return;
if (event.button.button == SDL_BUTTON_RIGHT) {
dragging_camera_change = false;
}
if (event.button.button == SDL_BUTTON_LEFT) {
if (selected_tile != -1 && dragging_tile_change) { if (selected_tile != -1 && dragging_tile_change) {
vec2 floor_intersection = get_floor_intersection_of_mouse(vec2(event.button.x, event.button.y)); vec2 floor_intersection = get_floor_intersection_of_mouse(vec2(event.button.x, event.button.y));
i32vec2 tile_pos = grid_tile_pos_from_floor_intersection(floor_intersection); i32vec2 tile_pos = grid_tile_pos_from_floor_intersection(floor_intersection);
@ -1791,10 +1777,19 @@ static void process_event_editor(SDL_Event event) {
} }
dragging_tile_change = false; dragging_tile_change = false;
}
} break; } break;
case SDL_EVENT_MOUSE_MOTION: { case SDL_EVENT_MOUSE_MOTION: {
mouse_pos = vec2(event.motion.x, event.motion.y); mouse_pos = vec2(event.motion.x, event.motion.y);
vec2 floor_intersection = get_floor_intersection_of_mouse(mouse_pos);
if (dragging_camera_change) {
editor_camera_position -= (floor_intersection - drag_start_pos);
view_matrix = view (vec3(editor_camera_position, 0), 0, editor_camera_distance);
inverse_view_matrix = inverse_view(vec3(editor_camera_position, 0), 0, editor_camera_distance);
}
if (selected_tile_kind != -1 && dragging_tile_change) { if (selected_tile_kind != -1 && dragging_tile_change) {
vec2 floor_intersection = get_floor_intersection_of_mouse(mouse_pos); vec2 floor_intersection = get_floor_intersection_of_mouse(mouse_pos);
@ -1814,6 +1809,28 @@ static void update_state_editor() {
if (ImGui::BeginMainMenuBar()) { if (ImGui::BeginMainMenuBar()) {
if (ImGui::BeginMenu("File")) { if (ImGui::BeginMenu("File")) {
if (ImGui::MenuItem("Save")) {
save_map(current_map);
}
if (ImGui::MenuItem("Reload")) {
recreate_tile_textures();
WGPUBindGroupEntry world_bind_group_entries[] = {
{ .binding = 0, .textureView = tile_textures_atlas_view },
{ .binding = 1, .sampler = pixel_sampler },
{ .binding = 2, .buffer = tint_color_buffer, .offset = 0, .size = WGPU_WHOLE_SIZE },
{ .binding = 3, .buffer = tile_uvs_buffer, .offset = 0, .size = WGPU_WHOLE_SIZE },
};
WGPUBindGroupDescriptor world_bind_group_descriptor = {
.label = { .data = "world_bind_group", .length = WGPU_STRLEN },
.layout = wgpuRenderPipelineGetBindGroupLayout(world_render_pipeline, 1),
.entryCount = SDL_arraysize(world_bind_group_entries),
.entries = world_bind_group_entries,
};
world_bind_group = wgpuDeviceCreateBindGroup(device, &world_bind_group_descriptor);
}
ImGui::MenuItem("Settings", NULL, &show_settings); ImGui::MenuItem("Settings", NULL, &show_settings);
ImGui::MenuItem("Demo Window", NULL, &show_demo_window); ImGui::MenuItem("Demo Window", NULL, &show_demo_window);
ImGui::Separator(); ImGui::Separator();
@ -2005,8 +2022,8 @@ static void render_editor(WGPURenderPassColorAttachment framebuffer) {
float aspect_ratio = ((float)window_size.x / (float)window_size.y); float aspect_ratio = ((float)window_size.x / (float)window_size.y);
view_matrix = view (vec3(player.position, 0), radians(camera_tilt), camera_distance); view_matrix = view (vec3(editor_camera_position, 0), 0, editor_camera_distance);
inverse_view_matrix = inverse_view(vec3(player.position, 0), radians(camera_tilt), camera_distance); inverse_view_matrix = inverse_view(vec3(editor_camera_position, 0), 0, editor_camera_distance);
projection_matrix = projection (radians(camera_fovy_degrees), aspect_ratio, NEAR_PLANE); projection_matrix = projection (radians(camera_fovy_degrees), aspect_ratio, NEAR_PLANE);
inverse_projection_matrix = inverse_projection(radians(camera_fovy_degrees), aspect_ratio, NEAR_PLANE); inverse_projection_matrix = inverse_projection(radians(camera_fovy_degrees), aspect_ratio, NEAR_PLANE);