Month: November 2020

How to make a game with QR codes

How to make a game with QR codes QR codeThis follows on from yesterday’s post about creating QR Codes. How abut creating a web game similar to the Choose Your own Adventure type games but with a difference? I did think about implementing this as a proof of concept and may yet still but ideas are worthless until executed so I’m happy to put this out there. Here are a few notes on a proposed web QR game.

  1. You can get open source QR generators for PHP, C#, probably most programming languages.
  2. The web pages show one or more QR codes with a title on them. These are actions and by opening the URL (say in a QR reading mobile app) the action applies. Eg Go West into a room, open a chest etc.
  3. If you use an 8 character parameter to the URL that gives you something like 2.8 trillion different destinations. eg  The idea is that the web application generates a page from this parameter. If the parameter has been spoofed then you fall into a bottomless cavern, get eaten by an Ice Worm etc and start again. These parameters are generated anew for each game.
  4. You can add extra information such as carrying items with extra characters. Unlike web games, there only way to pass information is via the url. So the URL might now be (the QR code shown is for this url) The page would still be created from h65tr321 but the a6 might mean you are carrying say a sword and key. If you add on a character to the query for each item, you can have 36 different types of item using a-z and 0-9.
  5. The whole adventure is created as a list of rooms, each with a description. A random eight character code is generated for each room. That’s where the h65tr321 comes from. At the start this list of rooms is read into the web application.
  6. Puzzles are of the blocker type. To open a door that lets you escape you need to be carrying a key but that’s in a chest protected by a Dragon. You can fight the Dragon but only if you have the sword and the Dragon will always kill you unless you are carrying a potion of extra hit points. So first you have to explore the rooms, going from room to room and picking up items. But you can only carry three items at a time. You can either use or drop an item which stays in the room where you dropped it.
  7. Every room you enter when carrying items displays each items with a use and a drop QR code.
  8. Other ideas include a time element that reduces each time you move rooms/do an action. You only get so long to escape but a potion that can be used once adds more time.

As you can use any app to scan a QR code and jump to the url, it would be a bit tedious to play but might make for an educational project for teenagers or schoolkids.

It should be possible to build an app might be able to detect the QR codes automatically and display just the link text. Again like QR generators code there are open source packages for reading QR codes.



Such simplicity- online QR generator

Such simplicity- online QR generator

QR code for Learncgames.comSometimes you come across a design that is sheer simplicity, could not be easier to use and it just works. That QR code you will not be surprised takes you to this very website!

The website that I got it from is Just paste or type in your URL and a QR code appears before your eyes.

This is slightly off-topic for this site and no its not an advert – just a recommendation of a useful website. The thing is QR codes are quite complex (understatement).  This is quite a nice visual description.

This however is a very detailed explanation of how to create a QR code in 10 steps and I wouldn’t be surprised if its the method used in

I remember seeing this explanation a few months ago and no I don’t have a perfect memory. Instead, many of the websites I’ve found are through hacker news. Even better though is this search engine that searches hacker news. I used it to find that QR website.

There are innumerable free QR apps on smartphones so good ahead and verify the QR code!

There’s an elegance about QR codes though it’s not exactly steganography (hiding data or images inside something else) but the fact that you can’t understand it without an app is quite clever. I had an idea once of making a Choose your own Adventure EBook using QR Codes to navigate around a website.

Text and URLs

QR codes can hold a lot of information. Would you believe it if I told you that the QR below has all this blog text (about 230 words) up to the Text and URLs title at the start of this paragraph? Try it!

Also I compressed the PNG file but it still works perfectly. Here’s a screenshot of the QR app that scanned it.

Blog entry QR code

iPhone QR Scan




A first-person-shooter in 32KB

A first-person-shooter in 32KB

Anarch FPSDon’t expect this to be Call of Duty standard but then those games typically have a 50GB or higher footprint on disk. Anarch comes with 10 levels, 6 weapons, 7 enemy types and 3 ammo types and runs in 200 KB of disk space and runs in 32KB RAM. It’s 100% C but doesn’t use any FPU, GPU or file I/O. It’s old skool 90s style.

If you’ve played original Doom you’ll have an idea what it’s like.

Interestingly, the creator has put everything, source code, graphics etc into the public domain which is a very philanthropic gesture. So if you want to understand how to write a FPS (first-person-shooter) in C, take a look. You can even play it in a browser.

I’ve added this to the C Code links page (on the top menu).


Back on the Raspberry Pi

Back on the Raspberry Pi

Pi Asteroids DevelopmentIt has been a few months since I last used it and as you’d expect, it took a little bit of time and effort to get things back to what they were.

I’m pretty good about backing things up and it took about 30 minutes to burn a new SD Card, update it, install VS Code and the C/C++ extension, then copy my asteroids version over. I use WinSCP so had to enable SSH on the PI. It’s disabled by default but just tick a checkbox on the interface tab of the Preferences->Configuration menu.

Even then it didn’t compile. Of course I had to reinstall the dev versions of SDL, SDL_image, SDL_mixer and SDL_ttf and the clang compiler. Still it didn’t compile. I had created a Projects folder and created an Asteroids folder underneath that. I also had the Vs Code JSON files that you need for compiling C/C++. the main one of which is tasks.json. Those were in a folder .vscode which I had backed up but I’d copied it over into the wrong location. You want it located inside your VS Code folder.

This makes sense. If you have say five different projects then you are going to have a different build, link stuff per project. So you’ll have a unique .vscode in each folder. When you want to switch projects, you just close the Folder in the VS Code File menu and open it in the folder for the project that you next want to work on.

Mind you it still wouldn’t compile. It turned out my tasks.json has clang-6 in it. When I did a clang –version on it, it told me it was clang 7.0.1. So I upped it to clang-7 in tasks.json and that fixed it. It all compiled and ran.

Once you’ve done this a time or two it becomes 2nd nature but I can understand novices frustration; there are a lot of moving parts that all have to be right before you can even write and run C code. It’s not like other programming languages are really any better though. If you have setup virtual environments in Python and installed Python modules, you’ll know what it can be like.


How to move a SDL project from Windows to Linux

How to move a SDL project from Windows to Linux

SDL Logo
The SDL Logo is from

It’s not actually that hard to do, there’s just a few things that are different between C and Linux, specifically between MSVC and GCC /Clang.. I did this when I wrote the Asteroids game in the book.

Originally I developed it on Windows then moved it to Linux (Ubuntu) and then I added the bits to have it work on a Raspberry Pi. It worked on the Pi more or less as is on Ubuntu but I added support for game pad, detecting that it was on a Pi and displaying the temperature.

Here’s what I’ve found is different between MSVC and GCC/Clang.

  1. The safe string functions. MSVC has the _s functions so instead of strcpy, there’s strcpy_s on MSVC. This doesn’t exists in GCC/Clang but there are similar functions with an n in the middle e.g. strncpy. In future I’ll create a macro for each function that uses the appropriate type so the compiler will pick the correct type.   However it seems even strncpy may not be all that safe. If there is no \0 in the n characters then the copied string won’t have a terminating \0 and thus could still blow up.  This article says that in order of safety it goes like this with strcpy least safe and strlcpy the most.
    strcpy < strncpy < snprintf < strlcpy

    so maybe I should be using strlcpy instead.

  2. The include path for SDL in GCC/Clang is “SDL\SDL.h” not “SDL.h” as it is in MSVC. Again this could be fixed with a macro prefix for the SDL path so all #include works correctly on either system.

3.  I found that the file type was wrong. This wasn’t just a matter of CR/LF versus LF which you get between Windows and Linux (CR = Carriage return, LF = Line feed). Somehow the Windows file had a different UTF encoding type to what GCC/Clang expected and the compiler did not like it. However Visual Studio Code shows you what encoding it is (on the bottom of the edit window) and lets you change it, so no harm done. You may need to do this once on each file you’ve moved from Windows to Linux.

4. I found that the time header file in Linux needed a bit of work to make it compile. The standard for this dictates that differences aren’t done to the header file but by adding in additional headers.

So I’ll look into strlcpy.




Loads of C Algorithms

Loads of C Algorithms

C algorithms
From C Algorithms

While it’s fun writing your own code when it comes to things like sort or graph algorithms, common algorithms, you can save a lot of time, grief, debugging by using code that someone else has created.

Today I came across an algorithms website with algorithm source code for 14 programming languages including C, C++, Java, Python and go.

The C site alone has over 150 algorithms, mind you the Python and C++ have over 600!. Each of the ones I’ve looked at links to a YouTube video (not always about a C implementation but about the topic) and has a source code listing.

Overall its a very useful resource, has a memorable url (algorithm examples) and I’;ve added a permanent link on the C Code utilities page.

How slow is SDL_TTF 2.0?

How slow is SDL_TTF 2.0?

FWhen  I created the Asteroids game, I deliberately didn’t use SDL_TTF instead I took a Monospaced font and saved it out as a PNG file which was loaded into a SDL Texture. I then created my own character printing routines by figuring out which character I wanted and then blitting it. The image shows the font I used zoomed in.

You probably can’t get much faster than that, although I wasn’t really doing that much output. Every frame, the score is output and if an asteroid is hit, the value of the hit scrolls upward for a few frames.

Also when you lost a life, it would print using a scaled up version of the font. The only problem with that is scaling up a bit map font just shows off the deficiencies of the font scaling as a bitmap, as the image shows. Not exactly smooth is it?

So I’ve decided to write a small program that uses SDL_TTF 2.0 and does high resolution timing to determine exactly how long it takes to draw text using a TTF font. The big advantage of doing that in a game is you can draw different sizes, weight (bold etc) and colour compared to a bitmap font.

But TTF is an interesting format, there’s a lot going on. Letters are drawn using mathematical equations so it is bound to be a bit slower than pure blitting and I’m interested in knowing just how long it takes. It’s all relative, if you view a page of text in MS Word (or just in Windows generally), Windows renders it pretty fast.  But its still important to know just how fast. For what I used text for in Asteroids, it probably could have used TTF text but in another game with more text it might be too slow for 60 frames per second.

If it was too slow then perhaps a hybrid approach might work. Figure out what text you’ll need, prerender it into bitmaps (when the program starts up) then use those bitmaps.

So I’ll publish the speed test program once written. Watch this space.


New C tutorial on implementing linked lists with pointers

New C tutorial on implementing linked lists with pointers

Image by Денис Марчук from Pixabay

Once you leave the relative safety of arrays and structs, the linked list using pointers is probably the next thing to consider.  Technically it’s a liked list of structs containing pointers. It’s like an array of structs only instead of allocating contiguous memory for an array, you allocate memory for each struct as you need it.

Linked lists are easy to program. You have a head pointer (a pointer to the head of the list). It starts as null as the list is empty. There’s two types of linked lists (single and double). A single list has a pointer to the next node (or is 0 at the end of the list) and can only be processed from head to end. A node is just a fancy name for the struct in the linked list.

A double list has two pointers. One to the previous node and one to the next. As well as a head pointer you need a tail pointer and you can process the list from head to tail or tail to head (i.e. backwards).

Now the first operation you can do on a linked list is add a node to it.

To do this you

  1. Allocate memory for the node using malloc.
  2.  Copy the head pointer to the node’s next pointer.
  3. Stitch the node in by pointing the head pointer to this node.

So when you are building a list you add each node to the head, in a sense pushing it in front of the others.

Double linked lists have to do an additional operation which is set the next node’s previous pointer to point to the newly added pointer. And when you add the first node to a double linked list, you have to set the tail pointer to point to this first node. After that it never changes unless you have an Append node at the end.

Uses of Linked Lists

Anything that needs dynamic memory for example a text editor might use a double linked list to store all the text. Each line could be a different length. So each node would not only have a pointer to the next and previous nodes, it would have a pointer to the text in memory. When you insert a new line, you are just inserting a new node in the list at the current node that the cursor is on.

Or you might store a directed graph (a bit like in the picture) where each node has multiple pointers to other nodes.  Anyway I’ve published tutorial eight which looks at pointers and linked lists.


C++ Template Metaprogramming Game

C++ Template Metaprogramming Game

Image by OpenClipart-Vectors from Pixabay

Templates in C++ are a useful feature. Without them you’d not have template functions, or more usefully template classes like vector etc.  But there is an even more useful feature called template metaprogramming. It’s a very advanced and clever feature; one I have never done and I don’t think anyone could say they’ve mastered C++ unless they are good at it.

Here’s an example of the simplest example I could find. It comes from here and what it does is generate factorials of numbers at compile time. So when you run it it comes back with the answer immediately.

// factorial.cpp

#include <iostream>

template <int N>                                                                 // (2)
struct Factorial{
    static int const value = N * Factorial<N-1>::value;

template <>                                                                      // (3)
struct Factorial<1>{
    static int const value = 1;

int main(){
    std::cout << std::endl;
    std::cout << "Factorial<5>::value: " << Factorial<5>::value << std::endl;    // (1)
    std::cout << "Factorial<10>::value: " << Factorial<10>::value << std::endl;
    std::cout << std::endl;


But if you think that is clever how about a game where every time you compile it, it makes a move and remembers the move between turns? A developer called Matt Bierner has developed a snake game using template metaprogramming.

So I downloaded it into my Ubuntu, installed Clang and clang tool just for good measure and compiled. This is the output. Iv’e snipped a lot out after the first two. Ot’s very clever, in this case, not much use but I doubt if there is any other programming language in which you could do this. The compile plays the game, running it just outputs the results. Yes it’s not exactly practical but still…

david@davidvm:~/STT-C-Compile-Time-Snake-master/stt-snake$ clang++ -std=c++1y main.cpp -o snake ; ./snake
-- You Are Dead --
A blog post worth reading

A blog post worth reading

Letter C
Image by Clker-Free-Vector-Images from Pixabay

A developer called Jakub “Jorengarenar” Łukasiewicz has posted a blog entry Best aspects of C language and I’m more than happy to link to it. It says much of what I think about C but expressed very nicely and its well worth the read.

In his post he refers to a document Rationale for International Standard Programming Languages C (PDF).  This is a 2003 document (C doesn’t change very often)  and not exactly a light read at a trifle over 200 pages long. It’s an insight and commentary into what C99 is about and also lists these five principles:

  • Trust the programmer.
  • Don’t prevent the programmer from doing what needs to be done.
  • Keep the language small and simple.
  • Provide only one way to do an operation.
  • Make it fast, even if it is not guaranteed to be portable.

If you are learning C this will provide you with explanations of why some things in C99 are what thy are. Like myself you will discover things that you didn’t know. For instance I didn’t know about long double. Or that in K & R C(Kernighan and Ritchie) that all floating point arithmetic was done at double precision but that was relaxed in C89.