File size: 1,999 Bytes
806d4b5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
<1>/* Graphics Not Rendered in HTML */
#define APPLES_NUM 128
I64 x[APPLES_NUM],y[APPLES_NUM],
cur_apple,cur_dist;
Bool trigger;
U0 DrawIt(CTask *,CDC *dc)
{
I64 i,best_dist=I64_MAX,best_apple=0;
dc->flags|=DCF_LOCATE_NEAREST;
dc->cur_x=ms.pos.x;
dc->cur_y=ms.pos.y;
dc->cur_z=0;
for (i=0;i<APPLES_NUM;i++) {
Sprite3(dc,x[i],y[i],0,<1>);
if (dc->nearest_dist<=best_dist) {
//Distance to nearest pix, squared
//Sqrt() is expensive so we work with distance squared.
best_dist=dc->nearest_dist;
best_apple=i;
}
}
if (trigger) {
cur_apple=best_apple;
cur_dist=best_dist;
trigger=FALSE;
}
GrPrint(dc,FONT_WIDTH,FONT_HEIGHT,"Dist Squared:%d",cur_dist);
}
U0 Pick()
{
I64 i,msg_code,arg1,arg2,delta_x,delta_y;
for (i=0;i<APPLES_NUM;i++) {
x[i]=RandU16%GR_WIDTH;
y[i]=RandU16%(GR_HEIGHT-FONT_HEIGHT*3)+FONT_HEIGHT*3;
}
SettingsPush; //See SettingsPush
AutoComplete;
DocCursor;
DocClear;
trigger=FALSE;
cur_apple=cur_dist=0;
Fs->draw_it=&DrawIt;
Fs->win_inhibit=WIG_TASK_DFT-WIF_SELF_FOCUS-WIF_SELF_BORDER;
try {
while (TRUE) {
if (GetMsg(&arg1,&arg2,1<<MSG_KEY_DOWN|1<<MSG_MS_L_DOWN)==MSG_KEY_DOWN)
goto pi_done;
//Wait for window mgr to call DrawIt()
trigger=TRUE;
do Refresh;
while (trigger);
//Use scrn coordinates, not window (arg1,arg2)
//ms.pos.x and ms.pos.y are global vars updated
//updated by the window mgr to hold the
//scrn coordinates of the mouse.
delta_x=x[cur_apple]-ms.pos.x;
delta_y=y[cur_apple]-ms.pos.y;
do {
msg_code=GetMsg(&arg1,&arg2,
1<<MSG_KEY_DOWN|1<<MSG_MS_MOVE|1<<MSG_MS_L_UP);
if (msg_code==MSG_KEY_DOWN)
goto pi_done;
x[cur_apple]=ms.pos.x+delta_x;
y[cur_apple]=ms.pos.y+delta_y;
} while (msg_code!=MSG_MS_L_UP);
}
pi_done:
GetMsg(,,1<<MSG_KEY_UP);
} catch
PutExcept;
SettingsPop;
}
Pick;
|