<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Guillaume Chereau Blog</title>
    <subtitle>A blog about coding, 3d rendering, C, OpenGL, and other things I care about.</subtitle>
    <link href="https://blog.noctua-software.com/atom.xml" rel="self" />
    <link href="https://blog.noctua-software.com" />
    <id>https://blog.noctua-software.com</id>
    <updated>2019-07-04</updated>

    
    <entry>
        <title>C++ is not cross platform anymore</title>
        <link href="https://blog.noctua-software.com/cpp-is-not-cross-platform-anymore.html" />
        <id>https://blog.noctua-software.com/cpp-is-not-cross-platform-anymore.html</id>
        <updated>2019-07-04</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>As of 2019, C and C++ are the only programming languages that are supported by
virtually any platforms on the market.  For example both iOS and android
support compiling C and C++ directly as part of their official IDEs.</p>
<p>This is one of the reason why I decided to write my voxel editor <a href="https://guillaumechereau.github.io/goxel/">Goxel</a> in
C99: I wanted to be able to run it on Linux, Mac, Windows, iOS, and now I am
working on a Android port as well.</p>
<p>While the code of goxel is mainly C99, there are a few external libraries
that I use that are written in C++.  I compile them directly alongside
the code, so I don't have to care about binary release or ABI compatibility.</p>
<p>In theory this is great, except that recently I get more and more difficulties
making C++ work in a cross platform way.  The C++ that I learned a long time
ago (basically C with some extra syntax for class and templates) has been
updated to a more modern language with the C++11, C++14 and C++17 versions.
Those new versions added features like lambda, variadic, templates, binding
declarations, etc...  The standard C++ library (almost considered a part of the
language) has also seen a lot of changes.</p>
<p>The difficulty comes from the fact that not all C++ features are
implemented by all toolchains.  For each version of gcc and clang (the two most
popular C++ compilers) there is a set of features that are supported, and
it's almost impossible to keep track which one.  Also different versions will
have some subtle differences in how they interpret the specification.</p>
<p>This doesn't matter if you work on a fixed environment: you can just use
whatever feature is available with the toolchain you use.  But if you want
to be able to port your code to other platform, you can only use the smallest
subset of C++ that is supported by all of those platforms.</p>
<p>Because of this I run into troubles in goxel.  Here is an example of build
that worked on my machine, but failed on travis continuous integration server:
<a href="https://travis-ci.org/guillaumechereau/goxel/jobs/553716829">https://travis-ci.org/guillaumechereau/goxel/jobs/553716829</a></p>
<p>One of the errors here is:</p>
<pre><code>In file included from src/yocto.cpp:56:
src/../ext_src/yocto/yocto_shape.cpp:1632:42: error: no matching function for
      call to 'size'
  solver.graph.reserve(size(positions) + size(edges));
</code></pre>
<p>This seemed to indicate that <code>std::size</code> wasn't recognised by clang 7, but
in fact some local tests show me that it is.  Maybe the <iterator> header
wasn't properly included?  Or the compiler didn't try the <code>size</code> templated
function from std because some other size function have been defined?
It is really hard to tell, and I couldn't reproduce this on my computer even
with clang 7.</p>
<p>An other error from the same travis failed build:</p>
<pre><code>src/../ext_src/yocto/yocto_image.cpp:339:9: error: cannot decompose this type;
  'std::tuple_size&lt;const yocto::vec4f&gt;::value' is not a valid integral
  constant expression
auto&amp; [a, b, c, d] = abcd;
</code></pre>
<p>Once again, this is hard to tell how to fix that.  Different versions of
clang and gcc could have different idea of what is an integral constant
expression.</p>
<p>Since all those errors are sent to me by email from travis servers, I have
no way to test them by myself, I can only attempt to fix the code and submit
a new version until it works.</p>
<p>At least with C (or C99), I am usually certain that if the code compile with
one version of the compiler it will with all the others.  There are a few
language extension to avoid (like gcc nested function, of clang blocks)
but that's pretty much it.</p>
<p>The solution is probably to avoid most 'modern' C++ feature and only use
those that have been proved to work well everywhere.  I urge people who write
open sources libraries to avoid using C++ features as much as possible.
Consider using C99 instead.</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Lua vs Duktape for embedded scripting language</title>
        <link href="https://blog.noctua-software.com/lua-vs-duktape.html" />
        <id>https://blog.noctua-software.com/lua-vs-duktape.html</id>
        <updated>2018-12-22</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>I recently started to see how to integrate a generic script language into
my voxel editor <a href="https://guillaumechereau.github.io/goxel/">Goxel</a>.</p>
<p>I want to use something light and easily embeddable into a C application.  One
obvious choice for this is to use <a href="https://www.lua.org/">Lua</a>, but I also wanted to try <a href="https://www.duktape.org/">Duktape</a>,
a lightweight JavaScript interpreter.  In this post I'll give some of my
conclusions after trying both.</p>
<h2>1. SIMPLICITY TO EMBED</h2>
<p>Both Lua and Duktape are relatively easy to embed into a C application.  They
use the same stack based approach that is both flexible and simple in term of
API.  The C functions you want to expose need to take a stack object from
which you can retrieve the arguments passed to the function.  Output from
functions are also passed into a stack.</p>
<p>Duktape API is almost identical to Lua, so there is not much to compare here.
I tend to find Duktape API slightly simpler, mostly because the library
is provided as a single C file with a corresponding header file.</p>
<h2>1. DOCUMENTATION</h2>
<p>In my opinion Duktape has a better documentation than Lua.  Most of the time
just reading the comments in the header file is enough to find what a
function does.  The online doc is using nice diagrams to explain how each
function will affect the stack.  This is really helpful since it's not always
obvious if a call will pop it's arguments from the stack or not.</p>
<p>Lua's website contains a link to a free version of the book 'programming in
Lua', but it only covers Lua 5.2 and so some of the examples are not working
anymore.</p>
<h2>2. LANGUAGES</h2>
<p>Lua interprets it's own language, while Duktape is just one of the many
interpreters of JavaScript (specifically ECMAScript E5).  Both languages have
some good and bad things in my opinion.  JavaScript is probably the most
popular language these days, so it might seem like the obvious choice.</p>
<p>Things I like in Lua: the meta-object model is quite clever.  The language
uses a special syntax for method call (using colon instead of a dot),
which makes the conceptual model of objects very natural and flexible.  In
comparison JavaScript object support seems poor.  The introduction of Proxy
objects helps a bit at the cost of an other layer of complexity.</p>
<p>One thing I don't like much in Lua is the fact that the arrays are one indexed.
This is specially annoying for Goxel since the scripts will have to index into
multi dimensional arrays for which zero indexed is the only sane way.  As an
example, this is how you would index a 2d image memory block in Lua:</p>
<pre><code>img[(y - 1) * w + x]
</code></pre>
<p>While in JavaScript you simply have:</p>
<pre><code>img[y * w + x]
</code></pre>
<p>As for javascript, in my opinion the most annoying thing is the confusing
coercion rules.  For example in Javascript:</p>
<pre><code>[1, 2] + [3, 4]
</code></pre>
<p>Evaluate to the string:</p>
<pre><code>'1, 23, 4'
</code></pre>
<p>An other problem with Javascript is that as of ES5, there is no easy
way to create multi line strings.</p>
<h2>3. SPEED</h2>
<p>This is where Lua is the clear winner.  In my test scripts, I got results twice
as fast with Lua than what I get with duktape.  For most scripts that is
probably not an issue, but in Goxel I would like to generate large 3D volumes
from parametric functions, and for this the speed of the script interpreter
is going to be critical.</p>
<h2>CONCLUSION</h2>
<p>At this time, I am still unsure what I should use, though I tend to think I
will go for Lua.  Duktape seems the most future proof choice, since JavaScript
is so popular, but the performances penalty is quite high.  I thought about
trying other JavaScript interpreters, but it seems most of them are too
heavy for my use.</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Creating application icons with inkscape</title>
        <link href="https://blog.noctua-software.com/goxel-icons.html" />
        <id>https://blog.noctua-software.com/goxel-icons.html</id>
        <updated>2018-04-18</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>Yesterday I tried to improve the style of my voxel editor <a href="https://guillaumechereau.github.io/goxel/">goxel</a>.  One thing
I wanted to do in particual is have better looking icons in the UI.</p>
<p>The current icons looked like that on my non retina screen:</p>
<p><img alt="goxel before" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-icons/orig.png" /></p>
<p>This is what I came up with:</p>
<p><img alt="goxel after" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-icons/curr.png" /></p>
<p>It's interesting to work on art for computer screen because for such small size
images, the pixels aliasing quality becomes primordial.  For example in goxel
on a non retina screen, the icons should all have a size of 22x22 pixels.  On a
double density screen, they are 44x44.</p>
<p>When you draw the icons (I use inkscape)
you need to keep in mind the target size, since the rasterisation will make
anything not pixel aligned blurry.</p>
<p>Here is an example of an arrow icon, and what it looks like exported as a
44x44 png image, and then resized to 22x22 using:</p>
<p><img alt="arrow before" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-icons/arrow.png" /></p>
<p>The 22x22 version doesn't look sharp, because the interpolation used when
resizing blurred the separation between the lines.  In order to prevent this
we have to make sure that the source image aligns properly with the final
22x22 image pixel position:</p>
<p><img alt="arrow before" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-icons/arrow2.png" /></p>
<p>Unfortunately inkscape doesn't have live preview of rastersized output, so
it takes some tests to design proper looking icons.  The trick (that I saw
in blender) is to set up the inkscape grid as a fraction of a pixel, with
one major grid line every pixel, that way it is easy to draw lines exactly
centered into a given pixel.  I also use lines thinner than a pixel to make
the result look a bit softer:</p>
<p><img alt="add icon" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-icons/add.png" /></p>
<p><img alt="add icon" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-icons/add2.png" /></p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Porting Goxel to iOS</title>
        <link href="https://blog.noctua-software.com/goxel-ios.html" />
        <id>https://blog.noctua-software.com/goxel-ios.html</id>
        <updated>2017-07-31</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p><img alt="goxel" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel/ios.png" /></p>
<p>I just made the <a href="https://t.co/3H274GxuNL">first release</a> of my voxel editor project <a href="http://guillaumechereau.github.io/goxel/">goxel</a>
for iOS, and I wanted to write a bit about the process.</p>
<p>It's not the first time I work on a mobile port of C/C++ application, so
the process was quite easy.  The only new challenge this time is that I
used swift on the platform code (instead of objective-C).</p>
<p>The global structure of the code consists of a core written in C (or C++),
and a small platform dependant layer on top of it to create the application
context and handle the inputs.</p>
<pre><code>An ugly schematisation of what is going on:
+-----------+           +----------+
| platform  |           | core (C) |
| (swift)   |           |          |
+-----------+           +----------+
     |                       |
     + create context        |
     |                       |
+---&gt;+ loop                  |
|       |                    |
|       +-get inputs         |
|       |                    |
|       +-------------------&gt;+-core_iter(inputs)
|                               |
|native +&lt;----------------------+
|  call |                       .
|       |                       .
|       +----------------------&gt;+
|                               |
|                               |
|       &lt;-----------------------+
|       |
+-------+ End loop
</code></pre>
<p>The important points to keep in mind are:</p>
<ul>
<li>Make sure the C code compiles with clang on iOS.</li>
<li>Separate the inputs listening from the core.</li>
<li>Have a mechanism to call platform dependant functions from the C code.</li>
<li>Be careful with the OpenGL version used.</li>
</ul>
<h1>Compiling the C code</h1>
<p>Compiling the C code to iOS was totally straightforward: I just added the
files to a new xcode project, and all of them compiled directly.</p>
<p>The only issue was that I got many warnings related to numerical type
conversions: I tend to omit casting when I convert a number to a lower
precision type.  On linux, gcc doesn't seem to care, but clang on xcode does.
Since I was not going to change my code for this I just added a rule to ignore
those warnings in xcode build settings.</p>
<p>The reason I didn't have any trouble was because I am very careful about the
dependencies I use in my code.  In the case of goxel, the only external
libraries used are:</p>
<ul>
<li><a href="https://github.com/ocornut/imgui">dear imgui</a> for the UI.</li>
<li><a href="https://github.com/nothings/stb">stb image</a> for png loading and writing.</li>
<li><a href="https://troydhanson.github.io/uthash">uthash</a> for list and hash data structures.</li>
<li><a href="https://github.com/benhoyt/inih">inih</a> for parsing ini files.</li>
</ul>
<p>All those libraries are very small, can be directly compiled as part of a
project code, and have no dependencies other than the standard C library.</p>
<p>Also, by not using C++, but only C99, I ensured that no part of my code
was using language features not implemented properly by all compilers.</p>
<p>A nice feature of xcode is that it gives you by default a header file
(PrefixHeader.pch) that is included before any compiled file.  It's the
prefect place to put some global macro declarations to customize the
application on iOS.</p>
<h1>Inputs and OpenGL context creation.</h1>
<p>The biggest problem when porting an application to a new platform is how to
handle the inputs (keyboard, mouse position and state, etc).  On the desktop
version, goxel relies on <a href="http://www.glfw.org">glfw</a>, but on iOS this is not available.</p>
<p>To make it easy, the best way it to clearly separate the inputs capture
from the core.  In the case of goxel, all the inputs are passed to the core
using a single data structure instance (<code>struct inputs</code>):</p>
<pre><code>typedef struct {
    vec2_t  pos;
    bool    down[3];
} touch_t;

typedef struct inputs
{
    int         window_size[2];
    float       scale;
    bool        keys[512]; // Table of all the pressed keys.
    uint32_t    chars[16];
    touch_t     touches[4];
    float       mouse_wheel;
    int         framebuffer; // Screen framebuffer
} inputs_t;
</code></pre>
<p>The platform specific code is in charge of updating this structure at each
iteration and pass it to the core iter function.  The structure is also
used to pass the OpenGL context information to the core: screen size,
framebuffer index, and scale (for retina display).</p>
<h1>C and swift communication</h1>
<p>The <code>inputs_t</code> structure is unfortunately not totally enough.  Sometime
we also need to be able to call platform specific code from the C code.</p>
<p>For example, when the user double tap on an input widget, we want to
open the iOS virtual keyboard.  So we need to somehow call a swift function
from the C code.</p>
<p>It's not really possible to expose a swift function to C, but it's possible
to convert to swift lambda to a C function pointer, and then have the C
code call it.</p>
<p>So in order to expose platform dependant function to C, all we need to do
is to maintain a list of callback functions in the C code, and initialize
them with swift lambdas.  The only trick is that if we want to use
class methods, we also need to pass a pointer to the object instance
as an argument to the callback.</p>
<p>In the C code:</p>
<pre><code>struct {
    /* ... */

    // Used to communicate with swift.
    struct {
        void *user;
        void (*show_keyboard)(bool hasText, void *user);
        /* ... Other callbacks ... */
    } callbacks;
} goxel_t;
</code></pre>
<p>In the swift code:</p>
<pre><code>self.goxel.callbacks.user = Unmanaged.passUnretained(self).toOpaque()
self.goxel.callbacks.show_keyboard = { (hasText, user) in
    let this = Unmanaged&lt;ViewController&gt;
                    .fromOpaque(user!)
                    .takeUnretainedValue()
    /* ... Rest of the function */
}
</code></pre>
<p>Now we can call the function from anywhere in the C code:</p>
<pre><code>    goxel-&gt;callbacks.show_keyboard(true, goxel-&gt;callbacks.user);
</code></pre>
<h1>OpenGL</h1>
<p>In order to make goxel multi platform, I decided to restrict myself to
OpenGL ES2.  It has some annoying limitations, but ES2 is implemented on
pretty much all possible platforms (including javascript).</p>
<p>I still had to profile my shaders to make sure that they would not be too
slow on a phone.  It turned out that the shadow map was actually slowing
down the rendering, so I disabled it for the moment in the mobile version.</p>
<h1>Conclusion</h1>
<p>Writing cross platform application is still an unsolved problem.  Currently
one common approach is to to use html 5 for its good widget support, this is
however still too slow for resource heavy applications.  If having a simplistic
user interface is acceptable, writing the application in C is in my opinion a
valid option.</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Generating the dodecahedron rotation group with python</title>
        <link href="https://blog.noctua-software.com/dodecahedron.html" />
        <id>https://blog.noctua-software.com/dodecahedron.html</id>
        <updated>2017-06-19</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>I was wondering how to compute the rotations for all the faces of the regular
dodecahedron (polyhedron with 12 faces).</p>
<p>You can find many resources online about the symmetry group of the
dodecahedron, but not much practical information about how you would use it in
you code, this is specially annoying for non mathematicians like me who
don't want to go too far down the rabbit hole of group theory just to
get my 12 rotations.</p>
<p>Specifically in my case, I was looking for a way to generate a set of twelve
rotations matrices (or quaternion), one for each face.</p>
<p>The first thing to realize is that since each face can be oriented in five
different way the cardinal of the group of all the rotations is actually
sixty (60).</p>
<p>The whole group can be generated from two rotations. Almost any would do,
as long as they don't generate the same subgroup.</p>
<p>I picked the easiest ones I could think of:</p>
<pre><code>a = Rx(72°)
b = Rx(-36°) * Rz(116.56°)
</code></pre>
<p>Now from those two rotations we can generate the whole set of 60 rotations.</p>
<p>The idea is to start with a set containing only those two rotations and the
identity, and then iteratively apply <code>a</code> and <code>b</code> until the set does not
grow anymore.</p>
<p>The very good trick from group theory, is that instead of working with a set of
rotation matrices, we can map each rotation to a permutation of the face, and
then do all the math in the permutation space.</p>
<p>Using the numbering scheme represented here:</p>
<p><img alt="dodecahedron" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/dodecahedron/faces.png" />
<em>(Image from wikipedia)</em>.</p>
<p>I can write <code>a</code> and <code>b</code> as a mapping function of faces.  Since each face
will move the same way as it opposing face, I only need to use 6 faces instead
of 12 (later we can add the opposite faces by multiplying the group with the
two elements group mapping a face to its opposite).  Visually I find that my
rotations map to the permutations:</p>
<pre><code>a = (0, 2, 3, 4, 5, 1)
b = (2, 4, 5, 3, 0, 1)
</code></pre>
<p>(Here I represent a permutation as a tuple, such that the value at index <code>i</code>
gives the mapping of the face <code>i</code>), so the identity rotation can be matched to
the permutation <code>e</code>:</p>
<pre><code>e = (0, 1, 2, 3, 4, 5)
</code></pre>
<p>From this, I wrote a small python script that would generate all the
60 possible permutations starting from <code>a</code>, <code>b</code> and <code>e</code>.</p>
<p>Since I only want to get one rotation for each face, I also filtered the
result so that the face 0 maps to each of the 6 other faces only once.</p>
<pre><code>#!/usr/bin/python

e = (0, 1, 2, 3, 4, 5)
a = (0, 2, 3, 4, 5, 1)
b = (2, 4, 5, 3, 0, 1)

def op(a, b):
    return tuple( a[b[i]] for i in range(6) )

def iter(g):
    gg = g.copy()
    for x, xr in g.items():
        for y, yr in g.items():
            xy = op(x, y)
            if xy not in g:
                gg[xy] = xr + yr
    return gg

g = {e: '', a: 'a', b: 'b'}

while True:
    size = len(g)
    g = iter(g)
    if len(g) == size: break

assert len(g) == 60

for i in range(6):
    print i, sorted([r for x, r in g.items() if x[0] == i],
                    key=lambda x: len(x))[0]
</code></pre>
<p>Running the script gives us:</p>
<pre><code>$ ./test.py

0
1 abb
2 b
3 ab
4 aab
5 bb
</code></pre>
<p>This gives us the sequence of rotations <code>a</code> and <code>b</code> to apply to get
each of the dodecahedron's faces once.</p>
<p>We still need to add all the opposing faces that we ignored, but this
is trivial by doing a rotation of 180° along z and 36° along x.</p>
<p>I used <a href="https://github.com/guillaumechereau/goxel">goxel</a> procedural tool to generate the dodecahedron by successive
subtraction on the sphere:</p>
<pre><code>shape test {
    sphere[]
    rem []
    rem [rx 72 rz 116.56 rx -36 rz 116.56 rx -36]
    rem [rz 116.56 rx -36]
    rem [rx 72 rz 116.56 rx -36]
    rem [rx 72 rx 72 rz 116.56 rx -36]
    rem [rz 116.56 rx -36 rz 116.56 rx -36]
}

shape rem {
    [sub]
    cube[x 0.75]
    cube[x -0.75]
}

shape main {
    [antialiased 1]
    [light -0.5 sat 0.8 hue 128]
    test[s 128]
}
</code></pre>
<p><img alt="goxel" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/dodecahedron/goxel.png" /></p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Goxel Voxel Editor Internals - Part 2: voxel data structure</title>
        <link href="https://blog.noctua-software.com/goxel-internals-part2-data-structure.html" />
        <id>https://blog.noctua-software.com/goxel-internals-part2-data-structure.html</id>
        <updated>2017-03-13</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>In this second part of my series about goxel voxel editor internals, I will
talk about the way voxel data is stored in memory.</p>
<p>As usual, all the code is open source and accessible on the <a href="https://github.com/guillaumechereau/goxel">github repo</a>.</p>
<p>The voxel models are composed of many individual voxel, each of them having
a color.</p>
<p>For the structure of the voxel I wanted:</p>
<ul>
<li>
<p><strong>Optimized memory usage</strong>: if a model is large but composed of few voxels
  I don't want to use extra memory for the empty spaces.</p>
</li>
<li>
<p><strong>Spatial and temporal locality of the data</strong>: this is important to take
  advantage of CPU cache.  The algorithms running on the voxel should not
  have to jump around the memory too much.  Imagine for example that the
  voxel are stored in a simple 3d array of size <code>W * H * D</code>.  The distance
  between two adjacent voxels in memory can be as high as <code>W * H</code>.  This is
  not optimal if <code>W * H * size(voxel)</code> exceed the CPU cache size.</p>
</li>
<li>
<p><strong>Eventual future GPU storage</strong>: For the moment the GPU is only used for
  actual rendering, so it means I have to spend time sending the voxel data
  to the GPU.  Eventually in the future I would like to be able to store the
  voxels directly in the GPU memory.  Can I map the data into 2d textures?</p>
</li>
<li>
<p><strong>Unlimited model size</strong>.</p>
</li>
</ul>
<p>So let see how it is done in goxel.  The file we want to look at is <a href="https://github.com/guillaumechereau/goxel/blob/master/src/goxel.h">goxel.h</a>,
as it contains the C structure definitions.</p>
<h4>Blocks</h4>
<p>The first structure of interest if <code>block_data_t</code>:</p>
<pre><code>#define BLOCK_SIZE 16

typedef struct block_data block_data_t;
struct block_data
{
    int         ref;
    uint64_t    id;
    uvec4b_t    voxels[BLOCK_SIZE * BLOCK_SIZE * BLOCK_SIZE];
};
</code></pre>
<p>This is the primordial voxel storage structure in the code.  The important
part is the matrix of 32 bits RGBA values, of fixed size 16x16x16 (attribute
<code>voxels</code>).</p>
<p>Why the size 16x16x16 (= 4096)?  It's because this number has an interesting
property: it can be expressed both as a cube and a square: 16^3 = 64^2.
That way a block data can be used both as a 3d voxel volume of size 16, or a 2d
image of size 64.  This is used for example when saving a file, to compress
the data as png images.</p>
<p>Now, <code>16^3</code> was not the only possible choices.  I let you convince yourself
that any square number would have worked.  If we restrict ourself to power of
two numbers, this mean we could have used for the size of the cube any number
of the form: <code>2^(2*n)</code>: <code>1</code>, <code>4</code>, <code>16</code>, <code>64</code>, <code>256</code>, ...</p>
<p>I picked 16 because I thought it would be a good compromise between memory
and CPU usage, we'll see why.</p>
<p>A block data is itself included into a structure of type <code>block_t</code>:</p>
<pre><code>typedef struct block block_t;
struct block
{
    UT_hash_handle  hh;
    block_data_t    *data;
    vec3_t          pos;
    int             id;
};
</code></pre>
<p>The block structure adds a few things around the block data:</p>
<ul>
<li>A 3d position (argument <code>pos</code>).</li>
<li>A hash handle (<code>hh</code>), so that it can be stored into a hash table (if you come
  from C++, don't be scared by the fact that we store container structure into
  the contained type!)</li>
<li>An id.  We'll ignore this in this post.</li>
</ul>
<p>To understand why we are using two structures instead of just one, is because
voxel blocks in goxel are using <em>copy on write</em> (COW).  Here is how it works:</p>
<p>When we create a new <code>block_t</code>, we also allocate memory for its data (as a
malloced <code>block_data_t</code>), and we set the reference counter (the <code>ref</code> attribute
of <code>block_data_t</code>) to one, meaning that only on block is using this data.</p>
<p>If we call the function <code>block_t *block_copy(const block_t *other)</code> to make a
copy of a block, we don't actually copy the data, but instead we just have the
two blocks points to the same <code>block_data_t</code>, and to remember that this data is
used by two blocks, we also increments its ref counting attribute.</p>
<p>If we modify a block (for example by calling the function <code>block_fill</code>),
there are two possible cases:</p>
<ul>
<li>If the block data pointed to by the block has a reference counting of one,
  then we know that only this block is using the data, and we can modify it
  directly.</li>
<li>If instead the data reference counter is greater than one, it means an other
  block is sharing the data, and so we cannot directly modify it.  Instead
  we first copy the data into a new <code>block_data_t</code> and decrease the ref
  counting of the original data.</li>
</ul>
<p>The function doing this is called <code>block_prepare_write</code> in <a href="https://github.com/guillaumechereau/goxel/blob/master/src/block.c">block.c</a>.
(It also does some bookkeeping of id numbers, but we are not interested in
that now).</p>
<pre><code>static void block_prepare_write(block_t *block)
{
    if (block-&gt;data-&gt;ref == 1) {
        return;
    }
    block-&gt;data-&gt;ref--;
    block_data_t *data;
    data = calloc(1, sizeof(*block-&gt;data));
    memcpy(data-&gt;voxels, block-&gt;data-&gt;voxels, N * N * N * 4);
    data-&gt;ref = 1;
    block-&gt;data = data;
    block-&gt;data-&gt;id = ++goxel-&gt;next_uid;
    goxel-&gt;block_count++;
}
</code></pre>
<p>Using copy on write is very powerful, because it means that making duplicates
of blocks is basically free in term of memory usage.</p>
<h4>Meshes</h4>
<p>A block has a fixed size of <code>16^3</code> voxels.  But of course we want to support
larger models, so we need to have a data structure that contains several
blocks.  In goxel I use a hash table (hence the <code>hh</code> attribute in <code>block_t</code>)
indexed by block position.  The hash allows to quickly find a block given
a position, and we don't store empty blocks to save space.  This structure
is quite flexible because we don't have to waste memory on the empty areas of a
model.  But there is an issue: I mentioned that we often need to access the
neighboring voxel of a given voxel. When the voxel is inside a block this
is not a problem we can just check the memory around it, but on the side of
a block, we would have to access the adjacent blocks, and the code would
be full of special branches for this case.  It would also break the spacial
locality of the algorithm since the block data might not be near each other.</p>
<pre><code>Logical blocks position
+-----+-----+-----+
|16^3 |16^3 |16^3 |
|     |     |     |
+-----+-----+-----+

Internal memory structure
+-----+       +-----+   +-----+
|16^3 |------&gt;|16^3 |--&gt;|16^3 |
|     |       |     |   |     |
+-----+       +-----+   +-----+
</code></pre>
<p>To fix this, I positions the blocks in such a way that each adjacent blocks
<em>overlap</em> by a distance of two voxels.</p>
<pre><code>&lt;-----&gt;
    &lt;-----&gt;
+---+-+---+
|   |x|   | Two blocks overlap.
|   |x|   | X are shared voxels.
+---+-+---+
</code></pre>
<p>Now, all we have to do is ignore the borders when rendering a block.  This
work only if we don't use a neighbor value to update a given block, in that
case we would still need to check the other blocks.</p>
<p>Because of this, a block that contains 16^3 voxels, actually only represents
a 14^3 area of a mesh.  This is an annoyance, but really makes many parts
of the code easier.</p>
<p>The mesh structure looks like this:</p>
<pre><code>typedef struct mesh mesh_t;
struct mesh
{
    block_t *blocks;
    int next_block_id;
    int *ref;
    uint64_t id;
};
</code></pre>
<p>The <code>block</code> attribute is actually a hash table of blocks.  Meshes also use copy
on write, albeit in a slightly more complicated way since I didn't want to use
a separate structure for the data.  This is why the ref counter attribute is a
pointer here: it is shared by all the meshes using the same blocks.  It
looks confusing but it really works the same.</p>
<h4>Conclusion</h4>
<p>We have seen how goxel internally stores the voxel data used in the models.
The copy on write mechanism, make duplicating a mesh basically a free
operation.  For example this is why it is quite easy to implement undo/redo in
goxel: at every step we can just make a copy of the mesh and store it in a
list.</p>
<p>To simplify the algorithm, we split the meshes into blocks of fixed size of of
<code>16^3</code>. Modifying part of a mesh only requires to update the blocks affected.
This is also a great optimization because we can now <em>cache</em> rendering data for
a given block.  I'll talk about this in the next part about OpenGL rendering.</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Goxel Voxel Editor Internals - Part 1: voxels and pixels</title>
        <link href="https://blog.noctua-software.com/goxel-internals-part1-voxel.html" />
        <id>https://blog.noctua-software.com/goxel-internals-part1-voxel.html</id>
        <updated>2017-03-09</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>This is the first in a series of posts I want to write about the technical
aspect of writing a voxel engine / editor.  I wrote a <a href="http://guillaumechereau.github.io/goxel">free and open source
voxel editor</a> available on github, and I wanted to share the things I learned
in the process.</p>
<p>Today's post is just an introduction, where I am going to talk about the
evolution of computer graphics and the advent of voxels.</p>
<h3>The evolution of 2d graphics, from vectorial to bitmap</h3>
<p>At the dawn of computer graphics, it was generally costly to store images as an
array of pixels, since the available memory was too little.  Instead vectorial
art was used for large images.</p>
<p>Screenshot from the game <a href="https://en.wikipedia.org/wiki/Space_Quest_III">Space Quest III</a>, released in 1989 (in my opinion the
best in the series):</p>
<p><img alt="Space Quest III" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel/spacequest.png" /></p>
<p>In this game, the backgrounds were not saved in conventional bitmap images, but
as vectorial data.  This youtube video (by jack) shows how the construction
looks like:</p>
<iframe width="560" height="315"
        src="https://www.youtube.com/embed/1QPVfGq19f8?ecver=1"
        frameborder="0" allowfullscreen=""></iframe>

<p>A vectorial image can potentially be much smaller, since you only save data
about the individual shapes.</p>
<p>As computer CPU and memory improved, it became possible to store images
directly as 2d arrays of pixels.</p>
<p>Screenshot of <a href="https://en.wikipedia.org/wiki/Space_Quest_IV">Space Quest IV</a>, released in 1991, that was this time using
bitmap graphics:</p>
<p><img alt="Space Quest IV" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel/spacequest4.png" /></p>
<p>These days both ways of storing images, vectorial and bitmap coexists and got
standardized in different file formats.  For example png and jpeg store
images as bitmap, while svg used vectors.</p>
<p>An important distinction between bitmap and vector images is that as you
zoom in a bitmap image, you start to see the individual pixels, while
a vectorial image always looks nice at any resolution.</p>
<p><img alt="Pixelation" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel/pixelation.png" /></p>
<h3>3d graphics and voxels</h3>
<p>For 3d graphics, until recently the only mainstream format was vectorial.
A 3d scene is composed of individual shapes represented by vertex coordinates.</p>
<p>Picture of the <a href="https://www.blender.org">blender</a><a href="suzanne">monkey shape (suzanne)</a> where
we can see the individual polygon shapes:</p>
<p><img alt="Blender Monkey" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel/blender-monkey.png" /></p>
<p>As with 2d vectorial images, this is a problem if we try to apply a boolean
operation on a volume, because it can quickly increases the complexity of the
model:</p>
<p><img alt="Blender Cube" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel/blender-cube-carved.png" /></p>
<p>We can see that the number of vertices increases in the operation.  Also the
algorithms doing the tessellations are usually not fast. For this reason
not many real time video games allow for destructive manipulation of the
environment in a realistic way.</p>
<p>Just like with 2d images, we can also store 3d images as a matrix of pixels,
though in that case we call them voxels (volumetric pixel).</p>
<p>Here is what a carved 3d cube looks like when rendered as voxels:</p>
<p><img alt="Goxel Cube" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel/goxel-cube-carved.png" /></p>
<p>It looks voxelated, because the resolution is not high enough, if we increase
the number of voxels, and use a smoothing rendering algo (I'll talk about it in
a future post) we can get something that looks more realistic:</p>
<p><img alt="Goxel Cube Marching Cube" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel/goxel-cube-carved-mc.png" /></p>
<p>The interesting part about it, is that any kind of boolean operation on
a voxel model is possible without any restriction, and at a fixed cost.</p>
<p>Just like with 2d, I think we might start to see more voxel content, because
computer performances reaches a point where it becomes feasible, even in real
time rendering.</p>
<p>My guess is that AAA games are probably not going to use voxel rendering as
primary engines any time soon though, since vectorial 3d models are still far
from reaching the complexity at which it would become simpler to use voxels.</p>
<p>That being said, outside of the realistic looking games market, we see the
emergence of voxel games.  The most famous examples being of course
minecraft (and <a href="https://noctua-software.com/voxel-invaders">voxel invaders</a> ;) )</p>
<p>In the next posts, I will detail the technical challenges faced when doing
voxel graphics and go into the details of goxel code.</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Introducing Noctua Sky</title>
        <link href="https://blog.noctua-software.com/noctuasky.html" />
        <id>https://blog.noctua-software.com/noctuasky.html</id>
        <updated>2017-03-01</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p><img alt="Noctuasky" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/noctuasky.png" /></p>
<p>I just released <a href="https://noctuasky.com">Noctua Sky</a>, a new project I am working on.</p>
<p>This is an online planetarium (or sky map) software similar to the open source
project <a href="http://www.stellarium.org">Stellarium</a> made by my brother.</p>
<p>The goal of the project is ultimately to provide a set of tools and API for
amateur astronomers to share their observations online.  The first step toward
this is to create a JavaScript planetarium that can run directly on the
browser.</p>
<p>Technically the application was written in C, and then compiled to JavaScript
using <a href="http://kripken.github.io/emscripten-site/">Emscripten</a>.  I already wrote about Emscripten in <a href="http://charlie137-2.blogspot.tw/2013/01/voxel-invaders-ported-to-javascript.html">an other post</a>
regarding a video game I made before.</p>
<p>The code is not open yet, but if you are interested in using the JavaScript
client in your own website please contact me.</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Swift vs C++: lambda functions</title>
        <link href="https://blog.noctua-software.com/swift-vs-cpp-lambdas.html" />
        <id>https://blog.noctua-software.com/swift-vs-cpp-lambdas.html</id>
        <updated>2016-11-12</updated>
        <summary>Swan vs Platypus</summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>Recently I got interested in the new Apple language <a href="https://swift.org/">Swift</a>, that will
probably replace objective-c as the language of choice for native iOS
and OSX applications.</p>
<p>There are many things I like in Swift, and also other things I don't like.
But one thing that I really enjoy is the support for <a href="https://en.wikipedia.org/wiki/Anonymous_function">lambdas</a>, specially
compared to the way it works in C++.</p>
<p>Why do I think the lambdas in swift are better?  Let's have a look:</p>
<h1>A common use case for lambdas</h1>
<p>In my opinion, one of the most important usage of lambdas is as a syntax
sugar for non-deferred callback functions.  By non-deferred I mean that
the callback will only be used during the call of the function it is
passed to (deferred callbacks are also interesting, but I think I actually use
them less often).</p>
<p>Let's imagine that I have a module that handles a collection of objects.
For the sake of the argument, let say it's an interface to a database of
cities.</p>
<p>The API provides a function that allows to search the data for all the
cities matching a given name.  We cannot return a single city, because
several cities might match the request.  We could return a list, but there
is a cost to creating a new list for each request.  So instead we can use
a callback function, and the API (in C) might look like that:</p>
<pre><code>// Iter all the cities matching a given name.
void city_search(const char *name,
                 void (*f)(const struct city *city));
</code></pre>
<p>It can be used like that:</p>
<pre><code>static void on_city(const struct city *city) {
    printf("got a city: %s, %s\n",
           city-&gt;name, city-&gt;country);
}

void list_cities(const char *name) {
    city_search(name, on_city);
}
</code></pre>
<p>I have two issues with this.  The first one is that the code is split into two
functions, even though they are logically both part of the same logical block.
When reading the code, it might not be obvious that the <code>on_city</code> function is
simply a helper for the <code>list_city</code> function.</p>
<p>The second problem is that we might want to use local variables of the
<code>list_cities</code> function inside the callback.  In order to do this the
usual way in C is to add an extra <code>void*</code> argument in the callback to pass
any value from the caller to the callback:</p>
<pre><code>void city_search(const char *name,
                 void (*f)(const struct city *city),
                 void *arg);

static void on_city(const struct city *city, void *arg) {
    const int x = *((int*)arg);
    printf("got a city: %s, %s %d\n",
           city-&gt;name, city-&gt;country, x);
}

void list_cities(const char *name) {
    int x = 10;
    city_search(name, on_city, &amp;x);
}
</code></pre>
<p>It works, but it's not very pretty.</p>
<h1>C++ 11 lambdas</h1>
<p>Then comes <a href="https://en.wikipedia.org/wiki/C%2B%2B11">C++ 11</a>, and it's lambda support.  If we start with the same
original API:</p>
<pre><code>void city_search(const char *name, void (*f)(const struct city *city));
</code></pre>
<p>We can reimplement the <code>list_cities</code> function using a lambda instead of a
callback:</p>
<pre><code>void list_cities(const char *name) {
    city_search(name, [](const struct city *city) {
        printf("got a city: %s, %s\n",
               city-&gt;name, city-&gt;country);
    });
}
</code></pre>
<p>This is already much better.  But there is a problem.  If we want to
use some local variables in the lambda, the compiler will complain that
our lambda has no capture-default, and so doesn't accept local variables.</p>
<p>After reading the documentation we figure out that we need to use the <code>[=]</code>
syntax instead of the <code>[]</code> for the lambda.  If we want to capture the local
variable by reference instead, we can use <code>[&amp;]</code>:</p>
<pre><code>void list_cities(const char *name) {
    int x = 10;
    // Error: city_search doesn't match the lambda.
    city_search(name, [=](const struct city *city) {
        printf("got a city: %s, %s %d\n",
                city-&gt;name, city-&gt;country, x);
    });
}
</code></pre>
<p>But this doesn't work either, because while converting a non capturing
lambda to a function pointer works fine, we cannot convert a capturing
lambda to a function pointer!</p>
<p>As far as I know, the simplest way to make it work properly is to use the
<code>&lt;functional&gt;</code> std library and change the syntax of the API to this hideous one:</p>
<pre><code>#include &lt;functional&gt;
void city_search(const char *name,
                 std::function&lt;void(const struct city *city)&gt; f);
</code></pre>
<p>Or, if we don't want or cannot use the std lib, we can also do the abomination
of turning the function into a template:</p>
<pre><code>template&lt;class F&gt;
void city_search(const char *name, F f) {
    ...
}
</code></pre>
<p>So in the end we have to decide between three different choices of function
for our API, none of them being simple and convenient.</p>
<h1>Lambdas in swift</h1>
<p>In swift, the same code would be as follow:</p>
<pre><code>func city_search(_ name: String, callback: (City) -&gt; Void) {
    ...
}

func list_cities(name: String) {
    let x = 10
    city_search(name) {
        print("got a city \($0.name) \($0.country) \(x)")
    }
}
</code></pre>
<p>It is much better:</p>
<ul>
<li>
<p>The lambda can access local variables by default, there is no need
  for a special syntax for it.</p>
</li>
<li>
<p>Since the last argument of <code>city_search</code> is a function, we can put the
  lambda <em>after</em> the closing paren!  This makes the code more readable, and
  the function looks similar to the native control structures (if, while,
  ...)</p>
</li>
<li>
<p>For such a small lambda, we can skip the argument declaration and use <code>$0</code> as
  the default name for the city variable.  This is specially nice for example
  when doing array mapping function one liners:</p>
<pre><code>var x = [1, 2, 3, 4]
var y = x.map {$0 * 2} // [2, 4, 6, 8]
</code></pre>
</li>
</ul>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Hacking Doom code for transparent POV display</title>
        <link href="https://blog.noctua-software.com/doom-on-spino.html" />
        <id>https://blog.noctua-software.com/doom-on-spino.html</id>
        <updated>2016-09-02</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p><img alt="Doom2" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/doom-spino/doom-title.png" /></p>
<p>Doom (the original version), is not only one of the best game ever made,
but it's also a great example of C code.</p>
<p>Thanks to id Software decision to release the code as open source in 1999,
anybody can have a look and modify it.</p>
<p>I had this project of changing the rendering of doom, to add a border
effect to the walls, or cel shading.  You can check the video at the
end of this post to see why I wanted to do this to start with.</p>
<p>This idea of the cel shading algorithm (as described in the <a href="https://en.wikipedia.org/wiki/Cel_shading">Wikipedia
page</a> ) is simple: we apply a Sobel filter to both the
depth and normal buffers of the rendered frame, that we then superpose
to the colors.</p>
<p><img alt="cel shading" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/doom-spino/cel-shading.png" />
(Image from wikipedia)</p>
<p>I used the excellent <a href="https://www.chocolate-doom.org">chocolate-doom</a> port of Doom, since this is the closest
to the original I could find, and it compiles nicely.</p>
<p>My goal is to create my own buffers for depth and normals, and hook a
function to fill them inside the rendering code, then just before the final
blit, to call an other function to perform the Sobel filter and applying to
the final image.</p>
<p>Here is what it will looks like:</p>
<p><img alt="before" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/doom-spino/before.png" /></p>
<p>Before.</p>
<p><img alt="after" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/doom-spino/after.png" /></p>
<p>After.</p>
<p>The difficulty when looking at doom source is that most of the functions
have side effects modifying global variables.  I have to admit it is a bit
ugly, and I suspect that this was an optimization to avoid useless copies
into the stack.</p>
<p>Neither the less, the code is easy to follow, as there is almost no
abstractions and indirections.  Clearly, John Carmack is an adept of the <a href="https://en.wikipedia.org/wiki/Worse_is_better">New
Jersey</a> style of programming.</p>
<p>I came up with this simplified function call tree for the rendering algorithm:</p>
<pre><code>R_RenderBSPNode
    R_Subsector // For each visible subsector
        R_FindPlane
        R_AddSprites  // Fill the vissprites list
        R_AddLine     // For each wall (front to back)
            R_ClipSolidWallSegment
                R_StoreWallRange(start, stop)
                    R_CheckPlane
                    R_RenderSegLoop
                        R_DrawColumn    // What I am looking for
R_DrawPlanes  // Render ceiling and floor.
R_DrawMasked
</code></pre>
<p>What they call 'line' is really a wall surface.  The reason it is a 'line' is
because from a logical point of view, doom walls are just 2d lines extruded
vertically (they never have slopes).</p>
<p>As we would expect, the walls are rendered front to back (function
<code>R_AddLine</code>).  To prevent rendering on top of previous walls, the code
keeps an array of up to 32 ranges (<code>struct cliprange</code>) of already rendered
walls.  Since the front to back order is guarantied, there is no need for
an actual depth buffer.</p>
<p>In my case, what I am looking for is the function <code>R_DrawColumn</code>, that
ultimately renders a column of pixels from a sprite.  It is used for both the
walls and the sprites.</p>
<p>Before the function is called, some global variables are set:</p>
<pre><code>dc_x:            x position.
dc_yl:           Start y position.
dc_yh:           End y position.
dc_iscale:       Texture scale (i.e: view distance).
rw_normalangle:  Normal of the surface angle.
</code></pre>
<p>Those are all I need to maintain the depth and a normal buffers.
In <code>R_DrawColum</code>, I add a call to my own function <code>spoom_AddColumn</code>, that
fills some pre-allocated buffers:</p>
<pre><code>void spoom_AddColumn(int x, int y1, int y2, int z, int a)
{
    double angle = M_PI * a / ANG180;
    int y;
    for (y = y1; y &lt; y2; y++) {
        depthBuffer[y * WIDTH + x] = (double)z / ((uint64_t)1 &lt;&lt; 16);
        normBuffer[0][y * WIDTH + x] = cos(angle);
        normBuffer[1][y * WIDTH + x] = sin(angle);
    }
}
</code></pre>
<p>(Note that at the time when doom was release, such unoptimized code would kill
the performances of the game.  Fortunately this is of no concern for my needs).</p>
<p>Now that my buffers are filled, I just need to process them with a Sobel
filter at each frame, and use the result to add the borders on top of the
color buffer.  I do it in a single function:</p>
<pre><code>// Screen point to the color buffer.
void spoom_Apply(byte *screen)
{
    int i, k;
    gaussianBlur(depthBuffer, tmpBuffers[0]);
    sobel(depthBuffer, tmpBuffers);
    gaussianBlur(normBuffer[0], tmpBuffers[0]);
    sobel(normBuffer[0], tmpBuffers);
    gaussianBlur(normBuffer[1], tmpBuffers[0]);
    sobel(normBuffer[1], tmpBuffers);
    for (i = 0; i &lt; WIDTH * HEIGHT; i++) {
        if (    depthBuffer[i] &gt; 1.0 ||
                normBuffer[0][i] &gt; 0.5 ||
                normBuffer[1][i] &gt; 0.5) {
            screen[i] = 0;
        }
    }
}
</code></pre>
<p>The Sobel and Gaussian filters are implemented with a convolution product:</p>
<pre><code>static void convolve(const double *m, const double *kern, double *dst)
{
    int i, j, i2, j2;
    double v;
    for (i = 1; i &lt; HEIGHT - 1; i++)
    for (j = 1; j &lt; WIDTH - 1; j++) {
        v = 0;
        for (i2 = -1; i2 &lt;= 1; i2++)
        for (j2 = -1; j2 &lt;= 1; j2++) {
            v += m[(i + i2) * WIDTH + j + j2] * kern[(i2 + 1) * 3 + j2 + 1];
        }
        dst[i * WIDTH + j] = v;
    }
}

static void gaussianBlur(double *m, double *tmp)
{
    const double kern[] = {
        1. / 16, 1. / 8, 1. / 16,
        1. /  8, 1. / 4, 1. / 8,
        1. / 16, 1. / 8, 1. / 16,
    };
    convolve(m, kern, tmp);
    convolve(tmp, kern, m);
}

static void sobel(double *m, double *tmp[2])
{
    int i;
    const double kernx[] = {-1. / 1,  0. / 1,  1. / 1,
                            -2. / 1,  0. / 1,  2. / 1,
                            -1. / 1,  0. / 1,  1. / 1};
    const double kerny[] = {-1. / 1, -2. / 1, -1. / 1,
                             0. / 1,  0. / 1,  0. / 1,
                             1. / 1,  2. / 1,  1. / 1};
    convolve(m, kernx, tmp[0]);
    convolve(m, kerny, tmp[1]);
    for (i = 0; i &lt; WIDTH * HEIGHT; i++)
        m[i] = sqrt(tmp[0][i] * tmp[0][i] + tmp[1][i] * tmp[1][i]);
}
</code></pre>
<p>Finally, here is the final result, of me playing doom on a LED POV display.
The walls are much easier to see compared to the unmodified game.</p>
<iframe width="560" height="315"
        src="https://www.youtube.com/embed/CtXzWXebjoM"
        frameborder="0" allowfullscreen=""></iframe>

<p>By the way, me and a friend are working on this project (link:
<a href="http://www.spino.tech">http://www.spino.tech</a>), feel free to ask us any
question about it.</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Unit Tests in C with the constructor attribute</title>
        <link href="https://blog.noctua-software.com/c-unit-tests-constructor-attribute.html" />
        <id>https://blog.noctua-software.com/c-unit-tests-constructor-attribute.html</id>
        <updated>2016-09-02</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>In one of my project, I used GNU <a href="https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes">constructor attribute</a> to add some simple
unit tests to any C file.  I liked this approach, but I don't see many open
source projects using anything similar, so I though I'll describe it here.</p>
<p>Here is an example of a C file containing a function:</p>
<pre><code>...
// Test if a string ends with an other string.
// If any of the string if NULL, return false.
bool str_endswith(const char *str, const char *end)
{
    if (!str || !end) return false;
    if (strlen(str) &lt; strlen(end)) return false;
    const char *start = str + strlen(str) - strlen(end);
    return strcmp(start, end) == 0;
}
</code></pre>
<h1>BASIC UNIT TESTS</h1>
<p>If I add the unit test code in the file:</p>
<pre><code>void my_unit_test(void)
{
    assert(str_endswith("hello", "lo"));
    assert(str_endswith("hello", ""));
    assert(!str_endswith("hello", "ol"));
    assert(!str_endswith("hello", NULL));
    assert(!str_endswith(NULL, "hello"));
}
</code></pre>
<p>Then somewhere in the main file, I can run the test like that:</p>
<pre><code>if (run_tests) {
    my_unit_test();
}
</code></pre>
<p>This could be improved.  The problems with this approach are:</p>
<ul>
<li>The function that executes the tests needs to know about the unit
  test signature.</li>
<li>If we add tests, we have to also add them to the main function.</li>
<li>We want some tests to be executed at each startup.</li>
</ul>
<h1>USING CONSTRUCTOR ATTRIBUTE</h1>
<p>We are going to use gcc constructor attributes to improve the unit test.
In the original file, we add:</p>
<pre><code>#include "tests.h"

...

#if COMPILE_TESTS

// (Notice that the function is now 'static')
static void test_str_endswith(void)
{
    // The orignal unit test
    ...
}

TEST_REGISTER(NULL, test_str_endswith, TEST_AUTO);

#endif
</code></pre>
<p>The file tests.h contains the <code>TEST_REGISTER</code> macro:</p>
<pre><code>enum {
    TEST_AUTO = 1 &lt;&lt; 0,
};

#if COMPILE_TESTS
    void test_register(const char *name, const char *file,
                       void (*setup)(void),
                       void (*func)(void),
                       int flags);
    void tests_run(const char *filter);

    #define TEST_REGISTER(setup_, func_, flags_) \
        static void reg_test_##func_() __attribute__((constructor)); \
        static void reg_test_##func_() { \
            test_register(#func_, __FILE__, setup_, func_, flags_); }
#else
    #define TEST_REGISTER(...)
    static inline void tests_run(const char *filter) {}
#endif
</code></pre>
<p>And we put the code for <code>test_register</code> in a new file (tests.c):</p>
<pre><code>// Tests.c

#if COMPILE_TESTS

typedef struct test {
    struct test *next;
    const char *name;
    const char *file;
    void (*setup)(void);
    void (*func)(void);
    int flags;
} test_t;

static test_t *g_tests = NULL;

void tests_register(const char *name, const char *file,
                    void (*setup)(void),
                    void (*func)(void),
                    int flags)
{
    test_t *test;
    test = calloc(1, sizeof(*test));
    test-&gt;name = name;
    test-&gt;file = file;
    test-&gt;setup = setup;
    test-&gt;func = func;
    test-&gt;flags = flags;
    // Add the test to the global list:
    test-&gt;next = g_tests;
    g_tests = test;
}

static bool filter_test(const char *filter, const test_t *test)
{
    if (!filter) return true;
    if (strcmp(filter, "auto") == 0) return test-&gt;flags &amp; TEST_AUTO;
    return strstr(test-&gt;file, filter);
}

void tests_run(const char *filter)
{
    test_t *test;
    printf("Run tests: %s\n", filter);
    for (test = g_tests; test; test = test-&gt;next) {
        if (!filter_test(filter, test)) continue;
        if (test-&gt;setup) test-&gt;setup();
        test-&gt;func();
        printf("Run %-20s OK (%s)\n", test-&gt;name, test-&gt;file);
    }
}

#endif
</code></pre>
<h1>HOW IT WORKS</h1>
<p>Using the <code>TEST</code> macro, we can register any unit test function and associated
attributes into a global list of unit tests.  The registering is done
automatically at startup time (before main is executed).</p>
<p>Then to run the tests we just need to call the <code>tests_run</code> function, with
an optional filter string.  The function will check for any tests whose name
contains the filter, and run it.</p>
<pre><code>tests_run("str_endswith");
</code></pre>
<p>Some tests can have the <code>TEST_AUTO</code> flag set, in that case we can execute
all of them using the filter "auto".  This allows to always run those tests
at startup with a single line of code:</p>
<pre><code>if (COMPILE_TESTS) tests_run("auto");
</code></pre>
<p>I try to use <code>TEST_AUTO</code> for all the tests that don't take time, so that I
don't have to think about running my tests, they are always executed.</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Keeping notes in unformatted text files</title>
        <link href="https://blog.noctua-software.com/how-to-keep-notes.html" />
        <id>https://blog.noctua-software.com/how-to-keep-notes.html</id>
        <updated>2016-07-25</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>For years I have been looking for the best way to keep organized notes on my
computer(s).  I tried a lot of different approaches, from web based tools to
emacs <a href="http://orgmode.org">org mode</a> and desktop wiki.</p>
<p>Now I think I can say that I found the best way (for my personal case) to do
it: I keep all my notes as unformatted text files, in a single flat directory.</p>
<p>Here is how it works: I have a directory <code>Notes</code> in my computer home folder.
Every time I need to take a note I put it in a text file named <code>&lt;TOPIC&gt;.txt</code>
inside this folder.  Even though I usually use something that looks like
markdown, I do not care about the format of the file, just plain unformatted
text.</p>
<p>So why does this method sticks when all the other failed?  Let see.</p>
<h2>1 Web based wiki</h2>
<p>This was my first approach: I was keeping all my notes in a personal private
wiki.  One advantage is that I could store images.  The big problem was that
it was too slow to open the wiki every time I need to make a small change to
a note (pretty much all the time).</p>
<h2>2 org mode</h2>
<p>After I stopped using a wiki, I turned to <a href="http://orgmode.org">org mode</a>.  I
have to say that org mode is actually quite good, and in fact very close to my
current approach.  I had two problems with org mode: the first is that I spent
too much time making sure my org mode files were correctly formatted, and the
second is that I stopped using emacs (originally to help with carpal tunnel
syndrome).</p>
<h2>3 zim (or other desktop wikis)</h2>
<p><a href="http://zim-wiki.org/">Zim</a> seemed like a good choice for a while, because I
could still edit my notes with any text editor, but I also had the ability to
browse them like a wiki with links and formatting.  The problem was that, just
like with org mode, I found it too troublesome to maintain the formatting,
no matter how simple it was.</p>
<h2>Conclusion</h2>
<p>I have been keeping my note as unformatted text files for years now, I and
really got used to it.  The cool thing about it is that since by design I don't
use any formatting, I can quickly add to a note without triggering my computer
OCD and worrying about it.  If for some reason I want to use some structure in
a note, I just do it on a need basis.</p>
<p>For example some of my note files have special meaning:</p>
<ul>
<li><code>tmp.txt</code> is used to keep short term notes, things that I know I
   won't use after a day or two.</li>
<li><code>todo.txt</code> is just a list of things I have to do.</li>
<li><code>days.txt</code> is a simple diary of my work time.  I use a script to
   automatically parse it for charging my clients.</li>
</ul>
<p>Since the notes are in plain text, I cannot have links between them, but in
practice I find it much easier to use <code>grep</code> to quickly find all the notes with
a specific word in it.</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Goxel 0.3 Released</title>
        <link href="https://blog.noctua-software.com/goxel-v0_3.html" />
        <id>https://blog.noctua-software.com/goxel-v0_3.html</id>
        <updated>2016-07-09</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>I just released the version 0.3.0 of my open source voxel editor <a href="https://guillaumechereau.github.io/goxel">Goxel</a>.</p>
<p>This release brings:</p>
<ul>
<li>Import/Export support for the popular editor <a href="https://ephtracy.github.io">Magica Voxel</a>.</li>
<li>Render shadows.</li>
<li>Better shapes support, specially when we use marching cube rendering.</li>
<li>Multiple palettes.</li>
<li>Better UI, with new tools icons.</li>
</ul>
<p>Here is a video of the laser tool:</p>
<video src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-laser.webm" controls="">
    Your browser does not support the video tag.
</video>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>OpenCV Camera to OpenGL Projection</title>
        <link href="https://blog.noctua-software.com/opencv-opengl-projection-matrix.html" />
        <id>https://blog.noctua-software.com/opencv-opengl-projection-matrix.html</id>
        <updated>2016-03-23</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p><img alt="OpenCV" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/opencv-test.png" /></p>
<p>I am sure some people are hitting their heads trying to solve this simple
problem when doing augmented reality with OpenCV and OpenGL:</p>
<p>How to get the proper OpenGL projection matrix from the OpenCV camera
calibration values?</p>
<p>For the simple common case where the OpenCV camera matrix has the form:</p>
<pre><code>|fx  0 cx|
|0  fy cy|
|0  0   1|
</code></pre>
<p>The corresponding OpenGL projection matrix can be computed like this:</p>
<pre><code>m[0][0] = 2.0 * fx / width;
m[0][1] = 0.0;
m[0][2] = 0.0;
m[0][3] = 0.0;

m[1][0] = 0.0;
m[1][1] = -2.0 * fy / height;
m[1][2] = 0.0;
m[1][3] = 0.0;

m[2][0] = 1.0 - 2.0 * cx / width;
m[2][1] = 2.0 * cy / height - 1.0;
m[2][2] = (zfar + znear) / (znear - zfar);
m[2][3] = -1.0;

m[3][0] = 0.0;
m[3][1] = 0.0;
m[3][2] = 2.0 * zfar * znear / (znear - zfar);
m[3][3] = 0.0;
</code></pre>
<p>Where <code>height</code> and <code>width</code> are the size of the captured image ; <code>znear</code> and
<code>zfar</code> are the clipping value for the projection.</p>
<p>It took me a lot of time to get it right, since we have to be careful of the
difference in referential between OpenGL and OpenCV.</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Goxel 3D procedural rendering</title>
        <link href="https://blog.noctua-software.com/goxel-procedural.html" />
        <id>https://blog.noctua-software.com/goxel-procedural.html</id>
        <updated>2016-03-10</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>In this post I introduce a new experimental feature I am adding to <a href="https://github.com/guillaumechereau/goxel">goxel</a>:
procedural voxel generation.</p>
<p>For this I created a mini language specifically designed to describe drawing
operations.  Think of it as the logo language, but in 3D.</p>
<p>Note: this is still experimental at this point, so anything in the language
could change in the future.  I will try to keep this page up to date as a
reference documentation.</p>
<p>To try it out, you need to get the last version of goxel from the
<a href="https://github.com/guillaumechereau/goxel">github</a> page, then use the 'procedural' tool, enter the code, and press
'run'.  You can use 'auto run' to automatically re-run the code anytime it
changes.</p>
<p>The language is largely inspired by <a href="http://www.contextfreeart.org/">ContextFree</a> language.  I tried to keep it
as close as possible.</p>
<h1>Basic syntax</h1>
<p>The smallest program (that does something) you can create is:</p>
<pre><code>shape main {
    cube[]
}
</code></pre>
<p><img alt="example" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-examples/tut-simple.png" /></p>
<p><code>shape main</code> is the entry point of the program and <code>cube[]</code> is the command used
to render a cube.  This program renders a 1x1x1 voxel on the screen.  To make
it bigger we can put adjustments inside the square brackets.</p>
<pre><code>shape main {
    cube[s 9]
}
</code></pre>
<p><img alt="example" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-examples/tut-simple-scaled.png" /></p>
<p>Here <code>s 9</code> tells the program to make the cube 9 times bigger (s is for
'scale').  If we wanted to scale the cube differently in x y and z we could
have used the s adjustment with three arguments: <code>cube[s 3 4 5]</code>.  Since it is
common to only scale along a single axis, we can also use <code>sx</code>, <code>sy</code> and <code>sz</code>.</p>
<p>Let's render a second cube next to the first one:</p>
<pre><code>shape main {
    cube[s 9]
    cube[s 9 x 2]
}
</code></pre>
<p><img alt="example" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-examples/tut-simple-translated.png" /></p>
<p>For the second cube we use two adjustments applied one after the other: <code>s 9</code>
to scale the cube, followed by <code>x 2</code> to translate it by 2 times its size.</p>
<p>The adjustments are applied in order, and not commutative, so <code>[s 9 x 2]</code> is
different from <code>[x 2 s 9]</code>, in the first case the cube is translated by 9
voxels, in the second case only by 1.</p>
<p>Note the syntax of the adjustments: any values following an adjustment
operation until the next operation is an argument to that operation.</p>
<h1>Creating shapes</h1>
<p>The basic shapes we can call are: cube, sphere, and cylinders.  We can also
define new shapes:</p>
<pre><code>// Render a cube with a sphere on top.
shape my_shape {
    cube[]
    sphere[z 0.5]
}

shape main {
    my_shape[s 20]
}
</code></pre>
<p><img alt="example" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-examples/tut-simple-shape.png" /></p>
<p>We render the new shape just like we did for a cube.  The adjustments we use in
the call are applied before the shape is rendered, so in that case we render
the new shape scaled 20 times.  The way to understand it is that the execution
of the code is linked to a context.  The context define the position, size and
color we are using.  When we call a new shape, we duplicate the value of the
current context and apply the adjustments to it before rendering the shape.  It
does not affect the rest of the rendering of the caller.</p>
<h1>Recursion shapes</h1>
<p>A shape can call itself for recurive rendering:</p>
<pre><code>shape my_shape {
    cylinder[]
    my_shape [s 0.8 z 1]
}

shape main {
    my_shape[s 20]
}
</code></pre>
<p><img alt="example" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-examples/tut-simple-recursion.png" /></p>
<p>The recursion is automatically stopped when the shape becomes too small.</p>
<h1>Loops</h1>
<p>We can use the <code>loop</code> directive to create loops:</p>
<pre><code>shape main {
    loop 8 [rz 45] {
        sphere[x 20 s 10]
    }
}
</code></pre>
<p><img alt="example" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-examples/tut-simple-loop.png" /></p>
<p>This will execute the block 8 times, each time with a context rotated 45°
compared to the previous one.  The adjustment only affect the block of the
loop.</p>
<h1>Expressions</h1>
<p>We can use mathematic expression to express the adjustement arguments.  The
basic operations are <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, and <code>+-</code></p>
<p>The <code>x +- y</code>  operation returns a random value between x-y and x+y.  This can
be used to add some randomness to your shapes:</p>
<pre><code>// Same as previous example, but the sphere
// have a random size between 4 and 16.
shape main {
    loop 8 [rz 45] {
        sphere[x 20 s 10+-6]
    }
}
</code></pre>
<p><img alt="example" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-examples/tut-plusmin.png" /></p>
<h1>Shape Rules</h1>
<p>It is possible to give several implementations of the same shape, and let the
program pick one randomly.  To do this we use use shape rules:</p>
<pre><code>shape my_shape
rule 4 {
    sphere[]
}
rule 1 {
    cube[]
}

shape main {
    loop 8 [rz 45] {
        my_shape[x 20 s 8]
    }
}
</code></pre>
<p><img alt="example" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-examples/tut-rule.png" /></p>
<p>In this example, <code>my_shape</code> is most of the time a shere, but sometime a cube.
The weight give the relative probabilities to use a given rule, here 4/5
chances to use the first rule, and 1/5 chance to use the second.</p>
<p>This can be used for branching effects:</p>
<pre><code>shape main {
    [antialiased 1 seed 4]
    test[s 16 life 40]
}

shape test
rule 5 {
    cube[]
    test[s 0.95 z 1]
}
rule 1 {
    test[]
    test[rz 0+-180 rx 90]
}
</code></pre>
<p><img alt="example" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-examples/tut-tree.png" /></p>
<p>We also see something new here: the <code>[antialiased 1 seed 4]</code> line apply some
adjustment to the current context: <code>antialiased 1</code> improves the rendering with
marching cube algorithm, and <code>seed 4</code> set the initial seed for the internal
random function.  In this example we could just have put those adjustments
in the call to test.</p>
<p>The <code>life 40</code> adjustment is here to stop the recursion after 40 iterations.</p>
<h1>Colors</h1>
<p>So far all the example used white color.  Let see how to change that.</p>
<p>The color can be changed using three adjustment: light, hue and saturation,
following the HSL color model.  The hue varies from 0 to 360, saturation and
light from 0 to 1.  The initial white color corresponds to a HSL value of (0,
0, 1).</p>
<p>There are 3 adjustments operations to change the color: <code>hue</code>, <code>sat</code> and
<code>light</code>.  Each can take one or two arguments.</p>
<ul>
<li>
<p><code>hue x</code>: adds the value <code>x</code> to the current hue.  If the value gets over 360
  or below 0 a modulo is applied to put it back in the range (0, 360).</p>
</li>
<li>
<p><code>light x</code>: <code>x</code> in the range [-1 +1].  If <code>x</code> &lt; 0, change the light value <code>x</code>%
  toward 0.  If <code>x</code> &gt; 0, change the light value <code>x</code>% toward 1.</p>
</li>
<li>
<p><code>sat x</code>: <code>x</code> in the range [-1 +1].  If <code>x</code> &lt; 0, change the saturation value
  <code>x</code>% toward 0.  If <code>x</code> &gt; 0, change the saturation value <code>x</code>% toward 1.</p>
</li>
<li>
<p><code>hue x h</code> change the current hue <code>x</code>% toward <code>h</code>.</p>
</li>
<li>
<p><code>light x t</code> change the light value <code>x</code>% toward <code>t</code>.</p>
</li>
<li>
<p><code>sat x t</code> change the saturation value <code>x</code>% toward <code>t</code>.</p>
</li>
</ul>
<p>Here is a small program that render 12 spheres with different hue values:</p>
<pre><code>shape main {
    [antialiased 1 sat 1 light -0.5]
    loop 12 [hue 30 rz 30] {
        sphere [s 4 x 4]
    }
}
</code></pre>
<p><img alt="example" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-examples/tut-colors.png" /></p>
<p>The first line sets the rendering to anti-aliased and the initial saturation
and light to 1 and 0.5.</p>
<p>Here is a second example where we also render all the saturation values from
0 to 1.  The variable <code>$i</code> will be explained in the next section.</p>
<pre><code>shape main {
    [antialiased 1 sat 1 light -0.5]
    loop $i = 12 [z 8] {
        [sat 1 $i / 12]
        loop 12 [hue 30 rz 30] {
            sphere [x 14 s 6]
        }
    }
}
</code></pre>
<p><img alt="example" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-examples/tut-colors-2.png" /></p>
<h1>Variables</h1>
<p>We can add variables to the code.  All the variable names start with a '$',
so that we don't mistake them for adjustment operations.</p>
<p>The first way to use variable is with the loop expression, for example:</p>
<pre><code>shape main {
    [hue 180 sat 0.5]
    loop $x = 10 [] {
        cube [x $x light $x / -10]
    }
}
</code></pre>
<p><img alt="example" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-examples/tut-loop-var.png" /></p>
<p>We can also use variables as argument of shapes.  In that case the values
need to be passed when we call the shape.</p>
<pre><code>// Render n cubes
shape my_shape($n) {
    loop $n [z 2] {
        cube[]
    }
}

shape main {
    [light -0.5 sat 0.5]
    my_shape(10) []
    my_shape(8) [x 4 hue 90]
}
</code></pre>
<p><img alt="example" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/goxel-examples/tut-var.png" /></p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Lambda expressions in C</title>
        <link href="https://blog.noctua-software.com/c-lambda.html" />
        <id>https://blog.noctua-software.com/c-lambda.html</id>
        <updated>2016-01-13</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>C does not have built-in support for lambda expressions, but if you are using
gcc, it is actually quite easy to implement them using a simple macro.</p>
<p>This trick relies on two C extensions: nested functions and statement
expressions.  Both are available in gcc (but not in clang!)</p>
<p>Here is a small program that could benefit from lambda expressions, written
in conventional C:</p>
<pre><code>// A function taking a function pointer as argument.
// Call the function `f` `nb` times.
void func(int nb, void (*f)(int)) {
    int i;
    for (i = 0; i &lt; nb; i++) f(i);
}

void callback(int v) {
    printf("%d\n", v);
}

int main()
{
    func(4, callback);
}
</code></pre>
<p>The first change we can do is use nested functions to put the callback
definition inside the main function:</p>
<pre><code>int main()
{
    void callback(int v) { printf("%d\n", v); }
    func(4, callback);
}
</code></pre>
<p>The second change comes from using a statement expression to create the
callback function directly in the argument list:</p>
<pre><code>int main()
{
    func(4, ({
        void callback(int v) { printf("%d\n", v); }
        callback;
    });
}
</code></pre>
<p>Now we are almost there.  Since the statement expression scope does not leak to
the rest of the function, we can use any name for the callback, so we are going
to use <code>_</code>.  We can also put the code into a macro so that we do not have to
repeat the name of the callback (as required by the statement expression).  And
the final result is:</p>
<pre><code>#define LAMBDA(c_) ({ c_ _;})

int main()
{
    func(4, LAMBDA(void _(int v) {
        printf("%d\n", v);
    }));
}
</code></pre>
<p>There is one limitation to this: the lambda is only valid during the scope of
the calling function, so we have to be careful not to use it for asynchronous
callbacks.</p>
<p>The remaining question is: should we use such a trick in production code?</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>How I made my parallel self win the lottery</title>
        <link href="https://blog.noctua-software.com/win-lottery.html" />
        <id>https://blog.noctua-software.com/win-lottery.html</id>
        <updated>2015-12-20</updated>
        <summary>0.000005 percent of the time, it works 100 percent of the time</summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>While I was in France recently I decided to make one of my parallel self
a millionaire.  It's very easy and you too can do it if you want.</p>
<p>How to do it?  First you have to check how the national lottery of the country
where you are works.  In France, a lotto ticket costs 2 euro, you have to
pick five different numbers from 1 to 49 (included), and one 'lucky' number
from 1 to 10 (included).  To win the big price, all your numbers must match the
winning numbers.  If you do, you can earn up to a few millions.</p>
<p>We are going to play this -otherwise pretty stupid- game in such a way that we
are sure that we are going to win in at least one parallel universe.</p>
<p>To pick your numbers, go on one of the numerous websites that offer live
stream of quantum random numbers, for example the ANU Quantum Random Number
Server (http://qrng.anu.edu.au).  From there pick a long enough random number.
In my case I got this one (in hexadecimal):</p>
<pre><code>1a7bc82fe1393178549ec0959b4b862d52d7f5ea02f16f5c848f15a1c68cedd789e2f
</code></pre>
<p>You should start to see where this is going.  If the multi worlds
interpretation of quantum physics is correct, and assuming the number really
was computed using a pure quantum process, then there exists one parallel
universe for each possible values of this random number.</p>
<p>Now that the universe has been separated into multiple equi-probable versions,
only differentiated by this number, we can start to pick our lottery numbers.
I used a simple python script that would generate the 5 + 1 numbers from the
initial random number I got.  The algorithm is not very important, the only
difficulty is to make sure that we don't get two times the same number, and to
ensure that each possible set of values has an equal chance to appears.</p>
<p>In my case the numbers I played are:</p>
<pre><code>[28, 31, 20, 12, 1] 8
</code></pre>
<p>And I know for sure that all the other possible numbers have been picked in
different universes.  Think about it: there are universes where you are right
now reading the same web page, with the only difference being that those
numbers (and the initial random number) are different.</p>
<p>Now, assuming that there is no cross correlation between the ANU Quantum Random
Number Server and the French lottery, I can be sure that in at least one
universe I got the numbers:</p>
<pre><code>[09, 20, 26, 27, 47] 07
</code></pre>
<p>Which happens to be the winning numbers of the day I bought the ticket.</p>
<p>I can know rest assured that somewhere in the multiverse, one of my parallel
self did actually win the lottery.</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Goxel, an open source 3D voxel editor</title>
        <link href="https://blog.noctua-software.com/goxel.html" />
        <id>https://blog.noctua-software.com/goxel.html</id>
        <updated>2015-10-05</updated>
        <summary>An open source 3D voxel editor</summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p><a href="https://github.com/guillaumechereau/goxel">Goxel</a> is a new project I have been working on since last month.  It's an open
source 3D editor to create voxel images (in the style of minecraft, or crossy
road).</p>
<p>It works on Linux and Windows.</p>
<h1>Features:</h1>
<ul>
<li>24 bits RGB colors.</li>
<li>Unlimited scene size.</li>
<li>Unlimited undo buffer.</li>
<li>Layers.</li>
<li>Smooth rendering mode.</li>
<li>Export to obj and pyl.</li>
</ul>
<p><img alt="screenshot-0" src="https://github.com/guillaumechereau/goxel/raw/master/screenshots/screenshot-0.png?raw=true" />
<img alt="screenshot-1" src="https://github.com/guillaumechereau/goxel/raw/master/screenshots/screenshot-1.png?raw=true" /></p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Voxel Snake 3D</title>
        <link href="https://blog.noctua-software.com/voxel-snake.html" />
        <id>https://blog.noctua-software.com/voxel-snake.html</id>
        <updated>2015-07-28</updated>
        <summary>A small video game project</summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>I just released a small game on <a href="https://play.google.com/store/apps/details?id=com.noctuasoftware.voxelsnake">google play</a>: <a href="http://noctua-software.com/voxel-snake">Voxel Snake 3D</a>.</p>
<p><img alt="Voxel Snake Screenshot" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/voxel-snake-screenshot.png" /></p>
<p>This is basically a snake game played on a 3D cube, using voxel rendering.</p>
<p>Let me know what you think!</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Blowfish Rescue 1.0 Released</title>
        <link href="https://blog.noctua-software.com/blowfish-rescue.html" />
        <id>https://blog.noctua-software.com/blowfish-rescue.html</id>
        <updated>2015-07-06</updated>
        <summary>My new video game</summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>Just a small post to announce the release of my new video game: <a href="http://noctua-software.com/blowfish-rescue">Blowfish
Rescue</a>.</p>
<p>This is a 2d action/puzzle game using a fluid simulation solver.  Most of the
graphics are procedurally generated.</p>
<p>The game is available for free on <a href="https://play.google.com/store/apps/details?id=com.noctuasoftware.blowfish">google play</a> and <a href="https://itunes.apple.com/app/blowfishrescue/id980155132">iTune</a>.</p>
<p>Here is a promo video I did:
<iframe width="560" height="315"
        src="https://www.youtube.com/embed/10XV1z5V8fQ"
    frameborder="0" allowfullscreen=""></iframe></p>
<p>In the following week I will open source the procedural generation code, as I
think this is an interesting part of the code, and can probably be reused for
other games.  Stay tuned.</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Procedural graphics generation in C</title>
        <link href="https://blog.noctua-software.com/noc_turtle.html" />
        <id>https://blog.noctua-software.com/noc_turtle.html</id>
        <updated>2015-06-12</updated>
        <summary>Introducing noc_turtle library</summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>I introduce <a href="https://github.com/guillaumechereau/noc">noc_turtle</a>, a small MIT licence C library to create procedural
graphics in plain C.  I release it in the hope that some people will use it in
their own project, and also to promote my video game <a href="http://noctua-software.com/blowfish-rescue">Blowfish Rescue</a> for
which I wrote it.</p>
<p>The library allows to write procedural rules directly in C, so it is very
simple to embed it in a project and to mix it with custom code.</p>
<p>The name comes from the turtle concept of the <a href="http://en.wikipedia.org/wiki/Logo_%28programming_language%29">LOGO</a> language, since it uses
a similar abstraction.</p>
<h1>How it works</h1>
<p>Let start with a simple example.  Here is a valid C function, using some
macros defined in noc_turtle.h:</p>
<pre><code>static void demo_sun(noctt_turtle_t *turtle)
{
    START
    TR(S, 0.2, SN);
    TR(HUE, 40, SAT, 1, LIGHT, 0.7);
    CIRCLE();
    LOOP(16, R, 360 / 16.) {
        RSQUARE(0, X, 1, S, 0.8, 0.1, LIGHT, 0.2);
        CIRCLE(X, 1.7, S, 0.4);
    }
    END
}
</code></pre>
<p>When we feed the function into noc_turtle, we get the following image:</p>
<p><img alt="noc_turtle demo sun" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/noc_turtle/sun.png" /></p>
<p>If you know the <a href="http://www.contextfreeart.org">Context Free project</a>, the code should feel familiar.  The
idea is that the function takes a turtle as argument and tell it what to do.
CIRCLE, and RSQUARE are primitives to render respectively a circle and a
rounded rectangle at the curent position of the turtle.  The arguments to
the primitives are a list of adjustments to do to the turtle before rendering
the primitive.  For example the line</p>
<pre><code>RSQUARE(0, X, 1, S, 0.8, 0.1, LIGHT, 0.2);
</code></pre>
<p>tells the turtle to do a translation along x of 1 unit, then scale itself of
0.8 in the x axis and 0.1 in the y axis, then increase its color lightness of
20%, and finally render a rounded square.  The first argument (0) defines the
roundness of the square.  After the operation the turtle returns to its initial
state, that is: the adjustments only affect the current primitive.</p>
<p>The adjustments used in LOOP are applied to the turtle at each iteration of
the loop, so in that case we call the block of the loop 16 times, with each
time a rotation of 22.5 degree.</p>
<p>The TR function (for TRansform) doesn't render anything but allows to change
the current turtle attributes, globally to the function.</p>
<h1>Animations</h1>
<p>The first interesting thing is that we can easily create animations by adding
pause in the rendering code.  If we add a YIELD call in the loop of the
previous function, like that:</p>
<pre><code>static void demo_sun(noctt_turtle_t *turtle)
{
    START
    TR(S, 0.2, SN);
    TR(HUE, 40, SAT, 1, LIGHT, 0.7);
    CIRCLE();
    LOOP(16, R, 360 / 16.) {
        YIELD(5);   // Pause for 5 iterations.
        RSQUARE(0, X, 1, S, 0.8, 0.1, LIGHT, 0.2);
        CIRCLE(X, 1.7, S, 0.4);
    }
    END
}
</code></pre>
<p>Now we get this animation:</p>
<p><img alt="noc_turtle demo sun animated" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/noc_turtle/sun.gif" /></p>
<p>Note that the YIELD here is not pausing the application: under the hood the
library is using coroutines to keep track of where in the programme a turtle
is, so that the function can return and be resumed later from where it was
paused.  If this idea seems weird, you can check <a href="http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html">this explanation</a>
by Simon Tatham.</p>
<h1>Multiple turtles</h1>
<p>The previous example was rather simple, with a single turtle doing linear
rendering.  We can however use the library for more complex animations.  Just
like a unix process, a turtle has the capacity to fork itself so that multiple
rendering functions can be executed in parallel.  Here is an other example, a
bit more complicated:</p>
<pre><code>static void spiral_node(noctt_turtle_t *turtle)
{
    START
    SQUARE();
    YIELD();
    if (BRAND(0.01)) {
        TR(FLIP, 0);
        SPAWN(spiral_node, R, -90);
    }
    SPAWN(spiral_node,
          X, 0.4, R, 3, X, 0.4,
          S, 0.99,
          LIGHT, -0.002);
    END
}

static void demo_spiral(noctt_turtle_t *turtle)
{
    START
    TR(HSL, 1, 100, 0.5, 0.5, S, 0.02, SN);
    SPAWN(spiral_node);
    SPAWN(spiral_node, FLIP, 90);
    END
}
</code></pre>
<p>Here is the resulting animation (I removed 7/8 of the frames to make the gif
smaller):</p>
<p><img alt="noc_turtle demo spiral" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/noc_turtle/spiral.gif" /></p>
<p>The new concept is the SPAWN function, that clones the current turtle and
branches the clone to a new function.  In this case we use it to create a
recursive rule rendering a typical L-tree structure.</p>
<h1>Conclusion</h1>
<p>I used the library extensively in my last video game (<a href="http://noctua-software.com/blowfish-rescue">Blowfish Rescue</a>, if you
haven't checked it out yet) to create all the backgrounds and most of the game
elements.  A few examples:</p>
<p>Some elements of the game:
<img alt="blowfish objects" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/noc_turtle/objs.png" /></p>
<p>An animated background:
<img alt="blowfish background" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/noc_turtle/city.gif" /></p>
<p>For more information about how to use the library, check the file
<a href="https://github.com/guillaumechereau/noc/blob/master/noc_turtle.h">noc_turtle.h</a> in the github repo.  One important aspect is that the library
itself does not do any rendering: instead you pass it a callback function that
will be called each time a turtle need to render something.  That way the code
is totally independent of any rendering library like OpenGL or DirectX.</p>
<p>Do not hesitate to contact me if you want to use noc_turtle in one of your
project and need some tips or info.</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Comparison of Doom 1, Quake, and Doom 3 entity references system</title>
        <link href="https://blog.noctua-software.com/entity-references.html" />
        <id>https://blog.noctua-software.com/entity-references.html</id>
        <updated>2015-04-29</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>The architecture of a video game code is usually based around a list of
entities that represent all the game elements: players, enemies, etc.  The
engine iterates over this list and call the appropriate methods to update or
render the entities.  Since game elements get constantly created and destroyed,
the list is not static but need to be updated at each iteration.</p>
<p>The problem is that some entities keep references to others.  For example, an
enemy might have a reference to the player it is currently attacking.  If an
entity gets removed from the list, all the references to it need to be
invalidated, failing that it will introduce bugs in the code.  So in a sense
those references are weak (in the sense that they won't prevent the entity from
being destroyed, but must be able to find out that it has happened).</p>
<p>Since I am talking about video games written in C or C++ here, the language has
no support for weak references, and so we have to find some other ways.</p>
<p>Let's imagine we have three entities in the game (A, B and C), and C has a
reference to A (maybe C is an enemy and A its target).</p>
<p>Imagine we use a basic implementation, where entities are stored in a linked
list, and references are done with normal pointers.</p>
<p>When an entity update function is called, the object can decide to remove
itself from the list.  We risk to run into this situation:</p>
<pre><code>Step 1: entity A update function is called,
        A decides that it should be
        removed from the game.

  +---------------+
  v               |
+---+   +---+   +---+
| A |--&gt;| B |--&gt;| C |
+---+   +---+   +---+
  ^


Step 2: The engine removes A from the list and
        release the memory, the update continue
        with object B, and then object C.
        At this point C might try to access A,
        resulting at best in a segmentation fault,
        at worst into strange random behaviors
        (those bugs are very hard to track!)

  +---------------+
  v               |
invalid +---+   +---+
pointer | B |--&gt;| C |
        +---+   +---+
                  ^
</code></pre>
<h1>Doom 1</h1>
<p>In the original <a href="https://github.com/id-Software/DOOM">DOOM</a> game, the solution used is very simple:  when an entity
dies, it is not immediately removed from the list, but instead it goes into the
'dead' state (this is implemented by having the entity state function points to
-1).  Only at the next iteration will the entity actually be removed from the
list.</p>
<p>This gives all the other entities the chance to update their references to NULL
if they point to an entity about to get deleted.</p>
<pre><code>Step 1:  entity A decides that is should die,
         so it flags itself as dead.

  +---------------+
  v               |
+---+   +---+   +---+
| A*|--&gt;| B |--&gt;| C |
+---+   +---+   +---+
  ^


Step 2:  when C update function is called,
         C find out that A is dead, and
         so remove its reference to it
         (make it point to the NULL address).

+---+   +---+   +---+
| A*|--&gt;| B |--&gt;| C |
+---+   +---+   +---+
                  ^

Step 3:  A is updated again, since it was
         flagged as dead, it can be safely
         removed from the list.

        +---+   +---+
        | B |--&gt;| C |
        +---+   +---+
   ^
</code></pre>
<p>This implementation is simple, but you have to be careful: every entity holding
references to a dead entity need to checked at each iteration that the
referenced object is still valid.  Moreover, an entity dead state can only be
set from the entity update function.</p>
<p>Imagine the following state:</p>
<pre><code>          +-------+
          v       |
+---+   +---+   +---+
| A |--&gt;| B |--&gt;| C |
+---+   +---+   +---+
  ^
</code></pre>
<p>If the update method of A sets the B state to dead, then B will immediately
removes itself from the list without leaving a chance to C to update its
reference.</p>
<p>As a side note: this is more or less the technique I used in my video game
<a href="http://noctua-software.com/voxel-invaders">voxel invaders</a>.  Though it works pretty well, a few times I spent hours
trying to figure out strange bugs that were due to the fact that an entity
forgot to check and update its references.</p>
<h1>Quake</h1>
<p>In <a href="https://github.com/id-Software/Quake">Quake 1</a>, the code is a bit different.  The entity list is preallocated
with a preset value of max entities.  Note: apparently someone at id software
(maybe John Carmack?) thought the max number of 768 might be too low.  In the
code we can see:</p>
<pre><code>#define MAX_EDICTS 768  // FIXME: ouch! ouch! ouch!
</code></pre>
<p>When an entity is deleted, it is simply flagged as such, so that the engine
will skip it.  If we need to create a new entity, we loop over the array and
find a free spot.  Now, the trick is that we also keep track of the time when a
slot was freed, and only allow to reuse a slot two seconds after it has been
freed.  What this mean is that other entities in the game gets two seconds to
update their references to dead entities.</p>
<pre><code>Step 1: at t = 3s, the entity A update method
        decides that it should be removed
        from the game.  It clears itself
        in the array, and set its timer to 3.

  +-------+
  V       |
+---+---+---+--
|t=3| B | C |
+---+---+---+--
  ^


Step 2: eventually (but no more than 2 seconds
        later), the entity C find out that A
        slot has been cleared, and so nullify
        its reference to it.

+---+---+---+--
|t=3| B | C |
+---+---+---+--
          ^

Step 3: at t &gt; 5s, the empty slot is reused
        for a new entity.

+---+---+---+--
| D | B | C |
+---+---+---+--
</code></pre>
<p>This solution is also quite simple to implement and give more flexibility to
the game: we do not have to update the references immediately, also it doesn't
matter if an entity get deleted from an other entity update method.  There is
till the danger that an entity might not update its references during the
window time.  An other problem is that if we create and destroy a lot of
entities quickly, then most of the slots get unused (in fact in quake, there is
an explicit relaxation of the window period during level initialization to
prevent this problem).</p>
<h1>Doom 3</h1>
<p>In <a href="https://github.com/id-Software/DOOM-3">DOOM III</a>, a more robust solution is used.  The idea is similar to quake,
except that every reference to an entity also stores an id that uniquely
identify the entity (an incremented counter).  If the slot pointed to has been
reused for an other entity, then we can figure it out by comparing the entity
id and the reference id.</p>
<p>In fact, in Doom 3 code, things are a bit more complicated, since the unique id
is not stored in the entity itself, but in a separated array (I suppose so that
we do not waste ids for entities that cannot be referenced anyway).</p>
<pre><code>State 1: all entities have a unique id
         assigned to them.  C keeps a pointer
         to A (in fact it is just an index in
         the entities array), but also the id
         of A (1).

  +-------+ 1
  V       |
+---+---+---+--
|A 1|B 4|C 2|
+---+---+---+--


State 2: A got removed from the list.
         C can figure out that its reference
         is invalid since the slot is empty.

  +-------+ 1
  V       |
+---+---+---+--
|   |B 4|C 2|
+---+---+---+--


State 3: the free slot has been reused by a
         new entity with id 5.  C does not
         need to update its reference, since
         it can still figure out that it is
         invalid by comparing the entity id
         (5) with the one he expects (1).

  +-------+ 1 (1 != 5)
  V       |
+---+---+---+--
|D 5|B 4|C 2|
+---+---+---+--
</code></pre>
<p>This solution is more robust than the two previous ones.  We can keep
references to entities around for as long as we want.  As long as the entities
ids are really unique, we can always tell that a reference is invalid.  From
the code we can tell that Doom 3 support spawning 1,048,576 entities before an
error occurs.</p>
<p>The drawback is that it makes the code a bit more complicated, since we need to
keep two pieces of information to represent a reference to an entity.</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>10 C99 tricks</title>
        <link href="https://blog.noctua-software.com/c-tricks.html" />
        <id>https://blog.noctua-software.com/c-tricks.html</id>
        <updated>2015-02-13</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>Here are ten interesting patterns and tricks I sometime use in my C code, some
are well known, some not so much.  I am sure all of them work with clang and
gcc (after fixing the evetual typos).  I didn't try with MSVC, but except for
no 1 and 5, I think it should also work.</p>
<p>[<strong>edit</strong>]: Here is a link to the <a href="https://news.ycombinator.com/item?id=9043594">hacker news thread</a>.</p>
<p>[<strong>edit</strong>]: Maybe the title is a bit misleading, this is not strictly about
C99.  Some items in the list are valid C89, and some others would not compile
with -std=c99 -pedantic, and there are also gnu extensions.  But pragmatically,
all of those will compile with recent gcc and clang compiler.</p>
<h2>0. Ternary operator without middle operand (gnu extension)</h2>
<pre><code>// Instead of
x = x ? x : 10;

// We can use the shorter form:
x = x ?: 10;

// Advantage: if x is an expression it
// will be evaluated only once.
</code></pre>
<h2>1. Unamed struct for smart vector type.</h2>
<pre><code>typedef union {
    struct { float x, y, z; };
    struct { vec2_t xy; };
    struct { float x_; vec2_t yz; };
    float v[3];
} vec3_t;
#define VEC3(x, y, z) { {x, y, z} }

...

vec3_t vec = VEC3(1, 2, 3);
// We can access the attributes in different ways.
float  x    = vec.x;
vec2_t xy   = vec.xy;
float  z    = vec.v[2];
</code></pre>
<h2>2. <code>IS_DEFINED</code> macro</h2>
<pre><code>// As used in the linux kernel.
// A macro that expands to 1 if a preprocessor value
// was defined to 1, and 0 if it was not defined or
// defined to an other value.

#define IS_DEFINED(macro) IS_DEFINED_(macro)
#define MACROTEST_1 ,
#define IS_DEFINED_(value) IS_DEFINED__(MACROTEST_##value)
#define IS_DEFINED__(comma) IS_DEFINED___(comma 1, 0)
#define IS_DEFINED___(_, v, ...) v

// Can be used in preprocessor macros:
#if IS_DEFINED(SOMETHING)
    ...
#endif

// Or even directly in the code.
// Same effect but looks better.
if (IS_DEFINED(SOMETHING)) {
    ...
}
</code></pre>
<h2>3. Convenience macro for OpenGL code</h2>
<pre><code>// Not really special, but so useful I thought
// I'll put it here.  Can also be used with other
// libraries (OpenAL, OpenSLES, ...)
#ifdef DEBUG
#  define GL(line) do {                      \
       line;                                 \
       assert(glGetError() == GL_NO_ERROR);  \
   } while(0)
#else
#  define GL(line) line
#endif

// Put GL around all your opengl calls:
GL(glClear(GL_COLORS_MASK));
GL(pos_loc = glGetAttribLocation(prog, "pos"));
</code></pre>
<h2>4. Array size macro</h2>
<pre><code>// Is there any C project that does not use it?
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

// Can be used like this:
int a[] = {0, 4, 5, 6};
int n = ARRAY_SIZE(a); // n = 4

// Warning: does not work with array arguments to functions:
int func(int a[]) {
    int nb = ARRAY_SIZE(a); // Would not work!
}
</code></pre>
<h2>5. Safe min macro (uses a gnu extension)</h2>
<pre><code>#define min(a, b) ({ \
      __typeof__ (a) _a = (a); \
      __typeof__ (b) _b = (b); \
      _a &lt; _b ? _a : _b; \
})
</code></pre>
<h2>6. Passing pointer to unnamed variables to function.</h2>
<pre><code>// A func that expects a pointer to three int values.
void func(const int *arg);

// Instead of using a local variable.
int tmp[] = {10, 20, 30};
func(tmp);

// We can write.
func( (const int[]){10, 20, 30} )

// Can be useful with a helper macro.
#define VEC(...) ((const int[]){__VA_ARGS__})
func(VEC(10, 20, 30));

// (Also works with struct or any other type).
</code></pre>
<h2>7. Named initializer, with default values</h2>
<pre><code>// I use this one all the time when writing
// video game.  One of the reason why I
// don't like to use C++.

// Let say we have this struct
struct obj {
    const char *name;
    float pos[2];
    float color[4];
};

// We can write a macro like this one
#define OBJ(_name, ...)             \
    (struct obj) {                  \
        .name = _name,              \
        .color = {1, 1, 1, 1},      \
        __VA_ARGS__                 \
    };

// Now we can use the macro to create new objects.
// This one with color defaulted to {1, 1, 1, 1}.
struct obj o1 = OBJ("o1", .pos = {0, 10});
// This one with pos defaulted to {0, 0}.
struct obj o2 = OBJ("o2", .color = {1, 0, 0, 1});
</code></pre>
<h2>8. X macros</h2>
<pre><code>// Define this once.
#define SPRITES \
    X(PLAYER,   "atlas_0.png", {0, 0, 128, 128})    \
    X(ENEMY0,   "atlas_0.png", {128, 0, 128, 128})  \
    X(ENEMY1,   "atlas_2.png", {0, 0, 64, 64})      \
    ...

// Create an enum with all the sprites.
emum {
    #define X(n, ...) SPR_##n,
    SPRITES
    #undef X
}

// Create an array with all the sprites values.
struct {
    const char *atlas;
    int rect[4];
} sprites[] = {
    #define X(n, a, r) [SPR_##n] = {a, r},
    SPRITES
    #undef X
};

// Many other possibilities...
</code></pre>
<h2>9. State machine helper using <code>__LINE__</code></h2>
<pre><code>// This is a great trick.
// Instead of:

int iter(int state) {
    switch (state) {
    case 0:
        printf("step 0\n");
        return 1;
    case 1:
        printf("step 1\n");
        return 2;
    case 2:
        printf("step 2\n");
        return 3;
    case 3:
        return -1;
    }
}

// We can define:
#define START switch(state) { case 0:
#define END return -1; }
#define YIELD return __LINE__; case __LINE__:;

// And now the function can be written
int iter(int state) {
    START
    printf("step 0\n");
    YIELD
    printf("step 1\n");
    YIELD
    printf("step 2\n");
    END
}

// It is possible to go totally wild with
// this one.
</code></pre>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Procedural colors for video games</title>
        <link href="https://blog.noctua-software.com/procedural-colors-for-game.html" />
        <id>https://blog.noctua-software.com/procedural-colors-for-game.html</id>
        <updated>2014-10-06</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>Recently I have been spending a lot of time thinking about how to use
procedural colors in my -still unnamed- incoming video game.</p>
<p>[<strong>edit</strong>]: The game is done now, it is called <a href="http://noctua-software.com/blowfish-rescue/get">Blowfish Rescue</a>, and you
can play it for free on Android of iOS.</p>
<p>This is a problem that I am sure many other indie game developers have been
thinking about, and there are not so many resources about it online, so I
though I would share my experience so far on the subject.</p>
<h1>The problem</h1>
<p>The question I asked myself is the following one: from a random seed value, how
to generate a set of RGB colors to assign to the elements of my game, such that
the final rendering will look reasonably good.</p>
<p>The algorithm should also allow to specify some extra constraints for certain
element color (for example I want the sky to alway look more or less blue).</p>
<p>And of course, I want to get as much variation as possible in the generated
sets of colors.</p>
<h1>HSV color space</h1>
<p>The first thing I realized is that the RGB color representation used by most
graphics API is not well suited for this kind of problem.  Humans perceive
colors in term of hue, value, and saturation or chroma.  So just like in many
mathematical problems, the solution comes from choosing a better referential.</p>
<p>My first choice was to use the HSV color space, since it is very easy to
convert back and forth between RGB and HSV.  Also gimp color selector allows to
enter values in HSV, making it easier to make tests.</p>
<p>However I soon realized that HSV actually match rather poorly our actual
perception of Hue Saturation and Value.  That is, changing the hue component of
the HSV representation of a color will also change it perceived lightness and
saturation.  At first I though that it wouldn't be a problem for my algorithm,
but in fact it makes a substantial difference.  For example here is a screen
shot of the chapter menu of my game where each button color has a fixed HSV
saturation and value, and the hue is uniformly incremented:</p>
<p><img alt="Menu using HSV colors" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/menu-hsl.png" /></p>
<p>We can see that the red button looks darker than the other buttons.</p>
<p>Here is the same menu, using this time the LCH color representation with fixed
chroma and lightness:</p>
<p><img alt="Menu using LCH colors" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/menu-lch.png" /></p>
<h1>LCH color space</h1>
<p>HSV (or the related HSL) are not really suitable for actual representation of
color components in term of hue, lightness and saturation.  So instead I used
the more advanced CIE LCH color space (Light, Chroma, Hue).</p>
<p>LCH is based on the CIE Lab color space and closely match our perception of
hue, light and chroma.  The algorithm to convert from RGB to LCH and reverse
can be found on the <a href="http://www.brucelindbloom.com/">great website of Bruce Lindbloom</a>.</p>
<p>Let's compare two color wheels of HSV and LCH:</p>
<p><img alt="HSV color whee" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/color-wheel-hsv.png" /></p>
<p><img alt="LCH color whee" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/color-wheel-lch.png" /></p>
<p>The colors in the LCH wheel look more uniform.  You might think that they also
look a bit boring, but this is in fact the feeling you would expect when all
the colors differ only by their hue.</p>
<p>Now, the problem with the LCH color space is that many LCH values don't map to
a RGB color.  For example here is the LCH color wheel with different Lightness
values</p>
<p><img alt="LCH color wheel holes" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/color-wheel-lch-hole.png" /></p>
<p>Some colors are missing, specially when the lightness value gets to close to 0
or 100.</p>
<p>Since this might be a problem for our algorithm, and I don't need to get
perfect values, I decided to fill the gap using an interpolation between the
closest valid RGB value and the HSL value.</p>
<p><img alt="LCH color wheel holes fixed" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/color-wheel-lch-hole-fixed.png" /></p>
<p>This is not perfect, but will do for the purpose of my algorithm.</p>
<h1>Color harmony</h1>
<p>Many resources online about color theory mention the color harmonies rules.
The idea is that some colors will look better placed next to each other if
their hue are placed at specified relative position within the color wheel.</p>
<p>What few resources point out is that the implicit color wheel used for this is
the paint mixing color wheel, where blue is the opposite to orange, and green
is opposite to red.</p>
<p>This is not really the case in my LCH color wheel, so I deformed the hue values
of my color wheel to make it look closer to the paint mixing color wheel.</p>
<p><img alt="LCH color deformed" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/color-wheel-lch-hole-fixed-hue-deformed.png" /></p>
<p>And this is my final color wheel, so to summarized, I started from the default
LCH color wheel, fixed the wholes using HSL values, and deformed the hue to
match more closely to the color mixing wheel.  Here are a few examples for
different chroma values:</p>
<p><img alt="some color wheels" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/color-wheel-smalls.png" /></p>
<h1>Color distance</h1>
<p>My color generation algorithm will use the notion of color distance (either to
force a color to be close to a referential color, either to force two colors
not to look too close).  The formula to compute LCH color distance can also be
found on Bruce Lindbloom website.</p>
<h1>The algorithm</h1>
<p>And finally, here is the algorithm I use so far:</p>
<pre><code>1) for each element of the game (sky, walls, walls iner part,
   fish, fluid, etc.) I predefine some optional constraints:

    - lightness range (usually just a fixed value)
    - chroma range    (also usually a fixed value)
    - color that we should be close to
    - color that we should be far from

    This allow for example to force the sky to be blue,
    or the fans color to be different from the walls color.

2) pick randomly a color harmony type (complementary,
   split complementary, triad, tetradic or square).

3) pick randomly a base hue value.

4) for each game element:

    a) randomly pick a color within the color harmony.

    b) if the color does not fit the constraint return to 'a'
</code></pre>
<h1>results</h1>
<p>I might still try to change the algorithm in order to get more color variation,
or to simplify the color wheel generation, but the results I got so far are not
that bad, at least none of the level look too ugly.</p>
<p>Here are some screen shots of different levels of the game:</p>
<p><img alt="screenshot 00" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/screenshot-00.png" />
<img alt="screenshot 01" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/screenshot-01.png" />
<img alt="screenshot 02" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/screenshot-02.png" />
<img alt="screenshot 03" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/screenshot-03.png" />
<img alt="screenshot 04" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/screenshot-04.png" />
<img alt="screenshot 05" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/screenshot-05.png" />
<img alt="screenshot 06" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/screenshot-06.png" />
<img alt="screenshot 07" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/screenshot-07.png" />
<img alt="screenshot 08" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/screenshot-08.png" />
<img alt="screenshot 09" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/screenshot-09.png" />
<img alt="screenshot 10" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/screenshot-10.png" />
<img alt="screenshot 11" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/procedural-colors/screenshot-11.png" /></p>
<h1>Additional resources</h1>
<p>Here are a few link to resources that I found useful when developing my
algorithm:</p>
<ul>
<li><a href="http://www.brucelindbloom.com/">Bruce Lindbloom website</a>: All the algorithm to convert
  between many color space.</li>
<li><a href="http://devmag.org.za/2012/07/29/how-to-choose-colours-procedurally-algorithms/">Devmag</a>: Interesting blog post covering some other ideas to
  procedurally generate colors.</li>
<li><a href="http://www.tigercolor.com/color-lab/color-theory/color-harmonies.htm">tigercolor</a>: description of the different color harmonies.</li>
<li><a href="http://threekings.tk/mirror/ryb_TR.pdf">ryb pdf</a>: paper explaining how to convert from RYB to RGB.</li>
</ul>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Named parameters in C</title>
        <link href="https://blog.noctua-software.com/named-parameters-in-c.html" />
        <id>https://blog.noctua-software.com/named-parameters-in-c.html</id>
        <updated>2014-07-14</updated>
        <summary>material for the IOCCC</summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>C99 introduced the concept of <a href="https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html">designated initializers</a>, that allows to
initialize a structure using the name of the fields, like this:</p>
<pre><code>struct MyStruct {int x; float y;};
struct MyStruct a = {
    .x = 10,
    .y = 3.6,
};
</code></pre>
<p>Here is a C macro that extends this syntax to function calls.</p>
<p>I present it as a curiosity, and I really wouldn't advise anybody to actually
use it in a project (except maybe for very special cases, like for example
emulating the interface of a language that accepts named arguments).</p>
<p>The macro is using a few tricks, notably the 'foreach' as described in this
<a href="http://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros">stack overflow question</a>.</p>
<p>[<strong>Edit</strong>] As some people pointed out on the <a href="https://news.ycombinator.com/item?id=8029606">hacker news thread</a>, the macro
also uses a <a href="https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html">statement expression</a>, which is a GNU C extension, and so it would
probably not work with MSVC (I am not going to try).</p>
<pre><code>#define FE_0(a, a0, X)     a0(0, X)
#define FE_1(a, a0, X, ...) a(1, X)FE_0(a, a0, __VA_ARGS__)
#define FE_2(a, a0, X, ...) a(2, X)FE_1(a, a0, __VA_ARGS__)
#define FE_3(a, a0, X, ...) a(3, X)FE_2(a, a0, __VA_ARGS__)
#define FE_4(a, a0, X, ...) a(4, X)FE_3(a, a0, __VA_ARGS__)
#define GET_MACRO(_0, _1, _2, _3, _4, NAME,...) NAME
#define FOR_EACH(a, a0, ...) \
    GET_MACRO(__VA_ARGS__, FE_4, FE_3, FE_2, FE_1, FE_0) \
              (a, a0, __VA_ARGS__)

#define ARGS_STRUCT_ATTR(n, attr) union {attr, _##n;};

#define ARGS_STRUCT(...) \
    struct { \
        FOR_EACH(ARGS_STRUCT_ATTR, ARGS_STRUCT_ATTR, \
                 __VA_ARGS__) \
    }

#define ARGS_PASS(n, attr) _args._##n,
#define ARGS_PASS0(n, attr) _args._##n
#define PASS_STRUCT(...) \
    FOR_EACH(ARGS_PASS, ARGS_PASS0, __VA_ARGS__)

#define CALL_NAMED_ARGS(func, args, ...) ({ \
    ARGS_STRUCT args _args = {__VA_ARGS__}; \
    func(PASS_STRUCT args); \
    })
</code></pre>
<p>And here is how we can use it:</p>
<pre><code>// A normal C function.
float func(int x, float y)
{
    printf("%d %f\n", x, y);
    return x + y;
}

// func2 will call `func` using named arguments.
#define func2(...) \
    CALL_NAMED_ARGS(func, (int x, float y), __VA_ARGS__)

int main()
{
    float ret = func2(.y = 1.5, .x = 20);
}
</code></pre>
<p>For the curious, here is what the macro expends to:</p>
<pre><code>float ret = ({
    struct {
        union {int x, _1;};
        union {float y, _0;};
    } _args = {
        .y = 1.5,
        .x = 20
    };
    func(_args._1,_args._0);
});
</code></pre>
<p>The macro can only support up to 5 arguments (but, could easily be extended for
more).  One interesting thing is that if we omit a parameter it will be
automatically set to 0.  So let say you have a function like:</p>
<pre><code>void func(int x0, int x1, int x2, int x3, int x4);
</code></pre>
<p>And you want to call it by setting most of the arguments to 0.  You could do:</p>
<pre><code>#define func2(...) CALL_NAMED_ARGS(func, \
    (int x0, int x1, int x2, int x3, int x4), \
    __VA_ARGS__)

func2(.x0 = 1);             // All 0 except x0
func2(.x1 = 4, .x4 = 2);    // All 0 except x1 and x4
func2();                    // All 0.
</code></pre>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>C++ patterns using plain C</title>
        <link href="https://blog.noctua-software.com/cpp-patterns-using-plain-c.html" />
        <id>https://blog.noctua-software.com/cpp-patterns-using-plain-c.html</id>
        <updated>2014-06-22</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>So that's it, you read <a href="http://yosefk.com/c++fqa/defective.html">this article</a>, and this <a href="http://c0de517e.blogspot.tw/2014/06/where-is-my-c-replacement.html">post</a>, and
<a href="http://www.fefe.de/c++/c++-talk.pdf">this</a>, and <a href="http://article.gmane.org/gmane.comp.version-control.git/57918/">this</a>, and you spent too many time trying to
debug 8 pages long compilation error messages, and you also spent hours trying
to follow your code path involving operator overloading, copy constructors, and
virtual class methods called from constructors or whatnot.  You have had enough
of it.  Yout think it's time for you to stop using C++ altogether and return to
simple, frank, easy to debug, plain, C.</p>
<p>But there is a problem.  After all those years spent mastering C++, you have no
idea how to implement things like containers or classes in C.  features.  You
know it's possible, because after all the linux kernel is doing all those
things, but you are not really sure how they achieve it, or how to do it in
your own project.</p>
<p>In this post I am going to show a few examples of simple C++ code, and for each
propose one <strong>possible</strong> implementation using plain C.  I try to focus on the
things I think people with a C++ background would miss the most when using C.
The goal is not to imply that C is better than C++ (in most of those examples
the C++ code is actually cleaner), but merely to show that all the alleged
benefices of C++ are achievable in C with little effort.</p>
<h2>1. BASIC CLASS</h2>
<p>The simplest form of a C++ class: two subclasses of one base class, each one
having a specialized version of a virtual method.  No attributes involved.</p>
<p>Here the trick is simply to store a pointer to the function as an attribute of
each instance of the class (in C it's actually a structure).  The advantage in
the C version is that you could specialize individual instances, the drawback
is that each virtual method requires an extra attribute in the base structure.</p>
<p>C++ version:</p>
<pre><code>#include &lt;iostream&gt;

class Base
{
public:
    virtual char func() = 0;
    virtual ~Base() {}
};

class A : public Base
{
public:
    virtual char func() {return 'A';}
};

class B : public Base
{
public:
    virtual char func() {return 'B';}
};

int main()
{
    Base *o1 = new A();
    Base *o2 = new B();
    std::cout &lt;&lt; o1-&gt;func() &lt;&lt; " " &lt;&lt; o2-&gt;func() &lt;&lt; std::endl;
    delete o1;
    delete o2;
}
</code></pre>
<p>C version:</p>
<pre><code>#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;

typedef struct Base {
    char (*func)();
} Base;

static char a_func() { return 'A'; }
static char b_func() { return 'B'; }

static Base *create(char (*func)())
{
    Base *ret = malloc(sizeof(*ret));
    ret-&gt;func = func;
    return ret;
}

int main()
{
    Base *o1 = create(a_func);
    Base *o2 = create(b_func);
    printf("%c %c\n", o1-&gt;func(), o2-&gt;func());
    free(o1);
    free(o2);
}
</code></pre>
<h2>2. ADVANCED CLASS</h2>
<p>A bit more advanced, for the case when we have more virtual methods, and we
don't want to waste memory by storing all the pointers each time.  Instead,
each instance keeps a pointer to a table of pointers to the virtual methods
(the so called V-table).  Instances of the same class share the same V-table to
save space.  Just to show that is is possible, I also added an attribute to the
base class.  Note that in the C version, the methods need to explicitly have a
<code>this</code> argument.</p>
<p>C++ code:</p>
<pre><code>#include &lt;iostream&gt;

class Base
{
public:
    Base(int x) {m_x = x;}
    virtual int func1() = 0;
    virtual int func2() = 0;
    virtual ~Base() {}
protected:
    int m_x;
};

class A : public Base
{
public:
    A(int x): Base(x) {}
    virtual int func1() {return m_x * 10;}
    virtual int func2() {return m_x * 11;}
};

class B : public Base
{
public:
    B(int x): Base(x) {}
    virtual int func1() {return m_x * 20;}
    virtual int func2() {return m_x * 21;}
};

int main()
{
    Base *o1 = new A(1);
    Base *o2 = new B(1);
    std::cout &lt;&lt; o1-&gt;func1() &lt;&lt; " " &lt;&lt; o2-&gt;func1() &lt;&lt; std::endl;
    std::cout &lt;&lt; o1-&gt;func2() &lt;&lt; " " &lt;&lt; o2-&gt;func2() &lt;&lt; std::endl;
    delete o1;
    delete o2;
}
</code></pre>
<p>C code:</p>
<pre><code>#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;

// Base class

typedef struct Base {
    struct Type *type;
    int x;
} Base;

// The V-Table type
typedef struct Type {
    int (*func1)(Base *this);
    int (*func2)(Base *this);
} Type;

int base_func1(Base *this) {return this-&gt;type-&gt;func1(this);}
int base_func2(Base *this) {return this-&gt;type-&gt;func2(this);}

// class A
int a_func1(Base *this) { return this-&gt;x * 10;}
int a_func2(Base *this) { return this-&gt;x * 11;}

static Type A = {
    .func1 = a_func1,
    .func2 = a_func2,
};

// class B
int b_func1(Base *this) { return this-&gt;x * 20;}
int b_func2(Base *this) { return this-&gt;x * 21;}

static Type B = {
    .func1 = b_func1,
    .func2 = b_func2,
};

Base *create(Type *type, int x)
{
    Base *ret = malloc(sizeof(*ret));
    ret-&gt;type = type;
    ret-&gt;x = x;
    return ret;
}

// Main

int main()
{
    Base *o1 = create(&amp;A, 1);
    Base *o2 = create(&amp;B, 1);
    printf("%d %d\n", base_func1(o1), base_func1(o2));
    printf("%d %d\n", base_func2(o1), base_func2(o2));
    free(o1);
    free(o2);
}
</code></pre>
<h2>3. MORE ADVANCED CLASS</h2>
<p>Now, we also introduce attributes of the subclasses.  This means that we have
to define a different storage structure for each subclass.  We put the parent
class storage structure as a first attribute, that way we can safely cast from
a parent type to a children type.  This is I think the closest to what a C++
compiler would actually do under the hood.</p>
<p>C++ code:</p>
<pre><code>#include &lt;iostream&gt;

class Base
{
public:
    Base(int x) {m_x = x;}
    virtual int func() = 0;
    virtual ~Base() {}
protected:
    int m_x;
};

class A : public Base
{
public:
    A(int x): Base(x) {}
    virtual int func() {return m_x * 10;}
};

class B : public Base
{
public:
    B(int x): Base(x), m_y(2 * x) {}
    virtual int func() {return m_x * 20 + m_y;}
protected:
    int m_y;
};

int main()
{
    Base *o1 = new A(1);
    Base *o2 = new B(1);
    std::cout &lt;&lt; o1-&gt;func() &lt;&lt; " " &lt;&lt; o2-&gt;func() &lt;&lt; std::endl;
    delete o1;
    delete o2;
}
</code></pre>
<p>C code:</p>
<pre><code>#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;

// Base class

struct Base;

typedef struct VTable {
    int (*func)(struct Base *this);
} VTable;

typedef struct Base {
    VTable *vtable;
    int x;
} Base;


int base_func(Base *this) {return this-&gt;vtable-&gt;func(this);}

Base *base_new(VTable* vtable, int size, int x)
{
    Base *ret = calloc(1, size);
    ret-&gt;vtable = vtable;
    ret-&gt;x = x;
    return ret;
}


// class A
typedef struct A {
    Base base;
} A;

int a_func(Base *this) { return this-&gt;x * 10;}

static VTable A_VTable = {
    .func = a_func,
};

A *a_new(int x)
{
    return (A*)base_new(&amp;A_VTable, sizeof(A), x);
}

// class B

typedef struct B {
    Base base;
    int y;
} B;

int b_func(Base *base) {
    B *this = (B*)base;
    return base-&gt;x * 20 + this-&gt;y;
}

static VTable B_VTable = {
    .func = b_func,
};

B *b_new(int x)
{
    B *ret = (B*)base_new(&amp;B_VTable, sizeof(B), x);
    ret-&gt;y = x * 2;
    return ret;
}

// Main

int main()
{
    Base *o1 = (Base*)a_new(1);
    Base *o2 = (Base*)b_new(1);
    printf("%d %d\n", base_func(o1), base_func(o2));
    free(o1);
    free(o2);
}
</code></pre>
<h2>4. BASIC TEMPLATE</h2>
<p>For the very simple cases, when we just need to have different version of a
function for different types, we can just use a macro.</p>
<p>C++ code:</p>
<pre><code>template &lt;class T&gt;
T max(T x, T y)
{
    return x &gt; y ? x : y;
}
</code></pre>
<p>C code:</p>
<pre><code>#define max(a, b) ({ \
  __typeof__ (a) _a = (a); \
  __typeof__ (b) _b = (b); \
  _a &gt; _b ? _a : _b; \
  })
</code></pre>
<h2>5. ADVANCED TEMPLATE</h2>
<p>The previous example was enough for simple cases, but what about if we there is
a whole module that we want to compile for different types.  For example a
vector library might need to work with <code>float</code> or <code>double</code>.</p>
<p>Here is how I would do that:  use a macro definition to set the type.  If the
macro is not set, then set it to all the wanted values and let the file include
itself.  Also make sure that the function names are postfixed with the type,
since in C all the function names are unique.</p>
<p>C++ code:</p>
<pre><code>template &lt;class T&gt;
T add(T x, T y)
{
    return x + y;
}

template &lt;class T&gt;
T sub(T x, T y)
{
    return x - y;
}
</code></pre>
<p>C code:</p>
<pre><code>#ifndef T
#  define T int
#  include __FILE__
#  undef T
#  define T float
#  include __FILE__
#  undef T
#  define T double
#  include __FILE__
#else

#define FUNC_(name, type) name ## _ ## type
#define FUNC(name, type) FUNC_(name, type)

static inline T FUNC(add, T)(T x, T y)
{
    return x + y;
}

static inline T FUNC(sub, T)(T x, T y)
{
    return x - y;
}

#endif
</code></pre>
<p>C code usage:</p>
<pre><code>int     x_int    = sub_int(2, 1);
float   x_float  = sub_float(2.1f, 1.0f);
double  x_double = sub_double(2.1, 1.0);
</code></pre>
<h2>6. LIST</h2>
<p>One nice way to implement lists in C is to use linked lists, by adding the
pointers directly into the structure we want to put in the list.  The linux
kernel has a generic implementation of this.  Here I show how it works using
the utlist library.</p>
<p>C++ code (using std::list):</p>
<pre><code>#include &lt;list&gt;
#include &lt;iostream&gt;

class A
{
public:
    A(int x) : x(x) {}
    int x;
};

int main()
{
    std::list&lt;A*&gt; list;
    for (int i = 0; i &lt; 10; i++) {
        list.push_back(new A(10));
    }

    for (   std::list&lt;A*&gt;::iterator it = list.begin();
            it != list.end();
            it++) {
        std::cout &lt;&lt; (*it)-&gt;x &lt;&lt; std::endl;
    }
}
</code></pre>
<p>C code (using utlist):</p>
<pre><code>#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include "utlist.h"

typedef struct A {
    struct A *prev;
    struct A *next;
    int x;
} A;

int main()
{
    A *list = NULL, *item;
    int i;
    for (i = 0; i &lt; 10; i++) {
        item = calloc(1, sizeof(A));
        item-&gt;x = i;
        DL_APPEND(list, item);
    }

    DL_FOREACH(list, item) {
        printf("%d\n", item-&gt;x);
    }
}
</code></pre>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Automatic object factory in C++</title>
        <link href="https://blog.noctua-software.com/object-factory-c++.html" />
        <id>https://blog.noctua-software.com/object-factory-c++.html</id>
        <updated>2014-06-15</updated>
        <summary>A nice trick for video game code design</summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>Unlike other object oriented languages like java, C++ does not have much
support for introspection.  So for example it is not possible to request the
list of subclasses of a given class.</p>
<p>In this post I am going to talk about a little trick that I found very useful
when dealing with a lot of subclasses of a single base class.  This is
typically what we see in video game, where we have a base <code>Object</code> class that
represents any kind of object in the game, and then one subclass for each type
of object, like the player, enemies, etc.</p>
<p>In its simplest form, we can imagine a program that defines two kind of
entities in the game, one being the player, and the other one an enemy.  Both
inherit from the same base class:</p>
<pre><code>class Object
{
public:
    virtual void iter() {}
};


class Player : public Object
{
public:
    virtual void iter() {
        cout &lt;&lt; "player iter" &lt;&lt; endl;
    }
};

class Enemy : public Object
{
public:
    virtual void iter() {
        cout &lt;&lt; "enemy iter" &lt;&lt; endl;
    }
};
</code></pre>
<p>Typically we would then have a list of objects, and we can fill it with
instances of either <code>Player</code> or <code>Enemy</code>.  As the code grows, we can add other
subclasses of <code>Object</code>.</p>
<p>Now the problem is that each time we add a new object in the game we need to
call the proper constructor for the type we want, so if we want to add one
player and two enemies the code might look like this:</p>
<pre><code>objects.append(new Player());
objects.append(new Enemy());
objects.append(new Enemy());
</code></pre>
<p>However, sometime we don't know in advance what type of object we want to add
into the game.  For example when we read a level file, we might decide the type
by parsing some sort of id.  So what we need is some sort of factory function
that could create any type of object from an id.  It might look like this:</p>
<pre><code>// Static method of Object to create an object from a subclass name.
Object *Object::create(const string &amp;name)
{
    if (name == "Player") return new Player();
    if (name == "Enemy") return new Enemy();
    ...
    return NULL;
}
</code></pre>
<p>Writing this function by hand is a bit tedious when the number of subclass
become important.  Fortunately we can do better than that:</p>
<p>First let's define an abstract factory class, whose only virtual method returns
a new instance of <code>Object</code>:</p>
<pre><code>class ObjectFactory
{
public:
    virtual Object *create() = 0;
};
</code></pre>
<p>Then, we also add a static map of string to factory object to the <code>Object</code>
class, and a static method to register a new factory into the map.</p>
<pre><code>class Object
{
...
public:
    static void registerType(
        const string&amp; name, ObjectFactory *factory)
    {
        fatories[name] = factory;
    }
private:
    static map&lt;string, ObjectFactory*&gt; factories;
};
</code></pre>
<p>So if we assume that the factories map is filled with items that map all the
subclass names to a factory creating a new instance of the subclass, our global
factory function can become:</p>
<pre><code>Object *Object::create(const string &amp;name)
{
    return factories[name]-&gt;create();
}
</code></pre>
<p>Now the only remaining problem is to fill the factories map automatically from
the subclasses of <code>Object</code>.  Turn out there is an elegant way to achieve that.</p>
<p>The idea is to add bellow each subclass a global static instance of a Factory
subclass, whose constructor calls <code>Object::registerType</code>.  Since global object
constructors are called at startup before main, this will automatically fill
our factory list.  The code for the <code>Player</code> class would look like this:</p>
<pre><code>// Player.cpp

class PlayerFactory : public ObjectFactory
{
public:
    PlayerFactory()
    {
        Object::registerType("Player", this);
    }
    virtual Object *create() {return new Player();}
};

static PlayerFactory global_PlayerFactory;
</code></pre>
<p>Since the code is always the same, we can turn it into a convenient macro:</p>
<pre><code>#define REGISTER_TYPE(klass) \
    class klass##Factory : public ObjectFactory { \
    public: \
        klass##Factory() \
        { \
            Object::registerType(#klass, this); \
        } \
        virtual Object *create() { \
            return new klass(); \
        } \
    }; \
    static klass##Factory global_##klass##Factory;
</code></pre>
<p>And now all we have to do is add a line to register the types in each subclass
cpp file:</p>
<pre><code>// Player.cpp
REGISTER_TYPE(Player)

// Enemy.cpp
REGISTER_TYPE(Enemy)
</code></pre>
<p>And there is even an extra advantage: since we never have to access the
subclasses directly -even their constructors- we can get ride of all the header
files, and directly move the subclass definition into the cpp files to make the
code smaller.</p>
<h1>}</h1>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>How our ads based game makes more revenue than our paid version</title>
        <link href="https://blog.noctua-software.com/app-of-the-day.html" />
        <id>https://blog.noctua-software.com/app-of-the-day.html</id>
        <updated>2014-06-02</updated>
        <summary>How we switched our app to a free+ads business model</summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>For more than one year, our game <a href="http://noctua-software.com/voxel-invaders">voxel invaders</a> was present on google play as
both a free demo version and a full paid version.</p>
<p>Recently we decided to remove the demo version and replace it with a free full
version with ads.</p>
<p>To make it simple, we just updated our demo package with a new package
incorporating the <a href="http://www.google.com/ads/admob">admob</a> SDK.</p>
<p>In order to boost the new version, we also cooperated with <a href="http://appturbo.it">app of the day</a>
(previously known as appturbo) to make a promotion of the game.  On April 21,
people who downloaded the free version of the game would get the full version
for free, without ads.  In exchange, 'app of the day' did a promotion on their
website and to the users of their app.</p>
<p>The promotion was a big success in term of download: for the day the promotion
was running, we had more than 60,000 users download voxel invaders.</p>
<p><img alt="Number of active devices on which the application is currently
installed" src="/static/imgs/app-of-the-day/active-devices.png" /></p>
<h2>How did it go in term of downloads and revenues?</h2>
<p>If we do not consider the huge number of downloads from the day of the
promotion.  Before the switch, we had:</p>
<pre><code>                        | demo     |   full
----------------------- | -------- |--------
Average download / day  | 280      | 14
Average revenues / day  |          | 11 USD
</code></pre>
<p>And after the switch:</p>
<pre><code>                        | free/ads |   full
----------------------- | -------- |--------
Average download / day  | 260      | 6
Average revenues / day  | 90 USD   | 5 USD
</code></pre>
<p>So we went from an average of 11 USD / day to an average of 95 USD / day,
<strong>more than 8 times more!</strong>.</p>
<p>Interestingly, the average number of downloads of the free full version with
ads is more or less the same than the demo version, even though I was expecting
an increase due to the promotion.  But maybe without the promotion the number
would have decreased.  It is hard to tell since we did both the promotion and
the switch to a free+ads model at the same time.</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>How to deal with spaghetti legacy code</title>
        <link href="https://blog.noctua-software.com/deal-with-legacy-code.html" />
        <id>https://blog.noctua-software.com/deal-with-legacy-code.html</id>
        <updated>2014-04-07</updated>
        <summary>Something we all hate, but all have to do at some point</summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>At least three times in my career as a programmer, I had to work on huge,
complicated code written by other people.  In one case I was specifically
hired to replace the original maintainer who -as I later learned- left the
company from a burnout caused by the frustration of having to maintain his own
code.</p>
<p>Inheriting legacy code is common, and few people enjoy it.  Here I put down a
few ideas that I think might help to deal with the situation.</p>
<h2>Don't blame the original coder</h2>
<p>It is easy to implement an algorithm using complicated code, everybody can do
it.  The difficulty is to implement the same algorithm using simple code.  That
is what only few people can achieve.  It is hard mostly because for the person
who writes the code, it always seems simple.  It already happened to me that I
had to fix a bug in some old software, wondering who was responsible for such a
bad code, before realizing I was the original author.  Beside, the quality of
simplicity is different for everybody.  Some people will find C++ classes
difficult to read, while some other might have troubles with C pointers.  When
you have to work on a project, it is best to adjust to the internal logic of it
rather than dismissing all the code as ugly.</p>
<p>Always remember that the guy coming after you might well say that <em>you</em> are a
bad coder.</p>
<h2>Get the full source code history in a local git repository</h2>
<p>If you are lucky enough, the project would already use git (or an other
distributed source code management system), but most of the time you will have
at best a SVN server, and at worse a few zip files containing snapshots of the
code at different time.  If you can, convert the project into a git repository
for your personal use.  There are various tools that can help you to do that.
The reason why you need that is because as you will try to understand the code,
you will need to guess not only what it does, but also <em>why</em> it does it.  To
answer this, being able to dig into the history of the code is invaluable.  For
example you might see some tricky lines of code that do not seem to be needed.
With the <code>git blame</code> command, you can see at what revision this code was added.
Maybe this code wasn't there before, and was added to solve a specific issue
that you might not understand yet.</p>
<p>Also, the code probably started simple, and did grow into the spaghetti mess
that you have to deal with.  Reading the first versions of the code will allow
you to differentiate the actual logic of the code from the hacks that have
grown around it.</p>
<h2>Make sure build and deploy works</h2>
<p>This is the first thing to do, even before you start to read the code.  You
should know how to compile it and run it.  It sounds obvious, but often, poorly
maintained projects have complicated build process, you might need to download
some extra libraries, to manually copy files around at each compilation, etc.
the compilation of the project shouldn't need anything else than calling
<code>make</code>, and it is a waste of time to try to improve the code while this is not
the case.</p>
<h2>Learn how to navigate the code</h2>
<p>You are going to spend most of your time reading code, instead of writing it.
Code reads a bit like those "book-where-you-are-the-hero", in the sense that
you keep jumping from function to function, possibly in different files.  If
you use vim or emacs, check ctags or the several code browsing plugins.  If the
code has been written with a specific IDE, it might be better to just use it,
even if this is not you favorite one.  Spending time greping in files is not
fun and will frustrate you too much, so make sure you have the proper
environment that allows you read the code as quickly as possible.</p>
<h2>Refrain from fixing 'anti-pattern' (just yet)</h2>
<p>We have all been taught that global variables are bad, that we should use
classes to abstract specialized algorithms, that functions and variable names
should have meaningful names, that we should use accessors to read and write
instances attributes, etc.  Every programmer has his own list of best practices
that he tries to follow.  So of course, when we read other people code, we get
upset to encounter those "anti-patterns" all around the code, and our first
reaction is to try to remove them.  This is a bad idea.</p>
<p>First of all, you shouldn't re-factor the code before you have a deep
understanding of it.  Many seemingly minor changes could have side effects that
you will not be able to track.  So the rule should be to try to touch the code
as little as possible in the beginning.</p>
<p>Then, those anti-pattern could be there for a good reason.  Sometime it makes
sense no to follow the rules, specially in big complicated projects.  You might
spent hours trying to make the code follow your personal idea of cleanliness
just to realize that it is not possible in that case.  Instead, when you see
something that seems just plain ugly, dig into the code history and try to
figure out why the code is like this.</p>
<p>Finally, I think that many of those so called "best practice" are in fact
stupid rules that people shouldn't follow blindly.  The only rule that really
matters is that the code should be easy to read, and should have as little
abstraction layers as possible.</p>
<h2>Ignore comments</h2>
<p>In my experience, average quality code are full of comments, and most of them
are only noise on the screen.  Even when you see a comment that actually
provides some extra information that the code does not carry, you should still
be suspicious: the comments might be outdated or just plain wrong.  It is
better to just read the code and understand it, and treat comments as hints.</p>
<h2>Start with small bug fixes</h2>
<p>It's going to take some time before you understand the code enough to clean it.
So at the beginning, it is more rewarding to focus on small parts of the code,
one at a time.  The best is to try to fix specific bugs that you can reproduce.
That way you will build up your knowledge of the design of the code.  After a
few weeks of working with the code, you will start to get a deeper
understanding of it, some of the patterns will become clear, and you will be
able to "stop seeing the code", but directly the algorithms.  Once you have
reached this point, you can start to work on bigger tasks.  Trying to fix
everything too quickly is just asking to get burned out.</p>
<h2>Re-factor only if it makes the code smaller</h2>
<p>After you've been working on the code for a while, you can try to actually
re-factor the code to make it easier to read and maintain.  I think a good rule
to keep in mind is that a re-factoring is only an improvement if it fixes an
actual bug, or if it makes the code smaller.  Spaghetti code usually contains a
small ratio of business code / total code.  One possible re-factorization could
be to use a well known library to replace part of the code, or to remove unused
functions (poorly maintained projects are full of it).</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>ARM assembly for iOS with XCode</title>
        <link href="https://blog.noctua-software.com/arm-asm.html" />
        <id>https://blog.noctua-software.com/arm-asm.html</id>
        <updated>2014-03-31</updated>
        <summary>Sometime you have to get your hands dirty</summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>This week-end I spent a lot of time trying to optimize the code of my incoming
video game.  The game engine features a fluid simulation algorithm based on
this <a href="http://www.intpowertechcorp.com/GDC03.pdf">excellent paper</a> by Jos Stam.</p>
<p>The algorithm is CPU intensive, and so I tried my best to make the code as fast
as possible, which eventually means having to write the code in assembly.</p>
<p>Since I couldn't find much information about how to write assembly code for iOS
on XCode, here is a small tutorial I wrote about it.</p>
<h2>GCC inline assembly</h2>
<p>XCode recognises any file ending with ".s" in a project as assembly.  Any
function declared in the file can then be called from the C code.  For small
chunk of assembly, though, there is a better way: using the C <code>asm</code> statement,
that allows to embed and call assembly code directly from C.  For an overview
of the syntax you can refer to <a href="http://www.ethernut.de/en/documents/arm-inline-asm.html">this documentation</a>.</p>
<p>Let's write a basic example: a function that adds two integers together using
the ARM <code>add</code> instruction.  Needless to say, for such a simple example there is
no advantage to writing assembly, the C version would result in exactly the
same binary code.</p>
<pre><code>static int add_two_int(int x, int y) __attribute__((noinline));

#ifdef __arm__

static int add_two_int(int x, int y) {
    int ret;
    asm volatile (
        "add %[ret], %[x], %[y]"
        // outputs
        : [ret]"=r"(ret)
        // inputs
        : [x]"r"(x), [y]"r"(y)
    );
    return ret;
}

#else

static int add_two_int(int x, int y) {
    return x + y;
}

#endif
</code></pre>
<p>A few notes about this code:</p>
<ul>
<li>
<p>Why the <code>__attribute__((noinline))</code>?  I marked the function as static - as
  you always should for function local to a file.  But since I want to check
  the produced asm code in XCode, it is nice to force the compiler not to
  inline the function.  Clang recognises most of GNU gcc attributes, so we can
  use it the <code>noinline</code> attribute to mark the function as not to be inlined.</p>
</li>
<li>
<p>I wrote a fall-back version of the code in plain C so that I support not only
  ARM but also other architectures.  For example the new iPhone 5 are now using
  arm64, so in that example XCode would compile the arm64 version using the
  fallback C code (if we wanted to also provide an arm64 version of the asm
  function we could have tested the <code>__arm64__</code> macro.</p>
</li>
<li>
<p>In the assembly code, I never actually use a named register.  Instead I
  aliased <code>ret</code>, <code>x</code> and <code>y</code> in the output and input lists, then I can refer to
  them in the asm as %[ret], %[x], %[y].  The compiler will automatically
  assign them to one of the 'r' registers.  This is the nice thing about inline
  assembly.</p>
</li>
<li>
<p>The volatile keyword is used to let clang knows that it should not try to
  optimize the assembly code (yes it would do that!)  Of course in that case I
  don't see how we could optimize such a simple function.</p>
</li>
<li>
<p>Some people would use <code>__asm__</code> instead of <code>asm</code>, and <code>__volatile__</code> instead
  of <code>volatile</code>.  Those are exactly the same. the versions with underscores may
  be used to prevent clash with already defined functions or variables (and are
  mandatory if you want to code to compile in strict ansi mode.)  I prefer to
  use the short versions here for simplicity.</p>
</li>
</ul>
<p>OK, so we can put this in our code and then use the function <code>add_two_int</code> as
if it was a normal static C function (and in fact it is!)</p>
<p>XCode is nice enough to let us see what the compiled version looks like.  For
that we have to click on the tuxedo looking like button in the top right of the
IDE, then in the drop down menu select "assembly".  A quick search for
<code>add_two_int</code> reveals the actual assembly code produced by the compiler:</p>
<pre><code>_add_two_int:
    add r0, r0, r1
    bx lr
</code></pre>
<p>As we see, the compiler decided to use the r0 register for both <code>ret</code> and <code>x</code>,
and the r1 register for <code>y</code>.  This is a clever choice, since the C function
call convention is using r0 and r1 to pass the two first parameters, and r0 as
the output.  Also note that the actual function name starts with an underscore,
as specified by the C ABI.</p>
<h2>A bigger example, with NEON instructions</h2>
<p>Now let's try with a slightly more interesting example.  We are going to use
the vector instructions of the NEON arm extension to quickly divide all the
values of a 16 bit integer array by 2.  To make things really simple, we are
going to assume that the size of the array is a multiple of 8 and that it is
memory aligned to 128 bit.</p>
<p>In this new version of the code, we are using the NEON instruction vst1.16 that
can right shift eight 16bit integers in parallel.</p>
<pre><code>#if defined __arm__ &amp;&amp; defined __ARM_NEON__

static void div_by_2(int16_t *x, int n)
{
    assert(n % 8 == 0);
    assert(x % 16 == 0);
    asm volatile (
        "Lloop:                         \n\t"
        "vld1.16    {q0}, [%[x]:128]    \n\t"
        "vshr.s16   q0, q0, #1          \n\t"
        "vst1.16    {q0}, [%[x]:128]!   \n\t"
        "sub        %[n], #8            \n\t"
        "cmp        %[n], #0            \n\t"
        "bne Lloop                      \n\t"
        // output
        : [x]"+r"(x), [n]"+r"(n)
        :
        // clobbered registers
        : "q0", "memory"
    );
}

#else

static void div_by_2(int16_t *x, int n)
{
    int i;
    for (i = 0; i &lt; n; i++)
        x[i] /= 2;
}

#endif
</code></pre>
<p>A few notes:</p>
<ul>
<li>
<p>Since we are using the ARM NEON extension, we have to check for
  <code>__ARM_NEON__</code> as well as <code>__arm__</code>.  Just testing for <code>__ARM_NEON__</code> would
  generate an error when compiling for arm64.</p>
</li>
<li>
<p>The loop label has to start with an upper case 'L' to tell the compiler that
  it is a local label.  Apparently, if you don't do it and the compiler decides
  to duplicate the assembly code, you might get a compilation error.</p>
</li>
<li>
<p>Don't forget to specify the output as read/write (with <code>"+r"</code>).  If you do
  not, you might run into strange run-time bugs because the compiler will
  assume that <code>x</code> and <code>n</code> have not changed after your function call and might
  optimize the calling function accordingly.</p>
</li>
<li>
<p>All the lines end with "\n" (end of line) followed by a "\t" (tab).  This
  make sure that your assembly code will be correctly indented.</p>
</li>
</ul>
<h2>Conclusion</h2>
<p>Being able to write assembly code is actually the easy part.  What is difficult
it to write <strong>efficient</strong> assembly code.  By efficient I mean that the code
should be faster than what the compiler would have produced from the C code.  I
am not that good at assembly, and that's why I am not going to claim that the
assembly code I put in this example will actually be faster than the C version.
In fact, with the memory access time being so slow on iPhone, I wouldn't be
surprised if both versions have similar performances (which is why measuring
the performance is so important).</p>
<p>In a follow up post I will try to compare the speed we can achieve with NEON
assembly compared to plain C version on an iPhone 4.</p>
<p>And if you want more information about ARM and NEON on iOS, here is a <a href="http://shervinemami.info/armAssembly.html">detailed
article</a> I found online that goes much deeper into the topic:</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Procedural rendering test 1</title>
        <link href="https://blog.noctua-software.com/jproc.html" />
        <id>https://blog.noctua-software.com/jproc.html</id>
        <updated>2014-03-17</updated>
        <summary></summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>For my incoming video game project I wrote a small procedural rendering engine.
So now I can do all kind of creepy animations. Here is an example, compiled to
javascript using <a href="https://github.com/kripken/emscripten/wiki">emscripten</a>.</p>
<div>
<div id="status">Downloading...</div>
<canvas class="emscripten" id="canvas"
        oncontextmenu="event.preventDefault()"></canvas>
</div>

<script src="http://code.jquery.com/jquery-latest.min.js"></script>

<script type='text/javascript'>
  var Module = {
    filePackagePrefixURL: "/static/other/jproc/",
    preRun: [],
    postRun: [],
    print: function(text) { console.log(text); },
    printErr: function(text) { console.log(text); },
    canvas: document.getElementById('canvas'),
    setStatus: function(text) {
      var statusElement = document.getElementById('status');
      statusElement.innerHTML = text;
    },
    totalDependencies: 0,
    monitorRunDependencies: function(left) {
      this.totalDependencies = Math.max(this.totalDependencies, left);
      Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
    }
  };

  $.getScript("/static/other/jproc/jproc.js");
</script>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Vocabulary learning wallpaper with python and gnome</title>
        <link href="https://blog.noctua-software.com/chinese-wallpaper.html" />
        <id>https://blog.noctua-software.com/chinese-wallpaper.html</id>
        <updated>2014-03-09</updated>
        <summary>Show Chinese words on your gnome desktop</summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>Here is a simple way to customize your gnome desktop to show random text taken
from a file.  I use it to learn Chinese, but it can be easily modified for any
other kind of textual information.</p>
<p>This is how I did it:</p>
<ol>
<li>
<p>Using inkscape, I made a template SVG file for the background.  I started
   from the default gnome 3 wallpaper, and I added three text elements: one for
   the Chinese characters, one for the pinyin, and one for the English
   translation.  In place of the text I used <code>"@chinese@"</code>, <code>"@pinyin@"</code>, and
   <code>"@english@"</code>.  Later my script will substitute those placeholder words with
   the correct values.</p>
</li>
<li>
<p>I created a input file with all the entries I want to learn.  The file looks
   like this:</p>
<pre><code>容易 [rong2 yi4] easy
決心 [jue2 xin1] determination
期待 [qi1 dai] to look forward
冷淡 [leng3 dan4] cold, indifferent
寂寞 [ji4 mo4] lonely
突然 [tu1 ran2] sudden
反正 [fan3 zheng] anyway
招牌 [zhao1 pai] signboard
討厭 [tao3 yan4] to dislike
努力 [nu3 li4] great effort, to strive
原諒 [yuan2 liang4] to excuse, to forgive, to pardon
</code></pre>
</li>
<li>
<p>I wrote this python script.  Every 10 minutes, the script would pick a new
   random entry from my input file, generate a png file from the svg template,
   and run the gsettings command to use it as a new wallpaper.</p>
<h1>!/usr/bin/env python</h1>
<p>import codecs
import os
import random
import re
import shutil
import subprocess
import tempfile
import time</p>
<p>INPUT_FILE = "/home/guillaume/notes/chinese.txt"
PERIOD = 60 * 10        # Change image every 10 minutes
PWD = os.path.dirname(<strong>file</strong>)</p>
<p>def get_entry():
    entries = []
    reg = re.compile(r"^(.+?) [(.+)] (.+)$")
    for line in codecs.open(INPUT_FILE, "r", "utf-8"):
        m = reg.match(line)
        if m is None:
            continue
        entries.append((m.group(1), m.group(2), m.group(3)))
    return random.sample(entries, 1)[0]</p>
<p>def create_png(tmpdir, chinese, pinyin, english):
    svg = open("%s/wallpaper.svg" % PWD).read()
    svg = svg.replace("@chinese@", chinese)
    svg = svg.replace("@pinyin@", pinyin)
    svg = svg.replace("@english@", english)
    svg_path = "%s/wallpaper.svg" % tmpdir
    png_path = "%s/wallpaper.png" % tmpdir
    out = codecs.open(svg_path, "w", "utf-8")
    out.write(svg)
    out.close()
    subprocess.call(["inkscape", svg_path, "-e", png_path])
    return png_path</p>
<p>def set_wallpaper(path):
    subprocess.call(["gsettings", "set",
                     "org.gnome.desktop.background", "picture-uri",
                     "file:///%s" % path])
    subprocess.call(["gsettings", "set",
                     "org.gnome.desktop.background",
                     "picture-options", "zoom"])</p>
<p>while True:
    tmpdir = tempfile.mkdtemp("chinese-wallpaper")
    entry = get_entry()
    png_path = create_png(tmpdir, *entry)
    set_wallpaper(png_path)
    time.sleep(PERIOD)
    shutil.rmtree(tmpdir)</p>
</li>
<li>
<p>I added the script to my startup application (using
   <em>gnome-session-properties</em>).</p>
</li>
<li>
<p>Log out and log in again.  Now my wallpaper looks something like this:</p>
</li>
</ol>
<p><img alt="Chinese wallpaper" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/chinese-wallpaper.png" /></p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Bash script to compress pdf files</title>
        <link href="https://blog.noctua-software.com/compress-pdf-files.html" />
        <id>https://blog.noctua-software.com/compress-pdf-files.html</id>
        <updated>2014-03-03</updated>
        <summary>A useful tool for freelancers</summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>Since I work as a freelance, I have to deal with a lot of pdf files I receive
from clients.  Some of them contain uncompressed images that make the files
bigger than they need to be.</p>
<p>On this <a href="http://unix.stackexchange.com/questions/50475/how-to-make-ghostscript-not-wipe-pdf-metadata">stackexchange question</a>, a user named Marco gives us a nice way to
use <a href="http://pages.cs.wisc.edu/~ghost">ghostscript</a> to compress any pdf file.</p>
<p>There is a loss of quality in the compression, but most of the time it is
acceptable.</p>
<p>Here is the script:</p>
<pre><code>#!/bin/sh

INPUTPDF=$1
OUTPUTPDF=$2
TMPPDF=$(mktemp)
METADATA=$(mktemp)

# save metadata
pdftk "$INPUTPDF" dump_data_utf8 &gt; "$METADATA"

# compress
gs                       \
  -q                     \
  -sOutputFile="$TMPPDF" \
  -sDEVICE=pdfwrite      \
  -dNOPAUSE              \
  -dBATCH                \
  -dPDFSETTINGS=/ebook   \
  "$INPUTPDF"

# restore metadata
pdftk "$TMPPDF" update_info_utf8 "$METADATA" output "$OUTPUTPDF"

# clean up
rm -f "$TMPPDF" "$METADATA"
</code></pre>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>My personal calendar using Remind</title>
        <link href="https://blog.noctua-software.com/personal-calendar-using-remind.html" />
        <id>https://blog.noctua-software.com/personal-calendar-using-remind.html</id>
        <updated>2014-02-24</updated>
        <summary>How I keep organised</summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>I made a cool <em>"getting things done"</em> script on my computer: any time I type
the 'rem1' command, I see a nicely formatted calendar of the current and
following three weeks.  It also prints out the most urgent things I need to do.
It looks like that:</p>
<p><img alt="remind output" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/rem1.png" /></p>
<p>In the past I tried several different note tracking tools, most notably the
great <a href="http://orgmode.org">org-mode</a>.  I like the concept of keeping all my personal notes locally
into plain text files.  The problem I have with org-mode is that it only works
with emacs, while I prefer to use vim.</p>
<p>Following the unix philosophy, I think the most important is not the tools used
to process the data, but the way we store them, and plain text is obviously
superior to anything (note: xml is <em>not</em> plain text!).  For example even though
I don't even use emacs anymore, I can still open and read my old org-mode
files, and I know I always will.</p>
<p>All my notes file are stored locally on my computer in the directory <em>~/Notes</em>.
Some of the files have special meaning, for example ~/Notes/todo.txt contains
my to-do list.  I also have one file per day for my diary and schedule, stored
as <em>~/Notes/days/yyyy-mm-dd.txt</em>.</p>
<p>Now, I wanted a way to print out an overview of my daily and weekly schedule.
That's where <a href="http://www.roaringpenguin.com/products/remind">remind</a> comes to play.  Remind is a unix tool similar to the
venerable 'calendar' command.  I am not going to enter into the details of
remind, it is a flexible software used to define calendar events in text files,
and generate different kind of outputs from it.  The syntax of a basic remind
event is:</p>
<pre><code>REM 2014-02-24 write a new blog post
</code></pre>
<p>It can get much more complex than that, for an comprehensive documentation
refer to the <a href="http://linux.die.net/man/1/remind">man page</a>.</p>
<p>Of course I do not want to change the format of my note files to accommodate
any special tool.  Instead what I did is write a small python script that
parses my files, generate the corresponding remind file, and then invoke remind
on it to show me the nice output you can see in the beginning of this post.</p>
<p>(note: I am not distributing the script for the moment, since it is dependent
on the way I organize my note files, but if you have some interest to see it,
please drop me an email, I'll make it available under a GPL licence.)</p>
<p>I am quite satisfied with the result.  I think many people do not want to use
remind because of the forced syntax, but once you use it as an intermediary
step in the process of managing your note files it can come in handy.</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Image color wheel generator</title>
        <link href="https://blog.noctua-software.com/image-color-whell-generator.html" />
        <id>https://blog.noctua-software.com/image-color-whell-generator.html</id>
        <updated>2014-02-16</updated>
        <summary>Turn any image into a Hue/Luminosity color wheel</summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p><a href="http://www.noctua-software.com/charmony">Here is a little side project</a> I did, that allows to quickly get an
idea of the color distribution of an image in the Hue / Luminosity <a href="http://en.wikipedia.org/wiki/Color_wheel">color
wheel</a>.</p>
<p>This can be used by designer to check if an image follows some specific color
scheme, like using only complementary colors.</p>
<p>I wrote the code in C, and compiled it in JavaScript with
<a href="https://github.com/kripken/emscripten/wiki">emscripten</a>.</p>
<p>I am planning to write a following post to show how I intend to use this tool
for a new video game project I am working on.</p>
<p><img alt="an image" src="https://d28vz5pczegriy.cloudfront.net/static/imgs/charmony-example.png" /></p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>Linus subsurface funny commits</title>
        <link href="https://blog.noctua-software.com/linus-subsurface.html" />
        <id>https://blog.noctua-software.com/linus-subsurface.html</id>
        <updated>2014-01-13</updated>
        <summary>Linus Torvald is a funny guy</summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>Linus Torvald is mostly famous for being the creator of the <a href="https://www.kernel.org/">Linux
Kernel</a> and <a href="http://git-scm.com">git</a>.  He is also the author of a diving application:
<a href="http://subsurface.hohndel.org/">subsurface</a>.</p>
<p>What I like about subsurface is that we can see how Linus works on a small
scale project, similar to the kind of things most developers like me work on.</p>
<p>I really recommend reading the <a href="http://git.hohndel.org/?p=subsurface.git;a=search;s=Linus+Torvalds;st=author">git logs of Linus</a>: not only are they
perfectly written and formatted (check how he separates sentences with two
spaces), but they also sometime contain funny parts.  Here are a few examples
of what we can find in it:</p>
<pre><code>Gaah.  XML is *stupid*.  It's not easy to parse for humans or for
computers, and some of these XML files are just disgusting.
</code></pre>
<hr />
<pre><code>I need to get stinking drunk before I look at more xml mess.
</code></pre>
<hr />
<pre><code>.. nice compiler warning hidden by the crazy gcc pointer sign warnings
that nobody wants to see (yes, we really do want to do 'strlen()' even
on unsigned strings, don't complain, crazy bitch compiler).
</code></pre>
<hr />
<pre><code>"But XML is portable". Crazy people.
</code></pre>
<hr />
<pre><code>I'd like to ask what drugs Suunto people are on, but hey, it's a Finnish
company.  No need to ask.  Vodka explains everything.  LOTS AND LOTS OF
VODKA.
In comparison, the libdivecomputer output is nice and sane
</code></pre>
<hr />
<pre><code>Did I just say "In comparison, the libdivecomputer output is nice and
sane"?

It turns out that libdivecomputer has been doing some drugs too when it
comes to gas mixes.  Like showing O2 percentages as 255.0% and N2
percentages as -155.0%.
</code></pre>
<hr />
<pre><code>"subsurface"? I don't know.  Maybe I should follow the "name all
projects after myself" mantra.  "divenut"?
</code></pre>
<hr />
<pre><code>Oh Gods. Why are all other scuba programs so f*&amp;% messed up?
</code></pre>
<hr />
<pre><code>Ok, this is the ugliest f*&amp;$ing printout I have ever seen in my life,
but think of it as a "the concept of printing works" commit, and you'll
be able to hold your lunch down and not gouge out your eyeballs with a
spoon.  Maybe.
</code></pre>
<hr />
<pre><code>Make the printout look different

Not *better* mint you. Just different.

I suck at graphs.
</code></pre>
<hr />
<pre><code>Sue me: I'm not a fan of Serif
</code></pre>
<hr />
<pre><code>xml-parsing: accept 'sitelat' and 'sitelon' for GPS coordinates

Are they ugly and insane tags? Yes.  Are they used? Bingo.  MacDive uses
this lovely format for specifying dive site location.

Christ, if you look up "Ugly dialog" on Wikipedia, I think it has a
picture of this "New dive" thing.  Or it should have.
</code></pre>
<hr />
<pre><code>Make our 'ascii_strtod()' helper more generic

We'll want to do sane parsing of strings, but the C library makes it
hard to handle user input sanely and the Qt toDouble() function
interface was designed by a retarded chipmunk.
</code></pre>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
    <entry>
        <title>My new blog</title>
        <link href="https://blog.noctua-software.com/new-blog.html" />
        <id>https://blog.noctua-software.com/new-blog.html</id>
        <updated>2014-01-11</updated>
        <summary>Like it or not, this is my blog.</summary>
        <content type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
                <p>This is the first post on my new blog.  I decided to stop using blogspot
(<a href="http://charlie137-2.blogspot.com">here</a> is a link to my previous blog) for a few reasons:</p>
<ul>
<li>
<p>Blogspot does not allow to write the posts in a markup language like
  <a href="https://daringfireball.net/projects/markdown">markdown</a> or <a href="http://docutils.sourceforge.net/rst.html">reStructuredText</a>.  Instead you have to use either the
  "user friendly" interface that seems to never render things the way you want
  to, or directly type the post in html.</p>
</li>
<li>
<p>Blogspot has no support for showing code.  I guess most bloggers don't really
  need that, but I do.  Until now I used <a href="http://highlightjs.org">highlight.js</a> that did the job
  quite well, but forced me to remember to use the proper html tags in my
  posts.  Not an optimal solution.</p>
</li>
<li>
<p>I want to keep a local copy of my blog on my computer, and a backup on a
  server using a version control system.</p>
</li>
<li>
<p>Finally, I wanted to write my own blog engine, for the fun of it.</p>
</li>
</ul>
<p>The code of this blog is very minimalist.  I wrote the whole thing in 400 lines
of python, using <a href="http://webpy.org">webpy</a>, <a href="http://jinja.pocoo.org">jinja2</a>, and <a href="http://getbootstrap.com">bootstrap</a>.  If some people show
interest to get the source code I will happily release it under a free licence.</p>
<p>2017-03-15 Update: Today I decided to use jekyll instead, as it does everything
my code was doing and more.  You can probably use the internet time
machine to find the original look of this blog.</p>
            </div>
        </content>
        <author>
            <name>Guillaume</name>
            <email>guillaume@noctua-software.com</email>
        </author>
    </entry>
    
</feed>