Skip to content

Commit

Permalink
Implemented multi-monitor support for windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximilian Wagenbach committed Sep 9, 2015
1 parent c55b8c1 commit 86a6eb9
Show file tree
Hide file tree
Showing 8 changed files with 311 additions and 43 deletions.
32 changes: 29 additions & 3 deletions include/SFML/Window/VideoMode.hpp
Expand Up @@ -29,11 +29,31 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/Export.hpp>
#include <SFML/Graphics/Rect.hpp>

#include <vector>
#include <string>


namespace sf
{

struct SFML_WINDOW_API Screen
{
Screen() :
name(),
geometry(),
frameRate(0)
{

}

std::wstring name;
sf::FloatRect geometry; // dimension in virtual screen space coordinates
unsigned int frameRate;
};


////////////////////////////////////////////////////////////
/// \brief VideoMode defines a video mode (width, height, bpp)
///
Expand All @@ -58,15 +78,15 @@ class SFML_WINDOW_API VideoMode
/// \param modeBitsPerPixel Pixel depths in bits per pixel
///
////////////////////////////////////////////////////////////
VideoMode(unsigned int modeWidth, unsigned int modeHeight, unsigned int modeBitsPerPixel = 32);
VideoMode(unsigned int modeWidth, unsigned int modeHeight, unsigned int modeBitsPerPixel = 32, unsigned int screenID = 0);

////////////////////////////////////////////////////////////
/// \brief Get the current desktop video mode
///
/// \return Current desktop video mode
///
////////////////////////////////////////////////////////////
static VideoMode getDesktopMode();
static VideoMode getDesktopMode(unsigned int screenId = 0);

////////////////////////////////////////////////////////////
/// \brief Retrieve all the video modes supported in fullscreen mode
Expand All @@ -82,7 +102,12 @@ class SFML_WINDOW_API VideoMode
/// \return Array containing all the supported fullscreen modes
///
////////////////////////////////////////////////////////////
static const std::vector<VideoMode>& getFullscreenModes();
static const std::vector<VideoMode>& getFullscreenModes(unsigned int screenId = 0);


static const Screen getScreenInfo(unsigned int id);

static unsigned int getScreenCount();

////////////////////////////////////////////////////////////
/// \brief Tell whether or not the video mode is valid
Expand All @@ -102,6 +127,7 @@ class SFML_WINDOW_API VideoMode
unsigned int width; ///< Video mode width, in pixels
unsigned int height; ///< Video mode height, in pixels
unsigned int bitsPerPixel; ///< Video mode pixel depth, in bits per pixels
unsigned int screenId;
};

////////////////////////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions src/SFML/Window/CMakeLists.txt
Expand Up @@ -61,6 +61,7 @@ if(SFML_OS_WINDOWS)
${SRCROOT}/Win32/JoystickImpl.hpp
${SRCROOT}/Win32/SensorImpl.hpp
${SRCROOT}/Win32/SensorImpl.cpp
${SRCROOT}/Win32/VideoModeImpl.hpp
${SRCROOT}/Win32/VideoModeImpl.cpp
${SRCROOT}/Win32/WindowImplWin32.cpp
${SRCROOT}/Win32/WindowImplWin32.hpp
Expand Down
60 changes: 47 additions & 13 deletions src/SFML/Window/VideoMode.cpp
Expand Up @@ -27,6 +27,7 @@
////////////////////////////////////////////////////////////
#include <SFML/Window/VideoMode.hpp>
#include <SFML/Window/VideoModeImpl.hpp>
#include <SFML/System/Err.hpp>
#include <algorithm>
#include <functional>

Expand All @@ -37,52 +38,85 @@ namespace sf
VideoMode::VideoMode() :
width (0),
height (0),
bitsPerPixel(0)
bitsPerPixel(0),
screenId (0)
{

}


////////////////////////////////////////////////////////////
VideoMode::VideoMode(unsigned int modeWidth, unsigned int modeHeight, unsigned int modeBitsPerPixel) :
VideoMode::VideoMode(unsigned int modeWidth, unsigned int modeHeight, unsigned int modeBitsPerPixel, unsigned int screenID) :
width (modeWidth),
height (modeHeight),
bitsPerPixel(modeBitsPerPixel)
bitsPerPixel(modeBitsPerPixel),
screenId (screenID)
{

}


////////////////////////////////////////////////////////////
VideoMode VideoMode::getDesktopMode()
VideoMode VideoMode::getDesktopMode(unsigned int screenId)
{
// Directly forward to the OS-specific implementation
return priv::VideoModeImpl::getDesktopMode();
return priv::VideoModeImpl::getDesktopMode(screenId);
}


////////////////////////////////////////////////////////////
const std::vector<VideoMode>& VideoMode::getFullscreenModes()
const std::vector<VideoMode>& VideoMode::getFullscreenModes(unsigned int screenId)
{
static std::vector<VideoMode> modes;
static std::vector<std::vector<VideoMode> > modesPerScreen;

// Populate the array on first call
if (modes.empty())
if (modesPerScreen.empty())
{
modes = priv::VideoModeImpl::getFullscreenModes();
std::sort(modes.begin(), modes.end(), std::greater<VideoMode>());
modesPerScreen = priv::VideoModeImpl::getFullscreenModes();

for(int i = 0; i < modesPerScreen.size(); ++i)
{
std::sort(modesPerScreen[i].begin(), modesPerScreen[i].end(), std::greater<VideoMode>());
}
}

if (screenId < modesPerScreen.size())
{
return modesPerScreen[screenId];
}

return modes;
err() << "Fullscreen modes for screen with ID: " << screenId << " requested. No device with specified ID found." << std::endl;
return std::vector<VideoMode>(); // reference to temporary :(
}


////////////////////////////////////////////////////////////
const Screen VideoMode::getScreenInfo(unsigned int id)
{
return priv::VideoModeImpl::getScreenInfo(id);
}


////////////////////////////////////////////////////////////
unsigned int VideoMode::getScreenCount()
{
return priv::VideoModeImpl::getScreenCount();
}



////////////////////////////////////////////////////////////
bool VideoMode::isValid() const
{
const std::vector<VideoMode>& modes = getFullscreenModes();
if(screenId < getScreenCount())
{
const std::vector<VideoMode>& modes = getFullscreenModes(screenId);

return std::find(modes.begin(), modes.end(), *this) != modes.end();
}

return std::find(modes.begin(), modes.end(), *this) != modes.end();
err() << "The screen with ID " << screenId << " is not available. Please use sf::VideoMode::getScreenCount() to get the available number of screens." << std::endl;
return false;
}


Expand Down
9 changes: 7 additions & 2 deletions src/SFML/Window/VideoModeImpl.hpp
Expand Up @@ -49,15 +49,20 @@ class VideoModeImpl
/// \return Array filled with the fullscreen video modes
///
////////////////////////////////////////////////////////////
static std::vector<VideoMode> getFullscreenModes();
static std::vector<std::vector<VideoMode> > getFullscreenModes();

////////////////////////////////////////////////////////////
/// \brief Get the current desktop video mode
///
/// \return Current desktop video mode
///
////////////////////////////////////////////////////////////
static VideoMode getDesktopMode();
static VideoMode getDesktopMode(unsigned int screenId);


static Screen getScreenInfo(unsigned int id);

static unsigned int getScreenCount();
};

} // namespace priv
Expand Down

0 comments on commit 86a6eb9

Please sign in to comment.