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

Author Topic: Texture not working from header c++  (Read 7297 times)

0 Members and 2 Guests are viewing this topic.

Artifact

  • Newbie
  • *
  • Posts: 2
    • View Profile
Texture not working from header c++
« on: July 30, 2014, 04:43:23 pm »
Hello, I'm trying to load a texture and a sprite. This works fine as long as I define them inside my .cpp file, but if I move the texture to the header file nothing is shown in the window.

Here is the header file:

#pragma once
#include <SFML/Graphics.hpp>
using namespace sf;
class GameObject
{

public:
        GameObject();
        ~GameObject();
        void Draw(RenderWindow *);
        void Update();
        Sprite sprite;
        Texture texture;
};

This is the .cpp file:

GameObject::GameObject()
{      
       
        if (!texture.loadFromFile("sprite.png"))
        {

        }

        sprite.setTexture(texture);
       
}

void GameObject::Draw(RenderWindow * window)
{      
        window->draw(GameObject::sprite);
}

I tried debugging it at it looks like my texture doesn't have a y size when its defined inside the header. Why is that?

Note: I'm a C# programmer who is trying to play around with C++, there might be some things, that I dont understand yet.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Texture not working from header c++
« Reply #1 on: July 30, 2014, 05:26:04 pm »
are you sure the texture is loaded at all? i like this kind of "debugging" sometimes
if (!texture.loadFromFile("sprite.png")){
    std::cout << "texture not loaded!";}
else {
    std::cout << "texture loaded!";
}
you could also try using the complete namespace
sf::Texture texture;
and of course, make sure the image name and folder are correct.

other than that, the code doesn't seems to have any errors to me...
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Artifact

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Texture not working from header c++
« Reply #2 on: July 30, 2014, 06:07:47 pm »
I'm sure that its loading the texture, it works fine if I cut the texture line from the header file into  the .cpp file.  I have no idea why it isn't working.

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: Texture not working from header c++
« Reply #3 on: July 30, 2014, 06:55:02 pm »
This works
#include <SFML/Graphics.hpp>
class GameObject
{
public:
        GameObject();
        ~GameObject();
        void Draw(sf::RenderWindow &);
        void Update();
        const std::string textureFile = "texture.png";
        sf::Texture texture;
        sf::Sprite sprite;     
};
GameObject::GameObject()
{

        if (!texture.loadFromFile(textureFile))
        {

        }
        sprite.setTexture(texture);
}
GameObject::~GameObject()
{
}
void GameObject::Draw(sf::RenderWindow& window)
{
        window.draw(GameObject::sprite);
}

int main()
{
        GameObject object;
        sf::RenderWindow window;
        window.create(sf::VideoMode(800, 600), "test");
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
                window.clear();
                object.Draw(window);
                window.display();
        }
        return 0;
}
« Last Edit: July 30, 2014, 06:56:51 pm by Strelok »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Texture not working from header c++
« Reply #4 on: July 30, 2014, 07:44:53 pm »
Please read this carefully and adjust your problem description accordingly.

The crystal ball says you perform an unintended copy.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

fatum

  • Newbie
  • *
  • Posts: 47
    • MSN Messenger - bowsers7@hotmail.com
    • AOL Instant Messenger - therealblah569
    • View Profile
    • http://boards.psynetfm.com
Re: Texture not working from header c++
« Reply #5 on: July 31, 2014, 12:39:13 am »
This works

You should try passing the render window as a reference instead of a pointer to `GameObject::Draw` like you're doing here.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Texture not working from header c++
« Reply #6 on: August 01, 2014, 10:19:07 am »
You should try passing the render window as a reference instead of a pointer to `GameObject::Draw` like you're doing here.
That won't make any difference, but it can be a good code style to express "null pointers are not allowed".
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything