有没有比 SDL_Delay 更精确地延迟程序的方法?
Is there a way to delay a program more precisely than with SDL_Delay?
我发现 SDL_Delay
并不完全准确。程序等待的时间可能比作为参数传递给函数的值多 1 或 2 毫秒。我读到这可能会发生,具体取决于所使用的 OS。也许对其他人来说差距更小或更大。
要制作每秒显示约 60 帧的程序,需要在每帧之间调用 SDL_Delay
,以便程序等待 17 毫秒(1000 / 60 = 16.6666 ...) ,或更少取决于自上次调用 SDL_Delay
以来其他操作所花费的时间。但是,通过使用这个函数,每帧之间的时间会略有不同,有时是 17ms,有时是 18ms,有时是 19ms...我想知道是否有任何方法可以重现 SDL_Delay
的行为,但在一种更精确的方式,而不会带来差距带来的不便。
是的,有一些方法,但它变得特定于操作系统。
在Linux上,阅读time(7), nanosleep(2), poll(2) (or perhaps the old select(2)), clock_gettime(2), timer_create(2) (or perhaps timerfd_create(2))
另请阅读 Advanced Linux Programming (but some recent Linux features are not mentioned there, so see also intro(2) and syscalls(2)).
也许您也可能对 signal(7) (notably with SIGALRM
and setitimer(2)), but then be sure to read signal-safety(7)
感兴趣
在其他操作系统上是不同的,你需要了解一下。
当然细节也和硬件有关(阅读HPET, APIC timer, PIT)
SDL free software and has (like all graphics toolkits) some event loop (see SDL_WaitEventTimeout) probably around poll(2) or select(2) Linux,因此您可以深入了解实施。
我发现 SDL_Delay
并不完全准确。程序等待的时间可能比作为参数传递给函数的值多 1 或 2 毫秒。我读到这可能会发生,具体取决于所使用的 OS。也许对其他人来说差距更小或更大。
要制作每秒显示约 60 帧的程序,需要在每帧之间调用 SDL_Delay
,以便程序等待 17 毫秒(1000 / 60 = 16.6666 ...) ,或更少取决于自上次调用 SDL_Delay
以来其他操作所花费的时间。但是,通过使用这个函数,每帧之间的时间会略有不同,有时是 17ms,有时是 18ms,有时是 19ms...我想知道是否有任何方法可以重现 SDL_Delay
的行为,但在一种更精确的方式,而不会带来差距带来的不便。
是的,有一些方法,但它变得特定于操作系统。
在Linux上,阅读time(7), nanosleep(2), poll(2) (or perhaps the old select(2)), clock_gettime(2), timer_create(2) (or perhaps timerfd_create(2))
另请阅读 Advanced Linux Programming (but some recent Linux features are not mentioned there, so see also intro(2) and syscalls(2)).
也许您也可能对 signal(7) (notably with SIGALRM
and setitimer(2)), but then be sure to read signal-safety(7)
在其他操作系统上是不同的,你需要了解一下。
当然细节也和硬件有关(阅读HPET, APIC timer, PIT)
SDL free software and has (like all graphics toolkits) some event loop (see SDL_WaitEventTimeout) probably around poll(2) or select(2) Linux,因此您可以深入了解实施。