Month: November 2022

I know a fair bit of C but

I know a fair bit of C but

The letter C in fancy script
Image by Gordon Johnson from Pixabay

There are a lot of subtleties that you only pick up with experience.  I was pleased to find a blog entry by a bloke Tom M on “Everything I wish I knew when learning C” which is well worth a read.

I’m not going to copy anything from it except for this little snippet below one below. For the rest, you’ll have to read his blog post.

char signedness

All other integer types default to signed, but bare char can be signed or unsigned, depending on the platform.

As said char is an integer type but unless you are doing stuff like ++ or — when its value can overflow according to being signed or unsigned, it’s not really a problem. And unless you are writing code that runs on wildly differing platforms, you can probably safely assume that a char is the same as a byte and has 8 bits. On those other platforms, I’d suggest you read the answers to this Stackoverflow question.

Update to the Windows eBook

Update to the Windows eBook

Visual Studio configurationA reader asked me how to setup SDL2 fo Windows given recent changes in SDL2. Specifically the files and libsdl projects have been moved from the libsdl.org website to GitHub. You can easily find SDL2 Image, Mixer, TTF etc.

However it can still be quite daunting setting up Visual Studio for SDL2. You have to download the specific files, then configure the project properties to specify the include paths for header files and then the lib files, both the path to them and identify the ones you want to use.

As I’m on my new PC, I bit the bullet and went through the process of setting it up. It took just over an hour to configure it.  I’ve put it into a PDF that’s a couple of pages long.

So the game works but only after I disabled the sound code; it was failing in the call to Mix_OpenAudio(). I think recent work on the SDL Mixer needs some work on my part. I need to sit down and look at the SDL Mixer page and figure out what’s failing. Once that’s done, I’ll update the files.

 

Designing a Rogue-like game for book two

Designing a Rogue-like game for book two

My 2nd eBook is going to be a bit like the first one- first it teaches C but it is oriented towards Raspberry Pis.  Then it shows how to program Asteroids (like the first book) followed by a Match three game and finally a Rogue type dungeon explorer game.

Rogue like game

Dungeon assets from itch.io
Dungeon assets

Now some rogue-likes stay true to the originals which were text based. Like this above from an article on Wikipedia. Now that’s ok, but I fancy something a bit more colourful so I’m going to use 16 x 16 pixel graphics.

I did a bit of searching and found these dungeon graphics on itch.io. I’ll use those to start with but may change. The view is not quite top down but top down with a bit of isometric in it. It looks better than pure top down.

So I figure the game has to have these elements.

(1) Generate a dungeon with multiple rooms and levels and stairs between,

(2) Fill the dungeon with monsters and treasures.

(3) Move the player’s character through the dungeon fighting monsters, picking up things including keys and maintaining a small inventory.

(4) Add some side quests like needing to find keys to open a chest with a magical item.

(5) I’ll also need to devise a simple combat system with magic.

Do monsters move- you’d hope so. How are rooms connected, in fact how are dungeons generated? I created a dungeon generator for the postal game Quest 30 years ago and can remember how I did it so there’ll will be that to implement in C.

I’m also going to leave out things like needing food and water.

So there it is now. Now to start writing the software. I’ll update this with progress reports.

++/– Operators work on floats and doubles

++/– Operators work on floats and doubles

laptop computer with an editor
From Instant Images

No matter how much I use C there are still things I thought I knew but find out otherwise. The latest one is that you can use ++ or — on float and double variables.

Here for instance is a short nonsense program that I compiled and ran with Visual Studio on Windows. As you’#ll notice, it increments and decrements floats and a double.

The danger with using this is that you might get rounding errors. When I ran it on Windows, I didn’t which surprised me. I will have to try this on Linux.

#include <stdio.h>

void main()
{
    float c = 1.0f;
    float cin = 10000.0f;
    double d = 2.0;
    int i = 0;

    while (cin != c)
    {
        i++;
        cin = c;
        c--;
        d++;
    }
    printf("%i %e %e\n", i, c, d);
}