]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/slider_resolution.c
bfd0e40232e2774fb67ac29814d7a5b78c1c2cb3
[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         cvar_set("vid_conwidth", ftos(rint(c_x)));
65         cvar_set("vid_conheight", ftos(rint(c_y)));
66         XonoticResolutionSlider_DataHasChanged = TRUE;
67 }
68 entity makeXonoticResolutionSlider()
69 {
70         entity me;
71         me = spawnXonoticResolutionSlider();
72         me.configureXonoticResolutionSlider(me);
73         return me;
74 }
75 void XonoticResolutionSlider_addResolution(entity me, float w, float h, float pixelheight, float desktopfullscreen)
76 {
77         if (pixelheight != 1)
78         {
79                 float aspect = w / (h * pixelheight);
80                 float bestdenom = rint(aspect);
81                 float bestnum = 1;
82                 float denom;
83                 for (denom = 2; denom < 10; ++denom) {
84                         float num = rint(aspect * denom);
85                         if (fabs(num / denom - aspect) < fabs(bestnum / bestdenom - aspect))
86                         {
87                                 bestnum = num;
88                                 bestdenom = denom;
89                         }
90                 }
91                 me.addValue(me, strzone(sprintf(_("%dx%d (%d:%d)"), w, h, bestnum, bestdenom)), strzone(strcat(ftos(w), " ", ftos(h), " ", ftos(pixelheight))));
92         }
93         else
94                 me.addValue(me, strzone(sprintf(_("%dx%d"), w, h)), strzone(strcat(ftos(w), " ", ftos(h), " ", ftos(pixelheight))));
95         // FIXME (in case you ever want to dynamically instantiate this): THIS IS NEVER FREED
96 }
97 float autocvar_menu_vid_allowdualscreenresolution;
98 void XonoticResolutionSlider_configureXonoticResolutionSlider(entity me)
99 {
100         me.configureXonoticTextSlider(me, "_menu_vid_width");
101         me.loadResolutions(me, cvar("vid_fullscreen"));
102 }
103 void XonoticResolutionSlider_loadResolutions(entity me, float fullscreen)
104 {
105         float i;
106         vector r0, r;
107
108         me.clearValues(me);
109
110         if (fullscreen)
111         {
112                 r0 = '0 0 0';
113                 for(i = 0;; ++i)
114                 {
115                         r = getresolution(i);
116                         if(r_x == 0 && r_y == 0)
117                         {
118                                 i = -1;
119                                 r = getresolution(i);
120                         }
121                         if(r_x == 0 && r_y == 0)
122                                 break;
123                         if(r == r0)
124                                 continue;
125                         r0 = r;
126                         if(r_x < 640 || r_y < 480)
127                                 continue;
128                         if(i != -1)
129                                 if(r_x > 2 * r_y) // likely dualscreen resolution, skip this one
130                                         if(autocvar_menu_vid_allowdualscreenresolution <= 0)
131                                                 continue;
132
133                         me.addResolution(me, r_x, r_y, r_z);
134
135                         if (i == -1)
136                                 break;
137                 }
138         }
139
140         if(me.nValues == 0)
141         {
142                 me.addResolution(me, 640, 480, 1); // pc res
143                 me.addResolution(me, 720, 480, 1.125); // DVD NTSC 4:3
144                 me.addResolution(me, 720, 576, 0.9375); // DVD PAL 4:3
145                 me.addResolution(me, 720, 480, 0.84375); // DVD NTSC 16:9
146                 me.addResolution(me, 720, 576, 0.703125); // DVD PAL 16:9
147                 me.addResolution(me, 800, 480, 1); // 480p at 1:1 pixel aspect
148                 me.addResolution(me, 800, 600, 1); // pc res
149                 me.addResolution(me, 1024, 600, 1); // notebook res
150                 me.addResolution(me, 1024, 768, 1); // pc res
151                 me.addResolution(me, 1280, 720, 1); // 720p
152                 me.addResolution(me, 1280, 960, 1); // pc res
153                 me.addResolution(me, 1280, 1024, 1); // pc res
154                 me.addResolution(me, 1920, 1080, 1); // 1080p
155         }
156
157         me.vid_fullscreen = fullscreen;
158
159         me.configureXonoticTextSliderValues(me);
160 }
161 void XonoticResolutionSlider_loadCvars(entity me)
162 {
163         me.setValueFromIdentifier(me, strcat(cvar_string("_menu_vid_width"), " ", cvar_string("_menu_vid_height"), " ", cvar_string("_menu_vid_pixelheight")));
164 }
165 void XonoticResolutionSlider_saveCvars(entity me)
166 {
167         if(me.value >= 0 || me.value < me.nValues)
168         {
169                 tokenize_console(me.getIdentifier(me));
170                 cvar_set("_menu_vid_width", argv(0));
171                 cvar_set("_menu_vid_height", argv(1));
172                 cvar_set("_menu_vid_pixelheight", argv(2));
173                 vector r = getresolution(-1);
174                 if (stof(argv(0)) == r_x && stof(argv(1)) == r_y && fabs(stof(argv(2)) - r_z) < 0.01)
175                         cvar_set("_menu_vid_desktopfullscreen", "1");
176                 else
177                         cvar_set("_menu_vid_desktopfullscreen", "0");
178         }
179 }
180 void XonoticResolutionSlider_draw(entity me)
181 {
182         if (cvar("vid_fullscreen") != me.vid_fullscreen)
183         {
184                 me.loadResolutions(me, cvar("vid_fullscreen"));
185                 XonoticResolutionSlider_DataHasChanged = TRUE;
186         }
187         if (XonoticResolutionSlider_DataHasChanged)
188         {
189                 XonoticResolutionSlider_DataHasChanged = FALSE;
190                 me.loadCvars(me);
191         }
192         SUPER(XonoticResolutionSlider).draw(me);
193 }
194 #endif