Created high-precision Timer interface and Win32 implementation of Timer.

pull/3/head
Chris Laurel 2001-04-30 20:45:47 +00:00
parent 6a30383124
commit 639e5272cb
3 changed files with 84 additions and 13 deletions

26
src/timer.h 100644
View File

@ -0,0 +1,26 @@
// timer.h
//
// Copyright (C) 2001, Chris Laurel <claurel@shatters.net>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#ifndef _TIMER_H_
#define _TIMER_H_
#include "celestia.h"
class Timer
{
public:
Timer() {};
virtual ~Timer() {};
virtual double getTime() const = 0;
virtual void reset() = 0;
};
extern Timer* CreateTimer();
#endif // _TIMER_H_

View File

@ -24,6 +24,7 @@
#include "vecmath.h"
#include "quaternion.h"
#include "util.h"
#include "timer.h"
#include "stardb.h"
#include "solarsys.h"
#include "asterism.h"
@ -48,13 +49,9 @@ static string welcomeMessage1("Welcome to Celestia 1.07");
static string welcomeMessage2("Press D to run demo");
//----------------------------------
// Timer info.
static LARGE_INTEGER TimerFreq; // Timer Frequency.
static LARGE_INTEGER TimeStart; // Time of start.
static LARGE_INTEGER TimeCur; // Current time.
static double currentTime=0.0;
static double deltaTime=0.0;
static double currentTime = 0.0;
static Timer* timer = NULL;
static int nFrames = 0;
static double fps = 0.0;
@ -1352,10 +1349,7 @@ int APIENTRY WinMain(HINSTANCE hInstance,
icex.dwICC = ICC_DATE_CLASSES;
InitCommonControlsEx(&icex);
// Reset the timer
QueryPerformanceFrequency(&TimerFreq);
QueryPerformanceCounter(&TimeStart);
timer = CreateTimer();
renderer = new Renderer();
// Prepare the scene for rendering.
@ -1391,7 +1385,7 @@ int APIENTRY WinMain(HINSTANCE hInstance,
// Set up the overlay
overlay = new Overlay();
overlay->setWindowSize(g_w, g_h);
font = txfLoadFont("fonts\\default.txf");
font = txfLoadFont("fonts/default.txf");
if (font != NULL)
{
txfEstablishTexture(font, 0, GL_FALSE);
@ -1896,9 +1890,8 @@ LRESULT CALLBACK MainWindowProc(HWND hWnd,
if (bReady)
{
// Get the current time, and update the time controller.
QueryPerformanceCounter(&TimeCur);
double lastTime = currentTime;
currentTime = (double) (TimeCur.QuadPart - TimeStart.QuadPart) / (double) TimerFreq.QuadPart;
currentTime = timer->getTime();
double deltaTime = currentTime - lastTime;
nFrames++;
if (nFrames == 100)

52
src/wintimer.cpp 100644
View File

@ -0,0 +1,52 @@
// wintimer.h
//
// Copyright (C) 2001, Chris Laurel <claurel@shatters.net>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include <windows.h>
#include "timer.h"
class WindowsTimer : public Timer
{
public:
WindowsTimer();
~WindowsTimer();
double getTime() const;
void reset();
private:
LARGE_INTEGER freq;
LARGE_INTEGER start;
};
WindowsTimer::WindowsTimer()
{
QueryPerformanceFrequency(&freq);
reset();
}
WindowsTimer::~WindowsTimer()
{
}
double WindowsTimer::getTime() const
{
LARGE_INTEGER t;
QueryPerformanceCounter(&t);
return (double) (t.QuadPart - start.QuadPart) / (double) freq.QuadPart;
}
void WindowsTimer::reset()
{
QueryPerformanceCounter(&start);
}
Timer* CreateTimer()
{
return new WindowsTimer();
}