Monday, February 07, 2005

Display Font in GLUT - 1

There are two ways to display text in GLUT. The first is using bitmap fonts -- unscalable and doesn’t response to modelview transform. My preferred way is stroke fonts -- fonts that are drawn from OpenGL primitives.

(1) To insert fonts in GLUT (see here for documentation)

void glutStrokeCharacter(void *font, int character);
font:
This method can handle two types of font (first argument), namely
GLUT_STROKE_ROMAN
GLUT_STROKE_MONO_ROMAN
The MONO version is a fixed-width font (like Courier New, etc)

character:

Takes in any ASCII character from 32 through 127

(2) To get the measurement of its length

int glutStrokeLength(void *font, const unsigned char *string);

This method returns the width in units from the unscaled modeling units


This is how to use them:
(3) Define a string generator function
This function serves to render the string being passed to it.

void renderStrokeString( void *font, const string& str )
{
for (int i = 0; i lessThan str.length(); i++)
glutStrokeCharacter(font, str[i]);
}




How to retrieve the date and time info (C/C++)

In order to get the current date and time in C / C++, we do the following steps

(1) #include "time.h"

(2) Declare two structure of type tm* and time_t
tm* currentTimePtr;
time_t time_tTime;

(3) Call time() by passing it the address of a time_t variable. When time() returns, the current time will be stored in time_tTime. time_tTime will contain the number of seconds elapsed since midnight (00:00:00) of January 1, 1970, coordinated universal time (UTC), according to the system clock.
time(&time_tTime);

(4) Call localtime()by passing it the address of a time_t variable which has been passed to time(). This function converts the time_t information to the current local time and extracting the time information (such as sec, min, day, etc)

currentTimePtr = localtime( &time_tTime);

The tm structure contains the following members (taken from the MSDN Library)

tm_sec
Seconds after minute (0 – 59).

tm_min
Minutes after hour (0 – 59).

tm_hour
Hours after midnight (0 – 23).

tm_mday
Day of month (1 – 31).

tm_mon
Month (0 – 11; January = 0).

tm_year
Year (current year minus 1900).

tm_wday
Day of week (0 – 6; Sunday = 0).

tm_yday
Day of year (0 – 365; January 1 = 0).

tm_isdst
Positive value if daylight saving time is in effect;
0 if daylight saving time is not in effect;
negative value if status of daylight saving time is unknown.

If the TZ environment variable is set, the C run-time library assumes rules appropriate to the United States for implementing the calculation of daylight-saving time (DST).