Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

Thread: Timer Function

  1. #1
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    205

    Timer Function

    I want to display elapsed time on the screen and I want to reset the timer when I reset it.

    How to get elapsed time ? and can we display integer on the screen?

  2. #2
    Junior Member Newbie
    Join Date
    Aug 2013
    Posts
    5
    Elapsed time can be measured using the <ctime> library.
    I am not an expert on displaying things on screen (yet), so if you want to just easily and quickly test something i'd recommend debugging it somewhere.

  3. #3
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by debonair View Post
    How to get elapsed time ?
    That's platform-specific. There are standard C functions (e.g. time()), but those have no specified resolution, and the actual resolution is typically 1 second, which is too coarse if you're trying to measure the time taken to render a frame. On Windows, use GetTickCount() or QueryPerformanceCounter(). On Unix, use gettimeofday() or clock_gettime().

    Quote Originally Posted by debonair View Post
    and can we display integer on the screen?
    Create a texture containing the digits 0-9 (and optionally a decimal point), and render each digit as a textured quad (or pair of triangles). Other methods for rendering text are available from utility libraries (e.g. GLUT).

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,128
    First of all:

    Quote Originally Posted by 8baanknexer
    I am not an expert on displaying things on screen (yet)
    I don't know why, but I feel that's kind of hilarious.

    Second, debonair: In addition to what GClements said, you can use std::chrono in C++11 or boost::chrono otherwise. Also, what Windowing toolkit are you using? FreeGLUT, Qt, SDL, GLFW, ... ? Most if not all of these already provide means for timing in a x-platform manner.

    Aside from the usual approach GClements mentioned, OpenGL has never had any facilities to directly render text. You can use various libraries like FreeType, libcairo + libpango and various others or you go the easy way and do it yourself, with the obvious drawback that you don't have support for multiple fonts and can only support characters you already represent with a texture. In other words, yes, you can haz integerz.

  5. #5
    Junior Member Regular Contributor
    Join Date
    Jun 2012
    Posts
    205
    Quote Originally Posted by thokra View Post
    Second, debonair: In addition to what GClements said, you can use std::chrono in C++11 or boost::chrono otherwise. Also, what Windowing toolkit are you using? FreeGLUT, Qt, SDL, GLFW, ... ? Most if not all of these already provide means for timing in a x-platform manner.

    Aside from the usual approach GClements mentioned, OpenGL has never had any facilities to directly render text. You can use various libraries like FreeType, libcairo + libpango and various others or you go the easy way and do it yourself, with the obvious drawback that you don't have support for multiple fonts and can only support characters you already represent with a texture. In other words, yes, you can haz integerz.
    I am using GLUT library.

    I came across BitmapCharacter API of glut but it looks painful for displaying scores on the screen. Is there any other simple way to display the time/ticks on the screen?

  6. #6
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by debonair View Post
    I am using GLUT library.

    I came across BitmapCharacter API of glut but it looks painful for displaying scores on the screen. Is there any other simple way to display the time/ticks on the screen?
    glutBitmapCharacter() is about a simple as it gets.

  7. #7
    Intern Contributor
    Join Date
    Jun 2013
    Posts
    69

    Wink

    heres a little time pausing function i wrote:
    Code :
    //time function
    #include <time.h>
     
    void skip(float t)
    {
    	clock_t time = clock() + (CLOCKS_PER_SECOND * t)
    	while (clock() < time);
    }
    the problem i think is that eventually clock will get too large for its type and wierd things will happen. good if youre just screwing around, but not good for serious work.

  8. #8
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by sandbucket View Post
    heres a little time pausing function i wrote:
    Code :
    	while (clock() < time);
    Ugh. This will consume 100% CPU (at least, for one core). Any platform which supports OpenGL can be expected to have some form of sleep() function, even if it's not standardised or entirely the same between platforms; e.g. Unix has sleep (seconds), usleep (microseconds) and nanosleep (guess), while Windows has Sleep (milliseconds, upper-case "S").

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •