Month: March 2021

Nearly finished translating asteroids to C++

Nearly finished translating asteroids to C++

Asteroids screenshotI always wanted to do this and have most of it done. It just needs a bit of polishing plus making it cross-platform. It wasn’t the hardest  thing I’ve done although I did start by trying to make the common part for asteroids, player ship, bullets and aliens, the bit that managed position and velocity into a base class.

I then spent a day wrestling with the compiler trying to access this in those methods that used this and in the end found it easier to make it into its own class and had an instance of it in each of the classes. I.e. using composition rather than inheritance.

Rewriting in C++ made things like saving high scores to disk and reloading a bit simpler using C++ strings.  Now I just have to get my main PC up and running and then polish the code and publish it on GitHub.

 

 

 

Looking at C/C++ extensions for VS Code.

Looking at C/C++ extensions for VS Code.

VS Code C++ extensionsI was curious as to how many C extensions there are for VS Code. If you visit the marketplace (not a great name- all are free-some market!) in a browser, you can search through the (currently) 24,779 available extensions.

Finding C extensions is not easy. A search for C returns almost 16,000 results. C++  is a better thing to search on and gives 207 results, many of which are for C and C++. You can also search in VS Code but it’s easier in a web browser.

Even that’s probably too much but you can use the showing pull down to see how many extensions are in the various categories. If for instance you select Debuggers, then you will only see 18 extensions.

VS Code Extensions showing

Note: As I’m still only my old creaky Ubuntu laptop, I had to use scrot for screen capture and gthumb for editing the image.  Note, the scrot project is looking for a programmer to look after it. Here is how to contribute to the project.

Another Minecraft game in C

Another Minecraft game in C

MinecraftIf you remember back in November I mentioned a Minecraft server that was written in C.  Well now there’s another one that has appeared. Just lIke the other one it uses SDL2 and OpenGL and includes full source code.  This one uses clang.

It’s cross-platform for Windows and Mac and there are two different binaries, one for creative mode and one for survival mode.

It’s still a work in progress and needs sound effects and music, saving and loading levels and multiplayer to complete it. If you are learning C and want to see how a game like this is programmed, download the source code from GitHub and start studying it.

Interesting programming exercise- no loops

Interesting programming exercise- no loops

Recursive fractal
Image by Pete Linforth from Pixabay

I read about a university course where the students had to write C code without using For, While or Goto for loops. Instead they had to use recursion.

Yes it’s bound to be inefficient compared to the looping structure but that’s not the point. For instance for a loop like this:

for (int i=0;i<10;i++) {
   DoSomething(i);
}

It could be done as

void DoSomething(int i) {
   if (i==10) return;
  // Do whatever here
   DoSomething(i+1);
}

DoSomething(0);

The point isn’t to teach you to write inefficient code but to think a bit differently. I’ve used recursion quite extensively in the Slay tutorials code. Here’s an example. It sets a field continent in all adjacent land squares where continent == -1.

void FillIn(int x, int y, int island) {
	if (onMap(x, y)) {
		if (island == map[x][y].island && map[x][y].continent == -1) {
			map[x][y].continent = numContinents;
			allContinents[numContinents].count++;
			FillIn(x - 1, y, island);
			FillIn(x + 1, y, island);
			FillIn(x, y - 1, island);
			FillIn(x, y + 1, island);
			FillIn(x - 1, y - 1, island);
			FillIn(x + 1, y - 1, island);
			FillIn(x + 1, y + 1, island);
			FillIn(x - 1, y + 1, island);
		}
	}
}
So what do I think of C++ compared to C?

So what do I think of C++ compared to C?

Software
Image by Gerd Altmann from Pixabay

I learnt C++ 30 years ago (1991)  and C about 10 years ago.  Mind you I’d had several years of Pascal by then including some OOP (Object Oriented Programming) so it wasn’t that big a thing to learn C++ after Pascal.

For me it’s the objects and the template structures like vect that make the difference. Most C programs that I’ve written don’t really progress beyond using an array of structs. Asteroids, which at 2,200 lines long used a few of those but that was it.

In fact if I wanted any more complicated data structures in C programs, I’d either have to use a 3rd party library or roll my own using pointers.

Programs I’ve written in C# probably use List<Class> more than any other data structure with Dictionary<string,Class> a close second. It really depends upon the type of program you are writing. A lot of mine are reading from a text file or database file, holding data in memory then outputting results.

C++ offers more advanced data structures than C and I spent a fair bit of time rewriting Asteroids replacing all the array of structs as arrays of objects.  Inheritance isn’t that big a thing in OOP but it was handy here because I was able to initially have Asteroids, Space Ships, Bullets and Aliens classes all inherit from a moveable base class. That class had all the code for rotation, movement etc. I found though that C++ could be a real pain when trying to do comparisons between different superclasses and eventually switched from inheritance to composition so those classes had a moveable object instead.

The downside to C++ is remembering if you are copying or moving objects. Not a problem you have in C. One of the interesting problems is how you track the number of active Asteroids which can change from frame to frame. In C I used a fixed array of structs with a field showing whether it was active. Using a vect though and pushing and popping asteroid objects would probably take longer.

Core War in C

Core War in C

Core WarCore war is an old game concept that dates back to 1984. If you imagine a simple CPU with small programs written in assembly language trying to wipe each other out, that’s Core War and you can read about it in a lot more detail on Wikipedia.

Inevitably for a game of that vintage there is a C implementation on GitHub by developer Emil Wallner. There’s an animated version of the screenshot showing the game play in action.

The programs (representing viruses) are small assembly language programs that move themselves around the virtual memory and duplicate themselves and try to wipe out the opposition. I’ll always meant to write one of these myself but for now there’s this.

My ideas for standards in C programs

My ideas for standards in C programs

Follow the rules...
Image by Gerd Altmann from Pixabay

As I’m somewhat restricted at the moment, bashing away on a 7 year old Toshiba laptop running Ubuntu, I thought I’d look around for some C games to help keep the blog going. There’s no shortage of games in C, even those that use SDL2 but what I’ve found is they can be a real pita to compile.

I guess most are for Linux and SDL but I’ve found things like the include path for SDL header files is one pain point. VS Code has a Replace in Files menu item so it’s not too difficult to change paths in multiple files.

Another difference I found was putting inline functions in the middle of a function. I didn’t know you could nest functions that way. A bit of digging found that good old gcc allows this as an extension but it’s definitely not normal C and clang doesn’t allow it which is what I tend to use.

My C standards

So my modest standards for writing C code are, to make life a bit simpler for porting…

  1. Use conditional compilation so you can compile on Windows and Linux. This includes paths to SDL, string print to buffers, MSVC _s functions (on Windows). Please compile on both platforms; it’s real easy to break it!
  2. I’ll eventually use this so that the timer routines I use will be one set of files that can compile on MSVC/clang.
  3. No nested functions. Yes I know gcc can do it, but its just unnatural. Use recursion if you must or function pointers but not nesting. If you really want to nest functions, program in Delphi or C#.

Other things I’d like are mostly taste, like putting a comment for every function.

Things that don’t really matter are whether you put function definitions before main() or after. If you put them after then you have to include a definition.

Likewise I don’t care if you use #pragma once or the older compile guards.

It’s probably a good idea to not call any of the GitHub banned functions. Remember who owns GitHub! (the same people who added the _s functions in Visual C++/MSVC…)

My PC is broke… plan b

My PC is broke… plan b

Linux Tux
Image by FreeCliparts from Pixabay

I repurposed my wife’s old Toshiba laptop a year ago, installing Ubuntu 20.04 LTS on it to replace Windows 10. Today my Windows PC broke. It appears that the cooler has given up after five and a half years faithful service. The warranty of course was only for five years… I just have to make sure it is the cooler and not the PSU has gone and get it replaced.

I know it was overheating because I rebooted after 30 minutes and into the UEFI BIOS. There I watched the temperature climb at a rate of 1C every second. When it hit 75 C and the text turned red, I knew that it was overheating.

So I dug out the Toshiba and upgraded Ubuntu on it finally figuring out the password after 20 or so tries. Only 560 packages needed upgrading! That took a while and I’ve also been installing other software since. My VS Code was that old that it won’t actually update. When I did the Linux version of Asteroids I tested it on here.  It seems I have to download a recent version of VS Code which will let it upgrade with apt.

It shouldn’t but it never fails to surprise me how quick Linux is. This laptop has an I3 CPU and a hard disk and is about . It has 4 GB of RAM; I upgraded that a couple of years ago to try and speed up Windows. Even after upgrading Windows 8 to 10 and removing some of the Toshiba bloatware, it was never that quick. But now it flies.

Thankfully Chrome had the password to the site so I was able to login and write this from the laptop. I may have to do without my Windows PC for a week or two…

New Tutorial on getting started with SDL

New Tutorial on getting started with SDL

SDL REctanglesI had this tutorial pencilled in to do and it’s now done. It’s the old SDL rectangles program that was part of the eBook. I’ve modernised it a bit so the same file will now compile under either MSVC on Windows or VS Code/Clang on Linux without changes.

I used the _WIN32 predefined macro so it compiles the call to Windows cod on Windows. Here’s an example:

#ifdef _WIN32
		sprintf_s(buff, sizeof(buff), "%10.6f", getElapsedTime(&s));
#else
		snprintf(buff, sizeof(buff),"%10.6f",diff(&s));
#endif

I do a similar thing with the path to SDL which is “SDL.h” on Windows but “SDL2/SDL.h” on Ubuntu. Writing code this way makes it  lot less of a hassle.

Widening the scope of this website

Widening the scope of this website

Chaso
Image by levelord from Pixabay

When I first started, I wanted this to be for two purposes. Learning C programming and learning games programming. Apart from a few more tutorials that I have to complete, it’s mostly complete as far as C goes.  I still have to finish my 2nd e-book which is now up to chapter seven. I received some advice after my first eBook and so I’ve completely revised the second one to have a lot more explanation.

But after a year of doing mostly C stuff (and a bit of C#), I have found it harder to get things to write about on C.  I’ve been revising what I’m doing in games programming and so am looking about. I do like Blazor but am going to try an alternative approach. If I succeed, you’ll hear a lot more about it.

The picture probably represents the contents of my head!