Trials and tribulations of mobile development

Trials and tribulations of mobile development

Mobile apps
Image by Gerd Altmann from Pixabay

Compared to desktop, I find mobile development somewhat frustrating at times. On iOS, you have a right palaver with certificates and provisioning profiles before you can even run a program on your device. Android is nowhere near so bad, but you have the fragmentation problem I mentioned yesterday. Too many different models with different screen sizes and resolutions.

C# is a nice and powerful language and well suite dto mobile, though to be fair so are Swift on iOS and Java/Kotlin on Android. Android software is a bit of a mishmash. You have to get the right SDKs to match your phone’s OS. Then you have to create a simulator if you are not testing on a device. iOS has its own issues, generally involving needing a Mac before you can put code on the iPhone.

MonoGame is a nice framework. Compared to both Android and iOS native development (no JavaScript here!) it’s a lot more straightforward to work with. You override the Game.Update method for reading keyboards and touchscreens. Then you override Game.Draw to output graphics. In that respect it’s even easier than using SDL for games. It’s a little like WinForm programming.

Another extension to the scope of this blog

Another extension to the scope of this blog

MonoGame application running on AndroidWhen I started this blog at the end of February it was to assist sales of my ebooks. The one written and the one being written. Things have changed a bit and the one being written is now the Raspberry Pi book. That book is still being written but it’s not as high a priority now.

But I also have another side project underway which is to do with mobile game development (and maybe eventually Raspberry Pi).  The only thing is, this project uses C# not C or C++. However it is games related and as its my blog, I’ve decided that I shall write about it here as well as the C stuff. So get used to hearing about MonoGame, XNA and Xamarin.

Just so you know. Xamarin is the company (bought by Microsoft a few years back) that was formed by the founder of the Mono Project (Miguel de Icaza)  and is an excellent cross-platform technology for iOS and Android development based on C#.  I wrote two mobile apps (both sadly derailed by events) one of which was the equivalent of the Uber app. I managed to store every one of the 29 million UK postal addresses in the RAM of an iPhone 6.

XNA was a Microsoft technology for creating games on Xbox, Windows and Windows Phone some ten years ago. It reached version 4 but was then dropped by Microsoft. However the MonoGame project took it over and MonoGame is XNA reincarnated.  It’s cross-platform and runs at 60 fps.

The only technology that I believe can rival Xamarin is Google’s Flutter but it is still too new and doesn’t do games. The C# code runtime adds 3.5 MB to the overall executable but is very efficient and fast.

The image is an Android phone I have running an app. It doesn’t look much but that menu will display with the same height and width on any Android phone.  The problem with Android development (compared to iOS)  that there’s over 20,000 different size screens compared to a dozen or so on iOS. My program is scaled to a fixed virtual size and then tranforms that to the real size. MonoGame lets you do that.

A game development website recommendation

A game development website recommendation

Games
Image by Maria_Alberto from Pixabay

In my previous post I mentioned that I’d had to discover a lot of stuff for myself. Just before the Web came into being ie 1994-1995 I’d been working on a hexagon map based game.  I figured out how to draw hexagons, how to store a hexagon map (as a 2D grid), how to click on a map location and discover exactly which hexagon you clicked.

I still have source code that I wrote 30 years ago for the postal game Quest and likewise for the hexagon game which I called Onslaught. Both were written for a 16-bit Turbo Pascal and probably could with a bit of effort be transformed into 32-bit or 64-bit Delphi had I the time and desire, both of which I lack.  Given that Quest is still run, that’s probably not a good idea anyway! Plus I had only been writing Pascal code for a few years and my old code is far from being comprehensible!

Anyway, a lot of the wisdom and stuff that I taught myself can be found on the excellent Amit’s Game Programming Information website (particularly the hexagon stuff) and I highly recommend it if you have any desires to be a game programmer.

 

 

 

 

My different programming style from before

My different programming style from before

Software
Image by Gerd Altmann from Pixabay

Next year I will have been developing software as a job for 40 years. I graduated in mid 1981 and three months later started my first job in October 1981. To say that things have changed enormously would be an understatement. Back then there was no internet so information was limited to three sources: Books, magazines and what you learnt yourself.

Along the way I have used commercially a large number of programming languages: BASIC (8k Basic on Apple II and ACT Sirius- a precursor to the IBM PC), CBasic on Dec Rainbow, Z80 assembler for home computers (MSX, Zx Spectrum and Amstrad CPC 464), 6502 for CBM Vic-20 and CBM-64 (and also one project for the rarely heard of CBM-16) then Turbo Pascal on CP/M and MsDos machines, Turbo C++, Ada, Delphi (4,5,7,9 and XE7) , SQL, HTML, PHP, CSS,Java, C and C# (with Xamarin for mobile). As well as the ebook (C) I’m working on two side projects (C#and MonoGame).

Back in the day pre-internet when I wanted to do graphics, I had to figure them out myself. Things like Turbo Pascal came with thick manuals that you more or less learnt, To draw rectangles on the CBM-64 in hires mode. I figured out how to draw them and finished a game in 6 weeks. Recently I’ve been doing the same using MonoGame for mobile game development. I did a quick search of Google, found a library in SourceForge or GitHub, downloaded it and fitted it in. No big manuals to learn, just knowing how to find stuff on the web, make sure the licence allows its use and I can understand its source code enough to use it.

Intellisense also helps. No more knowing that such and such a function has three parameters, you just type in the name and it shows the parameters and their types. Is it more productive than before the web? I think so but there’s also more distractions.

Do I miss those days when things were a lot simpler? A little but I know I’d miss being able to find things with Google, on StackOverflow, even in Wikipedia etc.  Things can be a lot more complicated but I think I’m way more productive.

Using Excel for level design

Using Excel for level design

Excel level spreadsheet for AsteroidsIn the asteroids game and shortly in my MatchThree game, I’ll be creating a level data array of struct. This has a struct for each level containing a count of particular features for that level.

This is the struct and array for a level in asteroids.

struct level {
	int nums[4]; // how many of each size of asteroid
	int aliens; // how many aliens
	float factor; // from 1.0 to 1.5 - multiply asteroid speed by this
};

struct level levels[50];

This is the first 3 levels and level 50.

#include "levels.h"

struct level levels[50] = {
{ .factor = (float)1,.aliens = 1,.nums = { 0,0,3,3 } }, // Level 1
{ .factor = (float)1,.aliens = 0,.nums = { 0,1,3,3 } }, // Level 2
{ .factor = (float)1,.aliens = 0,.nums = { 0,1,3,3 } }, // Level 3
..
{ .factor = (float)1.5,.aliens = 3,.nums = { 4,4,5,5 } } // Level 50

I didn’t type any of this in. Instead, I created that spreadsheet above. It’s easy to work out difficulty levels in column H.  The formula that calculates this is ins this on row 5. If you don’t know Excel, the $ in the factors means that as you copy this into successive row, it keeps the $ row value constant.

=(B5*B$4)+(C5*C$4)+(D5*D$4)+(E5*E$4)+(F5*F$4)+(G5*G$4)   - Row 5

=(B6*B$4)+(C6*C$4)+(D6*D$4)+(E6*E$4)+(F6*F$4)+(G6*G$4) - Row 6

So you can see its multiplying the values in rows 5 6 etc by the values in row 4.  Having put that in place I could tinker with the values in rows 5,6 etc to make the difficulty level increase roughly at the same pace. The difficulty level for level 50 is 77.5.

This is what the Excel formula looks like to generate the C code, it’s in cell M5 in the spreadsheet and then copied and pasted down.

="{.factor =(float)"&G5&", .aliens="&F5&", .nums = {"&B5&","&C5&","&D5&","&E5&"}}, // Level "&A5

and this is what it looks like. C code that can be copied and pasted directly. It even includes the comment for the level number!

{.factor =(float)1, .aliens=0, .nums = {0,0,3,3}}, // Level 1

Creating a spreadsheet and C code from it this way saved a lot of typing but let me quantify the numerical difficulty which increases from 17.5 on level 1 to 77.5 on level 50. There’s no meaning to this value, it’s just a calculation.

A tictactoe (aka noughts and crosses) game in C

A tictactoe (aka noughts and crosses) game in C

TicTacToe aka Noughts and Crosses
Image by Kevin Phillips from Pixabay

I was asked to write this a few month’s back and it took me 2 or 3 evenings. It’s 312 lines long in just one file. Hopefully there are enough sensible function and variable names to make sense of it.

It runs in a terminal. It was compiled with Visual Studio but should not need many changes to compile with gcc/clang. (I hope!).

I made extensive use of pointers. For instance this function uses pointers in a for-loop.

int InDanger(char piece, int * x, int * y) {

    for (*y = 0; *y < 3; (*y)++) {
        if (CountRows(piece, y,0)==2) return 1;
    }

    for (*x = 0; *x < 3; (*x)++) {
        if (CountCols(piece, x,0) == 2) return 2;
    }
    *x = 0;
    if (CountDiagonal(piece, x,0) == 2) return 3;
    *x = 2;
    if (CountDiagonal(piece, x,0) == 2) return 4;

    return 0; // no danger
}

I’ve added this to the GitHub C games repository and updated the C games source page.

Useful reference to C operators

Useful reference to C operators

Reference
Image by ElasticComputeFarm from Pixabay

Ever wondered what all the operators that you can use = are. Like ^=. Or what about operator precedence? Does * come before ++? (Answer no, ++ is higher precedence than *).

Do you know how

a & b == 7

is parsed? It’s actually a & (b==7)

This Wikipedia page lists all the operators with precedence order and as it includes C++, it lets you see what you can and can’t do in C and C++.  It’s worth bookmarking whenever you need to look these things up.

Playing Solitaire in a terminal

Playing Solitaire in a terminal

Solitaire in a termianalI thought this looked quite neat so I’ve added it to the C Codes Link page. It’s a game of Solitair (the Klondike version) running in a terminal.

It’s quite a detailed explanation of how its been implemented and of course it’s all in C. If you know of any games written in C (and open source), please let me know and I’ll add them.

A simple programming challenge

A simple programming challenge

Contest
Image by Arek Socha from Pixabay

Background

Around 1981, a British computer magazine (Personal Computer World) had a programming contest and this was one of the puzzles.

There are many 9-digit integers in the range 123456789 to 987654321 where each digit only ever appears once. What is the 100,000th number in this sequence?

Example

The first number is 123456789, the second is 123456798, the third is 123456879 and so on. No digit can repeat so 122345675 is not a valid number in this sequence.

The problem was “Write a program in C or C++ that outputs the 100,000th number as fast as possible. Use any algorithm, except you cannot pre-calculate the answer and then write a program that just prints the result (Somebody actually tried it). Your entry must calculate the number!”.  I’ll give this a month so the deadline is August 14th 2020. Please don’t copy any of the existing answers- you can with a bit of poking around and yesterday’s blog entry find some answers to this.

Prize?

Er fame and glory and a mention on here!

Note

It’s possible to solve this by hand. I did, back in 1981. It took me 20 minutes with a pen and paper. I also wrote a 6502 assembly language version of it than ran on a Vic-20. It took something like 10 hours to count up to the correct answer.

The Wayback machine-slightly offtopic

The Wayback machine-slightly offtopic

There is a wonderful website (archive.org) that makes a backup of websites. It even has one of this blog, taken on April 30th. It’s remarkable.

The reason I mention it is because I was looking at the backup of cplus.about.com, a website that I managed (curated, wrote tutorials etc.) on “Programming in C, C++, C#” (and even Go) between 2006 and 2013.  One of the things I ran there was a set of programming challenges and by the end I’d done a whopping 70 of them. I came up with the idea, published it, then adjudicated entries.

This page has a list of 65 of them.  There may be more from a later date but no more than 70.

As I can now gain access to most of them, I’m going to rerun a few here. starting with the first which I’ll publish tomorrow.