From fc9d4f1b76489a1427bdee15c1e0e7bf47be44b3 Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Tue, 28 Sep 2021 00:08:23 +0800 Subject: [PATCH] Replace hard-coded controls with string-mapped controls --- src/game/bootloader.cpp | 199 +------------------------- src/game/context.hpp | 37 +---- src/game/states/brood.cpp | 81 ++++++----- src/game/states/loading.cpp | 183 +++++++++++++++++++++++ src/renderer/passes/material-pass.cpp | 10 -- src/renderer/passes/material-pass.hpp | 6 - 6 files changed, 229 insertions(+), 287 deletions(-) diff --git a/src/game/bootloader.cpp b/src/game/bootloader.cpp index 1e1178b..9c5918a 100644 --- a/src/game/bootloader.cpp +++ b/src/game/bootloader.cpp @@ -95,7 +95,6 @@ #include #include #include -#include "utility/timestamp.hpp" static constexpr double seconds_per_day = 24.0 * 60.0 * 60.0; @@ -705,15 +704,7 @@ void setup_scenes(game::context* ctx) // Setup surface scene { ctx->surface_scene = new scene::collection(); - - ctx->lens_spot_light = new scene::spot_light(); - ctx->lens_spot_light->set_color({1, 1, 1}); - ctx->lens_spot_light->set_intensity(20.0f); - ctx->lens_spot_light->set_attenuation({1.0f, 0.0f, 0.0f}); - ctx->lens_spot_light->set_cutoff({math::radians(1.25f), math::radians(1.8f)}); - ctx->surface_scene->add_object(ctx->surface_camera); - //ctx->surface_scene->add_object(ctx->lens_spot_light); } // Clear active scene @@ -755,17 +746,11 @@ void setup_animation(game::context* ctx) ctx->ui_scene->add_object(ctx->radial_transition_outer->get_billboard()); ctx->animator->add_animation(ctx->radial_transition_outer->get_animation()); - // Setup tweens - ctx->focal_point_tween = new tween(); - ctx->focal_point_tween->set_interpolator(math::lerp); - // Set material pass tweens ctx->common_final_pass->set_time_tween(ctx->time_tween); ctx->surface_sky_pass->set_time_tween(ctx->time_tween); ctx->surface_material_pass->set_time_tween(ctx->time_tween); - ctx->surface_material_pass->set_focal_point_tween(ctx->focal_point_tween); ctx->underground_material_pass->set_time_tween(ctx->time_tween); - ctx->underground_material_pass->set_focal_point_tween(ctx->focal_point_tween); ctx->ui_material_pass->set_time_tween(ctx->time_tween); } @@ -773,16 +758,6 @@ void setup_entities(game::context* ctx) { // Create entity registry ctx->entity_registry = new entt::registry(); - - // Reserve named entities - ctx->brush_entity = ctx->entity_registry->create(); - ctx->flashlight_entity = ctx->entity_registry->create(); - ctx->forceps_entity = ctx->entity_registry->create(); - ctx->lens_entity = ctx->entity_registry->create(); - ctx->marker_entity = ctx->entity_registry->create(); - ctx->container_entity = ctx->entity_registry->create(); - ctx->twig_entity = ctx->entity_registry->create(); - ctx->focal_point_entity = ctx->entity_registry->create(); } void setup_systems(game::context* ctx) @@ -912,85 +887,6 @@ void setup_controls(game::context* ctx) ctx->input_listener = new input::listener(); ctx->input_listener->set_event_dispatcher(event_dispatcher); - // Create toggle fullscreen control - ctx->toggle_fullscreen_control = new input::control(); - ctx->toggle_fullscreen_control->set_activated_callback - ( - [ctx]() - { - bool fullscreen = !ctx->app->is_fullscreen(); - - ctx->app->set_fullscreen(fullscreen); - - if (!fullscreen) - { - int2 resolution = ctx->config->get("windowed_resolution"); - ctx->app->resize_window(resolution.x, resolution.y); - } - - ctx->config->set("fullscreen", (fullscreen) ? 1 : 0); - } - ); - - // Create screenshot control - ctx->screenshot_control = new input::control(); - ctx->screenshot_control->set_activated_callback - ( - [ctx]() - { - std::string path = ctx->screenshots_path + "antkeeper-" + timestamp() + ".png"; - ctx->app->save_frame(path); - } - ); - - // Create menu back control - ctx->menu_back_control = new input::control(); - ctx->menu_back_control->set_activated_callback - ( - std::bind(&application::close, ctx->app, 0) - ); - - // Create menu select control - ctx->menu_select_control = new input::control(); - - // Create application control set - ctx->application_controls = new input::control_set(); - ctx->application_controls->add_control(ctx->toggle_fullscreen_control); - ctx->application_controls->add_control(ctx->screenshot_control); - - // Create menu control set - ctx->menu_controls = new input::control_set(); - ctx->menu_controls->add_control(ctx->menu_back_control); - ctx->menu_controls->add_control(ctx->menu_select_control); - - // Create camera controls - ctx->camera_control_slow_modifier = new input::control(); - ctx->camera_control_fast_modifier = new input::control(); - ctx->camera_control_mouse_rotate = new input::control(); - ctx->camera_control_mouse_left = new input::control(); - ctx->camera_control_mouse_right = new input::control(); - ctx->camera_control_mouse_down = new input::control(); - ctx->camera_control_mouse_up = new input::control(); - ctx->camera_control_dolly_forward = new input::control(); - ctx->camera_control_dolly_backward = new input::control(); - ctx->camera_control_truck_left = new input::control(); - ctx->camera_control_truck_right = new input::control(); - ctx->camera_control_pedestal_up = new input::control(); - ctx->camera_control_pedestal_down = new input::control(); - ctx->camera_control_pan_left = new input::control(); - ctx->camera_control_pan_right = new input::control(); - ctx->camera_control_tilt_up = new input::control(); - ctx->camera_control_tilt_down = new input::control(); - - - // Application control mappings - ctx->input_event_router->add_mapping(input::key_mapping(ctx->toggle_fullscreen_control, nullptr, input::scancode::f11)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->screenshot_control, nullptr, input::scancode::f12)); - - // UI controls - ctx->input_event_router->add_mapping(input::key_mapping(ctx->menu_back_control, nullptr, input::scancode::escape)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->menu_back_control, nullptr, input::scancode::backspace)); - /* // Add menu control mappings @@ -1038,41 +934,6 @@ void setup_controls(game::context* ctx) } }); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_move_forward_control(), nullptr, input::scancode::w)); - ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_move_forward_control(), nullptr, input::game_controller_axis::left_y, true)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_move_back_control(), nullptr, input::scancode::s)); - ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_move_back_control(), nullptr, input::game_controller_axis::left_y, false)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_move_left_control(), nullptr, input::scancode::a)); - ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_move_left_control(), nullptr, input::game_controller_axis::left_x, true)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_move_right_control(), nullptr, input::scancode::d)); - ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_move_right_control(), nullptr, input::game_controller_axis::left_x, false)); - ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_rotate_ccw_control(), nullptr, input::game_controller_axis::right_x, false)); - ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_rotate_cw_control(), nullptr, input::game_controller_axis::right_x, true)); - ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_tilt_up_control(), nullptr, input::game_controller_axis::right_y, false)); - ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_tilt_down_control(), nullptr, input::game_controller_axis::right_y, true)); - ctx->input_event_router->add_mapping(input::mouse_wheel_mapping(ctx->control_system->get_zoom_in_control(), nullptr, input::mouse_wheel_axis::positive_y)); - ctx->input_event_router->add_mapping(input::mouse_wheel_mapping(ctx->control_system->get_zoom_out_control(), nullptr, input::mouse_wheel_axis::negative_y)); - //ctx->input_event_router->add_mapping(input::mouse_button_mapping(ctx->control_system->get_adjust_camera_control(), nullptr, 3)); - ctx->input_event_router->add_mapping(input::game_controller_button_mapping(ctx->control_system->get_ascend_control(), nullptr, input::game_controller_button::y)); - ctx->input_event_router->add_mapping(input::game_controller_button_mapping(ctx->control_system->get_descend_control(), nullptr, input::game_controller_button::a)); - ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_zoom_out_control(), nullptr, input::game_controller_axis::trigger_left, false)); - ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->control_system->get_zoom_in_control(), nullptr, input::game_controller_axis::trigger_right, false)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_rotate_ccw_control(), nullptr, input::scancode::q)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_rotate_cw_control(), nullptr, input::scancode::e)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_fast_forward_control(), nullptr, input::scancode::dot)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_rewind_control(), nullptr, input::scancode::comma)); - - ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_exposure_increase_control(), nullptr, input::scancode::right_brace)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_exposure_decrease_control(), nullptr, input::scancode::left_brace)); - - - ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_equip_brush_control(), nullptr, input::scancode::one)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_equip_twig_control(), nullptr, input::scancode::two)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_equip_forceps_control(), nullptr, input::scancode::three)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_equip_container_control(), nullptr, input::scancode::four)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_equip_lens_control(), nullptr, input::scancode::five)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->control_system->get_equip_marker_control(), nullptr, input::scancode::six)); - float time_scale = ctx->config->get("time_scale"); ctx->control_system->get_fast_forward_control()->set_activated_callback ( @@ -1107,35 +968,6 @@ void setup_controls(game::context* ctx) } ); */ - - ctx->input_event_router->add_mapping(input::key_mapping(ctx->camera_control_slow_modifier, nullptr, input::scancode::left_ctrl)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->camera_control_fast_modifier, nullptr, input::scancode::left_shift)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->camera_control_dolly_forward, nullptr, input::scancode::w)); - ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->camera_control_dolly_forward, nullptr, input::game_controller_axis::left_y, true)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->camera_control_dolly_backward, nullptr, input::scancode::s)); - ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->camera_control_dolly_backward, nullptr, input::game_controller_axis::left_y, false)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->camera_control_truck_left, nullptr, input::scancode::a)); - ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->camera_control_truck_left, nullptr, input::game_controller_axis::left_x, true)); - ctx->input_event_router->add_mapping(input::key_mapping(ctx->camera_control_truck_right, nullptr, input::scancode::d)); - ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(ctx->camera_control_truck_right, nullptr, input::game_controller_axis::left_x, false)); - - ctx->input_event_router->add_mapping(input::mouse_wheel_mapping(ctx->camera_control_pedestal_up, nullptr, input::mouse_wheel_axis::positive_y)); - ctx->input_event_router->add_mapping(input::mouse_wheel_mapping(ctx->camera_control_pedestal_down, nullptr, input::mouse_wheel_axis::negative_y)); - - ctx->input_event_router->add_mapping(input::mouse_button_mapping(ctx->camera_control_mouse_rotate, nullptr, 3)); - ctx->input_event_router->add_mapping(input::mouse_motion_mapping(ctx->camera_control_mouse_left, nullptr, input::mouse_motion_axis::negative_x)); - ctx->input_event_router->add_mapping(input::mouse_motion_mapping(ctx->camera_control_mouse_right, nullptr, input::mouse_motion_axis::positive_x)); - ctx->input_event_router->add_mapping(input::mouse_motion_mapping(ctx->camera_control_mouse_down, nullptr, input::mouse_motion_axis::positive_y)); - ctx->input_event_router->add_mapping(input::mouse_motion_mapping(ctx->camera_control_mouse_up, nullptr, input::mouse_motion_axis::negative_y)); - - - // Make lens tool's model instance unculled, so its shadow is always visible. - scene::model_instance* lens_model_instance = ctx->render_system->get_model_instance(ctx->lens_entity); - if (lens_model_instance) - { - lens_model_instance->set_culling_mask(&ctx->no_cull); - } - } void setup_cli(game::context* ctx) @@ -1158,25 +990,8 @@ void setup_callbacks(game::context* ctx) [ctx](double t, double dt) { // Update controls - ctx->application_controls->update(); - ctx->menu_controls->update(); - ctx->camera_control_slow_modifier->update(); - ctx->camera_control_fast_modifier->update(); - ctx->camera_control_mouse_rotate->update(); - ctx->camera_control_mouse_left->update(); - ctx->camera_control_mouse_right->update(); - ctx->camera_control_mouse_down->update(); - ctx->camera_control_mouse_up->update(); - ctx->camera_control_dolly_forward->update(); - ctx->camera_control_dolly_backward->update(); - ctx->camera_control_truck_left->update(); - ctx->camera_control_truck_right->update(); - ctx->camera_control_pedestal_up->update(); - ctx->camera_control_pedestal_down->update(); - ctx->camera_control_pan_left->update(); - ctx->camera_control_pan_right->update(); - ctx->camera_control_tilt_up->update(); - ctx->camera_control_tilt_down->update(); + for (const auto& control: ctx->controls) + control.second->update(); // Update tweens ctx->time_tween->update(); @@ -1184,7 +999,6 @@ void setup_callbacks(game::context* ctx) ctx->surface_scene->update_tweens(); ctx->underground_scene->update_tweens(); ctx->ui_scene->update_tweens(); - ctx->focal_point_tween->update(); // Set time tween time (*ctx->time_tween)[1] = t; @@ -1216,15 +1030,6 @@ void setup_callbacks(game::context* ctx) ctx->painting_system->update(t, dt); ctx->proteome_system->update(t, dt); - //(*ctx->focal_point_tween)[1] = ctx->orbit_cam->get_focal_point(); - - auto xf = entity::command::get_world_transform(*ctx->entity_registry, ctx->lens_entity); - //ctx->lens_spot_light->look_at(xf.translation, xf.translation + ctx->sun_direct->get_direction(), {0, 1, 0}); - - xf = entity::command::get_world_transform(*ctx->entity_registry, ctx->flashlight_entity); - //ctx->flashlight_spot_light->set_transform(xf); - ctx->flashlight_spot_light->look_at(xf.translation, xf.translation + xf.rotation * float3{0, 0, 1}, {0, 0, -1}); - ctx->ui_system->update(dt); ctx->render_system->update(t, dt); ctx->animator->animate(dt); diff --git a/src/game/context.hpp b/src/game/context.hpp index cd95b3f..3432cf9 100644 --- a/src/game/context.hpp +++ b/src/game/context.hpp @@ -192,7 +192,6 @@ struct context // Surface scene scene::collection* surface_scene; scene::camera* surface_camera; - scene::spot_light* lens_spot_light; // Underground scene scene::collection* underground_scene; @@ -204,7 +203,6 @@ struct context timeline* timeline; animator* animator; tween* time_tween; - tween* focal_point_tween; animation* radial_transition_in; animation* radial_transition_out; screen_transition* fade_transition; @@ -218,42 +216,11 @@ struct context input::event_router* input_event_router; input::mapper* input_mapper; input::listener* input_listener; - input::control_set* application_controls; - input::control_set* camera_controls; - input::control_set* menu_controls; - input::control* menu_back_control; - input::control* menu_select_control; - input::control* screenshot_control; - input::control* toggle_fullscreen_control; - - input::control* camera_control_mouse_rotate; - input::control* camera_control_mouse_left; - input::control* camera_control_mouse_right; - input::control* camera_control_mouse_down; - input::control* camera_control_mouse_up; - input::control* camera_control_dolly_forward; - input::control* camera_control_dolly_backward; - input::control* camera_control_truck_left; - input::control* camera_control_truck_right; - input::control* camera_control_pedestal_up; - input::control* camera_control_pedestal_down; - input::control* camera_control_pan_left; - input::control* camera_control_pan_right; - input::control* camera_control_tilt_up; - input::control* camera_control_tilt_down; - input::control* camera_control_slow_modifier; - input::control* camera_control_fast_modifier; + std::unordered_map controls; // Entities entity::registry* entity_registry; - entity::id brush_entity; - entity::id flashlight_entity; - entity::id forceps_entity; - entity::id lens_entity; - entity::id marker_entity; - entity::id container_entity; - entity::id twig_entity; - entity::id focal_point_entity; + std::unordered_map entities; // Systems entity::system::behavior* behavior_system; diff --git a/src/game/states/brood.cpp b/src/game/states/brood.cpp index 435b4ea..0c00c25 100644 --- a/src/game/states/brood.cpp +++ b/src/game/states/brood.cpp @@ -239,14 +239,17 @@ void setup_controls(game::context* ctx) const float pan_speed = math::radians(8.0f); const float tilt_speed = pan_speed; - // Dolly forward - ctx->camera_control_dolly_forward->set_active_callback + const input::control* move_slow = ctx->controls["move_slow"]; + const input::control* move_fast = ctx->controls["move_fast"]; + const input::control* mouse_rotate = ctx->controls["mouse_rotate"]; + + ctx->controls["dolly_forward"]->set_active_callback ( - [ctx, target_eid, three_dof_eid, truck_speed](float value) + [ctx, target_eid, three_dof_eid, truck_speed, move_slow, move_fast](float value) { - if (ctx->camera_control_slow_modifier->is_active()) + if (move_slow->is_active()) value *= 0.5f; - if (ctx->camera_control_fast_modifier->is_active()) + if (move_fast->is_active()) value *= 2.0f; auto& three_dof = ctx->entity_registry->get(three_dof_eid); @@ -258,13 +261,13 @@ void setup_controls(game::context* ctx) ); // Dolly backward - ctx->camera_control_dolly_backward->set_active_callback + ctx->controls["dolly_backward"]->set_active_callback ( - [ctx, target_eid, three_dof_eid, truck_speed](float value) + [ctx, target_eid, three_dof_eid, truck_speed, move_slow, move_fast](float value) { - if (ctx->camera_control_slow_modifier->is_active()) + if (move_slow->is_active()) value *= 0.5f; - if (ctx->camera_control_fast_modifier->is_active()) + if (move_fast->is_active()) value *= 2.0f; auto& three_dof = ctx->entity_registry->get(three_dof_eid); @@ -276,13 +279,13 @@ void setup_controls(game::context* ctx) ); // Truck right - ctx->camera_control_truck_right->set_active_callback + ctx->controls["truck_right"]->set_active_callback ( - [ctx, target_eid, three_dof_eid, truck_speed](float value) + [ctx, target_eid, three_dof_eid, truck_speed, move_slow, move_fast](float value) { - if (ctx->camera_control_slow_modifier->is_active()) + if (move_slow->is_active()) value *= 0.5f; - if (ctx->camera_control_fast_modifier->is_active()) + if (move_fast->is_active()) value *= 2.0f; auto& three_dof = ctx->entity_registry->get(three_dof_eid); @@ -294,13 +297,13 @@ void setup_controls(game::context* ctx) ); // Truck left - ctx->camera_control_truck_left->set_active_callback + ctx->controls["truck_left"]->set_active_callback ( - [ctx, target_eid, three_dof_eid, truck_speed](float value) + [ctx, target_eid, three_dof_eid, truck_speed, move_slow, move_fast](float value) { - if (ctx->camera_control_slow_modifier->is_active()) + if (move_slow->is_active()) value *= 0.5f; - if (ctx->camera_control_fast_modifier->is_active()) + if (move_fast->is_active()) value *= 2.0f; auto& three_dof = ctx->entity_registry->get(three_dof_eid); @@ -312,13 +315,13 @@ void setup_controls(game::context* ctx) ); // Pedestal up - ctx->camera_control_pedestal_up->set_active_callback + ctx->controls["pedestal_up"]->set_active_callback ( - [ctx, target_eid, pedestal_speed](float value) + [ctx, target_eid, pedestal_speed, move_slow, move_fast](float value) { - if (ctx->camera_control_slow_modifier->is_active()) + if (move_slow->is_active()) value *= 0.5f; - if (ctx->camera_control_fast_modifier->is_active()) + if (move_fast->is_active()) value *= 2.0f; const float3 movement = {0.0f, pedestal_speed * value * (1.0f / 60.0f), 0.0f}; @@ -327,13 +330,13 @@ void setup_controls(game::context* ctx) ); // Pedestal down - ctx->camera_control_pedestal_down->set_active_callback + ctx->controls["pedestal_down"]->set_active_callback ( - [ctx, target_eid, pedestal_speed](float value) + [ctx, target_eid, pedestal_speed, move_slow, move_fast](float value) { - if (ctx->camera_control_slow_modifier->is_active()) + if (move_slow->is_active()) value *= 0.5f; - if (ctx->camera_control_fast_modifier->is_active()) + if (move_fast->is_active()) value *= 2.0f; const float3 movement = {0.0f, -pedestal_speed * value * (1.0f / 60.0f), 0.0f}; @@ -342,14 +345,14 @@ void setup_controls(game::context* ctx) ); // Mouse rotate - ctx->camera_control_mouse_rotate->set_activated_callback + ctx->controls["mouse_rotate"]->set_activated_callback ( [ctx]() { ctx->app->set_relative_mouse_mode(true); } ); - ctx->camera_control_mouse_rotate->set_deactivated_callback + ctx->controls["mouse_rotate"]->set_deactivated_callback ( [ctx]() { @@ -358,11 +361,11 @@ void setup_controls(game::context* ctx) ); // Pan left - ctx->camera_control_mouse_left->set_active_callback + ctx->controls["mouse_left"]->set_active_callback ( - [ctx, three_dof_eid, pan_speed](float value) + [ctx, three_dof_eid, pan_speed, mouse_rotate](float value) { - if (!ctx->camera_control_mouse_rotate->is_active()) + if (!mouse_rotate->is_active()) return; auto& three_dof = ctx->entity_registry->get(three_dof_eid); @@ -371,11 +374,11 @@ void setup_controls(game::context* ctx) ); // Pan right - ctx->camera_control_mouse_right->set_active_callback + ctx->controls["mouse_right"]->set_active_callback ( - [ctx, three_dof_eid, pan_speed](float value) + [ctx, three_dof_eid, pan_speed, mouse_rotate](float value) { - if (!ctx->camera_control_mouse_rotate->is_active()) + if (!mouse_rotate->is_active()) return; auto& three_dof = ctx->entity_registry->get(three_dof_eid); @@ -384,11 +387,11 @@ void setup_controls(game::context* ctx) ); // Tilt up - ctx->camera_control_mouse_up->set_active_callback + ctx->controls["mouse_up"]->set_active_callback ( - [ctx, three_dof_eid, tilt_speed](float value) + [ctx, three_dof_eid, tilt_speed, mouse_rotate](float value) { - if (!ctx->camera_control_mouse_rotate->is_active()) + if (!mouse_rotate->is_active()) return; auto& three_dof = ctx->entity_registry->get(three_dof_eid); @@ -398,11 +401,11 @@ void setup_controls(game::context* ctx) ); // Tilt down - ctx->camera_control_mouse_down->set_active_callback + ctx->controls["mouse_down"]->set_active_callback ( - [ctx, three_dof_eid, tilt_speed](float value) + [ctx, three_dof_eid, tilt_speed, mouse_rotate](float value) { - if (!ctx->camera_control_mouse_rotate->is_active()) + if (!mouse_rotate->is_active()) return; auto& three_dof = ctx->entity_registry->get(three_dof_eid); diff --git a/src/game/states/loading.cpp b/src/game/states/loading.cpp index b75998d..27598b7 100644 --- a/src/game/states/loading.cpp +++ b/src/game/states/loading.cpp @@ -46,11 +46,16 @@ #include "resources/resource-manager.hpp" #include "scene/ambient-light.hpp" #include "scene/directional-light.hpp" +#include "resources/config-file.hpp" +#include "utility/timestamp.hpp" namespace game { namespace state { namespace loading { +/// Creates or loads control configuration +static void load_controls(game::context* ctx); + /// Creates the universe and solar system. static void cosmogenesis(game::context* ctx); @@ -71,6 +76,18 @@ static void colonigenesis(game::context* ctx); void enter(game::context* ctx) { + // Load controls + ctx->logger->push_task("Loading controls"); + try + { + load_controls(ctx); + } + catch (...) + { + ctx->logger->pop_task(EXIT_FAILURE); + } + ctx->logger->pop_task(EXIT_SUCCESS); + // Create universe ctx->logger->push_task("Creating the universe"); try @@ -106,6 +123,172 @@ void enter(game::context* ctx) void exit(game::context* ctx) {} +void load_controls(game::context* ctx) +{ + // Toggle fullscreen + if (ctx->controls.find("toggle_fullscreen") == ctx->controls.end()) + { + input::control* control = new input::control(); + ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::f11)); + ctx->controls["toggle_fullscreen"] = control; + } + ctx->controls["toggle_fullscreen"]->set_activated_callback + ( + [ctx]() + { + bool fullscreen = !ctx->app->is_fullscreen(); + + ctx->app->set_fullscreen(fullscreen); + + if (!fullscreen) + { + int2 resolution = ctx->config->get("windowed_resolution"); + ctx->app->resize_window(resolution.x, resolution.y); + } + + ctx->config->set("fullscreen", (fullscreen) ? 1 : 0); + } + ); + + // Screenshot + if (ctx->controls.find("screenshot") == ctx->controls.end()) + { + input::control* control = new input::control(); + ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::f12)); + ctx->controls["screenshot"] = control; + } + ctx->controls["screenshot"]->set_activated_callback + ( + [ctx]() + { + std::string path = ctx->screenshots_path + "antkeeper-" + timestamp() + ".png"; + ctx->app->save_frame(path); + } + ); + + // Menu back + if (ctx->controls.find("menu_back") == ctx->controls.end()) + { + input::control* control = new input::control(); + ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::escape)); + ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::backspace)); + ctx->controls["menu_back"] = control; + } + ctx->controls["menu_back"]->set_activated_callback + ( + std::bind(&application::close, ctx->app, 0) + ); + + // Dolly forward + if (ctx->controls.find("dolly_forward") == ctx->controls.end()) + { + input::control* control = new input::control(); + ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::w)); + ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(control, nullptr, input::game_controller_axis::left_y, true)); + ctx->controls["dolly_forward"] = control; + } + + // Dolly backward + if (ctx->controls.find("dolly_backward") == ctx->controls.end()) + { + input::control* control = new input::control(); + ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::s)); + ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(control, nullptr, input::game_controller_axis::left_y, false)); + ctx->controls["dolly_backward"] = control; + } + + // Truck left + if (ctx->controls.find("truck_left") == ctx->controls.end()) + { + input::control* control = new input::control(); + ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::a)); + ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(control, nullptr, input::game_controller_axis::left_x, true)); + ctx->controls["truck_left"] = control; + } + + // Truck right + if (ctx->controls.find("truck_right") == ctx->controls.end()) + { + input::control* control = new input::control(); + ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::d)); + ctx->input_event_router->add_mapping(input::game_controller_axis_mapping(control, nullptr, input::game_controller_axis::left_x, false)); + ctx->controls["truck_right"] = control; + } + + // Pedestal up + if (ctx->controls.find("pedestal_up") == ctx->controls.end()) + { + input::control* control = new input::control(); + ctx->input_event_router->add_mapping(input::mouse_wheel_mapping(control, nullptr, input::mouse_wheel_axis::positive_y)); + ctx->controls["pedestal_up"] = control; + } + + // Pedestal down + if (ctx->controls.find("pedestal_down") == ctx->controls.end()) + { + input::control* control = new input::control(); + ctx->input_event_router->add_mapping(input::mouse_wheel_mapping(control, nullptr, input::mouse_wheel_axis::negative_y)); + ctx->controls["pedestal_down"] = control; + } + + // Move slow + if (ctx->controls.find("move_slow") == ctx->controls.end()) + { + input::control* control = new input::control(); + ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::left_ctrl)); + ctx->controls["move_slow"] = control; + } + + // Move fast + if (ctx->controls.find("move_fast") == ctx->controls.end()) + { + input::control* control = new input::control(); + ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::left_shift)); + ctx->controls["move_fast"] = control; + } + + // Mouse rotate + if (ctx->controls.find("mouse_rotate") == ctx->controls.end()) + { + input::control* control = new input::control(); + ctx->input_event_router->add_mapping(input::mouse_button_mapping(control, nullptr, 3)); + ctx->input_event_router->add_mapping(input::key_mapping(control, nullptr, input::scancode::left_alt)); + ctx->controls["mouse_rotate"] = control; + } + + // Mouse left + if (ctx->controls.find("mouse_left") == ctx->controls.end()) + { + input::control* control = new input::control(); + ctx->input_event_router->add_mapping(input::mouse_motion_mapping(control, nullptr, input::mouse_motion_axis::negative_x)); + ctx->controls["mouse_left"] = control; + } + + // Mouse right + if (ctx->controls.find("mouse_right") == ctx->controls.end()) + { + input::control* control = new input::control(); + ctx->input_event_router->add_mapping(input::mouse_motion_mapping(control, nullptr, input::mouse_motion_axis::positive_x)); + ctx->controls["mouse_right"] = control; + } + + // Mouse up + if (ctx->controls.find("mouse_up") == ctx->controls.end()) + { + input::control* control = new input::control(); + ctx->input_event_router->add_mapping(input::mouse_motion_mapping(control, nullptr, input::mouse_motion_axis::negative_y)); + ctx->controls["mouse_up"] = control; + } + + // Mouse down + if (ctx->controls.find("mouse_down") == ctx->controls.end()) + { + input::control* control = new input::control(); + ctx->input_event_router->add_mapping(input::mouse_motion_mapping(control, nullptr, input::mouse_motion_axis::positive_y)); + ctx->controls["mouse_down"] = control; + } +} + void cosmogenesis(game::context* ctx) { // Init time diff --git a/src/renderer/passes/material-pass.cpp b/src/renderer/passes/material-pass.cpp index 7b83499..d2a5d3e 100644 --- a/src/renderer/passes/material-pass.cpp +++ b/src/renderer/passes/material-pass.cpp @@ -55,7 +55,6 @@ material_pass::material_pass(gl::rasterizer* rasterizer, const gl::framebuffer* fallback_material(nullptr), time_tween(nullptr), mouse_position({0.0f, 0.0f}), - focal_point_tween(nullptr), shadow_map_pass(nullptr), shadow_map(nullptr) { @@ -122,7 +121,6 @@ void material_pass::render(render_context* context) const float time = time_tween->interpolate(context->alpha); const float3& camera_position = context->camera_transform.translation; - float3 focal_point = (focal_point_tween) ? focal_point_tween->interpolate(context->alpha) : float3{0, 0, 0}; float4x4 view = context->camera->get_view_tween().interpolate(context->alpha); float4x4 projection = context->camera->get_projection_tween().interpolate(context->alpha); float4x4 view_projection = projection * view; @@ -492,8 +490,6 @@ void material_pass::render(render_context* context) const parameters->spot_light_attenuations->upload(0, spot_light_attenuations, spot_light_count); if (parameters->spot_light_cutoffs) parameters->spot_light_cutoffs->upload(0, spot_light_cutoffs, spot_light_count); - if (parameters->focal_point) - parameters->focal_point->upload(focal_point); if (parameters->shadow_map_directional && shadow_map) parameters->shadow_map_directional->upload(shadow_map); @@ -548,11 +544,6 @@ void material_pass::set_time_tween(const tween* time) this->time_tween = time; } -void material_pass::set_focal_point_tween(const tween* focal_point) -{ - this->focal_point_tween = focal_point; -} - const material_pass::parameter_set* material_pass::load_parameter_set(const gl::shader_program* program) const { // Allocate a new parameter set @@ -592,7 +583,6 @@ const material_pass::parameter_set* material_pass::load_parameter_set(const gl:: parameters->spot_light_directions = program->get_input("spot_light_directions"); parameters->spot_light_attenuations = program->get_input("spot_light_attenuations"); parameters->spot_light_cutoffs = program->get_input("spot_light_cutoffs"); - parameters->focal_point = program->get_input("focal_point"); parameters->shadow_map_directional = program->get_input("shadow_map_directional"); parameters->shadow_splits_directional = program->get_input("shadow_splits_directional"); parameters->shadow_matrices_directional = program->get_input("shadow_matrices_directional"); diff --git a/src/renderer/passes/material-pass.hpp b/src/renderer/passes/material-pass.hpp index f0b5fc1..e9d7d9a 100644 --- a/src/renderer/passes/material-pass.hpp +++ b/src/renderer/passes/material-pass.hpp @@ -52,8 +52,6 @@ public: /// Sets the time tween, which is interpolated between updates void set_time_tween(const tween* time); - void set_focal_point_tween(const tween* focal_point); - const ::shadow_map_pass* shadow_map_pass; const gl::texture_2d* shadow_map; @@ -100,8 +98,6 @@ private: const gl::shader_input* spot_light_attenuations; const gl::shader_input* spot_light_cutoffs; - const gl::shader_input* focal_point; - const gl::shader_input* shadow_map_directional; const gl::shader_input* shadow_splits_directional; const gl::shader_input* shadow_matrices_directional; @@ -113,7 +109,6 @@ private: const material* fallback_material; const tween* time_tween; float2 mouse_position; - const tween* focal_point_tween; int max_ambient_light_count; int max_point_light_count; @@ -142,4 +137,3 @@ private: }; #endif // ANTKEEPER_MATERIAL_PASS_HPP -