Category: libraries

Raylib – a C library for videogames

Raylib – a C library for videogames

Raylib screenshotaMuch as I love SDL, I’m quite happy to look at alternatives and Raylib looks very good. It was inspired by Borland BGI graphics lib and by XNA framework (both of which I’ve used).

It’s written in C99 and is very cross-platform, even listing Raspberry Pi (I’m guessing they mean Raspberry Pi OS) as well as Windows, Mac, Linux, Android and HTML5. Basically any platform that supports C and OpenGL. There is a massive list of bindings for use in other programming languages.

Check out the examples page; you can try them online and each has a C source code listing so you can see how it’s done.

What I really like about RayLib is the scope of it and this is where I think it beats SDL. There’s 2D and 3D support, text support, in eight modules (core, shapes, textures, text, models, shaders, raudio and physac). It also comes with 8 free fonts but you can use your own custom fonts, pretty much the same way as I did in the Asteroids game.

I was very impressed with the text demo where a red box contains the text and if you resize it with the mouse the text is word wrapped.

The Spanish developer Ray (surname unknown) was cited today as the #1 trending C developer on GitHub and that’s a testament to the seven years that he has put into Raylib. His design philosophy with Raylib is to make programming fun and I am going to check it out.  If it looks as good as I think it is, I will be switching to it for future games work. One thing that particular intrigues me is the RayGui library– an immediate mode GUI library. There are various tools that have been designed with it. and it supports these GUI controls:

Label       |  Button      |  LabelButton |  ImageButton  |  Toggle      |  ToggleGroup |  CheckBox
ComboBox    |  DropdownBox |  TextBox     |  TextBoxMulti |  ValueBox    |  Spinner     |  Slider   
SliderBar   |  ProgressBar |  StatusBar   |  ScrollBar    |  ScrollPanel |  DummyRec    |  Grid

RayGui table

 

 

 

 

 

 

Coding a text adventure in C

Coding a text adventure in C

Text AdventuresText adventures have been around since the likes of Colossal Cave which I first played in 1980. They were quite popular on the early home computers like ZX 81 which were quite limited in RAM and capability. With the capacity of modern computers there are Interactive fiction (the modern name for adventure games) games with over a million words of text.

I suspect I’d probably want to code them in another language because C isn’t the best language when it comes to text. Nevertheless other have gone ahead and there’s an almost complete (20 out of 23 lessons) on how to program a text adventure in C on GitHub.

It’s a fine piece of work and some of the 20 articles like 20. Combat are quite long with lots of annotated code. At the bottom of each page you can download the source code on that page as a zip file or run the sources on repl.it. If you are learning C the line-by-line annotations really help explain why something was done.

The C template library

The C template library

Texture templateC of course doesn’t have generics or templates like C++ does, but I came across Gustav Lou’s C Template library this morning. It provides C equivalents to the following C++ template libraries. Each is done as a header file that you include.

deq.h -> std::deque
lst.h -> std::list
pqu.h -> std::priority_queue
que.h -> std::queue
set.h -> std::set
stk.h -> std::stack
str.h -> std::string
vec.h -> std::vector
 

The page on GitHub shows how to use libraries with an example of vector. The string equivalent library str.h would be an interesting one to see just how much of C++ string is supported.  C would still never be my language of choice for anything text heavy but if this is better than strcpy, strcat etc. I’ll happily use it.  Likewise list and vector. From memory C++ std::array is just a wrapper around the C standard array!

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.

 

Single file libraries

Single file libraries

open book lot
Library image from Unsplash.com

Most days I scan the web for relevant or interesting stuff and today I found a real treasure. A website where they have curated a list of C and C++ single file libraries. That is libraries that are not multiple files but mostly one. I say mostly because there are a few with 2 or 3 files.

The bulk are in C++ but there are a large number of C/C++ and quite a number of C. All have their licensing; these are open source but licences vary a bit.

It’s worth a trawl through the List. For instance there’s an interpreter for a BASIC dialect scripting language,