Tag: object factory

Macro Templates, by example.

Posted by on July 13, 2009

Heres an example of what I call “macro templates” in PureBasic. A template encapsulates certain functionality, allowing you to dynamically generate the code in a flexible manner.

This example implements a bare-bones n-vector library using a structure and a static array:

It might look strange/complicated at first, but once you read it you’ll realize it’s fairly simple.

Let’s see the usage of this particular template:

Cool, huh?. And you can define any amount of elements with any basic type.

Of course we sacrificed speed for flexibility. In those cases where we have to define n-vectors, this would be an ideal solution. For everything else, I suggest a specific library, such as my vec3 macro lib.

Using this principle you can abstract almost anything, within reason. One good example is my object factory template. ¬†Ideally one would have arrays, lists, etc. Implemented in this very same way, in such case the possibilities would be endless and you’d be able to define dynamic lists/arrays inside structures, etc.

I strongly advice you to implement at least one of those templates, even if it’s just for an exercise.

Having the extra tools can’t hurt!


VEC3 and EasyObject Libraries updated.

Posted by on May 10, 2009

Today I updated both VEC3 and EasyObject, the download links remain the same.


VEC3 got angle helpers (now you can easily find the angle between two vec3, etc) as well as a curve function to smoothly interpolate variables (you can see it in action in one of the new examples where an enemy turret slowly aims toward the player).


EasyObject now allows you to send a user variable on all iterators. New example provided (particles)