A curious consequence of passing undefined pointer to printf

Late in the night I was writing something for SpelunkyDS. Mistakenly, I passed a pointer to printf, that was uninitialised, which definition looked like that:

Screenshot from 2018-11-18 12-10-32

Obviously, the held_sprite_width had undefined value. It could point to anything, if not set to nullptr. In get_sprite_width, all I called was:

Screenshot from 2018-11-18 12-16-46

What would it print? No, not just some rubbish, as you would expect.
It printed a “FAKE SKELETON”.

Screenshot from 2018-11-18 12-18-44.png

But hang on, why would it print a “FAKE SKELETON” anyway?
At first, I thought, that the pointer was just pointing to a const char literal that the FakeSkeleton class uses (that class is totally unrelated to the code above, it just happened that the pointer happened to be pointing somewhere in the FakeSkeleton’s memory area). Here’s some code of the FakeSkeleton class:

Screenshot from 2018-11-18 12-26-03

…but after editing the function I was sure that the printf didn’t simply use the literal “FAKE_SKELETON\n”, it called the FakeSkeleton::print_typename_newline as a whole!

I edited the function a bit:

Screenshot from 2018-11-18 12-36-13

Which caused:

Screenshot from 2018-11-18 12-36-19

Well, It could also be a compile-time optimization, where the printf(“FAKE_SKELETON%i\n”, 666) would be substituted with puts(“FAKE_SEKELETON666\n”), but I’m not sure of that.

If I set the width pointer to nullptr before printf, the whole effect would vanish and zeroes would be printed.

Links #1

Those articles/posts got me interested lately.
Most of them are about optimizing C++ code.

Data alignment in terms of performance


The clang compiler has a -Wpadded option that causes it to generate messages about alignment holes and padding. Some versions also have an undocumented -fdump-record-layouts option that yields more information.

Dynamic & Static inheritance in terms of performance:


Another problem with this approach is the use of virtual functions. We have virtual functions calling virtual functions when we are trying to something relatively simple! It should be noted that the compiler can not generally inline virtual functions and there is some overhead in calling a virtual function compared to calling a non-virtual function. This runtime hit seems unreasonable, but how can we overcome it?



Object Oriented Programming pitfalls:


Programming patterns:


C++ coding principles:




Having a look at the sources of Haven & Hearth MMO client (I)


If you’ve never heard of the H&H, here’s a snippet from their page:

Haven & Hearth is a MMORPG (Massive Multiplayer Online Roleplaying Game) set in a fictional world loosely inspired by Slavic and Germanic myth and legend. The game sets itself apart from other games in the genre in its aim to provide players with an interactive, affectable and mutable game world, which can be permanently and fundamentally changed and affected through actions undertaken by the players. Our fundamental goal with Haven & Hearth is to create a game in which player choices have permanent and/or lasting effects and, thus, providing said players with a meaningful and fun gaming experience.

But what’s special in my opinion is:

  • it’s developed by a team of two Swedes
  • it’s developed in Java using JOGL
  • client’s code is open and the game itself is free, which means there are many alternative clients today.

You can find original sources’ license here:


Following by the link to their official git repository and notes from developers.
However, the source I will be dealing with will come from the ‘Amber’ client:


It provides some additional functionalities and is regularly updated:


Downloading and building from sources

>> git clone https://github.com/romovs/amber.git 
>> cd amber-1.68.0
>> ant

At this moment some weird errors may occure, but if so, just type ant once more and it should build it successfully. Now, to run, type:

>> cd build/
>> java -jar hafen.jar -U http://game.havenandhearth.com/hres/ game.havenandhearth.com

You’ll end up with a menu screen. However, (at least) to me it wasn’t over, because after logging in an error popped op:

Screenshot from 2018-10-06 13-42-35

I got to the file on the top of the stacktrace (Buff.java), found the line, had a guess what’s wrong and corrected the thing from this:

Screenshot from 2018-10-06 13-43-20To this:Screenshot from 2018-10-06 13-43-28

Then build the whole thing once again with ant and I finally managed to log in.

My brief modifications

Camera zoom

So I proceeded through the code for some time after that. I’ve been thinking what would be easy to do and came with an idea of simply enabling maximum camera zoom for a starter. It proved to be easy.

Cameras are managed in the MapView.java,
there’s a chfield function there, which I modified in the following way:

Screenshot from 2018-10-06 14-20-54

Btw, these comments are mine. I hardly ever stumbled on an existing comment, but if so, they’re mostly some rants/hacks over Java. Anyways, I rebuilt the project and got the camera scrolled up to the orbit with arrow buttons – it worked. I could see the whole area of the map around the player:

Screenshot from 2018-10-06 14-18-51

Detaching camera from the player

The following idea was:

  • Space bar would toggle detaching
  • If detached, one could move the camera to the point on the map, by simply click on some place at the map, but the player still wouldn’t move there
  • Space bar pressed once again would attach camera to the player and focus on it

As I said, cameras are managed in the MapView.java.
There’s a function that returns object which defines the player:

Screenshot from 2018-10-06 14-47-27

And a function, that basing on that, returns player’s current coordinates:

Screenshot from 2018-10-06 14-48-16

…which I guessed, is used also by the camera, so I created an object that cached player’s position and updated it when:

  • detached mode is on
  • left click on map occurs

So getcc looks like this now:

Screenshot from 2018-10-06 14-56-15
I injected some of my code into the existing function that handles clicking, that is ‘hit’ function. Parts of it, before editing, looks like this:Screenshot from 2018-10-06 14-58-17And after adding my code, it starts with:Screenshot from 2018-10-06 14-59-19To toggle detaching mode, I edited the ‘keydown’ function, which starts with:Screenshot from 2018-10-06 15-00-37I just added an additional branch to the if-else tree:Screenshot from 2018-10-06 15-01-57

That’s all. I rebuilt it and recorded my modifications that you could watch it:


Next time we will tackle the networking code (which fun parts I already found, reside in the Session.java).

My article in this month’s “Programista” magazine

It covers basics of making homebrew for the Nintendo DS (using C++ and devkitPro’s libnds). There are 3 examples:

  • One that makes NDS a wireless controller for the PC
  • One that makes NDS a wireless microphone recording station
  • One that is simply a Pong game

These examples’ code is on my Github:

If that made you interested,  look for the magazine in the “Empik” stores or buy a PDF on the “Programista” webpage.

Recompiling Ubuntu clock to display in hex

Recently I was thinking what would be a good idea to enforce learning fast
hex<->decimal calculation in my head. Obviously, I thought about reading current time in hex, but there is no checkbox for that in the vanilla ubuntu indicator-datetime service (silly Canonical, not including hex time option).

Why not compile a version, that supports that, though? Here’s my modified version:

Screenshot from 2018-09-03 09-05-14
Hex hour to decimal: 09:05:14

And here’s how I did this:

I Download sources

My Ubuntu is 16.04 LTS, which is important because there are different sources for each major version. Anyway, I found sources here:

>> https://launchpad.net/ubuntu/+source/indicator-datetime

This link in particular provides sources for Ubuntu 15 / 16:

>> https://launchpad.net/ubuntu/+source/indicator-datetime/15.10+16.04.20160406-0ubuntu1

II Download dependencies

<in unzipped source folder>
>> mkdir build && cd build
>> cmake ..

My computer lacked certain packages, as I recognized when reading cmake’s log.
What I did in this case was googling “ubuntu package <package name>”, which moved me to the packages.ubuntu, i.e for libecall I found this package:

>> https://packages.ubuntu.com/xenial/libecal1.2-dev

so I typed

>> sudo apt-get install libecall1.2-dev in the terminal.

I did this for every missing package untill cmake returned successfully.

III Modify sources

open the formatter.cpp at void update_header() and modify the function so it would look like:

Screenshot from 2018-09-03 10-12-37

IV Compile

<in build folder> 
>> make

V Stop the current indicator-datetime.service, run our own for testing

I recommend you copy the existing indicator-datetime-service, so you could recover if you change your mind.

Screenshot from 2018-09-03 10-35-59

VI Reboot so the deamon would run our service

PS: I am not affiliated with Ubuntu nor Canonical, it’s just tinkering with their GNU sources.

How to fix Minecraft client attempt to authenticate server while playing on LAN with Linux’s network namespaces

So I recently stumbled upon a problem provoked by a random craving of launching a VPN-LAN party with my friends. The game we chose is called Minecraft, and it suggests, that one can easily host a game by launching a singleplayer game and then clicking the “Open to LAN” button. Thing is, it doesn’t work that well on VPN’s.

We chose to connect by a Hamachi network, I was the one who hosted the server. I did it by the means I described in the last paragraph, and – to my sheer surprise – no one could connect, everyone was being kicked from the server with a mysterious prompt of:

mc_error_authFailed to log in: The authentication servers are currently down for maintenance.

That’s weird, isn’t it? But anyway, my idea was – let’s just launch Minecraft with access only to the VPN network, not to the internet itself – maybe there will be no authentication attempts if it detects no Internet?

I proceeded to the scripting. Scripting, because on a Linux, one can run a process in a namespace, where it’s going to be sandboxed only to those network interfaces one selects.

Let’s list my interfaces then:


As you see, there’s a ham0 VPN interface that pops out after installing Hamachi, and wlp3s0 which provides me the Internet via WIFI. Now, as we know this, we need to run a minecraft-launcher in a namespace with ham0 only:


It does what it suggests in the echo’s. After running it I exclaimed SUCCESS!
Minecraft successfuly ran in the offline mode:


That’s great, but now it’s the worst part – my friends use Windows and I had no idea of scripting the same thing on it. I had luck, though – after googling I found out that someone wrote a small program that does exactly what my script did, but on Windows – it’s called ForceBindIP, and as it suggests, it binds a specific network interface / port to the process:


After installing, one can call Minecraft binded to its Hamachi IP by calling in cmd:

"C:\Program Files (x86)\ForceBindIP\ForceBindIP.exe" 25.60.2 33.140 "D:\Data\Minecraft\MinecraftLauncher.exe"

Where the first part is the place where ForceBindIP is installed, the second part is Hamachi IP and the third part is the Minecraft Launcher installation directory.

That’s the moment when we tried again to play. I hosted the game with a launcher I started with my script, my friend tried to connect via ForceBindIP and….


It worked!
And yes, I know that one can download only a *server* and it’s probably going to work – but if that so, why the Open to lan button anyway?




Polski przekład poradnika pisania homebrew na NintendoDS

Stworzyłem przekład (polecam sprawdzić różnicę między przekładem a tłumaczeniem!) świetnego tutoriala który można znaleźć pod adresem:


O tym że ten przekład był robiony z głową świadczy to, że sam piszę własne homebrew – moje SpelunkyDS są już całkiem rozbudowaną grą i wiem co tłumaczę. Prawdopodobnie wiem co tłumaczę

Mój jedyny komentarz do tego przekładu – początkowe rozdziały, w których mowa o pierwszych kartach do wrzucania homebrew na NDS można pominąć –  wspomniane tam narzędzia to już starocie i w tym momencie zdaje się że jedyną wartą uwagi kartą jest R4. Od rozdziału 4 zaczyna się pisanie kodu.

Poradnik NDS Homebrew

libnds – setting icon for .nds build


  1. Open gimp
  2. Create new 32 px / 32px image, paste there your icon (you may need to rescale it with Tools->Transform tools -> scale) or draw whatever you want. You may want to open a toolbox with Ctrl + B.
  3. Set image to use indexed pallette with maximum of 16 colors
  4. first colour of bitmap would be transparent when rendered on NDS menu, so set it as first in “colors->map->rearrange map”
  5. Save in .bmp format BUT you MUST check the “compatibility options” -> “do not write color space information”
  6. In your projects’ Makefile fill GAME_ICON property with path to your icon
  7. Build your nds project, put it on R4 or something else and enjoy


  1. Srsly
  2. File -> New, then in window which appears set width / height. No need to change anything in “Advanced options”.

Screenshot from 2018-05-29 21-21-03

Drawing something ugly:Screenshot from 2018-05-29 21-28-39

3. Image -> Mode -> Indexed. Set max colours to 16. Again, no need to change anything else.

Screenshot from 2018-05-29 21-30-09

4. As I said, choose whatever color that would be unique (you don’t use it in your icon to draw something) – it will stand for the transparence layer. I have arbitrarily choosen this red-pinkish color. Now you have to arrange it to be the first color in the icon palette.

Do this by Colors -> Map -> Rearrange colors map.

Just drag and drop your color to the first place:

Screenshot from 2018-05-29 21-34-32

5. Ctrl + E, name it as something.bmp, click Export,

Screenshot from 2018-05-29 21-37-33

Click Export again.


Screenshot from 2018-05-29 21-40-54

That’s it, finished, you can now build your .nds file with custom icon.


Some words on developing SpelunkyDS

I had to remove music from the .nds binaries, you see, the binary as it is, with only sprites packed into it, is ~600kB. With simple sounds like one for the whip or jumping ~1.2MB. Things get hard when music comes in, it is over ~3.3MB with main menu / title song which causes crashing moonshell and prompting an “too large binary file” error.

You would suggest compressing the .wav more, but the compression is already in its really limits, when it comes to decent sound quality.

I wrote a prototype of SpelunkyDS using nitrofs – it streams music from the R4 card, not from the binary itself. Works good enough in desmume, but when copying to the card and trying in moonshell, it can’t find a filesystem. Problem is, to use nitrofs is to use a specific firmware, which is nds-hb-menu.

It would render SpelunkyDS only usable on cards prepared explicitly for homebrew – sad, but that’s the cost of using so fatty assets like music.

But for now – I’ll just focus on coding another things and leave the music for some time, perhaps there’s a better solution.

Also, as I have recently seen, there’s not so many tutorials on homebrew things like putting an icon to NDS file or aforementioned streaming music from filesystem (with nitrofs).

Going to put some posts on these later.