Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tgt.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2010 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * Timer implementation using timeGetTime
25  */
26 
27 // note: WinMM is delay-loaded to avoid dragging it in when this timer
28 // implementation isn't used. (this is relevant because its startup is
29 // fairly slow)
30 
31 #include "precompiled.h"
33 
35 
36 #include "lib/sysdep/os/win/win.h"
37 #include <mmsystem.h>
38 
39 #if MSC_VERSION
40 #pragma comment(lib, "winmm.lib")
41 #endif
42 
43 
44 // "Guidelines For Providing Multimedia Timer Support" claims that
45 // speeding the timer up to 2 ms has little impact, while 1 ms
46 // causes significant slowdown.
47 static const UINT PERIOD_MS = 2;
48 
49 class CounterTGT : public ICounter
50 {
51 public:
52  virtual const wchar_t* Name() const
53  {
54  return L"TGT";
55  }
56 
58  {
59  // note: timeGetTime is always available and cannot fail.
60 
61  MMRESULT ret = timeBeginPeriod(PERIOD_MS);
62  ENSURE(ret == TIMERR_NOERROR);
63 
64  return INFO::OK;
65  }
66 
67  void Shutdown()
68  {
69  timeEndPeriod(PERIOD_MS);
70  }
71 
72  bool IsSafe() const
73  {
74  // the only point of criticism is the possibility of falling behind
75  // due to lost interrupts. this can happen to any interrupt-based timer
76  // and some systems may lack a counter-based timer, so consider TGT
77  // 'safe'. note that it is still only chosen when all other timers fail.
78  return true;
79  }
80 
81  u64 Counter() const
82  {
83  return timeGetTime();
84  }
85 
86  size_t CounterBits() const
87  {
88  return 32;
89  }
90 
91  double NominalFrequency() const
92  {
93  return 1000.0;
94  }
95 
96  double Resolution() const
97  {
98  return PERIOD_MS*1e-3;
99  }
100 };
101 
102 ICounter* CreateCounterTGT(void* address, size_t size)
103 {
104  ENSURE(sizeof(CounterTGT) <= size);
105  return new(address) CounterTGT();
106 }
u64 Counter() const
Definition: tgt.cpp:81
const Status OK
Definition: status.h:386
static const UINT PERIOD_MS
Definition: tgt.cpp:47
Status Activate()
Definition: tgt.cpp:57
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
virtual const wchar_t * Name() const
Definition: tgt.cpp:52
i64 Status
Error handling system.
Definition: status.h:171
#define u64
Definition: types.h:42
double NominalFrequency() const
initial measurement of the tick rate.
Definition: tgt.cpp:91
unsigned int UINT
Definition: wgl.h:54
bool IsSafe() const
Definition: tgt.cpp:72
ICounter * CreateCounterTGT(void *address, size_t size)
Definition: tgt.cpp:102
void Shutdown()
Definition: tgt.cpp:67
double Resolution() const
actual resolution [s].
Definition: tgt.cpp:96
size_t CounterBits() const
Definition: tgt.cpp:86