[ad_1]
I’ve the next class that animates a ‘fade to black’ impact:
#pragma as soon as
#embody <SDL.h>
class Renderer {
public:
express Renderer(SDL_Renderer *renderer) : renderer(renderer) {}
void Update(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);
}
};
void FadeOut(SDL_Rect rect, int length = 1500) {
fadeout = true;
secondeSteps = (255.0f/length)*1000; // opacity change per seconde
display = rect;
}
non-public:
float fadeValue {};
float secondeSteps{};
bool fadeout = false;
SDL_Rect display;
double dt;
SDL_Renderer *renderer;
};
It is run each body till a desired worth is reached, after which a flag is about to point we’re finished (fadeout = false;
).
But what I would really like is to go a callback, in order that after the impact is completed, one thing like a GameState is modified to a brand new worth, like this:
recreationRenderer->FadeOut(SDL_Rect{0,0,windowWidth,windowHeight}, GameState::STATE_LOAD);
Right now, the Renderer
class is a unique_ptr
inside my Game
class, however has no information of the Game object itself.
I’ve seen many recommendations about utilizing Lambdas or passing operate pointers, however I’m unsure what to choose, and what’s the most trendy resolution for,say, c++ compiler 20.
There’s additionally async (https://en.cppreference.com/w/cpp/thread/async) capabilities, might that be helpful on this situation right here as effectively?
Or perhaps it is simply as simple as passing the present occasion of my Game
to the Renderer
class? That appears type of messy.
Would gladly hear some recommendations about how you can method this.
[ad_2]