SFML, can't figure out how to store a texture in a class

I'm trying to get my player class to hold its texture and sprite, but in this test I'm just doing texture, and it isn't working, some code has been commented out, you can just ignore it.

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

#include <SFML/Graphics.hpp>
#include <iostream>
#include "Player.h"

int main()
{
    Player Player;
    sf::Texture tPlayer;

    if (!tPlayer.loadFromFile("textures/Player.png")) return 1;
    else Player.setTexture(tPlayer);

    std::cout << "setting player sprite in main" << std::endl;

    sf::Sprite sPlayer(Player.getTexture());

    sPlayer.setPosition(100, 100);

	sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "SFML Sample Application");
    //Player.setPos(100, 100);

	while (Window.isOpen())
	{/*
		sf::Event Event;
		while (Window.pollEvent(Event))
		{
			switch (Event.type)
			{
			case sf::Event::Closed:
				Window.close();
				break;
			default:
				break;
			}
		}*/

		Window.clear(sf::Color(0, 255, 255));
		//Player.getSprite(ref);
		//std::cout << ref.getPosition().x << ref.getPosition().y << std::endl;
		//Window.draw(ref);
		Window.draw(sPlayer);
		Window.display();
	}

	return 0;
}


Player.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#ifndef PLAYER_H
#define PLAYER_H
#include <SFML/Graphics.hpp>

class Player
{
public:
    Player();
    //Player(sf::Texture&);
    //do
    //void setPos(int = 0, int = 0);
    //get
    sf::Texture getTexture();
    //void getSprite(sf::Sprite&);
    //set
    void setTexture(sf::Texture&);
private:
    int speed;
    sf::Texture tPlayer;
    //sf::Sprite sPlayer;
};

#endif // PLAYER_H 


Player.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
#include "Player.h"
#include <iostream>

Player::Player()
{
    std::cout << "hi" << std::endl;
}
/*
Player::Player(sf::Texture& tex)
{
    std::cout << "hi2" << std::endl;
    tPlayer = tex;
    sPlayer.setTexture(tPlayer);
}

void Player::setPos(int x, int y)
{
    sPlayer.setPosition(x, y);
}



void Player::getSprite(sf::Sprite& ref)
{
    ref = sPlayer;
}
*/

sf::Texture Player::getTexture()
{
    std::cout << "sending player texture ref" << std::endl;
    return tPlayer;
}

void Player::setTexture(sf::Texture& tex)
{
    std::cout << "setting player texture" << std::endl;
    tPlayer = tex;
}


I'm guessing I'm not doing references right, because it looks as if the texture is being drawn with its default initialization (white square)

http://i.imgur.com/vztGf.png is what the player should look like
Last edited on
pfft, so every other thread gets half a dozen of replies and my sad little thread gets 0? /me sad

*cough* still haven't figured this thing out, it is really bugging me, how would you store something given to you by reference as a value, then give it back by value? I think I tried sending it by value and also giving it back by reference but no joy, might be wrong though, heh.

maybe if I stored it as a pointer and gave it as a pointer, but I'm not sure, I'll have to give it a try later.

Topic archived. No new replies allowed.