Month: November 2020

How slow is SDL_TTF 2.0?

How slow is SDL_TTF 2.0?

FWhen  I created the Asteroids game, I deliberately didn’t use SDL_TTF instead I took a Monospaced font and saved it out as a PNG file which was loaded into a SDL Texture. I then created my own character printing routines by figuring out which character I wanted and then blitting it. The image shows the font I used zoomed in.

You probably can’t get much faster than that, although I wasn’t really doing that much output. Every frame, the score is output and if an asteroid is hit, the value of the hit scrolls upward for a few frames.

Also when you lost a life, it would print using a scaled up version of the font. The only problem with that is scaling up a bit map font just shows off the deficiencies of the font scaling as a bitmap, as the image shows. Not exactly smooth is it?

So I’ve decided to write a small program that uses SDL_TTF 2.0 and does high resolution timing to determine exactly how long it takes to draw text using a TTF font. The big advantage of doing that in a game is you can draw different sizes, weight (bold etc) and colour compared to a bitmap font.

But TTF is an interesting format, there’s a lot going on. Letters are drawn using mathematical equations so it is bound to be a bit slower than pure blitting and I’m interested in knowing just how long it takes. It’s all relative, if you view a page of text in MS Word (or just in Windows generally), Windows renders it pretty fast.  But its still important to know just how fast. For what I used text for in Asteroids, it probably could have used TTF text but in another game with more text it might be too slow for 60 frames per second.

If it was too slow then perhaps a hybrid approach might work. Figure out what text you’ll need, prerender it into bitmaps (when the program starts up) then use those bitmaps.

So I’ll publish the speed test program once written. Watch this space.

 

New C tutorial on implementing linked lists with pointers

New C tutorial on implementing linked lists with pointers

Links
Image by Денис Марчук from Pixabay

Once you leave the relative safety of arrays and structs, the linked list using pointers is probably the next thing to consider.  Technically it’s a liked list of structs containing pointers. It’s like an array of structs only instead of allocating contiguous memory for an array, you allocate memory for each struct as you need it.

Linked lists are easy to program. You have a head pointer (a pointer to the head of the list). It starts as null as the list is empty. There’s two types of linked lists (single and double). A single list has a pointer to the next node (or is 0 at the end of the list) and can only be processed from head to end. A node is just a fancy name for the struct in the linked list.

A double list has two pointers. One to the previous node and one to the next. As well as a head pointer you need a tail pointer and you can process the list from head to tail or tail to head (i.e. backwards).

Now the first operation you can do on a linked list is add a node to it.

To do this you

  1. Allocate memory for the node using malloc.
  2.  Copy the head pointer to the node’s next pointer.
  3. Stitch the node in by pointing the head pointer to this node.

So when you are building a list you add each node to the head, in a sense pushing it in front of the others.

Double linked lists have to do an additional operation which is set the next node’s previous pointer to point to the newly added pointer. And when you add the first node to a double linked list, you have to set the tail pointer to point to this first node. After that it never changes unless you have an Append node at the end.

Uses of Linked Lists

Anything that needs dynamic memory for example a text editor might use a double linked list to store all the text. Each line could be a different length. So each node would not only have a pointer to the next and previous nodes, it would have a pointer to the text in memory. When you insert a new line, you are just inserting a new node in the list at the current node that the cursor is on.

Or you might store a directed graph (a bit like in the picture) where each node has multiple pointers to other nodes.  Anyway I’ve published tutorial eight which looks at pointers and linked lists.

 

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 --
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
*╺╺╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
╺▼╺╺╺╺╺╺╺╺
╺█▲╺╺╺╺╺╺╺
╺▼▶╺╺╺╺╺╺╺
╺╺╺╺╺╺╺╺╺╺
A blog post worth reading

A blog post worth reading

Letter C
Image by Clker-Free-Vector-Images from Pixabay

A developer called Jakub “Jorengarenar” Łukasiewicz has posted a blog entry Best aspects of C language and I’m more than happy to link to it. It says much of what I think about C but expressed very nicely and its well worth the read.

In his post he refers to a document Rationale for International Standard Programming Languages C (PDF).  This is a 2003 document (C doesn’t change very often)  and not exactly a light read at a trifle over 200 pages long. It’s an insight and commentary into what C99 is about and also lists these five principles:

  • Trust the programmer.
  • Don’t prevent the programmer from doing what needs to be done.
  • Keep the language small and simple.
  • Provide only one way to do an operation.
  • Make it fast, even if it is not guaranteed to be portable.

If you are learning C this will provide you with explanations of why some things in C99 are what thy are. Like myself you will discover things that you didn’t know. For instance I didn’t know about long double. Or that in K & R C(Kernighan and Ritchie) that all floating point arithmetic was done at double precision but that was relaxed in C89.

 

More thoughts on the design of Rogue like

More thoughts on the design of Rogue like

Unicorn hackSometimes I walk up around 3:00 AM and my mind is abuzz with things like this. Last night was one such night. The first thought was I should stop calling it a Roguelike. There’s a certain set of conventions with those and I don’t want to be limited by that.

For instance here’s an article about things you should or shouldn’t do in designing Roguelikes.

The game (for the next Ebook) is not going to be an all singing all dancing version but will be an MVP in the parlance. That’s Minimum Viable Product,

So if I’m going to divert away from the ‘standard’ then it needs a name. Something evocative like Dungeon Trawler but shorter more catchy.  So I’m going with V.O.R. (Vaults of Ruin) or maybe just Vor,

So onto design aspects. Here’s a list of the features:

  • 10-15 different Monster types with differing melee, range weapons and magic. Can have packs of them, not just one. There is only one player. Should be fun!
  • Simple set of castable spells using a Mana value. It is used up casting spells but regenerates slowly as you move. It can be replenished quicker by consuming potions.
  • No food or water but the character has an energy rating. This is depleted by movement and fighting. Rest or sleep oe rating food replenishes it but I don’t want it to be a major thing. I’m not having deep dungeons with 25 or more levels, but 12 levels maximum.
  • Weapons are sword, dagger or bow. Can switch between them.
  • A few powerful magical treasures.
  • Turn based not real-time. Asteroids is real-time but movement will be restricted to so many squares for you and monsters. You probably won’t be able to outrun the faster moving monsters.
  • Limited visibility can make things interesting. It means the game has to calculate what squares are visible each move.

I was tempted to do away with grid movement; for instance in the Asteroids game, they can move at any angle and velocity and are not tied to a grid. But that might be a bit too complicated so movement will be grid limited and Up/Down, Left/Right and maybe diagonal.

The screenshot is from an open source (.NET Core/C#) web rogue game called Unicornhack.

 

 

Hyper-V VMs are not all the same

Hyper-V VMs are not all the same

Hyper-V Ubuntu installOne of the big problems with Hyper-V and Ubuntu in particular is the clipboard or lack of it. I had 18.04 LTS installed with an X Org RDP login. This worked perfectly and I could have a full screen in my Monitor and could copy/paste.  Don’t underestimate copy/paste.

It’s a real PITA if you have to use say WinSCP to copy files over. I think WinSCP is excellent BTW but the amount of labour saving that copy/paste has done since some genius thought it up is immeasurable. That and allowing the full screen of the monitor are two highly important things.

Sadly the 20.04 LTS didn’t seem to allow it. Copy/paste didn’t work between my Windows PC (host) and Ubuntu (guest). There’s nothing worse than losing a feature you’ve grown fond of.

If you follow these instructions for creating a Hyper-V 18.04, you get the screen size popup but not with 20.04 LTS. For that you have to follow these instructions!

It’s things like this that suggest why Linux Desktop has never been that successful. You can waste many hours getting simple things working and sometimes like Copy/Paste they break between versions. And this is with Ubuntu, probably the biggest and best known and supported Distro.

 

And my Hyper-V Raspi error

And my Hyper-V Raspi error

Scary apt messageSeems to be with Visual Studio Code. I said yesterday that it had got into a funny state. Well I created a new VM and installed the Raspberry Pi OS that runs in a VM and after it updated tried installing VS Code on it.

It would have had the same problem had I let it. The problem is it when you install VS Code, it has some unmet dependencies and in fixing them it wants to remove 8 essential packages and this breaks apt which then gets in a funny state.

No one wants to see this scary message! So I think I may have to use the Code.headmelted.com version on Hyper-V for a while and see if this gets resolved with the next update of VS Code. Ah the jots of software development…

 

When Raspberry Pi Goes wrong

When Raspberry Pi Goes wrong

Raspberry Pi TuxIt happened to me today, not once but twice.  First the Hyper-V version of Raspberry PI OS I have installed (handy for screenshots) didn’t update properly.  Apt got itself in a right state and attempts to fix broken packages on APT just made things worse. In the end, I fetched another copy from this page and installed that in Hyper-V and deleted the first one. I wonder if Raspberry PI OS will ever make it into the Microsoft Store as one of their WSL Linuxes?

At the same time I had forgotten to write down a login password for a real PI and burnt myself a new Pi SD CArd. After it did the initial update and reboot, it came up with BCM2835 Exception stack errors and hung. I burnt it again, went through the update palaver and it did the same. A quick Google and I found a suggestion that my SD Card was failing although it had burnt without error twice and switching to a different card fixed it.

Out of curiosity, I fired up Win32DiskImager and ran a verify against it and got this error message below. If you can’t read it says Verification failed at sector 8192. Well at least I now know and the SD Card was about to be binned and I took a last look at it.  4GB! I’d been burning a 7GB image onto it; no wonder it crashed.. I’m amazed it ever burnt and worked… I’d used it in the past for the DietPI distro which is much smaller.  Most of my spare SD Cards are 8 GB; that’ll teach me for not checking…

SD Card failed verification in Win32dioskimager

 

Now this is interesting – Interactive Fiction

Now this is interesting – Interactive Fiction

Choicve of Games Interactive FictionIt’s not really a thing done in C, although you could do. I came across a site that offers commercial Interactive Fiction. As they put it “text-based, multiple-choice games. ” and “By using text, we can interact with the imagination in different ways from a graphics-based game. We can also allow game designers to quickly and inexpensively produce games in comparison with graphics-based games.”.

Their games aren’t free but aren’t that expensive either typically $5-$8 or thereabouts and you can get them on mobile as well as via Steam. A recent game they’ve just launched has 1.2 million words of text (that includes the game scripts) which is phenomenal considering that most novels are 50,000-90,000 words.  Their games use their own open source scripting language called ChoiceScript which is a very simple programming language.  You can read their online manual (over three web pages) which should take maybe 20 minutes long. You can also play some of their games partly or fully free on the web.

The games are put together through a series of scripts. There’s also a free IDE for creating the scripts from a 3rd party. If you think writing a book is hard then writing an IF (Interactive Fiction) game must be like that but on steroids.

Games like the 1.2 million words one have multiple paths through with different plot lines which aids replayability. This isn’t anything like the Fighting Fantasy books (Name drop: I worked on a football game for Ian Livingstone in the mid 90s) because ChoiceScript has variables. This means that characteristics can improve over time and you can make decisions using <. >, <=, >=,==,!= just like in C etc. It doesn’t have arrays (there seems to have been some attempt at adding them in 2015 but I suspect it didn’t work) but there are subroutines and Goto which lets you create a structure.

You can add graphics but the games I’ve looked at don’t use them much if at all. The pictures are painted with words and this company have a very wide variety of games on sales including RPG, superheroes, science fiction, mysteries, war and pirates amongst others. The game mechanics are multiple-choice questions which doesn’t sound very glamorous but its like reading a book; your mind fleshes out the backdrop. The original Colossal Cave Adventure game is an example of Interactive Fiction and not coincidentally the Awards for Interactive Fiction are called the XYZZY Awards.

As the saying goes “Everyone has a novel in them”. IF is one way to bring it out…

 

A Minecraft clone in C

A Minecraft clone in C

Open source Minecraft clone in CI’ve never really been a great fan of Minecraft, though it is a great and wildly successful game. My reason for mentioning it is that there’s a C open source version of it and it’s cross-platform as well on Windows, Mac and Linux.

It was developed seven years ago and has had the odd update since with the most recent earlier this year.

The developer (Michael Fogleman) has also provided an online server for it and has some Python in it to make that possible but 80% of the code is C. It uses OpenGl to draw the graphics; so if you want to write your own Minecraft clone, this is a good program to study. As usual I’ve added a link into the C Code Links page.