root/OpenSceneGraph/trunk/src/osg/Timer.cpp @ 13041

Revision 13041, 2.8 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13//#include <stdlib.h>
14#include <stdio.h>
15#include <string.h>
16
17#include <osg/Timer>
18#include <osg/Notify>
19
20using namespace osg;
21
22// follows are the constructors of the Timer class, once version
23// for each OS combination.  The order is WIN32, FreeBSD, Linux, IRIX,
24// and the rest of the world.
25//
26// all the rest of the timer methods are implemented within the header.
27
28
29Timer* Timer::instance()
30{
31    static Timer s_timer;
32    return &s_timer;
33}
34
35#ifdef WIN32
36
37    #include <sys/types.h>
38    #include <fcntl.h>
39    #include <windows.h>
40    #include <winbase.h>
41    Timer::Timer()
42    {
43        LARGE_INTEGER frequency;
44        if(QueryPerformanceFrequency(&frequency))
45        {
46            _secsPerTick = 1.0/(double)frequency.QuadPart;
47        }
48        else
49        {
50            _secsPerTick = 1.0;
51            OSG_NOTICE<<"Error: Timer::Timer() unable to use QueryPerformanceFrequency, "<<std::endl;
52            OSG_NOTICE<<"timing code will be wrong, Windows error code: "<<GetLastError()<<std::endl;
53        }
54
55        setStartTick();
56    }
57
58    Timer_t Timer::tick() const
59    {
60        LARGE_INTEGER qpc;
61        if (QueryPerformanceCounter(&qpc))
62        {
63            return qpc.QuadPart;
64        }
65        else
66        {
67            OSG_NOTICE<<"Error: Timer::Timer() unable to use QueryPerformanceCounter, "<<std::endl;
68            OSG_NOTICE<<"timing code will be wrong, Windows error code: "<<GetLastError()<<std::endl;
69            return 0;
70        }
71    }
72
73#else
74    #include <unistd.h>
75
76    Timer::Timer( void )
77    {
78        _secsPerTick = (1.0 / (double) 1000000);
79
80        setStartTick();
81    }
82
83    #if defined(_POSIX_TIMERS) && ( _POSIX_TIMERS > 0 ) && defined(_POSIX_MONOTONIC_CLOCK)
84        #include <time.h>
85
86        Timer_t Timer::tick() const
87        {
88            struct timespec ts;
89            clock_gettime(CLOCK_MONOTONIC, &ts);
90            return ((osg::Timer_t)ts.tv_sec)*1000000+(osg::Timer_t)ts.tv_nsec/1000;
91        }
92    #else
93        #include <sys/time.h>
94
95        Timer_t Timer::tick() const
96        {
97            struct timeval tv;
98            gettimeofday(&tv, NULL);
99            return ((osg::Timer_t)tv.tv_sec)*1000000+(osg::Timer_t)tv.tv_usec;
100        }
101    #endif
102
103#endif
Note: See TracBrowser for help on using the browser.