quicktune.cpp 2.5 KB

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