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

Author Topic: [Solved] Problem with sf::Sprite  (Read 1480 times)

0 Members and 1 Guest are viewing this topic.

Yemlhalf

  • Newbie
  • *
  • Posts: 2
    • View Profile
[Solved] Problem with sf::Sprite
« on: June 12, 2012, 04:36:39 pm »
Hello everyone,

I'm new in this forum and I got some problem with sf::Sprite. I'm trying to do a class called piece and one of his attributes is a sprite and a texture. This is the code I wrote in the class Piece.h.

#ifndef PIECE_H_
#define PIECE_H_

class Piece {
public:
        Piece();
        virtual ~Piece();

        sf::Texture p_texture;
        sf::Sprite p_sprite;
};


#endif /* PIECE_H_ */
 

And this is Piece.cpp

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

using namespace std;

Piece::Piece() {
        if(!p_texture.loadFromFile("./Images/Pieza_Azul.png"))
        {
                cout << "Error." << endl;
        }

        p_sprite.setTexture(p_texture,true);
}

Piece::~Piece() {
        // TODO Auto-generated destructor stub
}
 

And this is the main.cpp

#include <SFML/Graphics.hpp>
#include "Pieces/Piece.h"
#include <iostream>
using namespace std;

int main() {
        Piece obj_piece;

        // Create the main window
        sf::RenderWindow window(sf::VideoMode(800, 600), "SFML");
        window.setVerticalSyncEnabled(true);
        window.setFramerateLimit(60);



        // Start the game loop
        while (window.isOpen())
        {
                // Process events
                sf::Event event;
                while (window.pollEvent(event))
                {
                        // Close window : exit
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                // Clear screen
                window.clear(sf::Color::White);
                window.draw(obj_piece.p_sprite);
                // Update the window
                window.display();
        }

        return 0;
}

When I start to compile the program, does not give any error during the compilation process, but when I try to run get an error:

not valid domains REMAIN!

In addition, the window look really weird things.

I wanted to comment if you can put a sprite in a class, I've tried in various ways, but it still fails. When I put a Sprite in a class and compile SFML window disappears instantly, and gives no errors in compilation or execution.

The question is, you should put a sprite in a class such as a character in order to have the sprite associated with that object? If yes the attribute must be private right?

regards

Yemlhalf

ps: Sorry if I made grammatical or lexical errors I have to improve my English: p
« Last Edit: June 12, 2012, 05:11:07 pm by Yemlhalf »

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Re: Problem with sf::Sprite
« Reply #1 on: June 12, 2012, 05:07:53 pm »
Just with a quick look. I don't think you should try to create obj_piece before your window.

Also, in your Piece's constructor.
Piece::Piece() {
        if(!p_texture.loadFromFile("./Images/Pieza_Azul.png"))
        {
                cout << "Error." << endl;
        }

        p_sprite.setTexture(p_texture,true); // potential error if the texture failed to load
}
If loadFromFile() fails. then you send some data to cout, but you don't have any code to stop the call to setTexture(). If loadFromFile() had failed, then setTexture() is loading in a broken texture object, and could be the source of your error.

Try move the declaration of obj_piece below the declaration of the window and puting the setTexture() in an else block, like this:
Piece::Piece() {
        if(!p_texture.loadFromFile("./Images/Pieza_Azul.png"))
        {
                cout << "Error." << endl;
        }
        else
        {
                p_sprite.setTexture(p_texture,true); // now this will only be called if loadFromFile() succeeds
        }
}

Yemlhalf

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Problem with sf::Sprite
« Reply #2 on: June 12, 2012, 05:10:44 pm »
Solved.

The mistake was to create an object before initializing the window. I'm sorry: p  :-[ :-[

Thanks thePyro_13!!  ;)