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

Author Topic: Window flickers black during animation  (Read 1168 times)

0 Members and 1 Guest are viewing this topic.

billy.spiva

  • Newbie
  • *
  • Posts: 5
    • View Profile
Window flickers black during animation
« on: January 27, 2024, 03:17:30 am »
When my sprite is animating the window will flicker black, as if one frame it decides to clear the screen and then forgets to draw before displaying. 

I made a very short youtube video of the problem.



Here is the main event loop

while(Run==true)
    {
        while(window.pollEvent(event))
        {
            if(NewGame==true)
            {
                Quit=Splash(window,desktop,NewGame,text);
                if(Quit==true)
                {
                    return 0;
                }
            }
            NZ=clock.getElapsedTime().asSeconds();
            Next=false;
            if(event.type==sf::Event::Closed)
            {
                return 0;
            }
            else if((event.type==sf::Event::KeyPressed)&&(event.key.code==sf::Keyboard::Escape))
            {
                return 0;
            }
            else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
            {
                Player1.MR=true;
                Player1.Id=false;
                if (NZ>0.05f)
                {
                    Next=true;
                    Player1.Animate_Self(Next);
                    clock.restart();
                }
            }
            else if((event.type==sf::Event::KeyReleased)&&(event.key.code==sf::Keyboard::D))
            {
                Player1.MR=false;
                Player1.Id=true;
                Player1.Animate_Self(Next);
                clock.restart();
            }
            Display_Main(window,Player1);
        }
    }
 

Here is the relevant animation code

void Entity::Display_Self(sf::RenderWindow& window)
{
    sprite.setPosition(sf::Vector2f(X,Y));
    window.draw(sprite);
}

void Entity::Animate_Self(bool& Next)
{
    if(MR==true)
    {
        if(Next==true)
        {
            XR+=64;
            if(XR>256)
            {
                XR=64;
            }
        }
    }
    else if(Id==true)
    {
        XR=0;
    }
    sprite.setTextureRect(sf::IntRect(XR,YR,64,64));
}
 

Here is my call to display everything

void Display_Main(sf::RenderWindow& window,Player& Player1)
{
    window.clear(sf::Color(0,0,0,255));
    Player1.Display_Self(window);
    window.display();
}

 

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Window flickers black during animation
« Reply #1 on: January 27, 2024, 03:39:38 am »
What does the animation texture look like? When moving, the code is cycling through top left corners of frames starting at 64, 128, 192 and 256. Do you have 4 frames of the character with those coordinates? My guess is maybe theres only 3 frames for movement and the if(XR>256) part should be if(XR>=256) so it cycles 64, 128, 192

billy.spiva

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Window flickers black during animation
« Reply #2 on: January 27, 2024, 03:51:18 am »
What does the animation texture look like? When moving, the code is cycling through top left corners of frames starting at 64, 128, 192 and 256. Do you have 4 frames of the character with those coordinates? My guess is maybe theres only 3 frames for movement and the if(XR>256) part should be if(XR>=256) so it cycles 64, 128, 192

OMG!

How did I not see that!  There are three frames, 0 is the idle frame. 

It should be 64, 128, 192, and then back to 64.

I gotta change that code.

Thank you!

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Window flickers black during animation
« Reply #3 on: January 27, 2024, 03:54:02 am »
Cool. :)

 

anything