Tag Archives: setactivepage()

The clock in action

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

Yet another day “wasted” playing with my Pravetz 16. Really I should sit on my … and do some real work, but the battery of my wall clock died and I decided to code myself a digital one 🙂

The clock in action

The clock in action

The clock is written on C using Borland Graphics Interfaces also knows as BGI. The most easy and video hardware independent option for doing graphics under DOS. BGI comes bundled with drivers for common back then graphic adapters (CGA, VGA and EGA) and vector fonts.

There are other options such as using interrupt 10h or writing directly to the graphics hardware like most of the games were doing back then.

To build the code fire up the Turbo C IDE, go to Options->Linker and check “Graphics library”, then proceed as usual.

Turbo C Linker options

Turbo C Linker options

Compiled version of the CLOCK.EXE can be obtained from here and the source code (if you don’t like Ctrl + C & Ctrl + V) is here.

Wen I got some free time and motivation I will probably enhance the clock with some neat options such as 12/24 hours mode, moving objects in the background, drawing the “Pravetz” logo in the upper left corner of the screen, command line parameters for setting an alarm etc…

If you wan’t to learn more about BGI review the examples that comes with Turbo C such as BARCHART.C. View the GRAPHICS.H header. Also take a look here.

Code listing a.k.a “The Digital Clock” source 🙂

#include <stdio.h>
#include <dos.h>
#include <graphics.h>
 
#define TIME_BUF_LEN 6
#define DATE_BUF_LEN 10
 
#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;
}