Scopes are mean.

So when I run my program, the texture it drew was white. I googled around andfound out that it had to do something with a scope. Can anyone tell me why this is happening and how I can fix it? Thanks a bunch :) (The texture in question is the PlayButtonTxtr.png. I'm not using any other texture. (SFML 2)

Menu.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
#include <SFML\Graphics.hpp>
#include <SFML\Audio.hpp>

#include "Menu.h"
#include "Button.h"

class Button PlayButton;
class Button ExitButton;

void MenuInit()
{
	sf::Texture ButtonPlayTxtr;
	sf::Texture ButtonExitTxtr;

	sf::SoundBuffer ButtonClickBuff;

	ButtonPlayTxtr.loadFromFile("assets//ButtonPlayTxtr.png");
	ButtonExitTxtr.loadFromFile("assets//ButtonExitTxtr.png");

	ButtonClickBuff.loadFromFile("assets//ButtonClickBuff.ogg");

	
	PlayButton.Init(200, 200, true, ButtonPlayTxtr, ButtonClickBuff);
	
	ExitButton.Init(200, 400, true, ButtonExitTxtr, ButtonClickBuff);


}

void MenuMain(sf::RenderWindow& Window)
{
	


	Window.draw(PlayButton.sprite);
}


Last edited on
Tell us, which OS do you use.
I use Windows 7.
In that case directory separator will be '\' symbol ("\\" inside string literals), an in your include statements, not '/' as in your load functions.
That's not the problem, Mii. I've been using '//' forever now, and it's always worked. I'm certain it has to be the different scope. At least, that's what I think you meant. Thanks though!
Last edited on
Try to use two backslasher or single forward slash. Post what happened.
I hink that the problem s that the following call

ButtonPlayTxtr.loadFromFile("assets//ButtonPlayTxtr.png");

was not successful.

You should check the return value of the call to be sure that it was successful

Try to change string literal

"assets//ButtonPlayTxtr.png"

to

"assets\\ButtonPlayTxtr.png"
Last edited on
It is still white. I know it's not that - i've been using that way for a long time now.

These links make me think it's the scope:

http://en.sfml-dev.org/forums/index.php?topic=8961.0

http://facepunch.com/showthread.php?t=1159440

Look at the replies - they're all about scopes, and my loadFile is not in the same scope as the Window.draw.

Of course if you think it's something else I'll try it. I just have no idea how i'd fix it.

Thanks a bunch!
After some research I found, that SFML allows you to use single forward slashes as delimeteres. Tell if you tried to use single slashes.

There is no scope problems because you storing references to sprites in global variable.

Check if loadFromFile function call was successful.
It was! Here's a comparson between the two.

http://imgur.com/ZGAQt8z,7Jomab1#0

Yes, I've tried using single slashes. if you need any more info, please ask. i'm still changing things trying to find out what's going on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void MenuInit()
{
	sf::Texture* ButtonPlayTxtr = new sf::Texture;
	sf::Texture* ButtonExitTxtr = new sf::Texture;

	sf::SoundBuffer* ButtonClickBuff = new sf::SoundBuffer;

	ButtonPlayTxtr -> loadFromFile("assets//ButtonPlayTxtr.png");
	ButtonExitTxtr -> loadFromFile("assets//ButtonExitTxtr.png");

	ButtonClickBuff -> loadFromFile("assets//ButtonClickBuff.ogg");

	
	PlayButton.Init(200, 200, true, *ButtonPlayTxtr, *ButtonClickBuff);
	
	ExitButton.Init(200, 400, true, *ButtonExitTxtr, *ButtonClickBuff);


}

Try this.
Haha, it worked!!Thanks a bunch!

What did that do?? That's awesome!
What happened is that I am a blind idiot and you was right from the beginning.

I created ButtonPlayTxtr, etc. in heap instead of stack, so they will not be deleted after function finishes executing. Note that there is memory leak if you are not deleting those vatiables somewhere else.
So.. The way I was doing it, the texture was being deleted after Init was called?

And your way keeps its memory allocated until I say otherwise?
Thank you so much :) I'll remember this in the future!
Topic archived. No new replies allowed.