SFML community forums

Help => Graphics => Topic started by: masterassassin1398 on October 07, 2014, 08:03:29 pm

Title: SFML textures not loading
Post by: masterassassin1398 on October 07, 2014, 08:03:29 pm
Hiya.  I am having some problems trying to load my texture file.  I dont fully understand textures at all i dont feel that the tutorial is as descriptive as i would want it to be as far as what a texture is and what it is used for.  The way i am interpreting it is that its an image with a bunch of (for example characters) things on it and a sprite is an individual thing(character).  Im so confused =/  Anyway i am using codeblocks and c++.  My image is within the same file as my exe and everything else, i added the image file to my codeblocks project but that didnt seem to do anything.  Im not sure what else i could do.  Can anyone tell me what a texture is and how to use it?  And also what might be wrong with my code?  The reason it says its not working is because it is unable to open file.

Code:
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>

using namespace std;

int main(){
sf::RenderWindow window(sf::VideoMode(800, 600), "Insomnia");
window.setPosition(sf::Vector2i(0, 0));
sf::Vector2u size = window.getSize();
unsigned int windowLength = size.x;
unsigned int windowWidth = size.y;

while(window.isOpen()){
    sf::Texture texture;
    if(!texture.loadFromFile("TextureImage")){
        return 1;
    }

    sf::Event event;
        while(window.pollEvent(event)){
            if(event.type == sf::Event::Closed)
                window.close();
        }

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
            window.close();
        }

        window.clear(sf::Color::Black);

        window.display();
}
return 0;
}

 
Title: Re: SFML textures not loading
Post by: ripps on October 07, 2014, 09:04:15 pm
Could you try adding the extension to the filename?

eg.
!texture.loadFromFile("TextureImage.png")

The way I think of textures and sprites is the sprite is the object that you move, the texture is its "skin" - or what you see. You can't see a sprite.

Hope that makes sense.
Title: Re: SFML textures not loading
Post by: masterassassin1398 on October 08, 2014, 12:46:04 am
Ahh yes of course I totally missed that.  And ya it kinda makes sense.  I will have to look into it more.  Thank you.
Title: Re: SFML textures not loading
Post by: didii on October 08, 2014, 01:09:16 pm
The documentation (http://www.sfml-dev.org/documentation/2.0/annotated.php) is always a good place if you don't know exactly what each class does ;)
Title: Re: SFML textures not loading
Post by: masterassassin1398 on October 08, 2014, 05:48:30 pm
Thank you  :)
Title: Re: SFML textures not loading
Post by: Hapax on October 09, 2014, 01:36:32 am
A sprite is basically a rectangle and a texture is what can be shown on that rectangle.

You could think of it like a painting on the wall:
Sprite is the frame.
Texture is the painting.
Without a painting, it's just an empty frame.
Without a frame, there's no way to display the painting.