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

Author Topic: Error : with my Texture  (Read 1116 times)

0 Members and 1 Guest are viewing this topic.

Valky

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Error : with my Texture
« on: October 12, 2014, 08:32:19 pm »
Sorry, I'm a beginner with the SFML. I have just started to make projects with it.
And I have a annoying error ! The window draws a white square with the same dimension as the image that I want to draw. I think it's a problem with my texture.

Here's the code :
main.cpp
#include <SFML/Graphics.hpp>
#include "gastaud.h"
#include <iostream>

using namespace std;


int main()
{

    sf::RenderWindow app(sf::VideoMode(800, 600), "SFML window");
    Gastaud leboss;

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

        // Clear screen
        app.clear();

        // Draw the sprite
        app.draw(leboss.getPhotoGastaud());
        // Update the window
        app.display();
    }

    return EXIT_SUCCESS;
}
 

gastaud.h
#ifndef GASTAUD_H_INCLUDED
#define GASTAUD_H_INCLUDED
#include <SFML/Graphics.hpp>

class Gastaud
{
    public :
    Gastaud();
    sf::Sprite getPhotoGastaud();
    sf::Texture PreparePhoto();

    private :
    sf::Sprite *PhotoGastaud;

};
#endif // GASTAUD_H_INCLUDED
 

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

using namespace std;

Gastaud::Gastaud()
{
    PhotoGastaud = new sf::Sprite;
    PhotoGastaud->setTexture(PreparePhoto());
}

sf::Sprite Gastaud::getPhotoGastaud()
{
    return *PhotoGastaud;
}

sf::Texture Gastaud::PreparePhoto()
{
    sf::Texture textureProf;
    if(!textureProf.loadFromFile("gastaud.png"))
    {
        cout << "Probleme with the image : gastaud.png !" << endl;
    }
    textureProf.setSmooth(true);
    return textureProf;
}
 

Can you help me to fix the problem ?
« Last Edit: October 12, 2014, 08:33:59 pm by Valky »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Error : with my Texture
« Reply #1 on: October 12, 2014, 08:39:59 pm »
This looks like the usual "white square problem" to me: http://sfml-dev.org/tutorials/2.1/graphics-sprite.php#the-white-square-problem

I'm guessing you thought it would work because you're actually returning the Texture from the function, but I don't think Textures retain all of their data when they get copied around like that.  Make the texture a member of the class and it should work fine.
« Last Edit: October 12, 2014, 08:42:11 pm by Ixrec »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10998
    • View Profile
    • development blog
    • Email
AW: Error : with my Texture
« Reply #2 on: October 12, 2014, 08:54:52 pm »
A copy of a texture will work even though it's inefficient and mostly a sign of bad code. ;)

The issue here is that you return a copy of a texture, but use this rvalue texture to set it to the sprite. But as soon as the setTexture function returns, the rvalue will vanish and thus there's no texture to be seen.

I advise you to read again about pointers and references in C++. Also you should stop using manual memory management. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything