The Big (Text Mode) Graphical Grid

To output graphics, you need a palette. That is, you need a buffer that’s so many pixels wide and rows high. Create the buffer, draw in the buffer, output the buffer. That’s how graphics works, both in graphics as well as text mode.

For text mode graphics, you start by creating a graphics buffer, which is basically a char array. You could build a single dimension array, but use two dimensions to reflect the x and y coordinates of the grid. Here’s what I came up with:

#include <stdio.h>

#define WIDTH 60
#define HEIGHT 20

int main()
{
    char grid[HEIGHT][WIDTH];
    int x,y;

/* Initialize grid */
    for(y=0;y<HEIGHT;y++)
        for(x=0;x<WIDTH;x++)
            grid[y][x] = ' ';

/* display grid */
    for(y=0;y<HEIGHT;y++)
    {
        for(x=0;x<WIDTH;x++)
            putchar(grid[y][x]);
        putchar('\n');
    }

    return(0);
}

The text mode graphical grid’s dimension is set at Lines 3 and 4. The WIDTH and HEIGHT constants correspond to character columns and rows.

Line 8 declares the grid.

Lines 12 through 14 initialize the grid, filling it with space characters.

Lines 17 through 22 output the grid, breaking it up into rows.

What this code does is rather simple, but the potential is great.

When compiled and run, the code outputs a huge block of . . . nothing! Figure 1 shows the output from my computer’s terminal window.

Figure 1.

Figure 1. The text mode graphical grid is displayed in a terminal window.

Yawn . . .

The canvas is empty, and like all blank canvases, it’s boring.

Oh, yeah: Some avant garde artist somewhere can do an installation of blank canvases. I don’t think the university professors are giving out grades for such things any longer. No, a blank canvass begs for something to be drawn on it. That missing part of the code is shown below, from Lines 16 through 18:

#include <stdio.h>

#define WIDTH 60
#define HEIGHT 20

int main()
{
    char grid[HEIGHT][WIDTH];
    int x,y;

/* Initialize grid */
    for(y=0;y<HEIGHT;y++)
        for(x=0;x<WIDTH;x++)
            grid[y][x] = ' ';

/* draw a vertical line */
    for(y=0;y<HEIGHT;y++)
        grid[y][WIDTH/2] = '|';

/* display grid */
    for(y=0;y<HEIGHT;y++)
    {
        for(x=0;x<WIDTH;x++)
            putchar(grid[y][x]);
        putchar('\n');
    }

    return(0);
}

After the grid is created, a vertical line is drawn by using the | (pipe) character. Line 18 does the job:

grid[y][WIDTH/2] = '|';

The center of the grid is calculated by dividing the WIDTH in two. Then the | character is placed in that column for each row (variable y) in the grid array.

This drawing takes place after the grid (palette) is created. When the grid is displayed, the line appears, as shown in Figure 2.

Figure 2. Text mode graphics drawing success! A vertical line.

Figure 2. Text mode graphics drawing success! A vertical line.

This code is pretty crude, but it works. A better solution would be to craft a function that actually draws a line between two points on the grid. That’s also possible. In fact, you can craft functions that draw circles, squares, polygons, and more. As long as you work within the char array and then display it, all kinds of text mode graphics are possible.

Leave a Reply