hey, i wanted to add a scrolling background to my game and now i have an issue with the background, as the title says
(the jpg files are 800x600 just like the window)
i show you my code and screenshots so you can see whats going wrong
#ifndef GUIMOVABLEBACKGROUNDS_H
#define GUIMOVABLEBACKGROUNDS_H
#include <SFML\Graphics.hpp>
# include <iostream>
class MovableBackground
{
public:
MovableBackground();
void Update(sf::RenderWindow &window, float elapsedTime);
void Render(sf::RenderWindow &window);
private:
sf::Texture bg1Tex;
sf::Texture bg2Tex;
sf::Sprite bg1Sprite;
sf::Sprite bg2Sprite;
float bgSpeed;
float bg1Y;
float bg2Y;
};
#endif
GUImovableBackground.cpp
#include "GUImovableBackground.h"
MovableBackground::MovableBackground()
{
bgSpeed = 0.1;
bg1Tex.loadFromFile("graphics//background.jpg");
bg1Tex.setSmooth(false);
bg1Sprite.setTexture(bg1Tex);
bg1Y = bg1Sprite.getPosition().y;
bg2Tex.loadFromFile("graphics//background.jpg");
bg2Tex.setSmooth(false);
bg2Sprite.setTexture(bg2Tex);
bg2Sprite.setPosition(0, -799);
bg2Y = bg2Sprite.getPosition().y;
}
void MovableBackground::Update(sf::RenderWindow &window, float elapsedTime)
{
if (bg1Y >= window.getSize().y)
{
bg1Y = - 799;
}else
{
bg1Y += bgSpeed * elapsedTime;
}
if (bg2Y >= window.getSize().y)
{
bg2Y = - 799;
}else
{
bg2Y += bgSpeed * elapsedTime;
}
if (bg1Y == -799)
{
std::cout << bg1Y << "\t bg1" << std::endl;
}
if (bg2Y == -799)
{
std::cout << bg2Y << "\t bg2" << std::endl;
}
bg1Sprite.setPosition(0, bg1Y);
bg2Sprite.setPosition(0, bg2Y);
}
void MovableBackground::Render(sf::RenderWindow &window)
{
window.draw(bg1Sprite);
window.draw(bg2Sprite);
the first screenshot:
the second screenshot:
As you can see, one time it nearly works, the other time the distance is just too high :/
and this goes on all the time... when background1 is set to - 799 the little gap is there. When background2 is set to - 799 the huge gap is there.
Maybe the mistake i made is simple, but I just dont see it :S
(if you need more code please tell me)