🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.2 KiB

  1. /*
  2. Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* Test program to check the resolution of the SDL timer on the current
  11. platform
  12. */
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include "SDL.h"
  16. #define DEFAULT_RESOLUTION 1
  17. static int ticks = 0;
  18. static Uint32 SDLCALL
  19. ticktock(Uint32 interval, void *param)
  20. {
  21. ++ticks;
  22. return (interval);
  23. }
  24. static Uint32 SDLCALL
  25. callback(Uint32 interval, void *param)
  26. {
  27. SDL_Log("Timer %" SDL_PRIu32 " : param = %d\n", interval, (int) (uintptr_t) param);
  28. return interval;
  29. }
  30. int
  31. main(int argc, char *argv[])
  32. {
  33. int i, desired;
  34. SDL_TimerID t1, t2, t3;
  35. Uint64 start64, now64;
  36. Uint32 start32, now32;
  37. Uint64 start, now;
  38. /* Enable standard application logging */
  39. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  40. if (SDL_Init(SDL_INIT_TIMER) < 0) {
  41. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  42. return (1);
  43. }
  44. if (SDL_getenv("SDL_TESTS_QUICK") != NULL) {
  45. SDL_Log("Not running slower tests");
  46. SDL_Quit();
  47. return 0;
  48. }
  49. /* Verify SDL_GetTicks* acts monotonically increasing, and not erratic. */
  50. SDL_Log("Sanity-checking GetTicks\n");
  51. for (i = 0; i < 1000; ++i) {
  52. start64 = SDL_GetTicks64();
  53. start32 = SDL_GetTicks();
  54. SDL_Delay(1);
  55. now64 = SDL_GetTicks64() - start64;
  56. now32 = SDL_GetTicks() - start32;
  57. if (now32 > 100 || now64 > 100) {
  58. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "testtimer.c: Delta time erratic at iter %d. Delay 1ms = %d ms in ticks, %d ms in ticks64\n", i, (int)now32, (int)now64);
  59. SDL_Quit();
  60. return 1;
  61. }
  62. }
  63. /* Start the timer */
  64. desired = 0;
  65. if (argv[1]) {
  66. desired = SDL_atoi(argv[1]);
  67. }
  68. if (desired == 0) {
  69. desired = DEFAULT_RESOLUTION;
  70. }
  71. t1 = SDL_AddTimer(desired, ticktock, NULL);
  72. /* Wait 10 seconds */
  73. SDL_Log("Waiting 10 seconds\n");
  74. SDL_Delay(10 * 1000);
  75. /* Stop the timer */
  76. SDL_RemoveTimer(t1);
  77. /* Print the results */
  78. if (ticks) {
  79. SDL_Log("Timer resolution: desired = %d ms, actual = %f ms\n",
  80. desired, (double) (10 * 1000) / ticks);
  81. }
  82. /* Test multiple timers */
  83. SDL_Log("Testing multiple timers...\n");
  84. t1 = SDL_AddTimer(100, callback, (void *) 1);
  85. if (!t1)
  86. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,"Could not create timer 1: %s\n", SDL_GetError());
  87. t2 = SDL_AddTimer(50, callback, (void *) 2);
  88. if (!t2)
  89. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,"Could not create timer 2: %s\n", SDL_GetError());
  90. t3 = SDL_AddTimer(233, callback, (void *) 3);
  91. if (!t3)
  92. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,"Could not create timer 3: %s\n", SDL_GetError());
  93. /* Wait 10 seconds */
  94. SDL_Log("Waiting 10 seconds\n");
  95. SDL_Delay(10 * 1000);
  96. SDL_Log("Removing timer 1 and waiting 5 more seconds\n");
  97. SDL_RemoveTimer(t1);
  98. SDL_Delay(5 * 1000);
  99. SDL_RemoveTimer(t2);
  100. SDL_RemoveTimer(t3);
  101. start = SDL_GetPerformanceCounter();
  102. for (i = 0; i < 1000000; ++i) {
  103. ticktock(0, NULL);
  104. }
  105. now = SDL_GetPerformanceCounter();
  106. SDL_Log("1 million iterations of ticktock took %f ms\n", (double)((now - start)*1000) / SDL_GetPerformanceFrequency());
  107. SDL_Log("Performance counter frequency: %"SDL_PRIu64"\n", SDL_GetPerformanceFrequency());
  108. start64 = SDL_GetTicks64();
  109. start32 = SDL_GetTicks();
  110. start = SDL_GetPerformanceCounter();
  111. SDL_Delay(1000);
  112. now = SDL_GetPerformanceCounter();
  113. now64 = SDL_GetTicks64();
  114. now32 = SDL_GetTicks();
  115. SDL_Log("Delay 1 second = %d ms in ticks, %d ms in ticks64, %f ms according to performance counter\n", (int) (now32-start32), (int) (now64-start64), (double)((now - start)*1000) / SDL_GetPerformanceFrequency());
  116. SDL_Quit();
  117. return (0);
  118. }
  119. /* vi: set ts=4 sw=4 expandtab: */