]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/quicktune.cpp
Make logging cost free when there is no output target (#12247)
[dragonfireclient.git] / src / util / quicktune.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "quicktune.h"
21 #include "threading/mutex_auto_lock.h"
22 #include "util/string.h"
23
24 std::string QuicktuneValue::getString()
25 {
26         switch(type){
27         case QVT_NONE:
28                 return "(none)";
29         case QVT_FLOAT:
30                 return ftos(value_QVT_FLOAT.current);
31         }
32         return "<invalid type>";
33 }
34 void QuicktuneValue::relativeAdd(float amount)
35 {
36         switch(type){
37         case QVT_NONE:
38                 break;
39         case QVT_FLOAT:
40                 value_QVT_FLOAT.current += amount * (value_QVT_FLOAT.max - value_QVT_FLOAT.min);
41                 if(value_QVT_FLOAT.current > value_QVT_FLOAT.max)
42                         value_QVT_FLOAT.current = value_QVT_FLOAT.max;
43                 if(value_QVT_FLOAT.current < value_QVT_FLOAT.min)
44                         value_QVT_FLOAT.current = value_QVT_FLOAT.min;
45                 break;
46         }
47 }
48
49 static std::map<std::string, QuicktuneValue> g_values;
50 static std::vector<std::string> g_names;
51 std::mutex *g_mutex = NULL;
52
53 static void makeMutex()
54 {
55         if(!g_mutex){
56                 g_mutex = new std::mutex();
57         }
58 }
59
60 std::vector<std::string> getQuicktuneNames()
61 {
62         return g_names;
63 }
64
65 QuicktuneValue getQuicktuneValue(const std::string &name)
66 {
67         makeMutex();
68         MutexAutoLock lock(*g_mutex);
69         std::map<std::string, QuicktuneValue>::iterator i = g_values.find(name);
70         if(i == g_values.end()){
71                 QuicktuneValue val;
72                 val.type = QVT_NONE;
73                 return val;
74         }
75         return i->second;
76 }
77
78 void setQuicktuneValue(const std::string &name, const QuicktuneValue &val)
79 {
80         makeMutex();
81         MutexAutoLock lock(*g_mutex);
82         g_values[name] = val;
83         g_values[name].modified = true;
84 }
85
86 void updateQuicktuneValue(const std::string &name, QuicktuneValue &val)
87 {
88         makeMutex();
89         MutexAutoLock lock(*g_mutex);
90         std::map<std::string, QuicktuneValue>::iterator i = g_values.find(name);
91         if(i == g_values.end()){
92                 g_values[name] = val;
93                 g_names.push_back(name);
94                 return;
95         }
96         QuicktuneValue &ref = i->second;
97         if(ref.modified)
98                 val = ref;
99         else{
100                 ref = val;
101                 ref.modified = false;
102         }
103 }
104