Skip to content

Instantly share code, notes, and snippets.

@binary1248
Created August 17, 2014 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binary1248/175b7fd50c0fc8738c06 to your computer and use it in GitHub Desktop.
Save binary1248/175b7fd50c0fc8738c06 to your computer and use it in GitHub Desktop.
DPI Scaling Test
////////////////////////////////////////////////////////////////////////////////
// README
//
// This is a test program ment to display a DPI scaling bug in SFML that can be
// found by running on windows 8.1, surface pro 2, 256gb.
//
// Without compatibility settings set, the window appears larger than it should
// and on fullscreen mode the rectangle appears incorrectly positioned and
// sized.
//
// With compatibility settings set, everything appears the way it should.
//
// The data values representing size and position of all important values
// are the same with or without compatibility settings showing a possible bug.
//
// To change the compatibility settings, go to the location of the program
// executable and right click, select properties, compatibility settings, and
// then click the checkbox that says "Disable display scaling on high DPI
// settings".
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Includes
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <SFML\Graphics.hpp>
#include <windows.h>
////////////////////////////////////////////////////////////////////////////////
// Global vars
////////////////////////////////////////////////////////////////////////////////
#define VIEW_WIDTH 480 // Default view width
#define VIEW_HEIGHT 320 // Default view height
#define WINDOW_WIDTH 800 // Default window width
#define WINDOW_HEIGHT 600 // Default window height
sf::RenderWindow window; // The window
bool fullscreen; // State of fullscreen mode
sf::RectangleShape rect; // The rectangle that is used as reference
////////////////////////////////////////////////////////////////////////////////
// Prints all of the information nessicary to show there is a problem of
// incorrect values:
//
// - Size of desktop
// - Size of window
// - Size of view
// - Position of view
// - Size of rectangle
// - Position of rectangle
////////////////////////////////////////////////////////////////////////////////
void printInfo()
{
sf::View view = window.getView();
std::cout << "\nFullscreen State: " << fullscreen << "\n";
std::cout << "Desktop Size: " <<
sf::VideoMode::getDesktopMode().width << ", " <<
sf::VideoMode::getDesktopMode().height << "\n";
std::cout << "Window Size: " << window.getSize().x << ", " <<
window.getSize().y << "\n";
std::cout << "View Size: " << view.getSize().x <<
", " << view.getSize().y << "\n";
std::cout << "View Position: " << view.getCenter().x
- view.getSize().x / 2 << ", " << view.getCenter().y -
view.getSize().y / 2 << "\n";
std::cout << "Rect Size: " << rect.getSize().x << ", " <<
rect.getSize().y << "\n";
std::cout << "Rect Pos: " << rect.getPosition().x << ", " <<
rect.getPosition().y << "\n";
}
////////////////////////////////////////////////////////////////////////////////
// Entry point
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
////////////////////////////////////////////////////////////////////////////
// Setup the window, view, and rectangle, and print info
////////////////////////////////////////////////////////////////////////////
HINSTANCE user32Dll = LoadLibrary("user32.dll");
if (user32Dll)
{
typedef BOOL WINAPI (*SetProcessDPIAwareFuncType)(void);
SetProcessDPIAwareFuncType SetProcessDPIAwareFunc = GetProcAddress(user32Dll, "SetProcessDPIAware");
if (SetProcessDPIAwareFunc)
{
std::cout << "SetProcessDPIAware() available on this system, calling now.\n";
if (SetProcessDPIAwareFunc())
{
std::cout << "SetProcessDPIAware() call succeeded.\n";
}
else
{
std::cout << "SetProcessDPIAware() call failed.\n";
}
}
else
{
std::cout << "SetProcessDPIAware() unavailable on this system.\n";
}
FreeLibrary(user32Dll);
user32Dll = NULL;
}
else
{
std::cout << "Could not open user32.dll.\n";
}
window.create(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "DPI Test",
sf::Style::Default, sf::ContextSettings(0U, 0U, 4));
window.setView(sf::View(sf::FloatRect(0, 0, VIEW_WIDTH, VIEW_HEIGHT)));
rect.setFillColor(sf::Color::Blue);
rect.setSize(sf::Vector2f(VIEW_WIDTH / 2, VIEW_HEIGHT / 2));
printInfo();
////////////////////////////////////////////////////////////////////////////
// Main loop
////////////////////////////////////////////////////////////////////////////
while (window.isOpen())
{
////////////////////////////////////////////////////////////////////////
// Process events
////////////////////////////////////////////////////////////////////////
sf::Event event;
while (window.pollEvent(event))
{
////////////////////////////////////////////////////////////////////
// Close window when closed
////////////////////////////////////////////////////////////////////
if (event.type == sf::Event::Closed)
{
window.close();
}
////////////////////////////////////////////////////////////////////
// Toggle fullscreen mode when F11 key is pressed to first supported
// video mode, and print info
////////////////////////////////////////////////////////////////////
else if (event.type == sf::Event::KeyPressed &&
event.key.code == sf::Keyboard::F11)
{
if (!fullscreen)
{
window.create(sf::VideoMode::getFullscreenModes()[0],
"Game Engine", sf::Style::Fullscreen);
window.setView(sf::View(sf::FloatRect(0, 0, VIEW_WIDTH,
VIEW_HEIGHT)));
fullscreen = true;
}
else
{
window.create(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT),
"Game Engine");
window.setView(sf::View(sf::FloatRect(0, 0, VIEW_WIDTH,
VIEW_HEIGHT)));
fullscreen = false;
}
printInfo();
}
}
////////////////////////////////////////////////////////////////////////
// Clear, draw and display window
////////////////////////////////////////////////////////////////////////
window.clear();
window.draw(rect);
window.display();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment