- Platform: Linux (or WSL)
- Language: C++
- Other used: --
I was interested in C++ for a while, but I always need some goal or a challenging project to start learning something new. This is how the idea of an ASCII clock came. It could look supercool when everything is drawn on screen with only regular characters. And it sounds easier than jumping right into the middle of some complex GUI app with networking, multi threading, etc.
And it will be very nice if I could place it in an empty corner in the kitchen :)
Where to start
As with every learning project, we need to start with the basics.
- How to print characters to the screen?
- How to print formatted characters?
- How to print them on a specified position
- As some extra, how to handle multiple drawing layers, and how to add transparency?
- Maybe adding some animations?
Echoing a simple characters is not a dificult thing. But what if I want to fill the whole screen with letters? As always, there is a little trick. If we have for eg. a window with 25 lines, each of them 80 chars wide, then I can use a string what is 2000 (80x25) characters long, fill it with any chars, and echo it at once to the screen. The 0th character will appear on the top left corner, the 79th character will be printed on the end of the 1st row, and the 80th will be on the beginning of the 2nd row, and so on.
If I need to put a char to a given XY position, then a bit math is needed. To print out a letter to X=5, Y=12, I can simple replace the character in my string at the (12-1)*80+5 position with anything what I want to appear here, and print it as before.
Coloring is quite easy with the help of ANSI escape sequences, and fortunately they are simple character strings too what can be printed as the same way as our letters previously. (There are cursor positioning ANSI codes too, but this string printing method is fairly faster, and allows a whole screen refresh at once)
It's just a step away to store all of the character, the back- and foreground colors in an array as buffer. If we use multiple of them and add extra transparency properties, they can be used as layers above each other. Finally we can create the final string from it, and print the whole stuff out.
Now we have a "graphical engine", so fancy animations can be created with the help of random numbers...
... random and averaging ...
... some sin() & cos() ...
... or with the simplest algorithms.
The clock currently has 1 foreground and 5 background effects & 3 styles for digit display. It also contains basic 3D functions with a line drawing routine (->see video).
Please check the below video for full experience.
Project on Hackaday.io: https://hackaday.io/project/176720-animated-ascii-clock