Make the GameGlobalShaderConstantSetter use the settings callback (8% perf improvemen...
authorgregorycu <gregory.currie@gmail.com>
Sat, 24 Jan 2015 02:03:57 +0000 (13:03 +1100)
committerkwolekr <kwolekr@minetest.net>
Sat, 24 Jan 2015 02:27:51 +0000 (21:27 -0500)
Amend the settings callback to support userdata

src/fontengine.cpp
src/game.cpp
src/settings.cpp
src/settings.h

index 79d53c9898cd7306d350be1f60d0019c4dd4c724..2df6bf8337fe0654474c92729eab0496522864ac 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();
 }
 
index cedaabb1b0720c07141573924f71803d3d35388a..af744dcbb92ce8dc0e211de0a7f0863587eef6ed 100644 (file)
@@ -809,15 +809,31 @@ 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)
-       {}
+       {
+               g_settings->registerChangedCallback("enable_fog", SettingsCallback, this);
+       }
+
        ~GameGlobalShaderConstantSetter() {}
 
        virtual void onSetConstants(video::IMaterialRendererServices *services,
@@ -840,7 +856,7 @@ public:
                // Fog distance
                float fog_distance = 10000 * BS;
 
-               if (g_settings->getBool("enable_fog") && !*m_force_fog_off)
+               if (m_fogEnabled && !*m_force_fog_off)
                        fog_distance = *m_fog_range;
 
                services->setPixelShaderConstant("fogDistance", &fog_distance, 1);
index 2f515caba9165ef83dad7e7116b7ca3b679fe628..3416066e25cf1912e840e239c4ac2283c5f090ee 100644 (file)
@@ -965,15 +965,15 @@ void Settings::clearNoLock()
 
 
 void Settings::registerChangedCallback(std::string name,
-       setting_changed_callback cbf)
+       setting_changed_callback cbf, void *userdata)
 {
-       m_callbacks[name].push_back(cbf);
+       m_callbacks[name].push_back(std::make_pair(cbf,userdata));
 }
 
 
 void Settings::doCallbacks(const std::string name)
 {
-       std::vector<setting_changed_callback> tempvector;
+       std::vector<std::pair<setting_changed_callback,void*> > tempvector;
        {
                JMutexAutoLock lock(m_mutex);
                if (m_callbacks.find(name) != m_callbacks.end())
@@ -982,9 +982,9 @@ void Settings::doCallbacks(const std::string name)
                }
        }
 
-       std::vector<setting_changed_callback>::iterator iter;
+       std::vector<std::pair<setting_changed_callback, void*> >::iterator iter;
        for (iter = tempvector.begin(); iter != tempvector.end(); iter++)
        {
-               (*iter)(name);
+               (iter->first)(name,iter->second);
        }
 }
index 89f7589dfddc1bd40932133009ba1c52d6be25e1..47feb1755efb4443dfd3e56cf00c4c96a5977df3 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 *userdata);
 
 enum ValueType {
        VALUETYPE_STRING,
@@ -204,7 +204,7 @@ 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);
 
 private:
 
@@ -215,7 +215,7 @@ 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;
+       std::map<std::string, std::vector<std::pair<setting_changed_callback,void*> > > m_callbacks;
        // All methods that access m_settings/m_defaults directly should lock this.
        mutable JMutex m_mutex;
 };