Thursday, August 11, 2022
HomeGame Developmentc++ - Passing callback to animation operate?

c++ – Passing callback to animation operate?


I’ve the next class that animates a ‘fade to black’ impact:

#pragma as soon as
#embrace <SDL.h>

class Renderer {
public:
    specific Renderer(SDL_Renderer *renderer) : renderer(renderer) {}

    void Replace(double deltaTime) {
        this->dt = deltaTime;
    };

    void Render() {
        if (fadeout) {
            fadeValue += secondeSteps*dt;

            if (fadeValue > 255) {
                fadeValue = 255;
                fadeout = false;
                //callback?
            }
            SDL_SetRenderDrawColor(renderer, 0,0,0, (int)fadeValue);
            SDL_RenderFillRect(renderer, &display screen);
        }
    };

    void FadeOut(SDL_Rect rect, int period = 1500) {
        fadeout = true;
        secondeSteps = (255.0f/period)*1000; // opacity change per seconde
        display screen = rect;
    }

non-public:
    float fadeValue {};
    float secondeSteps{};
    bool fadeout = false;
    SDL_Rect display screen;
    double dt; 
    SDL_Renderer *renderer;
};

It’s run each body till a desired worth is reached, after which a flag is about to point we’re performed (fadeout = false;).

However what I would really like is to move a callback, in order that after the impact is finished, one thing like a GameState is modified to a brand new worth, like this:

gameRenderer->FadeOut(SDL_Rect{0,0,windowWidth,windowHeight}, GameState::STATE_LOAD);

Proper now, the Renderer class is a unique_ptr inside my Sport class, however has no information of the Sport object itself.

I’ve seen many solutions about utilizing Lambdas or passing operate pointers, however I am unsure what to select, and what’s the most fashionable answer for,say, c++ compiler 20.
There’s additionally async (https://en.cppreference.com/w/cpp/thread/async) features, may that be helpful on this state of affairs right here as nicely?

Or possibly it is simply as straightforward as passing the present occasion of my Sport to the Renderer class? That appears form of messy.

Would gladly hear some solutions about how one can method this.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments