• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
  • entries
    8
  • comments
    8
  • views
    11707

Cross-platform HiResTimer

Sign in to follow this  
Followers 0
Kazade

669 views

It's been a while since I've updated this, I'll make an effort to do so more often.

If anyone reading this has read OpenGL Game Programming or Beginning OpenGL Game Programming, you may remember the game timer class that is used in the source code by Dave Astle.

I've just spent a little while making this work on Linux by reimplementing QueryPerformanceFrequency, and QueryPerformanceTimer as private methods the code for these functions has been adapted from the equivalents in the Wine implementation.

I haven't tried recompiling on Windows yet, but I assume it will work as I haven't changed any of the Windows code. Anyway, here it is, I hope it's useful for someone:


/****************************************************************************
HiResTimer.h

Cross-platform implementation of the HiResTimer originally written
by Dave Astle for the book OpenGL Game Programming.

Author : Luke Benstead

Wrapper for the high-resolution timer. Can't be used (on Windows) if the
hi-res timer doesn't exist.

*****************************************************************************/



#ifndef __TIMER_H_INCLUDED__
#define __TIMER_H_INCLUDED__

#ifdef WIN32
#include
#endif

#ifdef unix
#include

/*
This is the definition of LARGE_INTEGER
for unix platforms only, Windows already has
this defined.
*/

typedef union _LARGE_INTEGER
{
struct
{
unsigned long LowPart;
long HighPart;
};
struct
{
unsigned long LowPart;
long HighPart;
} u;
long long QuadPart;
} LARGE_INTEGER;

//This is the number of ticks per second for
//unix platforms
const long TicksPerSecond = 10000000;
#endif

class HiResTimer
{
public:

/*****************************************************************************
Init()

If the hi-res timer is present, the tick rate is stored and the function
returns true. Otherwise, the function returns false, and the timer should
not be used.
*****************************************************************************/

bool Init()
{
if (!QueryPerformanceFrequency(&m_ticksPerSecond))
{
// system doesn't support hi-res timer
return false;
}
else
{
QueryPerformanceCounter(&m_startTime);
return true;
}
} // end Init()

float GetElapsedSeconds(unsigned long elapsedFrames = 1)
{
static LARGE_INTEGER s_lastTime = m_startTime;
LARGE_INTEGER currentTime;

QueryPerformanceCounter(¤tTime);

float seconds = ((float)currentTime.QuadPart - (float)s_lastTime.QuadPart) / (float)m_ticksPerSecond.QuadPart;

// reset the timer
s_lastTime = currentTime;

return seconds;
} // end GetElapsedSeconds()

/***************************************************************************
GetFPS()

Returns the average frames per second over elapsedFrames, which defaults to
one. If this is not called every frame, the client should track the number
of frames itself, and reset the value after this is called.
***************************************************************************/

float GetFPS(unsigned long elapsedFrames = 1)
{
static LARGE_INTEGER s_lastTime = m_startTime;
LARGE_INTEGER currentTime;

QueryPerformanceCounter(¤tTime);

float fps = (float)elapsedFrames * (float)m_ticksPerSecond.QuadPart / ((float)currentTime.QuadPart - (float)s_lastTime.QuadPart);

// reset the timer
s_lastTime = currentTime;

return fps;
} // end GetFPS

/***************************************************************************
LockFPS()

Used to lock the frame rate to a set amount. This will block until enough
time has passed to ensure that the fps won't go over the requested amount.
Note that this can only keep the fps from going above the specified level;
it can still drop below it. It is assumed that if used, this function will
be called every frame. The value returned is the instantaneous fps, which
will be <= targetFPS.
***************************************************************************/

float LockFPS(unsigned char targetFPS)
{
if (targetFPS == 0)
targetFPS = 1;

static LARGE_INTEGER s_lastTime = m_startTime;
LARGE_INTEGER currentTime;
float fps;

// delay to maintain a constant frame rate
do {
QueryPerformanceCounter(¤tTime);
fps = (float)m_ticksPerSecond.QuadPart/((float)(currentTime.QuadPart - s_lastTime.QuadPart));
} while (fps > (float)targetFPS);

// reset the timer
s_lastTime = m_startTime;

return fps;
} // end LockFPS()

private:
#ifdef unix
/*
These next 2 functions are reimplementations of the Windows
functions of the same name for unix. If we are compiling
on Windows these 2 methods will not be compiled and the
Windows built-in ones will be used instead.
*/

bool QueryPerformanceFrequency(LARGE_INTEGER* frequency)
{
frequency->QuadPart = TicksPerSecond;
return true;
}

bool QueryPerformanceCounter(LARGE_INTEGER* currentTime)
{
unsigned long long Ticks1601To1970 = (369 * 365 + 89) * 86400 * (unsigned long long) TicksPerSecond;
struct timeval now;
gettimeofday( &now, 0 );
currentTime->QuadPart = now.tv_sec * TicksPerSecond + Ticks1601To1970;
currentTime->QuadPart += now.tv_usec * 10;

return true;
}
#endif

LARGE_INTEGER m_startTime;
LARGE_INTEGER m_ticksPerSecond;
};

#endif



0
Sign in to follow this  
Followers 0


2 Comments


Looks good. However, are you aware that QueryPerformanceCounter has a lot of issues on Windows? Particularly on dual core CPUs, and variable frequency CPUs (I.e. laptops)?

In my code, I just use timeGetTime(), since it's usually accurate enough. I only fall back to QPC if it's a very short time, and even then, there's a fair bit of validation done to the results from it.
0

Share this comment


Link to comment
No I didn't realize that, but I can see why there could be problems, thanks for the heads up! [smile]
0

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now