Debian Codegraph @

code/apis/resolution_manager.cpp

raw
1 /*
2 Copyright (C) 2016 wxLauncher Team
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include "generated/configure_launcher.h"
20
21 #include "apis/resolution_manager.hpp"
22 #include "global/ProfileKeys.h"
23
24 #if HAS_SDL
25 #include "SDL.h"
26 #endif
27
28 /** \namespace ResolutionMan
29 The ResolutionMan namespace contains the code that gets the
30 available resolutions for the BasicSettingsTab.
31 */
32
33 using namespace ResolutionMan;
34
35 Resolution::Resolution(const int width, const int height, const bool isHeader) {
36 wxASSERT_MSG(width > 0, wxString::Format(_T("Resolution: provided width %d is invalid"), width));
37 wxASSERT_MSG(height > 0, wxString::Format(_T("Resolution: provided height %d is invalid"), height));
38 this->width = width;
39 this->height = height;
40 this->isHeader = isHeader;
41 resString = isHeader ? wxString::Format(_T("--- %d:%d ---"), width, height)
42 : wxString::Format(CFG_RES_FORMAT_STRING, width, height);
43 }
44
45 Resolution* Resolution::New() const {
46 return new Resolution(this->GetWidth(), this->GetHeight(),
47 this->IsHeader());
48 }
49
50 // sort by aspect ratio, then by size, in descending order
51 int CompareResolutions(Resolution **resp1, Resolution **resp2) {
52
53 wxASSERT_MSG(resp1 != NULL, _T("CompareResolutions: provided resp1 is NULL"));
54 wxASSERT_MSG((*resp1) != NULL, _T("CompareResolutions: resp1 points to NULL"));
55 wxASSERT_MSG(resp2 != NULL, _T("CompareResolutions: provided resp2 is NULL"));
56 wxASSERT_MSG((*resp2) != NULL, _T("CompareResolutions: resp2 points to NULL"));
57
58 // compare the two width/height ratios by cross-multiplying and comparing the results
59 const int width1 = (*resp1)->GetWidth();
60 const int height1 = (*resp1)->GetHeight();
61 const int width2 = (*resp2)->GetWidth();
62 const int height2 = (*resp2)->GetHeight();
63
64 wxASSERT_MSG(width1 > 0, wxString::Format(_T("CompareResolutions: width1 %d is invalid"), width1));
65 wxASSERT_MSG(height1 > 0, wxString::Format(_T("CompareResolutions: height1 %d is invalid"), height1));
66 wxASSERT_MSG(width2 > 0, wxString::Format(_T("CompareResolutions: width2 %d is invalid"), width2));
67 wxASSERT_MSG(height2 > 0, wxString::Format(_T("CompareResolutions: height2 %d is invalid"), height2));
68
69 const int value1 = width1 * height2;
70 const int value2 = width2 * height1;
71 int result;
72 // first compare aspect ratio
73 if (value1 < value2) {
74 result = -1;
75 }
76 else if (value1 > value2) {
77 result = 1;
78 }
79 else { // then compare size if aspect ratios are equal
80 if (width1 < width2) {
81 result = -1;
82 }
83 else if (width1 > width2) {
84 result = 1;
85 }
86 else {
87 result = 0;
88 }
89 }
90
91 return (-1) * result; // for reverse comparison (descending order)
92 }
93
94 // brought to you by http://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations
95 int ComputeGCD(int a, int b) {
96 wxASSERT_MSG(a > 0, wxString::Format(_T("ComputeGCD(a=%d, b=%d): a must be positive"), a, b));
97 wxASSERT_MSG(b > 0, wxString::Format(_T("ComputeGCD(a=%d, b=%d): b must be positive"), a, b));
98
99 // part of the original algorithm, but irrelevant, since a and b are positive
100 // if (a == 0) {
101 // return b;
102 // }
103 while (b != 0) {
104 if (a > b) {
105 a = a - b;
106 }
107 else {
108 b = b - a;
109 }
110 }
111 return a;
112 }
113
114 // Assumed resolutions has been sorted using CompareResolutions
115 void AddHeaders(ResolutionArray &resolutions) {
116 if (resolutions.IsEmpty()) {
117 wxLogWarning(_T("AddHeaders: provided ResolutionArray is empty"));
118 return;
119 }
120
121 ResolutionArray headers;
122 wxArrayInt headerIndexes;
123
124 int lastAspectWidth = -1;
125 int lastAspectHeight = -1;
126 int width;
127 int height;
128 int gcd;
129
130 // find aspect ratios and determine where the aspect ratio headers should be inserted
131 for (int i = 0, n = resolutions.GetCount(); i < n; ++i) {
132 width = resolutions.Item(i)->GetWidth();
133 height = resolutions.Item(i)->GetHeight();
134 gcd = ComputeGCD(width, height);
135 width /= gcd;
136 height /= gcd;
137
138 // special exception: 8:5 should be 16:10
139 if ((width == 8) && (height == 5)) {
140 width *= 2;
141 height *= 2;
142 }
143
144 if ((width != lastAspectWidth) || (height != lastAspectHeight)) {
145 wxLogDebug(_T(" found aspect ratio %d:%d"), width, height);
146 headers.Add(new Resolution(width, height, true));
147 headerIndexes.Add(i);
148 lastAspectWidth = width;
149 lastAspectHeight = height;
150 }
151 }
152
153 // now insert the headers into resolutions
154 // must insert in reverse to avoid messing up insertion indexes
155 for (int i = headerIndexes.GetCount() - 1; i >= 0; --i) {
156 resolutions.Insert(headers.Item(i), headerIndexes.Item(i));
157 }
158 }
159
160
161 #ifdef WIN32
162 void EnumerateGraphicsModes_win32(
163 ResolutionArray &out_modes,
164 const DWORD minHorizontalRes,
165 const DWORD minVerticalRes)
166 {
167 DEVMODE deviceMode;
168 DWORD modeCounter = 0;
169 BOOL result;
170
171 wxLogDebug(_T("Enumerating graphics modes with Win32"));
172
173 do {
174 memset(&deviceMode, 0, sizeof(DEVMODE));
175 deviceMode.dmSize = sizeof(DEVMODE);
176
177 result = EnumDisplaySettings(NULL, modeCounter, &deviceMode);
178
179 if (result == TRUE) {
180 wxLogDebug(_T(" %dx%d %d bit %d hertz (%d)"),
181 deviceMode.dmPelsWidth,
182 deviceMode.dmPelsHeight,
183 deviceMode.dmBitsPerPel,
184 deviceMode.dmDisplayFrequency,
185 deviceMode.dmDisplayFlags);
186
187 Resolution candidate(
188 deviceMode.dmPelsWidth, deviceMode.dmPelsHeight, false);
189
190 // check to see if the resolution has already been added
191 // since the current out_modes list is unsorted we need to
192 // iterate over all of the existing ones
193 bool resExists = false;
194 for (auto it = out_modes.begin(), end = out_modes.end();
195 it != end; ++it)
196 {
197 Resolution* p = (Resolution*)(*it);
198 if ((p)->IsSameResolution(candidate)) {
199 resExists = true;
200 break; // no need to keep looking
201 }
202 }
203
204 if (!resExists &&
205 (deviceMode.dmPelsWidth >= minHorizontalRes) &&
206 (deviceMode.dmPelsHeight >= minVerticalRes))
207 {
208 out_modes.Add(candidate.New());
209 }
210 }
211 modeCounter++;
212 } while (result == TRUE);
213 }
214 #endif
215
216 #if HAS_SDL
217 void EnumerateGraphicsModes_sdl(
218 ResolutionArray &out_modes,
219 const int minHorizontalRes,
220 const int minVerticalRes)
221 {
222 wxLogDebug(_T("Enumerating graphics modes with SDL"));
223 // FSO currently only supports the primary display
224 const int DISPLAY_INDEX = 0;
225
226 #if IS_APPLE || IS_WIN32
227 // It seems that linux cannot handle having the video subsystem inited
228 // too late
229 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
230 wxLogFatalError(wxT_2("SDL video subsystem failed to initialize!"));
231 }
232 #endif
233
234 if (SDL_GetNumVideoDisplays() > 0) {
235 int numDisplayModes = SDL_GetNumDisplayModes(DISPLAY_INDEX);
236
237 SDL_DisplayMode mode;
238
239 for (int i = 0; i < numDisplayModes; ++i) {
240 if (SDL_GetDisplayMode(DISPLAY_INDEX, i, &mode) != 0) {
241 wxLogWarning(_T("SDL_GetDisplayMode failed: %s"),
242 SDL_GetError());
243 continue;
244 }
245
246 if ((mode.w >= minHorizontalRes) && (mode.h >= minVerticalRes)) {
247 out_modes.Add(new Resolution(mode.w, mode.h, false));
248 }
249 }
250 }
251 else {
252 wxLogWarning(_T("SDL reported no displays!"));
253 }
254
255 #if IS_APPLE || IS_WIN32
256 SDL_QuitSubSystem(SDL_INIT_VIDEO);
257 #endif
258 #endif
259 }
260
261 #if !HAS_SDL && !defined(WIN32)
262 #error No implementation of EnumerateGraphicsModes available
263 #endif
264
265 /** Get available graphics modes for API and return them sorted */
266 void ResolutionMan::EnumerateGraphicsModes(
267 ApiType type, ResolutionArray& out_modes,
268 const long minHorizontalRes, const long minVerticalRes)
269 {
270 #if IS_WIN32
271 if (type == ResolutionMan::API_WIN32) {
272 EnumerateGraphicsModes_win32(out_modes,
273 static_cast<DWORD>(minHorizontalRes),
274 static_cast<DWORD>(minVerticalRes));
275 }
276 #endif
277 #if HAS_SDL
278 if (type == ResolutionMan::API_SDL) {
279 EnumerateGraphicsModes_sdl(out_modes,
280 static_cast<int>(minHorizontalRes),
281 static_cast<int>(minVerticalRes));
282 }
283 #endif
284
285 // then arrange the resolutions for drop down box and insert them
286 out_modes.Sort(CompareResolutions);
287
288 AddHeaders(out_modes);
289 }