Setting a background

We have made good progress lately. Here is the next problem to tackle.

Trying to put our forest background as the background for the rectangle we have created to move on top of.

1
2
3
4
5
sf::Texture texture;
		if (!texture.loadFromFile("Forest1.png"))
		{

		}


Note that we have saved the .png file to the correct folder.

We are trying to put that background into our progrom here:

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
67
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <Windows.h>

using namespace std;

int main()
{

	sf::RectangleShape rectangle(sf::Vector2f(150, 80));
	sf::RenderWindow window(sf::VideoMode(1000, 1000), "Moving a shape");
	window.setVerticalSyncEnabled(true);

	while (window.isOpen())
	{
		sf::Event event;
		while (window.pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
			{
				window.close();

				return 0;
			}
		}


		window.clear(sf::Color::White);

		rectangle.setFillColor(sf::Color(0, 250, 0));

		window.clear(sf::Color::Black);

		if (event.type == sf::Event::KeyPressed){
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
				rectangle.move(0, -5);
				Sleep(1);
			}
		}

		if (event.type == sf::Event::KeyPressed){
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
				rectangle.move(0, 5);
				Sleep(1);
			}
		}

		if (event.type == sf::Event::KeyPressed){
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
				rectangle.move(-5, 0);
				Sleep(1);
			}
		}

		if (event.type == sf::Event::KeyPressed){
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
				rectangle.move(5, 0);
				Sleep(1);
			}
		}

		window.draw(rectangle);
		window.display();
		
	}
}


I've tried inserting it anywhere I can think of. Can this be done without using threads? And can someone help guide me to the correct way to set the background image please?
It should be as simple as creating a sprite and setting its texture as the background then drawing that sprite before anything else.
1
2
3
4
5
6
7
8
9
10
11
sf::Texture texture;
if (!texture.loadFromFile("Forest1.png"))
{

}
sf::Sprite background(texture);
...
window.draw(background);
window.draw(recangle);
window.display();
...
Last edited on
Thanks naraku. I will try this when I get home.
thanks. now I got how to set background
Topic archived. No new replies allowed.