Category : C Source Code
Archive   : MNDL.ZIP
Filename : MNDL.C

 
Output of file : MNDL.C contained in archive : MNDL.ZIP
/*
MNDL.C
Copyright 1988 R. G. Jones
Washington, DC 20007
202-333-3045

*/

#define LINT_ARGS

#include "mndlincl.h"
#include "mndldefn.h"
#include "mndlextr.h"
#include "mndlprot.h"

main(ac, av)
int ac;
char **av;
{
int ic, stat,
ifsdraw(), modeset(), crash(), outifs(), getifs(),
shoit(), poltomat(), mattopol(), scaleifs(),
scrnparm(), draw(), getopt();
unsigned seed;
void srand(), exit();
FILE *fp;

SW = '/';
seed = 1234;
srand(seed);
mode0 = modeget();
while( (ic = getopt(ac, av, "m:d:p:a:")) != EOF) {
switch(ic) {
case 'd': sscanf(optarg, "%s", scrnfil); break;
case 'm': sscanf(optarg, "%d", &mndlmax); break;
case 'a': sscanf(optarg, "%f", &sar); break;
case 'p':
if (4 != sscanf(optarg,"%lf %lf %lf %lf",
&(gcs.x0), &(gcs.y0), &(gcs.x1), &(gcs.y1))
) crash("main: args don't match"); break;
default : crash("Invalid Argument"); break;
}
}
/* for(ic=optind; av[ic]; ic++) {
fp = fopen(av[ic], "rt");
if (!fp)
perror(av[ic]);
else {
getifs(fp, av[ic], &ifs1);
if (debug) outifs(&ifs1);
}
}
*/
if (scrnparm(gmode,scrn)) crash("\nGraphics Mode Not Available");
pageset(TXTPG);
shocore();
if (!(gcs.p = newarray())) perror("Main memcall ");
shocore();
initcs(&gcs);
shocs(&gcs);
draw(&gcs);
exit(0);
}

draw(pcs)
CLCSV *pcs;
{
int stat, modeget(), modeset(), dokey();

stat = FALSE;
if (modeget() != gmode) modeset(gmode);
while(!stat) {
pageset(GRFPG);
SETTXT;
crloc(SROW, 0);
mndlcalc(pcs);
pageset(TXTPG);
SETTXT;
clswndw(SROW, 0, 24, 79, TATT);
crloc(SROW, 0);
if (kbhit()) stat = dokey(pcs);
else waite();
}
modeset(mode0);
return (stat == 1 ? FALSE : stat);
}

initcs(p)
CLCSV *p;
{
double test, delta2;
double x0, y0, x1, y1;


x0 = MIN(p->x0, p->x1);
x1 = MAX(p->x0, p->x1);
y0 = MIN(p->y0, p->y1);
y1 = MAX(p->y0, p->y1);
p->x0 = x0;
p->x1 = x1;
p->y0 = y0;
p->y1 = y1;
p->sx = p->sy = 0;
p->xtest = p->ytest = 0;
test = (p->x1 - p->x0)/(p->y1 - p->y0);
if (test < ar) {
delta2 = ((p->y1 - p->y0)*ar - (p->x1 - p->x0))/2.0;
p->x0 -= delta2;
p->x1 += delta2;
} else if (test >ar) {
delta2 = ((p->x1 - p->x0)/ar - (p->y1 - p->y0))/2.0;
p->y0 -= delta2;
p->y1 += delta2;
}
p->xscl = (p->x1 - p->x0)/(double)sxmax;
p->yscl = (p->y1 - p->y0)/(double)symax;
p->clrfac = clrmax/mndlmax;
return;
}

shocs(p)
CLCSV *p;
{
printf("Plane segment: %10.4g %10.4g %10.4g %10.4g\n",
p->x0, p->y0, p->x1, p->y1);
printf("Current Point: %10.4g %10.4g\n", p->xtest, p->ytest);
printf("Screen Coords: %10d %10d\n", p->sx, p->sy);
printf("Scale Factors: %10.3g %10.3g\n", p->xscl, p->yscl);
printf("gxul: %d gyul: %d gbxsiz: %d gbmov: %d gbxsiz*symax/sxmax %d\n",
gxul, gyul, gbxsiz, gbmov,
(int)((float)gbxsiz*(float)symax/(float)sxmax));

}

mndlcalc(pcs)
CLCSV *pcs;
{
int sx, sy;
PHFDAT t, p;
double xscl, yscl, clrfac, xtest, ytest, x0, y0;

sx = pcs->sx; sy = pcs->sy;
x0 = pcs->x0; y0 = pcs->y0;
t = pcs->t; p = pcs->p;
xscl = pcs->xscl; yscl = pcs->yscl;
xtest = pcs->xtest; ytest = pcs->ytest;
clrfac = pcs->clrfac;

t = p + (unsigned long)((unsigned long)sy*(unsigned long)(sxmax+1));

for( ; sy <= symax && !kbhit(); sy++) {
ytest = y0 + yscl * (double)(symax - sy);
xtest = x0;
for( sx=0; sx <= sxmax && !kbhit(); sx++) {
*(t) = (char)(mndl( xtest, ytest));
if (debug)
printf( "\r%4d %4d %12.3g %12.3g %5x ",
sx, sy, xtest, ytest, *t);
t += 1;
xtest = xtest + xscl;
}
}
pcs->sx = sx; pcs->sy = MAX(sy-1, 0);
pcs->t = t; pcs->p = p;
pcs->xscl = xscl; pcs->yscl = yscl;
pcs->xtest = xtest; pcs->ytest = ytest;
pcs->clrfac = clrfac;
return;
}

mndl(x0,y0)
double x0, y0;
{
int i;
double x, y, x2, y2;

x = x2 = y = y2 = 0;
for(i=0; i < mndlmax && (x2 + y2) <= 4.0; i++) {
y = 2.0*x*y + y0;
x = x2 - y2 + x0;
x2 = x * x;
y2 = y * y;
}
return (i == mndlmax ? 0 : i);

}

drawmndl(p)
PHFDAT p;
{
int sx, sy;
PHFDAT t;
unsigned char mask = 0x80, exist_clr;
char far *base;
unsigned long base0;

pageset(GRFPG);
SETGRF;
t = p;
for(sy=symin; sy<=symax; sy++) {
base0 = (EGABASE + ((long)sy * WIDTH));
for(sx = sxmin; sx <= sxmax; sx++) {
base = (char far *)(base0 + ((long)sx/8L));
mask = 0x80 >> (sx % 8);
exist_clr = *base; /* Existing clr into EGA register */
OUTINDEX(0, *t); /* Set new Color */
OUTINDEX(8, mask); /* Set Mask */
*base &= TRUE; /* Force a write to the EGA */
t += 1;
}
}
SETTXT;
}

mndlfil(p, read)
CLCSV *p;
int read;
{
FILE *fp;
PHFDAT t, end;
unsigned wbytes;
int count, stat;


if (read)
fp = fopen(scrnfil, "rb");
else
fp = fopen(scrnfil, "wb");
if (!fp) crash("Mndlfil: fopen failure; ");
t = p->p;
end = t +
(unsigned long)((unsigned long)(symax+1) *
(unsigned long)(sxmax+1));
for(count = 0; t < end && count <= 20; count++) {
wbytes = (unsigned)MIN( 0xFFFFL - (unsigned long)t % 0x10000L,
(unsigned long)(end - t)
);
printf("\rmndlwrt: t: %lx end: %lx end - t %8lx wbytes %5x ",
t, end, end-t, wbytes);
if (read)
stat = fread((void far *)t, wbytes, 1, fp);
else
stat = fwrite((void far *)t, wbytes, 1, fp);
if (!stat) crash("Mndlwrt: fwrite error ");
t = t + wbytes;
}
if (read) {
fscanf(fp, "%lf%lf%lf%lf\r\n\r\n",
&(p->x0), &(p->y0), &(p->x1), &(p->y1));
initcs(p);
} else
fprintf(fp,
"\r\n\r\n%g\t%g\r\n%g\t%g\r\n%d\t%d\r\n%g\t%g\r\n\r\n",
(p->x0), (p->y0), (p->x1), (p->y1), p->xscl,
p->yscl);
fclose(fp);
}

shocore()
{
unsigned long bytes;

bytes = farcoreleft();
printf("\nshocore: %lx %lu\n", bytes, bytes);
return;
}

PHFDAT newarray()
{
PHFDAT t;

t = (PHFDAT)farcalloc((unsigned)(sxmax+1), (unsigned)(symax+1));
if (!t) {
perror("newarray: ");
exit(1);
}
return t;
}

scalmndl(p)
PHFDAT p;
{
int pmax, row, col;
double scalfac;

pmax = 0;
for(row = sxmin; row <= sxmax; row++)
for(col = symin; col <= symax; col++)
pmax = MAX(pmax, *(p+row*sxmax+col));
scalfac = (double)clrmax/(double)pmax;
for(row = sxmin; row <= sxmax; row++)
for(col = symin; col <= symax; col++)
*(p+row*sxmax+col) *= scalfac;
}

scrnparm(gm, sc)
int gm;
SCRNDAT sc[];
{
int i, stat;

stat = TRUE;
for(i=0; sc[i].mode > 0; i++)
if (sc[i].mode == gm) {
sxmax = sc[i].xmax;
symax = sc[i].ymax;
clrmax = sc[i].color;
ar = (double)sxmax/((double)symax*sar);
stat = FALSE;
}
return stat;
}

shoit(s)
char **s;
{
while(**s) fputs(*s++, stderr);
}


crash(s)
char *s;
{
int modeset();
void exit(), perror();
extern char *usage[];

modeset(mode0);
perror(s);
shoit(usage);
exit(1);
}

xmsg(s1, s2)
char *s1, *s2;
{
int modeset();
void exit();

puts(s1);
puts(s2);
puts("Terminating on error");
exit(1);
}

waite()
{
int c;

while(kbhit()) getch();
fprintf(stderr, "\nHit any key...");
while(!kbhit());
c = getch(); if (c == 0) c = getch();
return(c);
}

wait()
{
int c;

while(kbhit()) getch();
while(!kbhit());
c = getch(); if (c == 0) c = getch();
return(c);
}

getint(pf, s)
int *pf;
char *s;
{
int stat;
extern char cbuf[];

stat = FALSE;
strcpy(cbuf, s);
strcat(cbuf, "[%d]? ");
fprintf(stderr, cbuf, *pf);
if (gets(cbuf))
stat = (sscanf(cbuf, "%d", pf) == 1);
return stat;
}

getflt(pf, s)
float *pf;
char *s;
{
int stat;
extern char cbuf[];

stat = FALSE;
strcpy(cbuf, s);
strcat(cbuf, "[%f]? ");
fprintf(stderr, cbuf, *pf);
if (gets(cbuf))
stat = (sscanf(cbuf, "%f", pf) == 1);
return stat;
}

modeget()
{
union REGS regs;

regs.h.al = 0;
regs.h.ah = 0x0f;
int86(0x10, ®s, ®s);
return(regs.h.al);
}

modeset(mode)
int mode;
{
union REGS regs;

regs.h.al = mode;
regs.h.ah = 0;
int86(0x10, ®s, ®s);
}

pageget()
{
union REGS inregs, outregs;

inregs.h.ah = 0x0f;
int86(0x10, &inregs, &outregs);
return(outregs.h.bh);
}

cls()
{
union REGS inregs;

inregs.h.ah = 6; /* scroll */
inregs.h.al = 0; /* blank */
inregs.h.bh = 0; /* attribute */
inregs.h.ch = 0;
inregs.h.cl = 0;
inregs.h.dh = 24;
inregs.h.dl = 79;
int86(0x10, &inregs, &inregs);
crloc(0,0);
}

clswndw(r1, c1, r2, c2, att)
int r1, c1, r2, c2, att;
{
union REGS inregs;

inregs.h.ah = 6; /* scroll */
inregs.h.al = 0; /* blank */
inregs.h.bh = att; /* attribute */
inregs.h.ch = r1;
inregs.h.cl = c1;
inregs.h.dh = r2;
inregs.h.dl = c2;
int86(0x10, &inregs, &inregs);
}

crloc(row,col)
int row, col;
{
union REGS inreg, outreg;

inreg.h.ah = 2;
inreg.h.dh = row;
inreg.h.dl = col;
inreg.h.bh = pageget(); /* Page number; 0 for graphics modes */
int86(0x10, &inreg, &outreg);
}

pageset(n)
int n;
{
union REGS reg;

reg.h.ah = 5;
reg.h.al = n;
int86(0x10, ®, ®);
}

dohlp(p)
char **p;
{
crloc(0,0);
shoit(p);
crloc(SROW,0);
}



  3 Responses to “Category : C Source Code
Archive   : MNDL.ZIP
Filename : MNDL.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/