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

Author Topic: [SOLVED] Proper way to handle Resize  (Read 19819 times)

0 Members and 1 Guest are viewing this topic.

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
[SOLVED] Proper way to handle Resize
« on: March 20, 2015, 09:04:19 am »
Hi, I'm working with window resize events in order to realign the user interface depending on the window's size. Therefore I discovered RenderWindow::mapPixelToCoords() in order to align the position correctly. But unfortunately the dimension of the interface components (e.g. a CircleShape) is stretched.

So I started handling my "own default view" by resizing it, so the shape isn't streched anymore. But I'm not sure whether this is the proper way to handle window resize or not. Any suggestions? Here's a minimal example code:

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

int main() {
        sf::RenderWindow window{{640, 480}, "Resize Test"};
        sf::CircleShape shape{50.f};
        shape.setOrigin({50.f, 50.f});
       
        // create own view
        sf::View view = window.getDefaultView();
       
        while (window.isOpen()) {
                sf::Event event;
                while (window.pollEvent(event)) {
                        if (event.type == sf::Event::Closed) {
                                window.close();
                        } else if (event.type == sf::Event::Resized) {
                                // resize my view
                                view.setSize({
                                        static_cast<float>(event.size.width),
                                        static_cast<float>(event.size.height)
                                });
                                window.setView(view);
                                // and align shape
                                shape.setPosition(window.mapPixelToCoords(sf::Vector2i{window.getSize() / 2u}));
                        }
                }
               
                window.clear();
                window.draw(shape);
                window.display();
        }
}
 

Kind regards
Glocke
« Last Edit: March 23, 2015, 08:22:02 am by Glocke »
Current project: Racod's Lair - Rogue-inspired Coop Action RPG

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
Re: Proper way to handle Resize
« Reply #2 on: March 20, 2015, 12:50:07 pm »
http://www.sfml-dev.org/tutorials/2.2/graphics-view.php#showing-more-when-the-window-is-resized
Ok thx. But I'm switching between two views: hud/gui and my game scene. Each time a resize event is received, I notify the game state to resize the view, which is used for drawing the game scene. So I'd need to handle my hud/gui view as well (resize it) in order to be able to reapply it when drawing ui elements.

So I'd need to store either my hud/gui view or the visible area of this view (which doesn't make a real difference). I assume this is the correct and "official" way, because it's implied by the tutorial link.

Again, thanks for the link ... :)
Current project: Racod's Lair - Rogue-inspired Coop Action RPG

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Proper way to handle Resize
« Reply #3 on: March 20, 2015, 01:16:20 pm »
There is no "correct" way, just as long as you don't do it a "wrong" way.  ;)

The tutorials just show one way of doing it.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
Re: Proper way to handle Resize
« Reply #4 on: March 20, 2015, 02:44:32 pm »
There is no "correct" way, just as long as you don't do it a "wrong" way.  ;)
Well, but there's an intended way from the point of the dev team :) (Seems to be the way to tutorial shows).

Which alternatives are there?
« Last Edit: March 20, 2015, 02:56:28 pm by Glocke »
Current project: Racod's Lair - Rogue-inspired Coop Action RPG

BlueCobold

  • Full Member
  • ***
  • Posts: 105
    • View Profile
Re: Proper way to handle Resize
« Reply #5 on: March 21, 2015, 08:13:37 pm »
An alternative would be to ignore the events altogether and calculate your view and layout positions based on the current size of the used RenderWindow each frame - cached if you want, so you only do it if required. That way you don't need to pass down events. You have to pass down the RenderTarget anyway though.

 

anything