This is a problem I have been having with every program I write since I started using SDL 2. Whenever I compile my code and run my program, everything works perfectly fine until at some point (usually after 3-8 minutes of running), the program will stop responding completely and I will have to exit out of the console to close it. The code I believe is relevant is:
void MainLoop()
{
InitLoop();
while ( !QuitMain )
{
frametime = SDL_GetTicks() - start;
if ( frametime < 1000.f/FPS ) SDL_Delay( 1000.f/FPS - ( SDL_GetTicks() - start ) );
start = SDL_GetTicks();
HandleInput();
Display();
Update();
PrintSDLError();
PrintGLError();
FPSTracker.Average = (FPSTracker.Frame*FPSTracker.Average + (1000.f/frametime))/float(FPSTracker.Frame+1);
FPSTracker.Frame++;
}
}
void HandleInput()
{
while ( SDL_PollEvent(&Event) )
{
int x,y;
SDL_GetMouseState(&x,&y);
switch( Event.type )
{
case SDL_QUIT:
Close();
break;
case SDL_KEYDOWN:
KeyPress(Event.key.keysym.sym, x, y);
break;
case SDL_KEYUP:
KeyRelease(Event.key.keysym.sym, x, y);
break;
case SDL_MOUSEBUTTONDOWN:
MousePress(Event.button.button, x, y);
break;
case SDL_MOUSEBUTTONUP:
MouseRelease(Event.button.button, x, y);
break;
case SDL_MOUSEMOTION:
if ( !( Event.motion.state & SDL_BUTTON_LMASK or Event.motion.state & SDL_BUTTON_RMASK ) )
PassiveMouseMotion(x,y);
else
ActiveMouseMotion(x,y);
break;
case SDL_MOUSEWHEEL:
MouseWheel(Sign( Event.wheel.y ), x, y);
break;
case SDL_WINDOWEVENT:
if ( Event.window.type == SDL_WINDOWEVENT_RESIZED )
Resize( Event.window.data1, Event.window.data2 );
break;
case SDL_SYSWMEVENT:
{
UINT Message = Event.syswm.msg->msg.win.msg;
WPARAM wParam = Event.syswm.msg->msg.win.wParam;
LPARAM lParam = Event.syswm.msg->msg.win.lParam;
SysWM(Message, wParam, lParam);
}
break;
}
}
}
If I change SDL_PollEvent(&Event) to SDL_WaitEventTimeout(&Event,100), then the problem goes away (I did that and had the program running for about an hour without it stop responding before I decided that it solved the problem), so I believe that the problem has something to do with event handling. Also, it might be noteworthy to mention that when I use SDL_WaitEventTimeout with the second parameter being a small number (because 100 milliseconds is a long time to wait and makes the program run at like 8 FPS), the problem returns.