Skip to content

Commit b6879da

Browse files
authored
Fix deferred lighting Option (#6346)
* Fix deferred lighting option * Fix issue * Fix premature cache * Appease Clang * cache specific option not options in general
1 parent 45d9926 commit b6879da

File tree

5 files changed

+29
-15
lines changed

5 files changed

+29
-15
lines changed

code/lighting/lighting.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,13 @@ static auto DeferredLightingOption = options::OptionBuilder<bool>("Graphics.Defe
118118
bool light_deferred_enabled()
119119
{
120120
if (Using_in_game_options) {
121+
static bool isToggledOn = DeferredLightingOption->getValue();
121122
// This used to be getting the value of the option object itself,
122123
// however that is not a free operation and changing it requires a restart anyway
123124
// if the restart requirement is lifted care should be taken to cache this value
124125
// and never look it up more than once a frame
125126
// otherwise the performance footprint is measurable enough to worry about.
126-
return DeferredLightingEnabled;
127+
return isToggledOn;
127128
} else {
128129
return !Cmdline_no_deferred_lighting;
129130
}

code/options/Option.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ void OptionBase::setFlags(const flagset<OptionFlags>& flags) {
208208
_flags = flags;
209209
}
210210

211+
bool OptionBase::getIsOnce() const {
212+
return _is_once;
213+
}
214+
215+
void OptionBase::setIsOnce(bool is_once) {
216+
_is_once = is_once;
217+
}
218+
211219
//persists any changes made to this specific option and returns whether or not it was successful
212220
bool OptionBase::persistChanges() const { return _parent->persistOptionChanges(this); }
213221

code/options/Option.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class OptionBase {
7474
float _min = 0;
7575
float _max = 1;
7676

77+
bool _is_once = false;
78+
7779
SCP_unordered_map<PresetKind, SCP_string> _preset_values;
7880

7981
flagset<OptionFlags> _flags;
@@ -100,6 +102,9 @@ class OptionBase {
100102
const flagset<OptionFlags>& getFlags() const;
101103
void setFlags(const flagset<OptionFlags>& flags);
102104

105+
bool getIsOnce() const;
106+
void setIsOnce(bool is_once);
107+
103108
const SCP_string& getConfigKey() const;
104109
const SCP_string& getTitle() const;
105110
const SCP_string& getDescription() const;
@@ -519,6 +524,7 @@ class OptionBuilder {
519524
//The global variable to bind the option to once. Will require game restart to persist changes.
520525
OptionBuilder& bind_to_once(T* dest)
521526
{
527+
_instance.setIsOnce(true);
522528
return change_listener([dest](const T& val, bool initial) {
523529
if (initial) {
524530
*dest = val;
@@ -555,13 +561,13 @@ class OptionBuilder {
555561
return *this;
556562
}
557563
//Finishes building the option and returns a pointer to it
558-
const Option<T>* finish()
564+
std::shared_ptr<const Option<T>> finish()
559565
{
560566
for (auto& val : _preset_values) {
561567
_instance.setPreset(val.first, json_dump_string_new(_instance.getSerializer()(val.second),
562568
JSON_COMPACT | JSON_ENSURE_ASCII | JSON_ENCODE_ANY));
563569
}
564-
std::unique_ptr<Option<T>> opt_ptr(new Option<T>(_instance));
570+
auto opt_ptr = make_shared<Option<T>>(_instance);
565571

566572
if (mpark::holds_alternative<std::pair<const char*, int>>(_title)) {
567573
const auto& xstr_info = mpark::get<std::pair<const char*, int>>(_title);
@@ -573,9 +579,8 @@ class OptionBuilder {
573579
lcl_delayed_xstr(opt_ptr->_description, xstr_info.first, xstr_info.second);
574580
}
575581

576-
auto ptr = opt_ptr.get(); // We need to get the pointer now since we loose the type information otherwise
577-
OptionsManager::instance()->addOption(std::unique_ptr<OptionBase>(opt_ptr.release()));
578-
return ptr;
582+
OptionsManager::instance()->addOption(opt_ptr);
583+
return opt_ptr;
579584
}
580585
};
581586

code/options/OptionsManager.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void OptionsManager::setOverride(const SCP_string& key, const SCP_string& json)
9595
}
9696

9797
//Adds an option to the options vector
98-
const OptionBase* OptionsManager::addOption(std::unique_ptr<const OptionBase>&& option)
98+
const OptionBase* OptionsManager::addOption(std::shared_ptr<const OptionBase>&& option)
9999
{
100100
_options.emplace_back(std::move(option));
101101
_optionsSorted = false; // Order got invalidated by adding a new option
@@ -106,12 +106,12 @@ const OptionBase* OptionsManager::addOption(std::unique_ptr<const OptionBase>&&
106106
}
107107

108108
//Removes an option from the options vector
109-
void OptionsManager::removeOption(const OptionBase* option)
109+
void OptionsManager::removeOption(const std::shared_ptr<const OptionBase>& option)
110110
{
111111
_optionsMapping.erase(option->getConfigKey());
112112
_options.erase(
113113
std::remove_if(_options.begin(), _options.end(),
114-
[option](const std::unique_ptr<const OptionBase>& ptr) { return ptr.get() == option; }));
114+
[option](const std::shared_ptr<const OptionBase>& ptr) { return ptr == option; }));
115115
}
116116

117117
// Returns an option with the specified name
@@ -126,13 +126,13 @@ const OptionBase* OptionsManager::getOptionByKey(SCP_string key)
126126
}
127127

128128
//Returns a table of all built-in options available
129-
const SCP_vector<std::unique_ptr<const options::OptionBase>>& OptionsManager::getOptions()
129+
const SCP_vector<std::shared_ptr<const options::OptionBase>>& OptionsManager::getOptions()
130130
{
131131
if (!_optionsSorted) {
132132
// Keep options sorted by only sorting them when necessary
133133

134134
std::sort(_options.begin(), _options.end(),
135-
[](const std::unique_ptr<const OptionBase>& left, const std::unique_ptr<const OptionBase>& right) {
135+
[](const std::shared_ptr<const OptionBase>& left, const std::shared_ptr<const OptionBase>& right) {
136136
return *left < *right;
137137
});
138138

code/options/OptionsManager.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class OptionsManager {
1919

2020
SCP_unordered_map<SCP_string, const OptionBase*> _optionsMapping;
2121

22-
SCP_vector<std::unique_ptr<const OptionBase>> _options;
22+
SCP_vector<std::shared_ptr<const OptionBase>> _options;
2323
bool _optionsSorted = false;
2424

2525
public:
@@ -33,13 +33,13 @@ class OptionsManager {
3333

3434
void setOverride(const SCP_string& key, const SCP_string& json);
3535

36-
const OptionBase* addOption(std::unique_ptr<const OptionBase>&& option);
36+
const OptionBase* addOption(std::shared_ptr<const OptionBase>&& option);
3737

38-
void removeOption(const OptionBase* option);
38+
void removeOption(const std::shared_ptr<const OptionBase>& option);
3939

4040
const OptionBase* getOptionByKey(SCP_string name);
4141

42-
const SCP_vector<std::unique_ptr<const options::OptionBase>>& getOptions();
42+
const SCP_vector<std::shared_ptr<const options::OptionBase>>& getOptions();
4343

4444
bool persistOptionChanges(const options::OptionBase* option);
4545

0 commit comments

Comments
 (0)