Category : C Source Code
Archive   : SCRIO1.ZIP
Filename : SCRIO.C

 
Output of file : SCRIO.C contained in archive : SCRIO1.ZIP
/* =========================================================== */
/* */
/* SCRIO.C PCW's IBM-PC SCREEN */
/* & LOW LEVEL ROUTINES FOR TURBO C */
/* */
/* < DOS Interupt Version 1.5 > */
/* */
/* < Peter C. Worcester 8/87 > */
/* =========================================================== */

#include
#include
#include
#include
#include
#include

/* Turbo C prototypes for SCRIO functions */

int scr_setup(void);
void scr_page(int);
void scr_clr(void);
void locate(int, int);
int scr_srow(void);
int scr_scol(void);
void scr_cls(void);
void scr_clrl(void);
void scr_curson(void);
void scr_cursoff(void);
void scr_setcursor(int, int);
void scr_setmode(int);
int scr_getmode(void);
unsigned int scr_agetc(void);
void scr_aputc(unsigned char, int);
void scr_aputs(char *, int);
void scr_rcsa(int, int, char *, int);
int getbits(int, int, int);
void pset(int, int, int);
int pget(int, int);
void screen(int);
void graphics_on(int);
void graphics_off(void);
void scr_color(int, int);
void set_pallette(int);
void set_backgnd(int);
void line(int, int, int, int, int);
void clip_line(int *, int, int);
void box(int, int, int, int, int);
int video_board(void);
int scr_ci(void);
int scr_key(void);
int scr_inkey(void);


/* IBM CGA & EGA & MDA Display Defines */

#define CTEXT 3 /* IBM 80x25 color mode */
#define MEDRES 4 /* IBM medium-res graphics mode */
#define HIGHRES 6 /* IBM High-res graphics mode */
#define TEXT 7 /* IBM monochrome text mode */
#define EGAMONO 15 /* EGA High-res graphics mono */
#define EGAHRES 16 /* EGA High-res graphics mode */

#define MEGA 3
#define CEGA 2
#define CGA 1
#define MDA 0

/* CGA & EGA Colors */

#define RED 4
#define PURPL 5
#define WHITE 7
#define BLUE 9
#define BGREEN 10
#define LBLUE 11
#define BRED 12
#define YELOW 14
#define HIGHINTEN 15
#define INVERSE 112

#define sign(x) ((x < 0) ? 1 : 0)


/* ---------------- [ SCREEN GLOBALS ] ---------------------- */

int scr_mode; /* Current SCR Mode */
int scr_left; /* X Minimum */
int scr_right; /* X Maximum */
int scr_top; /* Y Minimum */
int scr_bottom; /* Y Maximum */
int scr_bg; /* background color */
int scr_fg; /* foreground color */

static int ftt;

/* ----------------------------------------------------------- */



/* =========================================================== */
/* */
/* I B M S C R E E N R O U T I N E S */
/* */
/* =========================================================== */
/* int scr_setup(); */
/* void scr_page(); */
/* void scr_clr() ; */
/* void locate() ; */
/* int scr_srow() ; */
/* int scr_scol() ; */
/* void scr_clrl() ; */
/* void scr_cls() ; */
/* void scr_cursoff() ; */
/* void scr_curson() ; */
/* void scr_setcursor() ; */
/* int scr_getmode() ; */
/* void scr_setmode() ; */
/* */
/* u int scr_agetc() ; */
/* void scr_aputc() ; */
/* void scr_aputs() ; */
/* void scr_rcsa() ; */
/* */
/* int getbits() ; */
/* =========================================================== */



/*******************************************************/
/* */
/* FUNCTION : scr_setup() */
/* */
/* Initialize IBM screen */
/* */
/*******************************************************/

int scr_setup(void) {
union REGS reg;
struct SREGS segr;

segread(&segr);
reg.x.ax = 0x0F00;
int86x(0x10, ®, ®, &segr);
scr_mode =(reg.x.ax & 0x00ff);
return(scr_mode);
}

/********************* E N D ***********************/


/*******************************************************/
/* */
/* FUNCTION : scr_page() */
/* */
/* Initialize Page X video page */
/* */
/*******************************************************/

void scr_page(int page) {
union REGS reg;
struct SREGS segr;

segread(&segr);
reg.x.ax = 0x0500 + page;
int86x(0x10, ®, ®, &segr);
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : scr_clr() */
/* */
/* Clear Screen < TEXT only > */
/* */
/*******************************************************/

void scr_clr(void) {
union REGS reg;
struct SREGS segr;

segread(&segr);
reg.x.ax = 0x0600;
reg.x.bx =(7 << 8);
reg.x.cx = 0;
reg.x.dx = 0x1800 + 0x004f;
int86x(0x10, ®, ®, &segr);
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : locate() */
/* */
/* Locate at Row , Col */
/* */
/*******************************************************/

void locate(int row, int col) {
union REGS reg;
struct SREGS segr;
segread(&segr);

reg.x.ax = 0x0200;
reg.x.bx = 0x0000;
reg.x.dx =(row << 8) + col;
int86x(0x10, ®, ®, &segr);

}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : scr_srow() */
/* */
/* Returns current Row position */
/* */
/*******************************************************/

int scr_srow(void) {
union REGS reg;
struct SREGS segr;

segread(&segr);

reg.x.ax = 0x0300;
reg.x.bx = 0x0000;
int86x(0x10, ®, ®, &segr);
return((reg.x.dx >> 8));
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : scr_scol() */
/* */
/* Returns current Col position */
/* */
/*******************************************************/

int scr_scol(void) {
union REGS reg;
struct SREGS segr;

segread(&segr);
reg.x.ax = 0x0300;
reg.x.bx = 0x0000;
int86x(0x10, ®, ®, &segr);
return((reg.x.dx & 0x00ff));
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : scr_cls() */
/* */
/* Clears rest of Screen */
/* */
/*******************************************************/

void scr_cls(void) {
int i,
lx,
ly;

ly = scr_srow();
lx = scr_scol();

scr_clrl();

for(i = ly; i < 25; i++) {
locate(i, 0);
scr_clrl();
}
locate(ly, lx);

}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : scr_clrl() */
/* */
/* Clears rest of line */
/* */
/*******************************************************/

void scr_clrl(void) {
union REGS reg;
struct SREGS segr;
char sp;
int r,
c,
leftover;

segread(&segr);

sp = ' ';
r = scr_srow();
c = scr_scol();

leftover = 79 - c;
reg.x.ax =(9 << 8) + sp;
reg.x.bx = 0x0007;
reg.x.cx = 0x0000 + leftover;
int86x(0x10, ®, ®, &segr);
locate(r, c);
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : scr_curson() */
/* */
/* Turns ON cursor display */
/* */
/*******************************************************/

void scr_curson(void) {
union REGS reg;
struct SREGS segr;

segread(&segr);
reg.x.ax = 0x0100;
reg.x.cx = 0x0607;
int86x(0x10, ®, ®, &segr);
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : scr_cursoff() */
/* */
/* Turns OFF cursor display */
/* */
/*******************************************************/

void scr_cursoff(void) {
union REGS reg;
struct SREGS segr;

segread(&segr);
reg.x.ax = 0x0100;
reg.x.cx = 0x0f00;
int86x(0x10, ®, ®, &segr);
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : scr_setcursor() */
/* */
/* Set cursor type & attribute */
/* */
/*******************************************************/

void scr_setcursor(int startline, int endline) {
union REGS reg;
struct SREGS segr;

segread(&segr);
reg.x.ax = 0x0100;
reg.x.cx =(startline << 8) + endline;
int86x(0x10, ®, ®, &segr);

}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : scr_setmode() */
/* */
/* Sets screen mode */
/* */
/*******************************************************/

void scr_setmode(int gmode) {
union REGS reg;
struct SREGS segr;

scr_page(0);

segread(&segr);
reg.x.ax = 0x0000 + gmode;
int86x(0x10, ®, ®, &segr);

scr_mode = gmode;
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : scr_getmode() */
/* */
/* Returns current screen mode */
/* */
/*******************************************************/


int scr_getmode(void) {
union REGS reg;
struct SREGS segr;

segread(&segr);
reg.x.ax = 0x0F00;
int86x(0x10, ®, ®, &segr);
scr_mode =(reg.x.ax & 0x00ff);

return(scr_mode);
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : scr_agetc() */
/* */
/* Returns Char & Attr at row, col */
/* */
/*******************************************************/

unsigned int scr_agetc(void) {
union REGS reg;
struct SREGS segr;
int atr, chr;
unsigned int chatr;

segread(&segr);
reg.x.ax =(8 << 8);
reg.x.bx = 0x0000;
int86x(0x10, ®, ®, &segr);

chr = reg.h.al;
atr = reg.h.ah;
chatr =(chr << 8) + atr;

return(chatr);

}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : scr_aputc() */
/* */
/* Writes Char & Attr at row, col */
/* */
/*******************************************************/

void scr_aputc(unsigned char c, int atr) {
union REGS reg;
struct SREGS segr;

segread(&segr);
reg.x.ax =(9 << 8) + 0x20;
reg.x.bx = 0x0000 + atr;
reg.x.cx = 0x0001;
int86x(0x10, ®, ®, &segr);

reg.x.ax =(14 << 8) + c;
int86x(0x10, ®, ®, &segr);
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : scr_aputs() */
/* */
/* Writes String & Attr at row, col */
/* */
/*******************************************************/

void scr_aputs(char *strn, int atr) {
int i,
c;

for(i = 0; i < strlen(strn); i++) {
c = strn[i];
scr_aputc(c, atr);
}
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : scr_rcsa() */
/* */
/* Locates & Writes Char & Attr. */
/* */
/*******************************************************/

void scr_rcsa(int row, int col, char *strn, int atr) {
int i;
int c;

locate(row, col);

for(i = 0; i < strlen(strn); i++) {
c = strn[i];
scr_aputc(c, atr);
}
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : getbits() */
/* */
/* Std K&R bit retrieval */
/* */
/*******************************************************/

int getbits(int x, int p, int n) {
return((x >>(p + 1 - n)) & ~(~0 << n));
}

/********************* E N D ***********************/






/* */
/* =========================================================== */
/* */
/* G R A P H I C S R O U T I N E S */
/* */
/* =========================================================== */
/* */
/* int pget() ; */
/* void pset() ; */
/* void screen() ; */
/* void graphics_on() ; */
/* void graphics_off() ; */
/* void scr_color() ; */
/* void set_pallette() ; */
/* void set_backgnd() ; */
/* int video_board() ; */
/* */
/* void line() ; */
/* void clip_line() ; */
/* void box() ; */
/* =========================================================== */


/*******************************************************/
/* */
/* FUNCTION : pset() */
/* */
/* Sets graphics pixel at X,Y */
/* */
/*******************************************************/

void pset(int y, int x, int color) {
union REGS reg;
struct SREGS segr;

segread(&segr);
reg.x.ax = 0x0C00 + color; /* write pixel */
reg.x.bx = 0x0000;
reg.x.cx = x;
reg.x.dx = y;

int86x(0x10, ®, ®, &segr);
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : pget() */
/* */
/* Returns graphics pixel at X,Y */
/* */
/*******************************************************/

int pget(int y, int x) {
union REGS reg;
int al;
struct SREGS segr;

segread(&segr);
reg.x.ax = 0x0D00; /* write pixel */
reg.x.bx = 0x0000;
reg.x.cx = x;
reg.x.dx = y;

int86x(0x10, ®, ®, &segr);

al =(reg.x.ax & 0x00ff);
return(al);
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : screen() */
/* */
/* Set screen mode < ala BASIC > */
/* */
/*******************************************************/

void screen(int res) {
if(ftt) { /* first time through */
scr_mode = scr_setup();
ftt++;
}

switch(res) {

case 0:
scr_setmode(CTEXT);
break;

case 1:
scr_setmode(MEDRES);
scr_left = 0;
scr_right = 319;
scr_top = 0;
scr_bottom = 199;
scr_fg = 3;
break;

case 2:
scr_setmode(HIGHRES);
scr_left = 0;
scr_right = 639;
scr_top = 0;
scr_bottom = 199;
scr_fg = 1;
break;

case 3:
scr_setmode(EGAHRES);
scr_left = 0;
scr_right = 639;
scr_top = 0;
scr_bottom = 349;
scr_fg = 1;
break;

case 4:
scr_setmode(EGAMONO);
scr_left = 0;
scr_right = 639;
scr_top = 0;
scr_bottom = 349;
scr_fg = 1;
break;
default:
scr_setmode(HIGHRES);
scr_left = 0;
scr_right = 639;
scr_top = 0;
scr_bottom = 199;
scr_fg = 1;
break;
}
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : graphics_on() */
/* */
/* Set screen mode < ala TECH ref > */
/* */
/*******************************************************/

void graphics_on(int gmode) {
if(ftt) { /* first time through */
scr_mode = scr_setup();
ftt++;
}
switch(gmode) {

case 16:
scr_setmode(EGAHRES);
scr_left = 0;
scr_right = 639;
scr_top = 0;
scr_bottom = 349;
scr_fg = 1;
break;

case 15:
scr_setmode(EGAMONO);
scr_left = 0;
scr_right = 639;
scr_top = 0;
scr_bottom = 349;
scr_fg = 1;
break;

case 6:
scr_setmode(HIGHRES);
scr_left = 0;
scr_right = 639;
scr_top = 0;
scr_bottom = 199;
scr_fg = 1;
break;

case 4:
scr_setmode(MEDRES);
scr_left = 0;
scr_right = 319;
scr_top = 0;
scr_bottom = 199;
scr_fg = 3;
break;

case 3:
scr_setmode(CTEXT);
scr_mode = CTEXT;
scr_fg = 7;
break;

default:
scr_setmode(HIGHRES);
scr_left = 0;
scr_right = 639;
scr_top = 0;
scr_bottom = 199;
scr_fg = 1;
break;
}
}
/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : graphics_off() */
/* */
/* Resets screen to TEXT mode */
/* */
/*******************************************************/

void graphics_off(void) {
scr_setmode(CTEXT);
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : scr_color() */
/* */
/* Sets FG & BG colors in GR mode */
/* */
/*******************************************************/

void scr_color(int p, int c) {
union REGS reg;
struct SREGS segr;

segread(&segr);
reg.x.ax = 0x0B00;
reg.x.bx =(p << 8) + c;
int86x(0x10, ®, ®, &segr);
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : set_pallette() */
/* */
/* Sets Pallette < ala BASIC > */
/* */
/*******************************************************/

void set_pallette(int set) {
union REGS reg;
struct SREGS segr;

segread(&segr);
reg.x.ax = 0x0B00;
reg.x.bx = 0x0100 + set;
int86x(0x10, ®, ®, &segr);
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : set_backgnd() */
/* */
/* Sets BackGround color */
/* */
/*******************************************************/

void set_backgnd(int kolor) {
union REGS reg;
struct SREGS segr;

segread(&segr);
reg.x.ax = 0x0B00;
reg.x.bx = kolor;
int86x(0x10, ®, ®, &segr);
}

/********************* E N D ***********************/


/* */
/* =========================================================== */


/*******************************************************/
/* */
/* FUNCTION : line() */
/* */
/* Graphics LINE fn < ala BASIC > */
/* */
/*******************************************************/

void line(int x1, int y1, int x2, int y2, int kolor) {
void pset();

int xdelta; /* change in x coordinates */
int ydelta; /* change in y coordinates */
int xstep; /* change in x in each step */
int ystep; /* change in y in each step */
int change; /* amount that x or y has changed */

xdelta = x2 - x1; /* calc change in x coordinates */
ydelta = y2 - y1; /* calc change in y coordinates */
if(xdelta < 0) { /* line drawn from right to left */
xdelta = -xdelta;
xstep = -1;
}
else /* line drawn from left to right */
xstep = 1;
if(ydelta < 0) { /* line drawn from bottom to top */
ydelta = -ydelta;
ystep = -1;
}
else /* line drawn from top to bottom */
ystep = 1;
if(xdelta > ydelta) { /* x changes quicker than y */
change = xdelta >> 1; /* change = twice value of xdelta */
while(x1 != x2) { /* Draw until terminating dot */

pset(y1, x1, kolor); /* Draw dot on the screen */
x1 += xstep; /* Update x coordinate */
change += ydelta; /* Update change */
if(change > xdelta) {
y1 += ystep; /* Update the y coordinate */
change -= xdelta; /* Reset change */
}
}
}
else {
change = ydelta >> 1; /* change = twice the value of ydelta */
while(y1 != y2) { /* Draw until terminating dot */
pset(y1, x1, kolor); /* Draw a dot on the screen */
y1 += ystep; /* Update y coordinate */
change += xdelta; /* Update change */
if(change > ydelta) {
x1 += xstep; /* Update the x coordinate */
change -= ydelta; /* Reset change */
}
}
}
}


/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : clip_line() */
/* */
/* Clips points outside screen */
/* */
/*******************************************************/

void clip_line(int *v, int vmin, int vmax) {
if(*v < vmin) {
*v = vmin;
}

if(*v > vmax) {
*v = vmax;
}
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : box() */
/* */
/* Draws graphics box < ala BASIC > */
/* */
/*******************************************************/

void box(int x1, int y1, int x2, int y2, int kolor) {
void line();
line(x1, y1, x2, y1, kolor);
line(x2, y1, x2, y2, kolor);
line(x2, y2, x1, y2, kolor);
line(x1, y2, x1, y1, kolor);
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : video_board() */
/* */
/* Returns Display type < w/EGA > */
/* */
/*******************************************************/

int video_board(void) {
union REGS reg;
struct SREGS segr;

int board,
bh,
bl,
cl;

scr_mode = scr_getmode();

segread(&segr);

/* Test for an EGA first */

reg.x.ax = 0x1200; /* Alternate Select */
reg.x.bx = 0xFF10; /* Return Info Code */
reg.x.cx = 0x000C;
int86x(0x10, ®, ®, &segr);

bh =(reg.x.bx >> 8); /* BH = CEGA or MEGA */
bl =(reg.x.bx & 0x00ff); /* BL = Memory Value */
cl =(reg.x.cx & 0x00ff); /* CL = Switch Value */


if((cl < 12) &&(bh < 2) &&(bl < 4)) {
/* EGA if TRUE */
if(bh == 0) {
board = CEGA;
}
else {
board = MEGA;
}
}

else {
if(scr_mode == TEXT) {
board = MDA;
}
else {
board = CGA;
}
}

return(board);
}

/********************* E N D ***********************/



/* =========================================================== */



/* */
/* =========================================================== */
/* */
/* K E Y B O A R D R O U T I N E S */
/* */
/* =========================================================== */
/* */
/* int scr_ci() ; */
/* int scr_key() ; */
/* */
/* =========================================================== */




/*******************************************************/
/* */
/* FUNCTION : scr_inkey() */
/* */
/* Returns key < no CR or echo > */
/* */
/*******************************************************/

int scr_inkey(void) {
int c,
al;
union REGS reg;

reg.x.ax = 0x0000;
int86(0x16, ®, ®);
al =(reg.x.ax & 0x00ff);
/* AL = 0 is Extended scan */
/* code used */
if(al == 0) {
c =(reg.x.ax >> 8) + 0x0097;
}
else {
c = al;
}
return(c);
}

/********************* E N D ***********************/




/*******************************************************/
/* */
/* FUNCTION : scr_key() */
/* */
/* INKEY$ type function */
/* */
/*******************************************************/

int scr_key(void) {
int key, lo, hi;

while(bioskey(1) == 0);

/* Function 0 to return key value */

key = bioskey(0);
lo = key & 0x00ff;
hi =(key & 0xff00);
return((lo == 0) ? hi + 256 : lo);
}

/********************* E N D ***********************/


/*******************************************************/
/* */
/* FUNCTION : scr_ci() */
/* */
/* Returns key < no CR or echo > */
/* */
/*******************************************************/

int scr_ci(void) {
int c,
al;
union REGS reg;
struct SREGS segr;

segread(&segr);

reg.x.ax = 0x0700;
int86x(0x21, ®, ®, &segr);
al =(reg.x.ax & 0x00ff);
/* AL = 0 is Extended scan */
/* code used */
if(al == 0) {
reg.x.ax = 0x0700;
int86x(0x21, ®, ®, &segr);
c =(reg.x.ax & 0x00ff) + 0x0097;
}
else {
c = al;
}
return(c);
}

/********************* E N D ***********************/


/* ================ E N D O F F I L E ============ */


  3 Responses to “Category : C Source Code
Archive   : SCRIO1.ZIP
Filename : SCRIO.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/