How to make a SDL time-based event

52 Views Asked by At

Im trying to make a timing-based event in SDL2, in which after 5 seconds, a different screen is loaded. But i have not been able to achieve that. I have tried SDL_Delay, SDL_AddTimer, but without sucess. Now i have tried this code, but also without sucess. After the 5 seconds time is up, the code in the function fEvent1() doesnt work and it doesnt change the screen. But i find it strange that after the time is up, if i move the mouse, it does change the screen!

This is the code:

Crono::Crono() {}

void Crono::starting() {
  SDL_GetTicks();
}

Uint32 Crono::cronom(){
  Uint32 uin = SDL_GetTicks();
  return uin;
}

void (*fptrEvent)();//pointer to event functions.
void (*fptrRender)();//pointer to render functions

Crono cro1;
bool boo2;
SDL_Event eve;

void fEvent1() {
   cro1.starting();
   if (boo2 == true) {
       fptrEvent = &fEvent2;//cant make this work
   }
}

void fRender1() {
   //some rendering
   if ((cro1.cronom()) > 5000) {
       boo2 = true;
   }
}

void fEvent2() {
   fptrRender = &fRender2;
   //some event
}

void fRender2() {
   //some rendering
}

int main(int argc, char* args[]) {

   Window wind(1280, 720);
   fptrEvent = &fEvent1;
   fptrRender = &fRender1;

   while (!end) {
       while (SDL_PollEvent(&eve) != 0) {
           fptrEvent();
       }
       wind.RenderDrawColor();
       wind.RenderClear();

       fptrRender();

       wind.RenderPresent();
   }
   return 0;
}
1

There are 1 best solutions below

0
Adato On BEST ANSWER

I have found the solution. It was as simple as changing the screen in the rendering function itself. I also dispensed with the bool.

void fEvent1() {
   cro1.starting();
   if ((cro1.cronom()) > 5000) {
      fptrEvent = &fEvent2;
   }
}

void fRender1() {
   //some rendering
   if ((cro1.cronom()) > 5000) {
      fptrRender = &fRende2;//now it works
   }
}