From 6c2abde5c99a236453b795abaa6d7d70105e31f7 Mon Sep 17 00:00:00 2001 From: untodesu Date: Fri, 26 Dec 2025 14:50:33 +0500 Subject: Just a big Ctrl+H refactoring --- src/game/client/config/gamepad_axis.cc | 4 +- src/game/client/config/gamepad_button.cc | 4 +- src/game/client/const.hh | 8 +- src/game/client/entity/camera.cc | 69 +++-- src/game/client/entity/camera.hh | 12 +- src/game/client/entity/factory.cc | 18 +- src/game/client/entity/factory.hh | 9 +- src/game/client/entity/interpolation.cc | 7 +- src/game/client/entity/interpolation.hh | 4 +- src/game/client/entity/listener.cc | 12 +- src/game/client/entity/listener.hh | 4 +- src/game/client/entity/player_look.cc | 24 +- src/game/client/entity/player_look.hh | 4 +- src/game/client/entity/player_move.cc | 36 +-- src/game/client/entity/player_move.hh | 4 +- src/game/client/entity/sound_emitter.cc | 14 +- src/game/client/entity/sound_emitter.hh | 3 - src/game/client/experiments.cc | 30 +-- src/game/client/game.cc | 350 ++++++++++++------------ src/game/client/globals.cc | 4 +- src/game/client/globals.hh | 10 +- src/game/client/gui/CMakeLists.txt | 8 +- src/game/client/gui/background.cc | 14 +- src/game/client/gui/background.hh | 4 +- src/game/client/gui/bother.cc | 12 +- src/game/client/gui/bother.hh | 7 +- src/game/client/gui/chat.cc | 30 +-- src/game/client/gui/chat.hh | 8 +- src/game/client/gui/crosshair.cc | 6 +- src/game/client/gui/crosshair.hh | 4 +- src/game/client/gui/direct_connection.cc | 20 +- src/game/client/gui/direct_connection.hh | 4 +- src/game/client/gui/hotbar.cc | 54 ++-- src/game/client/gui/hotbar.hh | 17 +- src/game/client/gui/imdraw_ext.cc | 34 --- src/game/client/gui/imdraw_ext.hh | 17 -- src/game/client/gui/imutils_button.cc | 95 +++++++ src/game/client/gui/imutils_button.hh | 14 + src/game/client/gui/imutils_popup.cc | 56 ++++ src/game/client/gui/imutils_popup.hh | 11 + src/game/client/gui/imutils_text.cc | 46 ++++ src/game/client/gui/imutils_text.hh | 9 + src/game/client/gui/language.cc | 38 +-- src/game/client/gui/language.hh | 19 +- src/game/client/gui/main_menu.cc | 190 ++++++------- src/game/client/gui/main_menu.hh | 4 +- src/game/client/gui/message_box.cc | 20 +- src/game/client/gui/message_box.hh | 11 +- src/game/client/gui/metrics.cc | 28 +- src/game/client/gui/metrics.hh | 4 +- src/game/client/gui/play_menu.cc | 65 +++-- src/game/client/gui/play_menu.hh | 4 +- src/game/client/gui/progress_bar.cc | 16 +- src/game/client/gui/progress_bar.hh | 11 +- src/game/client/gui/scoreboard.cc | 4 +- src/game/client/gui/scoreboard.hh | 4 +- src/game/client/gui/settings.cc | 434 ++++++++++++++---------------- src/game/client/gui/splash.cc | 32 +-- src/game/client/gui/splash.hh | 4 +- src/game/client/gui/status_lines.cc | 38 +-- src/game/client/gui/status_lines.hh | 11 +- src/game/client/gui/window_title.cc | 2 +- src/game/client/gui/window_title.hh | 4 +- src/game/client/io/gamepad.cc | 70 ++--- src/game/client/io/gamepad.hh | 14 +- src/game/client/io/glfw.hh | 3 - src/game/client/main.cc | 26 +- src/game/client/program.cc | 4 +- src/game/client/receive.cc | 34 +-- src/game/client/resource/sound_effect.cc | 2 +- src/game/client/screenshot.cc | 6 +- src/game/client/session.cc | 60 ++--- src/game/client/sound/sound.cc | 2 +- src/game/client/toggles.cc | 10 +- src/game/client/world/chunk_mesher.cc | 125 +++++---- src/game/client/world/chunk_mesher.hh | 7 +- src/game/client/world/chunk_quad.hh | 6 - src/game/client/world/chunk_renderer.cc | 32 +-- src/game/client/world/chunk_renderer.hh | 4 +- src/game/client/world/chunk_vbo.hh | 3 - src/game/client/world/chunk_visibility.cc | 14 +- src/game/client/world/chunk_visibility.hh | 4 +- src/game/client/world/outline.cc | 18 +- src/game/client/world/outline.hh | 8 +- src/game/client/world/player_target.cc | 48 ++-- src/game/client/world/player_target.hh | 8 +- src/game/client/world/skybox.cc | 6 +- src/game/client/world/skybox.hh | 8 +- src/game/client/world/voxel_anims.cc | 19 +- src/game/client/world/voxel_anims.hh | 8 +- src/game/client/world/voxel_atlas.cc | 22 +- src/game/client/world/voxel_atlas.hh | 15 +- src/game/client/world/voxel_sounds.cc | 18 +- src/game/client/world/voxel_sounds.hh | 8 +- 94 files changed, 1360 insertions(+), 1295 deletions(-) delete mode 100644 src/game/client/gui/imdraw_ext.cc delete mode 100644 src/game/client/gui/imdraw_ext.hh create mode 100644 src/game/client/gui/imutils_button.cc create mode 100644 src/game/client/gui/imutils_button.hh create mode 100644 src/game/client/gui/imutils_popup.cc create mode 100644 src/game/client/gui/imutils_popup.hh create mode 100644 src/game/client/gui/imutils_text.cc create mode 100644 src/game/client/gui/imutils_text.hh (limited to 'src/game/client') diff --git a/src/game/client/config/gamepad_axis.cc b/src/game/client/config/gamepad_axis.cc index c6f9e3d..4eb36a9 100644 --- a/src/game/client/config/gamepad_axis.cc +++ b/src/game/client/config/gamepad_axis.cc @@ -30,7 +30,7 @@ static std::string_view get_axis_name(int axis) return UNKNOWN_AXIS_NAME; } -config::GamepadAxis::GamepadAxis(void) : GamepadAxis(io::INVALID_GAMEPAD_AXIS, false) +config::GamepadAxis::GamepadAxis(void) : GamepadAxis(INVALID_GAMEPAD_AXIS, false) { } @@ -66,7 +66,7 @@ void config::GamepadAxis::set(std::string_view value) } m_inverted = false; - m_gamepad_axis = io::INVALID_GAMEPAD_AXIS; + m_gamepad_axis = INVALID_GAMEPAD_AXIS; m_name = UNKNOWN_AXIS_NAME; m_full_string = std::format("{}:{}", m_name, m_inverted ? 1U : 0U); } diff --git a/src/game/client/config/gamepad_button.cc b/src/game/client/config/gamepad_button.cc index 07e4457..d3f8a25 100644 --- a/src/game/client/config/gamepad_button.cc +++ b/src/game/client/config/gamepad_button.cc @@ -39,7 +39,7 @@ static std::string_view get_button_name(int button) config::GamepadButton::GamepadButton(void) { - m_gamepad_button = io::INVALID_GAMEPAD_BUTTON; + m_gamepad_button = INVALID_GAMEPAD_BUTTON; m_name = UNKNOWN_BUTTON_NAME; } @@ -64,7 +64,7 @@ void config::GamepadButton::set(std::string_view value) } } - m_gamepad_button = io::INVALID_GAMEPAD_BUTTON; + m_gamepad_button = INVALID_GAMEPAD_BUTTON; m_name = UNKNOWN_BUTTON_NAME; } diff --git a/src/game/client/const.hh b/src/game/client/const.hh index 461b500..709c0a1 100644 --- a/src/game/client/const.hh +++ b/src/game/client/const.hh @@ -7,11 +7,11 @@ // use this key for conventional gameplay things constexpr static int DEBUG_KEY = GLFW_KEY_F3; -constexpr static int BASE_WIDTH = 320; -constexpr static int BASE_HEIGHT = 240; +constexpr static int BASE_WIDTH = 640; +constexpr static int BASE_HEIGHT = 480; -constexpr static int MIN_WIDTH = 2 * BASE_WIDTH; -constexpr static int MIN_HEIGHT = 2 * BASE_HEIGHT; +constexpr static int MIN_WIDTH = BASE_WIDTH; +constexpr static int MIN_HEIGHT = BASE_HEIGHT; constexpr static int DEFAULT_WIDTH = 720; constexpr static int DEFAULT_HEIGHT = 480; diff --git a/src/game/client/entity/camera.cc b/src/game/client/entity/camera.cc index 3badd9d..2009066 100644 --- a/src/game/client/entity/camera.cc +++ b/src/game/client/entity/camera.cc @@ -23,23 +23,23 @@ #include "client/session.hh" #include "client/toggles.hh" -config::Float entity::camera::roll_angle(2.0f, 0.0f, 4.0f); -config::Float entity::camera::vertical_fov(90.0f, 45.0f, 110.0f); -config::Unsigned entity::camera::view_distance(16U, 4U, 32U); +config::Float camera::roll_angle(2.0f, 0.0f, 4.0f); +config::Float camera::vertical_fov(90.0f, 45.0f, 110.0f); +config::Unsigned camera::view_distance(16U, 4U, 32U); -glm::fvec3 entity::camera::angles; -glm::fvec3 entity::camera::direction; -glm::fmat4x4 entity::camera::matrix; -chunk_pos entity::camera::position_chunk; -glm::fvec3 entity::camera::position_local; +glm::fvec3 camera::angles; +glm::fvec3 camera::direction; +glm::fmat4x4 camera::matrix; +chunk_pos camera::position_chunk; +glm::fvec3 camera::position_local; static void reset_camera(void) { - entity::camera::angles = glm::fvec3(0.0f, 0.0f, 0.0f); - entity::camera::direction = DIR_FORWARD; - entity::camera::matrix = glm::identity(); - entity::camera::position_chunk = chunk_pos(0, 0, 0); - entity::camera::position_local = glm::fvec3(0.0f, 0.0f, 0.0f); + camera::angles = glm::fvec3(0.0f, 0.0f, 0.0f); + camera::direction = DIR_FORWARD; + camera::matrix = glm::identity(); + camera::position_chunk = chunk_pos(0, 0, 0); + camera::position_local = glm::fvec3(0.0f, 0.0f, 0.0f); } // Gracefully contributed by PQCraft himself in 2024 @@ -65,50 +65,49 @@ static glm::fmat4x4 platinumsrc_viewmatrix(const glm::fvec3& position, const glm return result; } -void entity::camera::init(void) +void camera::init(void) { - globals::client_config.add_value("camera.roll_angle", entity::camera::roll_angle); - globals::client_config.add_value("camera.vertical_fov", entity::camera::vertical_fov); - globals::client_config.add_value("camera.view_distance", entity::camera::view_distance); + globals::client_config.add_value("camera.roll_angle", camera::roll_angle); + globals::client_config.add_value("camera.vertical_fov", camera::vertical_fov); + globals::client_config.add_value("camera.view_distance", camera::view_distance); - settings::add_slider(1, entity::camera::vertical_fov, settings_location::GENERAL, "camera.vertical_fov", true, "%.0f"); - settings::add_slider(0, entity::camera::view_distance, settings_location::VIDEO, "camera.view_distance", false); - settings::add_slider(10, entity::camera::roll_angle, settings_location::VIDEO, "camera.roll_angle", true, "%.01f"); + settings::add_slider(1, camera::vertical_fov, settings_location::GENERAL, "camera.vertical_fov", true, "%.0f"); + settings::add_slider(0, camera::view_distance, settings_location::VIDEO, "camera.view_distance", false); + settings::add_slider(10, camera::roll_angle, settings_location::VIDEO, "camera.roll_angle", true, "%.01f"); reset_camera(); } -void entity::camera::update(void) +void camera::update(void) { if(!session::is_ingame()) { reset_camera(); return; } - const auto& head = globals::dimension->entities.get(globals::player); - const auto& transform = globals::dimension->entities.get(globals::player); - const auto& velocity = globals::dimension->entities.get(globals::player); + const auto& head = globals::dimension->entities.get(globals::player); + const auto& transform = globals::dimension->entities.get(globals::player); + const auto& velocity = globals::dimension->entities.get(globals::player); - entity::camera::angles = transform.angles + head.angles; - entity::camera::position_chunk = transform.chunk; - entity::camera::position_local = transform.local + head.offset; + camera::angles = transform.angles + head.angles; + camera::position_chunk = transform.chunk; + camera::position_local = transform.local + head.offset; glm::fvec3 right_vector, up_vector; - math::vectors(entity::camera::angles, &entity::camera::direction, &right_vector, &up_vector); + math::vectors(camera::angles, &camera::direction, &right_vector, &up_vector); - auto client_angles = entity::camera::angles; + auto client_angles = camera::angles; if(!toggles::get(TOGGLE_PM_FLIGHT)) { // Apply the quake-like view rolling - client_angles[2] = math::radians(-entity::camera::roll_angle.get_value() - * glm::dot(velocity.value / PMOVE_MAX_SPEED_GROUND, right_vector)); + client_angles[2] = math::radians(-camera::roll_angle.get_value() * glm::dot(velocity.value / PMOVE_MAX_SPEED_GROUND, right_vector)); } const auto z_near = 0.01f; - const auto z_far = 1.25f * static_cast(CHUNK_SIZE * entity::camera::view_distance.get_value()); + const auto z_far = 1.25f * static_cast(CHUNK_SIZE * camera::view_distance.get_value()); - auto proj = glm::perspective(math::radians(entity::camera::vertical_fov.get_value()), globals::aspect, z_near, z_far); - auto view = platinumsrc_viewmatrix(entity::camera::position_local, client_angles); + auto proj = glm::perspective(math::radians(camera::vertical_fov.get_value()), globals::aspect, z_near, z_far); + auto view = platinumsrc_viewmatrix(camera::position_local, client_angles); - entity::camera::matrix = proj * view; + camera::matrix = proj * view; } diff --git a/src/game/client/entity/camera.hh b/src/game/client/entity/camera.hh index 67baf72..526bc91 100644 --- a/src/game/client/entity/camera.hh +++ b/src/game/client/entity/camera.hh @@ -8,24 +8,24 @@ class Float; class Unsigned; } // namespace config -namespace entity::camera +namespace camera { extern config::Float roll_angle; extern config::Float vertical_fov; extern config::Unsigned view_distance; -} // namespace entity::camera +} // namespace camera -namespace entity::camera +namespace camera { extern glm::fvec3 angles; extern glm::fvec3 direction; extern glm::fmat4x4 matrix; extern chunk_pos position_chunk; extern glm::fvec3 position_local; -} // namespace entity::camera +} // namespace camera -namespace entity::camera +namespace camera { void init(void); void update(void); -} // namespace entity::camera +} // namespace camera diff --git a/src/game/client/entity/factory.cc b/src/game/client/entity/factory.cc index f6f6079..f4724c5 100644 --- a/src/game/client/entity/factory.cc +++ b/src/game/client/entity/factory.cc @@ -12,19 +12,19 @@ #include "client/globals.hh" -void entity::client::create_player(world::Dimension* dimension, entt::entity entity) +void client::create_player(Dimension* dimension, entt::entity entity) { - entity::shared::create_player(dimension, entity); + shared::create_player(dimension, entity); - const auto& head = dimension->entities.get(entity); - dimension->entities.emplace_or_replace(entity, head); - dimension->entities.emplace_or_replace(entity, head); + const auto& head = dimension->entities.get(entity); + dimension->entities.emplace_or_replace(entity, head); + dimension->entities.emplace_or_replace(entity, head); - const auto& transform = dimension->entities.get(entity); - dimension->entities.emplace_or_replace(entity, transform); - dimension->entities.emplace_or_replace(entity, transform); + const auto& transform = dimension->entities.get(entity); + dimension->entities.emplace_or_replace(entity, transform); + dimension->entities.emplace_or_replace(entity, transform); if(globals::sound_ctx) { - dimension->entities.emplace_or_replace(entity); + dimension->entities.emplace_or_replace(entity); } } diff --git a/src/game/client/entity/factory.hh b/src/game/client/entity/factory.hh index 63e6e44..776a0b8 100644 --- a/src/game/client/entity/factory.hh +++ b/src/game/client/entity/factory.hh @@ -1,11 +1,8 @@ #pragma once -namespace world -{ class Dimension; -} // namespace world -namespace entity::client +namespace client { -void create_player(world::Dimension* dimension, entt::entity entity); -} // namespace entity::client +void create_player(Dimension* dimension, entt::entity entity); +} // namespace client diff --git a/src/game/client/entity/interpolation.cc b/src/game/client/entity/interpolation.cc index a9a3349..69fd487 100644 --- a/src/game/client/entity/interpolation.cc +++ b/src/game/client/entity/interpolation.cc @@ -15,8 +15,7 @@ static void transform_interpolate(float alpha) { - auto group = globals::dimension->entities.group( - entt::get); + auto group = globals::dimension->entities.group(entt::get); for(auto [entity, interp, current, previous] : group.each()) { interp.angles[0] = glm::mix(previous.angles[0], current.angles[0], alpha); @@ -41,7 +40,7 @@ static void transform_interpolate(float alpha) static void head_interpolate(float alpha) { - auto group = globals::dimension->entities.group(entt::get); + auto group = globals::dimension->entities.group(entt::get); for(auto [entity, interp, current, previous] : group.each()) { interp.angles[0] = glm::mix(previous.angles[0], current.angles[0], alpha); @@ -54,7 +53,7 @@ static void head_interpolate(float alpha) } } -void entity::interpolation::update(void) +void interpolation::update(void) { if(globals::dimension) { auto alpha = static_cast(globals::fixed_accumulator) / static_cast(globals::fixed_frametime_us); diff --git a/src/game/client/entity/interpolation.hh b/src/game/client/entity/interpolation.hh index 6935fe8..8fb0db1 100644 --- a/src/game/client/entity/interpolation.hh +++ b/src/game/client/entity/interpolation.hh @@ -1,6 +1,6 @@ #pragma once -namespace entity::interpolation +namespace interpolation { void update(void); -} // namespace entity::interpolation +} // namespace interpolation diff --git a/src/game/client/entity/listener.cc b/src/game/client/entity/listener.cc index a79e8a5..991f014 100644 --- a/src/game/client/entity/listener.cc +++ b/src/game/client/entity/listener.cc @@ -18,19 +18,19 @@ #include "client/globals.hh" #include "client/session.hh" -void entity::listener::update(void) +void listener::update(void) { if(session::is_ingame()) { - const auto& velocity = globals::dimension->entities.get(globals::player).value; - const auto& position = entity::camera::position_local; + const auto& velocity = globals::dimension->entities.get(globals::player).value; + const auto& position = camera::position_local; alListener3f(AL_POSITION, position.x, position.y, position.z); alListener3f(AL_VELOCITY, velocity.x, velocity.y, velocity.z); float orientation[6]; - orientation[0] = entity::camera::direction.x; - orientation[1] = entity::camera::direction.y; - orientation[2] = entity::camera::direction.z; + orientation[0] = camera::direction.x; + orientation[1] = camera::direction.y; + orientation[2] = camera::direction.z; orientation[3] = DIR_UP.x; orientation[4] = DIR_UP.y; orientation[5] = DIR_UP.z; diff --git a/src/game/client/entity/listener.hh b/src/game/client/entity/listener.hh index 594cde1..07d750f 100644 --- a/src/game/client/entity/listener.hh +++ b/src/game/client/entity/listener.hh @@ -1,6 +1,6 @@ #pragma once -namespace entity::listener +namespace listener { void update(void); -} // namespace entity::listener +} // namespace listener diff --git a/src/game/client/entity/player_look.cc b/src/game/client/entity/player_look.cc index 0752e78..ad0f40e 100644 --- a/src/game/client/entity/player_look.cc +++ b/src/game/client/entity/player_look.cc @@ -51,7 +51,7 @@ static glm::fvec2 last_cursor; static void add_angles(float pitch, float yaw) { if(session::is_ingame()) { - auto& head = globals::dimension->entities.get(globals::player); + auto& head = globals::dimension->entities.get(globals::player); head.angles[0] += pitch; head.angles[1] += yaw; @@ -62,13 +62,13 @@ static void add_angles(float pitch, float yaw) // Re-assigning the previous state after the current // state has been already modified is certainly a way // to circumvent the interpolation applied to anything with a head - globals::dimension->entities.emplace_or_replace(globals::player, head); + globals::dimension->entities.emplace_or_replace(globals::player, head); } } -static void on_glfw_cursor_pos(const io::GlfwCursorPosEvent& event) +static void on_glfw_cursor_pos(const GlfwCursorPosEvent& event) { - if(io::gamepad::available && io::gamepad::active.get_value()) { + if(gamepad::available && gamepad::active.get_value()) { // The player is assumed to be using // a gamepad instead of mouse and keyboard last_cursor = event.pos; @@ -88,14 +88,14 @@ static void on_glfw_cursor_pos(const io::GlfwCursorPosEvent& event) last_cursor = event.pos; } -static void on_gamepad_button(const io::GamepadButtonEvent& event) +static void on_gamepad_button(const GamepadButtonEvent& event) { if(button_fastlook.equals(event.button)) { fastlook_enabled = event.action == GLFW_PRESS; } } -void entity::player_look::init(void) +void player_look::init(void) { globals::client_config.add_value("player_look.mouse.raw_input", mouse_raw_input); globals::client_config.add_value("player_look.mouse.sensitivity", mouse_sensitivity); @@ -121,15 +121,15 @@ void entity::player_look::init(void) last_cursor.x = 0.5f * static_cast(globals::width); last_cursor.y = 0.5f * static_cast(globals::height); - globals::dispatcher.sink().connect<&on_glfw_cursor_pos>(); - globals::dispatcher.sink().connect<&on_gamepad_button>(); + globals::dispatcher.sink().connect<&on_glfw_cursor_pos>(); + globals::dispatcher.sink().connect<&on_gamepad_button>(); } -void entity::player_look::update_late(void) +void player_look::update_late(void) { - if(io::gamepad::available && io::gamepad::active.get_value() && !globals::gui_screen) { - auto pitch_value = axis_pitch.get_value(io::gamepad::state, io::gamepad::deadzone.get_value()); - auto yaw_value = axis_yaw.get_value(io::gamepad::state, io::gamepad::deadzone.get_value()); + if(gamepad::available && gamepad::active.get_value() && !globals::gui_screen) { + auto pitch_value = axis_pitch.get_value(gamepad::state, gamepad::deadzone.get_value()); + auto yaw_value = axis_yaw.get_value(gamepad::state, gamepad::deadzone.get_value()); if(fastlook_enabled) { // Fastlook allows the camera to diff --git a/src/game/client/entity/player_look.hh b/src/game/client/entity/player_look.hh index 0ae18db..9d0f1dd 100644 --- a/src/game/client/entity/player_look.hh +++ b/src/game/client/entity/player_look.hh @@ -1,7 +1,7 @@ #pragma once -namespace entity::player_look +namespace player_look { void init(void); void update_late(void); -} // namespace entity::player_look +} // namespace player_look diff --git a/src/game/client/entity/player_move.cc b/src/game/client/entity/player_move.cc index 4087b04..14d64cc 100644 --- a/src/game/client/entity/player_move.cc +++ b/src/game/client/entity/player_move.cc @@ -114,7 +114,7 @@ static glm::fvec3 pm_flight_move(const glm::fvec3& wishdir) return wishdir * PMOVE_MAX_SPEED_AIR; } -void entity::player_move::init(void) +void player_move::init(void) { movement_direction = ZERO_VEC3; @@ -153,14 +153,14 @@ void entity::player_move::init(void) settings::add_checkbox(2, enable_speedometer, settings_location::VIDEO_GUI, "player_move.enable_speedometer", true); } -void entity::player_move::fixed_update(void) +void player_move::fixed_update(void) { - const auto& head = globals::dimension->entities.get(globals::player); - auto& transform = globals::dimension->entities.get(globals::player); - auto& velocity = globals::dimension->entities.get(globals::player); + const auto& head = globals::dimension->entities.get(globals::player); + auto& transform = globals::dimension->entities.get(globals::player); + auto& velocity = globals::dimension->entities.get(globals::player); // Interpolation - preserve current component states - globals::dimension->entities.emplace_or_replace(globals::player, transform); + globals::dimension->entities.emplace_or_replace(globals::player, transform); glm::fvec3 forward, right; math::vectors(glm::fvec3(0.0f, head.angles[1], 0.0f), &forward, &right, nullptr); @@ -175,7 +175,7 @@ void entity::player_move::fixed_update(void) return; } - auto grounded = globals::dimension->entities.try_get(globals::player); + auto grounded = globals::dimension->entities.try_get(globals::player); auto velocity_horizontal = glm::fvec3(velocity.value.x, 0.0f, velocity.value.z); if(grounded) { @@ -193,7 +193,7 @@ void entity::player_move::fixed_update(void) } if(footsteps_distance >= PMOVE_FOOTSTEP_SIZE) { - if(auto effect = world::voxel_sounds::get_footsteps(grounded->surface)) { + if(auto effect = voxel_sounds::get_footsteps(grounded->surface)) { sound::play_player(effect, false, pitch_distrib(pitch_random)); } @@ -228,27 +228,27 @@ void entity::player_move::fixed_update(void) if(glm::abs(speed_change) < 0.01f) { // No considerable speed increase within // the precision we use to draw the speedometer - gui::status_lines::set(gui::STATUS_DEBUG, new_speed_text, ImVec4(0.7f, 0.7f, 0.7f, 1.0f), 1.0f); + status_lines::set(STATUS_DEBUG, new_speed_text, ImVec4(0.7f, 0.7f, 0.7f, 1.0f), 1.0f); } else if(speed_change < 0.0f) { // Speed change is negative, we are actively // slowing down; use the red color for the status line - gui::status_lines::set(gui::STATUS_DEBUG, new_speed_text, ImVec4(1.0f, 0.0f, 0.0f, 1.0f), 1.0f); + status_lines::set(STATUS_DEBUG, new_speed_text, ImVec4(1.0f, 0.0f, 0.0f, 1.0f), 1.0f); } else { // Speed change is positive, we are actively // speeding up; use the green color for the status line - gui::status_lines::set(gui::STATUS_DEBUG, new_speed_text, ImVec4(0.0f, 1.0f, 0.0f, 1.0f), 1.0f); + status_lines::set(STATUS_DEBUG, new_speed_text, ImVec4(0.0f, 1.0f, 0.0f, 1.0f), 1.0f); } } - if(auto effect = world::voxel_sounds::get_footsteps(grounded->surface)) { + if(auto effect = voxel_sounds::get_footsteps(grounded->surface)) { sound::play_player(effect, false, 1.0f); } } } -void entity::player_move::update_late(void) +void player_move::update_late(void) { movement_direction = ZERO_VEC3; @@ -258,17 +258,17 @@ void entity::player_move::update_late(void) return; } - if(io::gamepad::available && io::gamepad::active.get_value()) { - if(button_move_down.is_pressed(io::gamepad::state)) { + if(gamepad::available && gamepad::active.get_value()) { + if(button_move_down.is_pressed(gamepad::state)) { movement_direction += DIR_DOWN; } - if(button_move_up.is_pressed(io::gamepad::state)) { + if(button_move_up.is_pressed(gamepad::state)) { movement_direction += DIR_UP; } - movement_direction.x += axis_move_sideways.get_value(io::gamepad::state, io::gamepad::deadzone.get_value()); - movement_direction.z -= axis_move_forward.get_value(io::gamepad::state, io::gamepad::deadzone.get_value()); + movement_direction.x += axis_move_sideways.get_value(gamepad::state, gamepad::deadzone.get_value()); + movement_direction.z -= axis_move_forward.get_value(gamepad::state, gamepad::deadzone.get_value()); } else { if(GLFW_PRESS == glfwGetKey(globals::window, key_move_forward.get_key())) { diff --git a/src/game/client/entity/player_move.hh b/src/game/client/entity/player_move.hh index 8c033cc..8516308 100644 --- a/src/game/client/entity/player_move.hh +++ b/src/game/client/entity/player_move.hh @@ -7,9 +7,9 @@ constexpr static float PMOVE_ACCELERATION_GROUND = 6.0f; constexpr static float PMOVE_FRICTION_GROUND = 10.0f; constexpr static float PMOVE_JUMP_FORCE = 0.275f; -namespace entity::player_move +namespace player_move { void init(void); void fixed_update(void); void update_late(void); -} // namespace entity::player_move +} // namespace player_move diff --git a/src/game/client/entity/sound_emitter.cc b/src/game/client/entity/sound_emitter.cc index 57909ec..79b5ab0 100644 --- a/src/game/client/entity/sound_emitter.cc +++ b/src/game/client/entity/sound_emitter.cc @@ -19,35 +19,35 @@ #include "client/globals.hh" -entity::SoundEmitter::SoundEmitter(void) +SoundEmitter::SoundEmitter(void) { alGenSources(1, &source); sound = nullptr; } -entity::SoundEmitter::~SoundEmitter(void) +SoundEmitter::~SoundEmitter(void) { alSourceStop(source); alDeleteSources(1, &source); } -void entity::SoundEmitter::update(void) +void SoundEmitter::update(void) { if(globals::dimension) { - const auto view = globals::dimension->entities.view(); + const auto view = globals::dimension->entities.view(); - const auto& pivot = entity::camera::position_chunk; + const auto& pivot = camera::position_chunk; const auto gain = glm::clamp(sound::volume_effects.get_value() * 0.01f, 0.0f, 1.0f); for(const auto [entity, emitter] : view.each()) { alSourcef(emitter.source, AL_GAIN, gain); - if(const auto transform = globals::dimension->entities.try_get(entity)) { + if(const auto transform = globals::dimension->entities.try_get(entity)) { auto position = coord::to_relative(pivot, transform->chunk, transform->local); alSource3f(emitter.source, AL_POSITION, position.x, position.y, position.z); } - if(const auto velocity = globals::dimension->entities.try_get(entity)) { + if(const auto velocity = globals::dimension->entities.try_get(entity)) { alSource3f(emitter.source, AL_VELOCITY, velocity->value.x, velocity->value.y, velocity->value.z); } diff --git a/src/game/client/entity/sound_emitter.hh b/src/game/client/entity/sound_emitter.hh index 72a3f74..6bc0846 100644 --- a/src/game/client/entity/sound_emitter.hh +++ b/src/game/client/entity/sound_emitter.hh @@ -4,8 +4,6 @@ struct SoundEffect; -namespace entity -{ struct SoundEmitter final { resource_ptr sound; ALuint source; @@ -17,4 +15,3 @@ public: public: static void update(void); }; -} // namespace entity diff --git a/src/game/client/experiments.cc b/src/game/client/experiments.cc index eb415f7..4df6f1e 100644 --- a/src/game/client/experiments.cc +++ b/src/game/client/experiments.cc @@ -19,10 +19,10 @@ #include "client/globals.hh" #include "client/session.hh" -static void on_glfw_mouse_button(const io::GlfwMouseButtonEvent& event) +static void on_glfw_mouse_button(const GlfwMouseButtonEvent& event) { if(!globals::gui_screen && session::is_ingame()) { - if((event.action == GLFW_PRESS) && world::player_target::voxel) { + if((event.action == GLFW_PRESS) && player_target::voxel) { if(event.button == GLFW_MOUSE_BUTTON_LEFT) { experiments::attack(); return; @@ -38,20 +38,20 @@ static void on_glfw_mouse_button(const io::GlfwMouseButtonEvent& event) void experiments::init(void) { - globals::dispatcher.sink().connect<&on_glfw_mouse_button>(); + globals::dispatcher.sink().connect<&on_glfw_mouse_button>(); } void experiments::init_late(void) { - gui::hotbar::slots[0] = game_items::cobblestone; - gui::hotbar::slots[1] = game_items::stone; - gui::hotbar::slots[2] = game_items::dirt; - gui::hotbar::slots[3] = game_items::grass; - gui::hotbar::slots[4] = game_items::oak_leaves; - gui::hotbar::slots[5] = game_items::oak_planks; - gui::hotbar::slots[6] = game_items::oak_log; - gui::hotbar::slots[7] = game_items::glass; - gui::hotbar::slots[8] = game_items::slime; + hotbar::slots[0] = game_items::cobblestone; + hotbar::slots[1] = game_items::stone; + hotbar::slots[2] = game_items::dirt; + hotbar::slots[3] = game_items::grass; + hotbar::slots[4] = game_items::oak_leaves; + hotbar::slots[5] = game_items::oak_planks; + hotbar::slots[6] = game_items::oak_log; + hotbar::slots[7] = game_items::glass; + hotbar::slots[8] = game_items::slime; } void experiments::shutdown(void) @@ -68,16 +68,16 @@ void experiments::update_late(void) void experiments::attack(void) { - globals::dimension->set_voxel(nullptr, world::player_target::coord); + globals::dimension->set_voxel(nullptr, player_target::coord); } void experiments::interact(void) { - auto active_item = gui::hotbar::slots[gui::hotbar::active_slot]; + auto active_item = hotbar::slots[hotbar::active_slot]; if(active_item) { if(auto place_voxel = active_item->get_place_voxel()) { - globals::dimension->set_voxel(place_voxel, world::player_target::coord + world::player_target::normal); + globals::dimension->set_voxel(place_voxel, player_target::coord + player_target::normal); return; } } diff --git a/src/game/client/game.cc b/src/game/client/game.cc index 4b8ffc8..5a67b59 100644 --- a/src/game/client/game.cc +++ b/src/game/client/game.cc @@ -100,7 +100,7 @@ static ImFont* load_font(std::string_view path, float size, ImFontConfig& font_c { std::vector font; - if(!io::read_file(path, font)) { + if(!read_file(path, font)) { spdlog::error("{}: utils::read_file failed", path); std::terminate(); } @@ -116,7 +116,7 @@ static ImFont* load_font(std::string_view path, float size, ImFontConfig& font_c return font_ptr; } -static void on_glfw_framebuffer_size(const io::GlfwFramebufferSizeEvent& event) +static void on_glfw_framebuffer_size(const GlfwFramebufferSizeEvent& event) { if(globals::world_fbo) { glDeleteRenderbuffers(1, &globals::world_fbo_depth); @@ -147,7 +147,7 @@ static void on_glfw_framebuffer_size(const io::GlfwFramebufferSizeEvent& event) } } -static void on_glfw_key(const io::GlfwKeyEvent& event) +static void on_glfw_key(const GlfwKeyEvent& event) { if(!globals::gui_keybind_ptr && hide_hud_toggle.equals(event.key) && (event.action == GLFW_PRESS)) { client_game::hide_hud = !client_game::hide_hud; @@ -175,8 +175,8 @@ void client_game::init(void) globals::font_unscii16 = load_font("fonts/unscii-16.ttf", 16.0f, font_config, ranges); globals::font_unscii8 = load_font("fonts/unscii-8.ttf", 8.0f, font_config, ranges); - gui::client_splash::init(); - gui::client_splash::render(); + client_splash::init(); + client_splash::render(); globals::client_config.add_value("game.streamer_mode", client_game::streamer_mode); globals::client_config.add_value("game.vertical_sync", client_game::vertical_sync); @@ -201,31 +201,31 @@ void client_game::init(void) std::terminate(); } - gui::language::init(); + language::init(); session::init(); - entity::player_look::init(); - entity::player_move::init(); - world::player_target::init(); + player_look::init(); + player_move::init(); + player_target::init(); - io::gamepad::init(); + gamepad::init(); - entity::camera::init(); + camera::init(); - world::voxel_anims::init(); + voxel_anims::init(); - world::outline::init(); - world::chunk_mesher::init(); - world::chunk_renderer::init(); + outline::init(); + chunk_mesher::init(); + chunk_renderer::init(); globals::world_fbo = 0; globals::world_fbo_color = 0; globals::world_fbo_depth = 0; - world::voxel_sounds::init(); + voxel_sounds::init(); - world::skybox::init(); + skybox::init(); ImGuiStyle& style = ImGui::GetStyle(); @@ -237,94 +237,91 @@ void client_game::init(void) // Rounding on elements looks cool but I am // aiming for a more or less blocky and // visually simple HiDPI-friendly UI style - style.TabRounding = 0.0f; - style.GrabRounding = 0.0f; style.ChildRounding = 0.0f; style.FrameRounding = 0.0f; + style.GrabRounding = 0.0f; style.PopupRounding = 0.0f; - style.WindowRounding = 0.0f; style.ScrollbarRounding = 0.0f; + style.TabRounding = 0.0f; + style.WindowRounding = 0.0f; - style.Colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); - style.Colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f); - style.Colors[ImGuiCol_WindowBg] = ImVec4(0.06f, 0.06f, 0.06f, 0.94f); - style.Colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); - style.Colors[ImGuiCol_PopupBg] = ImVec4(0.08f, 0.08f, 0.08f, 0.94f); style.Colors[ImGuiCol_Border] = ImVec4(0.79f, 0.79f, 0.79f, 0.50f); style.Colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + style.Colors[ImGuiCol_Button] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f); + style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f); + style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.12f, 0.12f, 0.12f, 1.00f); + style.Colors[ImGuiCol_CheckMark] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); + style.Colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.38f); + style.Colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 1.00f); style.Colors[ImGuiCol_FrameBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.54f); - style.Colors[ImGuiCol_FrameBgHovered] = ImVec4(0.36f, 0.36f, 0.36f, 0.40f); style.Colors[ImGuiCol_FrameBgActive] = ImVec4(0.63f, 0.63f, 0.63f, 0.67f); - style.Colors[ImGuiCol_TitleBg] = ImVec4(0.04f, 0.04f, 0.04f, 1.00f); - style.Colors[ImGuiCol_TitleBgActive] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f); - style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 0.51f); + style.Colors[ImGuiCol_FrameBgHovered] = ImVec4(0.36f, 0.36f, 0.36f, 0.40f); + style.Colors[ImGuiCol_Header] = ImVec4(0.00f, 0.00f, 0.00f, 0.75f); + style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f); + style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.12f, 0.12f, 0.12f, 1.00f); style.Colors[ImGuiCol_MenuBarBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f); + style.Colors[ImGuiCol_NavCursor] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + style.Colors[ImGuiCol_NavHighlight] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + style.Colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + style.Colors[ImGuiCol_NavWindowingHighlight] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + style.Colors[ImGuiCol_PlotHistogram] = ImVec4(0.00f, 1.00f, 0.20f, 1.00f); + style.Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); + style.Colors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f); + style.Colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.69f, 0.00f, 0.00f, 1.00f); + style.Colors[ImGuiCol_PopupBg] = ImVec4(0.08f, 0.08f, 0.08f, 0.94f); + style.Colors[ImGuiCol_ResizeGrip] = ImVec4(0.34f, 0.34f, 0.34f, 0.20f); + style.Colors[ImGuiCol_ResizeGripActive] = ImVec4(1.00f, 1.00f, 1.00f, 0.95f); + style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.57f, 0.57f, 0.57f, 0.67f); style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.02f, 0.02f, 0.02f, 0.53f); style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.00f, 0.00f, 0.00f, 0.75f); - style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.12f, 0.12f, 0.12f, 1.00f); style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f); - style.Colors[ImGuiCol_CheckMark] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); - style.Colors[ImGuiCol_SliderGrab] = ImVec4(0.81f, 0.81f, 0.81f, 0.75f); - style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f); - style.Colors[ImGuiCol_Button] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f); - style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.12f, 0.12f, 0.12f, 1.00f); - style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f); - style.Colors[ImGuiCol_Header] = ImVec4(0.00f, 0.00f, 0.00f, 0.75f); - style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.12f, 0.12f, 0.12f, 1.00f); - style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f); + style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.12f, 0.12f, 0.12f, 1.00f); style.Colors[ImGuiCol_Separator] = ImVec4(0.49f, 0.49f, 0.49f, 0.50f); - style.Colors[ImGuiCol_SeparatorHovered] = ImVec4(0.56f, 0.56f, 0.56f, 0.78f); style.Colors[ImGuiCol_SeparatorActive] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f); - style.Colors[ImGuiCol_ResizeGrip] = ImVec4(0.34f, 0.34f, 0.34f, 0.20f); - style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.57f, 0.57f, 0.57f, 0.67f); - style.Colors[ImGuiCol_ResizeGripActive] = ImVec4(1.00f, 1.00f, 1.00f, 0.95f); + style.Colors[ImGuiCol_SeparatorHovered] = ImVec4(0.56f, 0.56f, 0.56f, 0.78f); + style.Colors[ImGuiCol_SliderGrab] = ImVec4(0.81f, 0.81f, 0.81f, 0.75f); + style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f); style.Colors[ImGuiCol_Tab] = ImVec4(0.00f, 0.00f, 0.00f, 0.75f); - style.Colors[ImGuiCol_TabHovered] = ImVec4(0.12f, 0.12f, 0.12f, 1.00f); style.Colors[ImGuiCol_TabActive] = ImVec4(0.25f, 0.25f, 0.25f, 1.00f); - style.Colors[ImGuiCol_TabUnfocused] = ImVec4(0.13f, 0.13f, 0.13f, 0.97f); - style.Colors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.44f, 0.44f, 0.44f, 1.00f); - style.Colors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f); - style.Colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.69f, 0.00f, 0.00f, 1.00f); - style.Colors[ImGuiCol_PlotHistogram] = ImVec4(0.00f, 1.00f, 0.20f, 1.00f); - style.Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); - style.Colors[ImGuiCol_TableHeaderBg] = ImVec4(0.19f, 0.19f, 0.20f, 1.00f); - style.Colors[ImGuiCol_TableBorderStrong] = ImVec4(0.31f, 0.31f, 0.35f, 1.00f); + style.Colors[ImGuiCol_TabHovered] = ImVec4(0.12f, 0.12f, 0.12f, 1.00f); style.Colors[ImGuiCol_TableBorderLight] = ImVec4(0.23f, 0.23f, 0.25f, 1.00f); + style.Colors[ImGuiCol_TableBorderStrong] = ImVec4(0.31f, 0.31f, 0.35f, 1.00f); + style.Colors[ImGuiCol_TableHeaderBg] = ImVec4(0.19f, 0.19f, 0.20f, 1.00f); style.Colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); style.Colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.00f, 1.00f, 1.00f, 0.06f); + style.Colors[ImGuiCol_TabUnfocused] = ImVec4(0.13f, 0.13f, 0.13f, 0.97f); + style.Colors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.44f, 0.44f, 0.44f, 1.00f); + style.Colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); + style.Colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f); style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.61f, 0.61f, 0.61f, 0.35f); - style.Colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 1.00f); - style.Colors[ImGuiCol_NavHighlight] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f); - style.Colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f); - style.Colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f); - style.Colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f); - - // Making my own Game UI for Source Engine - // taught me one important thing: dimensions - // of UI elements must be calculated at semi-runtime - // so there's simply no point for an INI file. + style.Colors[ImGuiCol_TitleBg] = ImVec4(0.04f, 0.04f, 0.04f, 1.00f); + style.Colors[ImGuiCol_TitleBgActive] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f); + style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 0.51f); + style.Colors[ImGuiCol_WindowBg] = ImVec4(0.06f, 0.06f, 0.06f, 0.94f); + style.Colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.75f); + io.IniFilename = nullptr; toggles::init(); - gui::background::init(); + background::init(); - gui::scoreboard::init(); + scoreboard::init(); - gui::client_chat::init(); + client_chat::init(); - gui::bother::init(); + bother::init(); - gui::main_menu::init(); - gui::play_menu::init(); - gui::progress_bar::init(); - gui::message_box::init(); - gui::direct_connection::init(); + main_menu::init(); + play_menu::init(); + progress_bar::init(); + message_box::init(); + direct_connection::init(); - gui::crosshair::init(); - gui::hotbar::init(); - gui::metrics::init(); - gui::status_lines::init(); + crosshair::init(); + hotbar::init(); + metrics::init(); + status_lines::init(); screenshot::init(); @@ -342,8 +339,8 @@ void client_game::init(void) experiments::init(); - globals::dispatcher.sink().connect<&on_glfw_framebuffer_size>(); - globals::dispatcher.sink().connect<&on_glfw_key>(); + globals::dispatcher.sink().connect<&on_glfw_framebuffer_size>(); + globals::dispatcher.sink().connect<&on_glfw_key>(); } void client_game::init_late(void) @@ -354,13 +351,13 @@ void client_game::init_late(void) sound::init_late(); } - gui::language::init_late(); + language::init_late(); settings::init_late(); - gui::client_chat::init_late(); + client_chat::init_late(); - gui::status_lines::init_late(); + status_lines::init_late(); game_voxels::populate(); game_items::populate(); @@ -371,35 +368,35 @@ void client_game::init_late(void) // NOTE: this is very debug, early and a quite // conservative limit choice; there must be a better // way to make this limit way smaller than it currently is - for(const auto& voxel : world::voxel_registry::voxels) { + for(const auto& voxel : voxel_registry::voxels) { max_texture_count += voxel->get_default_textures().size(); - max_texture_count += voxel->get_face_textures(world::VFACE_NORTH).size(); - max_texture_count += voxel->get_face_textures(world::VFACE_SOUTH).size(); - max_texture_count += voxel->get_face_textures(world::VFACE_EAST).size(); - max_texture_count += voxel->get_face_textures(world::VFACE_WEST).size(); - max_texture_count += voxel->get_face_textures(world::VFACE_TOP).size(); - max_texture_count += voxel->get_face_textures(world::VFACE_BOTTOM).size(); - max_texture_count += voxel->get_face_textures(world::VFACE_CROSS_NWSE).size(); - max_texture_count += voxel->get_face_textures(world::VFACE_CROSS_NESW).size(); + max_texture_count += voxel->get_face_textures(VFACE_NORTH).size(); + max_texture_count += voxel->get_face_textures(VFACE_SOUTH).size(); + max_texture_count += voxel->get_face_textures(VFACE_EAST).size(); + max_texture_count += voxel->get_face_textures(VFACE_WEST).size(); + max_texture_count += voxel->get_face_textures(VFACE_TOP).size(); + max_texture_count += voxel->get_face_textures(VFACE_BOTTOM).size(); + max_texture_count += voxel->get_face_textures(VFACE_CROSS_NWSE).size(); + max_texture_count += voxel->get_face_textures(VFACE_CROSS_NESW).size(); } // UNDONE: asset packs for non-16x16 stuff - world::voxel_atlas::create(16, 16, max_texture_count); + voxel_atlas::create(16, 16, max_texture_count); - for(auto& voxel : world::voxel_registry::voxels) { + for(auto& voxel : voxel_registry::voxels) { constexpr std::array faces = { - world::VFACE_NORTH, - world::VFACE_SOUTH, - world::VFACE_EAST, - world::VFACE_WEST, - world::VFACE_TOP, - world::VFACE_BOTTOM, - world::VFACE_CROSS_NWSE, - world::VFACE_CROSS_NESW, + VFACE_NORTH, + VFACE_SOUTH, + VFACE_EAST, + VFACE_WEST, + VFACE_TOP, + VFACE_BOTTOM, + VFACE_CROSS_NWSE, + VFACE_CROSS_NESW, }; for(auto face : faces) { - if(auto strip = world::voxel_atlas::find_or_load(voxel->get_face_textures(face))) { + if(auto strip = voxel_atlas::find_or_load(voxel->get_face_textures(face))) { voxel->set_face_cache(face, strip->offset, strip->plane); continue; } @@ -409,22 +406,22 @@ void client_game::init_late(void) } } - world::voxel_atlas::generate_mipmaps(); + voxel_atlas::generate_mipmaps(); - for(auto& item : world::item_registry::items) { + for(auto& item : item_registry::items) { item->set_cached_texture(resource::load(item->get_texture(), TEXTURE_GUI_LOAD_CLAMP_S | TEXTURE_GUI_LOAD_CLAMP_T)); } experiments::init_late(); - gui::client_splash::init_late(); + client_splash::init_late(); - gui::window_title::update(); + window_title::update(); } void client_game::shutdown(void) { - world::voxel_sounds::shutdown(); + voxel_sounds::shutdown(); experiments::shutdown(); @@ -434,60 +431,60 @@ void client_game::shutdown(void) sound::shutdown(); } - gui::hotbar::shutdown(); - gui::main_menu::shutdown(); - gui::play_menu::shutdown(); + hotbar::shutdown(); + main_menu::shutdown(); + play_menu::shutdown(); - gui::bother::shutdown(); + bother::shutdown(); - gui::client_chat::shutdown(); + client_chat::shutdown(); - gui::background::shutdown(); + background::shutdown(); - gui::crosshair::shutdown(); + crosshair::shutdown(); delete globals::dimension; globals::player = entt::null; globals::dimension = nullptr; - world::item_registry::purge(); - world::voxel_registry::purge(); + item_registry::purge(); + voxel_registry::purge(); - world::voxel_atlas::destroy(); + voxel_atlas::destroy(); glDeleteRenderbuffers(1, &globals::world_fbo_depth); glDeleteTextures(1, &globals::world_fbo_color); glDeleteFramebuffers(1, &globals::world_fbo); - world::outline::shutdown(); - world::chunk_renderer::shutdown(); - world::chunk_mesher::shutdown(); + outline::shutdown(); + chunk_renderer::shutdown(); + chunk_mesher::shutdown(); enet_host_destroy(globals::client_host); } void client_game::fixed_update(void) { - entity::player_move::fixed_update(); + player_move::fixed_update(); // Only update world simulation gamesystems // if the player can actually observe all the // changes these gamesystems cause visually if(session::is_ingame()) { - entity::Collision::fixed_update(globals::dimension); - entity::Velocity::fixed_update(globals::dimension); - entity::Transform::fixed_update(globals::dimension); - entity::Gravity::fixed_update(globals::dimension); - entity::Stasis::fixed_update(globals::dimension); + Collision::fixed_update(globals::dimension); + Velocity::fixed_update(globals::dimension); + Transform::fixed_update(globals::dimension); + Gravity::fixed_update(globals::dimension); + Stasis::fixed_update(globals::dimension); } } void client_game::fixed_update_late(void) { if(session::is_ingame()) { - const auto& head = globals::dimension->entities.get(globals::player); - const auto& transform = globals::dimension->entities.get(globals::player); - const auto& velocity = globals::dimension->entities.get(globals::player); + const auto& head = globals::dimension->entities.get(globals::player); + const auto& transform = globals::dimension->entities.get(globals::player); + const auto& velocity = globals::dimension->entities.get(globals::player); protocol::EntityHead head_packet; head_packet.entity = entt::null; // ignored by server @@ -513,49 +510,42 @@ void client_game::update(void) { if(session::is_ingame()) { if(toggles::get(TOGGLE_PM_FLIGHT)) { - globals::dimension->entities.remove(globals::player); + globals::dimension->entities.remove(globals::player); } else { - globals::dimension->entities.emplace_or_replace(globals::player); + globals::dimension->entities.emplace_or_replace(globals::player); } } if(globals::sound_ctx) { sound::update(); - entity::listener::update(); + listener::update(); - entity::SoundEmitter::update(); + SoundEmitter::update(); } - entity::interpolation::update(); + interpolation::update(); - world::player_target::update(); + player_target::update(); - entity::camera::update(); + camera::update(); - world::voxel_anims::update(); + voxel_anims::update(); - world::chunk_mesher::update(); + chunk_mesher::update(); - gui::client_chat::update(); + client_chat::update(); experiments::update(); - constexpr auto half_base_width = 0.5f * static_cast(BASE_WIDTH); - constexpr auto half_base_height = 0.5f * static_cast(BASE_HEIGHT); - - auto twice_scale_x = static_cast(globals::width) / half_base_width; - auto twice_scale_y = static_cast(globals::height) / half_base_height; - - auto scale_x = glm::max(1.0f, 0.5f * glm::floor(twice_scale_x)); - auto scale_y = glm::max(1.0f, 0.5f * glm::floor(twice_scale_y)); - auto scale_min = static_cast(glm::ceil(glm::min(scale_x, scale_y))); - auto scale_int = glm::max(1U, (scale_min / 2U) * 2U); + auto scale_x = glm::max(1.0f, glm::floor(static_cast(globals::width) / static_cast(BASE_WIDTH))); + auto scale_y = glm::max(1.0f, glm::floor(static_cast(globals::height) / static_cast(BASE_HEIGHT))); + auto scale_min = glm::min(scale_x, scale_y); auto& io = ImGui::GetIO(); - io.FontGlobalScale = scale_int; - globals::gui_scale = scale_int; + io.FontGlobalScale = scale_min; + globals::gui_scale = scale_min; } void client_game::update_late(void) @@ -577,18 +567,18 @@ void client_game::update_late(void) } } - entity::player_look::update_late(); - entity::player_move::update_late(); + player_look::update_late(); + player_move::update_late(); - gui::play_menu::update_late(); + play_menu::update_late(); - gui::bother::update_late(); + bother::update_late(); experiments::update_late(); - io::gamepad::update_late(); + gamepad::update_late(); - world::chunk_visibility::update_late(); + chunk_visibility::update_late(); if(client_game::vertical_sync.get_value()) { glfwSwapInterval(1); @@ -602,22 +592,21 @@ void client_game::render(void) { glViewport(0, 0, globals::width, globals::height); glBindFramebuffer(GL_FRAMEBUFFER, globals::world_fbo); - glClearColor(world::skybox::fog_color.r, world::skybox::fog_color.g, world::skybox::fog_color.b, 1.000f); + glClearColor(skybox::fog_color.r, skybox::fog_color.g, skybox::fog_color.b, 1.000f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if(globals::dimension) { - world::chunk_renderer::render(); + chunk_renderer::render(); } glEnable(GL_DEPTH_TEST); - world::player_target::render(); + player_target::render(); if(globals::dimension) { - auto group = globals::dimension->entities.group( - entt::get); + auto group = globals::dimension->entities.group(entt::get); - world::outline::prepare(); + outline::prepare(); for(const auto [entity, collision, head, transform] : group.each()) { if(entity == globals::player) { @@ -633,8 +622,8 @@ void client_game::render(void) glm::fvec3 hull_fpos = transform.local + collision.aabb.min; glm::fvec3 look = transform.local + head.offset; - world::outline::cube(transform.chunk, hull_fpos, hull_size, 1.0f, glm::fvec4(1.0f, 0.0f, 0.0f, 1.0f)); - world::outline::line(transform.chunk, look, forward, 1.0f, glm::fvec4(0.9f, 0.9f, 0.9f, 1.0f)); + outline::cube(transform.chunk, hull_fpos, hull_size, 1.0f, glm::fvec4(1.0f, 0.0f, 0.0f, 1.0f)); + outline::line(transform.chunk, look, forward, 1.0f, glm::fvec4(0.9f, 0.9f, 0.9f, 1.0f)); } } @@ -653,7 +642,7 @@ void client_game::render(void) void client_game::layout(void) { if(!session::is_ingame()) { - gui::background::layout(); + background::layout(); } if(!globals::gui_screen || (globals::gui_screen == GUI_CHAT)) { @@ -661,47 +650,50 @@ void client_game::layout(void) // This contains Minecraft-esque debug information // about the hardware, world state and other // things that might be uesful - gui::metrics::layout(); + metrics::layout(); } } if(session::is_ingame()) { - gui::client_chat::layout(); - gui::scoreboard::layout(); + client_chat::layout(); + scoreboard::layout(); if(!globals::gui_screen && !client_game::hide_hud) { - gui::hotbar::layout(); - gui::status_lines::layout(); - gui::crosshair::layout(); + hotbar::layout(); + status_lines::layout(); + crosshair::layout(); } } if(globals::gui_screen) { if(session::is_ingame() && (globals::gui_screen != GUI_CHAT)) { - const float width_f = static_cast(globals::width); - const float height_f = static_cast(globals::height); - const ImU32 darken = ImGui::GetColorU32(ImVec4(0.00f, 0.00f, 0.00f, 0.75f)); - ImGui::GetBackgroundDrawList()->AddRectFilled(ImVec2(), ImVec2(width_f, height_f), darken); + auto width_f = static_cast(globals::width); + auto height_f = static_cast(globals::height); + auto darken = ImGui::GetColorU32(ImVec4(0.00f, 0.00f, 0.00f, 0.75f)); + auto darker = ImGui::GetColorU32(ImVec4(0.00f, 0.00f, 0.00f, 0.95f)); + auto draw_list = ImGui::GetBackgroundDrawList(); + + draw_list->AddRectFilledMultiColor({}, { width_f, height_f }, darker, darken, darken, darker); } switch(globals::gui_screen) { case GUI_MAIN_MENU: - gui::main_menu::layout(); + main_menu::layout(); break; case GUI_PLAY_MENU: - gui::play_menu::layout(); + play_menu::layout(); break; case GUI_SETTINGS: settings::layout(); break; case GUI_PROGRESS_BAR: - gui::progress_bar::layout(); + progress_bar::layout(); break; case GUI_MESSAGE_BOX: - gui::message_box::layout(); + message_box::layout(); break; case GUI_DIRECT_CONNECTION: - gui::direct_connection::layout(); + direct_connection::layout(); break; } } diff --git a/src/game/client/globals.cc b/src/game/client/globals.cc index 1932675..85e5cd6 100644 --- a/src/game/client/globals.cc +++ b/src/game/client/globals.cc @@ -6,7 +6,7 @@ #include "client/gui/gui_screen.hh" -io::ConfigMap globals::client_config; +ConfigMap globals::client_config; GLFWwindow* globals::window; @@ -30,7 +30,7 @@ std::size_t globals::num_triangles; ENetHost* globals::client_host; -world::Dimension* globals::dimension = nullptr; +Dimension* globals::dimension = nullptr; entt::entity globals::player; ImFont* globals::font_unscii16; diff --git a/src/game/client/globals.hh b/src/game/client/globals.hh index ba57a9e..a6c2de6 100644 --- a/src/game/client/globals.hh +++ b/src/game/client/globals.hh @@ -9,22 +9,16 @@ class GamepadAxis; class GamepadButton; } // namespace config -namespace io -{ class ConfigMap; -} // namespace io struct GLFWwindow; struct ImFont; -namespace world -{ class Dimension; -} // namespace world namespace globals { -extern io::ConfigMap client_config; +extern ConfigMap client_config; extern GLFWwindow* window; @@ -53,7 +47,7 @@ extern std::size_t num_triangles; extern ENetHost* client_host; -extern world::Dimension* dimension; +extern Dimension* dimension; extern entt::entity player; extern ImFont* font_unscii16; diff --git a/src/game/client/gui/CMakeLists.txt b/src/game/client/gui/CMakeLists.txt index 46d64a1..e3d8b7a 100644 --- a/src/game/client/gui/CMakeLists.txt +++ b/src/game/client/gui/CMakeLists.txt @@ -12,8 +12,12 @@ target_sources(vclient PRIVATE "${CMAKE_CURRENT_LIST_DIR}/gui_screen.hh" "${CMAKE_CURRENT_LIST_DIR}/hotbar.cc" "${CMAKE_CURRENT_LIST_DIR}/hotbar.hh" - "${CMAKE_CURRENT_LIST_DIR}/imdraw_ext.cc" - "${CMAKE_CURRENT_LIST_DIR}/imdraw_ext.hh" + "${CMAKE_CURRENT_LIST_DIR}/imutils_button.cc" + "${CMAKE_CURRENT_LIST_DIR}/imutils_button.hh" + "${CMAKE_CURRENT_LIST_DIR}/imutils_popup.cc" + "${CMAKE_CURRENT_LIST_DIR}/imutils_popup.hh" + "${CMAKE_CURRENT_LIST_DIR}/imutils_text.cc" + "${CMAKE_CURRENT_LIST_DIR}/imutils_text.hh" "${CMAKE_CURRENT_LIST_DIR}/language.cc" "${CMAKE_CURRENT_LIST_DIR}/language.hh" "${CMAKE_CURRENT_LIST_DIR}/main_menu.cc" diff --git a/src/game/client/gui/background.cc b/src/game/client/gui/background.cc index 50fef01..81fb764 100644 --- a/src/game/client/gui/background.cc +++ b/src/game/client/gui/background.cc @@ -12,9 +12,9 @@ static resource_ptr texture; -void gui::background::init(void) +void background::init(void) { - texture = resource::load("textures/gui/background.png", TEXTURE_GUI_LOAD_VFLIP); + texture = resource::load("textures/gui/background.png"); if(texture == nullptr) { spdlog::critical("background: texture load failed"); @@ -22,18 +22,14 @@ void gui::background::init(void) } } -void gui::background::shutdown(void) +void background::shutdown(void) { texture = nullptr; } -void gui::background::layout(void) +void background::layout(void) { auto viewport = ImGui::GetMainViewport(); auto draw_list = ImGui::GetBackgroundDrawList(); - - auto scaled_width = 0.75f * static_cast(globals::width / globals::gui_scale); - auto scaled_height = 0.75f * static_cast(globals::height / globals::gui_scale); - auto scale_uv = ImVec2(scaled_width / static_cast(texture->size.x), scaled_height / static_cast(texture->size.y)); - draw_list->AddImage(texture->handle, ImVec2(0.0f, 0.0f), viewport->Size, ImVec2(0.0f, 0.0f), scale_uv); + draw_list->AddImage(texture->handle, {}, viewport->Size); } diff --git a/src/game/client/gui/background.hh b/src/game/client/gui/background.hh index 5c72a3f..51497d1 100644 --- a/src/game/client/gui/background.hh +++ b/src/game/client/gui/background.hh @@ -1,8 +1,8 @@ #pragma once -namespace gui::background +namespace background { void init(void); void shutdown(void); void layout(void); -} // namespace gui::background +} // namespace background diff --git a/src/game/client/gui/bother.cc b/src/game/client/gui/bother.cc index a045284..7309bbc 100644 --- a/src/game/client/gui/bother.cc +++ b/src/game/client/gui/bother.cc @@ -28,7 +28,7 @@ static void on_status_response_packet(const protocol::StatusResponse& packet) bother_set.erase(identity); - gui::BotherResponseEvent event; + BotherResponseEvent event; event.identity = identity; event.is_server_unreachable = false; event.num_players = packet.num_players; @@ -42,7 +42,7 @@ static void on_status_response_packet(const protocol::StatusResponse& packet) enet_peer_disconnect(packet.peer, protocol::CHANNEL); } -void gui::bother::init(void) +void bother::init(void) { bother_host = enet_host_create(nullptr, BOTHER_PEERS, 1, 0, 0); bother_dispatcher.clear(); @@ -51,14 +51,14 @@ void gui::bother::init(void) bother_dispatcher.sink().connect<&on_status_response_packet>(); } -void gui::bother::shutdown(void) +void bother::shutdown(void) { enet_host_destroy(bother_host); bother_dispatcher.clear(); bother_set.clear(); } -void gui::bother::update_late(void) +void bother::update_late(void) { unsigned int free_peers = 0U; @@ -121,7 +121,7 @@ void gui::bother::update_late(void) } } -void gui::bother::ping(unsigned int identity, std::string_view host, std::uint16_t port) +void bother::ping(unsigned int identity, std::string_view host, std::uint16_t port) { if(bother_set.count(identity)) { // Already in the process @@ -143,7 +143,7 @@ void gui::bother::ping(unsigned int identity, std::string_view host, std::uint16 bother_queue.push_back(item); } -void gui::bother::cancel(unsigned int identity) +void bother::cancel(unsigned int identity) { bother_set.erase(identity); diff --git a/src/game/client/gui/bother.hh b/src/game/client/gui/bother.hh index b0355ca..ed4f84f 100644 --- a/src/game/client/gui/bother.hh +++ b/src/game/client/gui/bother.hh @@ -1,7 +1,5 @@ #pragma once -namespace gui -{ struct BotherResponseEvent final { unsigned int identity; bool is_server_unreachable; @@ -12,13 +10,12 @@ struct BotherResponseEvent final { std::uint32_t game_version_patch; std::string motd; }; -} // namespace gui -namespace gui::bother +namespace bother { void init(void); void shutdown(void); void update_late(void); void ping(unsigned int identity, std::string_view host, std::uint16_t port); void cancel(unsigned int identity); -} // namespace gui::bother +} // namespace bother diff --git a/src/game/client/gui/chat.cc b/src/game/client/gui/chat.cc index 70a1668..76260af 100644 --- a/src/game/client/gui/chat.cc +++ b/src/game/client/gui/chat.cc @@ -16,7 +16,7 @@ #include "client/config/keybind.hh" #include "client/gui/gui_screen.hh" -#include "client/gui/imdraw_ext.hh" +#include "client/gui/imutils_text.hh" #include "client/gui/language.hh" #include "client/gui/settings.hh" @@ -65,7 +65,7 @@ static void append_player_join(const std::string& sender) { GuiChatMessage message; message.spawn = globals::curtime; - message.text = std::format("{} {}", sender, gui::language::resolve("chat.client_join")); + message.text = std::format("{} {}", sender, language::resolve("chat.client_join")); message.color = ImGui::GetStyleColorVec4(ImGuiCol_DragDropTarget); history.push_back(message); @@ -78,7 +78,7 @@ static void append_player_leave(const std::string& sender, const std::string& re { GuiChatMessage message; message.spawn = globals::curtime; - message.text = std::format("{} {} ({})", sender, gui::language::resolve("chat.client_left"), gui::language::resolve(reason.c_str())); + message.text = std::format("{} {} ({})", sender, language::resolve("chat.client_left"), language::resolve(reason.c_str())); message.color = ImGui::GetStyleColorVec4(ImGuiCol_DragDropTarget); history.push_back(message); @@ -105,7 +105,7 @@ static void on_chat_message_packet(const protocol::ChatMessage& packet) } } -static void on_glfw_key(const io::GlfwKeyEvent& event) +static void on_glfw_key(const GlfwKeyEvent& event) { if(event.action == GLFW_PRESS) { if((event.key == GLFW_KEY_ENTER) && (globals::gui_screen == GUI_CHAT)) { @@ -138,7 +138,7 @@ static void on_glfw_key(const io::GlfwKeyEvent& event) } } -void gui::client_chat::init(void) +void client_chat::init(void) { globals::client_config.add_value("chat.key", key_chat); globals::client_config.add_value("chat.history_size", history_size); @@ -147,28 +147,28 @@ void gui::client_chat::init(void) settings::add_slider(1, history_size, settings_location::VIDEO_GUI, "chat.history_size", false); globals::dispatcher.sink().connect<&on_chat_message_packet>(); - globals::dispatcher.sink().connect<&on_glfw_key>(); + globals::dispatcher.sink().connect<&on_glfw_key>(); sfx_chat_message = resource::load("sounds/ui/chat_message.wav"); } -void gui::client_chat::init_late(void) +void client_chat::init_late(void) { } -void gui::client_chat::shutdown(void) +void client_chat::shutdown(void) { sfx_chat_message = nullptr; } -void gui::client_chat::update(void) +void client_chat::update(void) { while(history.size() > history_size.get_value()) { history.pop_front(); } } -void gui::client_chat::layout(void) +void client_chat::layout(void) { auto viewport = ImGui::GetMainViewport(); auto window_start = ImVec2(0.0f, 0.0f); @@ -177,7 +177,7 @@ void gui::client_chat::layout(void) ImGui::SetNextWindowPos(window_start); ImGui::SetNextWindowSize(window_size); - ImGui::PushFont(globals::font_unscii16, 8.0f); + ImGui::PushFont(globals::font_unscii16, 16.0f); if(!ImGui::Begin("###chat", nullptr, WINDOW_FLAGS)) { ImGui::End(); @@ -235,7 +235,7 @@ void gui::client_chat::layout(void) draw_list->AddRectFilled(rect_pos, rect_end, rect_col); - imdraw_ext::text_shadow_w(it->text, text_pos, text_col, shadow_col, font, draw_list, 8.0f, window_size.x); + imutils::text_wr(draw_list, it->text, text_pos, text_col, shadow_col, font, window_size.x, ImGui::GetFontSize()); ypos -= rect_size.y; } @@ -245,12 +245,12 @@ void gui::client_chat::layout(void) ImGui::PopFont(); } -void gui::client_chat::clear(void) +void client_chat::clear(void) { history.clear(); } -void gui::client_chat::refresh_timings(void) +void client_chat::refresh_timings(void) { for(auto it = history.begin(); it < history.end(); ++it) { // Reset the spawn time so the fadeout timer @@ -259,7 +259,7 @@ void gui::client_chat::refresh_timings(void) } } -void gui::client_chat::print(const std::string& text) +void client_chat::print(const std::string& text) { GuiChatMessage message = {}; message.spawn = globals::curtime; diff --git a/src/game/client/gui/chat.hh b/src/game/client/gui/chat.hh index 6a3ea33..91c3740 100644 --- a/src/game/client/gui/chat.hh +++ b/src/game/client/gui/chat.hh @@ -1,17 +1,17 @@ #pragma once -namespace gui::client_chat +namespace client_chat { void init(void); void init_late(void); void shutdown(void); void update(void); void layout(void); -} // namespace gui::client_chat +} // namespace client_chat -namespace gui::client_chat +namespace client_chat { void clear(void); void refresh_timings(void); void print(const std::string& string); -} // namespace gui::client_chat +} // namespace client_chat diff --git a/src/game/client/gui/crosshair.cc b/src/game/client/gui/crosshair.cc index 649602f..758a10b 100644 --- a/src/game/client/gui/crosshair.cc +++ b/src/game/client/gui/crosshair.cc @@ -13,7 +13,7 @@ static resource_ptr texture; -void gui::crosshair::init(void) +void crosshair::init(void) { texture = resource::load("textures/gui/hud_crosshair.png", TEXTURE_GUI_LOAD_CLAMP_S | TEXTURE_GUI_LOAD_CLAMP_T | TEXTURE_GUI_LOAD_VFLIP); @@ -24,12 +24,12 @@ void gui::crosshair::init(void) } } -void gui::crosshair::shutdown(void) +void crosshair::shutdown(void) { texture = nullptr; } -void gui::crosshair::layout(void) +void crosshair::layout(void) { auto viewport = ImGui::GetMainViewport(); auto draw_list = ImGui::GetForegroundDrawList(); diff --git a/src/game/client/gui/crosshair.hh b/src/game/client/gui/crosshair.hh index 589727e..6789e1e 100644 --- a/src/game/client/gui/crosshair.hh +++ b/src/game/client/gui/crosshair.hh @@ -1,8 +1,8 @@ #pragma once -namespace gui::crosshair +namespace crosshair { void init(void); void shutdown(void); void layout(void); -} // namespace gui::crosshair +} // namespace crosshair diff --git a/src/game/client/gui/direct_connection.cc b/src/game/client/gui/direct_connection.cc index 39ea2b5..d7cea64 100644 --- a/src/game/client/gui/direct_connection.cc +++ b/src/game/client/gui/direct_connection.cc @@ -29,7 +29,7 @@ static std::string str_password; static std::string direct_hostname; static std::string direct_password; -static void on_glfw_key(const io::GlfwKeyEvent& event) +static void on_glfw_key(const GlfwKeyEvent& event) { if((event.key == GLFW_KEY_ESCAPE) && (event.action == GLFW_PRESS)) { if(globals::gui_screen == GUI_DIRECT_CONNECTION) { @@ -39,14 +39,14 @@ static void on_glfw_key(const io::GlfwKeyEvent& event) } } -static void on_language_set(const gui::LanguageSetEvent& event) +static void on_language_set(const LanguageSetEvent& event) { - str_title = gui::language::resolve("direct_connection.title"); - str_connect = gui::language::resolve_gui("direct_connection.connect"); - str_cancel = gui::language::resolve_gui("direct_connection.cancel"); + str_title = language::resolve("direct_connection.title"); + str_connect = language::resolve_gui("direct_connection.connect"); + str_cancel = language::resolve_gui("direct_connection.cancel"); - str_hostname = gui::language::resolve("direct_connection.hostname"); - str_password = gui::language::resolve("direct_connection.password"); + str_hostname = language::resolve("direct_connection.hostname"); + str_password = language::resolve("direct_connection.password"); } static void connect_to_server(void) @@ -72,13 +72,13 @@ static void connect_to_server(void) session::connect(parsed_hostname.c_str(), parsed_port, direct_password.c_str()); } -void gui::direct_connection::init(void) +void direct_connection::init(void) { - globals::dispatcher.sink().connect<&on_glfw_key>(); + globals::dispatcher.sink().connect<&on_glfw_key>(); globals::dispatcher.sink().connect<&on_language_set>(); } -void gui::direct_connection::layout(void) +void direct_connection::layout(void) { auto viewport = ImGui::GetMainViewport(); auto window_start = ImVec2(0.25f * viewport->Size.x, 0.20f * viewport->Size.y); diff --git a/src/game/client/gui/direct_connection.hh b/src/game/client/gui/direct_connection.hh index aa02d7c..0a09a3f 100644 --- a/src/game/client/gui/direct_connection.hh +++ b/src/game/client/gui/direct_connection.hh @@ -1,7 +1,7 @@ #pragma once -namespace gui::direct_connection +namespace direct_connection { void init(void); void layout(void); -} // namespace gui::direct_connection +} // namespace direct_connection diff --git a/src/game/client/gui/hotbar.cc b/src/game/client/gui/hotbar.cc index 663f263..b38c368 100644 --- a/src/game/client/gui/hotbar.cc +++ b/src/game/client/gui/hotbar.cc @@ -19,13 +19,13 @@ #include "client/globals.hh" -constexpr static float ITEM_SIZE = 20.0f; -constexpr static float ITEM_PADDING = 2.0f; -constexpr static float SELECTOR_PADDING = 1.0f; -constexpr static float HOTBAR_PADDING = 2.0f; +constexpr static float ITEM_SIZE = 40.0f; +constexpr static float ITEM_PADDING = 4.0f; +constexpr static float SELECTOR_PADDING = 2.0f; +constexpr static float HOTBAR_PADDING = 4.0f; -unsigned int gui::hotbar::active_slot = 0U; -std::array gui::hotbar::slots = {}; +unsigned int hotbar::active_slot = 0U; +std::array hotbar::slots = {}; static config::KeyBind hotbar_keys[HOTBAR_SIZE]; @@ -40,22 +40,22 @@ static ImU32 get_color_alpha(ImGuiCol style_color, float alpha) static void update_hotbar_item(void) { - auto current_item = gui::hotbar::slots[gui::hotbar::active_slot]; + auto current_item = hotbar::slots[hotbar::active_slot]; if(current_item == nullptr) { - gui::status_lines::unset(gui::STATUS_HOTBAR); + status_lines::unset(STATUS_HOTBAR); } else { - gui::status_lines::set(gui::STATUS_HOTBAR, current_item->get_name(), ImVec4(1.0f, 1.0f, 1.0f, 1.0f), 5.0f); + status_lines::set(STATUS_HOTBAR, current_item->get_name(), ImVec4(1.0f, 1.0f, 1.0f, 1.0f), 5.0f); } } -static void on_glfw_key(const io::GlfwKeyEvent& event) +static void on_glfw_key(const GlfwKeyEvent& event) { if((event.action == GLFW_PRESS) && !globals::gui_screen) { for(unsigned int i = 0U; i < HOTBAR_SIZE; ++i) { if(hotbar_keys[i].equals(event.key)) { - gui::hotbar::active_slot = i; + hotbar::active_slot = i; update_hotbar_item(); break; } @@ -63,22 +63,22 @@ static void on_glfw_key(const io::GlfwKeyEvent& event) } } -static void on_glfw_scroll(const io::GlfwScrollEvent& event) +static void on_glfw_scroll(const GlfwScrollEvent& event) { if(!globals::gui_screen) { if(event.dy < 0.0) { - gui::hotbar::next_slot(); + hotbar::next_slot(); return; } if(event.dy > 0.0) { - gui::hotbar::prev_slot(); + hotbar::prev_slot(); return; } } } -void gui::hotbar::init(void) +void hotbar::init(void) { hotbar_keys[0].set_key(GLFW_KEY_1); hotbar_keys[1].set_key(GLFW_KEY_2); @@ -113,17 +113,17 @@ void gui::hotbar::init(void) hotbar_background = resource::load("textures/gui/hud_hotbar.png", TEXTURE_GUI_LOAD_CLAMP_S | TEXTURE_GUI_LOAD_CLAMP_T); hotbar_selector = resource::load("textures/gui/hud_selector.png", TEXTURE_GUI_LOAD_CLAMP_S | TEXTURE_GUI_LOAD_CLAMP_T); - globals::dispatcher.sink().connect<&on_glfw_key>(); - globals::dispatcher.sink().connect<&on_glfw_scroll>(); + globals::dispatcher.sink().connect<&on_glfw_key>(); + globals::dispatcher.sink().connect<&on_glfw_scroll>(); } -void gui::hotbar::shutdown(void) +void hotbar::shutdown(void) { hotbar_background = nullptr; hotbar_selector = nullptr; } -void gui::hotbar::layout(void) +void hotbar::layout(void) { auto& style = ImGui::GetStyle(); @@ -142,7 +142,7 @@ void gui::hotbar::layout(void) // Draw the hotbar selector image auto selector_padding_a = SELECTOR_PADDING * globals::gui_scale; auto selector_padding_b = SELECTOR_PADDING * globals::gui_scale * 2.0f; - auto selector_start = ImVec2(background_start.x + gui::hotbar::active_slot * item_size - selector_padding_a, + auto selector_start = ImVec2(background_start.x + hotbar::active_slot * item_size - selector_padding_a, background_start.y - selector_padding_a); auto selector_end = ImVec2(selector_start.x + item_size + selector_padding_b, selector_start.y + item_size + selector_padding_b); draw_list->AddImage(hotbar_selector->handle, selector_start, selector_end); @@ -153,7 +153,7 @@ void gui::hotbar::layout(void) // Draw individual item textures in the hotbar for(std::size_t i = 0; i < HOTBAR_SIZE; ++i) { - auto item = gui::hotbar::slots[i]; + auto item = hotbar::slots[i]; if((item == nullptr) || (item->get_cached_texture() == nullptr)) { // There's either no item in the slot @@ -167,16 +167,16 @@ void gui::hotbar::layout(void) } } -void gui::hotbar::next_slot(void) +void hotbar::next_slot(void) { - gui::hotbar::active_slot += 1U; - gui::hotbar::active_slot %= HOTBAR_SIZE; + hotbar::active_slot += 1U; + hotbar::active_slot %= HOTBAR_SIZE; update_hotbar_item(); } -void gui::hotbar::prev_slot(void) +void hotbar::prev_slot(void) { - gui::hotbar::active_slot += HOTBAR_SIZE - 1U; - gui::hotbar::active_slot %= HOTBAR_SIZE; + hotbar::active_slot += HOTBAR_SIZE - 1U; + hotbar::active_slot %= HOTBAR_SIZE; update_hotbar_item(); } diff --git a/src/game/client/gui/hotbar.hh b/src/game/client/gui/hotbar.hh index c529230..bba5e63 100644 --- a/src/game/client/gui/hotbar.hh +++ b/src/game/client/gui/hotbar.hh @@ -3,28 +3,25 @@ // TODO: design an inventory system and an item // registry and integrate the hotbar into that system -namespace world -{ class Item; -} // namespace world constexpr static unsigned int HOTBAR_SIZE = 9U; -namespace gui::hotbar +namespace hotbar { extern unsigned int active_slot; -extern std::array slots; -} // namespace gui::hotbar +extern std::array slots; +} // namespace hotbar -namespace gui::hotbar +namespace hotbar { void init(void); void shutdown(void); void layout(void); -} // namespace gui::hotbar +} // namespace hotbar -namespace gui::hotbar +namespace hotbar { void next_slot(void); void prev_slot(void); -} // namespace gui::hotbar +} // namespace hotbar diff --git a/src/game/client/gui/imdraw_ext.cc b/src/game/client/gui/imdraw_ext.cc deleted file mode 100644 index 4b44d5f..0000000 --- a/src/game/client/gui/imdraw_ext.cc +++ /dev/null @@ -1,34 +0,0 @@ -#include "client/pch.hh" - -#include "client/gui/imdraw_ext.hh" - -#include "client/globals.hh" - -void gui::imdraw_ext::text_shadow(const std::string& text, const ImVec2& position, ImU32 text_color, ImU32 shadow_color, ImFont* font, - ImDrawList* draw_list) -{ - imdraw_ext::text_shadow(text, position, text_color, shadow_color, font, draw_list, font->LegacySize); -} - -void gui::imdraw_ext::text_shadow(const std::string& text, const ImVec2& position, ImU32 text_color, ImU32 shadow_color, ImFont* font, - ImDrawList* draw_list, float font_size) -{ - const auto shadow_position = ImVec2(position.x + 0.5f * globals::gui_scale, position.y + 0.5f * globals::gui_scale); - draw_list->AddText(font, globals::gui_scale * font_size, shadow_position, shadow_color, text.c_str(), text.c_str() + text.size()); - draw_list->AddText(font, globals::gui_scale * font_size, position, text_color, text.c_str(), text.c_str() + text.size()); -} - -void gui::imdraw_ext::text_shadow_w(const std::string& text, const ImVec2& position, ImU32 text_color, ImU32 shadow_color, ImFont* font, - ImDrawList* draw_list, float wrap_width) -{ - imdraw_ext::text_shadow_w(text, position, text_color, shadow_color, font, draw_list, font->LegacySize, wrap_width); -} - -void gui::imdraw_ext::text_shadow_w(const std::string& text, const ImVec2& position, ImU32 text_color, ImU32 shadow_color, ImFont* font, - ImDrawList* draw_list, float font_size, float wrap_width) -{ - const auto shadow_position = ImVec2(position.x + 0.5f * globals::gui_scale, position.y + 0.5f * globals::gui_scale); - draw_list->AddText(font, globals::gui_scale * font_size, shadow_position, shadow_color, text.c_str(), text.c_str() + text.size(), - wrap_width); - draw_list->AddText(font, globals::gui_scale * font_size, position, text_color, text.c_str(), text.c_str() + text.size(), wrap_width); -} diff --git a/src/game/client/gui/imdraw_ext.hh b/src/game/client/gui/imdraw_ext.hh deleted file mode 100644 index a7e1503..0000000 --- a/src/game/client/gui/imdraw_ext.hh +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -namespace gui::imdraw_ext -{ -void text_shadow(const std::string& text, const ImVec2& position, ImU32 text_color, ImU32 shadow_color, ImFont* font, - ImDrawList* draw_list); -void text_shadow(const std::string& text, const ImVec2& position, ImU32 text_color, ImU32 shadow_color, ImFont* font, ImDrawList* draw_list, - float font_size); -} // namespace gui::imdraw_ext - -namespace gui::imdraw_ext -{ -void text_shadow_w(const std::string& text, const ImVec2& position, ImU32 text_color, ImU32 shadow_color, ImFont* font, - ImDrawList* draw_list, float wrap_width); -void text_shadow_w(const std::string& text, const ImVec2& position, ImU32 text_color, ImU32 shadow_color, ImFont* font, - ImDrawList* draw_list, float font_size, float wrap_width); -} // namespace gui::imdraw_ext diff --git a/src/game/client/gui/imutils_button.cc b/src/game/client/gui/imutils_button.cc new file mode 100644 index 0000000..2017e33 --- /dev/null +++ b/src/game/client/gui/imutils_button.cc @@ -0,0 +1,95 @@ +#include "client/pch.hh" + +#include "client/gui/imutils_button.hh" + +void imutils::button(const char* title, const ImVec2& size, void (*callback)(void)) +{ + assert(title); + assert(callback); + + if(ImGui::Button(title, size)) { + callback(); + } +} + +void imutils::button(const char* title, void (*callback)(void)) +{ + assert(title); + assert(callback); + + if(ImGui::Button(title)) { + callback(); + } +} + +bool imutils::selectable_button(const char* label, const ImVec2& size, bool value) +{ + assert(label); + + auto value_changed = false; + + if(value) { + ImVec4 button_color(0.750f, 0.750f, 0.750f, 1.000f); + ImGui::PushStyleColor(ImGuiCol_Button, button_color); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, button_color); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, button_color); + ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetStyleColorVec4(ImGuiCol_WindowBg)); + } + else { + ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyleColorVec4(ImGuiCol_WindowBg)); + } + + if(ImGui::Button(label, size)) { + value_changed = true; + } + + ImGui::PopStyleColor(value ? 4 : 1); + + return value_changed; +} + +bool imutils::toggle_button(const char* label, const ImVec2& size, bool& value) +{ + assert(label); + + auto use_custom_color = !value; + auto value_changed = false; + + if(use_custom_color) { + ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyleColorVec4(ImGuiCol_WindowBg)); + } + + if(ImGui::Button(label, size)) { + value_changed = true; + value = !value; + } + + if(use_custom_color) { + ImGui::PopStyleColor(); + } + + return value_changed; +} + +bool imutils::toggle_button(const char* label, bool& value) +{ + assert(label); + + auto use_custom_color = !value; + auto value_changed = false; + + if(use_custom_color) { + ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetStyleColorVec4(ImGuiCol_WindowBg)); + } + + if(ImGui::Button(label)) { + value_changed = true; + value = !value; + } + + if(use_custom_color) { + ImGui::PopStyleColor(); + } + + return value_changed; +} diff --git a/src/game/client/gui/imutils_button.hh b/src/game/client/gui/imutils_button.hh new file mode 100644 index 0000000..0c80694 --- /dev/null +++ b/src/game/client/gui/imutils_button.hh @@ -0,0 +1,14 @@ +#pragma once + +namespace imutils +{ +void button(const char* title, const ImVec2& size, void (*callback)(void)); +void button(const char* title, void (*callback)(void)); +} // namespace imutils + +namespace imutils +{ +bool selectable_button(const char* label, const ImVec2& size, bool value); +bool toggle_button(const char* label, const ImVec2& size, bool& value); +bool toggle_button(const char* label, bool& value); +} // namespace imutils diff --git a/src/game/client/gui/imutils_popup.cc b/src/game/client/gui/imutils_popup.cc new file mode 100644 index 0000000..f97facb --- /dev/null +++ b/src/game/client/gui/imutils_popup.cc @@ -0,0 +1,56 @@ +#include "client/pch.hh" + +#include "client/gui/imutils_popup.hh" + +#include "client/globals.hh" + +int imutils::popup(const std::string& title, const std::string& question, const std::string* choices, std::size_t num_choices, + float font_scale) +{ + assert(choices); + assert(num_choices); + + int result = POPUP_WAIT; + + ImGui::PushFont(globals::font_unscii16, globals::font_unscii16->LegacySize); + + if(ImGui::BeginPopupModal(title.c_str(), nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize)) { + const auto viewport = ImGui::GetMainViewport(); + const auto& viewport_size = viewport->Size; + + ImVec2 popup_size(ImGui::GetWindowSize()); + ImVec2 popup_pos(viewport_size.x * 0.5f - popup_size.x * 0.5f, viewport_size.y * 0.5f - popup_size.y * 0.5f); + + ImGui::PushTextWrapPos(popup_size.x); + ImGui::TextUnformatted(question.c_str()); + ImGui::PopTextWrapPos(); + + ImGui::NewLine(); + + auto& style = ImGui::GetStyle(); + auto& spacing = style.ItemSpacing; + ImVec2 button_size(0.5f * (ImGui::CalcItemWidth() - spacing.x), 0.0f); + + for(std::size_t i = 0; i < num_choices; ++i) { + if(ImGui::Button(choices[i].c_str(), button_size)) { + result = static_cast(i); + ImGui::CloseCurrentPopup(); + } + + if((i + 1) % 2 == 1) { + ImGui::SameLine(); + } + else if(i + 1 < num_choices) { + ImGui::Dummy(ImVec2(0.0, spacing.y)); + } + } + + ImGui::SetWindowPos(popup_pos, ImGuiCond_Always); + + ImGui::EndPopup(); + } + + ImGui::PopFont(); + + return result; +} diff --git a/src/game/client/gui/imutils_popup.hh b/src/game/client/gui/imutils_popup.hh new file mode 100644 index 0000000..1678f87 --- /dev/null +++ b/src/game/client/gui/imutils_popup.hh @@ -0,0 +1,11 @@ +#pragma once + +/// Returned by popup::show whenever the popup is opened +/// but there is no user action yet, so the UI should wait +constexpr static int POPUP_WAIT = -1; + +namespace imutils +{ +int popup(const std::string& title, const std::string& question, const std::string* choices, std::size_t num_choices, + float font_scale = 1.75f); +} // namespace imutils diff --git a/src/game/client/gui/imutils_text.cc b/src/game/client/gui/imutils_text.cc new file mode 100644 index 0000000..1058c7f --- /dev/null +++ b/src/game/client/gui/imutils_text.cc @@ -0,0 +1,46 @@ +#include "client/pch.hh" + +#include "client/gui/imutils_text.hh" + +#include "client/globals.hh" + +void imutils::text_nw(ImDrawList* list, std::string_view text, const ImVec2& pos, ImU32 color, ImU32 shadow, ImFont* font) +{ + assert(list); + assert(font); + + imutils::text_nw(list, text, pos, color, shadow, font, font->LegacySize); +} + +void imutils::text_nw(ImDrawList* list, std::string_view text, const ImVec2& pos, ImU32 color, ImU32 shadow, ImFont* font, float size) +{ + assert(list); + assert(font); + + auto shift = static_cast(globals::gui_scale); + auto shadow_pos = ImVec2(pos.x + shift, pos.y + shift); + + list->AddText(font, size, shadow_pos, shadow, text.data(), text.data() + text.size()); + list->AddText(font, size, pos, color, text.data(), text.data() + text.size()); +} + +void imutils::text_wr(ImDrawList* list, std::string_view text, const ImVec2& pos, ImU32 color, ImU32 shadow, ImFont* font, float wrap) +{ + assert(list); + assert(font); + + imutils::text_wr(list, text, pos, color, shadow, font, wrap, font->LegacySize); +} + +void imutils::text_wr(ImDrawList* list, std::string_view text, const ImVec2& pos, ImU32 color, ImU32 shadow, ImFont* font, float wrap, + float size) +{ + assert(list); + assert(font); + + auto shift = static_cast(globals::gui_scale); + auto shadow_pos = ImVec2(pos.x + shift, pos.y + shift); + + list->AddText(font, size, shadow_pos, shadow, text.data(), text.data() + text.size(), wrap); + list->AddText(font, size, pos, color, text.data(), text.data() + text.size(), wrap); +} diff --git a/src/game/client/gui/imutils_text.hh b/src/game/client/gui/imutils_text.hh new file mode 100644 index 0000000..64d06a4 --- /dev/null +++ b/src/game/client/gui/imutils_text.hh @@ -0,0 +1,9 @@ +#pragma once + +namespace imutils +{ +void text_nw(ImDrawList* list, std::string_view text, const ImVec2& pos, ImU32 color, ImU32 shadow, ImFont* font); +void text_nw(ImDrawList* list, std::string_view text, const ImVec2& pos, ImU32 color, ImU32 shadow, ImFont* font, float size); +void text_wr(ImDrawList* list, std::string_view text, const ImVec2& pos, ImU32 color, ImU32 shadow, ImFont* font, float wrap); +void text_wr(ImDrawList* list, std::string_view text, const ImVec2& pos, ImU32 color, ImU32 shadow, ImFont* font, float wrap, float size); +} // namespace imutils diff --git a/src/game/client/gui/language.cc b/src/game/client/gui/language.cc index 0109ae6..8a2d8d1 100644 --- a/src/game/client/gui/language.cc +++ b/src/game/client/gui/language.cc @@ -19,20 +19,20 @@ constexpr static std::string_view DEFAULT_LANGUAGE = "en_US"; // system knows what language it can load and will act accordingly constexpr static std::string_view MANIFEST_PATH = "lang/manifest.json"; -static gui::LanguageManifest manifest; -static gui::LanguageIterator current_language; +static LanguageManifest manifest; +static LanguageIterator current_language; static std::unordered_map language_map; -static std::unordered_map ietf_map; +static std::unordered_map ietf_map; static config::String config_language(DEFAULT_LANGUAGE); -static void send_language_event(gui::LanguageIterator new_language) +static void send_language_event(LanguageIterator new_language) { - gui::LanguageSetEvent event; + LanguageSetEvent event; event.new_language = new_language; globals::dispatcher.trigger(event); } -void gui::language::init(void) +void language::init(void) { globals::client_config.add_value("language", config_language); @@ -41,7 +41,7 @@ void gui::language::init(void) auto file = PHYSFS_openRead(std::string(MANIFEST_PATH).c_str()); if(file == nullptr) { - spdlog::critical("language: {}: {}", MANIFEST_PATH, io::physfs_error()); + spdlog::critical("language: {}: {}", MANIFEST_PATH, physfs_error()); std::terminate(); } @@ -85,19 +85,19 @@ void gui::language::init(void) current_language = manifest.cend(); } -void gui::language::init_late(void) +void language::init_late(void) { auto user_language = ietf_map.find(config_language.get_value()); if(user_language != ietf_map.cend()) { - gui::language::set(user_language->second); + language::set(user_language->second); return; } auto fallback = ietf_map.find(std::string(DEFAULT_LANGUAGE)); if(fallback != ietf_map.cend()) { - gui::language::set(fallback->second); + language::set(fallback->second); return; } @@ -106,7 +106,7 @@ void gui::language::init_late(void) std::terminate(); } -void gui::language::set(LanguageIterator new_language) +void language::set(LanguageIterator new_language) { if(new_language != manifest.cend()) { auto path = std::format("lang/lang.{}.json", new_language->ietf); @@ -114,7 +114,7 @@ void gui::language::set(LanguageIterator new_language) auto file = PHYSFS_openRead(path.c_str()); if(file == nullptr) { - spdlog::warn("language: {}: {}", path, io::physfs_error()); + spdlog::warn("language: {}: {}", path, physfs_error()); send_language_event(new_language); return; } @@ -156,12 +156,12 @@ void gui::language::set(LanguageIterator new_language) send_language_event(new_language); } -gui::LanguageIterator gui::language::get_current(void) +LanguageIterator language::get_current(void) { return current_language; } -gui::LanguageIterator gui::language::find(std::string_view ietf) +LanguageIterator language::find(std::string_view ietf) { const auto it = ietf_map.find(std::string(ietf)); if(it != ietf_map.cend()) { @@ -172,17 +172,17 @@ gui::LanguageIterator gui::language::find(std::string_view ietf) } } -gui::LanguageIterator gui::language::cbegin(void) +LanguageIterator language::cbegin(void) { return manifest.cbegin(); } -gui::LanguageIterator gui::language::cend(void) +LanguageIterator language::cend(void) { return manifest.cend(); } -std::string_view gui::language::resolve(std::string_view key) +std::string_view language::resolve(std::string_view key) { const auto it = language_map.find(std::string(key)); @@ -193,10 +193,10 @@ std::string_view gui::language::resolve(std::string_view key) return key; } -std::string gui::language::resolve_gui(std::string_view key) +std::string language::resolve_gui(std::string_view key) { // We need window tags to retain their hierarchy when a language // dynamically changes; ImGui allows to provide hidden unique identifiers // to GUI primitives that have their name change dynamically, so we're using this - return std::format("{}###{}", gui::language::resolve(key), key); + return std::format("{}###{}", language::resolve(key), key); } diff --git a/src/game/client/gui/language.hh b/src/game/client/gui/language.hh index 90132d7..20c7520 100644 --- a/src/game/client/gui/language.hh +++ b/src/game/client/gui/language.hh @@ -1,7 +1,5 @@ #pragma once -namespace gui -{ struct LanguageInfo final { std::string endonym; // Language's self-name std::string display; // Display for the settings GUI @@ -14,29 +12,28 @@ using LanguageIterator = LanguageManifest::const_iterator; struct LanguageSetEvent final { LanguageIterator new_language; }; -} // namespace gui -namespace gui::language +namespace language { void init(void); void init_late(void); -} // namespace gui::language +} // namespace language -namespace gui::language +namespace language { void set(LanguageIterator new_language); -} // namespace gui::language +} // namespace language -namespace gui::language +namespace language { LanguageIterator get_current(void); LanguageIterator find(std::string_view ietf); LanguageIterator cbegin(void); LanguageIterator cend(void); -} // namespace gui::language +} // namespace language -namespace gui::language +namespace language { std::string_view resolve(std::string_view key); std::string resolve_gui(std::string_view key); -} // namespace gui::language +} // namespace language diff --git a/src/game/client/gui/main_menu.cc b/src/game/client/gui/main_menu.cc index d60a507..d764449 100644 --- a/src/game/client/gui/main_menu.cc +++ b/src/game/client/gui/main_menu.cc @@ -9,6 +9,8 @@ #include "core/version.hh" #include "client/gui/gui_screen.hh" +#include "client/gui/imutils_button.hh" +#include "client/gui/imutils_popup.hh" #include "client/gui/language.hh" #include "client/gui/window_title.hh" @@ -19,18 +21,17 @@ #include "client/globals.hh" #include "client/session.hh" -constexpr static ImGuiWindowFlags WINDOW_FLAGS = ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration; +static std::string str_button_play; +static std::string str_button_resume; +static std::string str_button_settings; +static std::string str_button_disconnect; +static std::string str_button_quit; -static std::string str_play; -static std::string str_resume; -static std::string str_settings; -static std::string str_leave; -static std::string str_quit; +static std::string str_quit_popup_title; +static std::string str_quit_popup_question; +static std::string str_quit_popup_choices[2]; -static resource_ptr title; -static float title_aspect; - -static void on_glfw_key(const io::GlfwKeyEvent& event) +static void on_glfw_key(const GlfwKeyEvent& event) { if(session::is_ingame() && (event.key == GLFW_KEY_ESCAPE) && (event.action == GLFW_PRESS)) { if(globals::gui_screen == GUI_SCREEN_NONE) { @@ -45,127 +46,106 @@ static void on_glfw_key(const io::GlfwKeyEvent& event) } } -static void on_language_set(const gui::LanguageSetEvent& event) +static void on_language_set(const LanguageSetEvent& event) { - str_play = gui::language::resolve_gui("main_menu.play"); - str_resume = gui::language::resolve_gui("main_menu.resume"); - str_settings = gui::language::resolve("main_menu.settings"); - str_leave = gui::language::resolve("main_menu.leave"); - str_quit = gui::language::resolve("main_menu.quit"); + str_button_play = language::resolve_gui("main_menu.button.play"); + str_button_resume = language::resolve_gui("main_menu.button.resume"); + str_button_settings = language::resolve("main_menu.button.settings"); + str_button_disconnect = language::resolve("main_menu.button.disconnect"); + str_button_quit = language::resolve("main_menu.button.quit"); + + str_quit_popup_title = language::resolve_gui("main_menu.quit_popup.title"); + str_quit_popup_question = language::resolve("main_menu.quit_popup.question"); + str_quit_popup_choices[0] = language::resolve_gui("main_menu.quit_popup.choice.yes"); + str_quit_popup_choices[1] = language::resolve_gui("main_menu.quit_popup.choice.no"); } -void gui::main_menu::init(void) +void main_menu::init(void) { - title = resource::load("textures/gui/menu_title.png", TEXTURE_GUI_LOAD_CLAMP_S | TEXTURE_GUI_LOAD_CLAMP_T); - - if(title == nullptr) { - spdlog::critical("main_menu: texture load failed"); - std::terminate(); - } - - if(title->size.x > title->size.y) { - title_aspect = static_cast(title->size.x) / static_cast(title->size.y); - } - else { - title_aspect = static_cast(title->size.y) / static_cast(title->size.x); - } - - globals::dispatcher.sink().connect<&on_glfw_key>(); + globals::dispatcher.sink().connect<&on_glfw_key>(); globals::dispatcher.sink().connect<&on_language_set>(); } -void gui::main_menu::shutdown(void) +void main_menu::shutdown(void) { - title = nullptr; + // empty } -void gui::main_menu::layout(void) +void main_menu::layout(void) { const auto viewport = ImGui::GetMainViewport(); - const auto window_start = ImVec2(0.0f, viewport->Size.y * 0.15f); - const auto window_size = ImVec2(viewport->Size.x, viewport->Size.y); + const auto& viewport_size = viewport->Size; - ImGui::SetNextWindowPos(window_start); - ImGui::SetNextWindowSize(window_size); + ImVec2 margin(globals::gui_scale * 8.0f, globals::gui_scale * 8.0f); + ImVec2 control(globals::gui_scale * 256.0f, globals::gui_scale * 32.0f); - if(ImGui::Begin("###main_menu", nullptr, WINDOW_FLAGS)) { - ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0.0f, 2.0f * globals::gui_scale)); + ImGui::SetNextWindowPos({}); + ImGui::SetNextWindowSize(viewport_size); - if(session::is_ingame()) { - ImGui::Dummy(ImVec2(0.0f, 32.0f * globals::gui_scale)); - } - else { - auto reference_height = 0.225f * window_size.y; - auto image_width = glm::min(window_size.x, reference_height * title_aspect); - auto image_height = image_width / title_aspect; - ImGui::SetCursorPosX(0.5f * (window_size.x - image_width)); - ImGui::Image(title->handle, ImVec2(image_width, image_height)); - } - - ImGui::Dummy(ImVec2(0.0f, 24.0f * globals::gui_scale)); - - const float button_width = 240.0f * globals::gui_scale; - const float button_xpos = 0.5f * (window_size.x - button_width); - - if(session::is_ingame()) { - ImGui::SetCursorPosX(button_xpos); - - if(ImGui::Button(str_resume.c_str(), ImVec2(button_width, 0.0f))) { - globals::gui_screen = GUI_SCREEN_NONE; - } - - ImGui::Spacing(); - } - else { - ImGui::SetCursorPosX(button_xpos); + ImGuiWindowFlags flags = 0U; + flags |= ImGuiWindowFlags_NoBackground; + flags |= ImGuiWindowFlags_NoDecoration; - if(ImGui::Button(str_play.c_str(), ImVec2(button_width, 0.0f))) { - globals::gui_screen = GUI_PLAY_MENU; - } - - ImGui::Spacing(); - } + if(!ImGui::Begin("###gui::main_menu", nullptr, flags)) { + ImGui::End(); + return; + } - ImGui::SetCursorPosX(button_xpos); + ImGui::PushFont(globals::font_unscii16, 16.0f); + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.000f, 0.000f, 0.000f, 0.000f)); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.500f, 0.500f, 0.500f, 0.125f)); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.500f, 0.500f, 0.500f, 0.500f)); + ImGui::PushStyleVarX(ImGuiStyleVar_FramePadding, 16.0f * globals::gui_scale); + ImGui::PushStyleVarX(ImGuiStyleVar_ButtonTextAlign, 0.0f); + ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 0.0f); - if(ImGui::Button(str_settings.c_str(), ImVec2(button_width, 0.0f))) { - globals::gui_screen = GUI_SETTINGS; - } + ImVec2 cursor_pos(margin.x, viewport_size.y - margin.y); - ImGui::Spacing(); + cursor_pos.y -= control.y * 1.5f; + ImGui::SetCursorPos(cursor_pos); - if(session::is_ingame()) { - ImGui::SetCursorPosX(button_xpos); + if(session::is_ingame()) { + imutils::button(str_button_disconnect.c_str(), control, [] { + session::disconnect("protocol.client_disconnect"); + }); + } + else { + imutils::button(str_button_quit.c_str(), control, [] { + ImGui::OpenPopup(str_quit_popup_title.c_str()); + }); + } - if(ImGui::Button(str_leave.c_str(), ImVec2(button_width, 0.0f))) { - session::disconnect("protocol.client_disconnect"); - globals::gui_screen = GUI_PLAY_MENU; - gui::window_title::update(); - } + cursor_pos.y -= control.y; + ImGui::SetCursorPos(cursor_pos); - ImGui::Spacing(); - } - else { - ImGui::SetCursorPosX(button_xpos); + imutils::button(str_button_settings.c_str(), control, [] { + globals::gui_screen = GUI_SETTINGS; + }); - if(ImGui::Button(str_quit.c_str(), ImVec2(button_width, 0.0f))) { - glfwSetWindowShouldClose(globals::window, true); - } + cursor_pos.y -= control.y * 1.5f; + ImGui::SetCursorPos(cursor_pos); - ImGui::Spacing(); - } - - if(!session::is_ingame()) { - const auto& padding = ImGui::GetStyle().FramePadding; - const auto& spacing = ImGui::GetStyle().ItemSpacing; + if(session::is_ingame()) { + imutils::button(str_button_resume.c_str(), control, [] { + globals::gui_screen = GUI_SCREEN_NONE; + }); + } + else { + imutils::button(str_button_play.c_str(), control, [] { + globals::gui_screen = GUI_PLAY_MENU; + }); + } - ImGui::PushFont(globals::font_unscii8, 4.0f); - ImGui::SetCursorScreenPos(ImVec2(padding.x + spacing.x, window_size.y - ImGui::GetFontSize() - padding.y - spacing.y)); - ImGui::Text("Voxelius %*s", version::full.size(), version::full.data()); // string_view is not always null-terminated - ImGui::PopFont(); - } + ImGui::PopStyleVar(3); + ImGui::PopStyleColor(3); + ImGui::PopFont(); - ImGui::PopStyleVar(); + if(0 == imutils::popup(str_quit_popup_title, str_quit_popup_question, str_quit_popup_choices, 2)) { + // We don't really have a good way to + // pass "i want to quit" boolean to the main loop, + // so instead we just raise an external interrupt signal + // which handler latches an internal flag in the main loop + std::raise(SIGINT); } ImGui::End(); diff --git a/src/game/client/gui/main_menu.hh b/src/game/client/gui/main_menu.hh index 205f078..b5333d4 100644 --- a/src/game/client/gui/main_menu.hh +++ b/src/game/client/gui/main_menu.hh @@ -1,8 +1,8 @@ #pragma once -namespace gui::main_menu +namespace main_menu { void init(void); void shutdown(void); void layout(void); -} // namespace gui::main_menu +} // namespace main_menu diff --git a/src/game/client/gui/message_box.cc b/src/game/client/gui/message_box.cc index 59e2d33..d97267a 100644 --- a/src/game/client/gui/message_box.cc +++ b/src/game/client/gui/message_box.cc @@ -10,7 +10,7 @@ constexpr static ImGuiWindowFlags WINDOW_FLAGS = ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration; struct Button final { - gui::message_box_action action; + message_box_action action; std::string str_title; }; @@ -18,14 +18,14 @@ static std::string str_title; static std::string str_subtitle; static std::vector