Category: C++

A look at a Raspberry Pi Pico

A look at a Raspberry Pi Pico

Raspberry Pi Pico
From Raspberrypi.org

As you probably know I do like my Raspberry Pi. But the RPi Pico is a different kettle of fish. I’m only mentioning it here because it is programmable in C/C++ and some may find it a less say overwhelming place to learn C than say a traditional Raspberry Pi.

What’s different between a Pico and a Pi 4B? A Pico uses a microcontroller- basically a CPU with built in RAM, bit of flash RAM, real time clock. RAM is tiny compared to any Pi. Just 264 KB (That’s still much more RAM than my CBM Vic-20 in 1981 with 3.5 KB of RAM!) and 2MB of Flash RAM. The CPU, an ARM CPU designed in the UK runs at clock speeds up to 133 MHZ. A Pi 4B runs at 1.5 GB, over 11x faster.

The biggest difference is that a Raspberry Pi runs any operating system you want. Microcontrollers are different. To run a program on a Pico you have to program it into Flash RAM first. You can do this with drag and drop. See here for C/C++. The Pico is an embedded system. RAM is used for data, stack etc but not the program which runs out of Flash RAM.

But if you like hardware then this is an excellent place to get started. You get all these (see here for Specifications).

  • 26 × multi-function GPIO pins
  • 2 × SPI, 2 × I2C, 2 × UART, 3 × 12-bit ADC, 16 × controllable PWM channels (PWM I’m guessing is pulse-width modulation).
  • Accurate clock and timer on-chip
  • Temperature sensor
  • Accelerated floating-point libraries on-chip
  • 8 × Programmable I/O (PIO) state machines for custom peripheral support

So what about games? Not really. Or at best very simple games using the single LED. No, this is about learning C (or C++ or even- shock – Python) and interfacing hardware.  You might for example put one of these inside a drone to provide control software.

Fascinating online WebGL

Fascinating online WebGL

WebGL RabbitsWebGL (Web Graphics Library) is a JavaScript API for rendering interactive 2D and 3D graphics in a compatible web browser without the use of plug-ins. A developer called Todd Fleming has created a webpage where C++ programs (and maybe C?) can be compiled online by Clang and then run in the browser.

The colourful rabbits (numbering approximately 30) in the screenshot are rendered in real-time and rotated and moved (transformed). Each rabbit is actually each made up of 66,848 triangles.  Just click the (WebGL- Flying bunnies)  link on the right-hand side to load the 335 lines C++ source program then hit the compile button. After it has compiled in a second or so hit the Reboot/Run button on the right-hand side to start it running.

You can select all the source code and copy/paste it into a text editor if you want to examine it. You even can save out the compiled wasm (WebAssembly) file if you really want to though as it’s binary, it probably won’t mean too much unless you have a viewer.

 

 

C++ Template Metaprogramming Game

C++ Template Metaprogramming Game

Snake
Image by OpenClipart-Vectors from Pixabay

Templates in C++ are a useful feature. Without them you’d not have template functions, or more usefully template classes like vector etc.  But there is an even more useful feature called template metaprogramming. It’s a very advanced and clever feature; one I have never done and I don’t think anyone could say they’ve mastered C++ unless they are good at it.

Here’s an example of the simplest example I could find. It comes from here and what it does is generate factorials of numbers at compile time. So when you run it it comes back with the answer immediately.

// factorial.cpp

#include <iostream>

template <int N>                                                                 // (2)
struct Factorial{
    static int const value = N * Factorial<N-1>::value;
};

template <>                                                                      // (3)
struct Factorial<1>{
    static int const value = 1;
};

int main(){
    
    std::cout << std::endl;
    
    std::cout << "Factorial<5>::value: " << Factorial<5>::value << std::endl;    // (1)
    std::cout << "Factorial<10>::value: " << Factorial<10>::value << std::endl;
    
    std::cout << std::endl;

}

But if you think that is clever how about a game where every time you compile it, it makes a move and remembers the move between turns? A developer called Matt Bierner has developed a snake game using template metaprogramming.

So I downloaded it into my Ubuntu, installed Clang and clang tool just for good measure and compiled. This is the output. Iv’e snipped a lot out after the first two. Ot’s very clever, in this case, not much use but I doubt if there is any other programming language in which you could do this. The compile plays the game, running it just outputs the results. Yes it’s not exactly practical but still…

david@davidvm:~/STT-C-Compile-Time-Snake-master/stt-snake$ clang++ -std=c++1y main.cpp -o snake ; ./snake
------------------
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺▶*╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
------------------
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺*╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺▶▶╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
....
-- You Are Dead --
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
*╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺▼╺╺╺╺╺╺╺╺
╺█▲╺╺╺╺╺╺╺
╺▼▶╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
Star Ruler 2 – Open Source 4X game

Star Ruler 2 – Open Source 4X game

Star Ruler 2This is a 4x game (Explore, Expand, Exploit, Exterminate) .  “Select from one of seven races – or craft your own – to explore dozens, hundreds, or even thousands of systems in a galaxy of your choosing. Expand across unique and varied planets and ultimately exterminate – or subjugate – any who stand in your way either in offline single player or up to 28 player multiplayer.” as gog.com put it.

But as well as being available on Gog.com or Steam, it’s also open source but that doesn’t include the music from the game.  So you can pay for the game on Gog.com/Steam or download the open source version and build it yourself.

The GitHub website contains the full source code needed to build Star Ruler 2, and all secondary scripts, data files and assets required to run it. It’s 45% C and 22.5% C++ according to GitHub with a sprinkling of Flash (arr ar- saviour of the Universe- er sorry).

 

 

A list of open source physics engines

A list of open source physics engines

Chipmunk Color matchIt’s not uncommon to have 2D games (and 3D) incorporate a physics engine. So when objects move and hit each other they behave realistically. The code that deals with “physical” interaction, objects bouncing or rolling off other objects is usually all parcelled up in a game physics engine.

Doing that means the programmer doesn’t have to worry about objects interacting. Your character moves into a room and knocks a vase; the vase falls over and breaks. Imagine how complex it would be if you had to program all the interactions. Instead, all objects in the room are predefined. As objects move and hit other objects they behave according to the predetermined rules. Balls drop to the floor and bounce. Breakable objects break.

An indie game studio called Tapir Games has put together a pretty comprehensive list of open source game physics engines. There’s even a couple in C though many are programmed in C++, C# and so on.

The picture comes from Chipmunk color match, one of the games using the (C library) Chipmunk physics library.

A portable Windows Devkit

A portable Windows Devkit

Tool icons
Image by mohamed Hassan from Pixabay

I spotted this the other day. A C and C++ development distribution for Windows called w64devkit. It’s less than 80 MB and you don’t need to install it so you could easily fit it onto a USB memory stick or download it.

As the author (Chris Wellons) says “Despite its simple nature and small packaging, w64devkit is almost everything you need to develop any professional desktop application, from a command line utility to a AAA game”. Note it doesn’t include source control, nor does it access the internet, though no doubt you could. So you could backup stuff say to GitHub or wherever.

I am fan of software like this that you can take with you on a laptop or a memory stick. You don’t always have to have a full dev system with multiple monitors, Visual Studio etc.

If you’re interested, do read the entire blog post.

 

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.

Added the sources of another game

Added the sources of another game

Basic Computer games bookThis was a text mode game, my idea being to do something like the old Star Trek BASIC game but better. I called it Star Empires and it’s on GitHub, just follow the link to GitHub on the C Games sources link.

The zip file includes both C and a C++ version. Both will compile with Visual C++ 2019 Community edition. There is a minor compile error, a = that should be == in the C++ source. I ill get round to fixing it and uploading a replacement.

I do get a bit of pleasure (more like an exercise in masochism!) converting old BASIC games to C. I’m looking at the two Creative Computing books (and while 95% of the games are not exactly great (well lets be generous and say they were good 45 years ago!) but there’s one or two that might be worth the effort! I bought these in 1982 lost those and then bought them a few years back again.

Plus when you think how the games industry has matured since the late 70s and 80s, and many of those early programmers will have cut their teeth on games like that before learning more advanced stuff, so  lets not be too harsh!

 

Bit of an oddity with VS Code

Bit of an oddity with VS Code

When I first started using it, the C++ extension, and configuring for C++, I got a tasks.json one which was suited for gcc, but recently when I install it, (and the C/C++ Extension for Visual Studio Code, the only choices seem to be these. What happened to the the ones for clang/gcc? The one on the right is what I’m expecting. Even with a C/C++ file open as the instructions here say, I’m getting the one on the left.

VS Code ConfigureVs Configure C++
It’s possible that I’m getting this because I’m using the headmelted and VsCodium versions on a Raspberry Pi.

There’s a bit of a question mark about using the official extension on non-official build of Visual Studio. Headmelted allows it, but VsCodium has its own marketplace.

It’s easy enough to copy tasks.json over so not really a problem but just a minor irritation.

John Conways Game of Life

John Conways Game of Life

Golly - Life simulatorAn English mathematician John Conway (who died not that long ago) came up with a very simple cellular automaton that he called Life. This was back in the 1970s and I remember finding his original article in Scientific American while at University.

We had no internet then and I whiled away 10 or so hours trying to make my version of Life run faster. Given that this was 1978 and it was written in BASIC, it’s not surprising that it only did a couple of generations per second on a mainframe. They didn’t give us much CPU time and it was an ICL 1900. My iPhone is probably more powerful!

The rules are simple enough to implement but it’s unlikely you’ll outperform Golly which is what the image shows. That’s written in C++ and has been under near continuous development for the last 15 years.

But part of the fun is writing your own life simulator and watching the patterns explode. I’d call it the minecraft of its day given the amount of computing time spent on this since the 1970s. There are some amazing creations all following these three simple rules.

  • Any live cell with two or three live neighbours survives.
  • Any dead cell with three live neighbours becomes a live cell.
  • All other live cells die in the next generation. Similarly, all other dead cells stay dead.

The grid is just a simple bit field. Each cell is either on or off and the rules determine if new cells are created or if patterns die out.

There are innumerable ones on the web. Here for example is a C/SDL version. Note, it uses SDL1. When I get the time, I’ll build and run it. Comments are in French!