• 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
Tarviathun

C++ Timer Library

9 posts in this topic

Currently, I'm looking for a decent timer library for a 2D RTS engine that I'm making. I've looked around the internet but haven't found anything. I could use an API specific timer, such as those in DX, OpenGL, or something lesser known such as Grim. But I'd prefer to have seperate libraries for everything, allowing the maximum amount of flexibility when it comes to porting the engine to different APIs for performance comparison. If anyone could recommend a decent timer library for my game, I'd really appreciate it. Thanks for the time.
0

Share this post


Link to post
Share on other sites
Time.h has everything you need. It's as simple as calling clock() to get the number of millis passed since the app started. For the difference in seconds, it's like this:

clock_t initial = clock();
//do stuff here
clock_t current = clock();
float difference = (current - initial) / 1000.0f;//1000 millis = 1 second ;)

0

Share this post


Link to post
Share on other sites
afaik the granularity of clock(0) really sucks, so its not a good choice for a decent game...
plus, it only takes the CPU time, right? what if your programs task doesn't get 100% CPU time?

@Tarviathun: i don't know whether the boost library has good timers, but i'd just write my own timer class to wrap some DX timer (eg.), so you can easily replace it if necessary.
0

Share this post


Link to post
Share on other sites
I'll probably do what you said, max. Wrap a timer library from DX. I would presume it has a high-resolution timer. The reason why I don't use the standard clock in Time.h is I've heard that is has some issues with accuracy.
0

Share this post


Link to post
Share on other sites
Here's the timer class I made from a few various tutorials.


#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
// Ctime.h
class CTime
{
private:
float dt, td, td2;
__int64 frequency; // Timer Frequency
float resolution; // Timer Resolution
unsigned long mm_timer_start; // Multimedia Timer Start Value
unsigned long mm_timer_elapsed; // Multimedia Timer Elapsed Time
bool performance_timer; // Using The Performance Timer?
__int64 performance_timer_start; // Performance Timer Start Value
__int64 performance_timer_elapsed; // Performance Timer Elapsed Time

public:
CTime();
void Update();
float Get_dt() const;
float GetTime() const;
void Initialize();
};






// CTime.cpp
void CTime::Update()
{
td2 = GetTime();
dt = (td2-td) * 0.1f;
td = td2;
}

CTime::CTime()
{
dt = 0;
td2 = 0;
td = 0;
frequency = 0;
performance_timer_start = 0;
resolution = 0;
mm_timer_start = 0;
mm_timer_elapsed = 0;
performance_timer_elapsed = 0;
performance_timer = 0;
Initialize();
}

void CTime::Initialize() // Initialize Our Timer (Get It Ready)
{
// 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;
}
}

float CTime::Get_dt() const
{
return dt;
}

float CTime::GetTime() const // Get Time In Milliseconds
{
__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;
}
}





To use it, it's very easy:
CTime timer;

in your init code:
timer.Initialize();

in your update function:
timer.Update();

whenever you want to get the elapsed time:
timer.GetTime();

whenever you want to get the frame difference time
timer.Get_dt();

It should be bug free - I've had no problems with it [smile]. I just take credit for wrapping it up [lol] - the code is not my original ideas. I believe I had used some ideas from Cone3D, NeHe, and GameTutorials sometime ago.

- Drew

[edit] Also take a look at this page for info on the avaliable timers and why I use the one I did.[/edit]

[Edited by - Drew_Benton on January 13, 2005 10:25:15 PM]
0

Share this post


Link to post
Share on other sites
Thank you a whole lot. I think I'll use that for now, seeing as it's simple and effective. I'll definately give you credit for it. Thanks.
0

Share this post


Link to post
Share on other sites
void WINAPI GetCycleCount(ULARGE_INTEGER &ulCycleCount)
{
__asm
{
push ecx
rdtsc
mov ecx, ulCycleCount
mov DWORD PTR[ecx], eax
mov DWORD PTR[ecx+4], edx
pop ecx
}
}


That'll get the CPU cycle counter. Should be accurate to +/- 10 cycles or so, if you're running in realtime priority class.
0

Share this post


Link to post
Share on other sites
void CTime::Update() 
{
td2 = GetTime();
dt = (td2-td) * 0.1f;
td = td2;
}


Could somebody tell me why do we multiply the variable dt in the above timer update function by 0.1? That would give us the difference time not in ms, but in 0.01s.
0

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0