]> git.sesse.net Git - casparcg/blobdiff - common/utility/timer.h
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / common / utility / timer.h
index fffed7c950c0835e53607ad880c1e726562c3489..ce399327572e9190ef87d6dfbe27e56523bb2adc 100644 (file)
@@ -1,37 +1,55 @@
+/*\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()\r
+       high_prec_timer()\r
+               : time_(0)\r
        {\r
-               QueryPerformanceFrequency(&freq_);\r
-               QueryPerformanceCounter(&time_);\r
        }\r
 \r
        // Author: Ryan M. Geiss\r
        // http://www.geisswerks.com/ryan/FAQS/timing.html\r
-       void wait(double fps)\r
+       void tick(double interval)\r
        {       \r
-               LARGE_INTEGER t;\r
-               QueryPerformanceCounter(&t);\r
+               auto t = ::timeGetTime();\r
 \r
-               if (time_.QuadPart != 0)\r
+               if (time_ != 0)\r
                {\r
-                       int ticks_to_wait = static_cast<int>(static_cast<double>(freq_.QuadPart) / fps);\r
-                       int done = 0;\r
+                       auto ticks_to_wait = static_cast<DWORD>(interval*1000.0);\r
+                       bool done = 0;\r
                        do\r
-                       {\r
-                               QueryPerformanceCounter(&t);\r
-                               \r
-                               int ticks_passed = static_cast<int>(static_cast<__int64>(t.QuadPart) - static_cast<__int64>(time_.QuadPart));\r
-                               int ticks_left = ticks_to_wait - ticks_passed;\r
+                       {                               \r
+                               auto ticks_passed = t - time_;\r
+                               auto ticks_left   = ticks_to_wait - ticks_passed;\r
 \r
-                               if (t.QuadPart < time_.QuadPart)    // time wrap\r
+                               if (t < time_)    // time wrap\r
                                        done = 1;\r
                                if (ticks_passed >= ticks_to_wait)\r
                                        done = 1;\r
@@ -44,12 +62,14 @@ public:
                                        // 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 > static_cast<int>((freq_.QuadPart*2)/1000))\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
@@ -57,8 +77,8 @@ public:
                time_ = t;\r
        }               \r
 private:       \r
-       LARGE_INTEGER freq_;\r
-       LARGE_INTEGER time_;\r
+       DWORD time_;\r
 };\r
 \r
+\r
 }
\ No newline at end of file