Thomas Sampson

Leave a comment

Finding variable assignments

When searching through an entire codebase for a variable it is often useful to limit the search results to those instances where the variables is assigned a new value (or perhaps when the variable is used IN an assignment). This can be achieved with the following regular expressions…

Find assignments

MyVariableName[ \t\r\n\v\f]*=[^=]

Find usage in an assignment

=[^=][ \t\r\n\v\f]*MyVariableName

Leave a comment

ATI Tessellation demo

ATI tessellation demo showing how the GPU can take a relatively simple triangular mesh and use additional normal/displacement data to generate a more rich, detailed mesh on the fly. Extra tessellation detail is generated dynamically and passes straight through the GPU, requiring no extra video memory for the additional vertex data.

Leave a comment

Basic C++ Multi Threading

This is a quick post for anyone who wants to get up and running with multiple threads quickly in C++. I have been doing a fair amount of python programming recently, in particular a 2D networked game, and have realised how useful distributing independent tasks across multiple threads can be. After looking into how best and easiest to translate this experience from python and get a working example going in C++, I am posting the following code which I will attempt to explain beneath.

#include <iostream>
#include <windows.h>
#include <conio.h>
using std::cout;
using std::endl;

int thread_count(0);
int main_thread_count(0);

DWORD_PTR WINAPI threadFunction(void* p)
    cout << "Thread "<<(unsigned int)p<<" busy...."<<endl;

  return 0;

int main(int argc, char* argv[])
  cout << "Starting...."<<endl;

  //Gather processor info
  SYSTEM_INFO si={0,0};
  const unsigned int num_cores(si.dwNumberOfProcessors);

  cout << num_cores << " Cores detected"<<endl;

  //Set up thread handles
  HANDLE* thread_handle=new HANDLE[num_cores];

  for(unsigned int core(0);core<num_cores; ++core)
    DWORD_PTR rqd;
    thread_handle[core]=CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadFunction, (LPVOID)core,     NULL, &rqd);

  cout << "Main thread reached"<<endl;


  //Terminate threads
  DWORD exit_code( 0);
  for(unsigned int core(0);core<num_cores; ++core)

  delete[] thread_handle;

  return 0;

Quick run through!

How many threads to use? A good question! When you first come across threading you may believe it is the answer to every problem and spawn threads in abundance! In the example above I have used the GetSystemInfo() call to gather information about the available CPU’s, then use this information to spawn a thread per cpu. The threads get spawned in the same virtual address space as the parent process and are allocated a stack space of 1mb by default (although this is tweakable). For this reason you could essentially spawn as many threads as you like up to the virtual memory limit of the system, but a separate thread per cpu is usually prefered, and is the recommended approach over at msdn.

Spawning a thread essentially means running some previously declared function in parallel to main, and in this example each new thread runs threadFunction(). In real life this is seemingly useless, you would usually have a specific function and task for each thread such as rendering particles, or listening out on a socket for incoming connections.

Thread handles are used to allow you to make reference and work with your threads after you have created them, for a comprehensive list of thread operations see here. Be sure to use the thread handles to TerminateThread() on each of your threads before exiting the parent process.

Leave a comment

STL algorithm predicates

Just a quick reminder on how to set up a predicate function for an stl algorithm (accumulate shown here) using the () operator on a struct/class.

This arbitrary example accumulates all integer values in bag which are greater than 2. There third parameter to accumulate (the zero) means start accumulating from 0 rather than a given constant.

struct check
	int operator()(int l, int r)
		return (r>2)?  (l+r): l;

int total=std::accumulate(bag.begin(),bag.end(),0,check());

Leave a comment

Visual Studio Post Build Events

I am currently working on a Visual Studio solution that has multiple projects each building an exe or library to a different directory. My aim was to copy some of the output files all into the same folder as the main application is dependent on the other projects residing in the same directory.

Previously, if I had updated and rebuilt one of the projects in the solution I would manually locate the output file and drag it into the right place. However today I discovered the post build events area in Visual studio (right click solution > Build events). Here you can input your own DOS commands and utilise a bunch of VS environmental macros/variables to create a simple batch script which is automatically executed every time you build/rebuild your solution. Any output from your DOS commands (echo) is printed into the ouput window inline with all the other build information Visual Studio provides during the build process.

Here is a simple example which solved my problem but there are much more advanced techniques you could use with this feature.

echo Copying pre loader application to build directory
copy "$(SolutionDir)cwpl_app\bin\Debug\CwplApp.exe" "$(OutDir)"
echo Launching pre loader tool


Extracting data from float

Earlier this week i was presented with the simple problem of checking if floating point number was a whole number. I stumbled upon the useful function modf and thought it was worth a quick post. the modf function requires <stdio.h> and <math.h>, and allows you to separate the integral and fractional part of a floating point number.

Taking the value 5.24f as an example…

  • Integral part = 5
  • Fractional part=0.24

modf is fully documented here but is rather simple. modf takes two arguments, the first is the value you wish to query (in the case above this would be 5.24). The second argument is a pointer to where the integral part should be placed. The return value of the method is the fractal part. Therefore the solution to my original problem (testing if a float is infact an integer value) is as follows…


int f;


//myValue is a whole number