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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - billy.spiva

Pages: [1]
1
General / Re: Window flickers black during animation
« 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!

2
General / 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();
}

 

3
Adding the break keyword worked!

while(window.pollEvent(event))
    {
        if(event.type==sf::Event::Closed)
        {
            Run=false;
        }
        else if((event.type==sf::Event::KeyPressed)&&(event.key.code==sf::Keyboard::Escape))
        {
            Run=false;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
            Player1.MR=true;
            Player1.Id=false;
            if (NZ>0.1f)//was 0.05f
            {
                Next=true;
                Player1.Animate_Self(Next);
                clock.restart();
            }
            break;
        }
        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();
        }
    }

 

4
Update:

After doing a little more work on the code, The event loop is processing two events while the key is held down in one loop of the main while statement before the Display_Main function is called.

Is there a way, using pollEvents, to get it to only process one event each loop?

I'm going to try using the break keyword in the event loop, see if that helps.

5
Okay,  I'm getting really stuttering animations using a sprite sheet.

This is in main where I call my functions to check the keyboard events, and then display everything.

else
        {
            Keyboard_Control(window,Run,event,NewGame,Pause,text,Player1,clock);
            Display_Main(window,Player1);
        }
 

Here is my functions to animate and display the sprite.

void Entity::Display_Self(sf::RenderWindow& window)
{
    sprite.setPosition(sf::Vector2f(X,Y));
    window.draw(sprite);
    std::cout<<"XR displayed is "<<std::to_string(XR)<<std::endl;
}

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));
    std::cout<<"XR animated is "<<std::to_string(XR)<<std::endl;
}
 

The Display_Self is called in Display_Main, and the Animate_Self is called under the Keyboard_Control.

I used the couts in each to generate some data to show me what was happening, so, I'll post that too.

XR displayed is 128
XR displayed is 128
XR displayed is 128
XR displayed is 128
XR displayed is 128
XR animated is 192
XR animated is 256
XR displayed is 256
XR displayed is 256
XR displayed is 256
XR displayed is 256
XR displayed is 256
XR animated is 64
XR animated is 128
XR displayed is 128
XR displayed is 128
XR displayed is 128
XR displayed is 128
XR displayed is 128
XR displayed is 128
XR animated is 192
XR animated is 256
XR displayed is 256
XR displayed is 256
XR displayed is 256
XR displayed is 256
XR displayed is 256
 

The problem, to me, appears to be that the keyboard events are getting checked twice, without a call to display being made, followed by several calls to display with no keyboard event checks in between.  The functions are supposed to be called one right after the other, and I don't know what is going on here!

Let me know if you need any more information.  Thanks.

Pages: [1]
anything