]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/menu/xonotic/slider_resolution.c
Fix crash.
[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
31         r_x = width;
32         r_y = height;
33         r_z = pixelheight;
34         sz = cvar("menu_vid_scale");
35
36         // calculate the base resolution
37         c_z = 0;
38         c_x = 800;
39         c_y = c_x * r_y * r_z / r_x;
40         if(c_y < 600)
41         {
42                 c_y = 600;
43                 c_x = c_y * r_x / (r_y * r_z);
44         }
45
46         f = min(r_x / c_x, r_y / c_y);
47         if(f < 1)
48                 c = c * f; // ensures that c_x <= r_x and c_y <= r_y
49
50         minfactor = min(1, 640 / c_x);            // can be > 1 only if c_x is <640
51         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
52         dprint("min factor: ", ftos(minfactor), "\n");
53         dprint("max factor: ", ftos(maxfactor), "\n");
54
55         if(sz < 0)
56                 f = 1 - (maxfactor - 1) * sz;
57         else if(sz > 0)
58                 f = 1 + (minfactor - 1) * sz;
59         else
60                 f = 1;
61         c = c * f; // fteqcc fail
62
63         cvar_set("vid_conwidth", ftos(rint(c_x)));
64         cvar_set("vid_conheight", ftos(rint(c_y)));
65         XonoticResolutionSlider_DataHasChanged = TRUE;
66 }
67 entity makeXonoticResolutionSlider()
68 {
69         entity me;
70         me = spawnXonoticResolutionSlider();
71         me.configureXonoticResolutionSlider(me);
72         return me;
73 }
74 void XonoticResolutionSlider_addResolution(entity me, float w, float h, float pixelheight)
75 {
76         if (pixelheight != 1)
77         {
78                 float aspect = w / (h * pixelheight);
79                 float bestdenom = rint(aspect);
80                 float bestnum = 1;
81                 float denom;
82                 for (denom = 2; denom < 10; ++denom) {
83                         float num = rint(aspect * denom);
84                         if (fabs(num / denom - aspect) < fabs(bestnum / bestdenom - aspect))
85                         {
86                                 bestnum = num;
87                                 bestdenom = denom;
88                         }
89                 }
90                 me.addValue(me, strzone(sprintf(_("%dx%d (%d:%d)"), w, h, bestnum, bestdenom)), strzone(strcat(ftos(w), " ", ftos(h), " ", ftos(pixelheight))));
91         }
92         else
93                 me.addValue(me, strzone(sprintf(_("%dx%d"), w, h)), strzone(strcat(ftos(w), " ", ftos(h), " ", ftos(pixelheight))));
94         // FIXME (in case you ever want to dynamically instantiate this): THIS IS NEVER FREED
95 }
96 float autocvar_menu_vid_allowdualscreenresolution;
97 void XonoticResolutionSlider_configureXonoticResolutionSlider(entity me)
98 {
99         me.configureXonoticTextSlider(me, "_menu_vid_width");
100         me.loadResolutions(me, cvar("vid_fullscreen"));
101 }
102 void XonoticResolutionSlider_loadResolutions(entity me, float fullscreen)
103 {
104         float i;
105         vector r0, r;
106
107         me.clearValues(me);
108
109         if (fullscreen)
110         {
111                 r0 = '0 0 0';
112                 for(i = 0;; ++i)
113                 {
114                         r = getresolution(i);
115                         if(r_x == 0 && r_y == 0)
116                                 break;
117                         if(r_z == 0)
118                                 r_z = 1; // compat
119                         if(r == r0)
120                                 continue;
121                         r0 = r;
122                         if(r_x < 640 || r_y < 480)
123                                 continue;
124                         if(r_x > 2 * r_y) // likely dualscreen resolution, skip this one
125                                 if(autocvar_menu_vid_allowdualscreenresolution <= 0)
126                                         continue;
127                                 
128                         me.addResolution(me, r_x, r_y, r_z);
129                 }
130         }
131
132         if(me.nValues == 0)
133         {
134                 me.addResolution(me, 640, 480, 1); // pc res
135                 me.addResolution(me, 720, 480, 1.125); // DVD NTSC 4:3
136                 me.addResolution(me, 720, 576, 0.9375); // DVD PAL 4:3
137                 me.addResolution(me, 720, 480, 0.84375); // DVD NTSC 16:9
138                 me.addResolution(me, 720, 576, 0.703125); // DVD PAL 16:9
139                 me.addResolution(me, 800, 480, 1); // 480p at 1:1 pixel aspect
140                 me.addResolution(me, 800, 600, 1); // pc res
141                 me.addResolution(me, 1024, 600, 1); // notebook res
142                 me.addResolution(me, 1024, 768, 1); // pc res
143                 me.addResolution(me, 1280, 720, 1); // 720p
144                 me.addResolution(me, 1280, 960, 1); // pc res
145                 me.addResolution(me, 1280, 1024, 1); // pc res
146                 me.addResolution(me, 1920, 1080, 1); // 1080p
147         }
148
149         me.vid_fullscreen = fullscreen;
150
151         me.configureXonoticTextSliderValues(me);
152 }
153 void XonoticResolutionSlider_loadCvars(entity me)
154 {
155         me.setValueFromIdentifier(me, strcat(cvar_string("_menu_vid_width"), " ", cvar_string("_menu_vid_height"), " ", cvar_string("_menu_vid_pixelheight")));
156 }
157 void XonoticResolutionSlider_saveCvars(entity me)
158 {
159         if(me.value >= 0 || me.value < me.nValues)
160         {
161                 tokenize_console(me.getIdentifier(me));
162                 cvar_set("_menu_vid_width", argv(0));
163                 cvar_set("_menu_vid_height", argv(1));
164                 cvar_set("_menu_vid_pixelheight", argv(2));
165         }
166 }
167 void XonoticResolutionSlider_draw(entity me)
168 {
169         if (cvar("vid_fullscreen") != me.vid_fullscreen)
170         {
171                 me.loadResolutions(me, cvar("vid_fullscreen"));
172                 XonoticResolutionSlider_DataHasChanged = TRUE;
173         }
174         if (XonoticResolutionSlider_DataHasChanged)
175         {
176                 XonoticResolutionSlider_DataHasChanged = FALSE;
177                 me.loadCvars(me);
178         }
179         SUPER(XonoticResolutionSlider).draw(me);
180 }
181 #endif