make editor camera independent of player camera
This commit is contained in:
parent
6305d0b096
commit
53a7ed7890
201
src/main.cpp
201
src/main.cpp
@ -101,6 +101,9 @@ static vec2 mouse_pos;
|
||||
|
||||
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_tile_picker = false;
|
||||
static bool show_settings = false;
|
||||
@ -325,7 +328,8 @@ static Sint32 selected_tile_kind = -1;
|
||||
static Sint32 selected_tile = -1;
|
||||
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 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();
|
||||
|
||||
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(¤t_map);
|
||||
load_map(map_path, ¤t_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 (selected_tile != -1 && selected_rotation != -1) {
|
||||
if (modifiers & SDL_KMOD_SHIFT) {
|
||||
@ -1715,45 +1690,51 @@ static void process_event_editor(SDL_Event event) {
|
||||
|
||||
drag_start_pos = floor_intersection;
|
||||
|
||||
if (selected_tile_kind != -1) {
|
||||
change_map_tile(tile_pos.x, tile_pos.y, (TileKind)selected_tile_kind);
|
||||
if (event.button.button == SDL_BUTTON_RIGHT) {
|
||||
dragging_camera_change = true;
|
||||
}
|
||||
|
||||
if (-1 <= tile_pos.x && tile_pos.x < current_map.size.x && -1 <= tile_pos.y && tile_pos.y < current_map.size.y) {
|
||||
if (event.button.button == SDL_BUTTON_LEFT) {
|
||||
if (selected_tile_kind != -1) {
|
||||
change_map_tile(tile_pos.x, tile_pos.y, (TileKind)selected_tile_kind);
|
||||
|
||||
if (-1 <= tile_pos.x && tile_pos.x < current_map.size.x && -1 <= tile_pos.y && tile_pos.y < current_map.size.y) {
|
||||
dragging_tile_change = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (0 <= tile_pos.x && tile_pos.x < current_map.size.x && 0 <= tile_pos.y && tile_pos.y < current_map.size.y) {
|
||||
dragging_tile_change = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (0 <= tile_pos.x && tile_pos.x < current_map.size.x && 0 <= tile_pos.y && tile_pos.y < current_map.size.y) {
|
||||
dragging_tile_change = true;
|
||||
}
|
||||
SDL_Keymod modifiers = SDL_GetModState();
|
||||
if (modifiers & SDL_KMOD_SHIFT && tile_pos.x <= -1) {
|
||||
if(modifiers & SDL_KMOD_CTRL)
|
||||
change_map_size(¤t_map, 'W', -1);
|
||||
else
|
||||
change_map_size(¤t_map, 'W', 1);
|
||||
}
|
||||
|
||||
SDL_Keymod modifiers = SDL_GetModState();
|
||||
if (modifiers & SDL_KMOD_SHIFT && tile_pos.x <= -1) {
|
||||
if(modifiers & SDL_KMOD_CTRL)
|
||||
change_map_size(¤t_map, 'W', -1);
|
||||
else
|
||||
change_map_size(¤t_map, 'W', 1);
|
||||
}
|
||||
if (modifiers & SDL_KMOD_SHIFT && tile_pos.x == current_map.size.x) {
|
||||
if (modifiers & SDL_KMOD_CTRL)
|
||||
change_map_size(¤t_map, 'E', -1);
|
||||
else
|
||||
change_map_size(¤t_map, 'E', 1);
|
||||
}
|
||||
|
||||
if (modifiers & SDL_KMOD_SHIFT && tile_pos.x == current_map.size.x) {
|
||||
if (modifiers & SDL_KMOD_CTRL)
|
||||
change_map_size(¤t_map, 'E', -1);
|
||||
else
|
||||
change_map_size(¤t_map, 'E', 1);
|
||||
}
|
||||
if (modifiers & SDL_KMOD_SHIFT && tile_pos.y <= -1) {
|
||||
if (modifiers & SDL_KMOD_CTRL)
|
||||
change_map_size(¤t_map, 'N', -1);
|
||||
else
|
||||
change_map_size(¤t_map, 'N', 1);
|
||||
}
|
||||
|
||||
if (modifiers & SDL_KMOD_SHIFT && tile_pos.y <= -1) {
|
||||
if (modifiers & SDL_KMOD_CTRL)
|
||||
change_map_size(¤t_map, 'N', -1);
|
||||
else
|
||||
change_map_size(¤t_map, 'N', 1);
|
||||
}
|
||||
|
||||
if (modifiers & SDL_KMOD_SHIFT && tile_pos.y == current_map.size.y) {
|
||||
if (modifiers & SDL_KMOD_CTRL)
|
||||
change_map_size(¤t_map, 'S', -1);
|
||||
else
|
||||
change_map_size(¤t_map, 'S', 1);
|
||||
if (modifiers & SDL_KMOD_SHIFT && tile_pos.y == current_map.size.y) {
|
||||
if (modifiers & SDL_KMOD_CTRL)
|
||||
change_map_size(¤t_map, 'S', -1);
|
||||
else
|
||||
change_map_size(¤t_map, 'S', 1);
|
||||
}
|
||||
}
|
||||
} break;
|
||||
|
||||
@ -1761,40 +1742,54 @@ static void process_event_editor(SDL_Event event) {
|
||||
if (io.WantCaptureMouse)
|
||||
return;
|
||||
|
||||
if (selected_tile != -1 && dragging_tile_change) {
|
||||
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);
|
||||
|
||||
Sint32 tile_x = clamp(0, tile_pos.x, current_map.size.x - 1);
|
||||
Sint32 tile_y = clamp(0, tile_pos.y, current_map.size.y - 1);
|
||||
|
||||
i32vec2 drag_start = grid_tile_pos_from_floor_intersection(drag_start_pos);
|
||||
|
||||
Sint32 start_x = min(tile_x, drag_start.x);
|
||||
Sint32 start_y = min(tile_y, drag_start.y);
|
||||
|
||||
Sint32 end_x = max(tile_x, drag_start.x);
|
||||
Sint32 end_y = max(tile_y, drag_start.y);
|
||||
|
||||
for (Sint32 y = start_y; y <= end_y; y++) {
|
||||
for (Sint32 x = start_x; x <= end_x; x++) {
|
||||
if (selected_rotation == -1) {
|
||||
Sint32 rotation = SDL_rand(4);
|
||||
current_map.tiles[x + current_map.size.x * y] = ((rotation & 3) << 16) | selected_tile;
|
||||
} else {
|
||||
current_map.tiles[x + current_map.size.x * y] = ((selected_rotation & 3) << 16) | selected_tile;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
update_buffer(current_map.gpu_buffer, 0, current_map.size.x * current_map.size.y * sizeof(Uint32), current_map.tiles);
|
||||
if (event.button.button == SDL_BUTTON_RIGHT) {
|
||||
dragging_camera_change = false;
|
||||
}
|
||||
|
||||
dragging_tile_change = false;
|
||||
if (event.button.button == SDL_BUTTON_LEFT) {
|
||||
if (selected_tile != -1 && dragging_tile_change) {
|
||||
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);
|
||||
|
||||
Sint32 tile_x = clamp(0, tile_pos.x, current_map.size.x - 1);
|
||||
Sint32 tile_y = clamp(0, tile_pos.y, current_map.size.y - 1);
|
||||
|
||||
i32vec2 drag_start = grid_tile_pos_from_floor_intersection(drag_start_pos);
|
||||
|
||||
Sint32 start_x = min(tile_x, drag_start.x);
|
||||
Sint32 start_y = min(tile_y, drag_start.y);
|
||||
|
||||
Sint32 end_x = max(tile_x, drag_start.x);
|
||||
Sint32 end_y = max(tile_y, drag_start.y);
|
||||
|
||||
for (Sint32 y = start_y; y <= end_y; y++) {
|
||||
for (Sint32 x = start_x; x <= end_x; x++) {
|
||||
if (selected_rotation == -1) {
|
||||
Sint32 rotation = SDL_rand(4);
|
||||
current_map.tiles[x + current_map.size.x * y] = ((rotation & 3) << 16) | selected_tile;
|
||||
} else {
|
||||
current_map.tiles[x + current_map.size.x * y] = ((selected_rotation & 3) << 16) | selected_tile;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
update_buffer(current_map.gpu_buffer, 0, current_map.size.x * current_map.size.y * sizeof(Uint32), current_map.tiles);
|
||||
}
|
||||
|
||||
dragging_tile_change = false;
|
||||
}
|
||||
} break;
|
||||
|
||||
case SDL_EVENT_MOUSE_MOTION: {
|
||||
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) {
|
||||
vec2 floor_intersection = get_floor_intersection_of_mouse(mouse_pos);
|
||||
@ -1814,6 +1809,28 @@ static void update_state_editor() {
|
||||
|
||||
if (ImGui::BeginMainMenuBar()) {
|
||||
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("Demo Window", NULL, &show_demo_window);
|
||||
ImGui::Separator();
|
||||
@ -2005,8 +2022,8 @@ static void render_editor(WGPURenderPassColorAttachment framebuffer) {
|
||||
|
||||
float aspect_ratio = ((float)window_size.x / (float)window_size.y);
|
||||
|
||||
view_matrix = view (vec3(player.position, 0), radians(camera_tilt), camera_distance);
|
||||
inverse_view_matrix = inverse_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(editor_camera_position, 0), 0, editor_camera_distance);
|
||||
|
||||
projection_matrix = projection (radians(camera_fovy_degrees), aspect_ratio, NEAR_PLANE);
|
||||
inverse_projection_matrix = inverse_projection(radians(camera_fovy_degrees), aspect_ratio, NEAR_PLANE);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user