📄 dem2o_bp.cpp
字号:
/*******************************************************
* *
* Program : DEM2o_BP Language : Borland C++ *
* *
* Modified : 20 nov 2003 *
* *
* Copyright (c) 1988,2003, ORITECH V 5.1 *
* All rights reserved. *
* *
*******************************************************
* *
* DEM2o_BP.CPP Demo EasyTask DPMI16 *
* *
*******************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <graphics.h>
#include <dos.h>
#include <conio.h>
#include <string.h>
#include <float.h>
#include <math.h>
#include <time.h>
#include <malloc.h>
#include "etlib.h"
PMultiTask taskcontrolptr,pubptr;
void randcolor(void)
{
word colour;
do
{
colour = random(15);
} while (colour <= 8);
setcolor(colour);
}
int range(int xmin,int x, int xmax)
{
if (x < xmin) return xmin;
if (x > xmax) return xmax;
return x;
}
//=============================
class TAnim
{
public:
int num;
int numtype;
int top;
int left;
int windy;
int windx;
PMultiTask mytask;
TAnim(int anum);
virtual ~TAnim();
static void _thetask(pvoid a, TAnim &self);
virtual void thetask();
void setvport();
void Run();
static void _beforekilltask(pvoid a, TAnim &self);
void beforekilltask();
};
TAnim::TAnim(int anum)
{
num = anum;
windy = (getmaxy()-12) / 3;
windx = getmaxx() / 6;
//
Run();
}
TAnim::~TAnim()
{
if (mytask == NULL) beforekilltask();
gonexttask(mytask);
killtask(mytask);
waitkilledtask(mytask,0);
}
void TAnim::Run()
{
mytask = newtask((PTaskFunc)_thetask,8000,1,MT_CHILD,NULL,0,this);
if (mytask == NULL)
{
outtextxy(57,25," not memory:newtask");
}
}
void TAnim::_thetask(pvoid , TAnim &self)
{
self.setvport();
self.thetask();
}
void TAnim::thetask()
{
}
void TAnim::_beforekilltask(pvoid , TAnim &self)
{
self.beforekilltask();
}
void TAnim::beforekilltask()
{
clearviewport();
}
void TAnim::setvport()
{
// *** graphic window coordinate ***
top = ((num / 3) % 3) * windy;
left = (num % 6) * 100;
setviewport(left,top,left+99,top+windy-1,TRUE);
// *** draw window ***
setcolor(Black);
rectangle(0,0,99,windy-1);
setcolor(WHITE);
rectangle(0,0,99,windy-1);
setviewport(left+1,top+1,left+99-3,top+windy-1-3,TRUE);
}
//=======================
class TTask1: public TAnim // sinus drawing
{
PMultiTask mytask;
void thetask();
double mysin(double a, double k, double x);
public:
TTask1(int anum):TAnim(anum){numtype = 1;}
};
double TTask1::mysin(double a, double k, double x)
{
return (a*sin(k*x));
}
void TTask1::thetask()
{
int x0,y0,dx,dy;
int i,c;
double a,k,y,yi;
int wx;
int slowdisp;
slowdisp = random(2);
onkilltask(*currenttask,(PTaskFunc)_beforekilltask);
pausemode(MTG);
setvport();
wx = 100;
setcolor(WHITE);
do
{
c = 0;
while (c<2)
{
c++;
switch (c)
{
case 1:
randcolor();
break;
case 2:
setcolor(BLACK);
break;
}
x0 = 0;
y0 = 0;
a = (-windy / 2) +6;
k = 0.2;
y = mysin(a,k,0);
moveto(x0,y0+(int)y+(windy / 2));
i = 0;
while (i < wx-3)
{
i++;
dx = 1;
yi = mysin(a,k,i);
dy = (int)(yi-y);
linerel(dx,dy);
y = yi;
pause();
if (slowdisp) taskdelay(1);
}
}
} while (TRUE);
}
//============================
class TTask2: public TAnim // star drawing
{
PMultiTask mytask;
void thetask();
public:
TTask2(int anum):TAnim(anum){numtype = 2;}
};
void TTask2::thetask()
{
int i,c,x,y,yo;
int slowdisp;
slowdisp = random(2);
onkilltask(*currenttask,(PTaskFunc)_beforekilltask);
pausemode(MTG);
setvport();
do
{
c = 0;
while (c<2)
{
c++;
switch (c)
{
case 1:
setcolor(BLACK);
break;
case 2:
randcolor();
break;
}
yo = (windy/2);
i = 0;
do
{
moveto(50,yo);
y = 5*i;
x = 50+y;
lineto(x,y);
moveto(50,yo);
lineto(x,windy-y-3);
moveto(50,yo);
x = 50-y;
lineto(x,y);
moveto(50,yo);
lineto(x,windy-y-3);
i++;
pause();
if (slowdisp) taskdelay(1);
} while (x > 5);
}
} while (TRUE);
}
//=======================
class TTask3: public TAnim // square drawing
{
PMultiTask mytask;
void thetask();
public:
TTask3(int anum):TAnim(anum){numtype = 3;}
};
void TTask3::thetask()
{
int i,c,x1,y1,x2,y2;
int slowdisp;
slowdisp = random(2);
onkilltask(*currenttask,(PTaskFunc)_beforekilltask);
pausemode(MTG);
setvport();
do
{
c = 0;
while (c<2)
{
c++;
switch (c)
{
case 1:
setcolor(BLACK);
break;
case 2:
randcolor();
break;
}
x1 = 40;
y1 = (windy/2)-10;
x2 = 60;
y2 = (windy/2)+10;
i = 0;
while (i<8)
{
i++;
rectangle(x1,y1,x2,y2);
x1 = x1-5;
y1 = y1-5;
x2 = x2+5;
y2 = y2+5;
pause();
if (slowdisp) taskdelay(1);
}
}
} while (TRUE);
}
//=======================
class TTask4: public TAnim // spiral drawing
{
PMultiTask mytask;
void thetask();
public:
TTask4(int anum):TAnim(anum){numtype = 4;}
};
void TTask4::thetask()
{
int lmax,hmax,c,i,k,x0,y0,x1,y1;
int slowdisp;
slowdisp = random(2);
onkilltask(*currenttask,(PTaskFunc)_beforekilltask);
pausemode(MTG);
setvport();
lmax = 97;
hmax = windy-3;
do
{
k = 4;
while (k<10)
{
k++;
c = 0;
while (c<2)
{
c++;
switch (c)
{
case 2:
setcolor(BLACK);
break;
case 1:
randcolor();
break;
}
x0 = 20;
y0 = 20;
x1 = lmax - x0;
y1 = y0 - hmax;
moveto(range(0,x1,lmax),range(0,y1,hmax));
i = 10;
while (i<40)
{
i++;
lineto(range(0,x1+x0,lmax),range(0,y0-y1,hmax));
lineto(range(0,y1+x0,lmax),range(0,y0+x1,hmax));
lineto(range(0,-x1+x0,lmax),range(0,y0+y1,hmax));
lineto(range(0,-y1+x0,lmax),range(0,y0-x1,hmax));
lineto(range(0,x1+x0,lmax),range(0,y0-y1,hmax));
x1 = x1-((x1-y1)/k);
y1 = y1-((x1+y1)/k);
pause();
if (slowdisp) taskdelay(1);
}
}
}
} while (TRUE);
}
//=======================
class TTask5: public TAnim // neg spiral drawing
{
PMultiTask mytask;
void thetask();
public:
TTask5(int anum):TAnim(anum){numtype = 5;}
};
void TTask5::thetask()
{
int lmax,hmax,c,i,k,x0,y0,x1,y1;
int slowdisp;
slowdisp = random(2);
i = num;
onkilltask(*currenttask,(PTaskFunc)_beforekilltask);
pausemode(MTG);
setvport();
lmax = 97;
hmax = windy-3;
do
{
k = 4;
while (k<10)
{
k++;
c = 0;
while (c<2)
{
c++;
switch (c)
{
case 2:
setcolor(BLACK);
break;
case 1:
randcolor();
break;
}
x0 = 20;
y0 = 20;
x1 = lmax - x0;
y1 = y0 - hmax;
moveto(range(0,x1,lmax),range(0,y1,hmax));
i = 20;
while (i<40)
{
i++;
lineto(range(0,x1+x0,lmax),range(0,y0-y1,hmax));
lineto(range(0,y1+x0,lmax),range(0,y0+x1,hmax));
lineto(range(0,-x1+x0,lmax),range(0,y0+y1,hmax));
lineto(range(0,-y1+x0,lmax),range(0,y0-x1,hmax));
lineto(range(0,x1+x0,lmax),range(0,y0-y1,hmax));
x1 = x1-((x1-y1)/k);
y1 = y1-((x1+y1)/k);
pause();
if (slowdisp) taskdelay(1);
}
}
}
} while (TRUE);
}
//=============================
void dispmem(void)
{
char s[65], s2[65];
setfillstyle(SOLID_FILL,BLACK);
bar(46*8,getmaxy()-7,getmaxx(),getmaxy());
taskdelay(500);
setcolor(Yellow);
itoa(taskquantity(),s,10);
strcat(strcpy(s2,"Tasks:"),s);
if (GetEasytaskDemo()) strcat(s2," (Eval)");
outtextxy(46*8,getmaxy()-7,s2);
}
void taskcontrol(pvoid, pvoid)
{
int nt,nt2,nd,nd2,cntkill;
int nd2p;
TAnim *tasks[42];
for (nt=0; nt < 42; nt++) tasks[nt] = NULL;
cntkill = 0;
pausemode(MTG);
do
{
nt2 = 0;
taskdelay(1000); // wait 1 second
nt = random(36)+1; // task number
if (tasks[nt] != NULL)
{
cntkill++;
if (cntkill > 5)
{
cntkill = 0;
delete tasks[nt];
tasks[nt] = NULL;
dispmem();
}
}
else
{
nd = random(5)+1; // task number
if (nt <= 18)
{
if (tasks[nt+18] != NULL)
{
nd2p = tasks[nt+18]->numtype;
nt2 = tasks[nt+18]->num;
}
}
else // nt > 18
{
if (tasks[nt-18] != NULL)
{
nd2p = tasks[nt-18]->numtype;
nt2 = tasks[nt-18]->num;
}
}
nd2 = 0;
if (nd2p == 2) nd2 = 2;
if (nd2p == 3) nd2 = 3;
if (((nt2 == 0) || (tasks[nt2] == NULL)) ||
(((nd2 == 2) || (nd2 == 3)) && ((nd == 2) || (nd == 3))))
{
switch(nd)
{
case 1: tasks[nt] = new TTask1(nt); break; // sinus
case 2: tasks[nt] = new TTask2(nt); break; // star
case 3: tasks[nt] = new TTask3(nt); break; // square
case 4: tasks[nt] = new TTask4(nt); break; // spiral1
case 5: tasks[nt] = new TTask5(nt); break; // spiral2
}
if (tasks[nt] != NULL)
{
if (tasks[nt]->mytask == NULL)
{
delete tasks[nt];
tasks[nt] = NULL;
}
}
dispmem();
}
}
} while (!kbhit());
for (nt=0; nt < 42; nt++)
{
if (tasks[nt] != NULL) delete tasks[nt];
}
pause();
killalltasks();
}
void pub(pvoid, pvoid)
{
int n1,n2,n2max;
char *msg[8];
char cc[2] = {0,0};
pausemode(MTG);
msg[1] = "EASYTASK perfectly suits requirements";
msg[2] = "of scientific laboratories and";
msg[3] = "automation departments for measurement,";
msg[4] = "alarm or robotic application.";
do
{
for (n1 = 1; n1 <= 4; n1++)
{
setcolor(Yellow);
outtextxy(0,getmaxy()-7,msg[n1]);
taskdelay(3000);
setcolor(BLACK);
moveto(0,getmaxy()-7);
n2max = strlen(msg[n1]);
for (n2=0; n2 < n2max; n2++)
{
cc[0] = msg[n1][n2];
outtext(cc);
taskdelay(50);
}
}
} while (TRUE);
}
int main(void)
{
int gd,gm,ge;
etker_init();
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
ge = graphresult();
if (ge < 0)
{
cprintf("Graphic Error:%s\n\r",grapherrormsg(ge));
cprintf("Graphic device not recognize\n\r");
exit(1);
}
if (gd==CGA) setgraphmode(CGAHI);
if (gd==MCGA) setgraphmode(MCGAMED);
if (gd==EGA) setgraphmode(EGAHI);
if (gd==EGA64) setgraphmode(EGA64LO);
//
taskcontrolptr = newtask(taskcontrol,8000,1,MT_CHILD,NULL,0,NULL);
pubptr = newtask(pub,8000,1,MT_CHILD,NULL,0,NULL);
waitnotask();
//
closegraph();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -