Alternative Version









March 15, 2023

Klang: The Legend of Zelda - A Link to the Past


Zelda III






This is the third blog post about my project called Klang and this one will focus on new keywords.

As usual, I will use a real case example which is Zelda 3. A very nice repository where some people ported this game from SNES to PC and Switch.

The code base is in C but it works fine for this case since the new keywords are simple base logic.


Loop






if you are familiar with C or C++, you will recognize those patterns below:

while(true) {
  ...
}

for (;;) {
  ...
}

do
{
  ...
} while(1);

There are unconditional loops and you can expect to have at least one of these unconditional loops inside a real time application usually (e.g. games, video player).

I added a new keyword called "loop" for Klang which is a dedicated unconditional loop:

loop {
  ...
};

For the Zelda 3 repo, there are 43 for (;;) in 14 files. So I replaced that by the new keyword "loop":


































Those kinds of loops are dangerous if you don't add a "break" or "return" keyword since it will loop forever.

Random Fact: I tried an empty while(true); a long time ago in the World of Warcraft game script using Lua. Result: the game crashes quickly :P

I also added a protection inside Klang where it will automatically detect missing "break" or "return" inside the loop and generate an error:





The example below illustrates an interesting distinction between a "while" loop and a "do/while" loop.





elif






The new keyword is inspired from the python language: elif which is the contraction of else if.

Below are some examples from the Zelda 3 repository that I updated with the new keyword:























Result



Here's a video with a new build that includes the new keywords loop and elif. I pressed "tab" multiple times to accelerate the intro.

I spent around an hour playing this build, which happens to be the first game I ever got for my SNES, if we don't count Super Mario World (since it came with the console).

While this is one of the best Zelda games I've played, my personal favorite is still Zelda 2. I remember playing it extensively during a short trip to NYC last December on the console pictured below:





Thanks again, Dave, for the gift!
Thanks for reading,

JS.