Skip to content

Commit 3c8741a

Browse files
committed
Populate display resolution list at runtime.
1 parent 120ae28 commit 3c8741a

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

src/game.cpp

+35-3
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ bool Game::InitGUI()
481481
settings.Get(optionmap);
482482
gui.SetOptions(optionmap);
483483

484-
// Set driver/edit num to update gui explicitely
484+
// Set driver/edit num to update gui explicitly
485485
// as they are not stored in settings
486486
gui.SetOptionValue("game.car_edit", "0");
487487
gui.SetOptionValue("game.driver", "");
@@ -2089,9 +2089,9 @@ static void PopulateTrackSet(
20892089
template <class T0, class T1>
20902090
struct SortPairBySecond
20912091
{
2092-
bool operator()(const std::pair<T0, T1> & first, const std::pair<T0, T1> & second)
2092+
bool operator()(const std::pair<T0, T1> & a, const std::pair<T0, T1> & b)
20932093
{
2094-
return first.second < second.second;
2094+
return a.second < b.second;
20952095
}
20962096
};
20972097

@@ -2306,6 +2306,36 @@ void Game::PopulateAntialiasList(GuiOption::List & antialiaslist)
23062306
}
23072307
}
23082308

2309+
void Game::PopulateResolutionList(GuiOption::List & resolutionlist)
2310+
{
2311+
resolutionlist.clear();
2312+
int w = 0, h = 0;
2313+
int n = window.GetNumSupportedResolutions(error_output);
2314+
std::vector<int> res(n);
2315+
for (int i = 0; i < n; i++)
2316+
{
2317+
window.GetSupportedResolution(i, w, h, error_output);
2318+
assert(w <= 0xFFFF && h <= 0xFFFF);
2319+
res[i] = (w << 16) | h;
2320+
}
2321+
std::sort(res.begin(), res.end());
2322+
int wp = 0, hp = 0;
2323+
for (int i = 0; i < n; i++)
2324+
{
2325+
w = res[i] >> 16;
2326+
h = res[i] & 0xFFFF;
2327+
if (w != wp || h != hp)
2328+
{
2329+
std::ostringstream ds, vs;
2330+
ds << w << " X " << h;
2331+
vs << w << ',' << h;
2332+
resolutionlist.push_back(std::make_pair(vs.str(), ds.str()));
2333+
wp = w;
2334+
hp = h;
2335+
}
2336+
}
2337+
}
2338+
23092339
void Game::PopulateValueLists(std::map<std::string, GuiOption::List> & valuelists)
23102340
{
23112341
PopulateTrackList(valuelists["tracks"]);
@@ -2334,6 +2364,8 @@ void Game::PopulateValueLists(std::map<std::string, GuiOption::List> & valuelist
23342364

23352365
PopulateAntialiasList(valuelists["antialiasing"]);
23362366

2367+
PopulateResolutionList(valuelists["resolution"]);
2368+
23372369
// PopulateJoystickList
23382370
valuelists["joy_indices"].push_back(std::make_pair("0", "0"));
23392371
}

src/game.h

+2
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ friend class GameDownloader;
180180

181181
void PopulateAntialiasList(GuiOption::List & antialiaslist);
182182

183+
void PopulateResolutionList(GuiOption::List & resolutionlist);
184+
183185
void UpdateTrackMap();
184186

185187
void ShowLoadingScreen(float progress, float progress_max, const std::string & optional_text);

src/window.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,30 @@ static int GetVideoDisplay()
169169
return 0;
170170
}
171171

172+
int Window::GetNumSupportedResolutions(std::ostream & error_output)
173+
{
174+
int display = GetVideoDisplay();
175+
int i = SDL_GetNumDisplayModes(display);
176+
if (i < 0) {
177+
error_output << SDL_GetError() << std::endl;
178+
return 0;
179+
}
180+
return i;
181+
}
182+
183+
void Window::GetSupportedResolution(int i, int & w, int & h, std::ostream & error_output)
184+
{
185+
SDL_DisplayMode mode;
186+
int display = GetVideoDisplay();
187+
int err = SDL_GetDisplayMode(display, i, &mode);
188+
if (err) {
189+
error_output << SDL_GetError() << std::endl;
190+
return;
191+
}
192+
w = mode.w;
193+
h = mode.h;
194+
}
195+
172196
bool Window::ResizeWindow(int width, int height)
173197
{
174198
// We can't resize something we don't have.

src/window.h

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ class Window
5656

5757
int GetH() const;
5858

59+
int GetNumSupportedResolutions(std::ostream & error_output);
60+
61+
void GetSupportedResolution(int i, int & w, int & h, std::ostream & error_output);
62+
5963
private:
6064
SDL_Window * window;
6165
void * glcontext;

0 commit comments

Comments
 (0)