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