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

Thursday, December 23, 2004

boost::program_option #2

// testProgOpt.cpp
#include
#include
#include
using namespace boost;
using namespace boost::program_options;
using namespace std;
// A helper function to simplify the main part.
template
ostream& operator<<(ostream& os, const vector& v)
{
copy(v.begin(), v.end(), ostream_iterator(cout, " "));
return os;
}
int main(int ac, char* av[])
{
int light = 0;
options_description desc("Allowed options");
try {
desc.add_options()
("help", "Produce help message")
("compression", value(), "Set compression level")
("target-bpp", value(), "Set target bpp")
("lighting,L", value(&light)->default_value(10), "Set lighting")
("include-path,I", value<> >(), "input files")
("input-file", value<> >(), "input file")
;
/*
variables_map vm;
store(parse_command_line(ac, av, desc), vm);
notify(vm);
*/
positional_options_description p;
p.add("input-file", -1);
variables_map vm;
store(command_line_parser(ac, av).options(desc).positional(p).run(), vm);
notify(vm);
if (vm.count("help"))
{
cout << desc << "\n";
return 1;
}
if (vm.count("compression"))
{
cout << "Compression level was set to " <<>() << ".\n";
}
else
{
cout << "Compression level was not set.\n";
}
if (vm.count("include-path") )
{
cout << "Include paths are " <<> >() << endl;
}
if (vm.count("lighting") )
{
cout << "Lighting level is " << light << endl;
}
if (vm.count("input-file") )
{
cout << "Input files are " <<> >() << endl;
}
}
catch(exception& e)
{
cerr << e.what() << endl;
cout << desc << endl;
return 1;
}
return 0;
}