9
3

timer.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef TIMER_H
  17. #define TIMER_H
  18. #include "types.h"
  19. #include <chrono>
  20. // Disgrace: for windows compile
  21. #ifdef WIN32
  22. #include <WinSock2.h>
  23. #include <windows.h>
  24. int gettimeofday (timeval *tp, ...);
  25. #endif
  26. class Timer
  27. {
  28. public:
  29. Timer();
  30. Timer(int32 timer_time, bool iUseAcurateTiming = false);
  31. Timer(int32 start, int32 timer, bool iUseAcurateTiming);
  32. ~Timer() { }
  33. bool Check(bool iReset = true);
  34. void Enable();
  35. void Disable();
  36. void Start(int32 set_timer_time=0, bool ChangeResetTimer = true);
  37. void SetTimer(int32 set_timer_time=0);
  38. int32 GetRemainingTime();
  39. int32 GetElapsedTime();
  40. inline const int32& GetTimerTime() { return timer_time; }
  41. inline const int32& GetSetAtTrigger() { return set_at_trigger; }
  42. void Trigger();
  43. void SetAtTrigger(int32 set_at_trigger, bool iEnableIfDisabled = false);
  44. inline bool Enabled() { return enabled; }
  45. inline int32 GetStartTime() { return(start_time); }
  46. inline int32 GetDuration() { return(timer_time); }
  47. static const int32& SetCurrentTime();
  48. static const int32& GetCurrentTime2();
  49. static int32 GetUnixTimeStamp();
  50. private:
  51. int32 start_time;
  52. int32 timer_time;
  53. bool enabled;
  54. int32 set_at_trigger;
  55. // Tells the timer to be more acurate about happening every X ms.
  56. // Instead of Check() setting the start_time = now,
  57. // it it sets it to start_time += timer_time
  58. bool pUseAcurateTiming;
  59. // static int32 current_time;
  60. // static int32 last_time;
  61. };
  62. struct BenchTimer
  63. {
  64. typedef std::chrono::high_resolution_clock clock;
  65. BenchTimer() : start_time(clock::now()) {}
  66. void reset() { start_time = clock::now(); }
  67. // this is seconds
  68. double elapsed() { return std::chrono::duration<double>(clock::now() - start_time).count(); }
  69. private:
  70. std::chrono::time_point<std::chrono::high_resolution_clock> start_time;
  71. };
  72. #endif