Skip to content

Commit 755d3c0

Browse files
committed
Make VideoMode sortable and return as std::set
1 parent ef48af7 commit 755d3c0

5 files changed

Lines changed: 34 additions & 28 deletions

File tree

libs/driver/include/driver/VideoMode.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ struct VideoMode
1414
constexpr VideoMode(unsigned short width, unsigned short height) : width(width), height(height) {}
1515
constexpr bool operator==(const VideoMode& o) const { return (width == o.width && height == o.height); }
1616
constexpr bool operator!=(const VideoMode& o) const { return !(*this == o); }
17+
constexpr bool operator<(const VideoMode& o) const
18+
{
19+
const auto area = static_cast<unsigned>(width) * height;
20+
const auto otherArea = static_cast<unsigned>(o.width) * o.height;
21+
if(area != otherArea)
22+
return area < otherArea;
23+
if(width != o.width)
24+
return width < o.width;
25+
return height < o.height;
26+
}
1727
};
1828

1929
// Enum like type with extra flag

libs/s25main/desktops/dskOptions.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -755,22 +755,14 @@ void dskOptions::Msg_MsgBoxResult(const unsigned msgbox_id, const MsgboxResult /
755755
}
756756
}
757757

758-
static bool cmpVideoModes(const VideoMode& left, const VideoMode& right)
759-
{
760-
if(left.width == right.width)
761-
return left.height > right.height;
762-
else
763-
return left.width > right.width;
764-
}
765-
766758
void dskOptions::loadVideoModes()
767759
{
768760
// Get available modes
769-
videoModes_ = VIDEODRIVER.ListVideoModes();
770-
// Sort by aspect ratio
771-
helpers::sort(videoModes_, cmpVideoModes);
772-
windowSizes_ = VIDEODRIVER.GetDefaultWindowSizes();
773-
std::sort(windowSizes_.begin(), windowSizes_.end(), cmpVideoModes);
761+
const auto videoModes = VIDEODRIVER.ListVideoModes();
762+
// random access is need for selection
763+
videoModes_.assign(videoModes.begin(), videoModes.end());
764+
const auto windowSizes = VIDEODRIVER.GetDefaultWindowSizes();
765+
windowSizes_.assign(windowSizes.begin(), windowSizes.end());
774766
}
775767

776768
void dskOptions::Msg_ScreenResize(const ScreenResizeEvent& sr)

libs/s25main/drivers/VideoDriverWrapper.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -428,22 +428,23 @@ void VideoDriverWrapper::SetMousePos(const Position& newPos)
428428
videodriver->SetMousePos(newPos);
429429
}
430430

431-
std::vector<VideoMode> VideoDriverWrapper::ListVideoModes() const
431+
std::set<VideoMode> VideoDriverWrapper::ListVideoModes() const
432432
{
433433
if(!videodriver)
434434
return {};
435435

436-
auto videoModes = videodriver->ListVideoModes();
437-
// Remove everything below 800x600
438-
helpers::erase_if(videoModes,
439-
[](const auto& m) { return m.width < MinWindowSize.width && m.height < MinWindowSize.height; });
440-
return videoModes;
436+
const auto videoModes = videodriver->ListVideoModes();
437+
std::set<VideoMode> result;
438+
// Keep only modes larger than the minimum
439+
std::copy_if(videoModes.begin(), videoModes.end(), std::inserter(result, result.end()),
440+
[](const auto& m) { return m.width >= MinWindowSize.width || m.height >= MinWindowSize.height; });
441+
return result;
441442
}
442443

443-
std::vector<VideoMode> VideoDriverWrapper::GetDefaultWindowSizes() const
444+
std::set<VideoMode> VideoDriverWrapper::GetDefaultWindowSizes() const
444445
{
445446
// clang-format off
446-
std::vector<VideoMode> defaultWindowSizes = {
447+
std::array defaultWindowSizes = {
447448
VideoMode(800, 600),
448449
VideoMode(1024, 768),
449450
VideoMode(1152, 648),
@@ -456,9 +457,8 @@ std::vector<VideoMode> VideoDriverWrapper::GetDefaultWindowSizes() const
456457
VideoMode(1920, 1080)
457458
};
458459
// clang-format on
459-
std::vector<VideoMode> windowSizes = ListVideoModes();
460-
windowSizes.insert(windowSizes.end(), defaultWindowSizes.begin(), defaultWindowSizes.end());
461-
helpers::makeUnique(windowSizes);
460+
std::set<VideoMode> windowSizes = ListVideoModes();
461+
windowSizes.insert(defaultWindowSizes.begin(), defaultWindowSizes.end());
462462
return windowSizes;
463463
}
464464

libs/s25main/drivers/VideoDriverWrapper.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
#include "driver/VideoMode.h"
1313
#include "s25util/Singleton.h"
1414
#include <memory>
15+
#include <set>
1516
#include <string>
17+
#include <vector>
1618

1719
class IVideoDriver;
1820
class IRenderer;
@@ -61,8 +63,8 @@ class VideoDriverWrapper : public Singleton<VideoDriverWrapper, SingletonPolicie
6163
Position GetMousePos() const;
6264

6365
/// Get supported resolutions
64-
std::vector<VideoMode> ListVideoModes() const;
65-
std::vector<VideoMode> GetDefaultWindowSizes() const;
66+
std::set<VideoMode> ListVideoModes() const;
67+
std::set<VideoMode> GetDefaultWindowSizes() const;
6668
bool HasVSync() const;
6769

6870
/// Gibt Pointer auf ein Fenster zurück (device-dependent!), HWND unter Windows

libs/s25main/ingameWindows/iwSettings.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,16 @@ iwSettings::iwSettings()
7777
->AddCheckBox(ID_cbLockWindowSize, curPos, cbSize, TextureColor::Grey, _("Lock window size:"), NormalFont, false)
7878
->setChecked(!SETTINGS.video.displayMode.resizeable);
7979

80-
supportedResolutions_ = VIDEODRIVER.ListVideoModes();
80+
const auto supportedResolutions = VIDEODRIVER.ListVideoModes();
81+
supportedResolutions_.assign(supportedResolutions.begin(), supportedResolutions.end());
8182
for(const auto& videoMode : supportedResolutions_)
8283
{
8384
cbResolution->AddItem(helpers::format("%ux%u", videoMode.width, videoMode.height));
8485
if(videoMode == SETTINGS.video.fullscreenSize)
8586
cbResolution->SetSelection(cbResolution->GetNumItems() - 1);
8687
}
87-
windowSizes_ = VIDEODRIVER.GetDefaultWindowSizes();
88+
const auto windowSizes = VIDEODRIVER.GetDefaultWindowSizes();
89+
windowSizes_.assign(windowSizes.begin(), windowSizes.end());
8890
cbWindowSize.AddItem(""); // Placeholder for current window size
8991
for(const auto& size : windowSizes_)
9092
cbWindowSize.AddItem(helpers::format("%ux%u", size.width, size.height));

0 commit comments

Comments
 (0)