Reputation: 21
In all of my C++ SDL2 programs, there is a severe delay following the first keyboard input. After that first input, everything else seems to work just fine.
It happens even in the most bare-bones programs.
Is there something that I'm doing wrong, or is this a bug?
If it's relevant, my operating system is macOS.
And here is some code with the problem:
#include <SDL2/SDL.h>
#include <iostream>
#include <stdexcept>
int main(int argc, char *argv[])
{
if(SDL_Init(SDL_INIT_VIDEO) != 0)
{
throw std::runtime_error("SDL failed to initialize.\n");
}
SDL_Window *window = SDL_CreateWindow("App", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, NULL);
bool done = false;
unsigned start_time = 0;
unsigned tick_time = 0;
while(!done)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
done = true;
}
}
start_time = tick_time;
tick_time = SDL_GetTicks();
const unsigned delta_time = tick_time - start_time;
if(delta_time != 0)
{
if((1000 / delta_time) < 30)
{
std::cout << 1000 / delta_time << '\n';
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Upvotes: 2
Views: 255
Reputation: 388
I think you should set start_time with SDL_GetTicks() rather than set it to zero before entering to the main loop. SDL_GetTicks() returns time since SDL_Init() and it might be something else than zero at this point.
Upvotes: 1