⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timer.cpp

📁 小型的3D游戏引擎
💻 CPP
字号:
#include <windows.h>
#include <memory.h>
#include <stdio.h>

#include "timer.h"

//////////////////////////////////////////////////////////////////////////////////////

GcTimer::GcTimer()
{
	// Clear the object
	memset(this, 0, sizeof(this));

	// Check To See If A Performance Counter Is Available
	// If One Is Available The Timer Frequency Will Be Updated
	if (!QueryPerformanceFrequency((LARGE_INTEGER *) &frequency))
	{
		// No Performace Counter Available
		performance_timer = false;			// Set Performance Timer To FALSE
		mm_timer_start = timeGetTime();		// Use timeGetTime() To Get Current Time
		resolution = 1.0f/1000.0f;			// Set Our Timer Resolution To .001f
		frequency = 1000;					// Set Our Timer Frequency To 1000
		mm_timer_elapsed = mm_timer_start;	// Set The Elapsed Time To The Current Time
	}
	else
	{
		// Performance Counter Is Available, Use It Instead Of The Multimedia Timer
		// Get The Current Time And Store It In performance_timer_start
		QueryPerformanceCounter((LARGE_INTEGER *) &performance_timer_start);
		performance_timer = true;				// Set Performance Timer To TRUE

		// Calculate The Timer Resolution Using The Timer Frequency
		resolution = (float) (((double)1.0f)/((double)frequency));
		// Set The Elapsed Time To The Current Time
		performance_timer_elapsed = performance_timer_start;
	}


	//WriteLog("TIMER construced\n");
}

///////////////////////////////////////////////////////////////////////////////////////

GcTimer::~GcTimer()
{
	// Write the the logfile
	//WriteLog("TIMER destructed\n");
}

///////////////////////////////////////////////////////////////////////////////////////


void GcTimer::StartTimer()
{
	// Get the current time
	time = GetTime();
}

///////////////////////////////////////////////////////////////////////////////////////

float GcTimer::GetTime()
{
	__int64 time;				// Time will hold A 64 bit integer
	if (performance_timer)		// Are We Using The Performance Timer?
	{
		QueryPerformanceCounter((LARGE_INTEGER *) &time);	// Grab The Current Performance Time

		// Return The Current Time Minus The Start Time Multiplied By The Resolution And 1000 (To Get MS)
		return ((float) (time - performance_timer_start) * resolution)*1000.0f;
	}
	else
	{
		// Return The Current Time Minus The Start Time Multiplied By The Resolution And 1000 (To Get MS)
		return ((float) (timeGetTime() - mm_timer_start) * resolution)*1000.0f;
	}
}

//////////////////////////////////////////////////////////////////////////////////////

void GcTimer::DoTimer()
{
	// First determine how many seconds elapsed
	timeDiff = GetTime() - time;

	// The function reads in milliseconds so convert to seconds by dividing by 1000
	secsPerFrame = (float)(timeDiff/1000.0f);

	// Get the new time
	time = GetTime();
}

//////////////////////////////////////////////////////////////////////////////////////

char *GcTimer::FPS()
{
	// Print the FPS once every second
	if(time - lastTime > 1000.0f) {

		// Printe the FPS to the buffer
		sprintf(buffer, "FPS: %7.02f ", 1 / secsPerFrame );
		
		// Update the last time
		lastTime = time;
	}

	return buffer;
}

//////////////////////////////////////////////////////////////////////////////////////


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -