Skip to content

Commit

Permalink
Populate display resolution list at runtime.
Browse files Browse the repository at this point in the history
  • Loading branch information
logzero committed Oct 26, 2024
1 parent 120ae28 commit 3c8741a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ bool Game::InitGUI()
settings.Get(optionmap);
gui.SetOptions(optionmap);

// Set driver/edit num to update gui explicitely
// Set driver/edit num to update gui explicitly
// as they are not stored in settings
gui.SetOptionValue("game.car_edit", "0");
gui.SetOptionValue("game.driver", "");
Expand Down Expand Up @@ -2089,9 +2089,9 @@ static void PopulateTrackSet(
template <class T0, class T1>
struct SortPairBySecond
{
bool operator()(const std::pair<T0, T1> & first, const std::pair<T0, T1> & second)
bool operator()(const std::pair<T0, T1> & a, const std::pair<T0, T1> & b)
{
return first.second < second.second;
return a.second < b.second;
}
};

Expand Down Expand Up @@ -2306,6 +2306,36 @@ void Game::PopulateAntialiasList(GuiOption::List & antialiaslist)
}
}

void Game::PopulateResolutionList(GuiOption::List & resolutionlist)
{
resolutionlist.clear();
int w = 0, h = 0;
int n = window.GetNumSupportedResolutions(error_output);
std::vector<int> res(n);
for (int i = 0; i < n; i++)
{
window.GetSupportedResolution(i, w, h, error_output);
assert(w <= 0xFFFF && h <= 0xFFFF);
res[i] = (w << 16) | h;
}
std::sort(res.begin(), res.end());
int wp = 0, hp = 0;
for (int i = 0; i < n; i++)
{
w = res[i] >> 16;
h = res[i] & 0xFFFF;
if (w != wp || h != hp)
{
std::ostringstream ds, vs;
ds << w << " X " << h;
vs << w << ',' << h;
resolutionlist.push_back(std::make_pair(vs.str(), ds.str()));
wp = w;
hp = h;
}
}
}

void Game::PopulateValueLists(std::map<std::string, GuiOption::List> & valuelists)
{
PopulateTrackList(valuelists["tracks"]);
Expand Down Expand Up @@ -2334,6 +2364,8 @@ void Game::PopulateValueLists(std::map<std::string, GuiOption::List> & valuelist

PopulateAntialiasList(valuelists["antialiasing"]);

PopulateResolutionList(valuelists["resolution"]);

// PopulateJoystickList
valuelists["joy_indices"].push_back(std::make_pair("0", "0"));
}
Expand Down
2 changes: 2 additions & 0 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ friend class GameDownloader;

void PopulateAntialiasList(GuiOption::List & antialiaslist);

void PopulateResolutionList(GuiOption::List & resolutionlist);

void UpdateTrackMap();

void ShowLoadingScreen(float progress, float progress_max, const std::string & optional_text);
Expand Down
24 changes: 24 additions & 0 deletions src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,30 @@ static int GetVideoDisplay()
return 0;
}

int Window::GetNumSupportedResolutions(std::ostream & error_output)
{
int display = GetVideoDisplay();
int i = SDL_GetNumDisplayModes(display);
if (i < 0) {
error_output << SDL_GetError() << std::endl;
return 0;
}
return i;
}

void Window::GetSupportedResolution(int i, int & w, int & h, std::ostream & error_output)
{
SDL_DisplayMode mode;
int display = GetVideoDisplay();
int err = SDL_GetDisplayMode(display, i, &mode);
if (err) {
error_output << SDL_GetError() << std::endl;
return;
}
w = mode.w;
h = mode.h;
}

bool Window::ResizeWindow(int width, int height)
{
// We can't resize something we don't have.
Expand Down
4 changes: 4 additions & 0 deletions src/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class Window

int GetH() const;

int GetNumSupportedResolutions(std::ostream & error_output);

void GetSupportedResolution(int i, int & w, int & h, std::ostream & error_output);

private:
SDL_Window * window;
void * glcontext;
Expand Down

0 comments on commit 3c8741a

Please sign in to comment.