Line data Source code
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 "jthread/jmutex.h"
22 : #include "jthread/jmutexautolock.h"
23 : #include "util/string.h"
24 :
25 0 : std::string QuicktuneValue::getString()
26 : {
27 0 : switch(type){
28 : case QVT_NONE:
29 0 : return "(none)";
30 : case QVT_FLOAT:
31 0 : return ftos(value_QVT_FLOAT.current);
32 : }
33 0 : return "<invalid type>";
34 : }
35 0 : void QuicktuneValue::relativeAdd(float amount)
36 : {
37 0 : switch(type){
38 : case QVT_NONE:
39 0 : break;
40 : case QVT_FLOAT:
41 0 : value_QVT_FLOAT.current += amount * (value_QVT_FLOAT.max - value_QVT_FLOAT.min);
42 0 : if(value_QVT_FLOAT.current > value_QVT_FLOAT.max)
43 0 : value_QVT_FLOAT.current = value_QVT_FLOAT.max;
44 0 : if(value_QVT_FLOAT.current < value_QVT_FLOAT.min)
45 0 : value_QVT_FLOAT.current = value_QVT_FLOAT.min;
46 0 : break;
47 : }
48 0 : }
49 :
50 1 : static std::map<std::string, QuicktuneValue> g_values;
51 1 : static std::vector<std::string> g_names;
52 : JMutex *g_mutex = NULL;
53 :
54 0 : static void makeMutex()
55 : {
56 0 : if(!g_mutex){
57 0 : g_mutex = new JMutex();
58 : }
59 0 : }
60 :
61 1 : std::vector<std::string> getQuicktuneNames()
62 : {
63 1 : return g_names;
64 : }
65 :
66 0 : QuicktuneValue getQuicktuneValue(const std::string &name)
67 : {
68 0 : makeMutex();
69 0 : JMutexAutoLock lock(*g_mutex);
70 0 : std::map<std::string, QuicktuneValue>::iterator i = g_values.find(name);
71 0 : if(i == g_values.end()){
72 0 : QuicktuneValue val;
73 0 : val.type = QVT_NONE;
74 0 : return val;
75 : }
76 0 : return i->second;
77 : }
78 :
79 0 : void setQuicktuneValue(const std::string &name, const QuicktuneValue &val)
80 : {
81 0 : makeMutex();
82 0 : JMutexAutoLock lock(*g_mutex);
83 0 : g_values[name] = val;
84 0 : g_values[name].modified = true;
85 0 : }
86 :
87 0 : void updateQuicktuneValue(const std::string &name, QuicktuneValue &val)
88 : {
89 0 : makeMutex();
90 0 : JMutexAutoLock lock(*g_mutex);
91 0 : std::map<std::string, QuicktuneValue>::iterator i = g_values.find(name);
92 0 : if(i == g_values.end()){
93 0 : g_values[name] = val;
94 0 : g_names.push_back(name);
95 0 : return;
96 : }
97 0 : QuicktuneValue &ref = i->second;
98 0 : if(ref.modified)
99 0 : val = ref;
100 : else{
101 0 : ref = val;
102 0 : ref.modified = false;
103 : }
104 3 : }
105 :
|