Minor fix
[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, addResolution, void(entity, float, float, float))
5         METHOD(XonoticResolutionSlider, loadCvars, void(entity))
6         METHOD(XonoticResolutionSlider, saveCvars, void(entity))
7 ENDCLASS(XonoticResolutionSlider)
8 entity makeXonoticResolutionSlider();
9 void initConwidths();
10 void updateConwidths();
11 #endif
12
13 #ifdef IMPLEMENTATION
14 void initConwidths()
15 {
16         cvar_set("_menu_vid_width", cvar_string("vid_width"));
17         cvar_set("_menu_vid_height", cvar_string("vid_height"));
18         cvar_set("_menu_vid_pixelheight", cvar_string("vid_pixelheight"));
19 }
20 void updateConwidths()
21 {
22         vector r, c;
23         float minfactor, maxfactor;
24         float sz, f;
25         r_x = cvar("_menu_vid_width");
26         r_y = cvar("_menu_vid_height");
27         r_z = cvar("_menu_vid_pixelheight");
28         sz = cvar("menu_vid_scale");
29
30         // calculate the base resolution
31         c_z = 0;
32         c_x = 800;
33         c_y = c_x * r_y * r_z / r_x;
34         if(c_y < 600)
35         {
36                 c_y = 600;
37                 c_x = c_y * r_x / (r_y * r_z);
38         }
39
40         f = min(r_x / c_x, r_y / c_y);
41         if(f < 1)
42                 c = c * f; // ensures that c_x <= r_x and c_y <= r_y
43
44         minfactor = min(1, 640 / c_x);            // can be > 1 only if c_x is <640
45         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
46         dprint("min factor: ", ftos(minfactor), "\n");
47         dprint("max factor: ", ftos(maxfactor), "\n");
48
49         if(sz < 0)
50                 f = 1 - (maxfactor - 1) * sz;
51         else if(sz > 0)
52                 f = 1 + (minfactor - 1) * sz;
53         else
54                 f = 1;
55         c = c * f; // fteqcc fail
56
57         cvar_set("vid_width", ftos(rint(r_x)));
58         cvar_set("vid_height", ftos(rint(r_y)));
59         cvar_set("vid_pixelheight", ftos(rint(r_z)));
60         cvar_set("vid_conwidth", ftos(rint(c_x)));
61         cvar_set("vid_conheight", ftos(rint(c_y)));
62 }
63 entity makeXonoticResolutionSlider()
64 {
65         entity me;
66         me = spawnXonoticResolutionSlider();
67         me.configureXonoticResolutionSlider(me);
68         return me;
69 }
70 void XonoticResolutionSlider_addResolution(entity me, float w, float h, float pixelheight)
71 {
72         me.addValue(me, strzone(sprintf(_("%dx%d"), w, h)), strzone(strcat(ftos(w), " ", ftos(h), " ", ftos(pixelheight))));
73         // FIXME (in case you ever want to dynamically instantiate this): THIS IS NEVER FREED
74 }
75 float autocvar_menu_vid_allowdualscreenresolution;
76 void XonoticResolutionSlider_configureXonoticResolutionSlider(entity me)
77 {
78         float i;
79         vector r0, r;
80
81         me.configureXonoticTextSlider(me, "_menu_vid_width");
82
83         r0 = '0 0 0';
84         for(i = 0;; ++i)
85         {
86                 r = getresolution(i);
87                 if(r_x == 0 && r_y == 0)
88                         break;
89                 if(r_z == 0)
90                         r_z = 1; // compat
91                 if(r == r0)
92                         continue;
93                 r0 = r;
94                 if(r_x < 640 || r_y < 480)
95                         continue;
96                 if(r_x > 2 * r_y) // likely dualscreen resolution, skip this one
97                         if(autocvar_menu_vid_allowdualscreenresolution <= 0)
98                                 continue;
99                         
100                 me.addResolution(me, r_x, r_y, r_z);
101         }
102
103         if(me.nValues == 0)
104         {
105                 me.addResolution(me, 640, 480, 1);
106                 me.addResolution(me, 800, 600, 1);
107                 me.addResolution(me, 1024, 768, 1);
108                 me.addResolution(me, 1280, 960, 1);
109                 me.addResolution(me, 1280, 1024, 1);
110                 me.addResolution(me, 1650, 1080, 1);
111                 me.addResolution(me, 1920, 1080, 1);
112         }
113
114         me.configureXonoticTextSliderValues(me);
115 }
116 void XonoticResolutionSlider_loadCvars(entity me)
117 {
118         me.setValueFromIdentifier(me, strcat(cvar_string("_menu_vid_width"), " ", cvar_string("_menu_vid_height"), " ", cvar_string("_menu_vid_pixelheight")));
119 }
120 void XonoticResolutionSlider_saveCvars(entity me)
121 {
122         if(me.value >= 0 || me.value < me.nValues)
123         {
124                 tokenize_console(me.getIdentifier(me));
125                 cvar_set("_menu_vid_width", argv(0));
126                 cvar_set("_menu_vid_height", argv(1));
127                 cvar_set("_menu_vid_pixelheight", argv(2));
128         }
129 }
130 #endif