
Coding a console graphics library in C
Today we will begin writing our own (n)curses library. It is a library for making console graphics and in contrast with the regular console routines like printf() and scanf/fgets(), using a library like this you have full control over the screen.
Or at least as much as is possible. You can move around in a coordinate system, update individual characters, use foreground and background colors, the mouse and you can trigger on key presses which ie. makes it possible to move around with the arrow keys.
Putting things on the screen
In the 2nd and 3rd chapters, we'll be reading input from the keyboard. And that might not sound very interesting, but if you have used the standard C functions like scanf() or fgets() (or even read()), then you know how limited they are. You can read ascii characters but not special keys like the arrow keys and often you cannot even use the backspace key when you type.
Another severe limitation is that the functions only trigger when the user has entered a line of text and pressed the enter key. You can't make your code react instantly on the key-down event. Also, all key-presses are echoed to the screen, and that's not always what you want (for example in a game or when asked for a password).
We will solve all these limitations by writing our own keypress API. Our getch() function will react instantly on every keypress and it will successfully parse the arrow keys and backspace, without any echo to the screen (if you want the echo, you can print the character yourself). We will then use this custom API to control a cursor on the screen by using the arrow keys. Perhaps an early start of a gaming project or perhaps a text editor..?
Enroll now!
Or try out the free sample videos.
Sample
Here is a list of a few of the functions we'll be developing in this project:
private framebuffer *mkfb(int16);
public window *mkwin(state*,int8,int8,int8*);
private state *mkstate(void);
public _init void initlibrary(void);
public _fini void exitlibrary(void);
private void sfblinear(window*,int8,int8,int8,int16);
private void sfbxy(window*,int8,int8,int8,int8,int8);
private void updatewin(window*,int8,int8,int8);
private void termenable(state*);
private void termdisable(state*);
public void cputchar(window*,int8,int8,int8);
public void cjump(window*,int8,int8);
public void cupdate(window*);
public void cframeset(window*,int8,int8,int8);
protected void ansimove(int8,int8);
protected int8 *ansicolor(int8,int8);
protected int8 *ansireset(void);
protected void ansimode(void);
protected void showfb(framebuffer*,int16);
protected void showwin(window*);
protected void showstate(state*);