Jump to content
  • Advertisement
Sign in to follow this  
Canvas

SFML white box?

This topic is 2223 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

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

//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 gButton.cpp

#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;
}


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
Advertisement
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

i can change that, but was just testing, still how do i fix it?


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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!