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

Author Topic: Creating a background image  (Read 16210 times)

0 Members and 2 Guests are viewing this topic.

Wolff

  • Newbie
  • *
  • Posts: 22
    • View Profile
Creating a background image
« on: May 16, 2018, 10:23:33 pm »
Hello, I'm just starting to use SFML and I wanted to start off by making a window with some background picture filling it out (I'll worry about resizing it with the window later). I've seen this method work before but for me it only displays an error on the console: Unable to open file. The image is inside the folder named images which is right beside main.cpp. (Also tried pasting a whole path to the file - doesnt work either).

#include "stdafx.h"
#include<SFML/Graphics.hpp>
#include<SFML/Window.hpp>
#include<SFML/System.hpp>
int main()
{
        sf::RenderWindow window{ sf::VideoMode(800,600), "The game!" };
        sf::Texture t;
        t.loadFromFile("images/city.png");
        sf::Sprite s(t);
        while (window.isOpen())
        {
                sf::Event windowEvent;
                while (window.pollEvent(windowEvent))
                {
                        if (windowEvent.type == sf::Event::Closed)
                                window.close();
                }
                window.clear(sf::Color::White);
                window.draw(s);
                window.display();
        }
        return 0;
}
 
« Last Edit: May 17, 2018, 08:26:07 am by Laurent »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Creating a background image
« Reply #1 on: May 16, 2018, 11:14:42 pm »
This snippet from the official tutorials may be useful:
Quote
The loadFromFile function can sometimes fail with no obvious reason. First, check the error message that SFML prints to the standard output (check the console). If the message is unable to open file, make sure that the working directory (which is the directory that any file path will be interpreted relative to) is what you think it is: When you run the application from your desktop environment, the working directory is the executable folder. However, when you launch your program from your IDE (Visual Studio, Code::Blocks, ...) the working directory might sometimes be set to the project directory instead. This can usually be changed quite easily in the project settings.
You said you put your images folder next to your main.cpp, but that might not be your working directory. Make sure to double check that.

If you tried an absolute path (something like "C:/a/b/c/images/city.png") make sure you either used forward slashes or backslashes with proper escaping. Other than that if things are still not working then your image may be malformed or in a format SFML doesn't understand. Could you try a different image and see if that one works?

Wolff

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Creating a background image
« Reply #2 on: May 16, 2018, 11:43:22 pm »
Yea, for some reason another picture works (a picture of grass). Both are JPG and are located in the same folder in the same destination. No idea what the problem was/is. Could a size of the picture or its dimensions create a problem when trying to load it?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Creating a background image
« Reply #3 on: May 17, 2018, 08:47:03 am »
Make sure you check the returned value by loadFromFile as it indicates failure or success.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything