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

Author Topic: Problem with SFML's tutorial AnimatedSprite by Foaly  (Read 4554 times)

0 Members and 1 Guest are viewing this topic.

smguyk

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Problem with SFML's tutorial AnimatedSprite by Foaly
« on: March 03, 2014, 07:58:19 pm »
I read this tutorial https://github.com/SFML/SFML/wiki/Source:-AnimatedSprite and tried to implement it myself, but it's not completely working.
Everything is fine except for this line from the example code:

animatedSprite.play(*currentAnimation);

When I run the program with that line, the following error occurs: http://i.imgur.com/ckpzfdk.png
Does anyone know how to fix that? When I for example use the following code instead of the code above everything runs fine

animatedSprite.play(walkingAnimationLeft);

But of course that's not what I want. I want the play the currentAnimation.

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Problem with SFML's tutorial AnimatedSprite by Foaly
« Reply #1 on: March 03, 2014, 08:47:21 pm »
Try to debug your program. If you press retry on the window you start debugging. Check your call stack to see where the error occurred in the code.

Also, look at the message: Expression: vector subscript out of range. It should give a decent hint to what the error could be.

smguyk

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Re: Problem with SFML's tutorial AnimatedSprite by Foaly
« Reply #2 on: March 03, 2014, 08:58:03 pm »
Thanks for the reply. This is the call stack:

Code: [Select]
> msvcp110d.dll!std::_Debug_message(const wchar_t * message, const wchar_t * file, unsigned int line) Line 15 C++
  test.exe!std::vector<sf::Rect<int>,std::allocator<sf::Rect<int> > >::operator[](unsigned int _Pos) Line 1124 C++
  test.exe!Animation::getFrame(unsigned int index) Line 22 C++
  test.exe!AnimatedSprite::setFrame(unsigned int newFrame, bool resetTime) Line 68 C++
  test.exe!AnimatedSprite::setAnimation(const Animation & animation) Line 39 C++
  test.exe!AnimatedSprite::play(const Animation & animation) Line 49 C++
  test.exe!Game::update(sf::Time deltaTime, float fps) Line 104 C++
  test.exe!MainClass::operator()() Line 111 C++
  test.exe!main(int argc, char * * argv) Line 15 C++
  test.exe!_WinMain@16() Unknown
  test.exe!__tmainCRTStartup() Line 528 C
  test.exe!WinMainCRTStartup() Line 377 C
  kernel32.dll!755a336a() Unknown
  [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
  ntdll.dll!77939f72() Unknown
  ntdll.dll!77939f45() Unknown

It looks like the error is in the Animation class, maybe the
std::vector<sf::IntRect> m_frames;
?

I use the same code from the tutorial so I don't really know what's wrong
« Last Edit: March 03, 2014, 09:06:17 pm by smguyk »

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Problem with SFML's tutorial AnimatedSprite by Foaly
« Reply #3 on: March 03, 2014, 10:12:48 pm »
So now you have the call stack you can trace back to where the fault has started. The error you are getting has typically this syntax
vectorObj[i];
where i is equal or bigger than the size of the vector vectorObj. In your case it's in the function AnimatedSprite::getFrame. The argument given exceeds the vector dimensions. So try and keep tracing back to the point where the variable was set and see if you can find it. You need to check the size of your vector and the index for it of course. You can see these variables using the Locals or Auto's in MSVC.

I'm guessing your animation the pointer currentAnimation points to is empty.

smguyk

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Re: Problem with SFML's tutorial AnimatedSprite by Foaly
« Reply #4 on: March 04, 2014, 02:52:23 pm »
I think the problem are these two functions from the Animation class.
const sf::IntRect &getFrame(std::size_t n) const;
void addFrame(sf::IntRect rect);

I used some couts to check whether frames were added when the function addFrame was called. getSize() correctly prints 4 frames after adding them to each animation. But when I cout getSize() from within
const sf::IntRect &getFrame(std::size_t n) const;
it prints 0, so obviously somehow the frames weren't added after all even though it said otherwise in the addFrame function...

It's really weird

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Problem with SFML's tutorial AnimatedSprite by Foaly
« Reply #5 on: March 05, 2014, 11:56:01 am »
Hi!
From what you wrote here I come to the same conclusion as didii. The animation that currentAnimation points to seems to be empty. Might be because the animation has gone out of scope, but it also might be something different. But without anymore information we can only guess.
Please provide us with as much information as possible (especially code). What compiler (+ version) are you using? What version of SFML? Are you using exactly the example code I wrote on the wiki? Or are you using your own code (you said you were trying to implement it youself)? If the later is true, please show us your code.

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Problem with SFML's tutorial AnimatedSprite by Foaly
« Reply #6 on: March 05, 2014, 02:12:08 pm »
Please provide us with as much information as possible (especially code).
Actually, it's preferred to give as less information possible. Just enough to demonstrate the problem: complete and minimal example code.

smguyk

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Re: Problem with SFML's tutorial AnimatedSprite by Foaly
« Reply #7 on: March 05, 2014, 02:33:14 pm »
Okay, I just created a small project that represents my code's structure.

animatedsprite.h, animatedsprite.cpp, animation.h, animation.cpp are basically the same as in the tutorial, I only made some name and formatting changes, so I'm not posting them here.

(removed the code, see my next post)

player.png is the tutorial image. I am using Microsoft Visual Studio Premium 12 version 11.0.61030.00 update 4 and SFML 2.1. Please tell me if I need to include any other info
« Last Edit: March 05, 2014, 06:35:34 pm by smguyk »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Problem with SFML's tutorial AnimatedSprite by Foaly
« Reply #8 on: March 05, 2014, 02:42:32 pm »
I'll post all files for the sake of completeness, I hope it's not too much (I tried keeping it as minimal as possible).
It's still quite a lot of unrelated code. Please read the link posted by didii again, ideal would be a single .cpp file with just a main() function (+ the existing, unchanged AnimatedSprite functionality).
« Last Edit: March 05, 2014, 02:44:04 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Problem with SFML's tutorial AnimatedSprite by Foaly
« Reply #9 on: March 05, 2014, 05:44:50 pm »
This is still to much code. Please read the post linked above. Your minimal example should only contain a main function with only the code relevant to the problem.

smguyk

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Re: Problem with SFML's tutorial AnimatedSprite by Foaly
« Reply #10 on: March 05, 2014, 06:47:34 pm »
I fixed it, it had nothing to do with the tutorial code.

I replaced
Resources *resources
in Game's constructor to
Resources &resources
and now it's working.

Looks like that was the problem, damn it...

Thank you guys so much for your help, I seriously appreciate it

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Problem with SFML's tutorial AnimatedSprite by Foaly
« Reply #11 on: March 06, 2014, 02:32:23 am »
No problem! Glad to hear that you fixed the problem and that nothing is wrong with the animation code.

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Problem with SFML's tutorial AnimatedSprite by Foaly
« Reply #12 on: March 06, 2014, 03:48:00 am »
It's one of the reasons we ask for a complete and minimal example code. In many cases you'll find your own mistake and learn more from it ;)

 

anything