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?

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.

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().

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).

First of all:

I don’t know why, but I feel that’s kind of hilarious. :slight_smile:

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.

[QUOTE=thokra;1254553]
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.[/QUOTE]

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?

[QUOTE=debonair;1254574]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?[/QUOTE]
glutBitmapCharacter() is about a simple as it gets.

heres a little time pausing function i wrote:

//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.

[QUOTE=sandbucket;1254704]heres a little time pausing function i wrote:


	while (clock() < time);

[/QUOTE]
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”).