1 #include "slider_resolution.qh"
3 /* private static */ float XonoticResolutionSlider_DataHasChanged;
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)
9 float minfactor, maxfactor;
12 sz = cvar("menu_vid_scale");
14 return 0; // No recalculation.
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"));
26 // calculate the base resolution
29 c_y = c.x * r.y * r.z / r.x;
33 c_x = c.y * r.x / (r.y * r.z);
36 f = min(r.x / c.x, r.y / c.y);
38 c = c * f; // ensures that c_x <= r_x and c_y <= r_y
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");
46 f = 1 - (maxfactor - 1) * sz;
48 f = 1 + (minfactor - 1) * sz;
51 c = c * f; // fteqcc fail
56 // Please reload resolutions list and such stuff.
57 XonoticResolutionSlider_DataHasChanged = true;
59 if (c.x != cvar("vid_conwidth") || c.y != cvar("vid_conheight"))
61 cvar_set("vid_conwidth", ftos(c.x));
62 cvar_set("vid_conheight", ftos(c.y));
67 entity makeXonoticResolutionSlider()
70 me = NEW(XonoticResolutionSlider);
71 me.configureXonoticResolutionSlider(me);
74 void XonoticResolutionSlider_addResolution(entity me, float w, float h, float pixelheight)
76 if (me.maxAllowedWidth && w > me.maxAllowedWidth)
78 if (me.maxAllowedHeight && h > me.maxAllowedHeight)
81 for (i = 0; i < me.nValues; ++i)
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
94 float aspect = w / (h * pixelheight);
95 float bestdenom = rint(aspect);
98 for (denom = 2; denom < 10; ++denom) {
99 float num = rint(aspect * denom);
100 if (fabs(num / denom - aspect) < fabs(bestnum / bestdenom - aspect))
106 me.insertValue(me, i, strzone(sprintf(_("%dx%d (%d:%d)"), w, h, bestnum, bestdenom)), strzone(strcat(ftos(w), " ", ftos(h), " ", ftos(pixelheight))));
109 me.insertValue(me, i, strzone(sprintf(_("%dx%d"), w, h)), strzone(strcat(ftos(w), " ", ftos(h), " ", ftos(pixelheight))));
111 float autocvar_menu_vid_allowdualscreenresolution;
112 void XonoticResolutionSlider_configureXonoticResolutionSlider(entity me)
114 me.configureXonoticTextSlider(me, "_menu_vid_width",
115 _("Screen resolution"));
116 me.loadResolutions(me, cvar("vid_fullscreen"));
118 void XonoticResolutionSlider_loadResolutions(entity me, float fullscreen)
123 // HACK: text slider assumes the strings are constants, so clearValues
124 // will not unzone them
125 for(i = 0; i < me.nValues; ++i)
127 strunzone(me.valueToIdentifier(me, i));
128 strunzone(me.valueToText(me, i));
130 // NOW we can safely clear.
132 me.maxAllowedWidth = 0;
133 me.maxAllowedHeight = 0;
139 r = getresolution(i);
140 if(r_x == 0 && r_y == 0)
142 if(r.x < 640 || r.y < 480)
144 if(r.x > 2 * r.y) // likely dualscreen resolution, skip this one
145 if(autocvar_menu_vid_allowdualscreenresolution <= 0)
147 me.addResolution(me, r.x, r.y, r.z);
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");
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);
163 // Add it, and limit all other resolutions to the workarea/desktop size.
164 if(r.x != 0 || r.y != 0)
166 me.maxAllowedWidth = r.x;
167 me.maxAllowedHeight = r.y;
168 me.addResolution(me, r.x, r.y, r.z);
171 // Add nice hardcoded defaults.
172 me.addResolution(me, 640, 480, 1); // pc res
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
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");
189 LOG_TRACE("Total number of resolutions detected: ", ftos(me.nValues), "\n");
191 me.vid_fullscreen = fullscreen;
193 me.configureXonoticTextSliderValues(me);
195 void XonoticResolutionSlider_loadCvars(entity me)
197 me.setValueFromIdentifier_noAnim(me, strcat(cvar_string("_menu_vid_width"), " ", cvar_string("_menu_vid_height"), " ", cvar_string("_menu_vid_pixelheight")));
199 void XonoticResolutionSlider_saveCvars(entity me)
201 if(me.value >= 0 || me.value < me.nValues)
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");
211 cvar_set("_menu_vid_desktopfullscreen", "0");
214 void XonoticResolutionSlider_draw(entity me)
216 if (cvar("vid_fullscreen") != me.vid_fullscreen)
218 me.loadResolutions(me, cvar("vid_fullscreen"));
219 XonoticResolutionSlider_DataHasChanged = true;
221 if (XonoticResolutionSlider_DataHasChanged)
223 XonoticResolutionSlider_DataHasChanged = false;
226 SUPER(XonoticResolutionSlider).draw(me);