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