C++ DirectXTK Changing SpriteTint Over Time

76 views Asked by At

So for a University Project I am looking to try and change the tint of a sprite over a specific time frame. The aim is to make a set of rocks turn from grey to orange (simulating heating them up) over 5 seconds, then turn them from orange to grey again (simulating cooling them down) over the next 5 seconds.

This is done using the DirectXTK, SpriteBatch specifically, however I appear to be having some problems controlling the heating up and cooling down logic. Currently, the rocks heat up to orange, but do not cool back down.

The Update function for the rocks, along with a further HeatDelay function I am using to control the cooling down are are included below.

timeToChangeColour is initialised at 5.

void RockFade::Update(float timeDelta)
{

    if ((timeDelta >= timeToChangeColour) && (heatDelay == false))
    {
        this->heatUp = false;
        HeatDelay();
        timeToChangeColour = timeDelta + 10;
    }
    else if ((timeDelta < timeToChangeColour) && (heatDelay == false))
    {
        this->heatUp = true;
    }

    if (heatUp)
    {
        this->newBlue  -= 0.002f;
    }
    else
    {
        this->newBlue += 0.002f;
    }

    this->spriteTint = DirectX::SimpleMath::Color{ 1.0f, 1.0f, newBlue, 1.0f};
}

void RockFade::HeatDelay()
{
    heatDelay = true;
    Sleep(5);
    heatDelay = false;
}

Any help is greatly appreciated!

1

There are 1 answers

1
Steeve On BEST ANSWER

The biggest conceptual problem with your code is that in the function

void RockFade::Update(float timeDelta);

You seem to assume that timeDelta will be a time difference since a specified fixed time in the past, but it is actually the time difference since the last time Update was called.

Because of this the condition

timeDelta < timeToChangeColour

seem to be always true (because it seems timeToChangeColour's value exceeds one frame time).

The first step towards achieving what you are trying to do here might be to accumulate the time, and use it as your basis, e.g.:

this->myAccumulatedTime += timeDelta; // initialize it to zero