Text Mode Graphics

Graphics programming is entirely possible in the C language.

To program graphics you need a graphics programming library. The library contains the functions that do fancy graphical stuff, such as the graphics you see on a computer screen or generated by a gaming console. That’s the official, best way to program graphics in C.

Text mode graphics programming is also possible, albeit crude. The advantage is that anyone can code the graphics without messing with a graphics library or learning how to code for specific hardware.

To see text mode graphics in a Unix terminal, type banner -w 80 "Hello" The output is displayed vertically; use the scrollback buffer to view it.

Creating simple graphical output can be done in two ways.

First, if you have direct control over the console, you can output anything — even animations. This type of control involves being able to place the cursor at any position on the text screen. Many early PCs and microcomputers had this ability. Today, however, you still need a library or API to have that kind of console control.

For Unix, the NCurses library offers that level of control. Click here for info on my NCurses programming book.

Second, you can output a graphical image by building a character buffer, which is also how pixel-level graphics work: A buffer is allocated, an image is “drawn” in the buffer, and then the buffer is sent to the graphics hardware for display. For a text screen image, the buffer is a char array, which is then output to the terminal.

The sample code below generates a graphic image on the screen. It does so by outputting one character a time, which is my weak attempt to show interactive text mode graphics.

#include <stdio.h>

void spaces(int count)
{
    int s;

    for(s=0;s<count;s++)
        putchar(' ');
}

int main()
{
    int x;

    puts("i\\/\\/\\i");
    for(x=0;x<2;x++)
    {
        putchar('|');
        spaces(5);
        puts("|");
    }
    puts("| (O)(O)");
    putchar('C');
    spaces(5);
    printf("_)\n|");
    spaces(2);
    printf(",___|\n|");
    spaces(5);
    printf("/\n|");
    spaces(3);
    puts("/");

    return(0);
}

Compile and run the code to see the text-mode graphics output.

A better way to create text mode graphics is to create a buffer and draw the image in that buffer. The buffer is then output one line at a time.

The following code generates output similar to the first example, although the image is pre-created in the buffer declaration and, therefore, you can pretty much see the image before it’s output:

#include 

int main()
{
    int x;
    char bart[8][10] = {
        "i\\/\\/\\i \n",
        "|     | \n",
        "|     | \n",
        "| (O)(O)\n",
        "C     _)\n",
        "|  ,___|\n",
        "|     / \n",
        "|   /   \n" };

    for(x=0;x<8;x++)
        printf("%s",bart[x]);

    return(0);
}

Of course, ideally you’d want to code something that would draw the image in the buffer, then output the buffer. I’ll demonstrate how that’s done in next week’s Lesson.

Leave a Reply