Digital clock for my PC XT compatible, or having fun with Borland Graphics Interface (BGI)

·3 min read·paskov

Yet another day “wasted” playing with my Pravetz 16. I really should sit down and do some work, but the battery in my wall clock died, so I decided to code myself a digital one.

The clock in action

The clock in action

The clock is written in C using the Borland Graphics Interface, also known as BGI. It’s a simple way to write graphics programs for DOS without targeting specific graphics hardware. BGI came bundled with drivers for graphics adapters common at the time, such as CGA, EGA, and VGA, as well as vector fonts.

Other options include calling BIOS interrupt 10h or writing directly to the graphics hardware, as many DOS games did.

To build the code, open the Turbo C IDE, go to Options > Linker, and enable Graphics library. Then compile and link the program as usual.

Turbo C linker options

Turbo C linker options

Download the compiled CLOCK.EXE or the source code if you’d rather not copy and paste the listing below.

When I have some free time and motivation, I might add features such as a 12/24-hour display mode, moving objects in the background, a Pravetz logo in the upper left corner, or command line options for setting an alarm.

If you’d like to learn more about BGI, look at the examples that come with Turbo C, such as BARCHART.C, and inspect the GRAPHICS.H header. You can also explore these graphics.h examples.

Here’s the source for “The Digital Clock”

#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdio.h>

#define TIME_BUF_LEN 6
#define DATE_BUF_LEN 11

#define TIME_TEXT_TEMPLATE "00:00"
#define DATE_TEXT_TEMPLATE "00.00.0000"

#define NOTE_TEXT "Press any key to exit..."

#define BGI_LOCATION "C:\\TC\\bgi"

int gl_nMaxX = 0;
int gl_nMaxY = 0;

int gl_nCenterX = 0;
int gl_nCenterY = 0;

char gl_szTimeBuff[TIME_BUF_LEN];
char gl_szDateBuff[DATE_BUF_LEN];

int initGraphics(void)
{
    int nGraphicDriver = DETECT;
    int nGraphMode;
    int nErrorCode;

    /* Initialize graphics system */
    initgraph(&nGraphicDriver, &nGraphMode, BGI_LOCATION);
    nErrorCode = graphresult();

    if (nErrorCode != grOk)
    {
        return -1;
    }

    /* Graphics OK, so return "true" */
    return 1;
}

void drawNote()
{
    int textWidth, textHeight;

    settextstyle(DEFAULT_FONT, HORIZ_DIR, 0);

    textWidth = textwidth(NOTE_TEXT);
    textHeight = textheight(NOTE_TEXT);

    outtextxy(gl_nMaxX - textWidth,
              gl_nMaxY - textHeight,
              NOTE_TEXT);
}

void drawTime(struct time *pTime)
{
    int textWidth, textHeight;

    settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 8);

    textWidth = textwidth(TIME_TEXT_TEMPLATE);
    textHeight = textheight(TIME_TEXT_TEMPLATE);

    sprintf(gl_szTimeBuff, "%02d:%02d", pTime->ti_hour, pTime->ti_min);

    outtextxy(gl_nCenterX - (textWidth / 2),
              gl_nCenterY - textHeight,
              gl_szTimeBuff);
}

void drawDate(struct date *pDate)
{
    int textWidth;

    settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 6);

    textWidth = textwidth(DATE_TEXT_TEMPLATE);

    sprintf(gl_szDateBuff,
            "%02d.%02d.%d",
            pDate->da_day,
            pDate->da_mon,
            pDate->da_year);

    outtextxy(gl_nCenterX - (textWidth / 2), gl_nCenterY, gl_szDateBuff);
}

int main(void)
{
    struct time sysTime, startTime;
    struct date sysDate, startDate;

    if (initGraphics() == -1)
    {
        printf("Error initializing BGI!\n");
        return 0;
    }

    gl_nMaxX = getmaxx();
    gl_nMaxY = getmaxy();

    gl_nCenterX = (getmaxx() / 2);
    gl_nCenterY = (getmaxy() / 2);

    drawNote();

    gettime(&startTime);
    drawTime(&startTime);

    getdate(&sysDate);
    drawDate(&sysDate);

    while (!kbhit())
    {
        gettime(&sysTime);

        /*
         * To avoid flickering, redraw the screen only if there are
         * changes in the hour or minute values. Only some VGA and EGA
         * drivers support 'setactivepage()' and 'setvisualpage()'.
         */
        if (sysTime.ti_hour != startTime.ti_hour ||
            sysTime.ti_min != startTime.ti_min)
        {
            cleardevice();
            getdate(&sysDate);

            drawNote();
            drawTime(&sysTime);
            drawDate(&sysDate);

            gettime(&startTime);
        }
    }

    closegraph();

    return 0;
}