Interesting notes on C

Interesting notes on C

Notes
Image by David Schwarzenberg from Pixabay

If you are learning C there are a number of points that may not be mentioned and possibly aren’t obvious. For instance with multidimension arrays. “The array size can be derived from its initialization but that’s applicable for first dimension only. For example, ‘int arr[][2] = {1,2,3,4}’ is valid but ‘int arr[2][] = {1,2,3,4}’ is not valid.” and “Unlike ‘long long int’, there’s nothing like ‘long long double’ in C. Besides, real data types (i.e. floatdoublelong double) can’t be unsigned.”

These and a whole lot more notes can be found here. It’s definitely worth a read. The only thought I had that was worthwhile is to do with the order of execution in a for statement.

The definition is in this order: for (initialisation, end test, alter loop variable ) statement;  but the actual order of execution is initialisation; statement; alter loop variable; end test.

Is there a market for games on Linux?

Is there a market for games on Linux?

Ubuntu GamesDon’t get me wrong, I like Linux. I’ve put in enough hours, especially on Raspberry Pi programming but games don’t seem to sell at all well on Linux. For one there’s a massive expectation of free software. Ubuntu comes with plenty of free games and some really good quality ones like 0Ad or Battle of Wesnoth.

The screenshot is from the Games category of the Ubuntu software installer. A very rough count suggests there’s over 420 games available there. Add to that emulators and ROMs (if you can get them) and you end up with almost 20,000 games.

I bought a Retro Pi at a Winter fair a couple of years ago and it came with 18,000 games. It was an Orange PI with a 16GB SD Card running the Retro Pie distribution which is available online. There’s no way the games are legal BTW, I even found four games that I wrote back in the mid 1980s running on the ZX Spectrum and CBM-64 emulators. All of the four games were smaller in size than the screenshot image! (Please don’t ask for a copy of the games. I gave them away with the Orange Pi to a nephew.)

If you are a Linux developer, how many games have you ever bought?

As this article on The Register explains “In the past year, Puppygames has sold 290 of its eight games on Linux and nearly 9,000 on Windows. ”

Most money I’d guess is made by companies producing AAA games especially on console.

So if you restrict yourself to developing games only for the Linux market then I imagine it would be very disheartening. However it’s not difficult to port them to Windows. Or you can look beyond desktop and venture into mobile development and web games. Both are considerably different to desktop games development and have their own problems (like getting your mobile app found when there’s a million others competing against it!). Or you can go down the “Write an ebook book about game development” as I have done.

Wizznic – a cross platform game in C

Wizznic – a cross platform game in C

Wizznic screenshotWizznic is a sliding blocks puzzle game. The game is written in C and used SDL and SDL Gfx. The developer a Danish programmer called Jimmy Christensen has made it freely available on GitHub so you can modify it, do what you want with it. It’s distributed under the GNU GPL (General Public Licence).

The game itself is multi level and there are other levels (DLC – free) available, just get the code on the content page. The game includes an editor which is why I guess there are so many downloads available.

The game itself is for gp2x, Windows and Linux and also includes make files for Pandora and PSP (PlayStation Portable). I’m always curious about the source code and the main game comes in 36 .c files and 37 .h with the biggest file being the 2000 lines of code menu.c.

If you are learning C or just want to see how a game is done, it’s an excellent way to study, especially because it’s cross platform. I suspect it’s SDL 1.25 because it uses surfaces rather than textures as in SDL 2.

 

Tutorial on Atoms added

Tutorial on Atoms added

Atoms command lineThis is my alternative approach to learning C. Show code, explain what it does in depth rather than explaining the C language feature-by-feature. I’ve created a 90 line skeleton program for atoms. The full file is on GitHub, The file is called atoms.c.

All this does is setup the board and let you enter your move. I’ve tested it on Windows and it calls _getch() to read the keyboard so it’s fast to enter moves and you don’t have to press enter/return. It then validates the move and updates the board and draws it. No chain reactions yet or computer play but those will be in future tutorials.

In the screenshot you can see I added one atom to (3,3) and then (5,6).

The board uses three characters for each cell with atoms prefixed by P or C for player and computer.  Trying to have the computer play well will be an interesting bit of programming.

 

Raylib – a C library for videogames

Raylib – a C library for videogames

Raylib screenshotaMuch as I love SDL, I’m quite happy to look at alternatives and Raylib looks very good. It was inspired by Borland BGI graphics lib and by XNA framework (both of which I’ve used).

It’s written in C99 and is very cross-platform, even listing Raspberry Pi (I’m guessing they mean Raspberry Pi OS) as well as Windows, Mac, Linux, Android and HTML5. Basically any platform that supports C and OpenGL. There is a massive list of bindings for use in other programming languages.

Check out the examples page; you can try them online and each has a C source code listing so you can see how it’s done.

What I really like about RayLib is the scope of it and this is where I think it beats SDL. There’s 2D and 3D support, text support, in eight modules (core, shapes, textures, text, models, shaders, raudio and physac). It also comes with 8 free fonts but you can use your own custom fonts, pretty much the same way as I did in the Asteroids game.

I was very impressed with the text demo where a red box contains the text and if you resize it with the mouse the text is word wrapped.

The Spanish developer Ray (surname unknown) was cited today as the #1 trending C developer on GitHub and that’s a testament to the seven years that he has put into Raylib. His design philosophy with Raylib is to make programming fun and I am going to check it out.  If it looks as good as I think it is, I will be switching to it for future games work. One thing that particular intrigues me is the RayGui library– an immediate mode GUI library. There are various tools that have been designed with it. and it supports these GUI controls:

Label       |  Button      |  LabelButton |  ImageButton  |  Toggle      |  ToggleGroup |  CheckBox
ComboBox    |  DropdownBox |  TextBox     |  TextBoxMulti |  ValueBox    |  Spinner     |  Slider   
SliderBar   |  ProgressBar |  StatusBar   |  ScrollBar    |  ScrollPanel |  DummyRec    |  Grid

RayGui table

 

 

 

 

 

 

First new C Tutorial has been added

First new C Tutorial has been added

Atomes boardAs promised, I’ve added the first C Tutorial using the Atoms (aka Chain Reaction) game. However this is just a very easy intro to the Atoms game and doesn’t have a line of C code in it. We’ll save that for the next tutorial.

The planned tutorials for this are:

  1. Intro to the Atoms game.
  2. Drawing the Atoms board.
  3. Reading the keyboard and making a move.
  4. Handling explosions and checking if the game is over.
  5. Programming the computer player AI.
  6. Drawing a high-score table.

These will each include a C listing and an explanation of what the new C code does.

 

Thinking about future C tutorials

Thinking about future C tutorials

Molcules
Image by Anand Kumar from Pixabay

The tutorials I’ve done have followed a fairly “standard” model. Learn a feature, learn a new feature and so on. But what if this isn’t the best way to try and teach C?

I can’t remember how I learnt C. I’m not actually sure that I did which sounds weird but I learnt C++ nearly 28 years ago (I bought a book on C++ programming while on a weekend holiday in York). I learnt more C++ four years later when I was working on a football game. That project lasted six months and then the designer of the game decided to visit India and never came back and the whole thing fell though.

Fast forward to 2006 when I started writing the About C, C++ and C# column for about.com. Because I knew C++, I sort of knew C without explicitly having learnt it, Back then C++ was a superset of C. It still mostly is but there is the odd divergence.

This morning I was reading this thread on reddit. “What do you guys think its the best way to improve your coding?” and it got me thinking about the C tutorials. I know that I like to learn by working on small projects. Most games are too big for a tutorial (heck you can get a book out of them).

So I got to thinking, what is a simple game that would make for a good set of C tutorials? I even dug out the two books BASIC Computer games and More BASIC Computer games for inspiration. However in the end I decided on the game atoms. Here are the rules.

Rules for Atoms (aka Chain Reaction)

This takes place on an 8 x 8 board. Each turn you and then your opponent. Add 1 to any cell on the board. At the start all cells are empty with a value of 0. When you add 1 to a cell, it turns red or blue if the computer is playing. If a cell reaches 4 then it is cleared to 0 and all four cells around it (horizontal and vertical) have 1 added to them and become the player’s colour.  Cells in the corner only need to reach 2 to explode while cells at edges need 3.

When there are enough atoms on the board, a chain reaction can take place and you win if your atoms replace your opponents or lose in the opposite case.

To keep the tutorials simple this will be a console game. Each cell will be blank or show the number of atoms in the cell followed by a C (for computer) or P for Player instead of colours.  I’ll start the first tutorial tomorrow.

 

New tutorial on C loop statements published

New tutorial on C loop statements published

Mandala loops
Image by Renate Anna Becker from Pixabay

There are four types of loop statements in C, for loops, while loops, do-while loops and goto statements. The first two are much more popular; In my Windows asteroids game, I used 54 for-loops, six while-loops, three do-while loops and no gotos.

I found the same thing in Delphi and Turbo Pascal. The only difference is that Pascal uses repeat .. until instead of do-while and I prefer repeat-until.  The logic is slightly clearer I feel as well. You repeat the body of the loop until a condition becomes true. In the C do-while statement, you repeat the body of the loop as long as the condition is true.

Anyway I’ve published tutorial eleven on C loop statements. Have fun. I’ll get it added to the tutorials page shortly..

GitHub Gameoff-500 entries

GitHub Gameoff-500 entries

Game Off 2020Between November 1st and December 1st, GitHub organised the Game Off 2020 where programmers submitted their own original games on the theme moonshot. There were 500 submitted and you can view them ordered by score here.

The top scoring entries are shown here on GitHub which also explains what programming language and toolkits were used. It’s interesting that of the 500 entries more than half were for Windows and approximately the same number are browser playable. The breakdown is Browser playable (262), Windows (252), macOS (108), Linux (96) and Android (8).

The GitHub link shows the programming language and C# + Unity seems to make up a large proportion as well as Godot +GdScript. But I haven’t been through them all and it would be nice to see if there are any that are C + SDL.

 

Some thoughts about game AI

Some thoughts about game AI

Military
Image by Dimitris Vetsikas from Pixabay

When I say AI, I mean control logic for computer played units in a game, not Ai as in Machine Learning or Artificial Intelligence. For instance in the Empire type game, that I started (On the Games source link) which has ships and units fighting on land and sea, capturing cities and building new units.

An AI for an Empire type game has a lot of logic to be implemented. I thought about this quite a bit and came up with a task based Ai system. A neutral city is spotted by an exploring ship. If its a transport with armour on infantry then it just attacks the city and hopefully captures it.

If not a task is created. This comprises a number of steps.

  1. Locate units nearest to the city that aren’t allocated to the task.
  2. If the units are on the same continent then just send them to attack the city.
  3. If not Locate the nearest transport and send it to rendezvous with the units at the nearest point to them. Load the units.
  4. If there aren’t any units free then locate the nearest friendly city that’s not tasked with anything and task it to build the units.
  5. If there’s no transport, have the city build a transport after building the armour/infantry units.

That’s just one for task but what if a friendly city is threatened and the tasked units are the nearest. Care has to be taken so that units aren’t flip flopped, getting orders one turn then being pulled away three turns later to a different task.

Each task should be given a time-to-live (say 30 turns), long enough to acquire the resources (units it needs) to do the job and reach the target. The rule then is no diverting units on a task unless a dire emergency (like threatening one of its cities) and those are the nearest.

There’s nothing worse than allocating units to a task then seeing enemy units sail right past unopposed. Writing code to deal with different situations and priorities is not an easy task