Learn C Games

Learn C Games

Learn C Games Programming Book coverThis blog is about C and Games programming (in C mainly) and now C# on mobile with MonoGame. It’s written by David Bolton, author of the Learn C Games Programming for beginners EBook. This is the Windows version, with a Raspberry Pi/Linux one due out in September 2020.  

The first 20 chapters introduce and teach C programming with many examples. This link is to an .mp4 of the asteroids game from the book. It’s about 90 seconds long and demonstrates all of the features of the game. High score table, rotating asteroids (four sizes), sound, explosions, ship hyper-jump and shields.

The remaining 30 chapters builds up to full source code, about 2,000 lines, in 13 stages and I explain how each feature works and is implemented.  All of the book’s source code is on GithubMore about me. Buy it on Amazon(UK), Amazon(US).

 

Quake II – still a favourite

Quake II – still a favourite

Quake II screenshotOh I do play much newer games such as Far Cry 5 which I’ve completed recently but Quake II was a favourite of mine back in 1997 when I bought it. I’m more of a Quake than a Doom person. Unfortunately for me, my then PC couldn’t run it. It was only two years old as well.  But I got a new PC in 1998 and that played it just fine. I’m not saying I’m a saddo but I can play it through on the hardest level without losing a life.

The reason I mention it because of an article I came across that described the different ID Tech Game engines and the games made with each engine. All have been open sourced (Up to ID Tech 4). Quake II was written using the ID Tech 2 engine itself written in C and assembler.

For some reason the link to Quake II source code is wrong in that article but you can find it on GitHub. In one of 18 of ID Tech’s 18 repositories there.

If you are interested in downloading and trying to understand the Quake II source code, I strongly recommend you read Fabien Sangard’s walkthough of the code.

How to measure the area of a circle

How to measure the area of a circle

Odd ShapesWell if you remember maths (or math for US readers), you’ll know that the area of a circle is pi X r X r where r is the radius and pi is that 3.14159 number.

But what if you are given a weird shape instead of a circle? Like some of those in the picture.

There’s a method called Monte Carlo because of the casino there. In effect you simulate throwing darts at the shape (say printed out and stuck on the wall). You throw thousands or millions of darts and count how many hit the particular shape and how many darts were thrown in total. You only count those that hit the rectangular paper; those that miss it don’t count.

Then you divide the number of hits by the number thrown, multiply it by the width and height of the picture (which is rectangular or square) and that is a close approximation to the area. In more advanced ,maths/math, this method is sometimes used to calculate the integral of a function. The integral is the area under the function. Some complicated functions are particularly hard to integrate, so using a Monte Carlo method can solve it for you.

I’ll publish a C program to run Monte Carlo on an image in a day or two.

An alternative way of measuring the area is by using recursion. I discussed it in this article Using recursive fill to count maps.

C11 and C17 support in MSVC

C11 and C17 support in MSVC

 

The letter C
Image by Peggy und Marco Lachmann-Anke from Pixabay

Microsoft have announced that they will be supporting both C11 and C17 in Visual Studio 2019 version 16.8 Preview 3. All the required features but not optional features and not VLAs (Variable Length Arrays) which is considered unsafe.

One of the complaints has been that Microsoft always prioritised C++ over C for many years. C was supported inasmuch as it was needed for C++. Until fairly recently C++ was a superset of C and you could compile C program as C++. Just change the extension to .cpp.

That said, I will probably continue to write C code as C99 for now and take a look at the C11/C17 features such as restrict, stdnoreturn and so on.  Note C17 is considered a bit of a bug fix for C11.

The joys of setting up a server

The joys of setting up a server

Virtualmin install script runningThis is for my MMO game. I need a backend server, so yesterday I signed up for a vps service. I picked Ubuntu 20.04 LTS which I’ve used before and received the login details. I use Putty to log in via SSH and WinSCP for browsing, copying files.  Installing Apache2 is actually very easy. Likewise PHP. I didn’t install MySQL which might have been a mistake, so I had a LAP system (Linux, Apache, PHP) rather than LAMP.

What I did do sensibly was pay for a backup snapshot service.  When I logged in first I did an sudo apt update then upgrade. Then I took a snapshot.

And I needed it. My exact configuration was Apache 2.4 with PHP 7.4. Apache worked when I tested it but PHP didn’t. Well from the command line the PHP command worked and I could see the version but it didn’t work with Apache.  I created a PHP script with phpinfo in it but instead when I browsed to it, it downloaded the script rather than ran it.

This is usually the case that httpd.conf needs some AddType statements only there isn’t an httpd.conf anymore in Apache 2.4. I did locate a mime.conf but adding AddType into that and restarting Apache made no difference. I eventually wasted about three hours on it yesterday. However in my searches I came across mention of Virtualmin and so today I restored to the snapshot and ran the Virtualmin open source script. Luckily Ubuntu 20.04 LTS was one of the supported distros and Virtualmin installed without a hitch. It’s very impressive. The screenshot is of virtualmin installing. Thankfully all the bars on the right were green…

Back about 12 years ago when I first started experimenting with Linux and websites, i came across Webmin and used it extensively. Virtualmin installed both WebMin and Usermin. It has changed substantially in the time since I last used it. It surprised me a little because for years Ubuntu and Webmin didn’t seem to get on and Ubuntu recommended not to install it.

Anyway I now have a server that runs PHP. The backend will be running .NET Core once installed, but it’s just as easy to use Apache and PHP for receiving order files (from mobile). PHP is just to simplify getting those files onto and off the server.

My code is buried in the Arctic!

My code is buried in the Arctic!

Svalbard taken from a cruise ship in 2015It’s true. Well my code and that of thousands of others on Svalbard, a set of islands halfway between Norway and the North pole. I’ve actually been to Svalbard on a cruise a few years back. It was cold and a bit damp. That’s one of a bunch of photos I took.

GitHub shipped 21 terabytes to be stored in long term storage under the ice. It should be good for a few hunded years at least so thats my backups taken care of. I wish!

Normal blogging will be resumed tomorrow. Hey after 200 posts I’m allowed a semi-off topic post once in a while!

Godot and IO games

Godot and IO games

Agar.io homepageI occasionally answer (often in a somewhat ascerbic tone) questions on Quora.com, not just programming though that;’s the bulk of my answers.  Feel free to follow me there! One question today was asking whether it was better to deveIop IO games on Web or mobile first.

I’d never really looked at this category- what is an IO Game? Very loosely, its the replacement for Flash games, and unlike those, it can run on mobile or cross-platform. A typical development system for such games is Godot, an open source cross-platform development system. The main language is GDScript based loosely on Python but any programming languages that has GDNative bindings (so C++, C#, Rust, Nim, D and any others I don’t know about. ).

You get games like the multiplayer game agar.io which has an entry on Wikipedia and is cited as an example of an IO game.

 

How to extract text from an Image

How to extract text from an Image

Puzzle image with textThis is more of a tip, but it can be a useful thing to know. If you look at this photo you’ll see it contains a bunch of words. Now you could type them in but that’s a bit tedious.

If you have Ms Office then you’ll have probably have OneNote. Take the image and paste it into OneNote. Now right click on the image (in OneNote and in the popup menu) you should see Copy Text from Picture as the third item in the menu.

Just click that then paste the text in to notepad, a text editor, whatever. There’s your text.

OneNote with popup menu

Here’s the text pasted directly from the clipboard. 100% accurate apart from a ! that it found from somewhere near the edge (just after wain)!

Solution to Puzzle 1
ain, alb, albs, als, ani, ard, ards, arid, ars, awn, blah, blain, dhal
dhals, dirl, dirls, drain, draw, drawn, fah, fain, fan, far, fard, tards,
farl, farls, fars, faw, fawn, flan, flaw, flawn, fra, hain, half, halts, han,
hard, hards, harl, harls, harn, harns, haw, hid, ids, infra, inward,
inwards, lah, lain, lar, lard, lards, larn, larns, law, lawin, lawn, nard,
nards, rah, rai, rain, ran, rani, raw, rawn, rid, rids, slain, slaw, wain, !
wald, walds, wan, war, ward, wards, warn, warns, wars, wha, wharf,
wharfs, whid, whids, whir, whirl, whirls, whirs, Win

Undefined behaviour in C

Undefined behaviour in C

Unexpected
Image by John Hain from Pixabay

One of C’s not so brilliant features is the range of Undefined Behaviour (UB). Things like using an uninitialised variable or having an int variable overflow don’t have behaviour defined; thus it is UB and you cannot accurately predict what will happen. Likewise accessing a NULL pointer can cause odd behaviour. It gets more sophisticated than that. What if you type cast an int to a float?

The LLVM blog have an interesting set of posts on UB and it’s definitely worth reading. I’ve done a lot of C programming, so I’m rarely surprised by my programs but its useful to know about these things. In my case, I started with assembler programming and then learnt C++ and C in that order so my perspective has always been to try and understand whats going on deep down.

Chess in C

Chess in C

Tom Kerrigan ChessI noticed that the Covid lockdown had made streaming of chess games very popular and did a search to see if I could find the source of one in C. The first one I found was actually C++ (cout << is a bit of a giveaway!) but I found this one by a developer called Tom Kerrigan. It runs from the command line and the exe is just 157 KB in size.

Note it is copyrighted but he has put his source code out there so if you are interested in seeing how a chess program is written this is an excellent place to start. His code also includes a lot of comments and includes an opening book.