]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu/nexuiz/slider_decibels.c
menu-div0test -> menu
[divverent/nexuiz.git] / data / qcsrc / menu / nexuiz / slider_decibels.c
1 #ifdef INTERFACE
2 CLASS(NexuizDecibelsSlider) EXTENDS(NexuizSlider)
3         METHOD(NexuizDecibelsSlider, loadCvars, void(entity))
4         METHOD(NexuizDecibelsSlider, saveCvars, void(entity))
5         METHOD(NexuizDecibelsSlider, valueToText, string(entity, float))
6 ENDCLASS(NexuizDecibelsSlider)
7 entity makeNexuizDecibelsSlider(float, float, float, string);
8 #endif
9
10 #ifdef IMPLEMENTATION
11
12 float exp(float x)
13 {
14         float i;
15         float t, s;
16
17         s = 1;
18         t = 1;
19         for(i = 1; i < 100; ++i)
20         {
21                 t *= x;
22                 t /= i;
23                 s += t;
24         }
25
26         return s;
27 }
28
29 float ln(float x)
30 {
31         float i;
32         float r, r0;
33
34         r = 1;
35         r0 = 0;
36         for(i = 1; fabs(r - r0) >= 0.05; ++i)
37         {
38                 // Newton iteration on exp(r) = x:
39                 //   r <- r - (exp(r) - x) / (exp(r))
40                 //   r <- r - 1 + x / exp(r)
41                 r0 = r;
42                 r = r0 - 1 + x / exp(r0);
43         }
44         dprint("ln: ", ftos(i), " iterations\n");
45
46         return r;
47 }
48
49 #define LOG10 2.302585093
50
51 entity makeNexuizDecibelsSlider(float theValueMin, float theValueMax, float theValueStep, string theCvar)
52 {
53         entity me;
54         me = spawnNexuizDecibelsSlider();
55         me.configureNexuizSlider(me, theValueMin, theValueMax, theValueStep, theCvar);
56         return me;
57 }
58 void loadCvarsNexuizDecibelsSlider(entity me)
59 {
60         float v;
61         v = cvar(me.cvarName);
62         if(v >= 0.98)
63                 me.value = 0;
64         else if(v < 0.0005)
65                 me.value = -1000000;
66         else
67                 me.value = 0.1 * floor(0.5 + 10.0 * ln(cvar(me.cvarName)) * 10 / LOG10);
68 }
69 void saveCvarsNexuizDecibelsSlider(entity me)
70 {
71         if(me.value >= -0.1)
72                 cvar_set(me.cvarName, "1");
73         if(me.value < -33)
74                 cvar_set(me.cvarName, "0");
75         else
76                 cvar_set(me.cvarName, ftos(exp(me.value / 10 * LOG10)));
77 }
78
79 string valueToTextNexuizDecibelsSlider(entity me, float v)
80 {
81         if(v < -33)
82                 return "OFF";
83         else if(v >= -0.1)
84                 return "MAX";
85         return strcat(valueToTextSlider(me, v), " dB");
86 }
87
88 #endif