diff options
| author | untodesu <kirill@untode.su> | 2025-09-11 13:48:31 +0500 |
|---|---|---|
| committer | untodesu <kirill@untode.su> | 2025-09-11 13:48:31 +0500 |
| commit | aaed751bf4430bf4b9b30cef532b8753b9f639ce (patch) | |
| tree | 16bc751c272ba27ad53ec48dbdd3a6d9e6a8d4c2 /game/client | |
| parent | 96bd73ae020ecca1f94698744c77498a89ad19f7 (diff) | |
| download | voxelius-aaed751bf4430bf4b9b30cef532b8753b9f639ce.tar.bz2 voxelius-aaed751bf4430bf4b9b30cef532b8753b9f639ce.zip | |
Replace most of C strings with string_view
Diffstat (limited to 'game/client')
33 files changed, 158 insertions, 157 deletions
diff --git a/game/client/config/gamepad_axis.cc b/game/client/config/gamepad_axis.cc index a82de81..8ae74be 100644 --- a/game/client/config/gamepad_axis.cc +++ b/game/client/config/gamepad_axis.cc @@ -6,9 +6,9 @@ #include "client/io/gamepad.hh" -constexpr static const char* UNKNOWN_AXIS_NAME = "UNKNOWN"; +constexpr static std::string_view UNKNOWN_AXIS_NAME = "UNKNOWN"; -static const std::pair<int, const char*> axis_names[] = { +static const std::pair<int, std::string_view> axis_names[] = { { GLFW_GAMEPAD_AXIS_LEFT_X, "LEFT_X" }, { GLFW_GAMEPAD_AXIS_LEFT_Y, "LEFT_Y" }, { GLFW_GAMEPAD_AXIS_RIGHT_X, "RIGHT_X" }, @@ -17,7 +17,7 @@ static const std::pair<int, const char*> axis_names[] = { { GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER, "RIGHT_TRIG" }, }; -static const char* get_axis_name(int axis) +static std::string_view get_axis_name(int axis) { for(const auto& it : axis_names) { if(it.first != axis) { @@ -42,19 +42,20 @@ config::GamepadAxis::GamepadAxis(int axis, bool inverted) m_full_string = std::format("{}:{}", m_name, m_inverted ? 1U : 0U); } -const char* config::GamepadAxis::get(void) const +std::string_view config::GamepadAxis::get(void) const { - return m_full_string.c_str(); + return m_full_string; } -void config::GamepadAxis::set(const char* value) +void config::GamepadAxis::set(std::string_view value) { char new_name[64]; unsigned int new_invert; + std::string value_str(value); - if(2 == std::sscanf(value, "%63[^:]:%u", new_name, &new_invert)) { + if(2 == std::sscanf(value_str.c_str(), "%63[^:]:%u", new_name, &new_invert)) { for(const auto& it : axis_names) { - if(!std::strcmp(it.second, new_name)) { + if(0 == it.second.compare(new_name)) { m_inverted = new_invert; m_gamepad_axis = it.first; m_name = get_axis_name(m_gamepad_axis); @@ -108,7 +109,7 @@ float config::GamepadAxis::get_value(const GLFWgamepadstate& state, float deadzo return 0.0f; } -const char* config::GamepadAxis::get_name(void) const +std::string_view config::GamepadAxis::get_name(void) const { return m_name; } diff --git a/game/client/config/gamepad_axis.hh b/game/client/config/gamepad_axis.hh index 0308ce6..86a2990 100644 --- a/game/client/config/gamepad_axis.hh +++ b/game/client/config/gamepad_axis.hh @@ -14,8 +14,8 @@ public: explicit GamepadAxis(int axis, bool inverted); virtual ~GamepadAxis(void) = default; - virtual const char* get(void) const override; - virtual void set(const char* value) override; + virtual std::string_view get(void) const override; + virtual void set(std::string_view value) override; int get_axis(void) const; void set_axis(int axis); @@ -29,13 +29,13 @@ public: // this configuration value actually contain the // inversion flag. Since we're updating that flag // in the UI by means of a separate checkbox, we only need the name here - const char* get_name(void) const; + std::string_view get_name(void) const; private: bool m_inverted; int m_gamepad_axis; std::string m_full_string; - const char* m_name; + std::string_view m_name; }; } // namespace config diff --git a/game/client/config/gamepad_button.cc b/game/client/config/gamepad_button.cc index b983baa..07e4457 100644 --- a/game/client/config/gamepad_button.cc +++ b/game/client/config/gamepad_button.cc @@ -6,9 +6,9 @@ #include "client/io/gamepad.hh" -constexpr static const char* UNKNOWN_BUTTON_NAME = "UNKNOWN"; +constexpr static std::string_view UNKNOWN_BUTTON_NAME = "UNKNOWN"; -static const std::pair<int, const char*> button_names[] = { +static const std::pair<int, std::string_view> button_names[] = { { GLFW_GAMEPAD_BUTTON_A, "A" }, { GLFW_GAMEPAD_BUTTON_B, "B" }, { GLFW_GAMEPAD_BUTTON_X, "X" }, @@ -26,7 +26,7 @@ static const std::pair<int, const char*> button_names[] = { { GLFW_GAMEPAD_BUTTON_DPAD_LEFT, "DPAD_LEFT" }, }; -static const char* get_button_name(int button) +static std::string_view get_button_name(int button) { for(const auto& it : button_names) { if(it.first == button) { @@ -49,15 +49,15 @@ config::GamepadButton::GamepadButton(int button) m_name = get_button_name(button); } -const char* config::GamepadButton::get(void) const +std::string_view config::GamepadButton::get(void) const { return m_name; } -void config::GamepadButton::set(const char* value) +void config::GamepadButton::set(std::string_view value) { for(const auto& it : button_names) { - if(!std::strcmp(it.second, value)) { + if(0 == it.second.compare(value)) { m_gamepad_button = it.first; m_name = it.second; return; diff --git a/game/client/config/gamepad_button.hh b/game/client/config/gamepad_button.hh index 11566c1..e1d1224 100644 --- a/game/client/config/gamepad_button.hh +++ b/game/client/config/gamepad_button.hh @@ -14,8 +14,8 @@ public: explicit GamepadButton(int button); virtual ~GamepadButton(void) = default; - virtual const char* get(void) const override; - virtual void set(const char* value) override; + virtual std::string_view get(void) const override; + virtual void set(std::string_view value) override; int get_button(void) const; void set_button(int button); @@ -25,7 +25,7 @@ public: private: int m_gamepad_button; - const char* m_name; + std::string_view m_name; }; } // namespace config diff --git a/game/client/config/keybind.cc b/game/client/config/keybind.cc index 6bd8ef0..e254f7b 100644 --- a/game/client/config/keybind.cc +++ b/game/client/config/keybind.cc @@ -6,9 +6,9 @@ #include "client/const.hh" -constexpr static const char* UNKNOWN_KEY_NAME = "UNKNOWN"; +constexpr static std::string_view UNKNOWN_KEY_NAME = "UNKNOWN"; -static const std::pair<int, const char*> key_names[] = { +static const std::pair<int, std::string_view> key_names[] = { { GLFW_KEY_SPACE, "SPACE" }, { GLFW_KEY_APOSTROPHE, "'" }, { GLFW_KEY_COMMA, "," }, @@ -131,7 +131,7 @@ static const std::pair<int, const char*> key_names[] = { { GLFW_KEY_MENU, "MENU" }, }; -static const char* get_key_name(int keycode) +static std::string_view get_key_name(int keycode) { for(const auto& it : key_names) { if(it.first == keycode) { @@ -160,10 +160,10 @@ config::KeyBind::KeyBind(int default_value) } } -void config::KeyBind::set(const char* value) +void config::KeyBind::set(std::string_view value) { for(const auto& it : key_names) { - if((it.first != DEBUG_KEY) && !std::strcmp(it.second, value)) { + if((it.first != DEBUG_KEY) && 0 == it.second.compare(value)) { m_glfw_keycode = it.first; m_name = it.second; return; @@ -174,7 +174,7 @@ void config::KeyBind::set(const char* value) m_name = UNKNOWN_KEY_NAME; } -const char* config::KeyBind::get(void) const +std::string_view config::KeyBind::get(void) const { return m_name; } diff --git a/game/client/config/keybind.hh b/game/client/config/keybind.hh index abfb97a..0005fe4 100644 --- a/game/client/config/keybind.hh +++ b/game/client/config/keybind.hh @@ -12,8 +12,8 @@ public: explicit KeyBind(int default_value); virtual ~KeyBind(void) = default; - virtual void set(const char* value) override; - virtual const char* get(void) const override; + virtual void set(std::string_view value) override; + virtual std::string_view get(void) const override; void set_key(int keycode); int get_key(void) const; @@ -21,7 +21,7 @@ public: bool equals(int keycode) const; private: - const char* m_name; + std::string_view m_name; int m_glfw_keycode; }; } // namespace config diff --git a/game/client/gui/bother.cc b/game/client/gui/bother.cc index 3a35438..d87f220 100644 --- a/game/client/gui/bother.cc +++ b/game/client/gui/bother.cc @@ -117,7 +117,7 @@ void gui::bother::update_late(void) } } -void gui::bother::ping(unsigned int identity, const char* host, std::uint16_t port) +void gui::bother::ping(unsigned int identity, std::string_view host, std::uint16_t port) { if(bother_set.count(identity)) { // Already in the process @@ -133,7 +133,7 @@ void gui::bother::ping(unsigned int identity, const char* host, std::uint16_t po BotherQueueItem item; item.identity = identity; - item.hostname = std::string(host); + item.hostname = host; item.port = port; bother_queue.push_back(item); diff --git a/game/client/gui/bother.hh b/game/client/gui/bother.hh index c10bf8a..f555d74 100644 --- a/game/client/gui/bother.hh +++ b/game/client/gui/bother.hh @@ -19,7 +19,7 @@ namespace gui::bother void init(void); void shutdown(void); void update_late(void); -void ping(unsigned int identity, const char* host, std::uint16_t port); +void ping(unsigned int identity, std::string_view host, std::uint16_t port); void cancel(unsigned int identity); } // namespace gui::bother diff --git a/game/client/gui/language.cc b/game/client/gui/language.cc index b5bdc7d..3c700a2 100644 --- a/game/client/gui/language.cc +++ b/game/client/gui/language.cc @@ -9,13 +9,13 @@ #include "client/globals.hh" -constexpr static const char* DEFAULT_LANGUAGE = "en_US"; +constexpr static std::string_view DEFAULT_LANGUAGE = "en_US"; // Available languages are kept in a special manifest file which // is essentially a key-value map of semi-IETF-compliant language tags // and the language's endonym; after reading the manifest, the translation // system knows what language it can load and will act accordingly -constexpr static const char* MANIFEST_PATH = "lang/manifest.json"; +constexpr static std::string_view MANIFEST_PATH = "lang/manifest.json"; static gui::LanguageManifest manifest; static gui::LanguageIterator current_language; @@ -36,7 +36,7 @@ void gui::language::init(void) settings::add_language_select(0, settings_location::GENERAL, "language"); - auto file = PHYSFS_openRead(MANIFEST_PATH); + auto file = PHYSFS_openRead(std::string(MANIFEST_PATH).c_str()); if(file == nullptr) { spdlog::critical("language: {}: {}", MANIFEST_PATH, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); @@ -85,14 +85,14 @@ void gui::language::init(void) void gui::language::init_late(void) { - auto user_language = ietf_map.find(config_language.get()); + auto user_language = ietf_map.find(config_language.get_value()); if(user_language != ietf_map.cend()) { gui::language::set(user_language->second); return; } - auto fallback = ietf_map.find(DEFAULT_LANGUAGE); + auto fallback = ietf_map.find(std::string(DEFAULT_LANGUAGE)); if(fallback != ietf_map.cend()) { gui::language::set(fallback->second); @@ -159,9 +159,9 @@ gui::LanguageIterator gui::language::get_current(void) return current_language; } -gui::LanguageIterator gui::language::find(const char* ietf) +gui::LanguageIterator gui::language::find(std::string_view ietf) { - const auto it = ietf_map.find(ietf); + const auto it = ietf_map.find(std::string(ietf)); if(it != ietf_map.cend()) { return it->second; } @@ -180,18 +180,18 @@ gui::LanguageIterator gui::language::cend(void) return manifest.cend(); } -const char* gui::language::resolve(const char* key) +std::string_view gui::language::resolve(std::string_view key) { - const auto it = language_map.find(key); + const auto it = language_map.find(std::string(key)); + if(it != language_map.cend()) { - return it->second.c_str(); - } - else { - return key; + return it->second; } + + return key; } -std::string gui::language::resolve_gui(const char* key) +std::string gui::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 diff --git a/game/client/gui/language.hh b/game/client/gui/language.hh index d54208a..37cc790 100644 --- a/game/client/gui/language.hh +++ b/game/client/gui/language.hh @@ -32,15 +32,15 @@ void set(LanguageIterator new_language); namespace gui::language { LanguageIterator get_current(void); -LanguageIterator find(const char* ietf); +LanguageIterator find(std::string_view ietf); LanguageIterator cbegin(void); LanguageIterator cend(void); } // namespace gui::language namespace gui::language { -const char* resolve(const char* key); -std::string resolve_gui(const char* key); +std::string_view resolve(std::string_view key); +std::string resolve_gui(std::string_view key); } // namespace gui::language #endif // CLIENT_LANGUAGE_HH diff --git a/game/client/gui/main_menu.cc b/game/client/gui/main_menu.cc index 37ee398..62802a8 100644 --- a/game/client/gui/main_menu.cc +++ b/game/client/gui/main_menu.cc @@ -158,7 +158,7 @@ void gui::main_menu::layout(void) ImGui::PushFont(globals::font_debug); ImGui::SetCursorScreenPos(ImVec2(padding.x + spacing.x, window_size.y - globals::font_debug->FontSize - padding.y - spacing.y)); - ImGui::Text("Voxelius %s", project_version_string); + ImGui::Text("Voxelius %*s", version::semver.size(), version::semver.data()); // string_view is not always null-terminated ImGui::PopFont(); } diff --git a/game/client/gui/message_box.cc b/game/client/gui/message_box.cc index 615281b..59e2d33 100644 --- a/game/client/gui/message_box.cc +++ b/game/client/gui/message_box.cc @@ -75,17 +75,17 @@ void gui::message_box::reset(void) buttons.clear(); } -void gui::message_box::set_title(const char* title) +void gui::message_box::set_title(std::string_view title) { str_title = gui::language::resolve(title); } -void gui::message_box::set_subtitle(const char* subtitle) +void gui::message_box::set_subtitle(std::string_view subtitle) { str_subtitle = gui::language::resolve(subtitle); } -void gui::message_box::add_button(const char* text, const message_box_action& action) +void gui::message_box::add_button(std::string_view text, const message_box_action& action) { Button button = {}; button.str_title = std::format("{}###MessageBox_Button{}", gui::language::resolve(text), buttons.size()); diff --git a/game/client/gui/message_box.hh b/game/client/gui/message_box.hh index c5545fc..a906744 100644 --- a/game/client/gui/message_box.hh +++ b/game/client/gui/message_box.hh @@ -16,9 +16,9 @@ void reset(void); namespace gui::message_box { -void set_title(const char* title); -void set_subtitle(const char* subtitle); -void add_button(const char* text, const message_box_action& action); +void set_title(std::string_view title); +void set_subtitle(std::string_view subtitle); +void add_button(std::string_view text, const message_box_action& action); } // namespace gui::message_box #endif // CLIENT_MESSAGE_BOX_HH diff --git a/game/client/gui/metrics.cc b/game/client/gui/metrics.cc index 350208c..f439fd0 100644 --- a/game/client/gui/metrics.cc +++ b/game/client/gui/metrics.cc @@ -50,7 +50,7 @@ void gui::metrics::layout(void) auto y_step = 1.5f * globals::font_debug->FontSize; // Draw version - auto version_line = std::format("Voxelius {}", project_version_string); + auto version_line = std::format("Voxelius {} [{}]", version::semver, version::commit); gui::imdraw_ext::text_shadow(version_line, position, text_color, shadow_color, globals::font_debug, draw_list); position.y += 1.5f * y_step; diff --git a/game/client/gui/play_menu.cc b/game/client/gui/play_menu.cc index f2a8887..dc4ffed 100644 --- a/game/client/gui/play_menu.cc +++ b/game/client/gui/play_menu.cc @@ -19,9 +19,9 @@ #include "client/session.hh" constexpr static ImGuiWindowFlags WINDOW_FLAGS = ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration; -constexpr static const char* DEFAULT_SERVER_NAME = "Voxelius Server"; -constexpr static const char* SERVERS_TXT = "servers.txt"; -constexpr static const char* WARNING_TOAST = "[!]"; +constexpr static std::string_view DEFAULT_SERVER_NAME = "Voxelius Server"; +constexpr static std::string_view SERVERS_TXT = "servers.txt"; +constexpr static std::string_view WARNING_TOAST = "[!]"; constexpr static std::size_t MAX_SERVER_ITEM_NAME = 24; @@ -265,10 +265,11 @@ static void layout_server_item(ServerStatusItem* item) draw_list->AddText(stats_pos, ImGui::GetColorU32(ImGuiCol_TextDisabled), stats.c_str(), stats.c_str() + stats.size()); if(item->protocol_version != protocol::VERSION) { - auto warning_size = ImGui::CalcTextSize(WARNING_TOAST); + auto warning_size = ImGui::CalcTextSize(WARNING_TOAST.data(), WARNING_TOAST.data() + WARNING_TOAST.size()); auto warning_pos = ImVec2(stats_pos.x - warning_size.x - padding.x - 4.0f * globals::gui_scale, cursor.y + padding.y); auto warning_end = ImVec2(warning_pos.x + warning_size.x, warning_pos.y + warning_size.y); - draw_list->AddText(warning_pos, ImGui::GetColorU32(ImGuiCol_DragDropTarget), WARNING_TOAST); + draw_list->AddText(warning_pos, ImGui::GetColorU32(ImGuiCol_DragDropTarget), WARNING_TOAST.data(), + WARNING_TOAST.data() + WARNING_TOAST.size()); if(ImGui::IsMouseHoveringRect(warning_pos, warning_end)) { ImGui::BeginTooltip(); @@ -442,7 +443,7 @@ static void layout_servers_buttons(void) void gui::play_menu::init(void) { - if(auto file = PHYSFS_openRead(SERVERS_TXT)) { + if(auto file = PHYSFS_openRead(std::string(SERVERS_TXT).c_str())) { auto source = std::string(PHYSFS_fileLength(file), char(0x00)); PHYSFS_readBytes(file, source.data(), source.size()); PHYSFS_close(file); @@ -496,7 +497,7 @@ void gui::play_menu::shutdown(void) stream << std::format("{}:{}%{}%{}", item->hostname, item->port, item->password, item->name) << std::endl; } - if(auto file = PHYSFS_openWrite(SERVERS_TXT)) { + if(auto file = PHYSFS_openWrite(std::string(SERVERS_TXT).c_str())) { auto source = stream.str(); PHYSFS_writeBytes(file, source.data(), source.size()); PHYSFS_close(file); diff --git a/game/client/gui/progress_bar.cc b/game/client/gui/progress_bar.cc index 2bfb69e..8be2f8c 100644 --- a/game/client/gui/progress_bar.cc +++ b/game/client/gui/progress_bar.cc @@ -99,12 +99,12 @@ void gui::progress_bar::reset(void) button_action = nullptr; } -void gui::progress_bar::set_title(const char* title) +void gui::progress_bar::set_title(std::string_view title) { str_title = gui::language::resolve(title); } -void gui::progress_bar::set_button(const char* text, const progress_bar_action& action) +void gui::progress_bar::set_button(std::string_view text, const progress_bar_action& action) { str_button = std::format("{}###ProgressBar_Button", gui::language::resolve(text)); button_action = action; diff --git a/game/client/gui/progress_bar.hh b/game/client/gui/progress_bar.hh index 3765543..79fdcb5 100644 --- a/game/client/gui/progress_bar.hh +++ b/game/client/gui/progress_bar.hh @@ -16,8 +16,8 @@ void layout(void); namespace gui::progress_bar { void reset(void); -void set_title(const char* title); -void set_button(const char* text, const progress_bar_action& action); +void set_title(std::string_view title); +void set_button(std::string_view text, const progress_bar_action& action); } // namespace gui::progress_bar #endif // CLIENT_PROGRESS_BAR_HH diff --git a/game/client/gui/settings.cc b/game/client/gui/settings.cc index 034db4c..e8f9bca 100644 --- a/game/client/gui/settings.cc +++ b/game/client/gui/settings.cc @@ -311,7 +311,7 @@ void SettingValue_InputUnsigned::layout(void) const void SettingValue_InputString::layout(void) const { ImGuiInputTextFlags flags; - std::string current_value = value->get(); + std::string current_value(value->get_value()); if(allow_whitespace) { flags = ImGuiInputTextFlags_AllowTabInput; @@ -321,7 +321,7 @@ void SettingValue_InputString::layout(void) const } if(ImGui::InputText(wid.c_str(), ¤t_value, flags)) { - value->set(current_value.c_str()); + value->set(current_value); } layout_label(); @@ -847,7 +847,7 @@ void settings::layout(void) ImGui::End(); } -void settings::add_checkbox(int priority, config::Boolean& value, settings_location location, const char* name, bool tooltip) +void settings::add_checkbox(int priority, config::Boolean& value, settings_location location, std::string_view name, bool tooltip) { auto setting_value = new SettingValue_CheckBox; setting_value->type = setting_type::CHECKBOX; @@ -862,7 +862,7 @@ void settings::add_checkbox(int priority, config::Boolean& value, settings_locat values_all.push_back(setting_value); } -void settings::add_input(int priority, config::Int& value, settings_location location, const char* name, bool tooltip) +void settings::add_input(int priority, config::Int& value, settings_location location, std::string_view name, bool tooltip) { auto setting_value = new SettingValue_InputInt; setting_value->type = setting_type::INPUT_INT; @@ -877,13 +877,15 @@ void settings::add_input(int priority, config::Int& value, settings_location loc values_all.push_back(setting_value); } -void settings::add_input(int priority, config::Float& value, settings_location location, const char* name, bool tooltip, const char* format) +void settings::add_input( + int priority, config::Float& value, settings_location location, std::string_view name, bool tooltip, std::string_view fmt) { auto setting_value = new SettingValue_InputFloat; setting_value->type = setting_type::INPUT_FLOAT; setting_value->priority = priority; setting_value->has_tooltip = tooltip; setting_value->value = &value; + setting_value->format = fmt; setting_value->name = name; setting_value->wid = std::format("###{}", static_cast<const void*>(setting_value->value)); @@ -892,7 +894,7 @@ void settings::add_input(int priority, config::Float& value, settings_location l values_all.push_back(setting_value); } -void settings::add_input(int priority, config::Unsigned& value, settings_location location, const char* name, bool tooltip) +void settings::add_input(int priority, config::Unsigned& value, settings_location location, std::string_view name, bool tooltip) { auto setting_value = new SettingValue_InputUnsigned; setting_value->type = setting_type::INPUT_UINT; @@ -908,7 +910,7 @@ void settings::add_input(int priority, config::Unsigned& value, settings_locatio } void settings::add_input( - int priority, config::String& value, settings_location location, const char* name, bool tooltip, bool allow_whitespace) + int priority, config::String& value, settings_location location, std::string_view name, bool tooltip, bool allow_whitespace) { auto setting_value = new SettingValue_InputString; setting_value->type = setting_type::INPUT_STRING; @@ -924,7 +926,7 @@ void settings::add_input( values_all.push_back(setting_value); } -void settings::add_slider(int priority, config::Int& value, settings_location location, const char* name, bool tooltip) +void settings::add_slider(int priority, config::Int& value, settings_location location, std::string_view name, bool tooltip) { auto setting_value = new SettingValue_SliderInt; setting_value->type = setting_type::SLIDER_INT; @@ -940,7 +942,7 @@ void settings::add_slider(int priority, config::Int& value, settings_location lo } void settings::add_slider( - int priority, config::Float& value, settings_location location, const char* name, bool tooltip, const char* format) + int priority, config::Float& value, settings_location location, std::string_view name, bool tooltip, std::string_view fmt) { auto setting_value = new SettingValue_SliderFloat; setting_value->type = setting_type::SLIDER_FLOAT; @@ -949,14 +951,14 @@ void settings::add_slider( setting_value->value = &value; setting_value->name = name; - setting_value->format = format; + setting_value->format = fmt; setting_value->wid = std::format("###{}", static_cast<const void*>(setting_value->value)); values[static_cast<unsigned int>(location)].push_back(setting_value); values_all.push_back(setting_value); } -void settings::add_slider(int priority, config::Unsigned& value, settings_location location, const char* name, bool tooltip) +void settings::add_slider(int priority, config::Unsigned& value, settings_location location, std::string_view name, bool tooltip) { auto setting_value = new SettingValue_SliderUnsigned; setting_value->type = setting_type::SLIDER_UINT; @@ -971,7 +973,7 @@ void settings::add_slider(int priority, config::Unsigned& value, settings_locati values_all.push_back(setting_value); } -void settings::add_stepper(int priority, config::Int& value, settings_location location, const char* name, bool tooltip) +void settings::add_stepper(int priority, config::Int& value, settings_location location, std::string_view name, bool tooltip) { auto setting_value = new SettingValue_StepperInt; setting_value->type = setting_type::STEPPER_INT; @@ -987,7 +989,7 @@ void settings::add_stepper(int priority, config::Int& value, settings_location l values_all.push_back(setting_value); } -void settings::add_stepper(int priority, config::Unsigned& value, settings_location location, const char* name, bool tooltip) +void settings::add_stepper(int priority, config::Unsigned& value, settings_location location, std::string_view name, bool tooltip) { auto setting_value = new SettingValue_StepperUnsigned; setting_value->type = setting_type::STEPPER_UINT; @@ -1003,7 +1005,7 @@ void settings::add_stepper(int priority, config::Unsigned& value, settings_locat values_all.push_back(setting_value); } -void settings::add_keybind(int priority, config::KeyBind& value, settings_location location, const char* name) +void settings::add_keybind(int priority, config::KeyBind& value, settings_location location, std::string_view name) { auto setting_value = new SettingValue_KeyBind; setting_value->type = setting_type::KEYBIND; @@ -1018,7 +1020,7 @@ void settings::add_keybind(int priority, config::KeyBind& value, settings_locati values_all.push_back(setting_value); } -void settings::add_gamepad_axis(int priority, config::GamepadAxis& value, settings_location location, const char* name) +void settings::add_gamepad_axis(int priority, config::GamepadAxis& value, settings_location location, std::string_view name) { auto setting_value = new SettingValue_GamepadAxis; setting_value->type = setting_type::GAMEPAD_AXIS; @@ -1033,7 +1035,7 @@ void settings::add_gamepad_axis(int priority, config::GamepadAxis& value, settin values_all.push_back(setting_value); } -void settings::add_gamepad_button(int priority, config::GamepadButton& value, settings_location location, const char* name) +void settings::add_gamepad_button(int priority, config::GamepadButton& value, settings_location location, std::string_view name) { auto setting_value = new SettingValue_GamepadButton; setting_value->type = setting_type::GAMEPAD_BUTTON; @@ -1048,7 +1050,7 @@ void settings::add_gamepad_button(int priority, config::GamepadButton& value, se values_all.push_back(setting_value); } -void settings::add_language_select(int priority, settings_location location, const char* name) +void settings::add_language_select(int priority, settings_location location, std::string_view name) { auto setting_value = new SettingValue_Language; setting_value->type = setting_type::LANGUAGE_SELECT; diff --git a/game/client/gui/settings.hh b/game/client/gui/settings.hh index 15aa6a7..d807bc4 100644 --- a/game/client/gui/settings.hh +++ b/game/client/gui/settings.hh @@ -49,45 +49,46 @@ void layout(void); namespace settings { -void add_checkbox(int priority, config::Boolean& value, settings_location location, const char* name, bool tooltip); +void add_checkbox(int priority, config::Boolean& value, settings_location location, std::string_view name, bool tooltip); } // namespace settings namespace settings { -void add_input(int priority, config::Int& value, settings_location location, const char* name, bool tooltip); -void add_input(int priority, config::Float& value, settings_location location, const char* name, bool tooltip, const char* format = "%.3f"); -void add_input(int priority, config::Unsigned& value, settings_location location, const char* name, bool tooltip); -void add_input(int priority, config::String& value, settings_location location, const char* name, bool tooltip, bool allow_whitespace); +void add_input(int priority, config::Int& value, settings_location location, std::string_view name, bool tooltip); +void add_input( + int priority, config::Float& value, settings_location location, std::string_view name, bool tooltip, std::string_view fmt = "%.3f"); +void add_input(int priority, config::Unsigned& value, settings_location location, std::string_view name, bool tooltip); +void add_input(int priority, config::String& value, settings_location location, std::string_view name, bool tooltip, bool allow_whitespace); } // namespace settings namespace settings { -void add_slider(int priority, config::Int& value, settings_location location, const char* name, bool tooltip); +void add_slider(int priority, config::Int& value, settings_location location, std::string_view name, bool tooltip); void add_slider( - int priority, config::Float& value, settings_location location, const char* name, bool tooltip, const char* format = "%.3f"); -void add_slider(int priority, config::Unsigned& value, settings_location location, const char* name, bool tooltip); + int priority, config::Float& value, settings_location location, std::string_view name, bool tooltip, std::string_view format = "%.3f"); +void add_slider(int priority, config::Unsigned& value, settings_location location, std::string_view name, bool tooltip); } // namespace settings namespace settings { -void add_stepper(int priority, config::Int& value, settings_location location, const char* name, bool tooltip); -void add_stepper(int priority, config::Unsigned& value, settings_location location, const char* name, bool tooltip); +void add_stepper(int priority, config::Int& value, settings_location location, std::string_view name, bool tooltip); +void add_stepper(int priority, config::Unsigned& value, settings_location location, std::string_view name, bool tooltip); } // namespace settings namespace settings { -void add_keybind(int priority, config::KeyBind& value, settings_location location, const char* name); +void add_keybind(int priority, config::KeyBind& value, settings_location location, std::string_view name); } // namespace settings namespace settings { -void add_gamepad_axis(int priority, config::GamepadAxis& value, settings_location location, const char* name); -void add_gamepad_button(int priority, config::GamepadButton& value, settings_location location, const char* name); +void add_gamepad_axis(int priority, config::GamepadAxis& value, settings_location location, std::string_view name); +void add_gamepad_button(int priority, config::GamepadButton& value, settings_location location, std::string_view name); } // namespace settings namespace settings { -void add_language_select(int priority, settings_location location, const char* name); +void add_language_select(int priority, settings_location location, std::string_view name); } // namespace settings #endif // CLIENT_SETTINGS_HH diff --git a/game/client/gui/splash.cc b/game/client/gui/splash.cc index a144f97..9eed8d3 100644 --- a/game/client/gui/splash.cc +++ b/game/client/gui/splash.cc @@ -18,7 +18,7 @@ constexpr static ImGuiWindowFlags WINDOW_FLAGS = ImGuiWindowFlags_NoBackground | constexpr static int SPLASH_COUNT = 4; constexpr static std::size_t DELAY_MICROSECONDS = 2000000; -constexpr static const char* SPLASH_PATH = "textures/gui/client_splash.png"; +constexpr static std::string_view SPLASH_PATH = "textures/gui/client_splash.png"; static resource_ptr<TextureGUI> texture; static float texture_aspect; diff --git a/game/client/gui/window_title.cc b/game/client/gui/window_title.cc index 91e308f..6f46668 100644 --- a/game/client/gui/window_title.cc +++ b/game/client/gui/window_title.cc @@ -13,10 +13,10 @@ void gui::window_title::update(void) std::string title; if(globals::sound_ctx && globals::sound_dev) { - title = std::format("Voxelius {}: {}", project_version_string, splash::get()); + title = std::format("Voxelius {}: {}", version::semver, splash::get()); } else { - title = std::format("Voxelius {}: {} [NOSOUND]", project_version_string, splash::get()); + title = std::format("Voxelius {}: {} [NOSOUND]", version::semver, splash::get()); } glfwSetWindowTitle(globals::window, title.c_str()); diff --git a/game/client/io/gamepad.cc b/game/client/io/gamepad.cc index 9910950..d07ee86 100644 --- a/game/client/io/gamepad.cc +++ b/game/client/io/gamepad.cc @@ -91,7 +91,7 @@ void io::gamepad::init(void) settings::add_checkbox(0, io::gamepad::active, settings_location::GAMEPAD, "gamepad.active", true); settings::add_slider(1, io::gamepad::deadzone, settings_location::GAMEPAD, "gamepad.deadzone", true, "%.03f"); - auto mappings_path = io::cmdline::get("gpmap", "misc/gamecontrollerdb.txt"); + auto mappings_path = io::cmdline::get_cstr("gpmap", "misc/gamecontrollerdb.txt"); auto mappings_file = PHYSFS_openRead(mappings_path); if(mappings_file) { diff --git a/game/client/main.cc b/game/client/main.cc index cb3e209..06a0da2 100644 --- a/game/client/main.cc +++ b/game/client/main.cc @@ -157,7 +157,7 @@ int main(int argc, char** argv) shared_game::init(argc, argv); - spdlog::info("Voxelius Client {}", project_version_string); + spdlog::info("Voxelius Client {}", version::semver); glfwSetErrorCallback(&on_glfw_error); @@ -312,7 +312,7 @@ int main(int argc, char** argv) int vmode_width = DEFAULT_WIDTH; int vmode_height = DEFAULT_HEIGHT; - if(auto vmode = io::cmdline::get("mode")) { + if(auto vmode = io::cmdline::get_cstr("mode")) { std::sscanf(vmode, "%dx%d", &vmode_width, &vmode_height); vmode_height = math::max(vmode_height, MIN_HEIGHT); vmode_width = math::max(vmode_width, MIN_WIDTH); diff --git a/game/client/program.cc b/game/client/program.cc index 17441e6..e35716d 100644 --- a/game/client/program.cc +++ b/game/client/program.cc @@ -6,10 +6,10 @@ // This fills up the array of source lines and figures out // which lines are to be dynamically resolved as variant macros -static void parse_source(const char* source, std::vector<std::string>& out_lines, std::vector<GL_VariedMacro>& out_variants) +static void parse_source(std::string_view source, std::vector<std::string>& out_lines, std::vector<GL_VariedMacro>& out_variants) { std::string line; - std::istringstream stream = std::istringstream(source); + std::istringstream stream = std::istringstream(std::string(source)); unsigned long line_number = 0UL; out_lines.clear(); @@ -38,7 +38,7 @@ static void parse_source(const char* source, std::vector<std::string>& out_lines } } -static GLuint compile_shader(const char* path, const char* source, GLenum shader_stage) +static GLuint compile_shader(std::string_view path, const char* source, GLenum shader_stage) { GLuint shader = glCreateShader(shader_stage); glShaderSource(shader, 1, &source, nullptr); @@ -66,14 +66,14 @@ static GLuint compile_shader(const char* path, const char* source, GLenum shader return shader; } -bool GL_Program::setup(const char* vpath, const char* fpath) +bool GL_Program::setup(std::string_view vpath, std::string_view fpath) { destroy(); vert_path = std::string(vpath); frag_path = std::string(fpath); - auto vfile = PHYSFS_openRead(vpath); + auto vfile = PHYSFS_openRead(vert_path.c_str()); if(vfile == nullptr) { spdlog::warn("gl_program: {}: {}", vpath, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); @@ -84,7 +84,7 @@ bool GL_Program::setup(const char* vpath, const char* fpath) PHYSFS_readBytes(vfile, vsource.data(), vsource.size()); PHYSFS_close(vfile); - auto ffile = PHYSFS_openRead(fpath); + auto ffile = PHYSFS_openRead(frag_path.c_str()); if(ffile == nullptr) { spdlog::warn("gl_program: {}: {}", fpath, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); @@ -188,10 +188,10 @@ void GL_Program::destroy(void) needs_update = false; } -std::size_t GL_Program::add_uniform(const char* name) +std::size_t GL_Program::add_uniform(std::string_view name) { for(std::size_t i = 0; i < uniforms.size(); ++i) { - if(!uniforms[i].name.compare(name)) { + if(0 == uniforms[i].name.compare(name)) { return i; } } diff --git a/game/client/program.hh b/game/client/program.hh index ce2538b..4e796fa 100644 --- a/game/client/program.hh +++ b/game/client/program.hh @@ -15,11 +15,11 @@ struct GL_Uniform final { class GL_Program final { public: - bool setup(const char* vpath, const char* fpath); + bool setup(std::string_view vpath, std::string_view fpath); void destroy(void); bool update(void); - std::size_t add_uniform(const char* name); + std::size_t add_uniform(std::string_view name); void set_variant_vert(unsigned int variant, unsigned int value); void set_variant_frag(unsigned int variant, unsigned int value); diff --git a/game/client/resource/sound_effect.cc b/game/client/resource/sound_effect.cc index 0d987a5..75d5984 100644 --- a/game/client/resource/sound_effect.cc +++ b/game/client/resource/sound_effect.cc @@ -24,9 +24,9 @@ static drwav_bool32 drwav_seek_physfs(void* file, int offset, drwav_seek_origin } template<> -resource_ptr<SoundEffect> resource::load<SoundEffect>(const char* name, unsigned int flags) +resource_ptr<SoundEffect> resource::load<SoundEffect>(std::string_view name, unsigned int flags) { - auto it = resource_map.find(name); + auto it = resource_map.find(std::string(name)); if(it != resource_map.cend()) { // Return an existing resource @@ -38,7 +38,7 @@ resource_ptr<SoundEffect> resource::load<SoundEffect>(const char* name, unsigned return nullptr; } - auto file = PHYSFS_openRead(name); + auto file = PHYSFS_openRead(std::string(name).c_str()); if(file == nullptr) { spdlog::warn("resource: {} [SoundEffect]: {}", name, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); @@ -76,7 +76,7 @@ resource_ptr<SoundEffect> resource::load<SoundEffect>(const char* name, unsigned delete[] samples; - return resource_map.insert_or_assign(name, new_resource).first->second; + return resource_map.insert_or_assign(std::string(name), new_resource).first->second; } template<> diff --git a/game/client/resource/texture_gui.cc b/game/client/resource/texture_gui.cc index 971e201..415845d 100644 --- a/game/client/resource/texture_gui.cc +++ b/game/client/resource/texture_gui.cc @@ -8,9 +8,9 @@ static emhash8::HashMap<std::string, resource_ptr<TextureGUI>> resource_map; template<> -resource_ptr<TextureGUI> resource::load<TextureGUI>(const char* name, unsigned int flags) +resource_ptr<TextureGUI> resource::load<TextureGUI>(std::string_view name, unsigned int flags) { - auto it = resource_map.find(name); + auto it = resource_map.find(std::string(name)); if(it != resource_map.cend()) { // Return an existing resource @@ -67,7 +67,7 @@ resource_ptr<TextureGUI> resource::load<TextureGUI>(const char* name, unsigned i new_resource->size.x = image->size.x; new_resource->size.y = image->size.y; - return resource_map.insert_or_assign(name, new_resource).first->second; + return resource_map.insert_or_assign(std::string(name), new_resource).first->second; } return nullptr; diff --git a/game/client/session.cc b/game/client/session.cc index 521e0c2..5826b02 100644 --- a/game/client/session.cc +++ b/game/client/session.cc @@ -188,10 +188,10 @@ void session::invalidate(void) globals::dimension = nullptr; } -void session::connect(const char* host, std::uint16_t port, const char* password) +void session::connect(std::string_view host, std::uint16_t port, std::string_view password) { ENetAddress address; - enet_address_set_host(&address, host); + enet_address_set_host(&address, std::string(host).c_str()); address.port = port; session::peer = enet_host_connect(globals::client_host, &address, 1, 0); @@ -202,7 +202,7 @@ void session::connect(const char* host, std::uint16_t port, const char* password globals::fixed_frametime = 0.0f; globals::fixed_accumulator = 0; - server_password_hash = math::crc64(password); + server_password_hash = math::crc64(password.data(), password.size()); if(!session::peer) { server_password_hash = UINT64_MAX; @@ -245,7 +245,7 @@ void session::connect(const char* host, std::uint16_t port, const char* password globals::gui_screen = GUI_PROGRESS_BAR; } -void session::disconnect(const char* reason) +void session::disconnect(std::string_view reason) { if(session::peer) { protocol::Disconnect packet; @@ -279,8 +279,8 @@ void session::send_login_request(void) { protocol::LoginRequest packet; packet.version = protocol::VERSION; - packet.voxel_registry_checksum = world::voxel_registry::calcualte_checksum(); - packet.item_registry_checksum = world::item_registry::calcualte_checksum(); + packet.voxel_registry_checksum = world::voxel_registry::calculate_checksum(); + packet.item_registry_checksum = world::item_registry::calculate_checksum(); packet.password_hash = server_password_hash; packet.username = client_game::username.get(); diff --git a/game/client/session.hh b/game/client/session.hh index a763d18..8384a49 100644 --- a/game/client/session.hh +++ b/game/client/session.hh @@ -18,8 +18,8 @@ void invalidate(void); namespace session { -void connect(const char* hostname, std::uint16_t port, const char* password); -void disconnect(const char* reason); +void connect(std::string_view hostname, std::uint16_t port, std::string_view password); +void disconnect(std::string_view reason); void send_login_request(void); } // namespace session diff --git a/game/client/sound/sound.cc b/game/client/sound/sound.cc index f3bd5e2..c77f968 100644 --- a/game/client/sound/sound.cc +++ b/game/client/sound/sound.cc @@ -95,43 +95,43 @@ void sound::update(void) alSourcef(ui_source, AL_GAIN, ui_gain); } -void sound::play_generic(const char* sound, bool looping, float pitch) +void sound::play_generic(std::string_view sound, bool looping, float pitch) { - if(sound) { + if(sound.size()) { sound::play_generic(resource::load<SoundEffect>(sound), looping, pitch); } else { - sound::play_generic(static_cast<const char*>(nullptr), looping, pitch); + sound::play_generic(static_cast<resource_ptr<SoundEffect>>(nullptr), looping, pitch); } } -void sound::play_entity(entt::entity entity, const char* sound, bool looping, float pitch) +void sound::play_entity(entt::entity entity, std::string_view sound, bool looping, float pitch) { - if(sound) { + if(sound.size()) { sound::play_entity(entity, resource::load<SoundEffect>(sound), looping, pitch); } else { - sound::play_entity(entity, static_cast<const char*>(nullptr), looping, pitch); + sound::play_entity(entity, static_cast<resource_ptr<SoundEffect>>(nullptr), looping, pitch); } } -void sound::play_player(const char* sound, bool looping, float pitch) +void sound::play_player(std::string_view sound, bool looping, float pitch) { - if(sound) { + if(sound.size()) { sound::play_player(resource::load<SoundEffect>(sound), looping, pitch); } else { - sound::play_player(static_cast<const char*>(nullptr), looping, pitch); + sound::play_player(static_cast<resource_ptr<SoundEffect>>(nullptr), looping, pitch); } } -void sound::play_ui(const char* sound, bool looping, float pitch) +void sound::play_ui(std::string_view sound, bool looping, float pitch) { - if(sound) { + if(sound.size()) { sound::play_ui(resource::load<SoundEffect>(sound), looping, pitch); } else { - sound::play_ui(static_cast<const char*>(nullptr), looping, pitch); + sound::play_ui(static_cast<resource_ptr<SoundEffect>>(nullptr), looping, pitch); } } @@ -203,4 +203,4 @@ void sound::play_ui(resource_ptr<SoundEffect> sound, bool looping, float pitch) alSourcef(ui_source, AL_PITCH, math::clamp(pitch, MIN_PITCH, MAX_PITCH)); alSourcePlay(ui_source); } -}
\ No newline at end of file +} diff --git a/game/client/sound/sound.hh b/game/client/sound/sound.hh index 5eb3ed7..877c64a 100644 --- a/game/client/sound/sound.hh +++ b/game/client/sound/sound.hh @@ -30,10 +30,10 @@ void update(void); namespace sound { -void play_generic(const char* sound, bool looping, float pitch); -void play_entity(entt::entity entity, const char* sound, bool looping, float pitch); -void play_player(const char* sound, bool looping, float pitch); -void play_ui(const char* sound, bool looping, float pitch); +void play_generic(std::string_view sound, bool looping, float pitch); +void play_entity(entt::entity entity, std::string_view sound, bool looping, float pitch); +void play_player(std::string_view sound, bool looping, float pitch); +void play_ui(std::string_view sound, bool looping, float pitch); } // namespace sound namespace sound diff --git a/game/client/toggles.cc b/game/client/toggles.cc index 96c4ae4..e6ffc26 100644 --- a/game/client/toggles.cc +++ b/game/client/toggles.cc @@ -13,7 +13,7 @@ #include "client/globals.hh" struct ToggleInfo final { - const char* description; + std::string_view description; int glfw_keycode; bool is_enabled; }; @@ -24,7 +24,7 @@ static ToggleInfo toggle_infos[TOGGLE_COUNT]; static void print_toggle_state(const ToggleInfo& info) { - if(info.description) { + if(info.description.size()) { if(info.is_enabled) { gui::client_chat::print(std::format("[toggles] {} ON", info.description)); } @@ -102,7 +102,7 @@ void toggles::init(void) toggle_infos[TOGGLE_CHUNK_AABB].glfw_keycode = GLFW_KEY_G; toggle_infos[TOGGLE_CHUNK_AABB].is_enabled = false; - toggle_infos[TOGGLE_METRICS_UI].description = nullptr; + toggle_infos[TOGGLE_METRICS_UI].description = std::string_view(); toggle_infos[TOGGLE_METRICS_UI].glfw_keycode = GLFW_KEY_V; toggle_infos[TOGGLE_METRICS_UI].is_enabled = false; @@ -114,10 +114,6 @@ void toggles::init(void) toggle_infos[TOGGLE_PM_FLIGHT].glfw_keycode = GLFW_KEY_F; toggle_infos[TOGGLE_PM_FLIGHT].is_enabled = false; -#ifndef NDEBUG - toggle_infos[TOGGLE_WIREFRAME].is_enabled = true; -#endif - globals::dispatcher.sink<io::GlfwKeyEvent>().connect<&on_glfw_key>(); } diff --git a/game/client/world/voxel_sounds.cc b/game/client/world/voxel_sounds.cc index 235a851..71ea1fc 100644 --- a/game/client/world/voxel_sounds.cc +++ b/game/client/world/voxel_sounds.cc @@ -9,7 +9,7 @@ constexpr static std::size_t NUM_SURFACES = static_cast<std::size_t>(world::voxe static std::vector<resource_ptr<SoundEffect>> footsteps_sounds[NUM_SURFACES]; static std::mt19937_64 randomizer; -static void add_footsteps_effect(world::voxel_surface surface, const char* name) +static void add_footsteps_effect(world::voxel_surface surface, std::string_view name) { if(auto effect = resource::load<SoundEffect>(name)) { auto surface_index = static_cast<std::size_t>(surface); |
