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

Author Topic: [SOLVED] loadFromFile issues  (Read 12859 times)

0 Members and 3 Guests are viewing this topic.

captaindarkness

  • Newbie
  • *
  • Posts: 3
    • View Profile
[SOLVED] loadFromFile issues
« on: September 28, 2013, 12:34:07 pm »
Hi, i'm new to SFML and C++(somewhat).(Using Visual Studio 2012)
I'm getting issues when i try to load a image because I have no idea where the code will try and load the image.
I tried to give the complete path but didn't work.

An error I get:

Unhandled exception at 0x77DF1F34 (msvcr100.dll) in 2D platformer.exe: 0xC0000005: Access violation reading location 0x00491000.

I tried using the Tutorial on the site but not sure what els to do.

#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;

int main()
{
    sf::RenderWindow window(sf::VideoMode(1280, 720), "SFML works!");
    sf::CircleShape shape(50.f); //size of circle
    shape.setFillColor(sf::Color::Green);
        window.setTitle("First Game");
        window.setVerticalSyncEnabled(true);
        window.setFramerateLimit(32);
        sf::Texture texture;

    while (window.isOpen())
    {
        sf::Event event;
               
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
                //---------------------Arrow keys----------------------
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        cout << "Left"<< endl;
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        cout << "Right"<< endl;
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        cout << "Up"<< endl;
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                        cout << "Down"<< endl;
                }
                if(!texture.loadFromFile("9452.jpg")){
                        cout << "Error" << endl;
                }
                //----------------------------------------------------
        window.clear();
                texture.create(50,50);
        window.display();
    }

    return 0;
}
« Last Edit: September 29, 2013, 07:32:55 pm by captaindarkness »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10998
    • View Profile
    • development blog
    • Email
Re: loadFromFile issues
« Reply #1 on: September 28, 2013, 02:48:03 pm »
The crash my originate from mixing debug and release modes. Make sure to link against the debug libraries (-d suffix) in debug mode and against the release libraries (no suffix) in release mode, as described in the tutorial.

The relative path location goes from the working directory. This can be set in Visual Studio and is by default in the directory where the project file is located. Later on, when you're running the application not through Visual Studio, you want to put the images next to the executable.

// MyFirstGame.vcxproj
// img/myimage.png
sf::Texture texture;
texture.loadFromFile("img/myimage.png");

Also I'd advise against using namespace std; in global scope or in header files. It can lead to odd name clashes and the origin of some functions can be hard to understand. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

captaindarkness

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: loadFromFile issues
« Reply #2 on: September 28, 2013, 02:59:34 pm »
I did the tutorial on how to get it working and i'm guessing that if i got the Green cirle in the installation tutorial to work that the debug and release was working.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10998
    • View Profile
    • development blog
    • Email
Re: loadFromFile issues
« Reply #3 on: September 28, 2013, 03:16:59 pm »
i'm guessing that if i got the Green cirle in the installation tutorial to work that the debug and release was working.
Don't guess, check! Programming is not about assuming and guess, but it's about doing things with certainty. ;)
The issue with mixing debug and release lies within the runtime library, that is the issue is actually in mixing different versions of the runtime library and with the green circle example I think nothing of the runtime library gets used, but by loading a texture you'll end up using std::string which most often crashes the application when using different versions of the runtime lib/mixing debug and release modes..

Otherwise you can also start the application through the debugger and figure out where the issue originates from. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

captaindarkness

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: loadFromFile issues
« Reply #4 on: September 29, 2013, 02:43:35 pm »
UPDATE:

So i found this really good Guide on how to install SFML. http://sfmlcoder.wordpress.com/2011/05/18/creating-a-first-sfml-project/

Really good and when I try to run this:
 #include <SFML/Graphics.hpp>
 
 int main()
 {
     sf::RenderWindow window(sf::VideoMode(800, 600), "First SFML Project");
         sf::Texture texture;
         texture.loadFromFile("C:/Users/username/Documents/Visual Studio 2012/Projects/SFML project/img/9452.jpg");
     while (window.isOpen())
     {
         sf::Event event;
         while (window.pollEvent(event))
         {
             switch (event.type)
             {
                 case sf::Event::Closed:
                     window.close();
                     break;
             }
         }
                //---------------------Arrow keys----------------------
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                       
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                       
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                       
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                       
                }
                //-----------------------------------------------------
                if(!texture.loadFromFile("C:/Users/username/Documents/Visual Studio 2012/Projects/SFML project/img/9452.jpg")){
                       
                }
                 window.clear(sf::Color(0, 255, 255));
                 texture.create(50,50);
         window.display();
     }
 
     return 0;
 }

I get no Error which I take as the file was located without any problems, though it doesn't show it in the window which might be that i'm missing something.

EDIT:

Made a Sprite and got the image loaded :)

Thanks for the help! :)
« Last Edit: September 29, 2013, 02:52:40 pm by captaindarkness »

 

anything