Category: Techniques

Do you use assert in your code?

Do you use assert in your code?

assertIt’s a macro that checks an expression, and if that expression isn’t true (.e. non-zero) it outputs a message and halts the program.

Here’s an example.

#include 
#include 

int main() {
	int x = 0;
	assert(x != 0);
	printf("It is the end");
}

Because x is 0, it triggers the assert and the program never reaches the printf statement. It’s a bit of a crude tool. In other programming languages like C++ or Delphi it raises an exception which can be handles but C of course does not have exceptions.

My own preference is to check the value for example making sure a pointer is not null and nthen displaying an error but other programmers prefer to use assert and have it kill the program if things go wrong.

Using small delays in C with SDL ticks

Using small delays in C with SDL ticks

Asteroids game - player ship rotationRunning a game at 60 frames per second means that handling things like key presses can be interesting. In the game, I call various SDL functions and via a giant switch statement set flags. Then later in the game loop, those flags are used to determine actions

So if you press Q to rotate the player ship anti-clockwise (counter-clockwise to yanks!) without some limiting thing, it would whizz round through 900 degrees each second. (There are 24 rotation angles for the ship, each 15 degrees. 60 x 15 = 900) .

I use a very simple technique to limit it. SDL has a function called SDL_GetTicks that returns the number of ticks since SDL was initialized in the game, i.e. when it started running. A tick is 1/1000th of a second, i.e. a millisecond. By calling this function twice, you can measure a time period. It’s not as precise as the nanosecond CPU clock that I use but for the kind of delays I’m talking about it is more than sufficient.

This is the code that is called each frame in the game loop.

void RotatePlayerShip() {
	if (rotateFlag && (SDL_GetTicks() - rotTimer > 40)) {
		rotTimer = SDL_GetTicks();
		if (rotateFlag == 1) // CounterClockwise 
		{
			Player.dir += 23;
			Player.dir %= 24;
		}
		else
			if (rotateFlag == 2) // Clockwise
			{
				Player.dir++;
				Player.dir %= 24;
			}
	}
}

Because the game loop syncs to the vertical fly-back, the time between two successive calls of this would be about 16.666 milliseconds. (=1000/60), but the check to see if 40 ticks have passed slows it down to 25 x 15 = 375 degrees rotation per second, i.e. just over one complete revolution which is more manageable than 900/360 = 2.5 full rotations. Plus if you wish rotation speed to be faster just change the 40 to a lower value.

This measured time delay is used quite a few times in the game. You could use it as a means to make the game get harder by having shorter delays on say aliens moving or firing.

How to Draw a circle in C

How to Draw a circle in C

Asteroids-with shield-round-player-shipIn the asteroids game, when you press the s button to put up a shield, it draws a circle.  I must confess, I didn’t know how to draw a cuircle so looked it up and found an example on StackOverflow. You can use code from StackOverflow, licensed under a MIT license.  I include the link to StackOverflow in the game code (in the chapter 48 zip file) in a comment.

Here’s the code in the game.

void DrawCircle(SDL_Renderer *Renderer, int _x, int _y, int radius)
{
	int x = radius - 1;
	int y = 0;
	int tx = 1;
	int ty = 1;
	int err = tx - (radius << 1); // shifting bits left by 1 effectively
								  // doubles the value. == tx - diameter
	while (x >= y)
	{
		//  Each of the following renders an octant (1/8th) of the circle
		SDL_RenderDrawPoint(Renderer, _x + x, _y - y);
		SDL_RenderDrawPoint(Renderer, _x + x, _y + y);
		SDL_RenderDrawPoint(Renderer, _x - x, _y - y);
		SDL_RenderDrawPoint(Renderer, _x - x, _y + y);
		SDL_RenderDrawPoint(Renderer, _x + y, _y - x);
		SDL_RenderDrawPoint(Renderer, _x + y, _y + x);
		SDL_RenderDrawPoint(Renderer, _x - y, _y - x);
		SDL_RenderDrawPoint(Renderer, _x - y, _y + x);

		if (err <= 0)
		{
			y++;
			err += ty;
			ty += 2;
		}
		else 
		{
			x--;
			tx += 2;
			err += tx - (radius << 1);
		}
	}
}

it’s as simple as that! To make it more interesting, it is called each frame with the shield throbbing  by increasing  the radius from 38 to 46 pixels by 2 then restarting at 38 again. Here’s the code for that. Note that when the shield energy is below 10, it no longer works.

void DisplayShield(SDL_Rect * target) {
	if (shieldFlag && shieldStrength >10) {
		SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff);
		DrawCircle(renderer, target->x + (SHIPWIDTH/2), target->y + (SHIPHEIGHT/2), shieldRadius);
		shieldRadius += 2;
		if (shieldRadius == 46) {
			shieldRadius = 38;
		}
	}
	if (shieldStrength < 100) {
		TextAt(target->x + 10, target->y + 58, sltoa(shieldStrength), 0.67f);
	}
}

The number under the player ship is the shield energy which drains while the shield is being displayed and recharges back up to 100 when you take your finger off the shield button. The number is only shown when the value is less than 100.

Open source repositories are worth a trawl

Open source repositories are worth a trawl

Repository
Image by Jagrit Parajuli from Pixabay

How many C language projects do you think there are on GitHub? If you put language:C in the search box, you get the astonishing figure of 980,752! That would take some trawling through.

There are other search terms you can use as well. Click the cheat sheet link on the bottom left of the page and it will popup a form explaining how to filter for various things.  You are probably as well reading the GitHub search help. There’s a lot more to search there.

Confusingly the searches sometimes seem to come up with different values. I’ve seen it vary between 578,000 and over a million!   Add game to the search and there’s only 31,426 projects! Only…

Should you use #pragma once?

Should you use #pragma once?

Pragma as found by google image searchThe traditional way of using an include guard is to put all of the header inside a #ifdef like this.

#ifndef hr_time
  #define hr_time
  #include <linux/time.h>
  #include 

// rest of code here
#endif

However the modern way is to put this at the top of the header file.

#pragma once

And this seems supported by most compilers I’ve tried. Certainly Visual Studio C/C++, gcc and clang all work.

In fact when you add a new file and choose header in Visual Studio, it put the #pragma once in automatically for you!

Given that those three C compilers are the main ones I use, I much prefer this pragma and use it.  But I would be interested in hearing of any C compilers that don’t  use it.

 

Willing to take a Risc?

Willing to take a Risc?

One of the understated and wrong assumptions about the Raspberry Pi is that you can try any flavour of Linux on it but Linux is really the only OS.  It is true that most of the 20+ OSRISC Os that you can try are based on Linux but there are a few that aren’t.

  • Windows IOT Core – a limited version of Windows
  • Risc OS – Created by the inventor of ARM
  • RaspBSD – More Unix than Linux.
  • Chromium OS. Turn your Pi into a Chromebook.

Of these, I think Risc OS looks the most interesting. It certainly isn’t Linux and it reminds me of the Archimedes, the first ARM computer.

Risc OS is 33 years old, it started in Cambridge in 1987 and is a descendant of the BBC Micro OS. And yes they do have a BBC BASIC available.  There’s  lot more background to it on this page  but bear in mind that it and the comments date back to 2012. It has an  ARM C compiler called Norcroft.

I won’t be oing any more with Risc OS, but thought it worth a mention. All of the stuff I’m doing will be on Linux OS.

Not. ARM is in the news because Apple have just announced that they will be transitioning from Intel CPUs to ARM over the next year or two.

 

How to find files in Ubuntu

How to find files in Ubuntu

In this I’m looking for the SDL2 header files. These are installed when you install libsdl2-dev as we saw yesterday.

While you can do it from a terminal with the find command, I find it easier to do it from the GUI. We need the Files utility which you get by clicking on this icon on the left toolbar. files-icon Now click Other Locations on the left and you should see something like this.

On This computer
Click Computer. This will let you search through the entire Linux file tree.

Click on the magnifying glass icon and in the text box that appears type in sdl2 and press enter. It will spend a few seconds or minutes searching and then find a number of files/folders.

The first result was SDL2 and the usr/include is a big clue. Double-click on this and it will open on a folder full of header files!That’s what we want.SDL2 Folder

 

When you are configuring gcc/clang and want to add paths for include files like SDL2, it’s important to know where those files are located.

A large collection of ARM links

A large collection of ARM links

Circit board
From Pixabay

ARM being the CPU brand inside Raspberry Pis. This is a collection of talks and links to articles about the ARM architecture, concurrency, performance and way too much other stuff to list. There’s a lot in there and I defy anyone with the vaguest interest not to find something of interest.

I’m not a hardware person myself, but dipping into stuff like this can yield benefits. Remember the CPU in the Pi 4B has four cores. If you are writing software that just runs one one thread then it’s like driving a car with a four cylinder engine but its only firing on one. And if you manage to write software to use all four cores, do you know how to avoid false sharing? (Yes it’s a thing!)

 

 

 

A pleasant surprise – scrot handles multiple screens

A pleasant surprise – scrot handles multiple screens

Scrot screenshot of two monitosrMy Raspberry Pi now has both a 7″ touchscreen and a 24″ monitor working at the same time. Most work is done on the big screen but the smaller display is for testing. I’ve reconfigured it so the menu is on the bigger screen, it makes more sense.

For quick and dirty screen shots, I use scrot (the name is derived from SCReenshOT). I’m fairly certain this was included with the operating system as it was installed, it wasn’t anything I added. What I didn’t expect was that it would capture both screens and put them in the one image. It fills the gap with a copy of the smaller screen.

Scrot is fine for most stuff but I thought that if I wanted to capture things like menus then I was going to install another utility that offered a time delay but digging a bit deeper, it turns out that scrot can do that as well. As with most Linux programs,

scrot --help

Gives you a list of commands and there’s a -d (or –delay) NUM which pauses NUM seconds before doing the grab. Other options let you do a countdown (-c), capture with border (-b)  and you can even have it run another program (-e) on the screenshot. Handy if you had it running unattended on a cron job and wanted to email the grab.

There’s a bit more but I’ll leave that for you to investigate.

Goto is considered bad programming in C but

Goto is considered bad programming in C but

goto exit signThere is one case for using goto when you have nested loops and you’d like to jump all the way out. The goto statement was very popular in BASIC but often resulted in programs being like spaghetti with gotos all over the place.

Here’s a contrived example where I have used a goto.

#include <stdio.h> 

int array[10][10][10];
int main() {
    // Populate array
    for (int y=0;y<10;y++)
      for (int x=0;x<10;x++) 
        for (int z=0;z<10;z++) {
          if (array[x][y][z]==0)
            goto exit;
    }
exit:;
}

In my C programs, for-loops are the most popular followed by while-loops, do-while loops and gotos are hardly ever used at all.