Archive for the tag: snippets

Simple Scene Graph in C++

Tags:, , 4 Comments »

There are several articles gathering dust bunnies on the internet on creating a scene graph class in C++ for your 3D engine but most are pretty vague and quite old. Hopefully, this post will give you a foot in the door in creating your own scene graph for your engine.

To start off, let’s go over some basics. A scene graph is a tree-like data structure which holds information about the scene you want to display. Every node has a parent and every node may have children. In this post we’ll use the std::vector to store the scene nodes but you can substitute this with whatever you want. A scene graph can be visualized like so:

             [root node]
                  |
        o=========o=========o
        |         |         |
    [child 1] [child 2] [child 3]
        |
    o===o====o
    |        |
[child 4][child 5]
             |
         [child 6]

Any node may have any number of children who’s children may have any number of children, etc. Each node in the scene graph may have a name for lookup functionality so a very lookup system will be implemented. We will also need functionality to add a child node, remove a child node, set/get a node’s parent and an update function for updating the graph hierarchically.
Read the rest of this entry »

Little Endians, Bytes and Binary

Tags:, No Comments »

I’ve recently had the need to read an entire file into a byte array but still had the need to extract integers from it. Turns out, it’s very possible to do this but platform dependently since the byte-ordering (or endianness) on different machines can differ.

For example, Little-Endian machine A has an integer that it needs to write to disk: 12345. Big-Endian machine B has the same integer that it needs to write to disk as well.

The hexadecimal representation of the file on machine A would look like: 00 00 30 39 while machine B’s output would look like: 39 30 00 00.

If we were to read machine A’s file on machine B, the output would not be 12345 as expected but 245618442240 instead. Now that’s quite a problem. Any file written on machine A would be useless in any other environment.

In the meantime, be aware that there’s no way to determine if a file is Big or Little Endian so you would need to set a standard for your file. I use Little-Endian byte ordering since that’s my machine’s native format and 99% of the time, yours as the x86 family of processors is Little-Endian.

So in order to read any of the files we have on disk, regardless of endianness, we first need to detect the endianness of our current machine and somehow detect the endianness of the file we’re trying to read. This is not a big pain in the ass as you might suspect since machines, in addition to files, also order their memory in Big- or Little-Endian byte ordering.
Read the rest of this entry »

Amount of Digits in an Integer

Tags:, No Comments »

Here’s another little snippet that might come in handy in your programmatic travels. I’ll show you an example of usage below, which might also be of interest to you. The code presented is in C, not C++. First, the code to count the amount of digits in an integer:

const size_t intlen(long long int Num)
{
	size_t out = 1;
	while (Num /= 10) ++out;
	return out;
}; // numlen

Looks simple enough; simply count the amount of times we can divide the number by 10 without the result being zero. This function takes a copy of an int (or long long) so that we don’t have to copy the number inside the body of the function and returns a size_t (unsigned int).

As for the usage example, it’s a bit more complex and might seem a bit “obfuscated” at first, but fear not, I will explain below.

void inttoa(long long int Num, char** RetVal)
{
	size_t neg = (Num < 0);
	size_t len = intlen(Num) + (neg ? 1 : 0); // add one for the "-" character
	size_t i;

	*RetVal = (char *) malloc(sizeof(char) * (len + 1));

	if (NULL == (*RetVal))
		return; // bad malloc

	if (neg)
		Num = -Num; // make pos if neg

	for (i = len; i; (Num /= 10), --i) // loop backwards
		(*RetVal)[i-1] = (char)((Num % 10) + '0'); // add modulo to char zero

	if (neg)
		(*RetVal)[0] = '-'; // first char

	(*RetVal)[len] = 0; // last char, null terminator
}; // intttoa

As you might have suspected, this function converts an integer to character string. First, we determine if the number is negative and retrieve its length with the help of the previous function. We allocate a character string with the length determined and start appending a character to the string.

You can use it like so:

char* mystring; // don't allocate, don't do anything
inttoa(42, &mystring); // simply pass it to the function

// do things with the string

free(mystring); // you *do* have to free() the string though

Flattening Multidimensional Arrays

Tags:, 1 Comment »
Edit: Thank you, fixitman for the insightful comment; the code has been fixed to work with non-square arrays as well.

In an effort to produce a better performing multidimensional array, I would like to share the following with you. Say we have a Matrix (or multidimensional array) of 5 x 5 integer elements, M. In order to allocate such an array in C++, we use the following code:
Read the rest of this entry »

CriticalSection wrapper class

Tags:, , , , 2 Comments »

What: A C++ wrapper around both WINAPI (Microsoft Windows) and PThreads (POSIX threads) functionality.
Why: To abstract cross platform functionality.
Remarks: On windows, CRITICAL_SECTION objects cannot be shared cross-process. This means that the class is tied to your application or DLL process. Comments are in Doxygen/Javadoc style.
Read the rest of this entry »

One to watch: MSDN Code Gallery

Tags:, , No Comments »

While reading Betsy Aoki’s blog I stumbled upon her post about Gotdotnet being dead. I’m not crying about it, I barely knew that site, but when you go to the old URL you’ll be presented with what seems to be Microsoft’s implementation of a resource dump: the MSDN Code Gallery — which is pretty neat.

Sure, there are only 4 C++ related things so far but if you’re a .NET programmer this site is pretty nifty, it even allows you to post your own snippets or host your own discussion. Of course there’s a huge license you’ll have to obey to and you have to donate your firstborn to Microsoft before posting anything… but that’s besides the point ;)