diff --git a/src/user.c b/src/user.c index 6924c8d1..67b28e1d 100644 --- a/src/user.c +++ b/src/user.c @@ -39,11 +39,11 @@ GLOBAL struct { struct view world_view; struct tick blend_ticks[2]; - b32 panning; - struct v2 panning_from; + b32 debug_camera; + b32 debug_camera_panning; + struct v2 debug_camera_panning_from; b32 debug_draw; - b32 debug_camera; /* User thread input */ struct sys_mutex sys_events_mutex; @@ -329,10 +329,10 @@ INTERNAL void user_update(void) /* Move camera/view */ if (event->button == SYS_BTN_M3) { if (event->kind == SYS_EVENT_KIND_BUTTON_DOWN) { - L.panning = true; - L.panning_from = view_mouse_pos(L.world_view); + L.debug_camera_panning = true; + L.debug_camera_panning_from = view_mouse_pos(L.world_view); } else if (event->kind == SYS_EVENT_KIND_BUTTON_UP) { - L.panning = false; + L.debug_camera_panning = false; } } @@ -358,7 +358,7 @@ INTERNAL void user_update(void) } } - /* Bind */ + /* Update bind states */ if ((event->kind == SYS_EVENT_KIND_BUTTON_DOWN || event->kind == SYS_EVENT_KIND_BUTTON_UP) && !event->is_repeat) { enum sys_btn button = event->button; button = button >= SYS_BTN_COUNT ? SYS_BTN_NONE : button; @@ -374,42 +374,9 @@ INTERNAL void user_update(void) } /* ========================== * - * Process input + * Process movement input * ========================== */ - /* Pan view */ - if (L.panning) { - struct v2 offset = v2_sub(L.panning_from, view_mouse_pos(L.world_view)); - L.world_view.center = v2_add(L.world_view.center, offset); - L.panning_from = view_mouse_pos(L.world_view); - } - - /* Zoom view */ - i32 input_zooms = g_bind_states[USER_BIND_KIND_ZOOM_IN].num_presses - g_bind_states[USER_BIND_KIND_ZOOM_OUT].num_presses; - if (input_zooms != 0) { - i32 dir = input_zooms >= 0 ? 1 : -1; - u32 zooms_abs = input_zooms >= 0 ? input_zooms : -input_zooms; - - /* Zoom camera/view */ - f32 zoom_rate = 2; - f32 new_zoom = L.world_view.zoom; - for (u32 i = 0; i < zooms_abs; ++i) { - if (dir > 0) { - new_zoom *= zoom_rate; - } else { - new_zoom *= 1.0f / zoom_rate; - } - } - - struct v2 old_mouse = view_mouse_pos(L.world_view); - L.world_view.zoom = new_zoom; - struct v2 new_mouse = view_mouse_pos(L.world_view); - - /* Offset view to zoom in on mouse */ - struct v2 offset = v2_sub(old_mouse, new_mouse); - L.world_view.center = v2_add(L.world_view.center, offset); - } - /* Movement */ { struct v2 input_move_dir = { 0 }; @@ -447,14 +414,54 @@ INTERNAL void user_update(void) }); } - /* Debug */ + /* ========================== * + * Update view from debug camera + * ========================== */ + if (g_bind_states[USER_BIND_KIND_DEBUG_DRAW].num_presses > 0) { L.debug_draw = !L.debug_draw; } + if (g_bind_states[USER_BIND_KIND_DEBUG_CAMERA].num_presses > 0) { L.debug_camera = !L.debug_camera; } + if (L.debug_camera) { + + /* Pan view */ + if (L.debug_camera_panning) { + struct v2 offset = v2_sub(L.debug_camera_panning_from, view_mouse_pos(L.world_view)); + L.world_view.center = v2_add(L.world_view.center, offset); + L.debug_camera_panning_from = view_mouse_pos(L.world_view); + } + + /* Zoom view */ + i32 input_zooms = g_bind_states[USER_BIND_KIND_ZOOM_IN].num_presses - g_bind_states[USER_BIND_KIND_ZOOM_OUT].num_presses; + if (input_zooms != 0) { + i32 dir = input_zooms >= 0 ? 1 : -1; + u32 zooms_abs = input_zooms >= 0 ? input_zooms : -input_zooms; + + /* Zoom camera/view */ + f32 zoom_rate = 2; + f32 new_zoom = L.world_view.zoom; + for (u32 i = 0; i < zooms_abs; ++i) { + if (dir > 0) { + new_zoom *= zoom_rate; + } else { + new_zoom *= 1.0f / zoom_rate; + } + } + + struct v2 old_mouse = view_mouse_pos(L.world_view); + L.world_view.zoom = new_zoom; + struct v2 new_mouse = view_mouse_pos(L.world_view); + + /* Offset view to zoom in on mouse */ + struct v2 offset = v2_sub(old_mouse, new_mouse); + L.world_view.center = v2_add(L.world_view.center, offset); + } + } + /* ========================== * * Produce interpolated tick * ========================== */ @@ -504,6 +511,26 @@ INTERNAL void user_update(void) #endif } + /* ========================== * + * Update view from game camera + * ========================== */ + + /* Find camera */ + for (u64 entity_index = 0; entity_index < tick->entities_count; ++entity_index) { + struct entity *ent = &tick->entities[entity_index]; + if (!ent->valid) continue; + + if (entity_has_prop(ent, ENTITY_PROP_CAMERA) && ent->camera_active && !L.debug_camera) { + struct v2 center = mat3x3_get_pos(ent->world_xform); + f32 rot = ent->camera_rot; + f32 zoom = ent->camera_zoom; + zoom = zoom > 0 ? zoom : 1; + L.world_view.center = center; + L.world_view.rot = rot; + L.world_view.zoom = zoom; + } + } + /* ========================== * * Draw test grid * ========================== */ @@ -556,17 +583,6 @@ INTERNAL void user_update(void) b32 is_camera = entity_has_prop(ent, ENTITY_PROP_CAMERA); - /* Update view */ - if (is_camera && ent->camera_active && !L.debug_camera) { - struct v2 center = mat3x3_get_pos(ent->world_xform); - f32 rot = ent->camera_rot; - f32 zoom = ent->camera_zoom; - zoom = zoom > 0 ? zoom : 1; - L.world_view.center = center; - L.world_view.rot = rot; - L.world_view.zoom = zoom; - } - /* Draw sprite */ if (ent->sprite_name.len > 0) { struct string tex_name = ent->sprite_name;