Stop Watch and Time Out Classes

I have a passing interest in stop watch and time out type classes. Keeping track of elapsed time, remaining time. They are very basic classes, but I find them interesting.

GetTimeMS function

The stop watch and timer classes use this function to get the system time, we actually return the system time minus the application start time to get a time relative to the application start time, starting at 0. This is not necessary, return the system time is fine, returning the time the application is running is usually just easier to think about than the time since 1970, which is kind of arbitrary.
This function uses milliseconds, if this is not enough, we can easily switch to nanoseconds.

typedef uint64_t durationms_t;
// Get the time since epoch in milliseconds
durationms_t GetTimeMS()
  static const std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
  const std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
  return std::chrono::duration_cast<std::chrono::milliseconds>(now - start).count();

Stop watch class

Provides start, stop, reset and we can get the total duration.

class cStopWatch
  void Start();
  void Stop();
  void Reset();
  durationms_t GetTotalDurationMS() const;
  bool running;
  durationms_t started;
  durationms_t totalDuration;
cStopWatch::cStopWatch() :
void cStopWatch::Start()
  // Started our stop watch
  started = GetTimeMS();
  running = true;
void cStopWatch::Stop()
  // Get the time now
  const durationms_t now = GetTimeMS();
  // Add the duration for this period
  totalDuration += now - started;
  // Reset our start time
  started = 0;
  running = false;
void cStopWatch::Reset()
  started = 0;
  totalDuration = 0;
  running = false;
durationms_t cStopWatch::GetTotalDurationMS() const
  if (running) {
    // Get the time now
    const durationms_t now = GetTimeMS();
    // Return the previous duration plus the duration of the current period
    return totalDuration + (now - started);
  return totalDuration;

Example usage

cStopWatch stopWatch;
// Start the stop watch
// Sleep for 1 second
const uint64_t timeout_ms = 1000;
::usleep(1000 * timeout_ms);
// Stop the stop watch
// We have now waited for at least 1 second
std::cout<<"Stop watch time: "<<stopWatch.GetTotalDurationMS()<<" ms"<<std::endl;

Time out class

Allows us to get the time remaining, check if the time out has expired and reset the time out.

class cTimeOut {
  explicit cTimeOut(durationms_t timeout);
  void Reset();
  bool IsExpired() const;
  durationms_t GetRemainingMS() const;
  const durationms_t timeout;
  durationms_t startTime;
cTimeOut::cTimeOut(durationms_t _timeout) :
void cTimeOut::Reset()
  startTime = GetTimeMS();
bool cTimeOut::IsExpired() const
  return ((GetTimeMS() - startTime) > timeout);
durationms_t cTimeOut::GetRemainingMS() const
  // Get the total time this timeout uhas been running for so far
  const int64_t duration = int64_t(GetTimeMS()) - int64_t(startTime);
  // Calculate the remaining time
  const int64_t remaining = (int64_t(timeout) - duration);
  // Return the remaining time if there is any left
  return (remaining >= 0) ? remaining : 0;

Example usage

// Create a 5 second time out
cTimeOut timeout(5000);
// Wait until the time out is expired
while (!timeout.IsExpired()) {
  // Sleep for 1 second
  const uint64_t timeout_ms = 1000;
  ::usleep(1000 * timeout_ms);
std::cout<<"Time out reached"<<std::endl;

openglmm_shaders Test 20150817

Here is a video demo of my OpenGL 3.3 library libopenglmm.

YouTube Video

Lambert shading (Mirror’s Edge style)
Cube mapping
Normal mapping
Car paint (Slighty broken, I think a texture look up is incorrect)
Glass mixed with dirty metal texture (Similar to an old rusty/broken mirror)
Cel shading
Smoke particle system with lambert lighting with 4 normals that very roughly approximate a sphere (I think something is wrong with the depth texture look up)

Post render effects:
Teal and orange
HDR (It is cheesy and over done, but you get the idea, it would normally be a lot more tame, either brought back or blending less of the HDR textures in at the end)
Colour blind simulation (3 modes are possible, only one is shown)

Blender Export OBJ Python Script

First of all I’d like to state that I’m definitely a Blender n00b and I have never touched Python in my life.

Baby steps, textured sphere loaded from an obj file
Baby steps, textured sphere loaded from an obj file

In my game engine I basically have this sort of layout:


The .obj points to the .mat file which points to the .png file. So in Blender I would set the material name to “sphere” and ideally in the .obj file it would have a line like this, “usemtl sphere”, I would then get the sphere part and append “.mat” and load the material. I’m not sure if this is standard practice for Blender export scripts, but when exporting to the obj file format, Blender adds the texture name to the material like so, “usemtl sphere_sphere.png”, not cool. Anyway I thought, hey, the export script is written in Python, I wonder if I can fix this?

1) Locate the script. I read somewhere on the Blender site that scripts are in “/usr/share/blender/scripts/”. “” is there, along side “export_obj.pyc” and “export_obj.pyo”.
2) Edit the script.

gedit /usr/share/blender/scripts/

3) The script is pretty well documented. I knew that I should search for “usemtl” as that is the only constant part of the string (Apart from “_” which is much harder to search for). There are two in the file and we want the second one:

mat_data= MTL_DICT.get(key)
	if not mat_data:
		# First add to global dict so we can export to mtl
		# Then write mtl
		# Make a new names from the mat and image name,
		# converting any spaces to underscores with fixName.
		# If none image dont bother adding it to the name
		if key[1] == None:
			mat_data = MTL_DICT[key] = ('%s'%fixName(key[0])), materialItems[f_mat], f_image
			mat_data = MTL_DICT[key] = ('%s_%s' % (fixName(key[0]), fixName(key[1]))), materialItems[f_mat], f_image
		file.write('g %s_%s_%s\n' % (fixName(, fixName(ob.getData(1)), mat_data[0]) ) # can be mat_image or (null)
	file.write('usemtl %s\n' % mat_data[0]) # can be mat_image or (null)

mat_data is a dictionary that is filled out and then written the file with “usemtl”. I replaced the offending line:

mat_data = MTL_DICT[key] = ('%s_%s' % (fixName(key[0]), fixName(key[1]))), materialItems[f_mat], f_image

with this:

mat_data = MTL_DICT[key] = ('%s'%fixName(key[0])), materialItems[f_mat], f_image

And then I realised that it was exactly the same as the line from the “if key[1] == None:” so you could probably remove the branch etc. if you want, I didn’t bother.

If you are exporting groups by material you may also need to edit the “if EXPORT_GROUP_BY_MAT:” branch also, again I didn’t bother. Anyway, save and you’re good to go.

4) I was worried that I would have to compile into bytecode or something for Blender to be able to use this, nope, either restart Blender or hit “Update Menu”. Actually as it is an export script, I’m not sure you even need to do that, it probably gets reloaded when you hit “File > Export > Wavefront (.obj)”?

This is the entirety of my Python knowledge, oh, I also know that whitespace is important or something, woo.

PS. I’m loving git, I can’t believe I didn’t switch earlier!