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

Author Topic: Basic sprite problem  (Read 3018 times)

0 Members and 1 Guest are viewing this topic.

Powliq

  • Newbie
  • *
  • Posts: 5
    • View Profile
Basic sprite problem
« on: December 18, 2014, 01:01:26 am »
Hello guys, my first post.
I am pretty new in SFML (using 2.0) and I have really basic problem with sprites..
After the problem showed up in big project, I decided to simplify the problem scope to a basic project, and it still doesn't work.
Well, there is my little program:

#include <SFML/Graphics.hpp>
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");
    sf::Texture texture;
    texture.loadFromFile("img1.png");
    sf::Sprite sprite;
    sprite.setTexture(texture);
    sprite.setPosition(200, 300);

    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;
}
 

All i see is black screen..
When I'm using sprite.setTextureRect(sf::IntRect(0, 0, 50, 50)); the known white square appears.

Maybe i didn't save my img1.png at the correct place? it saved in the same directory of this .cpp file.
« Last Edit: December 18, 2014, 01:08:23 am by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Basic sprite problem
« Reply #1 on: December 18, 2014, 01:10:49 am »
Sorry for all the confusion, I deleted the double post before you removed things from here. Everything should be okay now. ;)

This example works fine for me.
Is your graphics driver up-to-date?

Maybe i didn't save my img1.png at the correct place? it saved in the same directory of this .cpp file.
Do you get a message on the console?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Basic sprite problem
« Reply #2 on: December 18, 2014, 01:15:48 am »
Could you try checking the return value for loadFromFile()?  If it failed to load for whatever reason it should return false.

Powliq

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Basic sprite problem
« Reply #3 on: December 18, 2014, 01:24:13 am »
Sorry for all the confusion, I deleted the double post before you removed things from here. Everything should be okay now. ;)

This example works fine for me.
Is your graphics driver up-to-date?

Maybe i didn't save my img1.png at the correct place? it saved in the same directory of this .cpp file.
Do you get a message on the console?

No I don't but by the way I'm getting these messages in console when i run any SFML project (alot of lines with different DLLs):
'SFMLDemo.exe' (Win32): Loaded 'C:\Windows\System32\ntdll.dll'. Cannot find or open the PDB file.
 

Could you try checking the return value for loadFromFile()?  If it failed to load for whatever reason it should return false.
Yeah, i see now its returning false.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Basic sprite problem
« Reply #4 on: December 18, 2014, 01:30:03 am »
The pdb files are a visual studio thing related to how it handles debugging information.  As far as I know that error should never affect your program's behavior.

Since it's clear the file can't be found for whatever reason, here's the standard blurb you may have seen in the SFML tutorials:
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.

tl;dr The working directory isn't what you think it is, so put the .png in different places (iirc, it should be the folders called Debug and Release).
« Last Edit: December 18, 2014, 01:32:39 am by Ixrec »

Powliq

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Basic sprite problem
« Reply #5 on: December 18, 2014, 01:47:30 am »
The pdb files are a visual studio thing related to how it handles debugging information.  As far as I know that error should never affect your program's behavior.

Since it's clear the file can't be found for whatever reason, here's the standard blurb you may have seen in the SFML tutorials:
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.

tl;dr The working directory isn't what you think it is, so put the .png in different places (iirc, it should be the folders called Debug and Release).

I tried to put it in every possible directory in my project and it still doesn't work.
When I made my first program in visual studio I modified the program configurations and all of my other projects are just a copy of the directory of the first project with additional classes.
Should I build new programs and modify them again instead of make copies of older ones?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Basic sprite problem
« Reply #6 on: December 18, 2014, 01:55:02 am »
I would say yes, build a new project.  Partly because it might help solve this issue, but also because it's important to understand how to configure your IDE/build system to handle a library.

That process is meant to be a fairly straightforward one.  As tedious and error-prone as it seems, it should become second nature after you've forced yourself to do it properly enough times.  More importantly, you're going to have to do something similar (but not quite the same) every time you add a library to your project.  Someday, you'll want to use more libraries than just SFML, and their tutorials won't be nearly as good as SFML's are.

Powliq

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Basic sprite problem
« Reply #7 on: December 18, 2014, 02:12:12 am »
I would say yes, build a new project.  Partly because it might help solve this issue, but also because it's important to understand how to configure your IDE/build system to handle a library.

That process is meant to be a fairly straightforward one.  As tedious and error-prone as it seems, it should become second nature after you've forced yourself to do it properly enough times.  More importantly, you're going to have to do something similar (but not quite the same) every time you add a library to your project.  Someday, you'll want to use more libraries than just SFML, and their tutorials won't be nearly as good as SFML's are.

You right, from now I'll build and reconfigure each and every project.
I did it and the loading function still returns false (img1.png is in every directory in project).
I think you should see this:


Edit: I'm using SFML 2.1, not 2.0 if it matters
« Last Edit: December 18, 2014, 02:18:09 am by Powliq »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Basic sprite problem
« Reply #8 on: December 18, 2014, 04:00:55 am »
It's a shot in the dark, but are you sure img1.png is the real name of your file? Some OS hide extensions, maybe your file is actually called img1.png.png  ;D
Have you tried putting your img in the same directory as the .exe and directly running the .exe without your IDE? Have you tried with another picture?

Powliq

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Basic sprite problem
« Reply #9 on: December 18, 2014, 04:22:24 am »
I built visual studio new project and used SFML 2.2 version with dynamic linking and now its working somehow  :D

Thanks everyone for help!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Basic sprite problem
« Reply #10 on: December 19, 2014, 04:10:41 pm »

You so you know for the future, if you wish to specify the working directory (the folder it starts from for relative paths when run from the IDE), you can change Working Directory, which is shown in your image (above). Remember that when you run the compiled binary/executable file directly (without the IDE), the relative path becomes that folder that that file is in (in most cases).

(click to show/hide)
« Last Edit: December 19, 2014, 04:36:54 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*