🛠️🐜 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.

122 lines
3.4 KiB

  1. /*
  2. Copyright (C) 1997-2019 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 %d : 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. Uint32 start32, now32;
  36. Uint64 start, now;
  37. /* Enable standard application logging */
  38. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  39. if (SDL_Init(SDL_INIT_TIMER) < 0) {
  40. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  41. return (1);
  42. }
  43. /* Start the timer */
  44. desired = 0;
  45. if (argv[1]) {
  46. desired = atoi(argv[1]);
  47. }
  48. if (desired == 0) {
  49. desired = DEFAULT_RESOLUTION;
  50. }
  51. t1 = SDL_AddTimer(desired, ticktock, NULL);
  52. /* Wait 10 seconds */
  53. SDL_Log("Waiting 10 seconds\n");
  54. SDL_Delay(10 * 1000);
  55. /* Stop the timer */
  56. SDL_RemoveTimer(t1);
  57. /* Print the results */
  58. if (ticks) {
  59. SDL_Log("Timer resolution: desired = %d ms, actual = %f ms\n",
  60. desired, (double) (10 * 1000) / ticks);
  61. }
  62. /* Test multiple timers */
  63. SDL_Log("Testing multiple timers...\n");
  64. t1 = SDL_AddTimer(100, callback, (void *) 1);
  65. if (!t1)
  66. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,"Could not create timer 1: %s\n", SDL_GetError());
  67. t2 = SDL_AddTimer(50, callback, (void *) 2);
  68. if (!t2)
  69. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,"Could not create timer 2: %s\n", SDL_GetError());
  70. t3 = SDL_AddTimer(233, callback, (void *) 3);
  71. if (!t3)
  72. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,"Could not create timer 3: %s\n", SDL_GetError());
  73. /* Wait 10 seconds */
  74. SDL_Log("Waiting 10 seconds\n");
  75. SDL_Delay(10 * 1000);
  76. SDL_Log("Removing timer 1 and waiting 5 more seconds\n");
  77. SDL_RemoveTimer(t1);
  78. SDL_Delay(5 * 1000);
  79. SDL_RemoveTimer(t2);
  80. SDL_RemoveTimer(t3);
  81. start = SDL_GetPerformanceCounter();
  82. for (i = 0; i < 1000000; ++i) {
  83. ticktock(0, NULL);
  84. }
  85. now = SDL_GetPerformanceCounter();
  86. SDL_Log("1 million iterations of ticktock took %f ms\n", (double)((now - start)*1000) / SDL_GetPerformanceFrequency());
  87. SDL_Log("Performance counter frequency: %"SDL_PRIu64"\n", (unsigned long long) SDL_GetPerformanceFrequency());
  88. start32 = SDL_GetTicks();
  89. start = SDL_GetPerformanceCounter();
  90. SDL_Delay(1000);
  91. now = SDL_GetPerformanceCounter();
  92. now32 = SDL_GetTicks();
  93. SDL_Log("Delay 1 second = %d ms in ticks, %f ms according to performance counter\n", (now32-start32), (double)((now - start)*1000) / SDL_GetPerformanceFrequency());
  94. SDL_Quit();
  95. return (0);
  96. }
  97. /* vi: set ts=4 sw=4 expandtab: */