Nuclex Signal/Slot Library: Benchmarks

When you’re writing some code that needs to notify code in othe r parts of the program, your weapon of choice is the "signal / slot concept". A signal is a connection point where any interested party can register a callback function to be invoked when the signal emits/fires.

There’s already an ocean of libraries out there providing this functionality to C++, but as you will see in this article, they’re all suffering from performance issues in one way or other. Plus, most don’t compile without warnings, have inconvenient sytax or lack unit tests.

So here’s the signal/slot "library" (it’s just three headers) I wrote to fix those issues for me, together with a summary of my design goals and a comprehensive benchmark on different compilers and CPUs.

Read More

How Many Watts Does a Power Supply Need?

This post has been sitting in my queue for a little too long, but maybe it helps someone out there. In 2016 I last upgraded my PC and went for a beefy system that would speed up my renders and game engine lighting builds (in this case, the more cores, the better).

I went for a dual socket motherboard and two Xeon CPUs rated at 130 Watts each. And I added the back then top-of-the-line GPU, an NVidia GeForce GTX 1080, rated at 180 Watts.

Picture of a dual CPU motherboard with water coolers mounted on a table-like wood construction

So how big should my PSU be?

Read More

Running on Bad Memory

Being able to rely on your memory is one of the most important aspects of having a stable PC. Thus, paying extra for premium memory seemed like a wise choice to me.

Yet I have been surprisingly unlucky with my memory.

In this post I’ll show how to identify broken memory cells and how to prevent Windows and Linux from accessing them, resulting in a stable system while discarding only a few Kilobytes of memory.

Read More

Using Wacom Touch Gestures in Unsupported Applications

I’ve recently made the decision to learn some drawing skills. Specifically, I wanted to start with a tablet right away so I could avoid having to re-teach myself to using a tablet instead of paper. However, I still consider it important to be able to shift and rotate the canvas with my hand, so I went for a Wacom tablet with touch.

Most artists I knew swear by Paint Tool SAI, but that just happens to be one of the applications not fully supported by Wacom’s drivers (pressure works fine, but touch is a no-go). Luckily, Wacom’s drivers are pretty flexible and you can easily modify them to support your favorite application.

Here’s a guide to enable pinch-zoom, panning and two-finger rotation in Paint Tool SAI and Manga Studio!

Read More

Why you should Indent with Spaces

I avoid tabs in all code I write. That’s why you can read my code in your browser with the exact same formatting as it had in my IDE – no matter what browser or device you are using: Nuclex Framework sources in TRAC. Yet from time to time, I encounter people evangelizing tabs.

While I am fine with working on projects that use tab characters and I neither argue about the decision to use tabs nor intentionally divert from their style, I do have an opinion and it is that you should avoid tabs wherever you can.

Even the one, lonely, supposed advantage of tabs – users being able to choose their preferred indentation level – is a drawback in disguise because you no longer can set a safe limit for line lengths (see below).

Read More

Simple Main Window Class

Here’s another fairly trivial code snippet. I’ve stumbled across some borked attempts at initializing and maintaining rendering windows for games lately. Most failed to properly respond to window messages, either ignoring WM_CLOSE outright or letting DefWindowProc() call DestroyWindow() when WM_CLOSE was received, thereby not giving the rest of the game’s code any time to cleanly shut down before the window handle becomes invalid.

So I’ll provide a clean and well-behaved window class here. It doesn’t use any global variables – in fact, you could create any number of windows from any number of threads. WM_CLOSE simply causes the class’ WasCloseRequested() method to return true, so by polling this method you can first shut down graphics and input devices and then destroy the window in an orderly fashion.

For your convenience I also added some helper methods: one resizes the window in a way that ensures the client area will actually end up with the exact pixel size requested. Another will center the window on the screen without messing up if the user has extended his desktop over multiple monitors.

Read More

Thread-Safe Random Access to Zip Archives

Many games choose to store their resources in packages instead of shipping the potentially thousands of individual files directly. This is sometimes an attempt at tamper-proofing, but mostly it is about performance. Try copying a thousand 1 KiB files from one drive to another, then copy a single 1 MiB file on the same way – the former operation will take many times longer.

A good choice for a package format is the well known .zip archive. It’s not necessarily well-designed, but it decompresses fast and you definitely won’t have any problems finding tools to create, modify and extract .zip archives. Thus, when I started work on a file system abstraction layer for my future games, seamless .zip support was one of my main goals (I may also add 7-Zip at a later time just for fun).

Here is the design I came up with after exploring the file system APIs available on Windows, WinRT and Linux:

UML diagram showing the design of my file system abstraction layer

You may notice some rather radical design choices in my File class: there are no Open() or Close() methods and there is no Seek() method, either – each read or write specifies the absolute position without requiring the file to be opened.

Read More

Ogre 1.8.0 for WinRT/Metro

Ogre 3D Logo

In March I provided some binaries of Ogre 1.8.0 RC1 that were based on Eugene’s Metro port of Ogre, allowing Ogre to run as a native Metro App, using the Direct3D 11 renderer and RTShaderSystem for dynamic shader generation.

Those binaries no longer work with the Windows 8 Release Preview and Visual Studio 2012 RC, so I thought I’d provide an updated package!

Screenshot of Ogre 1.8.0 on Windows 8 Release Preview running as a Metro app

This time I went a bit further: while the last package was compiled with multithreading disabled, I have in the meantime ported Boost 1.50.0 to compile on WinRT (using a slightly modified version of Shawn Hargreaves’ WinRT CreateThread emulation code. Thus, this Ogre build has full support for multithreading and includes Boost!

Read More

Code Better: Headers without Hidden Dependencies

When you work on a larger project, you cannot easily keep track of which header depends on which other header. You can (and should) do your best to keep the number of other headers referenced inside your headers low (to speed up compilation) and move as many header dependencies as you can into your source files, but this still doesn’t prevent you from building headers that implicitly depend on another header being included before them.

Take this example:

#ifndef MAP_H
#define MAP_H

/// <summary>Stores the tiles of a 2D map<summary>
struct Map {};

#endif // MAP_H
#ifndef WORLD_H
#define WORLD_H

#include "Actor.h"
#include <vector>
// Oops, forgot Map.h, but won't notice since World.cpp includes Map.h before World.h

/// <summary>Maintains the state of the entire world<summary>
struct World {
  /// <summary>Stores the map as a grid of 2D tiles</summary>
  public: Map Map;
  /// <summary>Actors (player, monsters, etc.) currently active in the world</summary>
  public: std::vector<Actor *> Actors;
};

#endif // WORLD_H

Throughout your project, map.h might always end up being included before world.h and you might never notice that if someone included world.h on its own, a nasty compilation error would be the result.

So what can you do ensure this situation never happens?

Read More