-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.cpp
66 lines (56 loc) · 1.59 KB
/
background.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "config.hpp"
#include "background.hpp"
Background::Background()
{
}
bool Background::init(std::string backgroundPath, bool retry)
{
if(!retry)
{
if (!_texture.loadFromFile(IMAGES_PATH + backgroundPath))
{
return false;
}
else
{
_sprite1.setTexture(_texture);
_sprite2.setTexture(_texture);
_sprite1.setPosition(0, 0);
_sprite2.setPosition(_sprite2.getGlobalBounds().width, 0);
}
}
else
{
_sprite1.setPosition(0, 0);
_sprite2.setPosition(_sprite2.getGlobalBounds().width, 0);
}
return true;
}
void Background::setPosY(int Y)
{
_sprite1.setPosition(_sprite1.getPosition().x, Y);
_sprite2.setPosition(_sprite2.getPosition().x, Y);
}
void Background::setSpeedX(int speedX)
{
_speedX = speedX;
}
void Background::update(sf::Time elapsedTime)
{
_elapsedTime += elapsedTime;
if(_elapsedTime > sf::milliseconds(20))
{
_sprite1.move(-_speedX, 0);
_sprite2.move(-_speedX, 0);
if(_sprite1.getPosition().x + _sprite1.getGlobalBounds().width <= 0)
_sprite1.setPosition(_sprite2.getPosition().x + _sprite2.getGlobalBounds().width, _sprite1.getPosition().y);
if(_sprite2.getPosition().x + _sprite2.getGlobalBounds().width <= 0)
_sprite2.setPosition(_sprite1.getPosition().x + _sprite1.getGlobalBounds().width, _sprite2.getPosition().y);
_elapsedTime = _elapsedTime.Zero;
}
}
void Background::draw(sf::RenderWindow &window)
{
window.draw(_sprite1);
window.draw(_sprite2);
}