Posts

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 ...