Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Splash screen  (Read 5663 times)

0 Members and 1 Guest are viewing this topic.

cob59

  • Newbie
  • *
  • Posts: 21
    • View Profile
Splash screen
« on: July 22, 2020, 02:46:07 pm »
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.
« Last Edit: July 22, 2020, 03:06:59 pm by cob59 »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Splash screen
« Reply #1 on: July 22, 2020, 09:20:50 pm »
1) There's no way - currently* - to find out the resolution of a single display.
Getting the desktop mode gives you information about the desktop, the entire thing the operating system uses.
So, centring on a single display isn't easy (or possible directly). You could use OS-specific commands to move it into place if it's absolutely essential.

2) It's likely to drag behind the mouse. The mouse is moved immediately by the operating system. The window is being moved by the program that first requires events to be sent by the operating system (whenever it wants to) and then to actually process those and make a request to the operating system to move the window.
Note that you shouldn't need co-ordinate mapping here since you aren't using any views. They're all pixel co-ordinates.

"PS") Making a draggable, borderless window seems to be something that people do at some point. I, myself, made one as an example and posted it on this forum. Somewhere... Ah, here.


* This was brought up recently so may be included in the future.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

cob59

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Splash screen
« Reply #2 on: July 23, 2020, 05:32:01 pm »
Hey, your example works way better than mine! My issue wasn't about a lag between the cursor movement and the window, but really a "bias" in the window movement going half as far as my cursor... Anyway, thanks.

Ok about the first point...
What's strange is that if I replace sf::Style::None by sf::Style::Default and run your example, the program opens a window in the current display (the one I launched the program from) at a (20,20) top-left coordinate or so. BUT if I run it as-is, that's when the borderless window appears astride both screens...

But yes, overall it would be nice to have some extra info about sf::VideoMode::getDesktopMode(), something like a std::vector<sf::IntRect> matching each screen # to an area in the desktop VideoMode... I bet there's a lot of portability issues I don't see though.
« Last Edit: July 23, 2020, 06:19:35 pm by cob59 »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Splash screen
« Reply #3 on: July 23, 2020, 10:00:38 pm »
No problem :)

My issue wasn't about a lag between the cursor movement and the window, but really a "bias" in the window movement going half as far as my cursor.
Interesting.
Did you try my example here:
https://en.sfml-dev.org/forums/index.php?topic=14391.msg101187#msg101187
and are you still getting the same problems?

If so, all I can think of right now is it could be related to DPI.

What's strange is that if I replace sf::Style::None by sf::Style::Default and run your example, the program opens a window in the current display (the one I launched the program from) at a (20,20) top-left coordinate or so. BUT if I run it as-is, that's when the borderless window appears astride both screens...
This is strange. So, if the style is default, it centres on the main display but if style is none, it centres on the entire desktop?

I would say that this is unintended.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

cob59

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Splash screen
« Reply #4 on: July 24, 2020, 10:39:53 am »
Did you try my example here:
Yeah as I said I tried your example and it fixed my problem. No more an issue, thanks!

This is strange. So, if the style is default, it centres on the main display but if style is none, it centres on the entire desktop?

I would say that this is unintended.

It does. Well... it centers horizontally but vertically it sticks to the bottom of the screen.
I'm trying this on 2 setups :

1. An Ubuntu 19.04 with 2 monitors (both 1920x1080)
In Style::None mode, the window always appears so that a) it's 50% on the left screen, 50% on the right one and b) its bottom edge is touching the bottom of the screen, whatever the window size is
(edit: Could it be because of Gnome's workspaces, somehow "extending" the desktop below?)

2. A WIndows 10 with 2 monitors (3440x1440 and 2560x1440)
And no issue so far with either sf::Style
« Last Edit: July 24, 2020, 12:50:44 pm by cob59 »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Splash screen
« Reply #5 on: July 27, 2020, 11:48:41 pm »
Unfortunately, I can't comment on its behaviour Ubuntu, sorry.

2. A WIndows 10 with 2 monitors (3440x1440 and 2560x1440)
And no issue so far with either sf::Style
Could you clarify what does actually happen here. I presume both styles work the same but do they centre on the display or the desktop?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

cob59

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Splash screen
« Reply #6 on: July 30, 2020, 01:08:42 am »
Could you clarify what does actually happen here. I presume both styles work the same but do they centre on the display or the desktop?

On Windows, both styles appear centered on my main display (the 3440x1440 one).