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

Author Topic: My sprite won't show ( black screen )  (Read 3146 times)

0 Members and 1 Guest are viewing this topic.

Tom Clabault

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
My sprite won't show ( black screen )
« 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
« Last Edit: February 08, 2019, 07:46:36 pm by Tom Clabault »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: My sprite won't show ( black screen )
« Reply #1 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
Laurent Gomila - SFML developer

Tom Clabault

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: My sprite won't show ( black screen )
« Reply #2 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 !

 

anything