Posts

Contexted drop in Rust

 In game development, it is often the case that, for performance reasons we allocate objects from a pool. When we want to clean up an allocated object, we need to release it back to the pool it was allocated from. Of course C++ is an unsafe language . It is very easy to cause one of various errors with such a pattern: Forget to free an allocated object (memory leak) The pool is freed before the allocated objects (dangling pointers) The pool is accessed unsafely from multiple threads (data races) We free the object to another pool than the one it was allocated from  Pool allocations have another quirk. They feel non-idiomatic . The idiomatic way of freeing an object in C++ is relying on its destructor. However, passing a pool as an argument to destructors in C++ is not possible as destructors in C++ take no arguments. The only workaround seems to be storing a pointer or reference to the pool in each allocated object. While this will allow easy cleanup of allocated objects through their

Improving the performance of UI list updates

My experience has shown that a recurrent problem in editors is updating lists of objects in a user interface fast . Example: entities in a level. Layers in a scene, etc, etc. An implementation usually stores the objects separately and an extra list of the object pointers in the UI classes themselves. Such lists might, for instance be stored in an array inside a subclass of a QAbstractItemModel if one works in Qt. Note: The code below does not use standard library classes, but it should hopefully be somewhat easy to follow Updating In a naive, object oriented approach there is a set method in our entity class. An implementation might look like this: 1 2 3 4 5 void Entity :: setName(String name) { m_Name = name; m_NameChangedSignal( this ); } Our UI class is a listener of the m_NameChangedSignal (which might be an implementation of signals as in my previous post on variadic templates ) and as soon as the name is set, an update is fired. Our UI class'

Logarithmic scales for UI

Logarithmic controls are common in games, especially in situations where high precision is required in low values and low precision in high values. Another use case is when we want to increase a value proportionally to its current value. One such example is speed of motion, as used in Unreal's navigation system. When the mouse wheel is rolled, motion speed is increased proportionally to its current value. That is necessary for the user to "feel" the increase in speed. If, say, speed is increased by constant increments, then there will be a time when the increase in speed won't be significant enough for the user to notice any difference. If we choose to increase speed by a constant increment of 0.1m/s, then if the user starts from a speed of 0m/s, then the first increment would be very abrupt (what if we want to move very slowly instead?) but if we want to move very fast , a potentially uncomfortably large number of rolls would be required to change between fast a

Implementing signals with variadic templates

One of the most popular patterns in modern C++ is the use of signals. Signals are an implementation of the observer pattern, where observers register, or connect themselves to a signal, so that the signal can notify them when it has been triggered. This paradigm has been used in a lot of software, with Qt's signal/slot system probably being the most widely known. The signal is usually called by using operator () and passing a certain number of arguments. Observers usually implement a function that matches the signature of the operator () and when operator () is called, each function of the registered observers is called with the same arguments. Modern C++ allows us to define a generic signal in a very straightforward manner by using variadic templates. Generic means: The signal class is written once and the template instantiation takes care of defining the different parameters and types that may be passed to the signal. In the traditional definition of the observer pattern, a

Tracking your mouse in games and editors

It is quite common to hide your mouse while the game is on the game window. Another common technique is restricting the mouse movement somehow inside the game window. Both of those techniques help with getting rid of the cursor while the player is playing the game Where is my mouse? It must be one of the most common issues in games/emulators/whatnot. The player needs to minimize the window to take care of some other task on her computer but the mouse remains hidden and/or trapped within an invisible rectangle, preventing the user to complete her action or even see what she is doing. This is a very frustrating problem and surprisingly, a lot of games get this wrong. Before I saw this problem in code, I assumed that this issue is taken care of automagically, however this is not the case. Let's consider the ShowCursor windows API function for a moment: Internally, the system is a reference counter: Each call of the function with a True argument will increase the counter and