+/*\r
+* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+*\r
+* This file is part of CasparCG.\r
+*\r
+* CasparCG is free software: you can redistribute it and/or modify\r
+* it under the terms of the GNU General Public License as published by\r
+* the Free Software Foundation, either version 3 of the License, or\r
+* (at your option) any later version.\r
+*\r
+* CasparCG is distributed in the hope that it will be useful,\r
+* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+* GNU General Public License for more details.\r
+\r
+* You should have received a copy of the GNU General Public License\r
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
+*\r
+*/\r
#pragma once\r
\r
+#define NOMINMAX\r
+\r
#include <windows.h>\r
+#include <Mmsystem.h>\r
\r
namespace caspar {\r
\r
-class timer\r
+class high_prec_timer\r
{\r
public:\r
- timer() : time_(timeGetTime()){}\r
-\r
- double elapsed()\r
+ high_prec_timer()\r
+ : time_(0)\r
{\r
- return static_cast<double>(timeGetTime() - time_)/1000.0;\r
}\r
- \r
- void reset()\r
- {\r
- time_ = timeGetTime();\r
- }\r
- \r
+\r
+ // Author: Ryan M. Geiss\r
+ // http://www.geisswerks.com/ryan/FAQS/timing.html\r
+ void tick(double interval)\r
+ { \r
+ auto t = ::timeGetTime();\r
+\r
+ if (time_ != 0)\r
+ {\r
+ auto ticks_to_wait = static_cast<DWORD>(interval*1000.0);\r
+ bool done = 0;\r
+ do\r
+ { \r
+ auto ticks_passed = t - time_;\r
+ auto ticks_left = ticks_to_wait - ticks_passed;\r
+\r
+ if (t < time_) // time wrap\r
+ done = 1;\r
+ if (ticks_passed >= ticks_to_wait)\r
+ done = 1;\r
+ \r
+ if (!done)\r
+ {\r
+ // if > 0.002s left, do Sleep(1), which will actually sleep some \r
+ // steady amount, probably 1-2 ms,\r
+ // and do so in a nice way (cpu meter drops; laptop battery spared).\r
+ // otherwise, do a few Sleep(0)'s, which just give up the timeslice,\r
+ // but don't really save cpu or battery, but do pass a tiny\r
+ // amount of time.\r
+ if (ticks_left > 2)\r
+ Sleep(1);\r
+ else \r
+ for (int i = 0; i < 10; ++i) \r
+ Sleep(0); // causes thread to give up its timeslice\r
+ }\r
+\r
+ t = ::timeGetTime();\r
+ }\r
+ while (!done); \r
+ }\r
+\r
+ time_ = t;\r
+ } \r
private: \r
DWORD time_;\r
};\r
\r
+\r
}
\ No newline at end of file