SFML st::texture out of scope :(

Hey guys, im trying to get a image to display using SFML with C++, here is the code

gState just displays the image
gState.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
bool gState::init()
{
	loaded = false;
	//graphics.loadImage("images/simplebackground.pi");
	//gButton newBtn(470,349,96,32,1);
	//buttons.push_back(newBtn);
	quit = false;
	frameRate = 60.0f/1000.0f;
	screenWidth = 1024;
	screenHeight = 768;
	screen.create(sf::VideoMode(screenWidth, screenHeight, 32), "Phor Temp (SFML)");
	screen.setVerticalSyncEnabled(true);
	currentState = 0;
	gButton temp(100.f,100.f,150,50,1);
	button = temp;
	return true;
}
void gState::run(int state)
{
	currentState = state;
}

void gState::update()
{
	while (rtnQuit() == false)
	{
		currentState += 1;
		//Event checker
		while(screen.pollEvent(iEvent))
		{
			if(iEvent.type == sf::Event::Closed)
			{
				quit = true;
			}
			//Events
			//stateEvents();
		}
		//State checker
		if(currentState == 1)
		{
			//if(buttons.at(0).rtnState() == 1 && buttons.at(0).rtnID() == 1)
			//{
			//	currentState = 2;
			//	changeState();
			//}
		}
		//Collision
		//stateCollision();
		//Update
		//stateUpdate();
		if(fps.getElapsedTime().asMilliseconds() > frameRate)
		{
			fps.restart();
		}
		screen.clear(sf::Color(currentState,currentState,currentState));
		screen.draw(button.sprite);
		screen.display();
		//DRAW
		//stateDraw();
	}
}


gButton.h file
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
//Player header
#ifndef gBUTTON_H
#define gBUTTON_H
//includes
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
//Using namespace

//body
class gButton{
private:
	int id;
	int state;
	float xPos, yPos;
	int width, height;
public:
	//Variables
	sf::Sprite sprite;
	sf::Texture graphic;

	gButton();
	gButton(int x,int y,int w,int h, int ident);
	sf::Sprite draw();
	void init();
	void handleEvents(sf::Event iEvent);
	int rtnState(){return state;}
	int rtnID(){return id;}
};
//end body
#endif 


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

gButton::gButton()
{
	//init();
}

gButton::gButton(int x,int y,int w,int h,int ident)
{
	state = 0;
	xPos = x;
	yPos = y;
	width = w;
	height = h;
	id = ident;
	init();
}

void gButton::init()
{
	graphic.loadFromFile("images/newgamebtn.png");
	sprite.setTexture(graphic);
}

sf::Sprite gButton::draw()
{
	return sprite;
}


Now when i compile the window comes up but in the top left is just a white box of the image, now i have read and it seems my texture goes out of scope, as a sf::sprite takes a reference to the image, now how can i get it so the texture variable will not be destroyed? Also i tried making sf::texture static, but it comes up with a compile error if i put static before the declaration.

Any help would be awesome

Cheers,
Canvas
I dont think its a scope issue, the texture is a class member so as long as the class object exists the texture should to. Are you sure the image is being loaded, you can try something like this to be sure.
1
2
if(!graphic.loadFromFile("images/newgamebtn.png"))
    std::cout<<"error";
Topic archived. No new replies allowed.