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

Author Topic: resolution independant movement?  (Read 3252 times)

0 Members and 1 Guest are viewing this topic.

Moderatgamer

  • Newbie
  • *
  • Posts: 3
    • View Profile
resolution independant movement?
« on: October 10, 2018, 01:43:26 am »
Hi all,

please excuse me if this is the wrong place to post such a question, this is my first post. I have recently started to learn c++ with SFML just messing around really and I have hit a problem I can't seem to fix. Basically if I set my movement speed to say for example 1 when I change the window size the game either speeds up or slows down as the move function directly related to pixels. I wonder if anyone has a solution for me to make the speed constant regardless of resolution. I've attached a small part of code that I was using to test endless scrolling background it's very simple I will be padding it out more and incorporating an array of textures that will be semi randomly picked but for now I was just testing. Appreciate any help you can give me.


#include "paralax_background.h"

paralax::paralax(sf::String texture_1_src, sf::String texture_2_src)
{
        if (!texture_1.loadFromFile(texture_1_src))
        {
                // error...
                std::cout << "loading failed" << std::endl;
        }
        back_1.setTexture(texture_1);
        back_1.setOrigin(sf::Vector2f(back_1.getScale().x / 2, back_1.getScale().y / 2));

        if (!texture_2.loadFromFile(texture_2_src))
        {
                // error...
                std::cout << "loading failed" << std::endl;
        }
        back_2.setTexture(texture_2);
        back_2.setOrigin(sf::Vector2f(back_2.getScale().x / 2, back_2.getScale().y / 2));
        back_2.setPosition(1000, 0); // TODO change to windwo width// width of image/ window !!
}

void paralax::scroll(sf::RenderWindow & window) //TODO make for loop that loops through array of backgrounds
{
       
        speed = 0.2f;


                back_1.move(-speed, 0);

                if (back_1.getPosition().x <= -1000) {
                        back_1.setPosition(0 + back_1.getLocalBounds().width , 0);
                        //back_2.setPosition(back_2.getLocalBounds().width * 2, 0);
                }

                back_2.move(-speed, 0);

                if (back_2.getPosition().x <= -1000) {
                        back_2.setPosition(0 + back_2.getLocalBounds().width, 0);
                }
       
}

void paralax::Draw(sf::RenderWindow & window)
{

        window.draw(back_1);
        window.draw(back_2);

}

 

Antonio9227

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: resolution independant movement?
« Reply #1 on: October 10, 2018, 02:01:30 pm »
First make sure you either set vsync on or set a framerate limit such as 60fps.
Code: [Select]
window.setFramerateLimit(60);

If the window is smaller (and thus the resolution) it will render much faster because overall your GPU has less pixels to deal with. (this is just a hunch, I haven't tested it)

You should also think about using views and view ports. This SFML tutorial will explain better than I ever could in a short post.
« Last Edit: October 10, 2018, 02:03:36 pm by Antonio9227 »

Moderatgamer

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: resolution independant movement?
« Reply #2 on: October 10, 2018, 06:15:34 pm »
Thanks for the reply appreciate it. I have set up a rudimentary frame limiter just capped it at 60 didn't use the built in feature just used a clock and timer to only draw every 60th of a second. I also just started using the view last night which unfortunately didn't help either.

the problem is that when you say move 1 it literally means 1 pixel so when I resize the window 1 pixel is either more or less than the original distance causing the speed variation. Do you know any way to do something similar to resolution scaling where I can set the internal resolution and it will just stretch the image to the window while remaining at the internal resolution?

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: resolution independant movement?
« Reply #3 on: October 11, 2018, 09:19:26 am »
That is exactly the usecase for sf::View, have a look here for an nice explanation .

Set the view at the beginning of the game to some hard coded value, lets say 800x600. Now the "internal resolution" will be exactly 800x600, the view will then map during rendering your values between 0-800 to 0-Window.getSize().x and the same for y.

When the window changes its size do not change the view. However keep in mind that this will stretch your game based on the aspect ratio of the window. Depending on your game you might want to show some black bars to avoid the stretching or allow the user to see further in one axis.


AlexAUT

Moderatgamer

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: resolution independant movement?
« Reply #4 on: October 13, 2018, 01:01:19 am »
Thanks I'll need to give the documentation a good read appreciate the help :)

 

anything