Sign in to follow this  
Canvas

SFML white box?

Recommended Posts

Hey guys ive moved from SDL to SFML for many reasons, but lets get straight to the question, at the moment i have some pieces of code

gState.cpp
[CODE]
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();
}
}
[/CODE]

gButton.h
[CODE]
//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;
sf::Texture* graphic;
public:
//Variables
sf::Sprite sprite;
gButton();
gButton(int x,int y,int w,int h, int ident);
sf::Sprite draw();
void handleEvents(sf::Event iEvent);
int rtnState(){return state;}
int rtnID(){return id;}
};
//end body
#endif
[/CODE]

and gButton.cpp
[CODE]
#include "gButton.h"
#include <iostream>
gButton::gButton()
{
//nothing
}
gButton::gButton(int x,int y,int w,int h,int ident)
{
state = 0;
xPos = x;
yPos = y;
width = w;
height = h;
id = ident;
sf::Texture temp;
temp.loadFromFile("images/newgamebtn.png");
graphic = &temp;
sprite.setTexture(*graphic);
}
sf::Sprite gButton::draw()
{
return sprite;
}
[/CODE]

now when i start my program, it loads the window, and in the top left is a white (black) box the same size of my image im trying to pass to it, now Ive read some pieces and I think graphic is going out of scope, so it gets deleted, but how can i fix this? Any ideas?

Canvas

Share this post


Link to post
Share on other sites
Yes your temp textures goes out scope as soon as the constructor is done. Why are you storing the texture as a pointer in button?

Share this post


Link to post
Share on other sites
[quote name='Canvas' timestamp='1339781011' post='4949595']
i can change that, but was just testing, still how do i fix it?
[/quote]

Hi, I don't use SFML 2.0, but I think this should help, try it out:


Change gButton.h to this:


//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;
sf::Texture graphic;
public:
//Variables
sf::Sprite sprite;
gButton();
gButton(int x,int y,int w,int h, int ident);
sf::Sprite draw();
void handleEvents(sf::Event iEvent);
int rtnState(){return state;}
int rtnID(){return id;}
};
//end body
#endif

And change gButton.cpp to this:

#include "gButton.h"
#include <iostream>
gButton::gButton()
{
//nothing
}
gButton::gButton(int x,int y,int w,int h,int ident)
{
state = 0;
xPos = x;
yPos = y;
width = w;
height = h;
id = ident;
graphic.loadFromFile("images/newgamebtn.png");
sprite.setTexture(graphic);
}
sf::Sprite gButton::draw()
{
return sprite;
}

Tell me if that worked :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this