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);
}