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