Avoid memory leak in the resolution slider when toggling the fullscreen
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / slider_resolution.c
1 #ifdef INTERFACE
2 CLASS(XonoticResolutionSlider) EXTENDS(XonoticTextSlider)
3         METHOD(XonoticResolutionSlider, configureXonoticResolutionSlider, void(entity))
4         METHOD(XonoticResolutionSlider, loadResolutions, void(entity, float))
5         METHOD(XonoticResolutionSlider, addResolution, void(entity, float, float, float))
6         METHOD(XonoticResolutionSlider, loadCvars, void(entity))
7         METHOD(XonoticResolutionSlider, saveCvars, void(entity))
8         METHOD(XonoticResolutionSlider, draw, void(entity))
9         ATTRIB(XonoticResolutionSlider, vid_fullscreen, float, -1)
10 ENDCLASS(XonoticResolutionSlider)
11 entity makeXonoticResolutionSlider();
12 void updateConwidths(float width, float height, float pixelheight);
13 #endif
14
15 #ifdef IMPLEMENTATION
16
17 /* private static */ float XonoticResolutionSlider_DataHasChanged;
18
19 // Updates cvars (to be called by menu.qc at startup or on detected res change)
20 void updateConwidths(float width, float height, float pixelheight)
21 {
22         vector r, c;
23         float minfactor, maxfactor;
24         float sz, f;
25
26         // Save off current settings.
27         cvar_set("_menu_vid_width", ftos(width));
28         cvar_set("_menu_vid_height", ftos(height));
29         cvar_set("_menu_vid_pixelheight", ftos(pixelheight));
30         cvar_set("_menu_vid_desktopfullscreen", cvar_string("vid_desktopfullscreen"));
31
32         r_x = width;
33         r_y = height;
34         r_z = pixelheight;
35         sz = cvar("menu_vid_scale");
36
37         // calculate the base resolution
38         c_z = 0;
39         c_x = 800;
40         c_y = c_x * r_y * r_z / r_x;
41         if(c_y < 600)
42         {
43                 c_y = 600;
44                 c_x = c_y * r_x / (r_y * r_z);
45         }
46
47         f = min(r_x / c_x, r_y / c_y);
48         if(f < 1)
49                 c = c * f; // ensures that c_x <= r_x and c_y <= r_y
50
51         minfactor = min(1, 640 / c_x);            // can be > 1 only if c_x is <640
52         maxfactor = max(1, r_x / c_x, r_y / c_y); // can be < 1 only if r_x < c_x and r_y < c_y
53         dprint("min factor: ", ftos(minfactor), "\n");
54         dprint("max factor: ", ftos(maxfactor), "\n");
55
56         if(sz < 0)
57                 f = 1 - (maxfactor - 1) * sz;
58         else if(sz > 0)
59                 f = 1 + (minfactor - 1) * sz;
60         else
61                 f = 1;
62         c = c * f; // fteqcc fail
63
64         cvar_set("vid_conwidth", ftos(rint(c_x)));
65         cvar_set("vid_conheight", ftos(rint(c_y)));
66         XonoticResolutionSlider_DataHasChanged = TRUE;
67 }
68 entity makeXonoticResolutionSlider()
69 {
70         entity me;
71         me = spawnXonoticResolutionSlider();
72         me.configureXonoticResolutionSlider(me);
73         return me;
74 }
75 void XonoticResolutionSlider_addResolution(entity me, float w, float h, float pixelheight)
76 {
77         float i;
78         for (i = 0; i < me.nValues; ++i)
79         {
80                 tokenize_console(me.valueToIdentifier(me, i));
81                 if (w > stof(argv(0))) continue;
82                 if (w < stof(argv(0))) break;
83                 if (h > stof(argv(1))) continue;
84                 if (h < stof(argv(1))) break;
85                 if (pixelheight > stof(argv(2)) + 0.01) continue;
86                 if (pixelheight < stof(argv(2)) - 0.01) break;
87                 return;  // already there
88         }
89         if (pixelheight != 1)
90         {
91                 float aspect = w / (h * pixelheight);
92                 float bestdenom = rint(aspect);
93                 float bestnum = 1;
94                 float denom;
95                 for (denom = 2; denom < 10; ++denom) {
96                         float num = rint(aspect * denom);
97                         if (fabs(num / denom - aspect) < fabs(bestnum / bestdenom - aspect))
98                         {
99                                 bestnum = num;
100                                 bestdenom = denom;
101                         }
102                 }
103                 me.insertValue(me, i, strzone(sprintf(_("%dx%d (%d:%d)"), w, h, bestnum, bestdenom)), strzone(strcat(ftos(w), " ", ftos(h), " ", ftos(pixelheight))));
104         }
105         else
106                 me.insertValue(me, i, strzone(sprintf(_("%dx%d"), w, h)), strzone(strcat(ftos(w), " ", ftos(h), " ", ftos(pixelheight))));
107 }
108 float autocvar_menu_vid_allowdualscreenresolution;
109 void XonoticResolutionSlider_configureXonoticResolutionSlider(entity me)
110 {
111         me.configureXonoticTextSlider(me, "_menu_vid_width");
112         me.loadResolutions(me, cvar("vid_fullscreen"));
113 }
114 void XonoticResolutionSlider_loadResolutions(entity me, float fullscreen)
115 {
116         float i;
117         vector r;
118
119         // HACK: text slider assumes the strings are constants, so clearValues
120         // will not unzone them
121         for(i = 0; i < me.nValues; ++i)
122         {
123                 strunzone(me.valueToIdentifier(me, i));
124                 strunzone(me.valueToText(me, i));
125         }
126         // NOW we can safely clear.
127         me.clearValues(me);
128
129         if (fullscreen)
130         {
131                 for(i = 0;; ++i)
132                 {
133                         r = getresolution(i);
134                         if(r_x == 0 && r_y == 0)
135                                 break;
136                         if(r_x < 640 || r_y < 480)
137                                 continue;
138                         if(r_x > 2 * r_y) // likely dualscreen resolution, skip this one
139                                 if(autocvar_menu_vid_allowdualscreenresolution <= 0)
140                                         continue;
141                         me.addResolution(me, r_x, r_y, r_z);
142                 }
143                 r = getresolution(-1);
144                 if(r_x != 0 || r_y != 0)
145                         me.addResolution(me, r_x, r_y, r_z);
146                 dprint("Added system resolutions.\n");
147         }
148
149         if(me.nValues == 0)
150         {
151                 me.addResolution(me, 640, 480, 1); // pc res
152                 me.addResolution(me, 720, 480, 1.125); // DVD NTSC 4:3
153                 me.addResolution(me, 720, 576, 0.9375); // DVD PAL 4:3
154                 me.addResolution(me, 720, 480, 0.84375); // DVD NTSC 16:9
155                 me.addResolution(me, 720, 576, 0.703125); // DVD PAL 16:9
156                 me.addResolution(me, 800, 480, 1); // 480p at 1:1 pixel aspect
157                 me.addResolution(me, 800, 600, 1); // pc res
158                 me.addResolution(me, 1024, 600, 1); // notebook res
159                 me.addResolution(me, 1024, 768, 1); // pc res
160                 me.addResolution(me, 1280, 720, 1); // 720p
161                 me.addResolution(me, 1280, 960, 1); // pc res
162                 me.addResolution(me, 1280, 1024, 1); // pc res
163                 me.addResolution(me, 1920, 1080, 1); // 1080p
164                 dprint("Added default resolutions.\n");
165         }
166         dprint("Total number of resolutions detected: ", ftos(me.nValues), "\n");
167
168         me.vid_fullscreen = fullscreen;
169
170         me.configureXonoticTextSliderValues(me);
171 }
172 void XonoticResolutionSlider_loadCvars(entity me)
173 {
174         me.setValueFromIdentifier(me, strcat(cvar_string("_menu_vid_width"), " ", cvar_string("_menu_vid_height"), " ", cvar_string("_menu_vid_pixelheight")));
175 }
176 void XonoticResolutionSlider_saveCvars(entity me)
177 {
178         if(me.value >= 0 || me.value < me.nValues)
179         {
180                 tokenize_console(me.getIdentifier(me));
181                 cvar_set("_menu_vid_width", argv(0));
182                 cvar_set("_menu_vid_height", argv(1));
183                 cvar_set("_menu_vid_pixelheight", argv(2));
184                 vector r = getresolution(-1);
185                 if (stof(argv(0)) == r_x && stof(argv(1)) == r_y && fabs(stof(argv(2)) - r_z) < 0.01)
186                         cvar_set("_menu_vid_desktopfullscreen", "1");
187                 else
188                         cvar_set("_menu_vid_desktopfullscreen", "0");
189         }
190 }
191 void XonoticResolutionSlider_draw(entity me)
192 {
193         if (cvar("vid_fullscreen") != me.vid_fullscreen)
194         {
195                 me.loadResolutions(me, cvar("vid_fullscreen"));
196                 XonoticResolutionSlider_DataHasChanged = TRUE;
197         }
198         if (XonoticResolutionSlider_DataHasChanged)
199         {
200                 XonoticResolutionSlider_DataHasChanged = FALSE;
201                 me.loadCvars(me);
202         }
203         SUPER(XonoticResolutionSlider).draw(me);
204 }
205 #endif