25 Years of Programming
An open source source for C, C++, OWL, BASIC, MDB, XLS, DOT, and more...
Home   Projects   Up   Sitemap   Search   Blog   Forum+Chat   About Us   Privacy   Terms of Use   Feedback   FAQ   Images   Services   Ads   Donate   Humor

Functions for MSDOS only

These all use Borland C++ DOS-only functions; most also use the Borland Graphics Interface (BGI).

ci.cpp

/*	ci.cpp	 CONSOLE INPUT  			10-19-99
	Copyright (C)1999 Steven Whitney.
	Published under GNU GPL (General Public License) Version 2, with ABSOLUTELY NO WARRANTY.

	RETURNS A 2 BYTE INT.
	use this function instead of getch() to read alt-, function keys, etc.

	ASCII CHARS ARE RETURNED AS: 1-255(extended set), WITH HIGH BYTE ZEROED OUT
	(You can only create chars above 127 with the alt-numeric keypad trick.)

	FUNCTION KEYS RETURNED AS: LOW BYTE ZEROED, HIGH BYTE has unique CODE
	enumerated in my.h.
	Example: if(ci() == 0x3b00) tests for F1.
	NULL CHAR (CONTROL-@) IS RETURNED AS 0X300

	"Middle" keypad keys on Gateway enhanced keyboard return same codes as
	other function keys, except low byte is 224 instead of 0.
	ci() zeroes out the low byte for these keys,
	so the duplicate home, ins, del, arrow, etc. keys on both keypads are interpreted
	the same, and can't be distinguished from each other (usually what I want).

	10-19-99 I changed this so it no longer translates all to lowercase,
	and modified the few pgms that use ci().

*/
#include <stdio.h>
#include <bios.h>
#include <ctype.h>
#include "c:\bcs\my.h"
int ci()
{
unsigned key;				// must be unsigned so negative is impossible
unsigned lowbyte;
                                        // bioskey is a Borland DOS-only function.
while(bioskey(_NKEYBRD_READY) == 0);    // wait for key, if one not waiting
key = bioskey(_NKEYBRD_READ);           // read it
lowbyte = key & 0xff;    				// this clears high byte
switch(lowbyte)
{
	case 224:				// "middle" keypad key on enhanced keyboard
		key &= 0xff00;		// zero out LOW byte: 2 keypads now identical
							// (to differentiate, remove the line)
							// falls through to next case...
	case 0:					// function or alt-key
		return((int)key);   // negative possible? hasn't happened so far.

	default:     		    // ASCII char, (low byte has data != 224)
		return((int)lowbyte);

// old version converted to lower case automatically, not a good idea.
// If any old pgm doesn't work properly, this is the return value it expected:
// 		return(tolower((int)lowbyte));

}
}							// ci()

gputs.cpp

/*	gputs.cpp   Graphics-PUTS()		3-12-96
	Copyright (C)1996 Steven Whitney.
	Published under GNU GPL (General Public License) Version 2, with ABSOLUTELY NO WARRANTY.

	Prints a string to the screen in graphics mode, similar to puts().

	To prevent printed text from being obscured by what's already on the screen,
	it first clears (in either black or the background color, whichever is
	different from the text color) a box large enough to hold the text.
	(it's still possible for text to be invisible if color == background == black.)

	You should first use ostrstream or sprintf() to format your output into "text".

	Also, the (x,y) coordinates are NOT pixels, but text mode (characters,lines), upper left = (1,1).
	(There is no test for whether the given (x,y) coordinates will land on the
	screen when conversion to pixels takes the font size into consideration.)

	Could add: when parameters x == y == 0 (or when negative?), it means "print at bottom"
	(or print n lines up from bottom): use different placement calculation to align on bottom
	line of screen. (must count up from bottom rather than down from top)

	This function leaves the draw color changed, since I usually set the color
	before any action, anyway.

	Bug: I thought this worked fine in the program it was originally designed for,
	but in a later program, the spacing between lines was too small, so line 2
	overwrote the lower portion of line 1.

*/
#include <stdio.h>
#include <cstring.h>
#include <graphics.h>
#include "c:\bcs\my.h"

void gputs(int x, int y, int color, char *text)
{
int xloc = (x - 1) * textwidth("W");    	// convert to pixels. W looks like the widest letter.
int yloc = (y - 1) * textheight("H");		// it seems to include between-line spacing. H tallest letter.
string s(text);			                	// local buffer so we can modify text
for(int i = 0 ; i < s.length() ; i++)		// strip CR and LF, because they're
	if((s[i] == '\n') || (s[i] == '\r'))	// printable chars in graphics mode
		s[i] = ' ';                   		// just turn them into spaces

setfillstyle(SOLID_FILL,(color != BLACK)? BLACK : getbkcolor());
bar(xloc,yloc,textwidth(s.c_str()),yloc + textheight("H") + 2); // clear a text box
setcolor(color);                        						// set draw color for outtext
outtextxy(xloc,yloc,s.c_str());
}

getpixel.cpp

/*	getpixel.cpp			12-8-96
	Copyright (C)1996 Steven Whitney.
	Published under GNU GPL (General Public License) Version 2, with ABSOLUTELY NO WARRANTY.

	Some custom getpixel functions.
	You must already be in graphics mode.

*/
#include <graphics.h>
#include "c:\bcs\my.h"

//-----------------------------------------------------------------------
// sgetpixel()    (SafeGetpixel)
// same as Borland getpixel(), except it returns 0 if (x,y) is offscreen
unsigned far sgetpixel(int x, int y)
{
	// more correct but slower
	// if((x < 0) || (x > getmaxx()) || (y < 0) || (y > getmaxy()))
	// 	return(0);

	// assumes VGAHI
	if((x < 0) || (x > 639) || (y < 0) || (y > 479))
		return(0);

	return(getpixel(x,y));
}
//-----------------------------------------------------------------------
// getpixel() version that takes double arguments.
// Returns the color at the point, or 0 if the point was offscreen.
unsigned far getpixel(double dx, double dy)
{
	return(sgetpixel((int)dx,(int)dy));
}
//-----------------------------------------------------------------------

graborts.cpp

/*	graborts.cpp    GraphicsABORTS()		10-31-96
	Copyright (C)1996 Steven Whitney.
	Published under GNU GPL (General Public License) Version 2, with ABSOLUTELY NO WARRANTY.

	prints specified error message and aborts program execution.
	Use this version of aborts() in any program using BGI graphics.

*/
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <iomanip.h>
#include "c:\bcs\my.h"

void graborts(const string& s)
{
	closegraph();				// no mode test: seems to do no harm if already in text mode.
	cerr << endl << s << endl;
	presskey();      			// prevent msg scrolling off before being seen
	exit(1);
}

initgraf.cpp

/*	initgraf.cpp  initialize graphics display		3-12-96
	Copyright (C)1996 Steven Whitney.
	Published under GNU GPL (General Public License) Version 2, with ABSOLUTELY NO WARRANTY.

	Initializes graphics to the mode I always use (VGA HI-RES),
	but could replace void with parameters, including path to driver.

	returns graphics mode selected for possible later use by setgraphmode()

*/
#include <stdio.h>
#include <stdlib.h>
#include <iomanip.h>
#include <graphics.h>
#include "c:\bcs\my.h"

int initgraf(void)
{
	int grdriver(VGA), grmode(VGAHI), errorcode;	// VGAHI = 1 page at 640x480
	initgraph(&grdriver,&grmode,"c:\\bc4\\bgi"); 	// path to driver is system-specific
	errorcode = graphresult();
	if(errorcode != grOk)
	{
		cerr << "Graphics System Error " << errorcode << ": " << grapherrormsg(errorcode) << endl;
		exit(1);
	}
	return(grmode);
}

picsave.cpp

/* 	picsave.cpp					3-12-96
	Copyright (C)1996 Steven Whitney.
	Published under GNU GPL (General Public License) Version 2, with ABSOLUTELY NO WARRANTY.

	Saves current graphics screen display to disk file in the format:
	ascii strings WIDTH,HEIGHT\N, THEN A SERIES OF (WIDTH*HEIGHT) CHARS:
	COLOR AT X1,Y1, COLOR AT X2,Y1, ETC. (X VARIES FASTEST)
	you must already be in graphics mode before calling this fn.
		to do: figure out how to test for it

	returns TRUE if succeed, FALSE if fail

	the screen size string is written in binary mode, so what would
	ordinarily be a /r/n is only a /n.  fscanf() won't read them properly,
	but >> will.  See picload() for proper way to do it.
	The advantage of this is that you can still type the .pic file
	to see what screen size it uses.

*/
#include <stdio.h>
#include <fstream.h>
#include <graphics.h>
#include "c:\bcs\my.h"

int picsave(const char *filename)  	// include the .pic extension when you call it
{
int maxx = getmaxx();
int maxy = getmaxy();

if(getmaxcolor() > 255)				// char can only hold 255
	return(FALSE);

ofstream outfile(filename,ios::binary);
outfile << (maxx+1) << "," << (maxy+1) << endl;	// binary: \n only writes linefeed

for(int y = 0 ; y <= maxy ; y++)
	for(int x = 0 ; x <= maxx ; x++)
		outfile << (unsigned char)getpixel(x,y);

return(TRUE);
}

picload.cpp

/* 	picload.cpp					12-18-96
	Copyright (C)1996 Steven Whitney.
	Published under GNU GPL (General Public License) Version 2, with ABSOLUTELY NO WARRANTY.

	Displays to screen a .PIC format file previously saved with picsave().
	see picsave() for the file format.

	console must already be in graphics mode before entry

	if current screen mode is lower resolution than the file,
	only the upper left corner of the image will be loaded.

	returns highest color used in file, or EOF on error

*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <fstream.h>
#include <graphics.h>
#include "c:\bcs\my.h"

int picload(const char *filename)
{
int maxx, maxy;

ifstream infile(filename,ios::binary);
if(!infile)
	return(EOF);
infile >> maxx;
infile.ignore(1);			// the comma
infile >> maxy;
infile.ignore(1);			// the \n

char ch;
int color, highcolor = 0;				// highest color seen so far
int xlimit = min(maxx-1,getmaxx()); 	// use current mode's dimensions
int ylimit = min(maxy-1,getmaxy());		// if smaller than those in the file.
for(int y = 0 ; y <= ylimit ; y++)		// ok to stop after last allowable line,
	for(int x = 0 ; x < maxx ; x++)		// but MUST read all x data for each line
	{
		if(!infile.get(ch))				// file too short
			return(EOF);
		if(x <= xlimit)					// only process chars that
		{								// are within current screen
			color = (int)ch;
			highcolor = max(highcolor,color);
			putpixel(x,y,color);
		}
		if(kbhit())						// ESC aborts
			if(getch() == 27)			// (^C causes a batch file to abort)
				return(highcolor);
	}
return(highcolor);
}

 

 

Valid HTML 4.01 Transitional Valid CSS
View content labeling at ICRA.
Copyright ©2008 Steven Whitney. Last modified 02/27/2008.