timeprobe.h 933 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * 2D Physic Engine
  3. * timeprobe.h: Simple time probe
  4. * Based on pikuma.com Learn Game Physics Engine Programming course.
  5. * Copyright (c) 2022 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 13/06/2022.
  8. */
  9. #ifndef PHYSICENGINE_TIMEPROBE_H
  10. #define PHYSICENGINE_TIMEPROBE_H
  11. #include <stdint.h>
  12. #include <sys/time.h>
  13. class timeProbe
  14. {
  15. private:
  16. double time;
  17. uint32_t tick;
  18. static uint32_t getMicroSecTime()
  19. {
  20. struct timeval curTime;
  21. gettimeofday(&curTime, nullptr);
  22. return ((curTime.tv_sec) * 1000 * 1000) + (curTime.tv_usec);
  23. }
  24. public:
  25. timeProbe() : time(0), tick(0) {};
  26. void start()
  27. {
  28. this->tick = this->getMicroSecTime();
  29. }
  30. void stop()
  31. {
  32. this->time = (this->time + (getMicroSecTime() - this->tick) / 1000.) / 2.0;
  33. }
  34. double get()
  35. {
  36. return this->time;
  37. }
  38. };
  39. #endif /* PHYSICENGINE_TIMEPROBE_H */