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

Author Topic: Handling different window resolutions + positions of sprites.  (Read 3720 times)

0 Members and 1 Guest are viewing this topic.

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
       Well I was programming the other day, and was wondering. If someone made the window bigger, or changed its size, it may throw off the positions of the sprites, ie, no longer symmetrical to the window.  For this I was wondering if there was a way to handle this, or if I would just have to write a bunch of code to handle it.

        Has anyone else had prior experience with this? I'm new to drawing sprites so I honestly have no clue. x3 I was thinking of making some massive file to be able to position them via percentages, but am worried that, that may make the program take longer to draw, or that it would be nearly impossible to cover every possible percentage.

         Any ideas? Perhaps make it that the window itself can't be resized and make resizing only a option via menu that updates variables for the positions of the sprites? That seems like the easiest way so far, but I don't want to go cheap if I don't have to. xD

         Anyway I look forward to hearing from you! :D

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Handling different window resolutions + positions of sprites.
« Reply #1 on: June 20, 2012, 06:14:06 pm »
The position of the sprite won't change, the only thing that does change are the properties of the window and its connected view, which will stretch the sprites.

A quick search on 'resize' would've probably answered your question (hint: this link leads you to the search function). ;)

I myself use something like this to adjust the window size and to preserve a minimum size:
if(event.type == sf::Event::Resized)
{
        sf::Vector2f size = static_cast<sf::Vector2f>(mWindow.getSize());

        // Minimum size
        if(size.x < 800)
                size.x = 800;
        if(size.y < 600)
                size.y = 600;

        view.setCenter(size/2.f);
        view.setSize(size);
        window.setSize(static_cast<sf::Vector2<unsigned int> >(size));
        window.setView(sf::View(sf::FloatRect(0.f, 0.f, size.x, size.y)));
}
« Last Edit: June 20, 2012, 06:16:28 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Flash619

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Handling different window resolutions + positions of sprites.
« Reply #2 on: June 20, 2012, 10:41:57 pm »
The position of the sprite won't change, the only thing that does change are the properties of the window and its connected view, which will stretch the sprites.

A quick search on 'resize' would've probably answered your question (hint: this link leads you to the search function). ;)

I myself use something like this to adjust the window size and to preserve a minimum size:
if(event.type == sf::Event::Resized)
{
        sf::Vector2f size = static_cast<sf::Vector2f>(mWindow.getSize());

        // Minimum size
        if(size.x < 800)
                size.x = 800;
        if(size.y < 600)
                size.y = 600;

        view.setCenter(size/2.f);
        view.setSize(size);
        window.setSize(static_cast<sf::Vector2<unsigned int> >(size));
        window.setView(sf::View(sf::FloatRect(0.f, 0.f, size.x, size.y)));
}

You know out of all the times I used the search function, I never once thought of using it for this... -_-

You mentioned it would stretch the sprites, Thought, I suppose you could check the window size and if its resized below or above certain points, you could probably load a more high res image as the sprite? Or is there a simpler way. I just think using the normal resize might take a bit more cpu and have less quality than loading multiple images but I may be wrong, I have been before.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Handling different window resolutions + positions of sprites.
« Reply #3 on: June 21, 2012, 03:44:48 am »
You mentioned it would stretch the sprites, Thought, I suppose you could check the window size and if its resized below or above certain points, you could probably load a more high res image as the sprite? Or is there a simpler way. I just think using the normal resize might take a bit more cpu and have less quality than loading multiple images but I may be wrong, I have been before.

You got me wrong I guess...

What my code does is, when ever you resize the window the resize event gets triggered and the view gets again fitted to the window size. This means your sprites get displayed again 1:1.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything