]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/slider_resolution.qc
Merge branch 'master' into TimePath/unified_weapons
[xonotic/xonotic-data.pk3dir.git] / qcsrc / menu / xonotic / slider_resolution.qc
1 #ifndef SLIDER_RESOLUTION_H
2 #define SLIDER_RESOLUTION_H
3 #include "textslider.qc"
4 CLASS(XonoticResolutionSlider, XonoticTextSlider)
5         METHOD(XonoticResolutionSlider, configureXonoticResolutionSlider, void(entity));
6         METHOD(XonoticResolutionSlider, loadResolutions, void(entity, float));
7         METHOD(XonoticResolutionSlider, addResolution, void(entity, float, float, float));
8         METHOD(XonoticResolutionSlider, loadCvars, void(entity));
9         METHOD(XonoticResolutionSlider, saveCvars, void(entity));
10         METHOD(XonoticResolutionSlider, draw, void(entity));
11         ATTRIB(XonoticResolutionSlider, vid_fullscreen, float, -1)
12         ATTRIB(XonoticResolutionSlider, maxAllowedWidth, float, 0)
13         ATTRIB(XonoticResolutionSlider, maxAllowedHeight, float, 0)
14 ENDCLASS(XonoticResolutionSlider)
15 entity makeXonoticResolutionSlider();
16 float updateConwidths(float width, float height, float pixelheight);
17 #endif
18
19 #ifdef IMPLEMENTATION
20
21 /* private static */ float XonoticResolutionSlider_DataHasChanged;
22
23 // Updates cvars (to be called by menu.qc at startup or on detected res change)
24 float updateConwidths(float width, float height, float pixelheight)
25 {
26         vector r, c;
27         float minfactor, maxfactor;
28         float sz, f;
29
30         sz = cvar("menu_vid_scale");
31         if (sz < -1)
32                 return 0;  // No recalculation.
33
34         // Save off current settings.
35         cvar_set("_menu_vid_width", ftos(width));
36         cvar_set("_menu_vid_height", ftos(height));
37         cvar_set("_menu_vid_pixelheight", ftos(pixelheight));
38         cvar_set("_menu_vid_desktopfullscreen", cvar_string("vid_desktopfullscreen"));
39
40         r_x = width;
41         r_y = height;
42         r_z = pixelheight;
43
44         // calculate the base resolution
45         c_z = 0;
46         c_x = 800;
47         c_y = c.x * r.y * r.z / r.x;
48         if(c.y < 600)
49         {
50                 c_y = 600;
51                 c_x = c.y * r.x / (r.y * r.z);
52         }
53
54         f = min(r.x / c.x, r.y / c.y);
55         if(f < 1)
56                 c = c * f; // ensures that c_x <= r_x and c_y <= r_y
57
58         minfactor = min(1, 640 / c.x);            // can be > 1 only if c_x is <640
59         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
60         LOG_TRACE("min factor: ", ftos(minfactor), "\n");
61         LOG_TRACE("max factor: ", ftos(maxfactor), "\n");
62
63         if(sz < 0)
64                 f = 1 - (maxfactor - 1) * sz;
65         else if(sz > 0)
66                 f = 1 + (minfactor - 1) * sz;
67         else
68                 f = 1;
69         c = c * f; // fteqcc fail
70
71         c_x = rint(c.x);
72         c_y = rint(c.y);
73
74         // Please reload resolutions list and such stuff.
75         XonoticResolutionSlider_DataHasChanged = true;
76
77         if (c.x != cvar("vid_conwidth") || c.y != cvar("vid_conheight"))
78         {
79                 cvar_set("vid_conwidth", ftos(c.x));
80                 cvar_set("vid_conheight", ftos(c.y));
81                 return 1;
82         }
83         return 0;
84 }
85 entity makeXonoticResolutionSlider()
86 {
87         entity me;
88         me = NEW(XonoticResolutionSlider);
89         me.configureXonoticResolutionSlider(me);
90         return me;
91 }
92 void XonoticResolutionSlider_addResolution(entity me, float w, float h, float pixelheight)
93 {
94         if (me.maxAllowedWidth && w > me.maxAllowedWidth)
95                 return;
96         if (me.maxAllowedHeight && h > me.maxAllowedHeight)
97                 return;
98         float i;
99         for (i = 0; i < me.nValues; ++i)
100         {
101                 tokenize_console(me.valueToIdentifier(me, i));
102                 if (w > stof(argv(0))) continue;
103                 if (w < stof(argv(0))) break;
104                 if (h > stof(argv(1))) continue;
105                 if (h < stof(argv(1))) break;
106                 if (pixelheight > stof(argv(2)) + 0.01) continue;
107                 if (pixelheight < stof(argv(2)) - 0.01) break;
108                 return;  // already there
109         }
110         if (pixelheight != 1)
111         {
112                 float aspect = w / (h * pixelheight);
113                 float bestdenom = rint(aspect);
114                 float bestnum = 1;
115                 float denom;
116                 for (denom = 2; denom < 10; ++denom) {
117                         float num = rint(aspect * denom);
118                         if (fabs(num / denom - aspect) < fabs(bestnum / bestdenom - aspect))
119                         {
120                                 bestnum = num;
121                                 bestdenom = denom;
122                         }
123                 }
124                 me.insertValue(me, i, strzone(sprintf(_("%dx%d (%d:%d)"), w, h, bestnum, bestdenom)), strzone(strcat(ftos(w), " ", ftos(h), " ", ftos(pixelheight))));
125         }
126         else
127                 me.insertValue(me, i, strzone(sprintf(_("%dx%d"), w, h)), strzone(strcat(ftos(w), " ", ftos(h), " ", ftos(pixelheight))));
128 }
129 float autocvar_menu_vid_allowdualscreenresolution;
130 void XonoticResolutionSlider_configureXonoticResolutionSlider(entity me)
131 {
132         me.configureXonoticTextSlider(me, "_menu_vid_width",
133                 _("Screen resolution"));
134         me.loadResolutions(me, cvar("vid_fullscreen"));
135 }
136 void XonoticResolutionSlider_loadResolutions(entity me, float fullscreen)
137 {
138         float i;
139         vector r;
140
141         // HACK: text slider assumes the strings are constants, so clearValues
142         // will not unzone them
143         for(i = 0; i < me.nValues; ++i)
144         {
145                 strunzone(me.valueToIdentifier(me, i));
146                 strunzone(me.valueToText(me, i));
147         }
148         // NOW we can safely clear.
149         me.clearValues(me);
150         me.maxAllowedWidth = 0;
151         me.maxAllowedHeight = 0;
152
153         if (fullscreen)
154         {
155                 for(i = 0;; ++i)
156                 {
157                         r = getresolution(i);
158                         if(r_x == 0 && r_y == 0)
159                                 break;
160                         if(r.x < 640 || r.y < 480)
161                                 continue;
162                         if(r.x > 2 * r.y) // likely dualscreen resolution, skip this one
163                                 if(autocvar_menu_vid_allowdualscreenresolution <= 0)
164                                         continue;
165                         me.addResolution(me, r.x, r.y, r.z);
166                 }
167                 r = getresolution(-1);
168                 if(r.x != 0 || r.y != 0)
169                         me.addResolution(me, r.x, r.y, r.z);
170                 LOG_TRACE("Added system resolutions.\n");
171         }
172
173         if(me.nValues == 0)
174         {
175                 // Get workarea.
176                 r = getresolution(-2);
177                 // If workarea is not supported, get desktop size.
178                 if(r.x == 0 && r.y == 0)
179                         r = getresolution(-1);
180
181                 // Add it, and limit all other resolutions to the workarea/desktop size.
182                 if(r.x != 0 || r.y != 0)
183                 {
184                         me.maxAllowedWidth = r.x;
185                         me.maxAllowedHeight = r.y;
186                         me.addResolution(me, r.x, r.y, r.z);
187                 }
188
189                 // Add nice hardcoded defaults.
190                 me.addResolution(me, 640, 480, 1); // pc res
191 #if 0
192                 me.addResolution(me, 720, 480, 1.125); // DVD NTSC 4:3
193                 me.addResolution(me, 720, 576, 0.9375); // DVD PAL 4:3
194                 me.addResolution(me, 720, 480, 0.84375); // DVD NTSC 16:9
195                 me.addResolution(me, 720, 576, 0.703125); // DVD PAL 16:9
196 #endif
197                 me.addResolution(me, 800, 480, 1); // 480p at 1:1 pixel aspect
198                 me.addResolution(me, 800, 600, 1); // pc res
199                 me.addResolution(me, 1024, 600, 1); // notebook res
200                 me.addResolution(me, 1024, 768, 1); // pc res
201                 me.addResolution(me, 1280, 720, 1); // 720p
202                 me.addResolution(me, 1280, 960, 1); // pc res
203                 me.addResolution(me, 1280, 1024, 1); // pc res
204                 me.addResolution(me, 1920, 1080, 1); // 1080p
205                 LOG_TRACE("Added default resolutions.\n");
206         }
207         LOG_TRACE("Total number of resolutions detected: ", ftos(me.nValues), "\n");
208
209         me.vid_fullscreen = fullscreen;
210
211         me.configureXonoticTextSliderValues(me);
212 }
213 void XonoticResolutionSlider_loadCvars(entity me)
214 {
215         me.setValueFromIdentifier_noAnim(me, strcat(cvar_string("_menu_vid_width"), " ", cvar_string("_menu_vid_height"), " ", cvar_string("_menu_vid_pixelheight")));
216 }
217 void XonoticResolutionSlider_saveCvars(entity me)
218 {
219         if(me.value >= 0 || me.value < me.nValues)
220         {
221                 tokenize_console(me.getIdentifier(me));
222                 cvar_set("_menu_vid_width", argv(0));
223                 cvar_set("_menu_vid_height", argv(1));
224                 cvar_set("_menu_vid_pixelheight", argv(2));
225                 vector r = getresolution(-1);
226                 if (stof(argv(0)) == r.x && stof(argv(1)) == r.y && fabs(stof(argv(2)) - r.z) < 0.01)
227                         cvar_set("_menu_vid_desktopfullscreen", "1");
228                 else
229                         cvar_set("_menu_vid_desktopfullscreen", "0");
230         }
231 }
232 void XonoticResolutionSlider_draw(entity me)
233 {
234         if (cvar("vid_fullscreen") != me.vid_fullscreen)
235         {
236                 me.loadResolutions(me, cvar("vid_fullscreen"));
237                 XonoticResolutionSlider_DataHasChanged = true;
238         }
239         if (XonoticResolutionSlider_DataHasChanged)
240         {
241                 XonoticResolutionSlider_DataHasChanged = false;
242                 me.loadCvars(me);
243         }
244         SUPER(XonoticResolutionSlider).draw(me);
245 }
246 #endif