Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Quick question  (Read 2059 times)

0 Members and 1 Guest are viewing this topic.

nisse pisse

  • Newbie
  • *
  • Posts: 9
    • View Profile
Quick question
« on: June 24, 2010, 12:37:06 pm »
Hello, when i call my drawgame function in the following code it just paints a white square where the picture are supposed to be. the square have the right proportions and such its just white...





Code: [Select]
#include "Gamestate.cpp"
#include "Player.cpp"
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
using namespace std;
class Model {

private :
int hej;
Gamestate* gamestate;
Player* player;
sf::Sprite Sprite1;

public :

Model();


void updategame();

void drawgame(sf::RenderWindow *App);

};

Model::Model(){

         sf::Image Image1;
        Image1.SetSmooth(false);
        Image1.LoadFromFile("trans.png");
        Sprite1.SetImage(Image1);
        Sprite1.SetPosition(500,-1);
}




void Model::drawgame(sf::RenderWindow *App){



     Sprite1.SetPosition(500,-1);
     App->Draw(Sprite1);


}




Also it works fine if I load the picture in the drawgame function


Code: [Select]
void Model::drawgame(sf::RenderWindow *App){
    sf::Image Image1;
        Image1.SetSmooth(false);
        Image1.LoadFromFile("trans.png");
        sf::Sprite Sprite1(Image1);
        Sprite1.SetPosition(500,-1);



     App->Draw(Sprite1);


}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Quick question
« Reply #1 on: June 24, 2010, 12:40:36 pm »
You declare your image as a local variable in the constructor. So, its lifetime ends at the end of the scope, and the sf::Sprite references an invalid sf::Image.

Just declare the sf::Image as a member variable of the class.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

nisse pisse

  • Newbie
  • *
  • Posts: 9
    • View Profile
Quick question
« Reply #2 on: June 24, 2010, 12:44:36 pm »
thank you :)