TempleOSTS_140317.ISO

This commit is contained in:
Terry A. Davis authored and minexew committed 2018-09-08 21:46:02 +02:00
1 parent 2d2a65d572
commit 7ef616a20e
1416 files changed
+223873 -53917

No files matched your search

+117
View File
@@ -0,0 +1,117 @@
U0 Main()
{
F64 é=0,č=0,ę=0;
CD3I32 poly[4];
CDC *dc=DCAlias;
DCAllocDepthBuf(dc);
dc->flags|=DCF_TRANSFORMATION|DCF_SYMMETRY;
try {
while (!ScanChar) {
DCDepthBufRst(dc);
GrSetSymmetry(dc,Fs->pix_width/2,Fs->pix_height/2,
ipx-Fs->pix_left-Fs->scroll_x,
ipy-Fs->pix_top-Fs->scroll_y);
dc->color=LTBLUE;
GrLine(dc,Fs->pix_width/2,Fs->pix_height/2,
ipx-Fs->pix_left-Fs->scroll_x,
ipy-Fs->pix_top-Fs->scroll_y);
GrIdentEqu(dc->r);
GrRotXEqu(dc->r,ę);
GrRotYEqu(dc->r,č);
GrRotZEqu(dc->r,é);
GrSetRotMat(dc,dc->r);
dc->x=Fs->pix_width/2;
dc->y=Fs->pix_height/2;
dc->z=500;
dc->color=GREEN;
poly[0].x=50;
poly[0].y=150;
poly[0].z=0;
poly[1].x=275;
poly[1].y=100;
poly[1].z=0;
poly[2].x=155;
poly[2].y=200;
poly[2].z=0;
poly[3].x=100;
poly[3].y=200;
poly[3].z=0;
GrFillPoly3(dc,4,poly);
dc->color=RED;
poly[0].x=50;
poly[0].y=150;
poly[0].z=50;
poly[1].x=275;
poly[1].y=100;
poly[1].z=50;
poly[2].x=155;
poly[2].y=200;
poly[2].z=50;
poly[3].x=100;
poly[3].y=200;
poly[3].z=50;
GrFillPoly3(dc,4,poly);
dc->color=BLUE;
poly[0].x=50;
poly[0].y=150;
poly[0].z=-50;
poly[1].x=275;
poly[1].y=100;
poly[1].z=-50;
poly[2].x=155;
poly[2].y=200;
poly[2].z=-50;
poly[3].x=100;
poly[3].y=200;
poly[3].z=-50;
GrFillPoly3(dc,4,poly);
dc->color=RED+GREEN<<16+ROPF_DITHER;
poly[0].x=50;
poly[0].y=150;
poly[0].z=50;
poly[1].x=275;
poly[1].y=100;
poly[1].z=50;
poly[2].x=275;
poly[2].y=100;
poly[2].z=0;
poly[3].x=50;
poly[3].y=150;
poly[3].z=0;
GrFillPoly3(dc,4,poly);
dc->color=BLUE+GREEN<<16+ROPF_DITHER;
poly[0].x=100;
poly[0].y=200;
poly[0].z=-50;
poly[1].x=155;
poly[1].y=200;
poly[1].z=-50;
poly[2].x=155;
poly[2].y=200;
poly[2].z=0;
poly[3].x=100;
poly[3].y=200;
poly[3].z=0;
GrFillPoly3(dc,4,poly);
WinMgrSync;
DCFill;
é+=2.1*ă/180.0;
č+=1.1*ă/180.0;
ę+=0.5*ă/180.0;
}
} catch
PutExcept;
DCFill;
DCDel(dc);
}
Main;
+33
View File
@@ -0,0 +1,33 @@
U0 BSplineDemo()
{
CDC *dc=DCAlias;
I64 p1,p2,i=0,msg_code;
CD3I32 c[64];
SettingsPush;
Fs->win_inhibit=WIG_TASK_DFT-WIF_FOCUS-WIF_BORDER;
DocClear;
"Select ctrl points with left mouse. Right when finished.\n";
DCFill;
do {
msg_code=GetMsg(&p1,&p2,1<<MSG_IP_L_UP+1<<MSG_IP_R_UP);
if (msg_code==MSG_IP_L_UP) {
c[i].x=p1; c[i].y=p2; c[i].z=0;
dc->color=GREEN;
GrCircle(dc,p1,p2,10);
i++;
}
} while (msg_code!=MSG_IP_R_UP);
dc->color=RED;
Gr2BSpline(dc,c,i,TRUE);
dc->color=BLUE;
Gr3BSpline(dc,c,i,TRUE);
DCDel(dc);
SettingsPop;
PressAKey;
DCFill;
}
BSplineDemo;
+38
View File
@@ -0,0 +1,38 @@
// Classic Commodore 64 Sprite
U8 a[60]={0,127,0,1,255,192,3,255,224,3,231,224,
7,217,240,7,223,240,7,217,240,3,231,224,
3,255,224,3,255,224,2,255,160,1,127,64,
1,62,64,0,156,128,0,156,128,0,73,0,0,73,0,
0,62,0,0,62,0,0,28,0};
U0 Balloon()
{
I64 i,j,k;
CLI
OutU8(VGA_SC_INDEX,VGA_MAP_MASK);
OutU8(VGA_SC_DATA,0xF);
MemSetU64(0xA0000,0,640*480/64);
OutU8(VGA_SC_INDEX,VGA_MAP_MASK);
OutU8(VGA_SC_DATA,RED+GREEN+8);
for (k=100;k<150;k++) {
for (i=0;i<20;i++)
for (j=0;j<3;j++)
*(0xA0000+0x1000+(i+k)*640/8+j)(U8 *)=a[i*3+j];
BusyWait(40000);
for (i=0;i<20;i++)
for (j=0;j<3;j++)
*(0xA0000+0x1000+(i+k)*640/8+j)(U8 *)=0;
}
STI
VGAFlush;
}
Balloon;
+41
View File
@@ -0,0 +1,41 @@
CDC *dc=DCAlias;
CDC *img1=DCNew(64,64);
CDC *img2=DCNew(512,512);
img1->color=COLOR_MONO;
img1->pen_width=3;
GrCircle(img1,30,30,20);
GrFloodFill3(img1,30,30,0,TRUE);
GrLine3(img1,60,0,0,0,60,0);
DCFill(img2);
img2->color=RED;
img2->brush=img1;
GrCircle(img2,256-30,256-30,128,67);
img2->color=BLUE;
img2->pen_width=6;
img2->brush=NULL;
GrLine3(img2,128,128,0,256+128,256+128,0);
GrLine3(img2,256+128,128,0,128,256+128,0);
img2->color=YELLOW;
GrRect(img2,256-20,256-20,40,40);
I64 i;
for (i=0;i<256;i+=4) {
WinMgrSync;
DCFill;
GrBlot(dc,i-256,i-256,img2);
Sleep(10);
}
U8 *tempg=DC2Sprite(img1);
Sprite(tempg);
Free(tempg);
NewLine(9);
DCDel(img1);
DCDel(img2);
DCDel(dc);
PressAKey;
DCFill;
+57
View File
@@ -0,0 +1,57 @@
//This uses $LK,"fixed-point-arithmetic",A="FI:::/Demo/Lectures/FixedPoint.CPP"$.
I64 x[16],y[16],dx[16],dy[16];
U0 Init()
{
I64 i;
F64 é;
MemSet(x,0,sizeof(x));
MemSet(y,0,sizeof(y));
for (i=0;i<16;i++) {
é=Rand*2*ã;
dx[i]=MAX_I32*Cos(é);
dy[i]=MAX_I32*Sin(é);
}
}
U0 Bounce()
{
CDC *dc=DCAlias;
I64 i,msg_code,p1,update;
Init;
try { //Catch <CTRL-ALT-C>
while (TRUE) {
update=win_updates;
do for (i=0;i<16;i++) {
dc->color=i;
GrPlot(dc,x[i].i32[1],y[i].i32[1]);
x[i]+=dx[i];
y[i]+=dy[i];
if (!(0<=x[i]<Fs->pix_width<<32)) {
x[i]-=dx[i];
dx[i]=-dx[i];
}
if (!(0<=y[i]<Fs->pix_height<<32)) {
y[i]-=dy[i];
dy[i]=-dy[i];
}
}
while (update==win_updates);
while (msg_code=ScanMsg(&p1,NULL,1<<MSG_KEY_DOWN|1<<MSG_RESIZE|1<<MSG_MOVE))
if (msg_code==MSG_KEY_DOWN) {
if (p1==CH_SHIFT_ESC || p1==CH_ESC)
goto bc_done;
else
Init;
} else
DCFill(dc);
}
bc_done:
} catch
PutExcept;
DCFill(dc);
DCDel(dc);
}
Bounce;
+54
View File
@@ -0,0 +1,54 @@
CDC *dc;
U0 DrawIt(CTask *,CDC *)
{//This technique is weird, but clever. In Box() we
//created an alias of the system $LK,"gr_dc2",A="MN:gr_dc2"$.
dc->pen_width=2;
dc->color=RED;
GrLine3(dc,-100,-100,-100, -100, 100,-100);
GrLine3(dc,-100, 100,-100, 100, 100,-100);
GrLine3(dc, 100, 100,-100, 100,-100,-100);
GrLine3(dc, 100,-100,-100, -100,-100,-100);
GrLine3(dc,-100,-100, 100, -100, 100, 100);
GrLine3(dc,-100, 100, 100, 100, 100, 100);
GrLine3(dc, 100, 100, 100, 100,-100, 100);
GrLine3(dc, 100,-100, 100, -100,-100, 100);
GrLine3(dc,-100,-100, 100, -100,-100,-100);
GrLine3(dc,-100, 100, 100, -100, 100,-100);
GrLine3(dc, 100, 100, 100, 100, 100,-100);
GrLine3(dc, 100,-100, 100, 100,-100,-100);
}
U0 Box()
{
F64 é=0,è=0,ê=0,s=1,s1=1.05;
dc=DCAlias(gr_dc2,Fs);
SettingsPush; //See $LK,"SettingsPush",A="MN:SettingsPush"$
DocClear;
Fs->draw_it=&DrawIt;
Preempt(OFF); //We don't want matrix used while we're changing it.
dc->x=200;
dc->y=200;
dc->flags|=DCF_TRANSFORMATION;
while (!ScanChar) {
GrIdentEqu(dc->r);
GrRotZEqu(dc->r,é);
GrRotXEqu(dc->r,è);
GrRotZEqu(dc->r,ê);
GrScaleEqu(dc->r,s);
GrSetRotMat(dc,dc->r);
Sleep(20);
é+=2*ã/70;
è+=2*ã/90;
ê+=2*ã/110;
s*=s1;
if ( !(0.2<s<1.4) ) s1=1/s1;
}
SettingsPop;
DCDel(dc);
}
Box;
Binary file not shown.
+27
View File
@@ -0,0 +1,27 @@
//See $LK,"::/Demo/Games/Stadium/StadiumGen.CPP"$.
U0 ViewScreenCapture(U8 *filename)
{
CDC *dc=DCAlias(,Fs),
*dc2=GRARead(filename);
GrBlot(dc,0,0,dc2);
DCDel(dc2);
DCDel(dc);
}
U0 Main()
{
"Capture screen...\n";
PressAKey;
GRACaptureScreen("HOME/ScreenShot");
"View captured screen...\n";
PressAKey;
ViewScreenCapture("HOME/ScreenShot");
PressAKey;
DCFill;
}
Main;
+48
View File
@@ -0,0 +1,48 @@
//x must be global.
//Might as well make y global.
F64 x,y,y_last;
U0 Cartesian()
{
U8 *st;
I64 ch=0,h,v;
Bool first;
CDC *dc=DCAlias;
do {
DocClear;
"Enter algebraic equation to graph.\n"
"Example: y=2*x\n"
"y=";
if (st=MGetS) {
if (*st) {
h=Fs->pix_width/2;
v=Fs->pix_height/2;
dc->color=RED;
GrLine(dc,h,0,h,2*v+1);
GrLine(dc,0,v,2*h+1,v);
first=TRUE;
dc->color=BLACK;
for (x=-h;x<=h;x++) {
y=-ExePrint("ToF64(%s);",st)(F64);
if (!first)
GrLine(dc,x-1+h,y_last+v,x+h,y+v);
y_last=y;
first=FALSE;
}
ch=GetChar;
} else
ch=CH_SHIFT_ESC;
Free(st);
} else
ch=CH_SHIFT_ESC;
DCFill;
} while (ch!=CH_SHIFT_ESC && ch!=CH_ESC);
DCDel(dc);
}
Cartesian;
/*$WW,1$
For better performance, compile the expression entered by the user one time with $LK,"LexExpression2Bin",A="MN:LexExpression2Bin"$() and use $LK,"Call",A="MN:Call"$(). See $LK,"::/Demo/CompileDemo.CPP"$.
*/
+35
View File
@@ -0,0 +1,35 @@
U8 *old_font=gr_font;
// See $LK,"::/Kernel/Font8x8.CPP"$.
// See $LK,"::/Demo/ScreenCodes.CPP"$.
// See $LK,"::/Demo/ExtendedChars.CPP"$.
// See $LK,"::/Demo/Graphics/FontEd.CPP"$.
U8 waves[8*4]={
0x00,0x11,0xAA,0x44,0x00,0x11,0xAA,0x44,
0x00,0x22,0x55,0x88,0x00,0x22,0x55,0x88,
0x00,0x44,0xAA,0x11,0x00,0x44,0xAA,0x11,
0x00,0x88,0x55,0x22,0x00,0x88,0x55,0x22};
U0 AnimateEndCB()
{
gr_font=old_font;
Exit;
}
U0 AnimateTask(I64)
{
I64 i;
U8 *font=MAlloc(256*8);
Fs->task_end_cb=&AnimateEndCB;
MemCpy(font,gr_font,256*8);
gr_font=font;
while (TRUE) {
MemCpy(font+CH_SPACE*FONT_HEIGHT,waves+8*(i++&0x3),8);
Sleep(100);
}
}
Spawn(&AnimateTask,NULL,"Animate",,Fs);
TaskRep;
+119
View File
@@ -0,0 +1,119 @@
#define N 32
class Node
{
Node *left,*right;
I64 n;
};
I64 n1,n2,common_ancestor;
Node *root;
#define X_SPACING 16
#define Y_SPACING 45
#define ARROW_SPACING 3
U0 ShowTree(CDC *dc,Node *tempn,I64 *_node_x,I64 *_tree_x,I64 y)
{
I64 node_x;
if (tempn) {
if (tempn->left) {
ShowTree(dc,tempn->left,&node_x,_tree_x,y+Y_SPACING);
dc->color=BLUE;
GrArrow3(dc,*_tree_x,y,0,node_x+ARROW_SPACING,y+Y_SPACING-ARROW_SPACING,0);
}
if (tempn->n==n1 || tempn->n==n2) {
if (tempn->n==common_ancestor)
dc->color=YELLOW;
else
dc->color=RED;
} else if (tempn->n==common_ancestor)
dc->color=GREEN;
else
dc->color=BLUE;
*_node_x=*_tree_x;
GrPrint(dc,*_node_x,y,"%d",tempn->n);
*_tree_x+=X_SPACING;
if (tempn->right) {
ShowTree(dc,tempn->right,&node_x,_tree_x,y+Y_SPACING);
dc->color=BLUE;
GrArrow3(dc,*_node_x,y,0,node_x-ARROW_SPACING,y+Y_SPACING-ARROW_SPACING,0);
}
}
}
U0 DrawIt(CTask *,CDC *dc)
{
I64 node_x=0,tree_x=0;
ShowTree(dc,root,&node_x,&tree_x,20);
}
U0 TreeAdd(Node **_root,Node *tempn)
{
Node *root=*_root;
if (!root)
*_root=tempn;
else if (tempn->n==root->n)
Free(tempn);
else if (tempn->n<root->n)
TreeAdd(&root->left,tempn);
else
TreeAdd(&root->right,tempn);
}
U0 TreeNew()
{
I64 i;
Node *tempn;
for (i=0;i<N;i++) {
tempn=CAlloc(sizeof(Node));
tempn->n=RandU16%N;
if (i==N-1)
n1=tempn->n;
else if (i==N-2)
n2=tempn->n;
TreeAdd(&root,tempn);
Sleep(50);
}
}
U0 TreeCommonAncestorFind(Node *root)
{
if (root && root->n!=n1 && root->n!=n2) {
common_ancestor=root->n;
if (n1<root->n && n2<root->n)
TreeCommonAncestorFind(root->left);
else if (n1>root->n && n2>root->n)
TreeCommonAncestorFind(root->right);
}
}
U0 TreeCommonAncestor()
{ //Make tree and find common ancestor to n1 & n2.
root=NULL;
n1=n2=common_ancestor=0;
SettingsPush; //See $LK,"SettingsPush",A="MN:SettingsPush"$
Fs->draw_it=&DrawIt;
DocClear;
"Scroll with {CTRL-Left Grab}.\n";
try {
TreeNew;
TreeCommonAncestorFind(root);
PressAKey;
} catch
PutExcept;
SettingsPop;
}
TreeCommonAncestor;
/*
Be careful with recursive routines in TempleOS
because the stack does not grow and will overflow.
See $LK,"::/Demo/StkGrow.CPP"$.
*/
+16
View File
@@ -0,0 +1,16 @@
U0 Main()
{
CDC *dc=DCAlias;
dc->color=RED+BLACK<<16+ROPF_DITHER;
GrCircle(dc,100,100,50);
GrLine(dc,10,10,280,70);
dc->color=RED+BLUE<<16+ROPF_DITHER;
GrFloodFill(dc,100,100);
dc->color=PURPLE+BLUE<<16+ROPF_DITHER;
GrRect(dc,20,20,100,100);
PressAKey;
DCFill;
DCDel(dc);
}
Main;
+49
View File
@@ -0,0 +1,49 @@
//This is a drawing program
U0 Doodle()
{
I64 msg_code,i,x1,y1,x2,y2,p1,p2,color=BLACK;
CDC *dc=DCAlias;
SettingsPush; //See $LK,"SettingsPush",A="MN:SettingsPush"$
Fs->win_inhibit=WIG_TASK_DFT-WIF_FOCUS-WIF_BORDER;
WinMax;
WordStat;
DCFill;
DocClear;
do {
msg_code=GetMsg(&p1,&p2,
1<<MSG_KEY_DOWN+1<<MSG_IP_L_DOWN+
1<<MSG_IP_R_UP);
switch (msg_code) {
case MSG_IP_R_UP:
i=PopUpColor;
if (i>=0) color=i;
break;
case MSG_IP_L_DOWN:
x1=p1; y1=p2;
x2=p1; y2=p2;
dc->color=ROP_XOR+color^COLOR_TRANSPARENT;
dc->pen_width=1;
while (msg_code!=MSG_IP_L_UP) {
GrLine3(dc,x1,y1,0,x2,y2,0);
msg_code=GetMsg(&p1,&p2,
1<<MSG_IP_L_UP+1<<MSG_IP_MOVE);
GrLine3(dc,x1,y1,0,x2,y2,0);
x2=p1; y2=p2;
}
GrLine3(dc,x1,y1,0,x2,y2,0);
break;
case MSG_KEY_DOWN:
break;
}
} while (msg_code!=MSG_KEY_DOWN || !p1);
GetMsg(,,1<<MSG_KEY_UP);
DCFill;
DCDel(dc);
SettingsPop;
}
Doodle; //Execute when #included
+16
View File
@@ -0,0 +1,16 @@
//Press F5 to compile and run
U0 DotNet()
{
I64 i;
CDC *dc=DCAlias;
DocClear; //Clear text
dc->color=RED;
for (i=0;i<480;i+=20)
GrLine(dc,i,0,0,480-i,3);
DCDel(dc);
PressAKey;
DCFill;
}
DotNet; //Run when #included
Binary file not shown.
Binary file not shown.
Binary file not shown.
+147
View File
@@ -0,0 +1,147 @@
//After making a font, you can save it as a binary
//file with:
// FileWrite("filename.BIN.Z",gr_font,256*FONT_HEIGHT);
//You can load it with:
// U8 *myfont=FileRead("filename.BIN");
// gr_font=myfont; //preserve gr_font if you want to go back.
//If you want to change the system font permanently,
//save to a file with this font editor program
//and cut and paste the code into $LK,"::/Kernel/Font8x8.CPP"$.
//You will need to recompile Kernel by calling $LK,"MakeOSPrtBootInstall",A="MN:MakeOSPrtBootInstall"$().
//See $LK,"::/Demo/ExtendedChars.CPP"$ and
//$LK,"::/Demo/ScreenCodes.CPP"$.
#define BLOW_UP_CHAR_X (18*FONT_WIDTH)
#define BLOW_UP_CHAR_Y (2*FONT_HEIGHT)
U8 cur_ch;
U0 DrawIt(CTask *task,CDC *dc)
{
I64 i,j,k,c;
k=0;
for (i=0;i<16;i++)
for (j=0;j<16;j++) {
if (k==cur_ch) {
if (Blink)
c=(BLUE<<4+YELLOW)<<8 + k++;
else
c=(YELLOW<<4+BLUE)<<8 + k++;
} else
c=(BLUE<<4+WHITE)<<8 + k++;
PlotWinU8(task,c,j,i);
}
k=0;
for (i=0;i<FONT_HEIGHT;i++)
for (j=0;j<FONT_WIDTH;j++) {
if (Bt(&gr_font[FONT_HEIGHT*cur_ch],k++))
dc->color=YELLOW;
else
dc->color=BLUE;
GrRect(dc,BLOW_UP_CHAR_X+j*FONT_WIDTH,
BLOW_UP_CHAR_Y+i*FONT_HEIGHT,
FONT_WIDTH,FONT_HEIGHT);
}
}
U0 FESave(Bool pmt)
{
U8 old_draw_it=Fs->draw_it;
CDoc *doc=DocNew;
I64 i,j,k=0;
for (i=0;i<256;i++) {
for (j=0;j<FONT_HEIGHT;j++)
DocPrint(doc,"0x%02X,",gr_font[k++]);
if (Bt(chars_bitmap_safe_dollar,i))
DocPrint(doc,"//%c",i);
else if (i=='$$')
DocPrint(doc,"//$$$$",i);
DocPrint(doc,"\n");
}
Fs->draw_it=NULL;
DocWrite(doc,pmt);
Fs->draw_it=old_draw_it;
DocDel(doc);
}
U0 FontEd()
{
I64 msg_code,p1,p2,k;
SettingsPush; //See $LK,"SettingsPush",A="MN:SettingsPush"$
MenuPush(
"File {"
" SaveAs(,CH_CTRLA);"
" Abort(,CH_SHIFT_ESC);"
" Exit(,CH_ESC);"
"}");
WordStat;
DocClear;
DocCursor;
Fs->win_inhibit|=WIG_DBL_CLICK;
cur_ch=0;
try {
Fs->draw_it=&DrawIt;
while (TRUE) {
switch (msg_code=GetMsg(&p1,&p2,1<<MSG_KEY_DOWN|1<<MSG_IP_L_DOWN|1<<MSG_IP_R_DOWN|1<<MSG_IP_MOVE)) {
case MSG_KEY_DOWN:
switch (p1) {
case 0:
switch (p2.u8[0]) {
case SC_CURSOR_LEFT:
cur_ch--;
break;
case SC_CURSOR_RIGHT:
cur_ch++;
break;
case SC_CURSOR_UP:
cur_ch-=16;
break;
case SC_CURSOR_DOWN:
cur_ch+=16;
break;
}
break;
goto fe_done;
case CH_CTRLA:
FESave(TRUE);
break;
case CH_ESC:
FESave(FALSE);
case CH_SHIFT_ESC:
goto fe_done;
default:
cur_ch=p1;
}
break;
case MSG_IP_L_DOWN:
case MSG_IP_R_DOWN:
if (0<=p1<FONT_WIDTH*16 &&
0<=p2<FONT_HEIGHT*16) {
cur_ch=(p2/FONT_HEIGHT)*16+p1/FONT_WIDTH;
break;
} //fall through
case MSG_IP_MOVE:
k=((p2-BLOW_UP_CHAR_Y)/FONT_HEIGHT)*FONT_WIDTH+
(p1-BLOW_UP_CHAR_X)/FONT_WIDTH;
if (0<=k<FONT_WIDTH*FONT_HEIGHT) {
if (ip_lb||msg_code==MSG_IP_L_DOWN)
Bts(&gr_font[FONT_HEIGHT*cur_ch],k);
if (ip_rb||msg_code==MSG_IP_R_DOWN)
Btr(&gr_font[FONT_HEIGHT*cur_ch],k);
}
break;
}
}
fe_done:
GetMsg(,,1<<MSG_KEY_UP);
} catch
PutExcept;
MenuPop;
SettingsPop;
}
FontEd;
+141
View File
@@ -0,0 +1,141 @@
#define NUM_DISKS 6
#define PEDESTAL_HEIGHT 20
#define DISK_HEIGHT 7
#define DISK_UNIT_WIDTH 5
I64 poles_x[3];
I64 disks_x[NUM_DISKS],disks_y[NUM_DISKS],disks_pole[NUM_DISKS];
I64 OtherPole(I64 pole1,I64 pole2)
{
return 3-pole1-pole2;
}
I64 TopDisk(I64 pole)
{
I64 i;
for (i=0;i<NUM_DISKS;i++)
if (disks_pole[i]==pole)
return i;
return -1;
}
I64 PosInStk(I64 pole,I64 disk)
{
I64 result=0,i;
for (i=NUM_DISKS-1;i>disk;i--)
if (disks_pole[i]==pole)
result++;
return result;
}
U0 SetDisksRestXY()
{
I64 i;
for (i=0;i<NUM_DISKS;i++) {
disks_x[i]=poles_x[disks_pole[i]];
disks_y[i]=Fs->pix_height-PEDESTAL_HEIGHT-(DISK_HEIGHT+1)/2-1-(DISK_HEIGHT+1)*PosInStk(disks_pole[i],i);
}
}
U0 DrawIt(CTask *task,CDC *dc)
{
I64 i;
for (i=0;i<3;i++)
poles_x[i]=(1+i)*task->pix_width/4;
dc->color=BLACK;
GrRect(dc,poles_x[0]-50,task->pix_height-PEDESTAL_HEIGHT,poles_x[2]-poles_x[0]+100,PEDESTAL_HEIGHT-FONT_HEIGHT);
dc->color=DKGRAY;
GrRect(dc,poles_x[0]-49,task->pix_height-PEDESTAL_HEIGHT+1,poles_x[2]-poles_x[0]+98,PEDESTAL_HEIGHT-FONT_HEIGHT-2);
for (i=0;i<3;i++) {
dc->color=BLACK;
GrRect(dc,poles_x[i]-3,task->pix_height-PEDESTAL_HEIGHT-(NUM_DISKS+1)*(DISK_HEIGHT+1),7,(NUM_DISKS+1)*(DISK_HEIGHT+1));
dc->color=YELLOW;
GrRect(dc,poles_x[i]-2,task->pix_height-PEDESTAL_HEIGHT+1-(NUM_DISKS+1)*(DISK_HEIGHT+1),5,(NUM_DISKS+1)*(DISK_HEIGHT+1)-1);
}
for (i=0;i<NUM_DISKS;i++) {
dc->color=BLACK;
GrRect(dc,disks_x[i]-(i+1)*DISK_UNIT_WIDTH,disks_y[i]-DISK_HEIGHT/2,(i+1)*(DISK_UNIT_WIDTH*2)+1,DISK_HEIGHT);
dc->color=gr_rainbow_10[i];
GrRect(dc,disks_x[i]-(i+1)*DISK_UNIT_WIDTH+1,disks_y[i]-DISK_HEIGHT/2+1,(i+1)*(DISK_UNIT_WIDTH*2)-1,DISK_HEIGHT-2);
}
}
U0 MySleep()
{
if (ScanChar)
ThrowBreak;
Sleep(3);
}
U0 MoveDisks(I64 src_pole,I64 dst_pole,I64 num)
{
I64 top,x,y;
if (num>1)
MoveDisks(src_pole,OtherPole(src_pole,dst_pole),num-1);
DocClear;
"$$CM+BY,0,0$$Disk:%d from %d to %d\n",TopDisk(src_pole),src_pole,dst_pole;
top=TopDisk(src_pole);
for (y=disks_y[top];y>Fs->pix_height-PEDESTAL_HEIGHT-(DISK_HEIGHT+1)/2-(DISK_HEIGHT+1)*(NUM_DISKS+2);y--) {
disks_y[top]=y;
MySleep;
}
if (src_pole<dst_pole)
for (x=poles_x[src_pole];x<=poles_x[dst_pole];x++) {
disks_x[top]=x;
MySleep;
}
else
for (x=poles_x[src_pole];x>=poles_x[dst_pole];x--) {
disks_x[top]=x;
MySleep;
}
disks_pole[top]=dst_pole;
for (y=disks_y[top];y<Fs->pix_height-PEDESTAL_HEIGHT-(DISK_HEIGHT+1)/2-1-(DISK_HEIGHT+1)*PosInStk(dst_pole,top);y++) {
disks_y[top]=y;
MySleep;
}
SetDisksRestXY;
if (num>1)
MoveDisks(OtherPole(src_pole,dst_pole),dst_pole,num-1);
}
U0 Init()
{
I64 i;
for (i=0;i<3;i++)
poles_x[i]=(1+i)*Fs->pix_width/4;
for (i=0;i<NUM_DISKS;i++)
disks_pole[i]=0;
SetDisksRestXY;
}
U0 Hanoi()
{
SettingsPush; //See $LK,"SettingsPush",A="MN:SettingsPush"$
Init;
DocClear;
Fs->draw_it=&DrawIt;
Sleep(1000);
try {
MoveDisks(0,2,NUM_DISKS);
Beep; Beep;
DocClear;
"$$CM+BY,0,0$$";
PressAKey;
} catch
PutExcept;
DocClear;
SettingsPop;
}
Hanoi;
+8
View File
@@ -0,0 +1,8 @@
U0 InputPointer()
{
while (!ip_lb) //Left Button
GrPlot(,ipx,ipy);
DCFill;
}
InputPointer;
+98
View File
@@ -0,0 +1,98 @@
CDC *dc[2]; //dbl buf
I64 cur_dc,;
Bool restart;
U0 DrawIt(CTask *,CDC *dc2)
{
dc[cur_dc]->flags|=DCF_NO_TRANSPARENTS;
GrBlot(dc2,0,0,dc[cur_dc]);
}
U0 AnimateTask(I64)
{
I64 x,y,x1,y1,cnt,next_dc;
while (TRUE) {
next_dc=cur_dc^1;
start_over:
restart=FALSE;
DCClear(dc[next_dc]);
for (y=1;y<Fs->parent_task->pix_height-1;y++) {
for (x=1;x<Fs->parent_task->pix_width-1;x++) {
cnt=0;
for (y1=y-1;y1<=y+1;y1++)
for (x1=x-1;x1<=x+1;x1++)
if (GrPeek(dc[cur_dc],x1,y1)==GREEN)
cnt++;
if (restart) goto start_over;
if (GrPeek(dc[cur_dc],x,y)==GREEN) {
if (cnt==3) {
dc[next_dc]->color=GREEN;
GrPlot(dc[next_dc],x,y);
}
} else {
// if (cnt==3) { //Use this for the classic rules
if (cnt==2) {
dc[next_dc]->color=GREEN;
GrPlot(dc[next_dc],x,y);
}
}
}
Yield;
}
Sleep(50);
cur_dc=next_dc;
}
}
public U0 Life()
{
I64 msg_code,cnt,x1,y1,x2,y2,p1,p2;
dc[0]=DCNew(GR_WIDTH,GR_HEIGHT);
dc[1]=DCNew(GR_WIDTH,GR_HEIGHT);
cur_dc=0;
restart=FALSE;
SettingsPush; //See $LK,"SettingsPush",A="MN:SettingsPush"$
WinMax;
DCFill;
DocClear;
Fs->animate_task=Spawn(&AnimateTask,NULL,"Animate",,Fs);
WordStat;
Fs->draw_it=&DrawIt;
Fs->win_inhibit=WIG_TASK_DFT-WIF_FOCUS-WIF_BORDER;
do {
msg_code=GetMsg(&p1,&p2,
1<<MSG_KEY_DOWN+1<<MSG_IP_L_DOWN);
switch (msg_code) {
case MSG_IP_L_DOWN:
x1=p1; y1=p2;
x2=p1; y2=p2;
cnt=0;
while (msg_code!=MSG_IP_L_UP) {
restart=TRUE;
dc[cur_dc]->color=GREEN;
dc[cur_dc]->pen_width=0.04*ip_speed;
GrLine3(dc[cur_dc],x1,y1,0,x2,y2,0);
restart=TRUE;
msg_code=GetMsg(&p1,&p2,
1<<MSG_IP_L_UP+1<<MSG_IP_MOVE);
x1=x2; y1=y2;
x2=p1; y2=p2;
}
GrLine3(dc[cur_dc],x1,y1,0,x2,y2,0);
break;
case MSG_KEY_DOWN:
break;
}
} while (msg_code!=MSG_KEY_DOWN || !p1);
GetMsg(,,1<<MSG_KEY_UP);
SettingsPop;
DCFill;
DCDel(dc[0]);
DCDel(dc[1]);
}
Life;
+26
View File
@@ -0,0 +1,26 @@
U0 Main()
{
I64 x1=0,y1=0,x2=0,y2=0,i=1,c=0;
I64 vx1=0,vy1=0,vx2=0,vy2=0;
CDC *dc=DCAlias;
while (!ScanChar) {
if (!--i) {
i=1000;
dc->color=c++&15;
}
GrLine(dc,x1,y1,x2,y2);
vx1=ClampI64(vx1+SignI64(RandI16),-5,5);
vy1=ClampI64(vy1+SignI64(RandI16),-5,5);
vx2=ClampI64(vx2+SignI64(RandI16),-5,5);
vy2=ClampI64(vy2+SignI64(RandI16),-5,5);
x1=ClampI64(x1+vx1,0,Fs->pix_width-1);
y1=ClampI64(y1+vy1,0,Fs->pix_height-1);
x2=ClampI64(x2+vx2,0,Fs->pix_width-1);
y2=ClampI64(y2+vy2,0,Fs->pix_height-1);
Sleep(1); //Take this out and see what happens!
}
DCFill(dc);
DCDel(dc);
}
Main;
+50
View File
@@ -0,0 +1,50 @@
$SP,"",BI=1,BP="::/Misc/StockSprites.TXT,TempleOSLogo"$
U0 Logo()
{
CDC *dc=DCAlias;
dc->flags|=DCF_TRANSFORMATION;
DocClear;
while (!ScanChar) {
dc->x=Fs->pix_width>>1;
dc->y=Fs->pix_height>>1;
//$LK,"Wrap",A="MN:Wrap"$() converts an angle to an
//equivalent angle in a range starting
//from [arg2,arg2+ã*2).
GrIdentEqu(dc->r);
GrRotYEqu(dc->r,Wrap(tS,-ã/2));
GrRotXEqu(dc->r,30.0*ã/180);
GrSetRotMat(dc,dc->r);
Sprite3(dc,0,0,0,$IB,"<1>",BI=1$);
//See also $LK,"SpriteX3B",A="MN:SpriteX3B"$(),
//$LK,"SpriteY3B",A="MN:SpriteY3B"$(),$LK,"SpriteZ3B",A="MN:SpriteZ3B"$().
WinMgrSync;
DCFill(dc);
}
DCDel(dc);
}
Logo;
+82
View File
@@ -0,0 +1,82 @@
#define STEP_SIZE 400
U0 LowPassFilterDemo()
{
F64 y,y1,t,dt;
CDC *dc=DCAlias;
WinMax;
DocClear;
dc->color=BLUE;
GrLine(dc,0,0,0,STEP_SIZE);
GrLine(dc,0,STEP_SIZE,GR_WIDTH/2,STEP_SIZE);
GrLine(dc,GR_WIDTH/2,STEP_SIZE,GR_WIDTH/2,0);
GrLine(dc,GR_WIDTH/2,0,GR_WIDTH,0);
PressAKey;
dc->color=CYAN;
t=0;
y=0.0;
while (t<GR_WIDTH) {
dt=1.0;
if (t<GR_WIDTH/2)
y1=STEP_SIZE-STEP_SIZE*Exp(-0.01*t);
else
y1=STEP_SIZE*Exp(-0.01*(t-GR_WIDTH/2));
GrLine(dc,t,y,t+dt,y1);
y=y1;
t+=dt;
}
PressAKey;
dc->color=BLACK;
t=0;
y=0.0;
while (t<GR_WIDTH) {
dt=1.0;
if (t<GR_WIDTH/2)
y1=0.99*y+.01*STEP_SIZE;
else
y1=0.99*y;
GrLine(dc,t,y,t+dt,y1);
y=y1;
t+=dt;
}
PressAKey;
dc->color=RED;
t=0;
y=0.0;
while (t<GR_WIDTH) {
dt=Rand/10;
if (t<GR_WIDTH/2)
y1=LowPass1(.01,y,STEP_SIZE,dt);
else
y1=LowPass1(.01,y,0,dt);
GrLine(dc,t,y,t+dt,y1);
y=y1;
t+=dt;
}
PressAKey;
dc->color=GREEN;
t=0;
y=0.0;
while (t<GR_WIDTH) {
dt=Rand/10;
if (t<GR_WIDTH/2)
y1=LowPass1(.1,y,STEP_SIZE,dt);
else
y1=LowPass1(.1,y,0,dt);
GrLine(dc,t,y,t+dt,y1);
y=y1;
t+=dt;
}
PressAKey;
DCDel(dc);
DCFill;
}
LowPassFilterDemo;
+104
View File
@@ -0,0 +1,104 @@
F64 MySin(F64 d)
{
return Sin(d);
}
F64 MyCos(F64 d)
{
return Cos(d);
}
U0 FunDraw1(CDC *dc,F64 (*f)(F64 t))
{
I64 i,w=Fs->pix_width,h=Fs->pix_height;
F64 scale=h/5.0;
for (i=-w/2;i<=w/2;i++)
GrLine(dc,w/2+i,h/2-scale*f(i/scale*2*ã),w/2+i+1,h/2-scale*f((i+1)/scale*2*ã));
}
U0 FunDraw2(CDC *dc,F64 (*f)(F64 t,F64 p),F64 T)
{
I64 i,w=Fs->pix_width,h=Fs->pix_height;
F64 scale=h/5.0;
for (i=-w/2;i<=w/2;i++)
GrLine(dc,w/2+i,h/2-scale*f(i/scale,T),w/2+i+1,h/2-scale*f((i+1)/scale,T));
}
U0 Init(CDC *dc)
{
I64 i,w=Fs->pix_width,h=Fs->pix_height;
F64 scale=h/5.0;
DocClear;
DCFill(dc);
dc->color=BLACK;
GrLine(dc,0,h/2,w-1,h/2);
GrLine(dc,w/2,0,w/2,h-1);
for (i=-2;i<=2;i++) {
dc->color=BLACK;
GrLine(dc,w/2-3,h/2-scale*i,w/2+3,h/2-scale*i);
if (i) {
dc->color=DKGRAY;
GrPrint(dc,w/2+5,h/2-scale*i-FONT_HEIGHT/2,"%5.1f",ToF64(i));
}
}
for (i=-4;i<=4;i++) {
dc->color=BLACK;
GrLine(dc,w/2+scale*i,h/2-3,w/2+scale*i,h/2+3);
if (i) {
dc->color=DKGRAY;
GrPrint(dc,w/2+scale*i-7*FONT_WIDTH/2,h/2+5+FONT_HEIGHT,"%5.1f*T",ToF64(i));
}
}
}
U0 SndMathDemo()
{
CDC *dc=DCAlias;
Init(dc);
dc->color=RED;
FunDraw1(dc,&MySin);
"$$RED$$Sin$$FG$$\n";
PressAKey;
dc->color=BLUE;
FunDraw2(dc,&Saw,1.0);
"$$BLUE$$Saw$$FG$$\n";
PressAKey;
dc->color=GREEN;
FunDraw2(dc,&FullSaw,1.0);
"$$GREEN$$FullSaw$$FG$$\n";
PressAKey;
Init(dc);
dc->color=RED;
FunDraw1(dc,&MyCos);
"$$RED$$Cos$$FG$$\n";
PressAKey;
dc->color=BLUE;
FunDraw2(dc,&Caw,1.0);
"$$BLUE$$Caw$$FG$$\n";
PressAKey;
dc->color=GREEN;
FunDraw2(dc,&FullCaw,1.0);
"$$GREEN$$FullCaw$$FG$$\n";
PressAKey;
Init(dc);
dc->color=RED;
FunDraw1(dc,&MySin);
"$$RED$$Sin$$FG$$\n";
PressAKey;
dc->color=BLUE;
FunDraw2(dc,&Tri,1.0);
"$$BLUE$$Tri$$FG$$\n";
PressAKey;
dc->color=GREEN;
FunDraw2(dc,&FullTri,1.0);
"$$GREEN$$FullTri$$FG$$\n";
PressAKey;
DCFill(dc);
DCDel(dc);
}
SndMathDemo;
+24
View File
@@ -0,0 +1,24 @@
/*
$LK,"gr_dc",A="MN:gr_dc"$ is the persistent layer. You can access
it directly but two tasks accessing it simultaneously
with no $LK,"DCAlias",A="MN:DCAlias"$() will screw-up color and stuff.
*/
U0 Moon()
{
I64 i;
for (i=0;i<0x8000;i+=0x200) {
gr_dc->color=BLACK;
GrCircle(gr_dc,300,300,100);
GrCircle(gr_dc,250,300,120,,-.32*pi,0.64*pi);
gr_dc->color=ROPF_PROBABILITY_DITHER+LTGRAY<<16+BLUE;
gr_dc->dither_probability_u16=i;
GrFloodFill(gr_dc,250,300);
gr_dc->dither_probability_u16=2*i;
GrFloodFill(gr_dc,370,300);
WinMgrSync; //Typically 60 fps or 16.6mS
DCFill;
}
}
Moon;
Binary file not shown.
+27
View File
@@ -0,0 +1,27 @@
U0 PaletteDemo()
{
I64 i,j;
CBGR bgr;
for (i=0;i<16;i++)
"$$BLACK$$$$BG,%d$$__%-8Z__$$FG$$$$BG$$\n",i,i,"ST_COLORS";
SettingsPush; //See $LK,"SettingsPush",A="MN:SettingsPush"$
//Alternatively, use $LK,"SetVGAPalette4",A="MN:SetVGAPalette4"$(vga_palette4_gray);
for (i=0;i<16;i++) {
j=0xFF*i/15;
bgr.b=j;
bgr.g=j;
bgr.r=j;
SetVGAPaletteColor(i,bgr);
}
PressAKey;
//You can do it this way, too.
SetVGAPaletteColor(WHITE,0xFFFF00);
PressAKey;
//$LK,"SettingsPop",A="MN:SettingsPop"$() restores original palette
SettingsPop;
}
PaletteDemo;
+35
View File
@@ -0,0 +1,35 @@
U0 PanText()
{
I64 i;
for (i=0;i<=7;i++) {
gr_pan_text_x=i;
Sleep(30);
}
i--;
for (;i>=-7;i--) {
gr_pan_text_x=i;
Sleep(30);
}
i++;
for (;i<=0;i++) {
gr_pan_text_x=i;
Sleep(30);
}
for (i=0;i<=7;i++) {
gr_pan_text_y=i;
Sleep(30);
}
i--;
for (;i>=-7;i--) {
gr_pan_text_y=i;
Sleep(30);
}
i++;
for (;i<=0;i++) {
gr_pan_text_y=i;
Sleep(30);
}
}
PanText;
Binary file not shown.
Binary file not shown.
Binary file not shown.
+171
View File
@@ -0,0 +1,171 @@
#define NUM_ZEROS 2
Complex zeros[NUM_ZEROS]={{10.0,0},{-30,0}};
#define NUM_POLES 2
Complex poles[NUM_POLES]={{-20.0,-15.0},{-20.0,15.0}};
F64 scale;
Complex *PoleZeroFind(I64 x,I64 y)
{
I64 i;
F64 d,best_d=MAX_F64;
Complex *result=NULL;
for (i=0;i<NUM_POLES;i++) {
d=Sqr(poles[i].x-x)+Sqr(poles[i].y-y);
if (d<best_d) {
best_d=d;
result=&poles[i];
}
}
for (i=0;i<NUM_ZEROS;i++) {
d=Sqr(zeros[i].x-x)+Sqr(zeros[i].y-y);
if (d<best_d) {
best_d=d;
result=&zeros[i];
}
}
return result;
}
F64 F(Complex *x)
{
F64 m,a;
Complex num,denom,n1;
CPoly(&num, x,zeros,NUM_ZEROS);
CPoly(&denom,x,poles,NUM_POLES);
CDiv(&num,&denom,&n1);
R2P(&m,&a,n1.x,n1.y);
if (ip_rb)
return ã+a;
else
return m;
}
F64 MPDraw(CTask *task)
{
Complex xx;
I64 x,y,w=task->pix_width,h=task->pix_height,cx=w/2,cy=h/2,
lo=Gs->num*h/mp_cnt,hi=(Gs->num+1)*h/mp_cnt;
F64 yy,y_total=0;
CDC *dc=DCAlias(,task);
for (y=lo;y<hi;y++) {
for (x=0;x<w;x++) {
CEqu(&xx,x-cx,cy-y);
yy=scale*F(&xx);
dc->color=Clamp(yy,0,14);
y_total+=Clamp(yy,-14,14);
GrPlot(dc,x,y);
}
}
DCDel(dc);
return y_total;
}
U0 Draw()
{
CSrvCmd *tempm[MP_MAX_PROCESSORS];
F64 y_total,old_y_total=MAX_F64;
I64 i,w=Fs->pix_width,h=Fs->pix_height,cx=w/2,cy=h/2;
CDC *dc=DCAlias;
while (TRUE) {
for (i=0;i<mp_cnt;i++)
tempm[i]=JobQue(&MPDraw,Fs,i,0);
y_total=0;
for (i=0;i<mp_cnt;i++)
y_total+=GetResult(tempm[i])(F64);
if (!y_total) break;
scale*=7*GR_WIDTH*GR_HEIGHT/y_total;
if (Abs(y_total-old_y_total)<1.0*GR_WIDTH*GR_HEIGHT)
break;
old_y_total=y_total;
}
dc->color=15;
GrLine(dc,0,cy,w,cy);
GrLine(dc,cx,0,cx,h);
for (i=0;i<NUM_ZEROS;i++)
GrPrint(dc,cx+zeros[i].x-FONT_WIDTH/2,cy-zeros[i].y-FONT_HEIGHT/2,"o");
for (i=0;i<NUM_POLES;i++)
GrPrint(dc,cx+poles[i].x-FONT_WIDTH/2,cy-poles[i].y-FONT_HEIGHT/2,"x");
DCDel(dc);
}
U0 PoleZeros()
{
I64 msg_code,p1,p2,p11,p22,cx,cy;
Complex *tempc=NULL;
PopUpOk(
"Drag the poles and zeros with left mouse.\n"
"Hold right mouse for phase plot.\n"
);
WinMgrSync;
SettingsPush; //See $LK,"SettingsPush",A="MN:SettingsPush"$
Fs->win_inhibit=WIG_TASK_DFT-WIF_FOCUS-WIF_BORDER;
SetVGAPalette4(vga_palette4_gray);
SetVGAPaletteColor(15,0xFF0000); //White is red
WinMax;
WordStat;
WinBorder;
DCFill;
DocClear;
scale=1.0;
try {
Draw;
while (TRUE) {
msg_code=GetMsg(&p1,&p2,1<<MSG_KEY_DOWN+1<<MSG_IP_L_DOWN+1<<MSG_IP_L_UP+1<<MSG_IP_R_DOWN+1<<MSG_IP_R_UP+1<<MSG_IP_MOVE);
pz_msg:
cx=Fs->pix_width/2;
cy=Fs->pix_height/2;
switch (msg_code) {
case MSG_IP_L_DOWN:
tempc=PoleZeroFind(p1-cx,cy-p2);
break;
case MSG_IP_MOVE:
if (tempc) {
p11=p1;p22=p2;
//get to last mouse move
while (msg_code=ScanMsg(&p1,&p2,1<<MSG_KEY_DOWN+1<<MSG_IP_L_DOWN+1<<MSG_IP_L_UP+1<<MSG_IP_R_DOWN+1<<MSG_IP_R_UP+1<<MSG_IP_MOVE))
if (msg_code==MSG_IP_MOVE) {
p11=p1;p22=p2;
} else
goto pz_msg;
tempc->x=p11-cx;
tempc->y=cy-p22;
Draw;
}
break;
case MSG_IP_L_UP:
if (tempc) {
tempc->x=p1-cx;
tempc->y=cy-p2;
tempc=NULL;
Draw;
}
break;
case MSG_IP_R_DOWN:
case MSG_IP_R_UP:
Draw;
break;
case MSG_KEY_DOWN:
if (p1==CH_SHIFT_ESC||p1==CH_ESC)
goto pz_done;
}
WinMgrSync;
}
pz_done:
GetMsg(,,1<<MSG_KEY_UP);
} catch
PutExcept;
SettingsPop;
DCFill;
}
PoleZeros;
+81
View File
@@ -0,0 +1,81 @@
//This is a classic 2nd order nonlinear ordinary differential equation
I64 mp_not_done_flags;
#define OX 10
#define OY GR_HEIGHT/2-FONT_HEIGHT
#define MICRO_STEP 0.000001
class State
{
F64 wabbits,hawks,
d_wabbits,d_hawks; //Technically, these are not part of the state.
};
U0 Steps(State *s)
{
I64 i;
for (i=0;i<10000;i++) {
s->d_wabbits=s->wabbits*(1.0-0.10*s->hawks);
s->d_hawks =-s->hawks *(1.0-0.01*s->wabbits);
s->hawks +=s->d_hawks *MICRO_STEP;
s->wabbits+=s->d_wabbits*MICRO_STEP;
}
}
U0 PlotTrajectory(CTask *parent)
{
I64 i;
CDC *dc=DCAlias(,parent);
State s;
MemSet(&s,0,sizeof(State));
s.wabbits=RandU16%100 +10;
s.hawks =RandU16%10 +1;
for (i=0;i<1000;i++) {
dc->color=LTGREEN;
GrPlot(dc,s.wabbits+OX,OY-s.d_wabbits);
dc->color=LTRED;
GrPlot(dc,s.hawks +OX,OY-s.d_hawks);
Steps(&s);
}
DCDel(dc);
LBtr(&mp_not_done_flags,Gs->num);
}
U0 PredatorPrey()
{
I64 i;
CDC *dc=DCAlias;
PopUpOk("This will plot multiple predator-prey\n"
"trajectories. It restarts many times\n"
"with different, random, initial populations.\n");
SettingsPush; //See $LK,"SettingsPush",A="MN:SettingsPush"$
try {
DocClear;
WinMax;
WinBorder;
WinMgrSync;
WordStat;
dc->color=BLACK;
GrLine(dc,OX,0,OX,GR_HEIGHT-FONT_HEIGHT-1);
GrLine(dc,0,OY,GR_WIDTH-1,OY);
while (!ScanChar) {
mp_not_done_flags=1<<mp_cnt-1;
for (i=0;i<mp_cnt;i++)
JobQue(&PlotTrajectory,Fs,i);
do Yield;
while (mp_not_done_flags);
}
} catch
PutExcept;
SettingsPop;
DCFill(dc);
DCDel(dc);
}
PredatorPrey;
Binary file not shown.
Binary file not shown.
+180
View File
@@ -0,0 +1,180 @@
//"ls" is light source.
U0 Lighting(CDC *dc,CD3I32 *ls,CD3I32 *poly,I64 color)
{ //color is a color from 0-7
CD3I32 v1,v2;
I64 *r=dc->r,i,vn_x,vn_y,vn_z;
F64 d;
v1.x=poly[0].x-poly[1].x;
v1.y=poly[0].y-poly[1].y;
v1.z=poly[0].z-poly[1].z;
v2.x=poly[2].x-poly[1].x;
v2.y=poly[2].y-poly[1].y;
v2.z=poly[2].z-poly[1].z;
//V1 and V2 are vects along two sides
//of the polygon joined at point[1].
vn_x=v1.y*v2.z-v1.z*v2.y;
vn_y=v1.z*v2.x-v1.x*v2.z;
vn_z=v1.x*v2.y-v1.y*v2.x;
if (d=Sqrt(SqrI64(vn_x)+SqrI64(vn_y)+SqrI64(vn_z)))
d=1<<16/d;
vn_x*=d;
vn_y*=d;
vn_z*=d;
//Vn is the cross product of V1 and V3
//which means it is perpendicular. It
//is the normal vect to the surface.
//It has been scaled to length 65536.
GrRot(r,&vn_x,&vn_y,&vn_z);
i=(vn_x*ls->x+vn_y*ls->y+vn_z*ls->z)>>16;
//The dot product of the light source
//vect and the surface normal
//gives an illumination number.
//TempleOS will generate a random U16
//and compare to dither_probability_u16 and
//will pick from two colors.
//Probability dithering does not work with pen_width>1 at this time.
if (i<0) {
dc->color=ROPF_PROBABILITY_DITHER+BLACK<<16+color;
dc->dither_probability_u16=-i;
} else {
dc->color=ROPF_PROBABILITY_DITHER+(color^8)<<16+color;
dc->dither_probability_u16=i;
}
}
#define RINGS 8
#define FACES 32
#define SLOP 0.03 //Gaps appear without this.
U0 DrawIt(CTask *task,CDC *dc)
{
CCtrl *c=CtrlFindUnique(task,CTRLT_VIEWING_ANGLES);
CViewAngles *s=c->state;
F64 tt=0.5*(Sin(ã*2*(tS%10.0)/10.0)+2.0),
é,é2,è,è2,radius,d;
I64 i,j,cx=task->pix_width/2,cy=task->pix_height/2;
CD3I32 poly[3],ls;
dc->flags|=DCF_TRANSFORMATION;
DCAllocDepthBuf(dc);
GrIdentEqu(dc->r);
GrRotZEqu(dc->r,s->az);
GrRotYEqu(dc->r,s->ay);
GrRotXEqu(dc->r,s->ax+ã);
GrScaleEqu(dc->r,tt);
GrSetRotMat(dc,dc->r);
ls.x=-(ipx-task->pix_left-task->scroll_x-cx);
ls.y=-(ipy-task->pix_top-task->scroll_y-cy);
ls.z=GR_WIDTH/8;
d=1<<16/D3I32Norm(&ls);
ls.x*=d;
ls.y*=d;
ls.z*=d;
dc->x=cx;
dc->y=cy;
dc->z=MaxI64(cx,cy);
radius =MinI64(cx,cy)/2;
for (i=0;i<RINGS;i++) {
è = i*ã/2/RINGS;
è2=(i+1)*ã/2/RINGS+SLOP;
for (j=0;j<FACES;j++) {
é = j*2*ã/FACES;
é2=(j+1)*2*ã/FACES+SLOP;
//Upper half
poly[0].x=radius*Cos(è)*Cos(é);
poly[0].y=radius*Cos(è)*Sin(é);
poly[0].z=radius*Sin(è);
poly[1].x=radius*Cos(è)*Cos(é2);
poly[1].y=radius*Cos(è)*Sin(é2);
poly[1].z=radius*Sin(è);
poly[2].x=radius*Cos(è2)*Cos(é+2*ã/FACES/2);
poly[2].y=radius*Cos(è2)*Sin(é+2*ã/FACES/2);
poly[2].z=radius*Sin(è2);
Lighting(dc,&ls,poly,BLUE);
GrFillPoly3(dc,3,poly);
poly[2].x=radius*Cos(è2)*Cos(é +2*ã/FACES/2);
poly[2].y=radius*Cos(è2)*Sin(é +2*ã/FACES/2);
poly[2].z=radius*Sin(è2);
poly[1].x=radius*Cos(è2)*Cos(é2+2*ã/FACES/2);
poly[1].y=radius*Cos(è2)*Sin(é2+2*ã/FACES/2);
poly[1].z=radius*Sin(è2);
poly[0].x=radius*Cos(è)*Cos(é2);
poly[0].y=radius*Cos(è)*Sin(é2);
poly[0].z=radius*Sin(è);
Lighting(dc,&ls,poly,BLUE);
GrFillPoly3(dc,3,poly);
//Lower half
poly[2].x=radius*Cos(è)*Cos(é);
poly[2].y=radius*Cos(è)*Sin(é);
poly[2].z=-radius*Sin(è);
poly[1].x=radius*Cos(è)*Cos(é2);
poly[1].y=radius*Cos(è)*Sin(é2);
poly[1].z=-radius*Sin(è);
poly[0].x=radius*Cos(è2)*Cos(é+2*ã/FACES/2);
poly[0].y=radius*Cos(è2)*Sin(é+2*ã/FACES/2);
poly[0].z=-radius*Sin(è2);
Lighting(dc,&ls,poly,RED);
GrFillPoly3(dc,3,poly);
poly[0].x=radius*Cos(è2)*Cos(é +2*ã/FACES/2);
poly[0].y=radius*Cos(è2)*Sin(é +2*ã/FACES/2);
poly[0].z=-radius*Sin(è2);
poly[1].x=radius*Cos(è2)*Cos(é2+2*ã/FACES/2);
poly[1].y=radius*Cos(è2)*Sin(é2+2*ã/FACES/2);
poly[1].z=-radius*Sin(è2);
poly[2].x=radius*Cos(è)*Cos(é2);
poly[2].y=radius*Cos(è)*Sin(é2);
poly[2].z=-radius*Sin(è);
Lighting(dc,&ls,poly,RED);
GrFillPoly3(dc,3,poly);
}
}
}
//See $LK,"::/Demo/Graphics/SpritePlot3D.CPP"$.
//for a CSprite example.
//See $LK,"SpriteMesh",A="MN:SpriteMesh"$() for a fancy example.
U0 Main()
{
CCtrl *c=ViewAnglesNew;
CViewAngles *s=c->state;
s->sx=2*VIEWANGLES_SNAP;
s->sy=7*VIEWANGLES_SNAP;
s->sz=6*VIEWANGLES_SNAP;
SettingsPush; //See $LK,"SettingsPush",A="MN:SettingsPush"$
DocClear;
WinMax;
WinBorder;
WordStat;
Fs->draw_it=&DrawIt;
try {
"\n\nMove mouse to change light source.\n\n";
PressAKey;
} catch
PutExcept;
DocClear;
SettingsPop;
ViewAnglesDel;
}
Main;
Binary file not shown.
+111
View File
@@ -0,0 +1,111 @@
/*
This was done with <CTRL-SHIFT-L> managed code.
It's conceptually easy to do a ctrl,
but tedious. You have complete freedom.
See $LK,"CCtrl",A="MN:CCtrl"$.
*/
// Start of autocreated Code
#define SLIDER_RANGE 30
#define SLIDER_SPACING 20
#define SLIDER_BORDER 2
class CSliderState
{
I64 left_pos;
I64 right_pos;
} sld;
U0 DrawSliderCtrl(CDC *dc,CCtrl *c)
{
CSliderState *s=c->state;
dc->color=LTRED;
GrRect(dc, c->left,c->top,SLIDER_SPACING*3+2,SLIDER_SPACING*2+SLIDER_RANGE);
dc->color=BLUE;
GrRect(dc, c->left+SLIDER_BORDER,c->top+SLIDER_BORDER,
SLIDER_SPACING*3+2-2*SLIDER_BORDER,SLIDER_SPACING*2+SLIDER_RANGE-2*SLIDER_BORDER);
dc->color=BLACK;
GrLine(dc,c->left+1*SLIDER_SPACING+0,c->top+SLIDER_SPACING,
c->left+1*SLIDER_SPACING+0,c->top+SLIDER_SPACING+SLIDER_RANGE-1);
GrLine(dc,c->left+2*SLIDER_SPACING+1,c->top+SLIDER_SPACING,
c->left+2*SLIDER_SPACING+1,c->top+SLIDER_SPACING+SLIDER_RANGE-1);
dc->color=LTRED;
GrPrint(dc,c->left+1*SLIDER_SPACING+0-FONT_WIDTH/2,
c->top+SLIDER_SPACING+SLIDER_RANGE+3,
"%d",s->left_pos*10/SLIDER_RANGE);
GrPrint(dc,c->left+2*SLIDER_SPACING+1-FONT_WIDTH/2,
c->top+SLIDER_SPACING+SLIDER_RANGE+3,
"%d",s->right_pos*10/SLIDER_RANGE);
GrRect(dc,c->left+1*SLIDER_SPACING+0-3,c->top+SLIDER_SPACING+SLIDER_RANGE-1-s->left_pos-2,7,5);
GrRect(dc,c->left+2*SLIDER_SPACING+1-3,c->top+SLIDER_SPACING+SLIDER_RANGE-1-s->right_pos-2,7,5);
dc->color=YELLOW;
GrRect(dc,c->left+1*SLIDER_SPACING+0-2,c->top+SLIDER_SPACING+SLIDER_RANGE-1-s->left_pos-1,5,3);
GrRect(dc,c->left+2*SLIDER_SPACING+1-2,c->top+SLIDER_SPACING+SLIDER_RANGE-1-s->right_pos-1,5,3);
}
U0 UpdateDerivedSliderCtrl(CCtrl *c)
{
CSliderState *s=c->state;
c->left=c->win_task->pix_width/2-(SLIDER_SPACING*3+2)/2;
c->right=c->left+3*SLIDER_SPACING+2;
c->top=c->win_task->pix_height/2-(SLIDER_SPACING*2+SLIDER_RANGE)/2;
c->bottom=c->top+SLIDER_SPACING*2+SLIDER_RANGE;
s->left_pos=ClampI64(s->left_pos,0,SLIDER_RANGE-1);
s->right_pos=ClampI64(s->right_pos,0,SLIDER_RANGE-1);
}
U0 LeftClickSlider(CCtrl *c,I64 x,I64 y,Bool)
{
CSliderState *s=c->state;
if (x<c->left+1*SLIDER_SPACING+0+SLIDER_SPACING/2)
s->left_pos=SLIDER_RANGE-1-(y-(c->top+SLIDER_SPACING));
else
s->right_pos=SLIDER_RANGE-1-(y-(c->top+SLIDER_SPACING));
if (c->update_derived_vals)
(*c->update_derived_vals)(c);
}
CCtrl *SliderNew()
{
Bool old_preempt;
CCtrl *c=CAlloc(sizeof(CCtrl));
c->win_task=Fs;
c->flags=CTRLF_SHOW|CTRLF_CAPTURE_LEFT_IP;
c->type=CTRLT_GENERIC;
c->state=&sld;
MemSet(&sld,0,sizeof(CSliderState));
c->draw_it=&DrawSliderCtrl;
c->left_click=&LeftClickSlider;
c->update_derived_vals=&UpdateDerivedSliderCtrl;
old_preempt=Preempt(OFF);
QueIns(c,Fs->last_ctrl);
TaskDerivedValsUpdate;
Preempt(old_preempt);
return c;
}
U0 SliderDel(CCtrl *c)
{
Bool old_preempt=Preempt(OFF);
QueRem(c);
Preempt(old_preempt);
Free(c);
}
// End of autocreated Code
U0 Main()
{
"This is to demo ctrls.\n";
CCtrl *c=SliderNew;
PressAKey;
"Left: %d Right: %d\n",
sld.left_pos*10/SLIDER_RANGE,
sld.right_pos*10/SLIDER_RANGE;
SliderDel(c);
DocBottom;
}
Main;
+51
View File
@@ -0,0 +1,51 @@
//This is a drawing program
public U0 SpeedLineDemo()
{
I64 msg_code,cnt,x1,y1,x2,y2,p1,p2,color=BLACK;
CDC *dc=DCAlias;
dc->color=color;
SettingsPush; //See $LK,"SettingsPush",A="MN:SettingsPush"$
Fs->win_inhibit=WIG_TASK_DFT-WIF_FOCUS-WIF_BORDER;
WinMax;
WinBorder;
WordStat;
DCFill;
DocClear;
do {
msg_code=GetMsg(&p1,&p2,
1<<MSG_KEY_DOWN+1<<MSG_IP_L_DOWN+
1<<MSG_IP_R_UP);
switch (msg_code) {
case MSG_IP_R_UP:
color=PopUpColor;
if (color>=0)
dc->color=color;
break;
case MSG_IP_L_DOWN:
x1=p1; y1=p2;
x2=p1; y2=p2;
cnt=0;
while (msg_code!=MSG_IP_L_UP) {
dc->pen_width=0.04*ip_speed;
GrLine3(dc,x1,y1,0,x2,y2,0);
msg_code=GetMsg(&p1,&p2,
1<<MSG_IP_L_UP+1<<MSG_IP_MOVE);
x1=x2; y1=y2;
x2=p1; y2=p2;
}
GrLine3(dc,x1,y1,0,x2,y2,0);
break;
case MSG_KEY_DOWN:
break;
}
} while (msg_code!=MSG_KEY_DOWN || !p1);
GetMsg(,,1<<MSG_KEY_UP);
DCFill;
DCDel(dc);
SettingsPop;
}
SpeedLineDemo; //Execute when #included
Binary file not shown.
Binary file not shown.
Binary file not shown.
+28
View File
@@ -0,0 +1,28 @@
/*
If you press <CTRL-T> you will see this:
$$IB+BP,"<1>",1,"::/Demo/Graphics/SpritePut.CPP,2"$$
That imports an external sprite. It's
called a $LK,"DOCEF1_BIN_PTR_LINK",A="MN:DOCEF1_BIN_PTR_LINK"$.
Press <CTRL-R> and "pointer to sprite".
Then, enter a file and the number of
the sprite in the file. Note: the
number can get messed-up if you change
the order of sprites in the source file.
You can enter a tag instead of a num.
*/
U0 SpritePutExt()
{
I64 i;
for (i=0;i<3;i++) {
"US Map:";
Sprite($IB,"<1>",BI=1,BP="::/Demo/Graphics/SpritePut.CPP,2"$);
NewLine(12);
}
}
SpritePutExt;
Binary file not shown.
+62
View File
@@ -0,0 +1,62 @@
//This is demonstrates symmetry.
//Right-click and draw line of symmetry.
//If the line of symmetry is a point, symmetry is turned-off.
U0 SymmetryDemo()
{
I64 msg_code,x1,y1,x2,y2,p1,p2;
CDC *dc=DCAlias;
SettingsPush; //See $LK,"SettingsPush",A="MN:SettingsPush"$
Fs->win_inhibit=WIG_TASK_DFT-WIF_FOCUS-WIF_BORDER;
WinMax;
DCFill;
DocClear;
dc->flags|=DCF_SYMMETRY;
GrSetSymmetry(dc,Fs->pix_width/2,0,Fs->pix_width/2,1);
do {
msg_code=GetMsg(&p1,&p2,
1<<MSG_KEY_DOWN+1<<MSG_IP_L_DOWN+
1<<MSG_IP_R_DOWN);
switch (msg_code) {
case MSG_IP_R_DOWN:
dc->flags&=~DCF_SYMMETRY;
dc->color=ROP_XOR+RED^COLOR_TRANSPARENT;
x1=p1; y1=p2;
x2=p1; y2=p2;
while (msg_code!=MSG_IP_R_UP) {
GrLine3(dc,x1,y1,0,x2,y2,0);
msg_code=GetMsg(&p1,&p2,
1<<MSG_IP_R_UP+1<<MSG_IP_MOVE);
GrLine3(dc,x1,y1,0,x2,y2,0);
x2=p1; y2=p2;
}
GrLine3(dc,x1,y1,0,x2,y2,0);
if (GrSetSymmetry3(dc,x1,y1,0,x2,y2,0,x2,y2,1))
dc->flags|=DCF_SYMMETRY;
break;
case MSG_IP_L_DOWN:
dc->color=ROP_XOR+BLACK^COLOR_TRANSPARENT;
x1=p1; y1=p2;
x2=p1; y2=p2;
while (msg_code!=MSG_IP_L_UP) {
GrLine3(dc,x1,y1,0,x2,y2,0);
msg_code=GetMsg(&p1,&p2,
1<<MSG_IP_L_UP+1<<MSG_IP_MOVE);
GrLine3(dc,x1,y1,0,x2,y2,0);
x2=p1; y2=p2;
}
GrLine3(dc,x1,y1,0,x2,y2,0);
break;
case MSG_KEY_DOWN:
break;
}
} while (msg_code!=MSG_KEY_DOWN || !p1);
GetMsg(,,1<<MSG_KEY_UP);
DCFill;
DCDel(dc);
SettingsPop;
}
SymmetryDemo; //Execute when #included
Binary file not shown.
+103
View File
@@ -0,0 +1,103 @@
/*
This was done with <CTRL-SHIFT-L> managed code
that was modified.
This is an advanced demo that shows
that you can place ctrls on the
wall paper.
See $LK,"::/Demo/Graphics/SliderCtrl.CPP"$
and $LK,"::/Demo/Graphics/WallPaperFish.CPP"$
before messing with this program.
It must be "Adam Included".
*/
#define SLIDER_RANGE 30
#define SLIDER_SPACING 20
#define SLIDER_BORDER 2
class CSliderState
{
I64 left_pos;
I64 right_pos;
};
U0 DrawSliderCtrl(CDC *dc,CCtrl *c)
{
CSliderState *s=c->state;
dc->color=LTRED;
GrRect(dc, c->left,c->top,SLIDER_SPACING*3+2,SLIDER_SPACING*2+SLIDER_RANGE);
dc->color=BLUE;
GrRect(dc, c->left+SLIDER_BORDER,c->top+SLIDER_BORDER,
SLIDER_SPACING*3+2-2*SLIDER_BORDER,SLIDER_SPACING*2+SLIDER_RANGE-2*SLIDER_BORDER);
dc->color=BLACK;
GrLine(dc,c->left+1*SLIDER_SPACING+0,c->top+SLIDER_SPACING,
c->left+1*SLIDER_SPACING+0,c->top+SLIDER_SPACING+SLIDER_RANGE-1);
GrLine(dc,c->left+2*SLIDER_SPACING+1,c->top+SLIDER_SPACING,
c->left+2*SLIDER_SPACING+1,c->top+SLIDER_SPACING+SLIDER_RANGE-1);
dc->color=LTRED;
GrPrint(dc,c->left+1*SLIDER_SPACING+0-FONT_WIDTH/2,
c->top+SLIDER_SPACING+SLIDER_RANGE+3,
"%d",s->left_pos*10/SLIDER_RANGE);
GrPrint(dc,c->left+2*SLIDER_SPACING+1-FONT_WIDTH/2,
c->top+SLIDER_SPACING+SLIDER_RANGE+3,
"%d",s->right_pos*10/SLIDER_RANGE);
GrRect(dc,c->left+1*SLIDER_SPACING+0-3,c->top+SLIDER_SPACING+SLIDER_RANGE-1-s->left_pos-2,7,5);
GrRect(dc,c->left+2*SLIDER_SPACING+1-3,c->top+SLIDER_SPACING+SLIDER_RANGE-1-s->right_pos-2,7,5);
dc->color=YELLOW;
GrRect(dc,c->left+1*SLIDER_SPACING+0-2,c->top+SLIDER_SPACING+SLIDER_RANGE-1-s->left_pos-1,5,3);
GrRect(dc,c->left+2*SLIDER_SPACING+1-2,c->top+SLIDER_SPACING+SLIDER_RANGE-1-s->right_pos-1,5,3);
}
U0 UpdateDerivedSliderCtrl(CCtrl *c)
{
CSliderState *s=c->state;
c->left=c->win_task->pix_width/2-(SLIDER_SPACING*3+2)/2;
c->right=c->left+3*SLIDER_SPACING+2;
c->top=c->win_task->pix_height/2-(SLIDER_SPACING*2+SLIDER_RANGE)/2;
c->bottom=c->top+SLIDER_SPACING*2+SLIDER_RANGE;
s->left_pos=ClampI64(s->left_pos,0,SLIDER_RANGE-1);
s->right_pos=ClampI64(s->right_pos,0,SLIDER_RANGE-1);
}
U0 LeftClickSlider(CCtrl *c,I64 x,I64 y,Bool)
{
CSliderState *s=c->state;
if (x<c->left+1*SLIDER_SPACING+0+SLIDER_SPACING/2)
s->left_pos=SLIDER_RANGE-1-(y-(c->top+SLIDER_SPACING));
else
s->right_pos=SLIDER_RANGE-1-(y-(c->top+SLIDER_SPACING));
if (c->update_derived_vals)
(*c->update_derived_vals)(c);
}
CCtrl *SliderNew()
{
Bool old_preempt;
CCtrl *c=ACAlloc(sizeof(CCtrl));
c->win_task=sys_winmgr_task;
c->flags=CTRLF_SHOW|CTRLF_CAPTURE_LEFT_IP;
c->type=CTRLT_GENERIC;
c->state=ACAlloc(sizeof(CSliderState));
c->draw_it=&DrawSliderCtrl;
c->left_click=&LeftClickSlider;
c->update_derived_vals=&UpdateDerivedSliderCtrl;
old_preempt=Preempt(OFF);
QueIns(c,sys_winmgr_task->last_ctrl);
TaskDerivedValsUpdate;
Preempt(old_preempt);
return c;
}
U0 SliderDel(CCtrl *c)
{
Bool old_preempt=Preempt(OFF);
QueRem(c);
Preempt(old_preempt);
Free(c->state);
Free(c);
}
SliderNew;
Binary file not shown.
+23
View File
@@ -0,0 +1,23 @@
U0 DrawIt(CTask *,CDC *dc)
{
I64 i,j;
U16 *ptr=gr_win_z_buf;
dc->color=BLACK;
GrPrint(dc,0,0,"Move the windows around and watch.");
for (i=0;i<TEXT_ROWS;i++)
for (j=0;j<TEXT_COLS;j++) {
dc->color=*ptr++&15;
GrPlot(dc,j,i+FONT_HEIGHT);
}
}
U0 ScrollBars()
{
SettingsPush; //See $LK,"SettingsPush",A="MN:SettingsPush"$
DocClear;
Fs->draw_it=&DrawIt;
GetChar(,FALSE);
SettingsPop;
}
ScrollBars;