Month: August 2020

Useful list of tutorials in C

Useful list of tutorials in C

 

Online educatioon
Image by Mudassar Iqbal from Pixabay

This GitHub repository has a pretty long list of beginner to medium sized project tutorials in C. There are over 30 on game development alone with titles like “Create a 2D platformer” and “SDL2 Isometric Game Tutorial“.

Other tutorials are on databases, networking, programming languages and operating systems plus even a bit on Blockchain. Recommended!

I’ve added a permanent link on the Links to C utilities page (on the top menu).

Programming Language Surveys

Programming Language Surveys

Arduino board
Image by Seven_au from Pixabay

It’s that time of year when several infrequent programming language surveys burst on the scene and I’m now beginning to doubt the veracity of the IEEE survey, along with the eternally mad Tiobe which has C at #1. The IEEE has C at #3 in popularity, but also counts Arduino as a programming language because people search for Arduino code. Such lack of rigour does you no favour chaps, so much for engineers!

Arduino (that’s one in the picture) is, like a Raspberry Pi; a hardware platform.  Given that Raspberry Pis have sold over 30  million which seems a lot more sold than Arduinos, why are they not listed?

RedMonk came out with their June survey recently (they do a small number each year)  which is based on popularity of GitHub project’s programming languages. They put C at 11th and C++/C# as joint 5th.  IEEE didn’t have C# in their  top ten which is completely at odds with every other programming survey. Even Tiobe has it at #5.

As always, I value Reddit’s programming language sub-reddits which has C at 9th and C# at 4th with C++ at 5th based on the numbers of members of each group. There is always a lag between the numbers shown on the summary and the actual numbers in each sub-reddit when you click into it.

Mobile game development progress

Mobile game development progress

Manana Banana Screenshot on AndroidDevelopment continues with the first game, which is a card game. I know the game as Top Banana, but I think that is the name of a commercial game, so for now the working title is Manana Banana. All it does currently is display the cards and backs as you can see. No photo this time, I learnt how to take snapshots on the phone (Hold down the power and Lower volume buttons at the same time) As it’s plugged in to my PC,. copying it across was not difficult.

This uses my virtual screen technology working on an 800 x 1400 virtual screen then scaling output onto the real screen which in this case on the Alps X27 Plus is 480 wide by 960 deep. As you can see I have a black rectangle at the top and the Android controls are visible at the bottom, neither of which I programmed for. I’ll get those fixed.

You play this game by clicking on the six top cards one-by-one and then tap the back of the card where you want to play that top card. So you end up with three poker hands. In this case I’d put the 7 and 9 on the top row for one pair, the two fives on the 2nd row also for a apir and the King and Jack for an Ace-high straight on the bottom row. The gap on the right will show the text of the hand so will say Pair, Pair and Ace Straight or something like that.

Next thing is making the cards clickable. That is the top six cards and the the three sets of two backs below. Once that’s done I’ll make that bit work then plug in the Jessie Chunn’s poker hand evaluation code to figure out what each hand of five cards is and more importantly give it a numeric score.

The gradient that covers the screen came from Unsplash.com.

 

How to create a big image from a number of smaller ones

How to create a big image from a number of smaller ones

Card DeckAfter the day before yesterday’s experiment showed that loading a single larger image on an Android phone is nearly three times faster than loading 52 smaller images, I decided to write a short utility program to read all the 52 individual card files and create one file with them laid out neatly in four rows of cards, one row per suite with each card running from Ace to King.

The individual card .png files were all in one folder with two letter filenames rank and suit in capitals like AS.png (Ace of spades), TH.png (Ten of Hearts and so on).

This 62 line C# program reads all 52 files into RAM in a two-dimension array cards (original name eh!) in the method LoadAllImages() then in BuildOneImage() it writes them out in the four rows into the bitmap allcards then saves them out as one .png format file.

C# makes doing this very easy. In C you would have to write your own file format handling code or use a library like libpng.

// Author D. Bolton Learncgames.com You are free to use and copy this program as you wish but please leave this line in.
using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace mpics
{
    class Program
    {
        const int CardWidth = 100;
        const int CardHeight = 153;
        const string source = "your path here"; // Path to all 52 card files
        const string dest = "target file here"; // Path + filename for target .png file.
        const string suits = "HCDS";
        const string ranks = "A23456789TJQK";

        static Bitmap[,] cards = new Bitmap[13,4];
        static Bitmap allcards = new Bitmap(CardWidth * 13,CardHeight * 4);

        static void Main(string[] args)
        {
            LoadAllImages();
            BuildOneImage();
        }

        private static void BuildOneImage()
        {
            Console.WriteLine();
            var aty = 0.0f;
            using (Graphics g = Graphics.FromImage(allcards))
            {
                for (var y = 0; y < 4; y++)
                {
                    var atx = 0.0f;
                    for (int x = 0; x < 13; x++)
                    {
                        var r = new Rectangle((int)atx, (int)aty, CardWidth, CardHeight);
                        g.DrawImage(cards[x,y],r);
                        atx += CardWidth;
                    }
                    aty += CardHeight;
                }
            }
            allcards.Save(dest, ImageFormat.Png);
        }

        private static void LoadAllImages()
        {
            var i = 0;
            foreach (char c in suits)
            {
                var j = 0;
                foreach (char r in ranks)
                {
                    var s = source + r + c+".png";
                    cards[j++,i] = (Bitmap)Bitmap.FromFile(s);
                }
                i++;
            }
        }
    }
}

Don’t forget to set the source and dest path constants. This was compiled in Visual Studio 2019 and run on Windows 10. The only really important thing I found was using the Rectangle r in BuildOneImage(). If I used just atx and aty and didn’t specify the size of card then DrawImage() drew much smaller images into the bitmap. I’m not sure why as the card images when loaded from disk were 100 wide by 153 deep, the same as specified in the constants CardWidth and CardHeight.

The card images were originally downloaded from the American Contract Bridge League but were much bigger so I scaled them first to 100 x 153 pixels. There are many free playing card images on the web but these are one of the nicer sets.

Very nice guide to Threads and Processes

Very nice guide to Threads and Processes

Concurrency vs Paralleism b y Backblaze
Concurrency vs parallism by Backblaze.

In my experience, if you ask most programmers to explain the difference between processes and threads they will struggle a bit. Even trickier is the difference between concurrency and parallelism. I wasn’t 100% sure myself.  Do you know the difference between multi-threading and multiprocessing?

This article on the Backblaze website is one of the nicer explanations of threads vs processes and I would recommend it to anyone. Backblaze is a backup solution that uploads your files (and subsequent changes) to their site. When I first moved into my home I used Backblaze but had to abandon it (not their fault!) because my internet was abysmally slow. It told me it would take a year to do the first upload!

Although in C, multi-threading/multi-processing is not that common, it is in other languages and with the trend towards more and more cores (my CPU is five years old and has 6 cores or 12 with hyper-threading), so writing software that only uses one thread is wasting a lot of processing power.