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

Author Topic: Workaround for resizing window while dragging it  (Read 655 times)

0 Members and 1 Guest are viewing this topic.

Sic

  • Newbie
  • *
  • Posts: 2
    • View Profile
Workaround for resizing window while dragging it
« on: September 03, 2023, 08:02:21 pm »
Hello,

SFML does not allow you to modify the window when it is drag by the user, which is extremely annoying.
I'm currently trying to find a workaround but I'm having a lot of trouble, there are errors, OpenGL doesn't seem to like what I'm doing and everything doesn't work as expected...

Could you help me find a workaround for this problem ? Thanks in advance.

Here is my code :

#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <Windows.h>

void fonct(sf::RenderWindow * window) {
        sf::Texture texture;
        if (!texture.loadFromFile("Vernet-Battle_of_Hanau.jpg")) std::cout << "erreur" << std::endl;
        sf::Sprite sprite;
        sprite.setTexture(texture);
       
        while (window->isOpen()) {
               
                RECT rect;
                GetWindowRect(window->getSystemHandle(), &rect);
               
                int taille_x = rect.right - rect.left - (GetSystemMetrics(SM_CXMAXIMIZED) - GetSystemMetrics(SM_CXFULLSCREEN));
                int taille_y = rect.bottom - rect.top - (GetSystemMetrics(SM_CYMAXIMIZED) - GetSystemMetrics(SM_CYFULLSCREEN));
               
                sf::FloatRect visibleArea(0.f, 0.f, taille_x, taille_y);
        window->setView(sf::View(visibleArea));
               
                window->draw(sprite);
                window->display();
        }
}

int main() {
       
        sf::Mutex mutex;
        sf::Lock lock(mutex);
       
        sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
        window.setActive(false);
       
        sf::Thread thread(&fonct, &window);
        thread.launch();
       
        while (window.isOpen()) {
                sf::Event event;
                while (window.pollEvent(event)) {
                       
                        if (event.type == sf::Event::Closed) {
                                window.close();
                        }
                       
                        if (event.type == sf::Event::Resized) {
                                sf::FloatRect visibleArea(0.f, 0.f, event.size.width, event.size.height);
                                window.setView(sf::View(visibleArea));
                        }
                       
                }
        }
       
        thread.wait();
}

Hapax

  • Hero Member
  • *****
  • Posts: 3360
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Workaround for resizing window while dragging it
« Reply #1 on: September 06, 2023, 05:37:44 pm »
If I remember correctly, an sf::RenderWindow isn't guaranteed to be fully thread-safe so just be aware of how both threads are using it simultaneously.

Also, this issue has been mentioned before (a while back) and I can't remember if there came a solution. However, if this issue actually bothers you (it's bothered most for a while and then you get used it being there unless it's important for the app; e.g. most games don't normally care to update if the user is messing with its window), the best solution will likely to use some other specific window library (Qt is one, I believe) that has more advanced window control features. You should still be able to use the rest of SFML within those windows though so maybe that's a thought.

Can I also ask why, in your example, you aren't using the window-specific code to paste the rectangle back onto the window? Note that SFML's display does some behind-the-scenes stuff. Possibly most notable is that it swaps the buffer (a window is double-buffered). Could this have something to so with why you aren't getting the result you expect from your code?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10846
    • View Profile
    • development blog
    • Email
Re: Workaround for resizing window while dragging it
« Reply #2 on: September 07, 2023, 08:58:27 pm »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Sic

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Workaround for resizing window while dragging it
« Reply #3 on: September 08, 2023, 07:05:00 pm »
If I remember correctly, an sf::RenderWindow isn't guaranteed to be fully thread-safe so just be aware of how both threads are using it simultaneously.
Yes, that's part of my problem indeed, I have quite a hard time managing that.

Can I also ask why, in your example, you aren't using the window-specific code to paste the rectangle back onto the window? Note that SFML's display does some behind-the-scenes stuff. Possibly most notable is that it swaps the buffer (a window is double-buffered). Could this have something to so with why you aren't getting the result you expect from your code?
If you are talking about the clear() function, that doesn't change anything in my problem, by default I remove it because it helps me in certain cases but even if I put it back the problem is still the same.

There's a very complicated Windows-only solution posted on the SFML wiki.
This looks interesting thank you, however the code generates several errors because certain types are incompatible with each other in the code.

 

anything