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

Author Topic: Problem Loading External Files - CodeBlocks  (Read 5751 times)

0 Members and 3 Guests are viewing this topic.

noah.i.rivera

  • Newbie
  • *
  • Posts: 2
    • View Profile
Problem Loading External Files - CodeBlocks
« on: August 19, 2013, 09:15:49 am »
Hello,

I'm rather experienced programming-wise. I am completely new, however, to IDEs, linking, and other such facets of software development. I've recently found out about SFML and decided to use it to learn about such processes.

My OS is Windows 8 64-bit, and I am using SFML with MinGW in CodeBlocks.
I've had experience using the command line MinGW g++ compile, and would've actually prefered using SFML in that, but learning about linking and other such things seemed was becoming a headache to figure out. So, instead, I set up SFML to work within CodeBlocks.

I've set up a test project, using only a Release build, which can compile and run fine. My problem, however, is figuring out how to access external files with SFML. I'm trying to run code provided on the SFML website, but am having problems loading an image.

Here is my code:
int main()
{
    sf::RenderWindow window(sf::VideoMode(320, 320), "Test");

    sf::Texture texture;
    if (!texture.loadFromFile("image.png"))
        return -1;

    sf::Sprite sprite;
    sprite.setTexture(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();
    }

    return 0;
}

The process is always terminated with status -1, so I'm assuming loadFromFile is always returning false.

My project directory is:
C:\Users\<name>\Documents\CodeBlocks Projects\Test

I've put "image.png" in:
"C:\Users\<name>\Documents\CodeBlocks Projects\Test\"

as well as in:
C:\Users\<name>\Documents\CodeBlocks Projects\Test\bin\Release\

but the program still throws the error.

I've tried to do research, and the only thing I thought would be the problem would be CodeBlock's "working directory" for the project, which I've tried setting to "bin\Release", but I have not met any success. Aside from that, my search did not yield that helped me fix the problem.

Am I using the function incorrectly, or is there some setting I missed somewhere?

Also, is there a way, using CodeBlocks, that I can package and access files, such as images, within the executable itself?

Thanks for any help,
Noah

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem Loading External Files - CodeBlocks
« Reply #1 on: August 19, 2013, 09:22:46 am »
You should have a look at the console: SFML should have written the reason of the failure. Maybe it's not related to the working directory at all.

Quote
I've had experience using the command line MinGW g++ compile, and would've actually prefered using SFML in that, but learning about linking and other such things seemed was becoming a headache to figure out. So, instead, I set up SFML to work within CodeBlocks.
It's not a headache at all: for example, instead of adding <path-to-sfml>/lib to the linker search paths in your Code::Blocks project, you would add "-L<path-to-sfml>/lib" to the command line. Same for include path and linked libraries. And that's all you need to know to use SFML.

Quote
Also, is there a way, using CodeBlocks, that I can package and access files, such as images, within the executable itself?
Yes, but it would involve low-level OS specific functions. A simpler solution is to package resources in an external file.
Laurent Gomila - SFML developer

noah.i.rivera

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Problem Loading External Files - CodeBlocks
« Reply #2 on: August 19, 2013, 07:33:26 pm »
I set up a debug build target, which I should've done in the first place, I discovered that my 8-bit png wouldn't load. I've tried several other png and gif files and the program reads them fine.

After doing a bit of research on "-L", and link and include paths, I was able to compile the program within the command line. Thanks for the info, as it was indeed very simple.

Quote
Yes, but it would involve low-level OS specific functions. A simpler solution is to package resources in an external file.
Could you recommend some commonly used methods and/or tutorials on how to package external resources?

I really appreciate your help. I haven't delved deeper into "true" software development is largely due to lack of knowledge on what to look for and inadequate documentation. I think it's fantastic that you're able to personally help out.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem Loading External Files - CodeBlocks
« Reply #3 on: August 19, 2013, 08:17:28 pm »
Quote
Could you recommend some commonly used methods and/or tutorials on how to package external resources?
Not me, but I'm sure someone else will give you useful links. You may also find interesting stuff on the wiki, if I remember correctly.
Laurent Gomila - SFML developer

NoobsArePeople2

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
Re: Problem Loading External Files - CodeBlocks
« Reply #4 on: August 21, 2013, 07:58:45 pm »
Could you recommend some commonly used methods and/or tutorials on how to package external resources?

Here's a quick rundown of how I do this on Windows using CodeBlocks and OSX using Xcode. I'm far from an experienced expert, my current project being my first C++ project but this system is working well for me so far.

On OSX applications are typically distributed in an app bundle. An app bundle is actually just a folder with the extension "app" that OSX recognizes and effectively treats as a file (though you can easily dig into it). I mention this because app bundles have an expected layout described here and my game is currently targeting Windows and OSX (and Linux when I get around to installing it).

As OSX has an expected layout and Windows doesn't I go with the OSX layout so everything lives in the same place regardless of platform. Also the OSX layout happens to be quite logical and nice. So all my audio, graphics and other external resources live in a folder called "Resources" in the final binary.

On Windows I use a tool called exe-packager to package my exe, dlls and resources into a single exe file. You can set the working directory with exe-packager but I haven't quite worked out exactly how I want to do that. You'll probably want to write a batch script to automate packing everything up with exe-packager.

When setting up a project I use premake which allows me to describe my project with a single configuration file and then generate specific IDE projects from that. On Windows I generate a CodeBlocks project, on Mac an Xcode one. Premake uses Lua as a scripting language and provides a script API to do things like detect which OS you are generating from and do OS-specific things.

In my premake script I make sure to include my Resources folder in the project and CodeBlocks automatically picks it up and my resources load just fine. Xcode works a tad differently (of course) so I ended up writing a script that gets run after a successful build that copies all my resources into the app bundle.