Author: David

The joys of SSL certs

The joys of SSL certs

Certificate ass u meThere’s a saying “Don’t assume- it makes an ass of U and me“, and I er fell foul of this a month ago. A couple of months ago I setup a cheap VPS. It was one of those that you pay every month. What I didn’t realise was you are explicitly meant to renew the hosting every month and they send you an email with a link. Of course what did I do?, I er forgot to renew it. Annoyingly, I’d installed Virtualmin, redirected a domain and bought a cheap SSL certificate. All lost.

Now I actually did something right and there’s a lesson here. When you setup a SSL certificate, you create a CSR (Certificate Signing Request) and a Private key. You upload the CSR, pay your money (£20 for four years) and get a certificate back. The hosting companies I’ve used provide a SCR creation facility and somewhere to paste the private key and certificate when you get it. Then you click a button and your website now has a working SSL. It couldn’t be easier.

So luckily for me I had made a backup copy of my private key and was able to download the certificate. I’ve setup a completely new VPS, redirected the domain and very nervously pasted in the cert and private key. It worked. I had been bothered that the CSR was generated on a different server but it doesn’t seem to matter. So long as you have the private key the certificate works on a different server.

 

Drawing  dungeon rooms using characters

Drawing dungeon rooms using characters

Some roguelike roomsThe final game will use graphics but those graphics will be based on characters, so I’ve started off by drawing a room or two using the provided extended ASCII characters.

Here for example is an 8 x 8 cell room with four inner columns and four possible doors. I’ve used spaces in one and full stops in the other to see which looks better. I think I prefer full stops as you can count them but it’s not a big difference.

╔════╬═╗  ╔════╬═╗  /----\
║      ║  ║......║  |....|
║ ╬  ╬ ║  ║.╬..╬.║  #....|
╬      ╬  ╬......╬  |....|
║      ║  ║......║  \.||./
║ ╬  ╬ ║  ║.╬..╬.║
║      ║  ║......║
╚═╬════╝  ╚═╬════╝

The 3rd room is smaller and roughly octagonal though not as nice looking. I used the normal ASCII characters for that with the two slashes ( / and \ ) as well as # for a vertical wall door and two | for a double door. Here’s a corridor that goes round two corners and then meets another one.

═══     ╔════╦══
   ╚════╝    ║

There are plenty of ASCII character charts and I used this one which describes all the graphical chars making it easy, albeit a bit tedious to draw these in any text editor. There are single box characters as well as the double ones I’ve used. The ╬ character has proved very versatile as it provides not only columns but doors in both horizontal and vertical walls.

It’s noticeable that despite this room being square, it looks rectangular thanks to characters being taller than wide. Also in WordPress, the gaps between lines are more noticeable as gaps whereas in Notepad++ (the editor I used to create these) as shown in the image at the top, these gaps are absent.

How to make a game with QR codes

How to make a game with QR codes

Example.com QR codeThis follows on from yesterday’s post about creating QR Codes. How about 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 example.com/r/h65tr321.  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 example.com/r/h65tr321a6. (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 simpleqr.xyz. 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 simpleqr.xyz.

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

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

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