There are a lot of subtleties that you only pick up with experience. I was pleased to find a blog entry by a bloke Tom M on “Everything I wish I knew when learning C” which is well worth a read.
I’m not going to copy anything from it except for this little snippet below one below. For the rest, you’ll have to read his blog post.
char signedness
All other integer types default to signed, but bare char can be signed or unsigned, depending on the platform.
As said char is an integer type but unless you are doing stuff like ++ or — when its value can overflow according to being signed or unsigned, it’s not really a problem. And unless you are writing code that runs on wildly differing platforms, you can probably safely assume that a char is the same as a byte and has 8 bits. On those other platforms, I’d suggest you read the answers to this Stackoverflow question.
A reader asked me how to setup SDL2 for Windows given recent changes in SDL2. Specifically the files and libsdl projects have been moved from the libsdl.org website to GitHub. You can easily find SDL2 Image, Mixer, TTF etc.
However it can still be quite daunting setting up Visual Studio for SDL2. You have to download the specific files, then configure the project properties to specify the include paths for header files and then the lib files, both the path to them and identify the ones you want to use.
As I’m on my new PC, I bit the bullet and went through the process of setting it up. It took just over an hour to configure it. I’ve put it into a PDF that’s a couple of pages long.
So the game works but only after I disabled the sound code; it was failing in the call to Mix_OpenAudio(). I think recent work on the SDL Mixer needs some work on my part. I need to sit down and look at the SDL Mixer page and figure out what’s failing. Once that’s done, I’ll update the files.
My 2nd eBook is going to be a bit like the first one- first it teaches C but it is oriented towards Raspberry Pis. Then it shows how to program Asteroids (like the first book) followed by a Match three game and finally a Rogue type dungeon explorer game.
Now some rogue-likes stay true to the originals which were text based. Like this above from an article on Wikipedia. Now that’s ok, but I fancy something a bit more colourful so I’m going to use 16 x 16 pixel graphics.
I did a bit of searching and found these dungeon graphics on itch.io. I’ll use those to start with but may change. The view is not quite top down but top down with a bit of isometric in it. It looks better than pure top down.
So I figure the game has to have these elements.
(1) Generate a dungeon with multiple rooms and levels and stairs between,
(2) Fill the dungeon with monsters and treasures.
(3) Move the player’s character through the dungeon fighting monsters, picking up things including keys and maintaining a small inventory.
(4) Add some side quests like needing to find keys to open a chest with a magical item.
(5) I’ll also need to devise a simple combat system with magic.
Do monsters move- you’d hope so. How are rooms connected, in fact how are dungeons generated? I created a dungeon generator for the postal game Quest 30 years ago and can remember how I did it so there’ll will be that to implement in C.
I’m also going to leave out things like needing food and water.
So there it is now. Now to start writing the software. I’ll update this with progress reports.
No matter how much I use C there are still things I thought I knew but find out otherwise. The latest one is that you can use ++ or — on float and double variables.
Here for instance is a short nonsense program that I compiled and ran with Visual Studio on Windows. As you’#ll notice, it increments and decrements floats and a double.
The danger with using this is that you might get rounding errors. When I ran it on Windows, I didn’t which surprised me. I will have to try this on Linux.
#include <stdio.h>
void main()
{
float c = 1.0f;
float cin = 10000.0f;
double d = 2.0;
int i = 0;
while (cin != c)
{
i++;
cin = c;
c--;
d++;
}
printf("%i %e %e\n", i, c, d);
}
If you’re into C, one of the most interesting applications you can write is a text editor. It demands ability to use pointers for storing the text efficiently and command handling and doing things like searching, handling Unicode.
It can be equally instructive reading code someone else has written and this case Mle, is a text editor in less than 10,000 lines of C. It’s also cross-platform apparently, though you’ll have to build it on the relevant platform.
It uses three other open source libraries, linked at the foot of the main page. They are uthash, termbox2 and PCRE2.
I’m currently operating on a Linux laptop as the M2 SSD on my new desktop PC decided to stop working the other day. But I thought I’d mention a couple of utilities that I’ve recently started using.
My new PC, just three months old when working has a 1TB M2 SSD with Windows on it. Most software is installed here. All the data, backups, everything else is stored on a 10 TB Hard disk. This is my 10th PC since 1989 so it has the name PC10. Yes, original I know.
I have been copying everything I’ve created and written since 1989 and like a snowball rolling down a hill it has grown. When I finished copying everything from my old PC; a process that took a large chunk of four days, I’d used 2.3 TB out of the 10 TB hard disk. I just copied across the Gigabit network connection and it maxxed out at 113 MB/S. That’s even with anti-virus running. Of course Windows has an overhead so sometimes the transfer speed drops down to KB/S for small files. I worked it out that over the 48 hours (4 days- 12 hours a day), it copied at 13 MB/S average.
In future it might make sense to zip up folders with small files in them. In the meantime I need to do a bit of pruning of files, removing duplicates etc.
Everything
I’d been looking for a utility to let me find files quickly and somebody had suggested Everything. It’s brilliant. It tells me that I have just over 4 million files. I can filter on document types, images, music files or just search on matching names. And it is very very fast. If you want to find the massive file that’s eating up disk space or duplicates, it does it. But it’s only for Windows. Lacking a Windows system at the mo, I’ve just borrowed the image from the Everything home page.
Fsearch
Developer Christian Boxdörfer also liked Everything and decided to create a Unix clone of it which is FSearch. And it’s written in C. That’s it on the left.
I spent several years in the 1980s programming games.
I have a memory of 26 year old me sat hunched over a computer late at night back in 1985. I was working a 60-70 hour week as a partner in a games company. My current game was an American Civil War tactical wargame called Johnny Reb II. I was struggling with some ‘artificial intelligence’ code for the attackers (Confederate troops) to cross a bridge over a river. On the other side the defenders (Union) were trying to defend the bridge.
Artificial Intelligence in games is a completely different thing from ML and Data Science nowadays. Back then it was just a control algorithm for troops reacting to the presence of enemy troops and working out the best routes, targets to attack, whether to retreat and so on.
What made it worse was that the whole thing was written in 6502 assembly language (and later converted to Z80). Back then you had two choices: Basic which was to be honest slow and clunky for writing games or assembly language. If I was doing it now, without a moments hesitation I’d program it in C. But C compilers for 6502 didn’t exist back then.
The Problem with assembly language
The problem with assembly language is (a) it’s slow to write. You can write 10 lines of C in the same time as ten lines of assembly code. Those ten lines of C will do far more than ten lines of assembly code. In 6502 all you are doing is moving values between registers or register <> memory. Maybe add a number or increment one of the three available registers A, X or Y. These were all 8-bit registers so you couldn’t even index easily through 64-bit memory. To do 16-bit indexing you stored the 16-bit address in two successive page-0 locations (addresses 0-255) and then used Y as an 8-bit index. You could do the same in page 0- memory with the X register.
(b). It takes a lot of code to do anything in assembly language. You want floating point arithmetic in 6502? Take a look. I think Steve Wozniak wrote those for the Apple I/II. What we take for granted in languages like C# or Java or JavaScript is code for high level data structures like dictionaries. I’m sure it could be done but it takes a fair bit of programming. You don’t have those in assembly language; all you have to use is simple and not very long arrays.
In C# I wrote a program to read a 46 MB text file and produce a sorted count of all words in the file. It used a Dictionary, took me 30 minutes to write and it ran in 5 seconds. It would take weeks to do the same in assembler.
6502 Page 0 locations were valuable because they made your code both shorter and faster.
I wrote a cross-assembler for 6502 in Z80 as a way to learn Z80. Assemblers use labels (L20, L30, L31 etc. in the screenshot) and I needed a way to hold them efficiently in memory. I ended up with a 26 x 26 index table of 2 byte pointers. If you had a label ‘ROUTE’ then there would be a pointer to a chain at the location for [‘R’][‘O’]. Each entry in the chain was like this
1 byte length of rest of label (i.e. 3 for ‘UTE’) – 0 marks the end of the chain.
3 bytes to hold ‘UTE’.
2 byte address value
No need to hold the whole word as you know the first two letters. It also makes comparing a label against one in the table was faster because it only needed to match against len(label)-2 characters.
So the next value in the chain would start after that or be a 0 for the end of the chain. Yes most of the index table might be empty (all 26x26x2= 1352 bytes) but every label in a chain used 2 bytes less than the full label text. So with more than 676 labels you saved memory. Searching for a label was just a matter of walking a chain. Labels were just addresses; so a location could hold a value like a count. You’d identify it with a label and use that label in 6502 instructions. No variables in assembler; it’s all addresses…
With 6502 you need to do two passes to generate code. If you have a label in the first page of memory (0-255) then instructions are only two bytes long and are faster to execute than the three byte instructions. So on the first pass you don’t know if a LDA label will be 2 or 3 bytes long. After the first pass through though you do know now, so on the 2nd pass it can output the correct size instructions.
Programming in assembler means you have to write a lot of code and in the early days before I had a development machine that meant I had to save the source code to tape and compile it using a cartridge assembler. The CBM-64 could take cartridges and one of them stored assembly language in RAM just like Basic. If the game did something wrong then the CBM-64 would reset and you’d lose your source and have to reload it from the slow tape. Let’s hope you didn’t forget to save changes before you ran it. I spent a few hours gnashing my teeth over a persistent crash. I was calling a CLR routine when it should have been CLS! d’oh…
So a game back then might be 5,000 lines of code or longer. That’s quite a bit to hold in memory, given that you need space for the game machine code, sprites, graphics etc. as well. Plus it’s wasteful having to recompile the same code over and over again. My cross-assembler did 250 lines per second but divide that by two for the two passes.
So I split up long files into smaller ones and created a jump table at the start. There was no linker so the code was loaded into RAM at fixed addresses. If you had five subroutines in one file then there’d be five jumps at the start to the actual function. And the files that called those functions just had a block of five calls at the start.
That way you didn’t have to worry exactly where the function was located in RAM so long as that file was always loaded at the same address.
Switching to Development Machines
It got easier when we switched to development machines. The CBM-64 had a parallel port as did the development machine (Tatung Einstein-a CP/M computer) so a little bit of handler code in the CBM-64 set up the CIA chip to wait for data sent down the parallel cable and put the code directly in RAM. It took no time to load the handler from tape after a crash and then send down the whole file.
Modern CPUs do all sort of optimizing tricks and that’s even before you use vectorization. Compiler writers know how to generate code that uses these tricks but it would take quite a while to learn them so you could use them in hand-written assembly.
Conclusion
Writing in assembler in the 80s was easy to learn. Nowadays I wouldn’t know where to start- the Intel and AMD CPUs have a lot of different chips in their families so there are variations in what instructions are available. Oh and don’t forget there’s ARM CPUs as well.
Writing in C (or even C++) is a lot easier to get into and I very much doubt if you’d get any better performance in writing things in assembly. Also, it would take a lot longer.
Moving to a new PC can be a time consuming process. This is my 10th PC since 1989 and I’ve been accumulating files, old programs, websites, manuals etc. as I moved from PC to PC. My first PC had a 20 MB disk, the 2nd PC had a hard disk with 240 MB capacity. On my new PC it’s 10 TB and 1 TB SSD.
It’s another i7 but 11th Gen Intel(R) Core(TM) i7-11700K. Only 32 GB RAM for now as they didn’t have 64 GB in stock but the motherboard can go up to 128 GB.
I estimated that I had accumulated just over 2 TB of my stuff and once I got my new PC setup started the process of moving files across. 2 TB seems a lot but VMs under Hyper-V can easily eat up a few hundred GBs.
Both PCs had a gigabit network card and were plugged into a gigabit switch so what speed did it give me? 11 MB/S. That’s not gigabit speed which would be about 1000 MB/s but closer to 100 Mbs. Trouble was the switch was plugged into a router and I think that pulled things down. I borrowed a crossover cable. Setup both Pcs with static IP and connected them directly. That was much better and it was soon maxing out at 112 MBs.
Windows of course put its spoke in. If you are copying large files- anything over about 1 MB then it uses the full bandwidth but smaller files pull it down and I’d see as slow as KB/s speeds. So I’d have three or four files copying at the same time.
As well as just copying files to the new PC, I was cleaning up my old PC to sell it so I was either deleting stuff or moving it. Overall my total elapsed time was something like 28 hours over four days and evenings. Plus of course installing software (Visual Studio, VS Code, Android Studio, MS Office). It all takes time. Luckily I’ve been quite disciplined and kept all licences and serial numbers in an encrypted text file.
Then I discovered that my new PC’s video cards had different connectors. My old one had DVIs but this one I could only find an HDMI and 2 DisplayPorts. There was a DVI connector on the video card but the firm that did it put the card in the top slot and the DVI (2nd row) was covered up by a blanking plate! It’s a RTX 2060 card. I removed it and all was well.
Something had gone wrong and I couldn’t change the DNS because it was showing up in somebody else’s account. After I proved ownership of the domain by adding a TXT record they fixed it and I added the hosting name servers in.
Within a few minutes the domain was hitting the server and after I edited the A record to get rid of the parking, it correctly picked up my dummy place holder index.html.
Interestingly I could only add TXT/SPF records at 123-reg.co.uk and Google’s online dig tool (shown) couldn’t pick them up.
Hiding the VPS
I decided that I’d let the main web domain take the strain from hackers trying to access the VPS. So the web game will save orders into gamedomain.org/n/orders and get results from gamedomain.org/n/results for game n. (Not the real game domain!) But behind the scenes the VPS that runs the game processing code will pull orders from that orders folder. The VPS is located elsewhere, not even in the same hosting and after processing, it will copy the results back to the relevant results folder.
This solves a couple of problems.
Getting the orders in (and results out) requires a web interface. Because the game is based on the postal model, it’s quite feasible to keep the web interface and the processing separate.
Increased security. If you don’t know where the VPS is located, it’s somewhat harder to hack it!
I’m also experimenting with having a web command interface. Commands get picked up pulled to the VPS and executed. For example creating a new game, pulling in game processing logs and so on.
Back about ten years ago I gave a few lectures on what it took to setup a website. I’d ask the audience (who were not technical) for ideas, showed them how to search on a registrar, find a domain name, register it and it would be live usually within 15-30 minutes. Then I’d add some content and publish it. It worked every time.
So you’d think the process would be smooth as silk now. Think again. I registered a domain about three weeks ago. I’d had the .eu version for a game idea but of course being .eu and me not living in a EU country and so on so I decided not to renew it. However the .org of it was available and I registered that.
Last week I decided to configure hosting, buy a SSL certificate and start using it. Only when I added it to the server I rent (a reseller account), it didn’t show up on the domain page. That’s where you get the name servers from. Rather than faff around with A recs and MX records, I usually set up Name Servers to point to the hosting server. For that of course you need to know the name servers. But the domain was not on the GoDaddy DNS page so no name server informtion. The hassle factor was ramping up a bit quickish. Very odd.
I faffed around for a couple of days and got onto GoDaddy’s technical support. It turned out, well they pointed out that (a) the registrar (123-reg.co.uk) had put parking on it. and (b) there was something a bit weird with the DNS hosting. Weirdly, on Chrome, the page is blank which is why I’d not seen it. When I looked at it on iPhone sure enough there was a parking page with adverts.
So I got name servers from GoDaddy and set them on 123. I gave it two days and looked and no change. The name servers were set but The DNS page on their site did not say that it was being managed by external name servers. You get that on other domains; this one for instance.
Onto 123 support and to be fair to them, they sorted it pretty quickly. Also I found out that 123-reg.co.uk and GoDaddy are sister companies. You learn something every day. So I looked at the site and it was the same Parking page except it now said Parked on GoDaddy instead of Parked on 123-reg.co.uk. Me I find this a bit cheeky; I’m paying for registering a domain and hosting but those companies are making a little (maybe more than a little) by parking my domain. I can understand that they’ve probably got a lot of domains in limbo; alone and unhosted so why not park em. But it seems a bit cheeky just doing it without saying anything.
How long to Wait?
So back to GoDaddy support which isn’t always easy. You can do it online by a chat Window and at one point I got this gem. 371 Minutes! Normally its 4 or 5 minutes… I closed the browser window and came back 30 minutes later.
So the oddity I mentioned earlier; somehow, the domain was added for hosting against somebody else’s account. I can’t explain it, they can’t explain it. So they asked me to verify ownership by reverting the name servers to 123’s and adding a TXT record in the DNS with a certain name and value. Once they can see that I really do own it, they’ll change ownership of its hosting (not even the domain just the hosting!) to me.
So what used to take me 15-30 minutes is about nine days elapsed time. And it’s not finished yet. I really do think this domain is cursed.