Hi,
I'm having a few issues with windows using sf::Style::None to implement something like a splash screen.
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!", sf::Style::None);
// center the window
sf::Vector2i splashOrig = {
int(sf::VideoMode::getDesktopMode().width) / 2 - int(window.getSize().x) / 2,
int(sf::VideoMode::getDesktopMode().height) / 2 - int(window.getSize().y) / 2
};
window.setPosition(splashOrig);
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
bool dragging { false };
sf::Vector2i dragAnchor;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::MouseButtonPressed)
{
if (event.mouseButton.button == sf::Mouse::Left)
{
dragging = true;
dragAnchor = sf::Mouse::getPosition();
}
}
if (event.type == sf::Event::MouseButtonReleased)
{
if (event.mouseButton.button == sf::Mouse::Left)
{
dragging = false;
dragAnchor = {};
}
}
if (dragging)
{
auto currentPos = sf::Mouse::getPosition();
auto deltaPos = dragAnchor - currentPos;
window.setPosition(window.getPosition() - deltaPos);
dragAnchor = sf::Mouse::getPosition();
}
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
1. I want my window to be positioned at the center of my main monitor on startup, but since I have 2 monitors the getDesktopMode() gives an area which is the sum of those 2 and the window ends up astride both. It kind of makes sense, but how can I set up "splashOrig" so that my window appears at the center of my main, or current, or any screen?
2. I'm trying to make the window movable by drag-n-dropping as you can see. It kind of works, but the anchor point I set when left-clicking isn't stuck to the cursor... the faster I move, the more it "drags behind".
I've already implemented an Image viewer before in SFML and I know you need mapPixelToCoords/mapCoordsToPixel transformations in some cases... so what am I missing?
PS: I'm aware using sf::Style::None and tweaking it so it behaves like an sf::Style::Default is a bit odd... I'm just testing the limits of the thing. If it's more convenient I'll go with sf::Style::Titlebar in order to move my splash screen around.