Category: C

Kilo- a thousand lines text editor in C

Kilo- a thousand lines text editor in C

Antirez kilo text editorDeveloped by Salvatore Sanfilippo aka antirez and licensed under the BSD 2 licence, kilo is a simple text editor in one file.

If you are learning C and want to see how to write a utility, this might be a good example to follow. Warning though he does use pointers so make sure you’ve learnt them first!

I had a stab at writing one quite a few years ago but it wasn’t very good. I have a suspicion that writing a good text editor depends upon you first creating a good implementation of the text storage. Solve that and it’s downhill for the rest.

I’ve added this to my curated library of C code, on the C Code link on the top menu.

 

Onslaught (aka Slay on Linux) tutorial two published

Onslaught (aka Slay on Linux) tutorial two published

Onslaught linux hexagons screenI’m quite pleased with this. It took about six hours in total to create including the time to create the graphics. Running in Hyper-V under Ubuntu 20.04, it draws a screenful of graphics in about 65 microseconds.

I took the hexagon drawing code from the AboutEmpire.zip code on GitHub and modernised it for SDL2. The Empire code uses Surfaces from SDL1 while this uses Textures from SDL2.

Orange hexagon Blue hexagon There are nine hexagons with all but the dark one having an internal border.

I think the orange and salmon hex look a bit too close, so I’ll change one of them.

The tutorial goes into a bit more depth. about the program (which is just over 200 lines long) and can be found on GitHub in the file Onslaught1.zip.

Doing 3D Math(s) in C

Doing 3D Math(s) in C

Donuts.cIf you’re interested in seeing how 3D math(s) are done, this article by ex Googler Andy Sloane is about a program he wrote called donut.c. The name is doubly appropriate as not only is the source code shaped like a Donut (doughnut for us Brits), when run on Linux, it outputs an animated 3D donut. This was an attempt at obfuscated C back in 2006.

Now 15 years later he’s updated it to work without using the math library. Interesting is the explanation of how he did all the mathematics without using sin and cos directly. Also it includes a version that uses 10 bit integer arithmetic; no floats at all. If you’re interested in understanding how Donuts.c works, read this post from 2011.

I like articles like this- they’re a bit different and add to programming knowledge. and you can never get enough of that

 

 

 

New Tutorial – Implementing the Windows game Slay

New Tutorial – Implementing the Windows game Slay

Slay gameOne of my favourite casual games is Slay by Sean O’Connor. I bought the full game which sells for $10 and it’s still selling well via his site or Steam. His game is only for Windows so my reimagining (great word!) will be for Linux only including Raspberry Pi.

His game comes with several hundred islands, in four sizes from very small, small, Large and very large. You play against 6 computer players with intelligence ranging from very stupid, stupid, clever to very clever. In addition there are a bunch of predefined maps like Britain or even the world.

Troops exist in four sizes with each size three times bigger than the one below. Your territory must be big enough to support the troops there and if it isn’t they all starve.  Splitting an enemies’ territory to starve his troops is a valid tactic.

Add in with trees which can spread and waste land which spreads even faster and its a cute addictive game.

My game will be called Onslaught and use its own graphics. Islands will be computer generated and hopefully the AI will play a decent game. The first tutorial which just explains the game and the scope of what I intend to do is now available.

 

 

Atoms tutorial three- finishing the game off

Atoms tutorial three- finishing the game off

Atoms 3 screenshotI fixed the bug in the Atoms game and the third tutorial finishes it off. The source code (in this case atoms3.c) has been placed on GitHub and the final version is about 300 lines long.

As well as the bug fix, I added a line to show the number of cells owned by the player and the computer. You can see it after the player move (Player : 41 Computer 16) and after the Computer move.

This whole atoms game was an experiment to see if I can introduce C programming without having to go overboard explaining every statement etc as I’ve done in the past. I think I’ll get back to programming games!

 

Tronus Project 1945 – Coursework

Tronus Project 1945 – Coursework

Project 1945A university student by the pen name of Tronus has put a project from his 3rd year in his degree course on GitHub. The screenshot shows the game running on my PC. The build folder includes a release folder with game exe plus all dlls and game assets in a resources folder below that so you can run it immediately on Windows. ,

I vaguely remember playing a similar game (a vertical scroller) in arcades back in the 1980s. That game was 1942.

The 1945 game has been programmed in C and uses SDL2. As always I took a walk through the source just to compare how it was done say compared to my own efforts. One thing I noticed is that he uses a fixed delay of 11 milliseconds each frame in the game loop by calling SDL_delay(11) rather than setting up the video to sync to the fly back. Is this better or worse? I’m not sure.

I think I prefer my method in case something takes a bit longer. Having a fixed delay means that you could risk overrunning the 16.666 milliseconds allowed per frame. That could never happen with my asteroids game as syncing to the fly back means my game can take as long as 16.66 milliseconds per frame and still not overrun.

 

 

Answer to simple C Question

Answer to simple C Question

Answer sign
Image by Gerd Altmann from Pixabay

Hopefully you got choice 2. 62. Here’s the question again.

#include <stdio.h>
int main() {
	float* p = (float*)50;
	p = p + 3;
	printf("%u\n", p);
}

The first line in main() creates a “pointer to a float” p with the initial value 50. This 50 is the value of p i.e. the address that p is pointing to, NOT the value at that address P is pointed to.

Adding 3 to p moves the pointer by 3 x the size of a float. As it’s unspecified, this size of a float is typically 32-bits or 4 bytes. So this line adds 12 to p. It now has the value 62.

You can try this out and step through it on the Python Tutor. Despite its name, it will let you run run programs in Python, C, C++, Java, JavaScript, TypeScript and Ruby. It compiles C with GCC 4.8 and uses a Valgrind Memcheck plugin. There are a few restrictions its quite a nice way to show off execution of simple programs.

Simple C question

Simple C question

question marks
Image by Gerd Altmann from Pixabay

I saw this this morning and got the correct answer. I also tried it just to be sure in Visual Studio and apart from a warning, it compiled ok and ran and gave the correct answer.

#include <stdio.h>
int main() {
	float* p = (float*)50;
	p = p + 3;
	printf("%u\n", p);
}

If you want to avoid the warning, change the last line to

	printf("%u\n", (unsigned int)p);

So the question is what does this output?

  1. 53
  2. 62
  3. 66
  4. 68

Answer and explanation tomorrow.

Next atoms tutorial added

Next atoms tutorial added

Atom2sThe first version of the program was 90 lines long but now it has grown to 275. This includes code to let the computer play and checking code plus I’ve refactored it a bit, simplifying the code.  I haven’t extensively tested it and at least one bug has crept in. Occasionally the computer seems to claim ownership of a player cell. This stops you adding one to that cell so its not good.

You are welcome to have a look at the code and fix it. I will in a few days. The code is on GitHub and is a single file atoms2.c. As before its been compiled with Visual Studio 2019 Community edition though it should compile more or less unchanged with gcc, clang etc. It runs in a terminal/command line. You can see the computer playing in the screenshot.

I’ve also added the tutorial for this.

Program a NES game in C

Program a NES game in C

BNES Marioack in the mid 80s I was busy writing games for ZX Spectrum, MSX and CBM-64 and also porting games between 6502 and Z80. That was also when the original NES appeared.  That had a 6502 CPU but a lot less RAM than the CBM-64.

In those days the NES was programmed in assembly language. C was still too new for most things but now, it’s possible to program a NES game in C. Developer Douglas Fraker has produced a series of tutorials on how to create NES games in C using cc65, a C compiler/assembler that takes C code and outputs 6502 assembly language.

His tutorials cover the whole range of things including sprites, how palettes work, music, sound effects, even how to use the Zapper (a stand alone gun-like accessory) with all source code on GitHub.  You can run games on an emulator such as the one that comes with RetroPie.

Reading the page on 6502 ASM Basics brought it all back to me even though it’s 31 years since I last wrote 6502 code.

 

.