templeos-info/public/Wb/Adam/Gr/GrDC.HC.HTML

466 lines
50 KiB
HTML
Executable File

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="generator" content="TempleOS V5.03">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/style/templeos.css">
<script src="/script/templeos.js"></script>
<style type="text/css">
.cF0{color:#000000;background-color:#ffffff;}
.cF1{color:#0000aa;background-color:#ffffff;}
.cF2{color:#00aa00;background-color:#ffffff;}
.cF3{color:#00aaaa;background-color:#ffffff;}
.cF4{color:#aa0000;background-color:#ffffff;}
.cF5{color:#aa00aa;background-color:#ffffff;}
.cF6{color:#aa5500;background-color:#ffffff;}
.cF7{color:#aaaaaa;background-color:#ffffff;}
.cF8{color:#555555;background-color:#ffffff;}
.cF9{color:#5555ff;background-color:#ffffff;}
.cFA{color:#55ff55;background-color:#ffffff;}
.cFB{color:#55ffff;background-color:#ffffff;}
.cFC{color:#ff5555;background-color:#ffffff;}
.cFD{color:#ff55ff;background-color:#ffffff;}
.cFE{color:#ffff55;background-color:#ffffff;}
.cFF{color:#ffffff;background-color:#ffffff;}
</style>
</head>
<body>
<pre id="content">
<a name="l1"></a><span class=cF0>#</span><span class=cF1>help_index</span><span class=cF0> </span><span class=cF6>&quot;Graphics/Math/3D Transformation&quot;</span><span class=cF0>
<a name="l2"></a>#</span><span class=cF1>help_file</span><span class=cF0> </span><span class=cF6>&quot;::/Doc/Transform&quot;</span><span class=cF0>
<a name="l3"></a>
<a name="l4"></a>#</span><span class=cF1>define</span><span class=cF0> </span><span class=cF3>GR_SCALE</span><span class=cF0> (1&lt;&lt;32)
<a name="l5"></a>
<a name="l6"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>Mat4x4MulXYZ</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> *r,</span><span class=cF9>I64</span><span class=cF0> *_x,</span><span class=cF9>I64</span><span class=cF0> *_y,</span><span class=cF9>I64</span><span class=cF0> *_z)
<a name="l7"></a>{</span><span class=cF2>//Rotate 3D point using 4x4 matrix. Uses </span><a href="/Wb/Demo/Lectures/FixedPoint.HC.HTML#l1"><span class=cF4>fixed-point</span></a><span class=cF2>.</span><span class=cF0>
<a name="l8"></a> </span><span class=cF9>I64</span><span class=cF0> x1,y1,z1,xx=*_x,yy=*_y,zz=*_z;
<a name="l9"></a> x1=(r[0*4+0]*xx+r[0*4+1]*yy+r[0*4+2]*zz+r[0*4+3])&gt;&gt;32;
<a name="l10"></a> y1=(r[1*4+0]*xx+r[1*4+1]*yy+r[1*4+2]*zz+r[1*4+3])&gt;&gt;32;
<a name="l11"></a> z1=(r[2*4+0]*xx+r[2*4+1]*yy+r[2*4+2]*zz+r[2*4+3])&gt;&gt;32;
<a name="l12"></a> *_x=x1;*_y=y1;*_z=z1;
<a name="l13"></a>}
<a name="l14"></a>
<a name="l15"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>DCTransform</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0> *dc,</span><span class=cF9>I64</span><span class=cF0> *_x,</span><span class=cF9>I64</span><span class=cF0> *_y,</span><span class=cF9>I64</span><span class=cF0> *_z)
<a name="l16"></a>{</span><span class=cF2>//This is the dft dc-&gt;transform() callback.</span><span class=cF0>
<a name="l17"></a></span><span class=cF2>//Uses </span><a href="/Wb/Demo/Lectures/FixedPoint.HC.HTML#l1"><span class=cF4>fixed-point</span></a><span class=cF2>.</span><span class=cF0>
<a name="l18"></a> </span><span class=cF5>Mat4x4MulXYZ</span><span class=cF0>(dc-&gt;r,_x,_y,_z);
<a name="l19"></a> *_x+=dc-&gt;x;
<a name="l20"></a> *_y+=dc-&gt;y;
<a name="l21"></a> *_z+=dc-&gt;z;
<a name="l22"></a>}
<a name="l23"></a>
<a name="l24"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF9>I64</span><span class=cF0> *</span><span class=cF5>Mat4x4IdentEqu</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> *r)
<a name="l25"></a>{</span><span class=cF2>//Set matrix to identity. Uses </span><a href="/Wb/Demo/Lectures/FixedPoint.HC.HTML#l1"><span class=cF4>fixed-point</span></a><span class=cF2>.</span><span class=cF0>
<a name="l26"></a> </span><span class=cF5>MemSet</span><span class=cF0>(r,0,</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>I64</span><span class=cF7>)</span><span class=cF0>*16);
<a name="l27"></a> r[0*4+0].i32[1]=1;
<a name="l28"></a> r[1*4+1].i32[1]=1;
<a name="l29"></a> r[2*4+2].i32[1]=1;
<a name="l30"></a> r[3*4+3].i32[1]=1;
<a name="l31"></a> </span><span class=cF1>return</span><span class=cF0> r;
<a name="l32"></a>}
<a name="l33"></a>
<a name="l34"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF9>I64</span><span class=cF0> *</span><span class=cF5>Mat4x4IdentNew</span><span class=cF0>(</span><span class=cF9>CTask</span><span class=cF0> *mem_task=</span><span class=cF3>NULL</span><span class=cF0>)
<a name="l35"></a>{</span><span class=cF2>//MAlloc an identity matrix. Uses </span><a href="/Wb/Demo/Lectures/FixedPoint.HC.HTML#l1"><span class=cF4>fixed-point</span></a><span class=cF2>.</span><span class=cF0>
<a name="l36"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF5>Mat4x4IdentEqu</span><span class=cF0>(</span><span class=cF5>MAlloc</span><span class=cF7>(</span><span class=cF1>sizeof</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0>)*16,mem_task</span><span class=cF7>)</span><span class=cF0>);
<a name="l37"></a>}
<a name="l38"></a>
<a name="l39"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF9>I64</span><span class=cF0> </span><span class=cF5>Mat4x4NormSqr65536</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> *r)
<a name="l40"></a>{</span><span class=cF2>//Norm Squared of r.</span><span class=cF0>
<a name="l41"></a></span><span class=cF2>//(1.0/Sqrt(3))*65536=37837.22</span><span class=cF0>
<a name="l42"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF5>SqrI64</span><span class=cF0>(</span><span class=cF7>(</span><span class=cF0>r[0*4+0]*37838+r[0*4+1]*37838+r[0*4+2]*37838</span><span class=cF7>)</span><span class=cF0>&gt;&gt;32)+
<a name="l43"></a> </span><span class=cF5>SqrI64</span><span class=cF0>(</span><span class=cF7>(</span><span class=cF0>r[1*4+0]*37837+r[1*4+1]*37837+r[1*4+2]*37837</span><span class=cF7>)</span><span class=cF0>&gt;&gt;32)+
<a name="l44"></a> </span><span class=cF5>SqrI64</span><span class=cF0>(</span><span class=cF7>(</span><span class=cF0>r[2*4+0]*37837+r[2*4+1]*37837+r[2*4+2]*37837</span><span class=cF7>)</span><span class=cF0>&gt;&gt;32);
<a name="l45"></a>}
<a name="l46"></a>
<a name="l47"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>DCMat4x4Set</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0> *dc=</span><span class=cF3>NULL</span><span class=cF0>,</span><span class=cF9>I64</span><span class=cF0> *r)
<a name="l48"></a>{</span><span class=cF2>//Set device context's rot matrix. Will be </span><a href="/Wb/Kernel/Mem/MAllocFree.HC.HTML#l383"><span class=cF4>Free</span></a><span class=cF2>d() in </span><a href="/Wb/Adam/Gr/GrDC.HC.HTML#l208"><span class=cF4>DCDel</span></a><span class=cF2>().Uses </span><a href="/Wb/Demo/Lectures/FixedPoint.HC.HTML#l1"><span class=cF4>fixed-point</span></a><span class=cF2>.</span><span class=cF0>
<a name="l49"></a></span><span class=cF2>//The main purpose is to set matrix norm for thick scaling.</span><span class=cF0>
<a name="l50"></a> </span><span class=cF2>//NULL as dc means gr.dc</span><span class=cF0>
<a name="l51"></a> </span><span class=cF1>if</span><span class=cF0> (!dc) dc=</span><span class=cFB>gr</span><span class=cF0>.dc;
<a name="l52"></a> dc-&gt;r=r;
<a name="l53"></a> dc-&gt;r_norm=</span><span class=cF5>Sqrt</span><span class=cF0>(</span><span class=cF5>Mat4x4NormSqr65536</span><span class=cF7>(</span><span class=cF0>r</span><span class=cF7>)</span><span class=cF0>)*65536; </span><span class=cF2>//scaled 32 bits</span><span class=cF0>
<a name="l54"></a>}
<a name="l55"></a>
<a name="l56"></a>#</span><span class=cF1>help_index</span><span class=cF0> </span><span class=cF6>&quot;Graphics/Mesh&quot;</span><span class=cF0>
<a name="l57"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>DCLighting</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0> *dc,
<a name="l58"></a> </span><span class=cF9>CD3I32</span><span class=cF0> *p1,</span><span class=cF9>CD3I32</span><span class=cF0> *p2,</span><span class=cF9>CD3I32</span><span class=cF0> *p3,</span><span class=cF9>CColorROPU32</span><span class=cF0> color)
<a name="l59"></a>{</span><span class=cF2>//This is the dft dc-&gt;lighting() callback.</span><span class=cF0>
<a name="l60"></a> </span><span class=cF9>CD3I32</span><span class=cF0> v1,v2;
<a name="l61"></a> </span><span class=cF9>I64</span><span class=cF0> i,vn_x,vn_y,vn_z;
<a name="l62"></a> </span><span class=cF1>F64</span><span class=cF0> d;
<a name="l63"></a>
<a name="l64"></a> v1.x=p1-&gt;x-p2-&gt;x;
<a name="l65"></a> v1.y=p1-&gt;y-p2-&gt;y;
<a name="l66"></a> v1.z=p1-&gt;z-p2-&gt;z;
<a name="l67"></a>
<a name="l68"></a> v2.x=p3-&gt;x-p2-&gt;x;
<a name="l69"></a> v2.y=p3-&gt;y-p2-&gt;y;
<a name="l70"></a> v2.z=p3-&gt;z-p2-&gt;z;
<a name="l71"></a>
<a name="l72"></a> </span><span class=cF2>//V1 and V2 are vects along two sides</span><span class=cF0>
<a name="l73"></a> </span><span class=cF2>//of the tri joined at p2.</span><span class=cF0>
<a name="l74"></a>
<a name="l75"></a> vn_x=v1.y*v2.z-v1.z*v2.y;
<a name="l76"></a> vn_y=v1.z*v2.x-v1.x*v2.z;
<a name="l77"></a> vn_z=v1.x*v2.y-v1.y*v2.x;
<a name="l78"></a> </span><span class=cF1>if</span><span class=cF0> (d=</span><span class=cF5>Sqrt</span><span class=cF7>(</span><span class=cF5>SqrI64</span><span class=cF0>(vn_x)+</span><span class=cF5>SqrI64</span><span class=cF0>(vn_y)+</span><span class=cF5>SqrI64</span><span class=cF0>(vn_z)</span><span class=cF7>)</span><span class=cF0>)
<a name="l79"></a> d=1&lt;&lt;16/d;
<a name="l80"></a> vn_x*=d;
<a name="l81"></a> vn_y*=d;
<a name="l82"></a> vn_z*=d;
<a name="l83"></a></span><span class=cF2>//Vn is the cross product of V1 and V3</span><span class=cF0>
<a name="l84"></a> </span><span class=cF2>//which means it is perpendicular. It</span><span class=cF0>
<a name="l85"></a> </span><span class=cF2>//is the normal vect to the surface.</span><span class=cF0>
<a name="l86"></a> </span><span class=cF2>//It has been scaled to length 65536.</span><span class=cF0>
<a name="l87"></a>
<a name="l88"></a> </span><span class=cF2>//Light source has been scaled to length 65536.</span><span class=cF0>
<a name="l89"></a> i=(vn_x*dc-&gt;ls.x+vn_y*dc-&gt;ls.y+vn_z*dc-&gt;ls.z)&gt;&gt;16;
<a name="l90"></a></span><span class=cF2>//The dot product of the light source</span><span class=cF0>
<a name="l91"></a> </span><span class=cF2>//vect and the surface normal</span><span class=cF0>
<a name="l92"></a> </span><span class=cF2>//gives an illumination number.</span><span class=cF0>
<a name="l93"></a> </span><span class=cF2>//65536*65536&gt;&gt;16=65536</span><span class=cF0>
<a name="l94"></a>
<a name="l95"></a> </span><span class=cF2>//TempleOS will generate a random U16</span><span class=cF0>
<a name="l96"></a> </span><span class=cF2>//and compare to dither_probability_u16 and</span><span class=cF0>
<a name="l97"></a> </span><span class=cF2>//will pick from two colors.</span><span class=cF0>
<a name="l98"></a> </span><span class=cF2>//Probability dithering does not work with thick&gt;1 at this time.</span><span class=cF0>
<a name="l99"></a> </span><span class=cF1>if</span><span class=cF0> (color.c0.rop&amp;</span><span class=cF3>ROPBF_TWO_SIDED</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l100"></a> color.c0.rop&amp;=~</span><span class=cF3>ROPBF_TWO_SIDED</span><span class=cF0>;
<a name="l101"></a> i=</span><span class=cF5>AbsI64</span><span class=cF0>(i)&lt;&lt;1;
<a name="l102"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>else</span><span class=cF0>
<a name="l103"></a> i+=65536;
<a name="l104"></a> </span><span class=cF1>if</span><span class=cF0> (color.c0.rop&amp;</span><span class=cF3>ROPBF_HALF_RANGE_COLOR</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l105"></a> color.c0.rop&amp;=~</span><span class=cF3>ROPBF_HALF_RANGE_COLOR</span><span class=cF0>;
<a name="l106"></a> i&gt;&gt;=1;
<a name="l107"></a> </span><span class=cF1>if</span><span class=cF0> (color&gt;=8) {
<a name="l108"></a> color-=8;
<a name="l109"></a> i+=65536;
<a name="l110"></a> }
<a name="l111"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l112"></a> </span><span class=cF1>if</span><span class=cF0> (i&lt;65536) </span><span class=cF7>{</span><span class=cF0>
<a name="l113"></a> dc-&gt;color=</span><span class=cF3>ROPF_PROBABILITY_DITHER</span><span class=cF0>+color&lt;&lt;16+</span><span class=cF3>BLACK</span><span class=cF0>;
<a name="l114"></a> dc-&gt;dither_probability_u16=i;
<a name="l115"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l116"></a> dc-&gt;color=</span><span class=cF3>ROPF_PROBABILITY_DITHER</span><span class=cF0>+(color^8)&lt;&lt;16+color;
<a name="l117"></a> dc-&gt;dither_probability_u16=i-65536;
<a name="l118"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l119"></a>}
<a name="l120"></a>
<a name="l121"></a>#</span><span class=cF1>help_index</span><span class=cF0> </span><span class=cF6>&quot;Graphics/Device Contexts&quot;</span><span class=cF0>
<a name="l122"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>DCFill</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0> *dc=</span><span class=cF3>NULL</span><span class=cF0>,</span><span class=cF9>CColorROPU32</span><span class=cF0> val=</span><span class=cF3>TRANSPARENT</span><span class=cF0>)
<a name="l123"></a>{</span><span class=cF2>//Fill entire device context with color.</span><span class=cF0>
<a name="l124"></a> </span><span class=cF1>if</span><span class=cF0> (!dc) dc=</span><span class=cFB>gr</span><span class=cF0>.dc;
<a name="l125"></a> </span><span class=cF5>MemSet</span><span class=cF0>(dc-&gt;body,val,dc-&gt;width_internal*dc-&gt;height);
<a name="l126"></a>}
<a name="l127"></a>
<a name="l128"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>DCClear</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0> *dc=</span><span class=cF3>NULL</span><span class=cF0>)
<a name="l129"></a>{</span><span class=cF2>//Set entire device context image body to 0 (BLACK).</span><span class=cF0>
<a name="l130"></a> </span><span class=cF1>if</span><span class=cF0> (!dc) dc=</span><span class=cFB>gr</span><span class=cF0>.dc;
<a name="l131"></a> </span><span class=cF5>DCFill</span><span class=cF0>(dc,0);
<a name="l132"></a>}
<a name="l133"></a>
<a name="l134"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>DCRst</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0> *dc)
<a name="l135"></a>{</span><span class=cF2>//Reset </span><a href="/Wb/Kernel/KernelA.HH.HTML#l3599"><span class=cF4>CDC</span></a><span class=cF2> structure members but not image body, itself.</span><span class=cF0>
<a name="l136"></a> dc-&gt;color=</span><span class=cF3>BLACK</span><span class=cF0>;
<a name="l137"></a> dc-&gt;color2=</span><span class=cF3>BLACK</span><span class=cF0>;
<a name="l138"></a> dc-&gt;bkcolor=</span><span class=cF3>BLACK</span><span class=cF0>;
<a name="l139"></a> dc-&gt;collision_cnt=0;
<a name="l140"></a> dc-&gt;thick=1;
<a name="l141"></a> dc-&gt;ls.x=37837; </span><span class=cF2>//1&lt;&lt;16/Sqrt(3)</span><span class=cF0>
<a name="l142"></a> dc-&gt;ls.y=37837;
<a name="l143"></a> dc-&gt;ls.z=37837;
<a name="l144"></a> dc-&gt;x=0;
<a name="l145"></a> dc-&gt;y=0;
<a name="l146"></a> dc-&gt;z=0;
<a name="l147"></a> dc-&gt;transform=&amp;</span><span class=cF5>DCTransform</span><span class=cF0>;
<a name="l148"></a> dc-&gt;lighting =&amp;</span><span class=cF5>DCLighting</span><span class=cF0>;
<a name="l149"></a> </span><span class=cF5>Mat4x4IdentEqu</span><span class=cF0>(dc-&gt;r);
<a name="l150"></a> dc-&gt;r_norm=</span><span class=cF3>GR_SCALE</span><span class=cF0>;
<a name="l151"></a> dc-&gt;flags&amp;=~(</span><span class=cF3>DCF_SYMMETRY</span><span class=cF0>|</span><span class=cF3>DCF_TRANSFORMATION</span><span class=cF0>|</span><span class=cF3>DCF_JUST_MIRROR</span><span class=cF0>);
<a name="l152"></a> </span><span class=cF5>MemCpy</span><span class=cF0>(dc-&gt;palette,</span><span class=cFB>gr_palette_std</span><span class=cF0>,</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CBGR48</span><span class=cF7>)</span><span class=cF0>*</span><span class=cF3>COLORS_NUM</span><span class=cF0>);
<a name="l153"></a>}
<a name="l154"></a>
<a name="l155"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>DCExtentsInit</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0> *dc=</span><span class=cF3>NULL</span><span class=cF0>)
<a name="l156"></a>{</span><span class=cF2>//Init markers for extent of next newly drawn graphics.</span><span class=cF0>
<a name="l157"></a></span><span class=cF2>//NULL means gr.dc</span><span class=cF0>
<a name="l158"></a> </span><span class=cF2>//See </span><a href="/Wb/Demo/Graphics/Extents.HC.HTML#l1"><span class=cF4>::/Demo/Graphics/Extents.HC</span></a><span class=cF0>
<a name="l159"></a> </span><span class=cF2>//You should clear the record flag yourself</span><span class=cF0>
<a name="l160"></a> </span><span class=cF1>if</span><span class=cF0> (!dc) dc=</span><span class=cFB>gr</span><span class=cF0>.dc;
<a name="l161"></a> dc-&gt;flags|=</span><span class=cF3>DCF_RECORD_EXTENTS</span><span class=cF0>;
<a name="l162"></a> dc-&gt;min_x=</span><span class=cF3>I64_MAX</span><span class=cF0>;
<a name="l163"></a> dc-&gt;max_x=</span><span class=cF3>I64_MIN</span><span class=cF0>;
<a name="l164"></a> dc-&gt;min_y=</span><span class=cF3>I64_MAX</span><span class=cF0>;
<a name="l165"></a> dc-&gt;max_y=</span><span class=cF3>I64_MIN</span><span class=cF0>;
<a name="l166"></a>}
<a name="l167"></a>
<a name="l168"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF9>CDC</span><span class=cF0> *</span><span class=cF5>DCAlias</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0> *dc=</span><span class=cF3>NULL</span><span class=cF0>,</span><span class=cF9>CTask</span><span class=cF0> *task=</span><span class=cF3>NULL</span><span class=cF0>)
<a name="l169"></a>{</span><span class=cF2>//Create alias of dc, so can change pen, color, etc.</span><span class=cF0>
<a name="l170"></a></span><span class=cF2>//NULL means gr.dc</span><span class=cF0>
<a name="l171"></a> </span><span class=cF9>CDC</span><span class=cF0> *res;
<a name="l172"></a> </span><span class=cF1>if</span><span class=cF0> (!dc) dc=</span><span class=cFB>gr</span><span class=cF0>.dc;
<a name="l173"></a> </span><span class=cF1>if</span><span class=cF0> (!task) task=</span><span class=cF5>Fs</span><span class=cF0>;
<a name="l174"></a> </span><span class=cF1>if</span><span class=cF0> (dc-&gt;dc_signature!=</span><span class=cF3>DCS_SIGNATURE_VAL</span><span class=cF0>)
<a name="l175"></a> </span><span class=cF5>throw</span><span class=cF0>(</span><span class=cF6>'Graphics'</span><span class=cF0>);
<a name="l176"></a> res=</span><span class=cF5>MAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CDC</span><span class=cF7>)</span><span class=cF0>,task);
<a name="l177"></a> </span><span class=cF5>MemCpy</span><span class=cF0>(res,dc,</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CDC</span><span class=cF7>)</span><span class=cF0>);
<a name="l178"></a> res-&gt;win_task=res-&gt;mem_task=task;
<a name="l179"></a> res-&gt;r=</span><span class=cF5>MAlloc</span><span class=cF0>(16*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>I64</span><span class=cF7>)</span><span class=cF0>,task);
<a name="l180"></a> </span><span class=cF5>DCRst</span><span class=cF0>(res);
<a name="l181"></a> res-&gt;flags|=</span><span class=cF3>DCF_ALIAS</span><span class=cF0>;
<a name="l182"></a> res-&gt;alias=dc;
<a name="l183"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l184"></a>}
<a name="l185"></a>
<a name="l186"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF9>CDC</span><span class=cF0> *</span><span class=cF5>DCNew</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> width,</span><span class=cF9>I64</span><span class=cF0> height,</span><span class=cF9>CTask</span><span class=cF0> *task=</span><span class=cF3>NULL</span><span class=cF0>,</span><span class=cF1>Bool</span><span class=cF0> null_bitmap=</span><span class=cF3>FALSE</span><span class=cF0>)
<a name="l187"></a>{</span><span class=cF2>//Create new width x height device context.</span><span class=cF0>
<a name="l188"></a></span><span class=cF2>//Internally only allows widths which are divisible by 8.</span><span class=cF0>
<a name="l189"></a> </span><span class=cF2>//Don't forget these </span><span class=cF9><u>sizeof(CDC)</u></span><span class=cF2>.</span><span class=cF0>
<a name="l190"></a> </span><span class=cF9>CDC</span><span class=cF0> *res;
<a name="l191"></a> </span><span class=cF1>if</span><span class=cF0> (!task) task=</span><span class=cF5>Fs</span><span class=cF0>;
<a name="l192"></a> res=</span><span class=cF5>CAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CDC</span><span class=cF7>)</span><span class=cF0>,task);
<a name="l193"></a> res-&gt;win_task=task;
<a name="l194"></a> res-&gt;mem_task=task;
<a name="l195"></a> res-&gt;width=width;
<a name="l196"></a> res-&gt;width_internal=(width+7)&amp;~7;
<a name="l197"></a> res-&gt;height=height;
<a name="l198"></a> </span><span class=cF1>if</span><span class=cF0> (null_bitmap)
<a name="l199"></a> res-&gt;flags|=</span><span class=cF3>DCF_DONT_DRAW</span><span class=cF0>;
<a name="l200"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l201"></a> res-&gt;body=</span><span class=cF5>CAlloc</span><span class=cF0>(res-&gt;width_internal*res-&gt;height,task);
<a name="l202"></a> res-&gt;r=</span><span class=cF5>MAlloc</span><span class=cF0>(16*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>I64</span><span class=cF7>)</span><span class=cF0>,task);
<a name="l203"></a> </span><span class=cF5>DCRst</span><span class=cF0>(res);
<a name="l204"></a> res-&gt;dc_signature=</span><span class=cF3>DCS_SIGNATURE_VAL</span><span class=cF0>;
<a name="l205"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l206"></a>}
<a name="l207"></a>
<a name="l208"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>DCDel</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0> *dc)
<a name="l209"></a>{</span><span class=cF2>//Free dc, image body, rot mat and depth buf.</span><span class=cF0>
<a name="l210"></a> </span><span class=cF1>if</span><span class=cF0> (!dc) </span><span class=cF1>return</span><span class=cF0>;
<a name="l211"></a> </span><span class=cF1>if</span><span class=cF0> (dc-&gt;dc_signature!=</span><span class=cF3>DCS_SIGNATURE_VAL</span><span class=cF0>)
<a name="l212"></a> </span><span class=cF5>throw</span><span class=cF0>(</span><span class=cF6>'Graphics'</span><span class=cF0>);
<a name="l213"></a> dc-&gt;dc_signature=0;
<a name="l214"></a> </span><span class=cF5>Free</span><span class=cF0>(dc-&gt;r);
<a name="l215"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>dc-&gt;flags &amp; </span><span class=cF3>DCF_ALIAS</span><span class=cF7>)</span><span class=cF0>)
<a name="l216"></a> </span><span class=cF5>Free</span><span class=cF0>(dc-&gt;body);
<a name="l217"></a> </span><span class=cF5>Free</span><span class=cF0>(dc-&gt;depth_buf);
<a name="l218"></a> </span><span class=cF5>Free</span><span class=cF0>(dc);
<a name="l219"></a>}
<a name="l220"></a>
<a name="l221"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF9>I64</span><span class=cF0> </span><span class=cF5>DCSize</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0> *dc)
<a name="l222"></a>{</span><span class=cF2>//Mem size of header, image body and depth buffer.</span><span class=cF0>
<a name="l223"></a> </span><span class=cF1>if</span><span class=cF0> (dc)
<a name="l224"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF5>MSize2</span><span class=cF0>(dc)+</span><span class=cF5>MSize2</span><span class=cF0>(dc-&gt;body)+</span><span class=cF5>MSize2</span><span class=cF0>(dc-&gt;depth_buf);
<a name="l225"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l226"></a> </span><span class=cF1>return</span><span class=cF0> 0;
<a name="l227"></a>}
<a name="l228"></a>
<a name="l229"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF9>I32</span><span class=cF0> *</span><span class=cF5>DCDepthBufRst</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0> *dc)
<a name="l230"></a>{</span><span class=cF2>//Reset device context depth buf to far away.</span><span class=cF0>
<a name="l231"></a> </span><span class=cF1>if</span><span class=cF0> (dc-&gt;depth_buf)
<a name="l232"></a> </span><span class=cF5>MemSetU32</span><span class=cF0>(dc-&gt;depth_buf,</span><span class=cF3>I32_MAX</span><span class=cF0>,dc-&gt;width_internal*dc-&gt;height);
<a name="l233"></a> </span><span class=cF1>return</span><span class=cF0> dc-&gt;depth_buf;
<a name="l234"></a>}
<a name="l235"></a>
<a name="l236"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF9>I32</span><span class=cF0> *</span><span class=cF5>DCDepthBufAlloc</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0> *dc)
<a name="l237"></a>{</span><span class=cF2>//Alloc a 32-bit depth buffer for device context.</span><span class=cF0>
<a name="l238"></a> </span><span class=cF5>Free</span><span class=cF0>(dc-&gt;depth_buf);
<a name="l239"></a> dc-&gt;depth_buf=</span><span class=cF5>MAlloc</span><span class=cF0>(dc-&gt;width_internal*dc-&gt;height*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>I32</span><span class=cF7>)</span><span class=cF0>,dc-&gt;mem_task);
<a name="l240"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF5>DCDepthBufRst</span><span class=cF0>(dc);
<a name="l241"></a>}
<a name="l242"></a>
<a name="l243"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF9>CDC</span><span class=cF0> *</span><span class=cF5>DCCopy</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0> *dc,</span><span class=cF9>CTask</span><span class=cF0> *task=</span><span class=cF3>NULL</span><span class=cF0>)
<a name="l244"></a>{</span><span class=cF2>//Alloc copy of dc, including image body, rot mat and depth buf.</span><span class=cF0>
<a name="l245"></a> </span><span class=cF9>CDC</span><span class=cF0> *res;
<a name="l246"></a> </span><span class=cF1>if</span><span class=cF0> (!dc) </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l247"></a> </span><span class=cF1>if</span><span class=cF0> (dc-&gt;dc_signature!=</span><span class=cF3>DCS_SIGNATURE_VAL</span><span class=cF0>)
<a name="l248"></a> </span><span class=cF5>throw</span><span class=cF0>(</span><span class=cF6>'Graphics'</span><span class=cF0>);
<a name="l249"></a> res=</span><span class=cF5>MAllocIdent</span><span class=cF0>(dc,task);
<a name="l250"></a> </span><span class=cF5>DCMat4x4Set</span><span class=cF0>(res,</span><span class=cF5>Mat4x4New</span><span class=cF7>(</span><span class=cF0>dc-&gt;r,task</span><span class=cF7>)</span><span class=cF0>);
<a name="l251"></a> res-&gt;mem_task=task;
<a name="l252"></a> res-&gt;body=</span><span class=cF5>MAllocIdent</span><span class=cF0>(dc-&gt;body,task);
<a name="l253"></a> res-&gt;depth_buf=</span><span class=cF5>MAllocIdent</span><span class=cF0>(dc-&gt;depth_buf,task);
<a name="l254"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l255"></a>}
<a name="l256"></a>
<a name="l257"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>DCMono</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0> *dc,
<a name="l258"></a> </span><span class=cF9>I64</span><span class=cF0> quest=</span><span class=cF3>TRANSPARENT</span><span class=cF0>,</span><span class=cF9>I64</span><span class=cF0> true_color=0,</span><span class=cF9>I64</span><span class=cF0> false_color=</span><span class=cF3>COLOR_MONO</span><span class=cF0>)
<a name="l259"></a>{</span><span class=cF2>//Set entire device context to one of two colors.</span><span class=cF0>
<a name="l260"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l261"></a> </span><span class=cF1>U8</span><span class=cF0> *dst;
<a name="l262"></a> dst=dc-&gt;body;
<a name="l263"></a> i=dc-&gt;width_internal*dc-&gt;height;
<a name="l264"></a> </span><span class=cF1>while</span><span class=cF0> (i--)
<a name="l265"></a> </span><span class=cF1>if</span><span class=cF0> (*dst==quest)
<a name="l266"></a> *dst++=true_color;
<a name="l267"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l268"></a> *dst++=false_color;
<a name="l269"></a>}
<a name="l270"></a>
<a name="l271"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF9>I64</span><span class=cF0> </span><span class=cF5>DCColorChg</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0> *dc,</span><span class=cF9>I64</span><span class=cF0> src_color,</span><span class=cF9>I64</span><span class=cF0> dst_color=</span><span class=cF3>TRANSPARENT</span><span class=cF0>)
<a name="l272"></a>{</span><span class=cF2>//Find and replace src color with dst in device context.</span><span class=cF0>
<a name="l273"></a> </span><span class=cF9>I64</span><span class=cF0> i,res=0;
<a name="l274"></a> </span><span class=cF1>U8</span><span class=cF0> *dst;
<a name="l275"></a> dst=dc-&gt;body;
<a name="l276"></a> i=dc-&gt;width_internal*dc-&gt;height;
<a name="l277"></a> </span><span class=cF1>while</span><span class=cF0> (i--)
<a name="l278"></a> </span><span class=cF1>if</span><span class=cF0> (*dst==src_color) </span><span class=cF7>{</span><span class=cF0>
<a name="l279"></a> *dst++=dst_color;
<a name="l280"></a> res++;
<a name="l281"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>else</span><span class=cF0>
<a name="l282"></a> dst++;
<a name="l283"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l284"></a>}
<a name="l285"></a>
<a name="l286"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF1>U8</span><span class=cF0> *</span><span class=cF5>DCSave</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0> *dc,</span><span class=cF9>I64</span><span class=cF0> *_size=</span><span class=cF3>NULL</span><span class=cF0>,</span><span class=cF9>I64</span><span class=cF0> dcsf_flags=</span><span class=cF3>DCSF_COMPRESSED</span><span class=cF0>)
<a name="l287"></a>{</span><span class=cF2>//Stores device context to mem, perhaps, with compression.</span><span class=cF0>
<a name="l288"></a> </span><span class=cF1>U8</span><span class=cF0> *res,*ptr,*body;
<a name="l289"></a> </span><span class=cF9>CArcCompress</span><span class=cF0> *arc;
<a name="l290"></a> </span><span class=cF9>I64</span><span class=cF0> body_size=dc-&gt;width_internal*dc-&gt;height,total_size,flags;
<a name="l291"></a> </span><span class=cF9>CBGR48</span><span class=cF0> palette[</span><span class=cF3>COLORS_NUM</span><span class=cF0>];
<a name="l292"></a>
<a name="l293"></a> </span><span class=cF1>if</span><span class=cF0> (dcsf_flags&amp;</span><span class=cF3>DCSF_COMPRESSED</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l294"></a> arc=</span><span class=cF5>CompressBuf</span><span class=cF0>(dc-&gt;body,body_size);
<a name="l295"></a> body_size=arc-&gt;compressed_size;
<a name="l296"></a> body=arc;
<a name="l297"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l298"></a> arc=</span><span class=cF3>NULL</span><span class=cF0>;
<a name="l299"></a> body=dc-&gt;body;
<a name="l300"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l301"></a>
<a name="l302"></a> total_size=</span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0>.</span><span class=cF1>end</span><span class=cF0>)-</span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0>.</span><span class=cF1>start</span><span class=cF0>)+body_size;
<a name="l303"></a> </span><span class=cF1>if</span><span class=cF0> (dcsf_flags&amp;</span><span class=cF3>DCSF_COMPRESSED</span><span class=cF0>)
<a name="l304"></a> flags=</span><span class=cF3>DCF_COMPRESSED</span><span class=cF0>;
<a name="l305"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l306"></a> flags=0;
<a name="l307"></a>
<a name="l308"></a> </span><span class=cF1>if</span><span class=cF0> (dcsf_flags&amp;</span><span class=cF3>DCSF_PALETTE_GET</span><span class=cF0>)
<a name="l309"></a> </span><span class=cF5>GrPaletteGet</span><span class=cF0>(palette);
<a name="l310"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l311"></a> </span><span class=cF5>MemCpy</span><span class=cF0>(palette,&amp;dc-&gt;palette,</span><span class=cF3>COLORS_NUM</span><span class=cF0>*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CBGR48</span><span class=cF7>)</span><span class=cF0>);
<a name="l312"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>MemCmp</span><span class=cF7>(</span><span class=cF0>palette,</span><span class=cFB>gr_palette_std</span><span class=cF0>,</span><span class=cF3>COLORS_NUM</span><span class=cF0>*</span><span class=cF1>sizeof</span><span class=cF0>(</span><span class=cF9>CBGR48</span><span class=cF0>)</span><span class=cF7>)</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l313"></a> flags|=</span><span class=cF3>DCF_PALETTE</span><span class=cF0>;
<a name="l314"></a> total_size+=</span><span class=cF3>COLORS_NUM</span><span class=cF0>*</span><span class=cF1>sizeof</span><span class=cF0>(</span><span class=cF9>CBGR48</span><span class=cF0>);
<a name="l315"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l316"></a>
<a name="l317"></a> ptr=res=</span><span class=cF5>MAlloc</span><span class=cF0>(total_size);
<a name="l318"></a>
<a name="l319"></a>#</span><span class=cF1>assert</span><span class=cF0> !</span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0>.</span><span class=cF1>start</span><span class=cF0>)
<a name="l320"></a> </span><span class=cF5>MemCpy</span><span class=cF0>(ptr,&amp;dc-&gt;</span><span class=cF1>start</span><span class=cF0>,</span><span class=cF1>offset</span><span class=cF7>(</span><span class=cF9>CDC</span><span class=cF0>.</span><span class=cF1>end</span><span class=cF7>)</span><span class=cF0>-</span><span class=cF1>offset</span><span class=cF7>(</span><span class=cF9>CDC</span><span class=cF0>.</span><span class=cF1>start</span><span class=cF7>)</span><span class=cF0>);
<a name="l321"></a> ptr(</span><span class=cF9>CDC</span><span class=cF0> *)-&gt;flags=flags;
<a name="l322"></a> ptr+=</span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0>.</span><span class=cF1>end</span><span class=cF0>)-</span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0>.</span><span class=cF1>start</span><span class=cF0>);
<a name="l323"></a>
<a name="l324"></a>#</span><span class=cF1>assert</span><span class=cF0> </span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0>.</span><span class=cF1>end</span><span class=cF0>)==</span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0>.palette)
<a name="l325"></a> </span><span class=cF1>if</span><span class=cF0> (flags&amp;</span><span class=cF3>DCF_PALETTE</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l326"></a> </span><span class=cF5>MemCpy</span><span class=cF0>(ptr,palette,</span><span class=cF3>COLORS_NUM</span><span class=cF0>*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CBGR48</span><span class=cF7>)</span><span class=cF0>);
<a name="l327"></a> ptr+=</span><span class=cF3>COLORS_NUM</span><span class=cF0>*</span><span class=cF1>sizeof</span><span class=cF0>(</span><span class=cF9>CBGR48</span><span class=cF0>);
<a name="l328"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l329"></a>
<a name="l330"></a> </span><span class=cF5>MemCpy</span><span class=cF0>(ptr,body,body_size);
<a name="l331"></a> ptr+=body_size;
<a name="l332"></a>
<a name="l333"></a> </span><span class=cF5>Free</span><span class=cF0>(arc);
<a name="l334"></a> </span><span class=cF1>if</span><span class=cF0> (_size) *_size=total_size;
<a name="l335"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l336"></a>}
<a name="l337"></a>
<a name="l338"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF9>CDC</span><span class=cF0> *</span><span class=cF5>DCLoad</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> *src,</span><span class=cF9>I64</span><span class=cF0> *_size=</span><span class=cF3>NULL</span><span class=cF0>,</span><span class=cF9>CTask</span><span class=cF0> *task=</span><span class=cF3>NULL</span><span class=cF0>)
<a name="l339"></a>{</span><span class=cF2>//Loads device context from mem.</span><span class=cF0>
<a name="l340"></a> </span><span class=cF9>CDC</span><span class=cF0> *res;
<a name="l341"></a> </span><span class=cF1>U8</span><span class=cF0> *ptr=src;
<a name="l342"></a> </span><span class=cF9>CArcCompress</span><span class=cF0> *arc;
<a name="l343"></a> </span><span class=cF9>I64</span><span class=cF0> body_size;
<a name="l344"></a> </span><span class=cF1>if</span><span class=cF0> (!task) task=</span><span class=cF5>Fs</span><span class=cF0>;
<a name="l345"></a> res=</span><span class=cF5>CAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CDC</span><span class=cF7>)</span><span class=cF0>,task);
<a name="l346"></a> res-&gt;win_task=task;
<a name="l347"></a> res-&gt;mem_task=task;
<a name="l348"></a> </span><span class=cF5>MemCpy</span><span class=cF0>(&amp;res-&gt;</span><span class=cF1>start</span><span class=cF0>,ptr,</span><span class=cF1>offset</span><span class=cF7>(</span><span class=cF9>CDC</span><span class=cF0>.</span><span class=cF1>end</span><span class=cF7>)</span><span class=cF0>-</span><span class=cF1>offset</span><span class=cF7>(</span><span class=cF9>CDC</span><span class=cF0>.</span><span class=cF1>start</span><span class=cF7>)</span><span class=cF0>);
<a name="l349"></a> ptr+=</span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0>.</span><span class=cF1>end</span><span class=cF0>)-</span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDC</span><span class=cF0>.</span><span class=cF1>start</span><span class=cF0>);
<a name="l350"></a>
<a name="l351"></a> </span><span class=cF1>if</span><span class=cF0> (res-&gt;flags&amp;</span><span class=cF3>DCF_PALETTE</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l352"></a> </span><span class=cF5>MemCpy</span><span class=cF0>(&amp;res-&gt;palette,ptr,</span><span class=cF3>COLORS_NUM</span><span class=cF0>*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CBGR48</span><span class=cF7>)</span><span class=cF0>);
<a name="l353"></a> ptr+=</span><span class=cF3>COLORS_NUM</span><span class=cF0>*</span><span class=cF1>sizeof</span><span class=cF0>(</span><span class=cF9>CBGR48</span><span class=cF0>);
<a name="l354"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>else</span><span class=cF0>
<a name="l355"></a> </span><span class=cF5>MemCpy</span><span class=cF0>(&amp;res-&gt;palette,</span><span class=cFB>gr_palette_std</span><span class=cF0>,</span><span class=cF3>COLORS_NUM</span><span class=cF0>*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CBGR48</span><span class=cF7>)</span><span class=cF0>);
<a name="l356"></a>
<a name="l357"></a> body_size=res-&gt;width_internal*res-&gt;height;
<a name="l358"></a> </span><span class=cF1>if</span><span class=cF0> (res-&gt;flags&amp;</span><span class=cF3>DCF_COMPRESSED</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l359"></a> res-&gt;flags&amp;=~</span><span class=cF3>DCF_COMPRESSED</span><span class=cF0>;
<a name="l360"></a> arc=ptr;
<a name="l361"></a> res-&gt;body=</span><span class=cF5>ExpandBuf</span><span class=cF0>(arc,task);
<a name="l362"></a> ptr+=arc-&gt;compressed_size;
<a name="l363"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l364"></a> res-&gt;body=</span><span class=cF5>MAlloc</span><span class=cF0>(body_size,task);
<a name="l365"></a> </span><span class=cF5>MemCpy</span><span class=cF0>(res-&gt;body,ptr,body_size);
<a name="l366"></a> ptr+=body_size;
<a name="l367"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l368"></a> res-&gt;thick=1;
<a name="l369"></a> res-&gt;r=</span><span class=cF5>Mat4x4IdentNew</span><span class=cF0>(task);
<a name="l370"></a> res-&gt;r_norm.u32[1]=1;
<a name="l371"></a> res-&gt;dc_signature=</span><span class=cF3>DCS_SIGNATURE_VAL</span><span class=cF0>;
<a name="l372"></a> </span><span class=cF1>if</span><span class=cF0> (_size) *_size=ptr-src;
<a name="l373"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l374"></a>}
<a name="l375"></a>
<a name="l376"></a>#</span><span class=cF1>help_index</span><span class=cF0> </span><span class=cF6>&quot;Graphics/GR Files&quot;</span><span class=cF0>
<a name="l377"></a>#</span><span class=cF1>help_file</span><span class=cF0> </span><span class=cF6>&quot;::/Doc/GRFiles&quot;</span><span class=cF0>
<a name="l378"></a>#</span><span class=cF1>help_index</span><span class=cF0> </span><span class=cF6>&quot;Graphics/Device Contexts;Graphics/GR Files&quot;</span><span class=cF0>
<a name="l379"></a>
<a name="l380"></a>#</span><span class=cF1>define</span><span class=cF0> </span><span class=cF3>GR_FILE_MAX</span><span class=cF0> (</span><span class=cF1>offset</span><span class=cF7>(</span><span class=cF9>CDC</span><span class=cF0>.</span><span class=cF1>end</span><span class=cF7>)</span><span class=cF0>-</span><span class=cF1>offset</span><span class=cF7>(</span><span class=cF9>CDC</span><span class=cF0>.</span><span class=cF1>start</span><span class=cF7>)</span><span class=cF0>+\
<a name="l381"></a> </span><span class=cF3>COLORS_NUM</span><span class=cF0>*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CBGR48</span><span class=cF7>)</span><span class=cF0>+</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CArcCtrl</span><span class=cF7>)</span><span class=cF0>+</span><span class=cF3>GR_WIDTH</span><span class=cF0>*</span><span class=cF3>GR_HEIGHT</span><span class=cF0>)
<a name="l382"></a>
<a name="l383"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF9>I64</span><span class=cF0> </span><span class=cF5>GRWrite</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> *filename,</span><span class=cF9>CDC</span><span class=cF0> *dc,</span><span class=cF9>I64</span><span class=cF0> dcsf_flags=</span><span class=cF3>DCSF_COMPRESSED</span><span class=cF0>)
<a name="l384"></a>{</span><span class=cF2>//TempleOS GR File.</span><span class=cF0>
<a name="l385"></a> </span><span class=cF9>I64</span><span class=cF0> size;
<a name="l386"></a> </span><span class=cF1>U8</span><span class=cF0> *st=</span><span class=cF5>ExtDft</span><span class=cF0>(filename,</span><span class=cF6>&quot;GR.Z&quot;</span><span class=cF0>),
<a name="l387"></a> *src=</span><span class=cF5>DCSave</span><span class=cF0>(dc,&amp;size,dcsf_flags);
<a name="l388"></a> </span><span class=cF5>FileWrite</span><span class=cF0>(st,src,size);
<a name="l389"></a> </span><span class=cF5>Free</span><span class=cF0>(st);
<a name="l390"></a> </span><span class=cF5>Free</span><span class=cF0>(src);
<a name="l391"></a> </span><span class=cF1>return</span><span class=cF0> size;
<a name="l392"></a>}
<a name="l393"></a>
<a name="l394"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF9>CDC</span><span class=cF0> *</span><span class=cF5>GRRead</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> *filename,</span><span class=cF9>CTask</span><span class=cF0> *task=</span><span class=cF3>NULL</span><span class=cF0>)
<a name="l395"></a>{</span><span class=cF2>//TempleOS GR File.</span><span class=cF0>
<a name="l396"></a> </span><span class=cF9>CDC</span><span class=cF0> *dc=</span><span class=cF3>NULL</span><span class=cF0>;
<a name="l397"></a> </span><span class=cF1>U8</span><span class=cF0> *st=</span><span class=cF5>ExtDft</span><span class=cF0>(filename,</span><span class=cF6>&quot;GR.Z&quot;</span><span class=cF0>),
<a name="l398"></a> *src=</span><span class=cF5>FileRead</span><span class=cF0>(st);
<a name="l399"></a> </span><span class=cF1>if</span><span class=cF0> (src)
<a name="l400"></a> dc=</span><span class=cF5>DCLoad</span><span class=cF0>(src,,task);
<a name="l401"></a> </span><span class=cF5>Free</span><span class=cF0>(src);
<a name="l402"></a> </span><span class=cF5>Free</span><span class=cF0>(st);
<a name="l403"></a> </span><span class=cF1>return</span><span class=cF0> dc;
<a name="l404"></a>}
<a name="l405"></a>
<a name="l406"></a>#</span><span class=cF1>help_index</span><span class=cF0> </span><span class=cF6>&quot;Graphics/Sprite;Graphics/GR Files;DolDoc/Output;StdOut/DolDoc&quot;</span><span class=cF0>
<a name="l407"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>DocGR</span><span class=cF0>(</span><span class=cF9>CDoc</span><span class=cF0> *doc=</span><span class=cF3>NULL</span><span class=cF0>,</span><span class=cF1>U8</span><span class=cF0> *filename)
<a name="l408"></a>{</span><span class=cF2>//Put a GR file into a document as asprite.</span><span class=cF0>
<a name="l409"></a> </span><span class=cF9>CDC</span><span class=cF0> *dc=</span><span class=cF5>GRRead</span><span class=cF0>(filename);
<a name="l410"></a> </span><span class=cF9>CSprite</span><span class=cF0> *elems=</span><span class=cF5>DC2Sprite</span><span class=cF0>(dc);
<a name="l411"></a> </span><span class=cF5>DocSprite</span><span class=cF0>(doc,elems);
<a name="l412"></a> </span><span class=cF5>Free</span><span class=cF0>(elems);
<a name="l413"></a> </span><span class=cF5>DCDel</span><span class=cF0>(dc);
<a name="l414"></a>}
<a name="l415"></a>
<a name="l416"></a>#</span><span class=cF1>help_index</span><span class=cF0> </span><span class=cF6>&quot;Graphics/Device Contexts;Graphics/Scrn&quot;</span><span class=cF0>
<a name="l417"></a></span><span class=cF1>public</span><span class=cF0> </span><span class=cF9>CDC</span><span class=cF0> *</span><span class=cF5>DCScrnCapture</span><span class=cF0>(</span><span class=cF1>Bool</span><span class=cF0> include_zoom=</span><span class=cF3>TRUE</span><span class=cF0>,</span><span class=cF9>CTask</span><span class=cF0> *task=</span><span class=cF3>NULL</span><span class=cF0>)
<a name="l418"></a>{</span><span class=cF2>//Capture scrn to a device context.</span><span class=cF0>
<a name="l419"></a> </span><span class=cF9>CDC</span><span class=cF0> *dc;
<a name="l420"></a> </span><span class=cF1>U8</span><span class=cF0> *dst;
<a name="l421"></a> </span><span class=cF5>Refresh</span><span class=cF0>(0,</span><span class=cF3>FALSE</span><span class=cF0>);
<a name="l422"></a> </span><span class=cF1>if</span><span class=cF0> (include_zoom)
<a name="l423"></a> dc=</span><span class=cF5>DCCopy</span><span class=cF0>(</span><span class=cFB>gr</span><span class=cF0>.scrn_image,task);
<a name="l424"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l425"></a> dc=</span><span class=cF5>DCCopy</span><span class=cF0>(</span><span class=cFB>gr</span><span class=cF0>.dc1,task);
<a name="l426"></a> dc-&gt;flags&amp;=~</span><span class=cF3>DCF_SCRN_BITMAP</span><span class=cF0>;
<a name="l427"></a> dst=</span><span class=cF5>MAlloc</span><span class=cF0>(dc-&gt;width_internal*dc-&gt;height,task);
<a name="l428"></a></span><span class=cF2>//Pick background color that never occurs. COLOR_INVALID</span><span class=cF0>
<a name="l429"></a> </span><span class=cF5>GrBitMap4ToBitMap8</span><span class=cF0>(dst,dc-&gt;body,
<a name="l430"></a> </span><span class=cF7>(</span><span class=cF0>dc-&gt;width_internal*dc-&gt;height</span><span class=cF7>)</span><span class=cF0>&gt;&gt;1,</span><span class=cF3>COLOR_INVALID</span><span class=cF0>);
<a name="l431"></a> </span><span class=cF5>Free</span><span class=cF0>(dc-&gt;body);
<a name="l432"></a> dc-&gt;body=dst;
<a name="l433"></a> </span><span class=cF1>return</span><span class=cF0> dc;
<a name="l434"></a>}
</span></pre></body>
</html>