SFML community forums

Help => Graphics => Topic started by: Tom Clabault on February 08, 2019, 07:41:18 pm

Title: My sprite won't show ( black screen )
Post by: Tom Clabault on February 08, 2019, 07:41:18 pm
Hi!

So I tried to draw a simple sprite but I can't get it to work. Why ? I don't think I have done any mistakes but it just shows a black screen when running the code. What's the problem ?

Here is my code:

#include "include/SFML/Graphics.hpp"

#pragma region Variables
sf::RenderWindow window(sf::VideoMode(750, 750), "Title");
#pragma endregion Variables





int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
{
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                window.clear();

                sf::Texture texture;
                texture.loadFromFile(".\\Resources\\Level\\Textures\\dirt.jpg");
                sf::Sprite sprite;
                sprite.setTexture(texture);
                window.draw(sprite);

                window.display();
        }
}

I'm using Visual Studio under Windows 10 if some you wanted these infos.

Thanks in advance
Title: Re: My sprite won't show ( black screen )
Post by: Laurent on February 08, 2019, 08:45:10 pm
https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Sprite.php#a3729c88d88ac38c19317c18e87242560

Have a look at the second argument. Or you can directly use the sf::Sprite constructor to get everything right in a single call.

Additionally:
- avoid Windows specific stuff when it's not necessary (WinMain -> link to sfml-main; "\\" in paths -> "/")
- don't construct SFML variables such as windows at global scope, you may experience undefined behaviours
- don't load your texture again and again every time you show it
Title: Re: My sprite won't show ( black screen )
Post by: Tom Clabault on February 08, 2019, 09:34:10 pm
int main()
{
        sf::Texture texture;
        if (!texture.loadFromFile("./Resources/Level/Textures/dirt.jpg"))
                exit(0);
        sf::Sprite sprite(texture);

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

                window.clear();

                window.draw(sprite);

                window.display();
        }
}

Changes done, it works fine, thank you !