Settings fixes Make the GameGlobalShaderConstantSetter use the settings callback...
authorgregorycu <gregory.currie@gmail.com>
Sat, 24 Jan 2015 09:40:27 +0000 (20:40 +1100)
committerCraig Robbins <kde.psych@gmail.com>
Sun, 25 Jan 2015 05:23:37 +0000 (15:23 +1000)
src/fontengine.cpp
src/game.cpp
src/settings.cpp
src/settings.h

index 79d53c9898cd7306d350be1f60d0019c4dd4c724..3b82a3c470ff82cec67702f4940eb40f9ff95b54 100644 (file)
@@ -36,7 +36,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 FontEngine* g_fontengine = NULL;
 
 /** callback to be used on change of font size setting */
-static void font_setting_changed(const std::string) {
+static void font_setting_changed(const std::string, void *userdata) {
        g_fontengine->readSettings();
 }
 
@@ -91,22 +91,22 @@ FontEngine::FontEngine(Settings* main_settings, gui::IGUIEnvironment* env) :
        updateSkin();
 
        if (m_currentMode == FM_Standard) {
-               m_settings->registerChangedCallback("font_size", font_setting_changed);
-               m_settings->registerChangedCallback("font_path", font_setting_changed);
-               m_settings->registerChangedCallback("font_shadow", font_setting_changed);
-               m_settings->registerChangedCallback("font_shadow_alpha", font_setting_changed);
+               m_settings->registerChangedCallback("font_size", font_setting_changed, NULL);
+               m_settings->registerChangedCallback("font_path", font_setting_changed, NULL);
+               m_settings->registerChangedCallback("font_shadow", font_setting_changed, NULL);
+               m_settings->registerChangedCallback("font_shadow_alpha", font_setting_changed, NULL);
        }
        else if (m_currentMode == FM_Fallback) {
-               m_settings->registerChangedCallback("fallback_font_size", font_setting_changed);
-               m_settings->registerChangedCallback("fallback_font_path", font_setting_changed);
-               m_settings->registerChangedCallback("fallback_font_shadow", font_setting_changed);
-               m_settings->registerChangedCallback("fallback_font_shadow_alpha", font_setting_changed);
+               m_settings->registerChangedCallback("fallback_font_size", font_setting_changed, NULL);
+               m_settings->registerChangedCallback("fallback_font_path", font_setting_changed, NULL);
+               m_settings->registerChangedCallback("fallback_font_shadow", font_setting_changed, NULL);
+               m_settings->registerChangedCallback("fallback_font_shadow_alpha", font_setting_changed, NULL);
        }
 
-       m_settings->registerChangedCallback("mono_font_path", font_setting_changed);
-       m_settings->registerChangedCallback("mono_font_size", font_setting_changed);
-       m_settings->registerChangedCallback("screen_dpi", font_setting_changed);
-       m_settings->registerChangedCallback("gui_scaling", font_setting_changed);
+       m_settings->registerChangedCallback("mono_font_path", font_setting_changed, NULL);
+       m_settings->registerChangedCallback("mono_font_size", font_setting_changed, NULL);
+       m_settings->registerChangedCallback("screen_dpi", font_setting_changed, NULL);
+       m_settings->registerChangedCallback("gui_scaling", font_setting_changed, NULL);
 }
 
 /******************************************************************************/
index ec025b73ff825ac07d1d5c8063dc26ca2b6d5db8..b496cf3bf335f792b259fb53da405b34be3def5d 100644 (file)
@@ -809,16 +809,35 @@ class GameGlobalShaderConstantSetter : public IShaderConstantSetter
        bool *m_force_fog_off;
        f32 *m_fog_range;
        Client *m_client;
+       bool m_fogEnabled;
 
 public:
+       void onSettingsChange(const std::string &name)
+       {
+               if (name == "enable_fog")
+                       m_fogEnabled = g_settings->getBool("enable_fog");
+       }
+
+       static void SettingsCallback(const std::string name, void *userdata)
+       {
+               reinterpret_cast<GameGlobalShaderConstantSetter*>(userdata)->onSettingsChange(name);
+       }
+
        GameGlobalShaderConstantSetter(Sky *sky, bool *force_fog_off,
                        f32 *fog_range, Client *client) :
                m_sky(sky),
                m_force_fog_off(force_fog_off),
                m_fog_range(fog_range),
                m_client(client)
-       {}
-       ~GameGlobalShaderConstantSetter() {}
+       {
+               g_settings->registerChangedCallback("enable_fog", SettingsCallback, this);
+               m_fogEnabled = g_settings->getBool("enable_fog");
+       }
+
+       ~GameGlobalShaderConstantSetter()
+       {
+               g_settings->deregisterChangedCallback("enable_fog", SettingsCallback, this);
+       }
 
        virtual void onSetConstants(video::IMaterialRendererServices *services,
                        bool is_highlevel)
index 2f515caba9165ef83dad7e7116b7ca3b679fe628..7339af62baec80beb477950bb5dcd089924d3873 100644 (file)
@@ -963,28 +963,39 @@ void Settings::clearNoLock()
        m_defaults.clear();
 }
 
-
 void Settings::registerChangedCallback(std::string name,
-       setting_changed_callback cbf)
+       setting_changed_callback cbf, void *userdata)
 {
-       m_callbacks[name].push_back(cbf);
+       JMutexAutoLock lock(m_callbackMutex);
+       m_callbacks[name].push_back(std::make_pair(cbf, userdata));
 }
 
+void Settings::deregisterChangedCallback(std::string name, setting_changed_callback cbf, void *userdata)
+{
+       JMutexAutoLock lock(m_callbackMutex);
+       std::map<std::string, std::vector<std::pair<setting_changed_callback, void*> > >::iterator iterToVector = m_callbacks.find(name);
+       if (iterToVector != m_callbacks.end())
+       {
+               std::vector<std::pair<setting_changed_callback, void*> > &vector = iterToVector->second;
+
+               std::vector<std::pair<setting_changed_callback, void*> >::iterator position =
+                       std::find(vector.begin(), vector.end(), std::make_pair(cbf, userdata));
+
+               if (position != vector.end())
+                       vector.erase(position);
+       }
+}
 
 void Settings::doCallbacks(const std::string name)
 {
-       std::vector<setting_changed_callback> tempvector;
+       JMutexAutoLock lock(m_callbackMutex);
+       std::map<std::string, std::vector<std::pair<setting_changed_callback, void*> > >::iterator iterToVector = m_callbacks.find(name);
+       if (iterToVector != m_callbacks.end())
        {
-               JMutexAutoLock lock(m_mutex);
-               if (m_callbacks.find(name) != m_callbacks.end())
+               std::vector<std::pair<setting_changed_callback, void*> >::iterator iter;
+               for (iter = iterToVector->second.begin(); iter != iterToVector->second.end(); iter++)
                {
-                       tempvector = m_callbacks[name];
+                       (iter->first)(name, iter->second);
                }
        }
-
-       std::vector<setting_changed_callback>::iterator iter;
-       for (iter = tempvector.begin(); iter != tempvector.end(); iter++)
-       {
-               (*iter)(name);
-       }
 }
index 89f7589dfddc1bd40932133009ba1c52d6be25e1..1a7d9ab96aad4834d889f4bbd81a8b844e51c457 100644 (file)
@@ -32,7 +32,7 @@ class Settings;
 struct NoiseParams;
 
 /** function type to register a changed callback */
-typedef void (*setting_changed_callback)(const std::string);
+typedef void (*setting_changed_callback)(const std::string, void*);
 
 enum ValueType {
        VALUETYPE_STRING,
@@ -204,7 +204,8 @@ public:
        void clear();
        void updateValue(const Settings &other, const std::string &name);
        void update(const Settings &other);
-       void registerChangedCallback(std::string name, setting_changed_callback cbf);
+       void registerChangedCallback(std::string name, setting_changed_callback cbf, void *userdata = NULL);
+       void deregisterChangedCallback(std::string name, setting_changed_callback cbf, void *userdata = NULL);
 
 private:
 
@@ -215,9 +216,12 @@ private:
 
        std::map<std::string, SettingsEntry> m_settings;
        std::map<std::string, SettingsEntry> m_defaults;
-       std::map<std::string, std::vector<setting_changed_callback> > m_callbacks;
-       // All methods that access m_settings/m_defaults directly should lock this.
-       mutable JMutex m_mutex;
+
+       std::map<std::string, std::vector<std::pair<setting_changed_callback,void*> > > m_callbacks;
+
+       mutable JMutex m_callbackMutex;
+       mutable JMutex m_mutex; // All methods that access m_settings/m_defaults directly should lock this.
+
 };
 
 #endif