ZealOS/docs/Demo/Graphics/Pick.ZC.html
2021-12-30 22:21:33 -05:00

123 lines
11 KiB
HTML
Executable file

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
<meta name="generator" content="ZealOS V1.08">
<style type="text/css">
body {background-color:#1f1f1f;}
.cF0{color:#e3e3e3;background-color:#1f1f1f;}
.cF1{color:#4f84a6;background-color:#1f1f1f;}
.cF2{color:#73a255;background-color:#1f1f1f;}
.cF3{color:#297582;background-color:#1f1f1f;}
.cF4{color:#b34f4b;background-color:#1f1f1f;}
.cF5{color:#8a52c3;background-color:#1f1f1f;}
.cF6{color:#b7822f;background-color:#1f1f1f;}
.cF7{color:#444444;background-color:#1f1f1f;}
.cF8{color:#6d6d6d;background-color:#1f1f1f;}
.cF9{color:#94bfde;background-color:#1f1f1f;}
.cFA{color:#a1ce97;background-color:#1f1f1f;}
.cFB{color:#6db4be;background-color:#1f1f1f;}
.cFC{color:#e88e88;background-color:#1f1f1f;}
.cFD{color:#ca94e8;background-color:#1f1f1f;}
.cFE{color:#d4b475;background-color:#1f1f1f;}
.cFF{color:#1f1f1f;background-color:#1f1f1f;}
</style>
</head>
<body>
<pre style="font-family:monospace;font-size:12pt">
<a name="l1"></a><span class=cFA>
<a name="l2"></a>&lt;1&gt;/* Graphics Not Rendered in HTML */</span><span class=cF0>
<a name="l3"></a>
<a name="l4"></a>#</span><span class=cF1>define</span><span class=cF0> APPLES_NUM </span><span class=cFE>128</span><span class=cF0>
<a name="l5"></a>
<a name="l6"></a></span><span class=cF9>I64</span><span class=cF0> x[APPLES_NUM], y[APPLES_NUM], cur_apple, cur_dist;
<a name="l7"></a></span><span class=cF1>Bool</span><span class=cF0> trigger;
<a name="l8"></a>
<a name="l9"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>DrawIt</span><span class=cF0>(</span><span class=cF9>CTask</span><span class=cF0> *, </span><span class=cF9>CDC</span><span class=cF0> *dc)
<a name="l10"></a>{
<a name="l11"></a> </span><span class=cF9>I64</span><span class=cF0> i, best_dist = </span><span class=cF3>I64_MAX</span><span class=cF0>, best_apple = </span><span class=cFE>0</span><span class=cF0>;
<a name="l12"></a>
<a name="l13"></a> dc-&gt;flags |= </span><span class=cF3>DCF_LOCATE_NEAREST</span><span class=cF0>;
<a name="l14"></a> dc-&gt;cur_x = </span><span class=cFB>mouse</span><span class=cF0>.pos.x;
<a name="l15"></a> dc-&gt;cur_y = </span><span class=cFB>mouse</span><span class=cF0>.pos.y;
<a name="l16"></a> dc-&gt;cur_z = </span><span class=cFE>0</span><span class=cF0>;
<a name="l17"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i &lt; APPLES_NUM; i++)
<a name="l18"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l19"></a> </span><span class=cF5>Sprite3</span><span class=cF0>(dc, x[i], y[i], </span><span class=cFE>0</span><span class=cF0>, </span><span class=cFA>&lt;1&gt;</span><span class=cF0>);
<a name="l20"></a> </span><span class=cF1>if</span><span class=cF0> (dc-&gt;nearest_dist &lt;= best_dist)
<a name="l21"></a> {
<a name="l22"></a> </span><span class=cF2>//Distance to nearest pix, squared</span><span class=cF0>
<a name="l23"></a> </span><span class=cF2>//</span><a href="https://zeal-operating-system.github.io/ZealOS/Kernel/KernelB.HH.html#l84"><span class=cF4>Sqrt</span></a><span class=cF2>() is expensive so we work with distance squared.</span><span class=cF0>
<a name="l24"></a> best_dist = dc-&gt;nearest_dist;
<a name="l25"></a> best_apple = i;
<a name="l26"></a> }
<a name="l27"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l28"></a> </span><span class=cF1>if</span><span class=cF0> (trigger)
<a name="l29"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l30"></a> cur_apple = best_apple;
<a name="l31"></a> cur_dist = best_dist;
<a name="l32"></a> trigger = </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l33"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l34"></a> </span><span class=cF5>GrPrint</span><span class=cF0>(dc, </span><span class=cF3>FONT_WIDTH</span><span class=cF0>, </span><span class=cF3>FONT_HEIGHT</span><span class=cF0>, </span><span class=cF6>&quot;Dist Squared:%d&quot;</span><span class=cF0>, cur_dist);
<a name="l35"></a>}
<a name="l36"></a>
<a name="l37"></a></span><span class=cF1>U0</span><span class=cF0> Pick()
<a name="l38"></a>{
<a name="l39"></a> </span><span class=cF9>I64</span><span class=cF0> i, message_code, arg1, arg2, delta_x, delta_y;
<a name="l40"></a>
<a name="l41"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i &lt; APPLES_NUM; i++)
<a name="l42"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l43"></a> x[i] = </span><span class=cF5>RandU16</span><span class=cF0> % </span><span class=cF3>GR_WIDTH</span><span class=cF0>;
<a name="l44"></a> y[i] = </span><span class=cF5>RandU16</span><span class=cF0> % (</span><span class=cF3>GR_HEIGHT</span><span class=cF0> - </span><span class=cF3>FONT_HEIGHT</span><span class=cF0> * </span><span class=cFE>3</span><span class=cF0>) + </span><span class=cF3>FONT_HEIGHT</span><span class=cF0> * </span><span class=cFE>3</span><span class=cF0>;
<a name="l45"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l46"></a> </span><span class=cF5>SettingsPush</span><span class=cF0>; </span><span class=cF2>//See </span><a href="https://zeal-operating-system.github.io/ZealOS/System/TaskSettings.ZC.html#l3"><span class=cF4>SettingsPush</span></a><span class=cF0>
<a name="l47"></a> </span><span class=cF5>AutoComplete</span><span class=cF0>;
<a name="l48"></a> </span><span class=cF5>DocCursor</span><span class=cF0>;
<a name="l49"></a> </span><span class=cF5>DocClear</span><span class=cF0>;
<a name="l50"></a>
<a name="l51"></a> trigger = </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l52"></a> cur_apple = cur_dist = </span><span class=cFE>0</span><span class=cF0>;
<a name="l53"></a>
<a name="l54"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;draw_it = &amp;</span><span class=cF5>DrawIt</span><span class=cF0>;
<a name="l55"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;win_inhibit = </span><span class=cF3>WIG_TASK_DEFAULT</span><span class=cF0> - </span><span class=cF3>WIF_SELF_FOCUS</span><span class=cF0> - </span><span class=cF3>WIF_SELF_BORDER</span><span class=cF0>;
<a name="l56"></a>
<a name="l57"></a> </span><span class=cF1>try</span><span class=cF0>
<a name="l58"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l59"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>)
<a name="l60"></a> {
<a name="l61"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>MessageGet</span><span class=cF7>(</span><span class=cF0>&amp;arg1, &amp;arg2, </span><span class=cFE>1</span><span class=cF0> &lt;&lt; </span><span class=cF3>MESSAGE_KEY_DOWN</span><span class=cF0> | </span><span class=cFE>1</span><span class=cF0> &lt;&lt; </span><span class=cF3>MESSAGE_MS_L_DOWN</span><span class=cF7>)</span><span class=cF0> == </span><span class=cF3>MESSAGE_KEY_DOWN</span><span class=cF0>)
<a name="l62"></a> </span><span class=cF1>goto</span><span class=cF0> pi_done;
<a name="l63"></a>
<a name="l64"></a> </span><span class=cF2>//Wait for window mgr to call DrawIt()</span><span class=cF0>
<a name="l65"></a> trigger = </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l66"></a> </span><span class=cF1>do</span><span class=cF0> </span><span class=cF5>Refresh</span><span class=cF0>;
<a name="l67"></a> </span><span class=cF1>while</span><span class=cF0> (trigger);
<a name="l68"></a>
<a name="l69"></a> </span><span class=cF2>//Use screen coordinates, not window (arg1,arg2)</span><span class=cF0>
<a name="l70"></a> </span><span class=cF2>//</span><a href="https://zeal-operating-system.github.io/ZealOS/Kernel/KGlobals.ZC.html#l25"><span class=cF4>mouse.pos.x</span></a><span class=cF2> and </span><a href="https://zeal-operating-system.github.io/ZealOS/Kernel/KGlobals.ZC.html#l25"><span class=cF4>mouse.pos.y</span></a><span class=cF2> are global variables updated</span><span class=cF0>
<a name="l71"></a> </span><span class=cF2>//updated by the window mgr to hold the</span><span class=cF0>
<a name="l72"></a> </span><span class=cF2>//screen coordinates of the mouse.</span><span class=cF0>
<a name="l73"></a> delta_x = x[cur_apple] - </span><span class=cFB>mouse</span><span class=cF0>.pos.x;
<a name="l74"></a> delta_y = y[cur_apple] - </span><span class=cFB>mouse</span><span class=cF0>.pos.y;
<a name="l75"></a>
<a name="l76"></a> </span><span class=cF1>do</span><span class=cF0>
<a name="l77"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l78"></a> message_code = </span><span class=cF5>MessageGet</span><span class=cF0>(&amp;arg1, &amp;arg2, </span><span class=cFE>1</span><span class=cF0> &lt;&lt; </span><span class=cF3>MESSAGE_KEY_DOWN</span><span class=cF0> | </span><span class=cFE>1</span><span class=cF0> &lt;&lt; </span><span class=cF3>MESSAGE_MS_MOVE</span><span class=cF0> | </span><span class=cFE>1</span><span class=cF0> &lt;&lt; </span><span class=cF3>MESSAGE_MS_L_UP</span><span class=cF0>);
<a name="l79"></a> </span><span class=cF1>if</span><span class=cF0> (message_code == </span><span class=cF3>MESSAGE_KEY_DOWN</span><span class=cF0>)
<a name="l80"></a> </span><span class=cF1>goto</span><span class=cF0> pi_done;
<a name="l81"></a> x[cur_apple] = </span><span class=cFB>mouse</span><span class=cF0>.pos.x + delta_x;
<a name="l82"></a> y[cur_apple] = </span><span class=cFB>mouse</span><span class=cF0>.pos.y + delta_y;
<a name="l83"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l84"></a> </span><span class=cF1>while</span><span class=cF0> (message_code != </span><span class=cF3>MESSAGE_MS_L_UP</span><span class=cF0>);
<a name="l85"></a> }
<a name="l86"></a>pi_done:
<a name="l87"></a> </span><span class=cF5>MessageGet</span><span class=cF0>(,, </span><span class=cFE>1</span><span class=cF0> &lt;&lt; </span><span class=cF3>MESSAGE_KEY_UP</span><span class=cF0>);
<a name="l88"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l89"></a> </span><span class=cF1>catch</span><span class=cF0>
<a name="l90"></a> </span><span class=cF5>PutExcept</span><span class=cF0>;
<a name="l91"></a> </span><span class=cF5>SettingsPop</span><span class=cF0>;
<a name="l92"></a>}
<a name="l93"></a>
<a name="l94"></a>Pick;
</span></pre></body>
</html>