SFML C++ question

hey im building a game in sfml/c++
im trying to make void that draws circle in 4 parts
example of a box in 4 parts:
1. |

2. |_

3. |_|

4.
__
|_|

this is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	void drawCircle(sf::RenderWindow & window, int x , int y)
	{
		sf::Texture texture;
		texture.loadFromFile("gamefiles//O.png");
		sf::Sprite circle;
		sf::Clock repeat;
		circle.setPosition(x, y);
		circle.setTexture(texture);
		circle.setTextureRect(sf::IntRect(0, 0, 176, 141));
		int step = 0;
		while (step != 5)
		{
			if (repeat.getElapsedTime().asSeconds() > 0.1)
			{
				circle.setTextureRect(sf::IntRect(0 + (178 * step), 0, 176, 141));
				step++;
				repeat.restart();
			}
			window.draw(circle);
			window.display();
		}
	}


now i have 2 problems:
1.when im doing window.display() on my function void and not on my main void the rest of the code that i wrote in the main void is flashing on a off
2.after the action is done the circle it self deleted at all i know why it happens i just dont have solution

please help me ASAP tnx !
Last edited on
You need to read up about what window.display actually does. You only want to display once, at the very end of your program.

If we take your box as an example. You need to draw all 4 parts first. Then do window.display();
Last edited on
@TarikNeaj
i know what it does, i read about it(at least i think )
but when i do window.display(); at the end of the main void and call window.draw(circle); on the function it self
it does not draw anything at all
edit:
and i cant draw all 4 parts before i display. cause the idea is the draw every time single part of the circle
so it will looks like 1/4 circle , then 1/2 ,then 3/4, then 1 completed circle.
i try to make it looks like someone draw the circle and not just display circle
Last edited on
You don't want to load your textures and draw to a sprite every frame. Load your texture once, what you're doing right now is horribly inefficient. If you want to create your sprite in the draw function, that's probably ok as sprites are very cheap in comparison to textures.

Just always remember, in relation to each other...

Textures: Expensive
Sprites: Not expensive

For drawing different quadrants of the sprite, I wouldn't use hard-coded pixel positions and sizes. Calculate the positioning and sizing based on the size of the texture you load. This makes your program flexible, able to be changed without having to change the code and recompile.

Don't call display() in your draw functions. Display is supposed to be called after you've finished drawing everything for that frame.
@Austin J
first of all tnx for your reply.
second i understand what u say so i will put add this code to my class cause the void you see in the old code "drawCircle" is part of class i created
1
2
3
4
5
6
7
 
public:
     Shape(){ //my class name
      Otexture.loadfromfile("gamefiles//O.png");
}
private: 
           sf::texture Otexture


last thing after i solve the texture problem.
how i suppose to make the function display each part of the circle after certain time and then go back to the main void and not each time it display a part?
if u have solution to my problem please help
Last edited on
Sorry for the late response.

For controlling how it is drawn, you can use something similar to what you have. Your shape could keep its own instance of a clock, and keep track of time that way. You can reset the clock when you need to, and read its time to determine how to draw your circle.
Topic archived. No new replies allowed.