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

Compute how long it takes todo something

11 posts in this topic

What is the best way to compute how long it takes todo something in seconds,minutes,hours, etc compute how long it takes to complete a level , do you use WM_TIMER and win32 functions or is there something else , thanks for reading.

0

Share this post


Link to post
Share on other sites

In order to calculate how much time has passed, you can use the C++ method, time() then perform the action that you want to be timed, then call time() again and subtract to get the amount of time that has passed.

0

Share this post


Link to post
Share on other sites

time() returns seconds, not millaseconds, so it'd give a very inaccurate measurement.
 
Using millaseconds give fairly accurate measurements for most needs. You can use these to get the number of millaseconds past since your program began:

#include <ctime>

//The time, in seconds, since the program started. (1.5f = one and a half seconds)
Seconds RunTimeInSeconds()
{
    //CPU "ticks" since the program started.
    clock_t programTickCount = clock();

    //Convert from ticks to seconds.
    float seconds = float(programTickCount) / CLOCKS_PER_SEC;

    return seconds;
}

//Same as above, but in millaseconds as an integer. (1500 = one and a half seconds)
Milliseconds RunTime()
{
    //CPU "ticks" since the program started.
    clock_t programTickCount = clock();

    //Conversion rate between ticks and millaseconds.
    float msMultiplyer = 1000.0f / CLOCKS_PER_SEC;

    //Convert from ticks to seconds.
    int millaSeconds = (programTickCount * msMultiplyer);

    return millaSeconds;
}

 
Using microseconds gives ever more accuracy, however there are not any standard microsecond timers in C++, so you have to use third party timers, unless you are using the new C++11 standard's <chrono> library.

2

Share this post


Link to post
Share on other sites

The OP probably does not need millisecond accuracy to measure how long the user took to complete a level, let alone microseconds! The [url="http://www.cplusplus.com/reference/ctime/time/"]time()[/url] function should suffice.

 

If you were measuring the user's reaction time, or measuring how long your program was taking to perform an action, then the higher resolution functions would be preferred.

1

Share this post


Link to post
Share on other sites
The OP probably does not need millisecond accuracy to measure how long the user took to complete a level, let alone microseconds! The time() function should suffice.

 

If you were measuring the user's reaction time, or measuring how long your program was taking to perform an action, then the higher resolution functions would be preferred.

yeh ive been told that, would those functions calculate the same way you calculate the fps , would it also go in the msg loop or in the function thats doing the task?

0

Share this post


Link to post
Share on other sites
It'd go something like this:
//When the level begins:
unsigned int startTime = (unsigned int)time(); //Get current clock time. Example: 75400000
 
//When the level is completed:
unsigned int endTime = (unsigned int)time(); //Get current clock time. Example: 75400127
unsigned int timeItTook = (endTime - startTime); //Example: 75400127 - 75400000 = It took 127 seconds.

You can then do something like:
unsigned int seconds = (endTime - startTime);

unsigned int minutes = (seconds  / 60);
seconds %= 60;

std::cout << "It took you " << minutes << " minutes, and " << seconds << " seconds." << std::endl;
0

Share this post


Link to post
Share on other sites
The OP probably does not need millisecond accuracy to measure how long the user took to complete a level, let alone microseconds! 

You have to understand, it is a very competitive game :P

1

Share this post


Link to post
Share on other sites

It'd go something like this:

//When the level begins:
unsigned int startTime = (unsigned int)time(); //Get current clock time. Example: 75400000
 
//When the level is completed:
unsigned int endTime = (unsigned int)time(); //Get current clock time. Example: 75400127
unsigned int timeItTook = (endTime - startTime); //Example: 75400127 - 75400000 = It took 127 seconds.

You can then do something like:
unsigned int seconds = (endTime - startTime);

unsigned int minutes = (seconds  / 60);
seconds %= 60;

std::cout << "It took you " << minutes << " minutes, and " << seconds << " seconds." << std::endl;

 

would that be using the high performance counter?

0

Share this post


Link to post
Share on other sites

this is exactly what i want

 

#include "stdafx.h"
#include <Windows.h>

DWORD WINAPI test_thread(LPVOID Param)
{
    MessageBox(NULL,"test_thread started ","",0);

    for(int i = 0; i < 99999999999; i++)
    {

    }
    //i would like to work out how many seconds that loop ran for

    MessageBox(NULL,"test_thread over","",0);

    return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE test_thread_handle = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)test_thread,NULL,NULL,NULL);
    WaitForSingleObject(test_thread_handle,INFINITE);
    return 0;
}
0

Share this post


Link to post
Share on other sites
If you want to test how many seconds it ran for, you can do this:
 
DWORD WINAPI test_thread(LPVOID Param)
{
    MessageBox(NULL,"test_thread started ","",0);

    time_t startTime = time();

    for(int i = 0; i < 99999999999; i++)
    {
        //WARNING! If you don't have anything in this loop, your compiler might optimize the entire loop into non-existence.
    }
     
    time_t endTime = time();
    time_t numSeconds = (endTime - startTime);

    //numSeconds is the number of seconds. You could cast it to unsigned int or int.
    
    MessageBox(NULL,"test_thread over","",0);

    return 0;
}
No, time() is not "high performance". time() returns seconds. You can use the RunTime() function I posted above for millaseconds (1/1000 of a second), which is still not considered "high performance" but is perfectly fine for most uses and what I usually use. When people say 'high performance' they usually mean microseconds (1/1000000 of a second).

There is no cross-platform way to get guaranteed microsecond performance, but C++11 has a few classes for getting microseconds if it's available, and millaseconds if it's not. Win32 also has some microsecond precision timers, if I recall correctly, but they are Windows-only.

Here is the same test as above, but with millasecond performance, if you need it:
DWORD WINAPI test_thread(LPVOID Param)
{
    MessageBox(NULL,"test_thread started ","",0);

    unsigned int startTime = RunTime();

    for(int i = 0; i < 99999999999; i++)
    {
        //WARNING! If you don't have anything in this loop, your compiler might optimize the entire loop into non-existence.
    }
     
    unsigned int endTime = RunTime();
    unsigned int millaseconds = (endTime - startTime);

    //Some conversion examples:
    unsigned int seconds = (millaseconds / 1000); //1000 millaseconds in a second.
    millaseconds %= 1000;
    unsigned int minutes = (seconds / 60); //60 seconds in a minute.
    seconds %= 60;
    unsigned int hours = (minutes / 60); //60 minutes in an hour.
    minutes %= 60;

    //Example: "It took: 0 hours, 2 minutes, 24 seconds, and 456 millaseconds."
    std::cout << "It took: " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds, and " << millaseconds << " millaseconds." << std::endl;
    
    MessageBox(NULL,"test_thread over","",0);

    return 0;
}
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