Is this syntatically legal and will it do what I want?

I am in the steps of writing a game, and I'm not sure how to go about loading images. Would this be syntactically correct?
A portion of my code:
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
class loader // loads assets, functions to return pointers to the loaded assets
{
public:
	loader();
	~loader();
	sf::Texture * getLoadingScreen();
	sf::Texture * getLoadingMessage();	
	sf::Texture * getFirstLevel();
	sf::Texture * getLogo();
	sf::Texture * getCredits();
private:
	sf::Texture loadingScreen;
	sf::Texture loadingMessage;
	sf::Texture firstLevel;
	sf::Texture credits;
	sf::Texture logo;
};
loader::loader()
{
	sf::Texture loadingScreen;
	sf::Texture loadingMessage;
	sf::Texture firstLevel;
	sf::Texture credits;
	sf::Texture logo;
	loadingScreen.loadFromFile("28.png");
	loadingMessage.loadFromFile("22.png");
	firstLevel.loadFromFile("1.png");
	credits.loadFromFile(("idea-and-graphics.png");
	logo.loadFromFile("21.png");
}
sf::Texture * loader::getLoadingScreen()
{
	return &loadingScreen;
}

And then later:
1
2
3
sf::Sprite.setTexture((*instance.getLoadingScreen())) // instance is an instance of loader
...
I'm not really into pointers but as an avid SFML user I can tell you from experience that you do not want to use pointers with Textures but if you are going to do it, do it with smart pointers. This happens due to the fact that sprites are a mere pointer to the Texture so when using normal pointers its easy to mess things up.
But OOP :P... Ok, i was just using pointers & things to keep my code very OOP, I find if i do keep my code object oriented it keeps it organized. Ty for the tip.
Just read the SFML documentation... This object will be "alive" during the entire main function, so I think this will work. Ill just be careful to kill the sprite before the texture dies... Wow my c++ terminology is very morbid
I'm not saying it won't work. For experience I find that I often re-use my objects in different functions and even when I do use pointers, I use smart pointers. I'm not deferring the use of pointers, I'm merely pushing the use of smart pointers since these, as the name states are smarter and prevent memory allocation problems. If you want to use pointers, use them but please use smart pointers.
jsbrowndog2013 wrote:
But OOP :P... Ok, i was just using pointers & things to keep my code very OOP, I find if i do keep my code object oriented it keeps it organized. Ty for the tip.
In modern C++, we use pointers as little as possible. In fact, pointers have nothing to do with OOP at all.
Umm.. Polymorphism? You have to use pointers eventually. Also, to use this in a class I have to use pointers or declare the thing as public, but since I prefer a getter and setter I use a pointer here. But I see your point (pun somewhat intended)
Umm.. Polymorphism? You have to use pointers eventually.

Umm... hello references? An sf::Texture isn't meant to be derived from and has no virtual functions. Where's the need for polymorphism?

I don't see any problem with the way you're using pointers here, but your reasoning is a little off. Also, your constructor is wrong. Consider how it might look with references rather than pointers:


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
class loader // loads assets, functions to return pointers to the loaded assets
{
public:
    loader();
    ~loader();
    sf::Texture& getLoadingScreen();
    sf::Texture& getLoadingMessage();	
    sf::Texture& getFirstLevel();
    sf::Texture& getLogo();
    sf::Texture& getCredits();
private:
    sf::Texture loadingScreen;
    sf::Texture loadingMessage;
    sf::Texture firstLevel;
    sf::Texture credits;
    sf::Texture logo;
};
loader::loader()
{
//  These variables hide the actual class variables.
//  sf::Texture loadingScreen;
//  sf::Texture loadingMessage;
//  sf::Texture firstLevel;
//  sf::Texture credits;
//  sf::Texture logo;
    loadingScreen.loadFromFile("28.png");
    loadingMessage.loadFromFile("22.png");
    firstLevel.loadFromFile("1.png");
    credits.loadFromFile(("idea-and-graphics.png");
    logo.loadFromFile("21.png");
}
sf::Texture& loader::getLoadingScreen()
{
	return loadingScreen;
}


And then later:
1
2
sf::Sprite.setTexture(instance.getLoadingScreen())
...


It looks much cleaner to me.
That is cleaner & better.
Topic archived. No new replies allowed.