Category: C

Some Pretty fractals in C + SDL2

Some Pretty fractals in C + SDL2

Fractals
Code by Misha

If you’ve ever wanted to draw fractals in C, here’s some code for you. These come from Misha on his(her?) GitHub page.  There are three as you can see. Each has their own program – barnsley.c, sierpinski.c and y-fractal.c. All three programs create an SDL2 window then display the image.

Note that this was coded for Linux. I modified the programs so they would run on Windows and the screenshot was done from my machine. I know these images are on the GitHub page.

If you want to compile and run, then on all three programs change the SDL include from

#include <SDL2/SDL.h>

to

#include "SDL.h"

Also with the barnsley.c you need to give initial values to mx and my in the renderFractals() function (lines 88 and 89) and change these two lines: (114 and 115). No other changes are needed to let them compile.


mx_c = mx - win_w / 2;
my_c = my - win_h / 2;
  to
mx_c = mx - WIDTH / 2;
my_c = my - HEIGHT / 2;
Beej’s Guide to C programming

Beej’s Guide to C programming

BookBrian Hall (aka Beej) has written or collected guides to several programming languages including one on C. Out of curiosity I pasted the one document HTML version into both Word and a text editor to get a feel for how big it is and it filled 416 pages on Word, almost 13,700 lines in a text editor.

It is quite comprehensive covering most aspects of C. He has a disclaimer that its a work in practice and there may be bits that need correcting but still, if you are looking for an excellent guide to C, I’d definitely recommend it and if you like it, send him a tip on Paypal.

 

A little C Puzzle

A little C Puzzle

Question mark
Image by Gordon Johnson from Pixabay

The following C program doesn’t output anything. Why?

#include<stdio.h>

#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = { 23,34,12,17,204,99,16 };

int main()
{
    int d;

    for (d = -1; d <= (TOTAL_ELEMENTS - 2); d++)
        printf("%d\n", array[d + 1]);

    return 0;
}

 

This came from this site by Gowri Kumar which has a lot more C puzzles. Answer in a day or two…

There’s always someone inventing something better

There’s always someone inventing something better

JSON Logo
JSON Graphical Logo

In this case, I’m talking about better than JSON. JSON was invented as an alternative to XML which was invented as a way for computers to send data in a human readable form. Unfortunately XML is a pretty bloated format. Putting data into XML can make it five or six times larger.

Plus by the time XML was coming into common use in the mid-late 90s, there was a horrible letter soup of associated acronyms. It sort of appeared at the same time as Java and the whole XML ecosystem was lapped up by big business. JSON was born as an alternative method and it became very popular as JavaScript was growing at the same time, JSON is short for JavaScript Object Notation.

Now there’s MessagePack an alternative to JSON. It’s supposed to be faster and smaller than JSON. I came across it while looking at SignalR, a way to send data between clients and servers and used in Blazor and .NET websites.  It must have been around for a while as it was used back in 2011.

There’s now 50 programming language implementations including C at the bottom of the MessagePack home page although there’s over 100 but that includes several implementations for the same language.

If you have to move data between two computers, or maybe two processes on the same computer, you want it to be as small as possible and that’s what MessagePack allows.

 

A mini-project- SDL toolkit

A mini-project- SDL toolkit

Coloured Rectangles
Image by Gerd Altmann from Pixabay

I’ve thought about doing this for a while. Build a small toolkit (a library) of helper routines for any program that uses the SDL2 library. That means it will have functions to do the following:

  1. Draw horizontal and vertical lines in a specified colour.
  2. Draw coloured rectangles both filled in and empty.
  3. Draw Circles of specified radius and colour.
  4. Draw hexagons in either orientation and of a specified size, hollow or filled.

Plus any other things that occur to me. I’ll start on this shortly.

Games in C on GitHub

Games in C on GitHub

Tetris lamp
Image by Tobias Kozlowski from Pixabay

If you visit this site often, you’ll find many many links to projects on GitHub and not just mine. One of the thing I like about GitHub is how easy it is to search for stuff. So I put games in the top left search box and then ticked the C checkbox.

That returned 2,569 results but not all are playable games. Some are one off projects for other hardware such as Wii, GameBoy, Switch and similar. One gem I found was BSDGames. This is quite old and the 43 games are text based including the original Adventure.  I get the feeling many of these are from the Basic Computer Games books but at least these are in C not BASIC.

These will probably compile better on Linux and with difficulty and lots of fixes on Windows. And yes there is a Tetris game in the collection!

 

DungeonRush – open source C + SDL 2 game

DungeonRush – open source C + SDL 2 game

DungeonRush gameI continue my quest, looking around for open source games in C that use SDL 2. The latest one is DungeonRush by developer Rapiz1 (does no one ever use their real names these days?) who hails from Wuhan.

It is not a rogue-like, but more a Snake-like game.   You move your hero around the playing area avoiding monsters and things fired at you while picking up stuff and people to give you extra lives.  As well as the difficulty levels it includes multiplayer mode as well though I haven’t tried that.

If you are learning C or games programming, it’s worth studying to see how others do things; this includes its own text drawing and high scoring and saving high scores.  It’s quite fun to play though I am rubbish playing it, even at the normal level. I’ve added this to the C Code links page. (Accessed on the top menu),

Snake games are interesting because if you use a circular buffer, no matter how big the snake grows, you can move it by just moving the head and tail elements. An O(1) operation!

Handle with care – C programming

Handle with care – C programming

Warning sign
Image by Clker-Free-Vector-Images from Pixabay

If you had programmed in say BASIC before learning C then you’d be used to a world where the worst that could happen might be a divide-by-zero crash or a hang-up due to an infinite loop. But comes to C and it has a whole raft of undefined and unexpceted behaviours.

Just how big is that list of undefined behaviours? Would you believe 193? A developer with the user name Earnestly on GitHub has compiled a list of 193 undefined behaviours. MInd you, it came from a 2007 draft C99 specification which you can find here. Note, if you scroll up a bit you can also find a list of unexpected behaviours such as what happens when you call malloc with 0 as a parameter.

Even more interesting take a look at 5.2.4.1 Translation limits which is a list of maximums in the document. Such as 1023 cases in a switch statement. or 127 nesting levels of blocks or 63 nesting levels of conditional inclusion. If you’ve ever wondered how nested #include files can go, it turns out it’s 15.

I wouldn’t bother trying to learn all the undefined or unexpected behaviours. You just have to know how to write code that doesn’t make any assumptions for instance when a signed int overflows what happens? Or if you add an unsigned int to a signed int? Best if you can, not doing these things in the first place. If you do, it might work ok for you on your computer but if someone else runs it on a different system. it may behave quite differently.

 

Tutorial 14 on working with strings published

Tutorial 14 on working with strings published

Code listing on screen with keyboard
Image by Markus Spiske from Pixabay

Reading the C reddit most days, I see from time to time question about strings. They’re not particularly complex but I feel you really have to get your head around pointers to grok strings. I’ve already done a C tutorial on Pointers and c-strings but I thought showing some examples of doing things with c-strings would not go amiss.

The pointers aspect of C strings probably muddies the water a bit. All you are doing is manipulating a contiguous block of characters in RAM that ends with a 0. The pointer just tells you where in RAM that block begins. So long as it finishes with a 0 (\0 in C), it will work ok.

That’s what Tutorial 14- working with strings is about.  Especially for beginners, doing things like converting ints to strings, or concatenating strings can be a bit fiddly. I’ve also provided both the Windows and Linux versions. i.e. compiling with Visual C on Windows and clang on Ubuntu.

For instance, you might have heard about the long to ascii function ltoa. Bad news. It doesn’t exist in Linux compilers.  There is a Windows version with the instantly memorable (not) name _ltoa_s, which is one of the safe C functions.  Incidentally if you are using SDL then there is a SDL_ltoa function provided for you although oddly it’s not documented.