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

Author Topic: Strange error when trying to loadFromFile  (Read 8760 times)

0 Members and 1 Guest are viewing this topic.

eliasfrost

  • Newbie
  • *
  • Posts: 5
    • View Profile
Strange error when trying to loadFromFile
« on: August 31, 2013, 03:39:22 pm »
Hi! I'm new to SFML (and a bit of newbie at programming), and I'm currently reading the tutorials. I'm on the sprites tutorial and I'm stuck on the first example, it says:

sf::Texture texture;
if (!texture.loadFromFile("image.png"))
{
    // error...
}

So I drew an image, named it image.png, saved it in the project folder and wrote this in my .cpp file:

#include <iostream>
#include <SFML\Graphics.hpp>

int main()
{
        // create render window
        sf::RenderWindow rmain(sf::VideoMode(800, 600), "My Window");
        rmain.setFramerateLimit(30);

        sf::Texture texture;
        if (!texture.loadFromFile("image.png"))
        {
                std::cout << "Unable to load image from file" << std::endl;
        }

        // run program as long as windos is open
        while (rmain.isOpen())
        {
                // check all the window's events that were triggered since the last iteration of loop
                sf::Event event;
                while (rmain.pollEvent(event))
                {
                        // "close requested" event, close the windoe
                        if (event.type == sf::Event::Closed)
                                rmain.close();
                }

                // clear the window with black color
                rmain.clear(sf::Color::Black);

                // draw stuff here
                // windoe.draw(..);

                // end current frame
                rmain.display();

        }

        return 0;
}

The error that I get is pretty crazy, it makes the console go all nuts while blips and bloops spill out from my computers alarm and I have no idea why this happens, the image is on the right place, I even moved it around the project folder in case it was misplaced, but to no avail.

Can someone please help me identify the problem and perhaps guide me through the proces of fixing it? Thanks.

Also, I did a search on the forums but I got errors every time I tried, so very sorry if this have been brought up times before.

Elias
« Last Edit: August 31, 2013, 03:47:11 pm by eliasfrost »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Strange error when trying to loadFromFile
« Reply #1 on: August 31, 2013, 03:51:02 pm »
That code shouldn't fail, you must be doing something else wrong. Where did you get the binaries from and what os and compiler are you using exactly?(I can tell it's windows and probably visual c++ but still tell)
And which line exactly fails and causes the weird console output?
Back to C++ gamedev with SFML in May 2023

eliasfrost

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Strange error when trying to loadFromFile
« Reply #2 on: August 31, 2013, 03:58:18 pm »
Thanks for the fast reply!


Quote
That code shouldn't fail, you must be doing something else wrong.
That's what I figured as well, but I was unsure as to where to look for the source of the problem.

My OS is Win7 64x
I'm compiling using VS12 and the version of SFML I downloaded was "Visual C++ 11 (2012) - 32 bits", which I guess is the right one, because I want to compile for 32x and not 64x.

Oh and he line that fails is:
if (!texture.loadFromFile("image.png"))

The error I get says:
Quote
Unhandled exception at 0x738BDEF8 (msvcr110.dll) in SFML_test.exe: 0xC0000005: Access violation reading location 0x00273000.

btw, does it matter what settings I use to save .png's? In case the file I try to open is problem.
« Last Edit: August 31, 2013, 04:03:17 pm by eliasfrost »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Strange error when trying to loadFromFile
« Reply #3 on: August 31, 2013, 04:04:44 pm »
It should just fail with comprehensive error but bugs in the image loading old C code that SFML uses are not unheard of so you can post the png here. And most things seem to save pngs right for SFML without adjusting anything but you might have made an error that might have caused crashes in that old C code.
Back to C++ gamedev with SFML in May 2023

eliasfrost

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Strange error when trying to loadFromFile
« Reply #4 on: August 31, 2013, 04:08:50 pm »
Quote
but bugs in the image loading old C code that SFML uses are not unheard of

Thanks but what do you mean by this?

Also, I attached the image, which is simply a 50 by 50 .png.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Strange error when trying to loadFromFile
« Reply #5 on: August 31, 2013, 04:15:36 pm »
There was/is bug in the library that SFML uses called stbi that causes crashes because if file is empty it tries to use uninitialized variable for something. But this png is fine and loads for me so it must be something else, are you sure you're not mixing debug and release?
Back to C++ gamedev with SFML in May 2023

eliasfrost

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Strange error when trying to loadFromFile
« Reply #6 on: August 31, 2013, 04:22:03 pm »
Oh alright thanks for the info.

The dependencies I use are these:

sfml-graphics.lib
sfml-window.lib
sfml-system.lib

Which are release libraries right?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Strange error when trying to loadFromFile
« Reply #7 on: August 31, 2013, 04:24:10 pm »
Yes. :P
But are you building as release too?
Back to C++ gamedev with SFML in May 2023

eliasfrost

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Strange error when trying to loadFromFile
« Reply #8 on: August 31, 2013, 04:29:52 pm »
Oh, oh oh! I get it. That fixed it for me, thanks. :)

I didn't know you could build in relase/debug, I was fixed on the lib files. I guess you learn new things everyday. Thanks again!

kevinpaladin

  • Guest
Re: Strange error when trying to loadFromFile
« Reply #9 on: October 21, 2013, 09:00:25 pm »
I get the same error too. But, when I try to build it under release mode, I get this erroneous output :

Code: [Select]
1>------ Build started: Project: sfml, Configuration: Release Win32 ------
1>Build started 10/22/2013 12:27:42 AM.
1>InitializeBuildStatus:
1>  Touching "Release\sfml.unsuccessfulbuild".
1>ClCompile:
1>  Game.cpp
1>  main.cpp
1>MSVCRT.lib(crtexew.obj) : error LNK2001: unresolved external symbol _WinMain@16
1>C:\Users\kevin\Documents\Visual Studio 2012\Projects\sfml\Release\sfml.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:05.11
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Strange error when trying to loadFromFile
« Reply #10 on: October 21, 2013, 09:41:42 pm »
You need to link the "main" module to deal with _WinMain, as the official tutorial explains.

 

anything