VideoMode.cpp
1 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
26 // Headers
28 #include <SFML/Window/VideoMode.hpp>
29 #include <SFML/Window/VideoModeSupport.hpp>
30 #include <algorithm>
31 #include <vector>
32 
33 
37 namespace
38 {
39  // Global array of supported video modes
40  std::vector<sf::VideoMode> SupportedModes;
41 
42  // Functor for sorting modes from highest to lowest
43  struct CompareModes
44  {
45  bool operator ()(const sf::VideoMode& v1, const sf::VideoMode& v2) const
46  {
47  if (v1.BitsPerPixel > v2.BitsPerPixel)
48  return true;
49  else if (v1.BitsPerPixel < v2.BitsPerPixel)
50  return false;
51  else if (v1.Width > v2.Width)
52  return true;
53  else if (v1.Width < v2.Width)
54  return false;
55  else
56  return (v1.Height > v2.Height);
57  }
58  };
59 }
60 
61 
62 namespace sf
63 {
68 Width (0),
69 Height (0),
70 BitsPerPixel(0)
71 {
72 
73 }
74 
75 
79 VideoMode::VideoMode(unsigned int ModeWidth, unsigned int ModeHeight, unsigned int ModeBpp) :
80 Width (ModeWidth),
81 Height (ModeHeight),
82 BitsPerPixel(ModeBpp)
83 {
84 
85 }
86 
87 
92 {
93  // Directly forward to the video mode support
94  return priv::VideoModeSupport::GetDesktopVideoMode();
95 }
96 
97 
102 VideoMode VideoMode::GetMode(std::size_t Index)
103 {
104  if (SupportedModes.empty())
105  InitializeModes();
106 
107  if (Index < GetModesCount())
108  return SupportedModes[Index];
109  else
110  return VideoMode();
111 }
112 
113 
118 {
119  if (SupportedModes.empty())
120  InitializeModes();
121 
122  return SupportedModes.size();
123 }
124 
125 
129 bool VideoMode::IsValid() const
130 {
131  if (SupportedModes.empty())
132  InitializeModes();
133 
134  return std::find(SupportedModes.begin(), SupportedModes.end(), *this) != SupportedModes.end();
135 }
136 
137 
141 bool VideoMode::operator ==(const VideoMode& Other) const
142 {
143  return (Width == Other.Width) &&
144  (Height == Other.Height) &&
145  (BitsPerPixel == Other.BitsPerPixel);
146 }
147 
148 
152 bool VideoMode::operator !=(const VideoMode& Other) const
153 {
154  return !(*this == Other);
155 }
156 
157 
161 void VideoMode::InitializeModes()
162 {
163  // We request the array of valid modes
164  priv::VideoModeSupport::GetSupportedVideoModes(SupportedModes);
165 
166  // And we sort them from highest to lowest (so that number 0 is the best)
167  std::sort(SupportedModes.begin(), SupportedModes.end(), CompareModes());
168 }
169 
170 } // namespace sf
static VideoMode GetDesktopMode()
Get the current desktop video mode.
Definition: VideoMode.cpp:91
bool operator==(const VideoMode &Other) const
Comparison operator overload – tell if two video modes are equal.
Definition: VideoMode.cpp:141
bool IsValid() const
Tell whether or not the video mode is supported.
Definition: VideoMode.cpp:129
static VideoMode GetMode(std::size_t Index)
Get a valid video mode Index must be in range [0, GetModesCount()[ Modes are sorted from best to wors...
Definition: VideoMode.cpp:102
VideoMode defines a video mode (width, height, bpp, frequency) and provides static functions for gett...
Definition: VideoMode.hpp:42
VideoMode()
Default constructor.
Definition: VideoMode.cpp:67
unsigned int Width
Video mode width, in pixels.
Definition: VideoMode.hpp:121
unsigned int Height
Video mode height, in pixels.
Definition: VideoMode.hpp:122
bool operator!=(const VideoMode &Other) const
Comparison operator overload – tell if two video modes are different.
Definition: VideoMode.cpp:152
unsigned int BitsPerPixel
Video mode pixel depth, in bits per pixels.
Definition: VideoMode.hpp:123
static std::size_t GetModesCount()
Get valid video modes count.
Definition: VideoMode.cpp:117