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

Author Topic: [SOLVED]Can't load a background  (Read 7012 times)

0 Members and 3 Guests are viewing this topic.

denisonl

  • Newbie
  • *
  • Posts: 4
    • View Profile
[SOLVED]Can't load a background
« on: August 27, 2016, 11:06:11 am »
Hello! I can't load a background for a window. That's my code:

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

int main()
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
    while (window.isOpen())
    {
        // Process events
        sf::Event event;
        sf::Image image;
        image.loadFromFile("texture.png");

        sf::Texture texture;
        texture.update(image);
        sf::Sprite sprite;
        sprite.setTexture(texture);
        window.draw(sprite);

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

       
        // Clear screen
        window.clear();
        // Update the window
        window.display();
    }
    return EXIT_SUCCESS;
}

What did I do wrong? Thank you
« Last Edit: August 29, 2016, 04:32:36 pm by denisonl »

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: Can't load a background
« Reply #1 on: August 27, 2016, 11:50:04 am »
You need to draw the sprite between the clear and display statements.

By the way, you don't need to load the texture every frame. Just load it before the "while".

denisonl

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Can't load a background
« Reply #2 on: August 27, 2016, 01:08:06 pm »
I've done what you said, but there is no result. Only black window

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
int main()
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
    sf::Image image;
    image.loadFromFile("texture.png");

    sf::Texture texture;
    texture.update(image);
    sf::Sprite sprite;
    sprite.setTexture(texture);
    while (window.isOpen())
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window: exit
            if (event.type == sf::Event::Closed)
                window.close();
        }
       
        // Clear screen
        window.clear();
        window.draw(sprite);
        // Update the window
        window.display();
    }
    return EXIT_SUCCESS;
}
« Last Edit: August 27, 2016, 04:17:28 pm by denisonl »

Stellaris

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Can't load a background
« Reply #3 on: August 27, 2016, 01:43:54 pm »
Maybe the "texture.png" doesn't exists (typo ?)
Check the image was correctly loaded with the return value of loadFromFile

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Can't load a background
« Reply #4 on: August 27, 2016, 01:44:27 pm »
Is the image loaded successfully? Make sure to always check that. You can btw. load an image directly on a texture.

Also make sure to use the [code=cpp][/code] tags on the forum for code formatting.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

denisonl

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Can't load a background
« Reply #5 on: August 27, 2016, 04:19:43 pm »
Maybe the "texture.png" doesn't exists (typo ?)
Check the image was correctly loaded with the return value of loadFromFile
File exists, return value of loadFromFile is 1 (true)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Can't load a background
« Reply #6 on: August 27, 2016, 09:16:30 pm »
You aren't creating the texture before attempting to update it with the content of the image.
"This function does nothing if the texture was not previously created." - sf::Texture::update(sf::Image) documentation

You could create the texture first or just "load" the texture from the image:
sf::Texture texture;
if (!texture.loadFromImage(image))
    return EXIT_FAILURE;

Simpler still, you can skip the sf::Image completely if you don't need to do any image processing and load directly into the texture from the file:
sf::Texture texture;
if (!texture.loadFromFile("texture.png"))
    return EXIT_FAILURE;
« Last Edit: August 27, 2016, 09:18:49 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

denisonl

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Can't load a background
« Reply #7 on: August 29, 2016, 04:32:18 pm »
Thank you very much! I've done it :)

 

anything