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

Author Topic: Loading an image fail  (Read 1625 times)

0 Members and 1 Guest are viewing this topic.

Toxicshadow

  • Newbie
  • *
  • Posts: 3
    • View Profile
Loading an image fail
« on: August 25, 2012, 05:58:26 am »
First: I did do a search and did find other topics containing the same issue, however they didn't help in fixing my problem.


I'm completely new to sfml, and completely lost. I looked at the official tutorial on rendering graphics with the graphics package. Managed to get a black screen. After that I looked at a different tutorial on actually getting an image to render, and all that happens is a quick 1frame white screen and then it closes and returns that it cant open the image file when I have the file in the location I *think* it should go(Source->Bin->Debug, with the debug exe) My image's name is 'ball.png'

My code looks like:

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

using namespace sf;

int main()
{
    // Create the main rendering window
    sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "SFML Graphics");
    sf::Image Ball;
    if (!Ball.LoadFromFile("ball.png"))
        return 1;
    sf::Sprite SprBall;
    SprBall.SetImage(Ball);
    SprBall.SetPosition(100.0f, 30.0f);

    // Start game loop
    while (Window.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (Window.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                Window.Close();
        }

        // Clear the screen (fill it with some random color)
        Window.Clear(sf::Color(23, 98, 137));
        //Drawing the Ball
        Window.Draw(SprBall);
        // Display window contents on screen
        Window.Display();
    }

    return EXIT_SUCCESS;
}
 
Thanks for any/all help given :)

Edit: Oh and sorry if it looks very messy, its a mix of 2 different tutorials.
« Last Edit: August 25, 2012, 09:59:21 am by Toxicshadow »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Loading an image fail
« Reply #1 on: August 25, 2012, 09:54:41 am »
Well you then probably hacen't read enough... ;)
You've already found the cause and your uncertenty about the images location says it all. Unfortunatly you didn't state what IDE you're using.
E.g. in VS you have to place the resources next to the project file.
Generally speaking the application will search for the resource in the working directory, if your IDE has such an option, then you probably should check its path.

Note: This forum has a code=cpp tag, so please make use of it. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Toxicshadow

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Loading an image fail
« Reply #2 on: August 25, 2012, 09:58:54 am »
Well you then probably hacen't read enough... ;)
You've already found the cause and your uncertenty about the images location says it all. Unfortunatly you didn't state what IDE you're using.
E.g. in VS you have to place the resources next to the project file.
Generally speaking the application will search for the resource in the working directory, if your IDE has such an option, then you probably should check its path.

Note: This forum has a code=cpp tag, so please make use of it. ;)
Sorry about the code=cpp thing. Also I'm using Code::Blocks. My image is in Source\bin\Debug. This is also where I placed the dlls and where CB compiles the debug exe to.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Loading an image fail
« Reply #3 on: August 25, 2012, 10:56:15 am »
In Code::Blocks the working directory is defined through: "Project -> Properties -> Build targets -> [name of target] -> Execution working dir"

Quote
it cant open the image file
Is that the actual error message, because there could also be others, like unsupported image formats (e.g. progressiv JPEG or 8bit PNGs).

Btw: I strongly advice you to use SFML 2. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Toxicshadow

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Loading an image fail
« Reply #4 on: August 31, 2012, 05:52:22 am »
Posting the answer for future reference:

In order for the image to work, you have to have the image where your source is, not where the exe compiles.
In Code::Blocks your folders look like:

Source             <- Image and source .cpp files go here
Source\bin\Debug       <- Exe compiles here
Source\obj\Debug       <- Main.o is here, but irrelevant to this topic.
 

Sorry if this is considered necroing, just posting this for future reference for anyone else who was confused.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Loading an image fail
« Reply #5 on: August 31, 2012, 10:48:59 am »
To be 100% correct, I think it's where the project file is, not source files. And an even more correct solution is, in my opinion, to change the project settings so that the working directory is the exe directory. So that executions from both the IDE and the explorer are consistent.
Laurent Gomila - SFML developer

 

anything