Skip to content

Commit

Permalink
Move option change handlers out of options.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Jan 11, 2025
1 parent fd70d99 commit f702588
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 127 deletions.
18 changes: 13 additions & 5 deletions Source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ set(libdevilutionx_SRCS

controls/axis_direction.cpp
controls/controller.cpp
controls/controller_buttons.cpp
controls/controller_motion.cpp
controls/devices/joystick.cpp
controls/devices/kbcontroller.cpp
Expand Down Expand Up @@ -213,6 +212,13 @@ target_link_dependencies(libdevilutionx_codec PRIVATE
libdevilutionx_log
)

add_devilutionx_object_library(libdevilutionx_controller_buttons
controls/controller_buttons.cpp
)
target_link_dependencies(libdevilutionx_controller_buttons
DevilutionX::SDL
)

add_devilutionx_object_library(libdevilutionx_crawl
crawl.cpp
)
Expand Down Expand Up @@ -250,6 +256,10 @@ target_link_dependencies(libdevilutionx_format_int PUBLIC
add_devilutionx_object_library(libdevilutionx_game_mode
game_mode.cpp
)
target_link_dependencies(libdevilutionx_game_mode PRIVATE
tl
libdevilutionx_options
)

add_devilutionx_object_library(libdevilutionx_gendung
levels/crypt.cpp
Expand Down Expand Up @@ -406,12 +416,9 @@ add_devilutionx_object_library(libdevilutionx_options
)
target_link_dependencies(libdevilutionx_options PUBLIC
DevilutionX::SDL
SDL_audiolib::SDL_audiolib
fmt::fmt
tl
${LUA_LIBRARIES}
sol2::sol2
libdevilutionx_game_mode
libdevilutionx_controller_buttons
libdevilutionx_logged_fstream
libdevilutionx_strings
libdevilutionx_ini
Expand Down Expand Up @@ -632,6 +639,7 @@ target_link_dependencies(libdevilutionx PUBLIC
libdevilutionx_clx_render
libdevilutionx_codec
libdevilutionx_config
libdevilutionx_controller_buttons
libdevilutionx_crawl
libdevilutionx_direction
libdevilutionx_surface
Expand Down
14 changes: 9 additions & 5 deletions Source/controls/controller_buttons.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "controller_buttons.h"

#include "plrctrls.h"
#include "controls/game_controls.h"

namespace devilution {
namespace {
Expand Down Expand Up @@ -280,17 +280,21 @@ std::string_view ToXboxIcon(ControllerButton button)

} // namespace

// Defined in `plrctrls.cpp`.
// Declared here to avoid having to depend on it in tests.
extern GamepadLayout GamepadType;

std::string_view ToString(ControllerButton button)
{
switch (GamepadType) {
case devilution::GamepadLayout::PlayStation:
case GamepadLayout::PlayStation:
return ToPlayStationIcon(button);
case devilution::GamepadLayout::Nintendo:
case GamepadLayout::Nintendo:
return ToNintendoIcon(button);
case devilution::GamepadLayout::Xbox:
case GamepadLayout::Xbox:
return ToXboxIcon(button);
default:
case devilution::GamepadLayout::Generic:
case GamepadLayout::Generic:
return ToGenericButtonText(button);
}
}
Expand Down
8 changes: 8 additions & 0 deletions Source/discord/discord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
#include "utils/str_cat.hpp"

namespace devilution {
namespace {
void IsHellfireChanged()
{
discord_manager::UpdateMenu(true);
}
const auto IsHellfireChangedHandler = (AddIsHellfireChangeHandler(IsHellfireChanged), true);
} // namespace

namespace discord_manager {

// App ID used for DevilutionX's Diablo (classic Diablo's is 496571953147150354)
Expand Down
9 changes: 9 additions & 0 deletions Source/engine/render/scrollrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,15 @@ void DrawMain(int dwHgt, bool drawDesc, bool drawHp, bool drawMana, bool drawSba
}
}

void OptionShowFPSChanged()
{
if (*GetOptions().Graphics.showFPS)
EnableFrameCount();
else
frameflag = false;
}
const auto OptionChangeHandlerShowFPS = (GetOptions().Graphics.showFPS.SetValueChangedCallback(OptionShowFPSChanged), true);

} // namespace

Displacement GetOffsetForWalking(const AnimationInfo &animationInfo, const Direction dir, bool cameraMode /*= false*/)
Expand Down
10 changes: 10 additions & 0 deletions Source/engine/render/text_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "engine/render/clx_render.hpp"
#include "engine/render/primitive_render.hpp"
#include "engine/ticks.hpp"
#include "options.h"
#include "utils/algorithm/container.hpp"
#include "utils/display.h"
#include "utils/is_of.hpp"
Expand Down Expand Up @@ -475,6 +476,15 @@ uint32_t DoDrawString(const Surface &out, std::string_view text, Rectangle rect,
return static_cast<uint32_t>(remaining.data() - text.data());
}

void OptionLanguageCodeChanged()
{
UnloadFonts();
LanguageInitialize();
LoadLanguageArchive();
}

const auto OptionChangeHandlerResolution = (GetOptions().Language.code.SetValueChangedCallback(OptionLanguageCodeChanged), true);

} // namespace

void LoadSmallSelectionSpinner()
Expand Down
20 changes: 20 additions & 0 deletions Source/engine/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,26 @@ int CapVolume(int volume)
return std::clamp(volume, VOLUME_MIN, VOLUME_MAX);
}

void OptionAudioChanged()
{
effects_cleanup_sfx();
music_stop();
snd_deinit();
snd_init();
music_start(TMUSIC_INTRO);
if (gbRunGame)
sound_init();
else
ui_sound_init();
}

const auto OptionChangeSampleRate = (GetOptions().Audio.sampleRate.SetValueChangedCallback(OptionAudioChanged), true);
const auto OptionChangeChannels = (GetOptions().Audio.channels.SetValueChangedCallback(OptionAudioChanged), true);
const auto OptionChangeBufferSize = (GetOptions().Audio.bufferSize.SetValueChangedCallback(OptionAudioChanged), true);
const auto OptionChangeResamplingQuality = (GetOptions().Audio.resamplingQuality.SetValueChangedCallback(OptionAudioChanged), true);
const auto OptionChangeResampler = (GetOptions().Audio.resampler.SetValueChangedCallback(OptionAudioChanged), true);
const auto OptionChangeDevice = (GetOptions().Audio.device.SetValueChangedCallback(OptionAudioChanged), true);

} // namespace

void ClearDuplicateSounds()
Expand Down
29 changes: 29 additions & 0 deletions Source/game_mode.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
#include "game_mode.hpp"

#include <function_ref.hpp>

#include "options.h"

namespace devilution {
namespace {
std::vector<tl::function_ref<void()>> IsHellfireChangeHandlers;

void OptionGameModeChanged()
{
gbIsHellfire = *GetOptions().GameMode.gameMode == StartUpGameMode::Hellfire;
for (tl::function_ref<void()> handler : IsHellfireChangeHandlers) {
handler();
}
}
const auto OptionChangeHandlerGameMode = (GetOptions().GameMode.gameMode.SetValueChangedCallback(OptionGameModeChanged), true);

void OptionSharewareChanged()
{
gbIsSpawn = *GetOptions().GameMode.shareware;
}
const auto OptionChangeHandlerShareware = (GetOptions().GameMode.shareware.SetValueChangedCallback(OptionSharewareChanged), true);
} // namespace

bool gbIsSpawn;
bool gbIsHellfire;
bool gbVanilla;
bool forceHellfire;

void AddIsHellfireChangeHandler(tl::function_ref<void()> callback)
{
IsHellfireChangeHandlers.push_back(callback);
}

} // namespace devilution
5 changes: 5 additions & 0 deletions Source/game_mode.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <function_ref.hpp>

#include "utils/attributes.h"

namespace devilution {
Expand All @@ -13,4 +15,7 @@ extern DVL_API_FOR_TEST bool gbVanilla;
/** Whether the Hellfire mode is required (forced). */
extern bool forceHellfire;

/** Adds a handler to be called then `gbIsHellfire` changes after the initial startup. */
void AddIsHellfireChangeHandler(tl::function_ref<void()> callback);

} // namespace devilution
4 changes: 4 additions & 0 deletions Source/lua/lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ void LuaInitialize()
// Used by the custom require implementation.
lua["setEnvironment"] = [](const sol::environment &env, const sol::function &fn) { sol::set_environment(env, fn); };

for (OptionEntryBase *mod : GetOptions().Mods.GetEntries()) {
mod->SetValueChangedCallback(LuaReloadActiveMods);
}

LuaReloadActiveMods();
}

Expand Down
Loading

0 comments on commit f702588

Please sign in to comment.