25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

94 lines
2.2 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2010 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * This library is free software. It comes without any warranty, to
  7. * the extent permitted by applicable law. You can redistribute it
  8. * and/or modify it under the terms of the Do What The Fuck You Want
  9. * To Public License, Version 2, as published by Sam Hocevar. See
  10. * http://sam.zoy.org/wtfpl/COPYING for more details.
  11. */
  12. /*
  13. * This file contains simple timer routines.
  14. */
  15. #include "config.h"
  16. #if !defined(__KERNEL__)
  17. # include <stdlib.h>
  18. # if defined(HAVE_SYS_TIME_H)
  19. # include <sys/time.h>
  20. # endif
  21. # include <time.h>
  22. # if defined(USE_WIN32)
  23. # include <windows.h>
  24. # endif
  25. # if defined(HAVE_UNISTD_H)
  26. # include <unistd.h>
  27. # endif
  28. #endif
  29. #include "caca.h"
  30. #include "caca_internals.h"
  31. void _caca_sleep(int usec)
  32. {
  33. #if defined(HAVE_USLEEP)
  34. usleep(usec);
  35. #elif defined(HAVE_SLEEP)
  36. Sleep((usec + 500) / 1000);
  37. #else
  38. /* SLEEP */
  39. #endif
  40. }
  41. int _caca_getticks(caca_timer_t *timer)
  42. {
  43. #if defined(HAVE_GETTIMEOFDAY)
  44. struct timeval tv;
  45. #elif defined(USE_WIN32)
  46. static __int64 freq = -1; /* FIXME: can this move to caca_context? */
  47. __int64 usec;
  48. #endif
  49. int ticks = 0;
  50. int new_sec, new_usec;
  51. #if defined(HAVE_GETTIMEOFDAY)
  52. gettimeofday(&tv, NULL);
  53. new_sec = tv.tv_sec;
  54. new_usec = tv.tv_usec;
  55. #elif defined(USE_WIN32)
  56. if(freq == -1)
  57. {
  58. if(!QueryPerformanceFrequency((LARGE_INTEGER *)&freq))
  59. freq = 0;
  60. }
  61. QueryPerformanceCounter((LARGE_INTEGER *)&usec);
  62. new_sec = (int)(usec * 1000000 / freq / 1000000);
  63. new_usec = (int)((usec * 1000000 / freq) % 1000000);
  64. #endif
  65. if(timer->last_sec != 0)
  66. {
  67. /* If the delay was greater than 60 seconds, return 10 seconds
  68. * otherwise we may overflow our ticks counter. */
  69. if(new_sec >= timer->last_sec + 60)
  70. ticks = 60 * 1000000;
  71. else
  72. {
  73. ticks = (new_sec - timer->last_sec) * 1000000;
  74. ticks += new_usec;
  75. ticks -= timer->last_usec;
  76. }
  77. }
  78. timer->last_sec = new_sec;
  79. timer->last_usec = new_usec;
  80. return ticks;
  81. }