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

Author Topic: (SOLVED - WorkAround) - [THOR2] - Questions about the animation class.  (Read 5438 times)

0 Members and 1 Guest are viewing this topic.

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
I have this code snipit... and this is working fine.. like you press the Q button the animation plays.. but I thought I would be able to use it in the do{}while() loop to make it autoplay before any pollevents are checked.

This animation is for a logo tumble at the start of the game.

Any ideas?

void SplashScreen::Show(sf::RenderWindow& rw){

        _sprite.setTexture(*TextureManager::getInstance().rSplashScreen());
        _sprite.setTextureRect(sf::IntRect(0,0,844,514)); //Screen
        _sprite.setPosition(220,23);

        initAnimation();
/*
                do{
                        std::cout<<"Animation Tester\n";
                        animSign.playAnimation("FaSign");
                        animSign.update(frameClock.restart());
                        animSign.animate(_Sign);
                        rw.draw(_Sign);
                        rw.display();
                }while(animSign.isPlayingAnimation());*/



        sf::Event eventSplash;
        while(true)
        {
                while(rw.pollEvent(eventSplash))
                {
                        if ((eventSplash.type == sf::Event::KeyPressed) && (eventSplash.key.code == sf::Keyboard::Q))
                        {
                                animSign.playAnimation("FaSign");
                        }                      
                }
                animSign.update(frameClock.restart());
                animSign.animate(_Sign);
                rw.draw(_Sign);
                rw.display();
        }
       
}

If I change some of the code to this...

        sf::Event eventSplash;
        while(true)
        {
                while(rw.pollEvent(eventSplash))
                {
                        animSign.playAnimation("FaSign");                    
                }
                animSign.update(frameClock.restart());
                animSign.animate(_Sign);
                rw.draw(_Sign);
                rw.display();
        }

Then it plays fine, but it will play form the start every time I move my mouse inside the window... if I remove the pollevent and place the it in the while loop (what I was trying to do with the do{}while() loop then nothing happens like b4..

        sf::Event eventSplash;
        while(true)
        {
                animSign.playAnimation("FaSign");
                animSign.update(frameClock.restart());
                animSign.animate(_Sign);
                rw.draw(_Sign);
                rw.display();
        }

 
« Last Edit: June 22, 2012, 07:50:27 pm by aNewHobby »
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Re: [THOR2] - Questions about the animation class.
« Reply #1 on: June 22, 2012, 03:43:11 pm »
In your second code snippit you are restarting the animation(by calling playAnimation) every time you receive an event, any event(mouse movements, key presses, window resizes, etc.).

In your last example you are restarting the animation every loop(you're just playing the first frame over and over again). You should only call playAnimation once per play through.

I think you're trying to create something like this:
        sf::Event eventSplash;
        animSign.playAnimation("FaSign"); //start animation
        while(animSign.isPlaying())
        {
                while(rw.pollEvent(eventSplash))
                {
                        //handle events like resize or minimize.  No reason for any animation logic to go
                        //here unless you're collecting input.            
                }

                rw.clear();
                animSign.update(frameClock.restart());  //send frame time
                animSign.animate(_Sign); //move to next animation frame
                rw.draw(_Sign);
                rw.display();
        }

       //animation complete, move on to next gamestate

Disclaimer: I haven't used thor::animation yet, but this looks like it would create the desired behaviour based on the syntax.

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Re: [THOR2] - Questions about the animation class.
« Reply #2 on: June 22, 2012, 03:54:50 pm »
yes, that is exactly what is happening.. as in it is stuck on teh first frame.. but your code dose not seam to help unfortunately :(
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Re: [THOR2] - Questions about the animation class.
« Reply #3 on: June 22, 2012, 04:22:28 pm »
I kind of have it working by copying the code directly from the example file and modding it a bit...

        animSign.playAnimation("FaSign", true);
        for (;;)
        {

                // Handle events
                sf::Event eventSplash;
                while (rw.pollEvent(eventSplash))
                {
                        if (eventSplash.type == sf::Event::KeyPressed)
                        {
                                // stuff would go in here
                        }
                        else if (eventSplash.type == sf::Event::Closed)
                        {
                                // stuff would go here
                        }
                }

                // Update animator and apply current animation state to the sprite
                animSign.update(frameClock.restart());
                animSign.animate(_Sign);

                // Draw everything
                rw.clear(sf::Color::Black);
                rw.draw(_Sign);
                rw.display();
        }

Now this works fine.... but if I change the play animation thing to ...

        animSign.playAnimation("FaSign", false);

// or

        animSign.playAnimation("FaSign");

then it is stuck at the 1st frame again.... so unless you want an infinity animation loop.. .? ?
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

Celtic Minstrel

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: [THOR2] - Questions about the animation class.
« Reply #4 on: June 22, 2012, 04:44:20 pm »
Just for the record, the name "_Sign" is reserved by the standard for future expansions, since it begins with an underscore followed by an uppercase letter. You're probably fine with it for now, but if it's not too much trouble I'd suggest lowercasing it.

More information here if you're curious.

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Re: [THOR2] - Questions about the animation class.
« Reply #5 on: June 22, 2012, 04:45:59 pm »
I kinda got it to work.. seamed there was a problem with my initialisation functions.. once i moved all the animation init stuff (as in .addFrame etc etc) then it works as expected... ALMOST :) heh heh

        animSign.playAnimation("FaSign");
        for (;;)
        {
                // Update animator and apply current animation state to the sprite
                animSign.update(frameClock.restart());
                animSign.animate(_sign);

                // Draw everything
                rw.clear(sf::Color(50, 50, 50));
                rw.draw(_sign);
                rw.display();

                std::cout<<animSign.isPlayingAnimation()<<"\n";
        }
        std::cout<<"Out of loop\n";

This will now work.. BUT the animSign.isPlayingAnimation() is always returning 1... it is never switching to false and reporting the animation as stopped playing.
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Re: [THOR2] - Questions about the animation class.
« Reply #6 on: June 22, 2012, 04:59:12 pm »
I think this must be the Thor lib itself as it seams like the isPlayingAnimation() is returning 1 no matter what even in the demo provided?

<--- EDIT

if you just add
std::cout<<animator.isPlayingAnimation()<<"\n";

To the example "Animation.cpp" that comes with Thor at the base of the while poll event.. ... then as you move your mouse about even though you are not playing any animations.. it still reports 1 :(
« Last Edit: June 22, 2012, 05:06:18 pm by aNewHobby »
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [THOR2] - Questions about the animation class.
« Reply #7 on: June 22, 2012, 05:32:40 pm »
Yes, the behavior was confusing, because if you stop the animator and there's a default animation (playing in idle mode), then isPlayingAnimation() still returns true. I changed that, thanks for the hint.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: [THOR2] - Questions about the animation class.
« Reply #8 on: June 22, 2012, 06:57:25 pm »
Yes, the behavior was confusing, because if you stop the animator and there's a default animation (playing in idle mode), then isPlayingAnimation() still returns true. I changed that, thanks for the hint.

And I just recompiled everything a few hours ago. ::) :P

aNewHobby: I guess your infinite loop were just for testing and debugging, but with real code try to avoid infinite loops like for(;;) or while(1), because it's harder to maintaine that loop and you're breaking the 'flow'.
Is your problem now resolved?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Celtic Minstrel

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: [THOR2] - Questions about the animation class.
« Reply #9 on: June 22, 2012, 07:44:12 pm »
Infinite loops have their uses. So do break and continue. I'd never recommend against using the latter (in the general case at least), and for the former I'll only say "exercise caution" since, being infinite, they have the potential to hang your application.

Still, if you can do something like "dool done = false; while(!done) { /* ...stuff... */ }", then that's probably better.

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Re: [THOR2] - Questions about the animation class.
« Reply #10 on: June 22, 2012, 07:49:23 pm »
For now I just removed the default animation completely... then everything worked as planned.. It also had the bonus of pausing the animation on the last from... witch was nice...


anyway if you like you can check out my test animation demo thing @ MediaFire
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)