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