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