Skip to content

Commit

Permalink
sgOptions -> GetOptions()
Browse files Browse the repository at this point in the history
In C++, globals initialization order accross translation units is not
defined. Accessing a global via a function ensures that it is initialized.

This will be needed for diasurgical#7638, which will statically initialize change
handlers after the Options object has been initialized.
  • Loading branch information
glebm committed Jan 11, 2025
1 parent 651c70f commit fd70d99
Show file tree
Hide file tree
Showing 50 changed files with 447 additions and 428 deletions.
6 changes: 3 additions & 3 deletions Source/DiabloUI/diabloui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ void UiHandleEvents(SDL_Event *event)
if (event->type == SDL_KEYDOWN && event->key.keysym.sym == SDLK_RETURN) {
const Uint8 *state = SDLC_GetKeyState();
if (state[SDLC_KEYSTATE_LALT] != 0 || state[SDLC_KEYSTATE_RALT] != 0) {
sgOptions.Graphics.fullscreen.SetValue(!IsFullScreen());
GetOptions().Graphics.fullscreen.SetValue(!IsFullScreen());
SaveOptions();
if (gfnFullscreen != nullptr)
gfnFullscreen();
Expand All @@ -455,9 +455,9 @@ void UiHandleEvents(SDL_Event *event)
// For example, if the previous size was too large for a hardware cursor then it was invisible
// but may now become visible.
DoReinitializeHardwareCursor();
} else if (event->window.event == SDL_WINDOWEVENT_FOCUS_LOST && *sgOptions.Gameplay.pauseOnFocusLoss) {
} else if (event->window.event == SDL_WINDOWEVENT_FOCUS_LOST && *GetOptions().Gameplay.pauseOnFocusLoss) {
music_mute();
} else if (event->window.event == SDL_WINDOWEVENT_FOCUS_GAINED && *sgOptions.Gameplay.pauseOnFocusLoss) {
} else if (event->window.event == SDL_WINDOWEVENT_FOCUS_GAINED && *GetOptions().Gameplay.pauseOnFocusLoss) {
diablo_focus_unpause();
}
}
Expand Down
4 changes: 2 additions & 2 deletions Source/DiabloUI/hero/selhero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ void SelheroListSelect(size_t value)
vecSelHeroDlgItems.push_back(std::make_unique<UiListItem>(_("Sorcerer"), static_cast<int>(HeroClass::Sorcerer)));
if (gbIsHellfire) {
vecSelHeroDlgItems.push_back(std::make_unique<UiListItem>(_("Monk"), static_cast<int>(HeroClass::Monk)));
if (HaveBardAssets() || *sgOptions.Gameplay.testBard) {
if (HaveBardAssets() || *GetOptions().Gameplay.testBard) {
vecSelHeroDlgItems.push_back(std::make_unique<UiListItem>(_("Bard"), static_cast<int>(HeroClass::Bard)));
}
if (HaveBarbarianAssets() || *sgOptions.Gameplay.testBarbarian) {
if (HaveBarbarianAssets() || *GetOptions().Gameplay.testBarbarian) {
vecSelHeroDlgItems.push_back(std::make_unique<UiListItem>(_("Barbarian"), static_cast<int>(HeroClass::Barbarian)));
}
}
Expand Down
14 changes: 7 additions & 7 deletions Source/DiabloUI/multi/selgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ void UiInitGameSelectionList(std::string_view search)
}

if (provider == SELCONN_ZT) {
CopyUtf8(selgame_Ip, sgOptions.Network.szPreviousZTGame, sizeof(selgame_Ip));
CopyUtf8(selgame_Ip, GetOptions().Network.szPreviousZTGame, sizeof(selgame_Ip));
} else {
CopyUtf8(selgame_Ip, sgOptions.Network.szPreviousHost, sizeof(selgame_Ip));
CopyUtf8(selgame_Ip, GetOptions().Network.szPreviousHost, sizeof(selgame_Ip));
}

selgame_FreeVectors();
Expand Down Expand Up @@ -622,9 +622,9 @@ void selgame_Password_Select(size_t /*value*/)
for (unsigned int i = 0; i < (sizeof(selgame_Ip) / sizeof(selgame_Ip[0])); i++) {
selgame_Ip[i] = (selgame_Ip[i] >= 'A' && selgame_Ip[i] <= 'Z') ? selgame_Ip[i] + 'a' - 'A' : selgame_Ip[i];
}
strcpy(sgOptions.Network.szPreviousZTGame, selgame_Ip);
strcpy(GetOptions().Network.szPreviousZTGame, selgame_Ip);
} else {
strcpy(sgOptions.Network.szPreviousHost, selgame_Ip);
strcpy(GetOptions().Network.szPreviousHost, selgame_Ip);
}
if (allowJoin && SNetJoinGame(selgame_Ip, gamePassword, gdwPlayerId)) {
if (!IsGameCompatibleWithErrorMessage(*m_game_data)) {
Expand Down Expand Up @@ -657,9 +657,9 @@ void selgame_Password_Select(size_t /*value*/)

m_game_data->nDifficulty = nDifficulty;
m_game_data->nTickRate = nTickRate;
m_game_data->bRunInTown = *sgOptions.Gameplay.runInTown ? 1 : 0;
m_game_data->bTheoQuest = *sgOptions.Gameplay.theoQuest ? 1 : 0;
m_game_data->bCowQuest = *sgOptions.Gameplay.cowQuest ? 1 : 0;
m_game_data->bRunInTown = *GetOptions().Gameplay.runInTown ? 1 : 0;
m_game_data->bTheoQuest = *GetOptions().Gameplay.theoQuest ? 1 : 0;
m_game_data->bCowQuest = *GetOptions().Gameplay.cowQuest ? 1 : 0;

GameData gameInitInfo = *m_game_data;
gameInitInfo.swapLE();
Expand Down
2 changes: 1 addition & 1 deletion Source/DiabloUI/selstart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ std::vector<std::unique_ptr<UiItemBase>> vecDialog;
void ItemSelected(size_t value)
{
auto option = static_cast<StartUpGameMode>(vecDialogItems[value]->m_value);
sgOptions.GameMode.gameMode.SetValue(option);
GetOptions().GameMode.gameMode.SetValue(option);
SaveOptions();
endMenu = true;
}
Expand Down
8 changes: 4 additions & 4 deletions Source/DiabloUI/settingsmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void ItemFocused(size_t value)
optionDescription[0] = '\0';
if (vecItem->m_value < 0)
return;
auto *pCategory = sgOptions.GetCategories()[vecItem->m_value];
auto *pCategory = GetOptions().GetCategories()[vecItem->m_value];
UpdateDescription(*pCategory);
} break;
case ShownMenuType::Settings: {
Expand Down Expand Up @@ -254,7 +254,7 @@ void ItemSelected(size_t value)

switch (shownMenu) {
case ShownMenuType::Categories: {
selectedCategory = sgOptions.GetCategories()[vecItemValue];
selectedCategory = GetOptions().GetCategories()[vecItemValue];
endMenu = true;
shownMenu = ShownMenuType::Settings;
} break;
Expand Down Expand Up @@ -318,7 +318,7 @@ void EscPressed()

void FullscreenChanged()
{
auto *fullscreenOption = &sgOptions.Graphics.fullscreen;
auto *fullscreenOption = &GetOptions().Graphics.fullscreen;

for (auto &vecItem : vecDialogItems) {
int vecItemValue = vecItem->m_value;
Expand Down Expand Up @@ -384,7 +384,7 @@ void UiSettingsMenu()
switch (shownMenu) {
case ShownMenuType::Categories: {
size_t catIndex = 0;
for (OptionCategoryBase *pCategory : sgOptions.GetCategories()) {
for (OptionCategoryBase *pCategory : GetOptions().GetCategories()) {
for (OptionEntryBase *pEntry : pCategory->GetEntries()) {
if (!IsValidEntry(pEntry))
continue;
Expand Down
2 changes: 1 addition & 1 deletion Source/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,7 @@ void DiabloHotkeyMsg(uint32_t dwMsg)

assert(dwMsg < QUICK_MESSAGE_OPTIONS);

for (const std::string &msg : sgOptions.Chat.szHotKeyMsgs[dwMsg]) {
for (const std::string &msg : GetOptions().Chat.szHotKeyMsgs[dwMsg]) {
#ifdef _DEBUG
constexpr std::string_view LuaPrefix = "/lua ";
if (msg.starts_with(LuaPrefix)) {
Expand Down
32 changes: 18 additions & 14 deletions Source/controls/controller_motion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,16 @@ void ScaleJoystickAxes(float *x, float *y, float deadzone)
bool IsMovementOverriddenByPadmapper(ControllerButton button)
{
ControllerButtonEvent releaseEvent { button, true };
std::string_view actionName = sgOptions.Padmapper.ActionNameTriggeredByButtonEvent(releaseEvent);
ControllerButtonCombo buttonCombo = sgOptions.Padmapper.ButtonComboForAction(actionName);
const Options &options = GetOptions();
std::string_view actionName = options.Padmapper.ActionNameTriggeredByButtonEvent(releaseEvent);
ControllerButtonCombo buttonCombo = options.Padmapper.ButtonComboForAction(actionName);
return buttonCombo.modifier != ControllerButton_NONE;
}

bool TriggersQuickSpellAction(ControllerButton button)
{
ControllerButtonEvent releaseEvent { button, true };
std::string_view actionName = sgOptions.Padmapper.ActionNameTriggeredByButtonEvent(releaseEvent);
std::string_view actionName = GetOptions().Padmapper.ActionNameTriggeredByButtonEvent(releaseEvent);

std::string_view prefix { "QuickSpell" };
if (actionName.size() < prefix.size())
Expand Down Expand Up @@ -118,8 +119,9 @@ namespace {

void ScaleJoysticks()
{
const float rightDeadzone = sgOptions.Controller.fDeadzone;
const float leftDeadzone = sgOptions.Controller.fDeadzone;
const Options &options = GetOptions();
const float rightDeadzone = options.Controller.fDeadzone;
const float leftDeadzone = options.Controller.fDeadzone;

if (leftStickNeedsScaling) {
leftStickX = leftStickXUnscaled;
Expand Down Expand Up @@ -208,11 +210,12 @@ AxisDirection GetLeftStickOrDpadDirection(bool usePadmapper)
bool isLeftPressed = stickX <= -0.5;
bool isRightPressed = stickX >= 0.5;

const Options &options = GetOptions();
if (usePadmapper) {
isUpPressed |= sgOptions.Padmapper.IsActive("MoveUp");
isDownPressed |= sgOptions.Padmapper.IsActive("MoveDown");
isLeftPressed |= sgOptions.Padmapper.IsActive("MoveLeft");
isRightPressed |= sgOptions.Padmapper.IsActive("MoveRight");
isUpPressed |= options.Padmapper.IsActive("MoveUp");
isDownPressed |= options.Padmapper.IsActive("MoveDown");
isLeftPressed |= options.Padmapper.IsActive("MoveLeft");
isRightPressed |= options.Padmapper.IsActive("MoveRight");
} else if (!SimulatingMouseWithPadmapper) {
isUpPressed |= IsPressedForMovement(ControllerButton_BUTTON_DPAD_UP);
isDownPressed |= IsPressedForMovement(ControllerButton_BUTTON_DPAD_DOWN);
Expand Down Expand Up @@ -251,7 +254,8 @@ void SimulateRightStickWithPadmapper(ControllerButtonEvent ctrlEvent)
if (!ctrlEvent.up && ctrlEvent.button == SuppressedButton)
return;

std::string_view actionName = sgOptions.Padmapper.ActionNameTriggeredByButtonEvent(ctrlEvent);
const Options &options = GetOptions();
std::string_view actionName = options.Padmapper.ActionNameTriggeredByButtonEvent(ctrlEvent);
bool upTriggered = actionName == "MouseUp";
bool downTriggered = actionName == "MouseDown";
bool leftTriggered = actionName == "MouseLeft";
Expand All @@ -262,10 +266,10 @@ void SimulateRightStickWithPadmapper(ControllerButtonEvent ctrlEvent)
return;
}

bool upActive = (upTriggered && !ctrlEvent.up) || (!upTriggered && sgOptions.Padmapper.IsActive("MouseUp"));
bool downActive = (downTriggered && !ctrlEvent.up) || (!downTriggered && sgOptions.Padmapper.IsActive("MouseDown"));
bool leftActive = (leftTriggered && !ctrlEvent.up) || (!leftTriggered && sgOptions.Padmapper.IsActive("MouseLeft"));
bool rightActive = (rightTriggered && !ctrlEvent.up) || (!rightTriggered && sgOptions.Padmapper.IsActive("MouseRight"));
bool upActive = (upTriggered && !ctrlEvent.up) || (!upTriggered && options.Padmapper.IsActive("MouseUp"));
bool downActive = (downTriggered && !ctrlEvent.up) || (!downTriggered && options.Padmapper.IsActive("MouseDown"));
bool leftActive = (leftTriggered && !ctrlEvent.up) || (!leftTriggered && options.Padmapper.IsActive("MouseLeft"));
bool rightActive = (rightTriggered && !ctrlEvent.up) || (!rightTriggered && options.Padmapper.IsActive("MouseRight"));

rightStickX = 0;
rightStickY = 0;
Expand Down
14 changes: 7 additions & 7 deletions Source/controls/game_controls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ void PressControllerButton(ControllerButton button)
SetSpeedSpell(slot);
return;
}
if (!*sgOptions.Gameplay.quickCast)
if (!*GetOptions().Gameplay.quickCast)
ToggleSpell(slot);
else
QuickCast(slot);
Expand Down Expand Up @@ -289,7 +289,7 @@ void PressControllerButton(ControllerButton button)
return;
case devilution::ControllerButton_BUTTON_Y:
#ifdef __3DS__
sgOptions.Graphics.zoom.SetValue(!*sgOptions.Graphics.zoom);
GetOptions().Graphics.zoom.SetValue(!*GetOptions().Graphics.zoom);
CalcViewportGeometry();
#endif
return;
Expand All @@ -298,7 +298,7 @@ void PressControllerButton(ControllerButton button)
}
}

sgOptions.Padmapper.ButtonPressed(button);
GetOptions().Padmapper.ButtonPressed(button);
}

} // namespace
Expand Down Expand Up @@ -337,7 +337,7 @@ bool IsSimulatedMouseClickBinding(ControllerButtonEvent ctrlEvent)
return false;
if (!ctrlEvent.up && ctrlEvent.button == SuppressedButton)
return false;
std::string_view actionName = sgOptions.Padmapper.ActionNameTriggeredByButtonEvent(ctrlEvent);
std::string_view actionName = GetOptions().Padmapper.ActionNameTriggeredByButtonEvent(ctrlEvent);
return IsAnyOf(actionName, "LeftMouseClick1", "LeftMouseClick2", "RightMouseClick1", "RightMouseClick2");
}

Expand All @@ -356,7 +356,7 @@ bool HandleControllerButtonEvent(const SDL_Event &event, const ControllerButtonE
~ButtonReleaser()
{
if (ctrlEvent.up)
sgOptions.Padmapper.ButtonReleased(ctrlEvent.button, false);
GetOptions().Padmapper.ButtonReleased(ctrlEvent.button, false);
}
ControllerButtonEvent ctrlEvent;
};
Expand All @@ -377,10 +377,10 @@ bool HandleControllerButtonEvent(const SDL_Event &event, const ControllerButtonE
SuppressedButton = ControllerButton_NONE;
}

if (ctrlEvent.up && sgOptions.Padmapper.ActionNameTriggeredByButtonEvent(ctrlEvent) != "") {
if (ctrlEvent.up && GetOptions().Padmapper.ActionNameTriggeredByButtonEvent(ctrlEvent) != "") {
// Button press may have brought up a menu;
// don't confuse release of that button with intent to interact with the menu
sgOptions.Padmapper.ButtonReleased(ctrlEvent.button);
GetOptions().Padmapper.ButtonReleased(ctrlEvent.button);
return true;
} else if (GetGameAction(event, ctrlEvent, &action)) {
ProcessGameAction(action);
Expand Down
2 changes: 1 addition & 1 deletion Source/controls/plrctrls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ void FindTrigger()
bool IsStandingGround()
{
if (ControlMode == ControlTypes::Gamepad) {
ControllerButtonCombo standGroundCombo = sgOptions.Padmapper.ButtonComboForAction("StandGround");
ControllerButtonCombo standGroundCombo = GetOptions().Padmapper.ButtonComboForAction("StandGround");
return StandToggle || IsControllerButtonComboPressed(standGroundCombo);
}
#ifndef USE_SDL1
Expand Down
8 changes: 4 additions & 4 deletions Source/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,15 @@ bool TrySelectPixelBased(Point tile)
const Point renderPosition = GetScreenPosition(renderingTile) + renderingOffset;
Point spriteTopLeft = renderPosition - Displacement { 0, sprite.height() };
Size spriteSize = { sprite.width(), sprite.height() };
if (*sgOptions.Graphics.zoom) {
if (*GetOptions().Graphics.zoom) {
spriteSize *= 2;
spriteTopLeft *= 2;
}
const Rectangle spriteCoords = Rectangle(spriteTopLeft, spriteSize);
if (!spriteCoords.contains(MousePosition))
return false;
Point pointInSprite = Point { 0, 0 } + (MousePosition - spriteCoords.position);
if (*sgOptions.Graphics.zoom)
if (*GetOptions().Graphics.zoom)
pointInSprite /= 2;
return IsPointWithinClx(pointInSprite, sprite);
};
Expand Down Expand Up @@ -666,7 +666,7 @@ void AlterMousePositionViaScrolling(Point &screenPosition, Rectangle mainPanel)
*/
void AlterMousePositionViaZoom(Point &screenPosition)
{
if (*sgOptions.Graphics.zoom) {
if (*GetOptions().Graphics.zoom) {
screenPosition.x /= 2;
screenPosition.y /= 2;
}
Expand Down Expand Up @@ -722,7 +722,7 @@ Point ConvertToTileGrid(Point &screenPosition)
currentTile.y++;
}

if (*sgOptions.Graphics.zoom) {
if (*GetOptions().Graphics.zoom) {
screenPosition.y -= TILE_HEIGHT / 4;
}

Expand Down
Loading

0 comments on commit fd70d99

Please sign in to comment.