File size: 6,589 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
//This is a whimsical program which demonstrates some techniques.
#define BORDER 20
#define PTY_PT 0
#define PTY_CIRCLE 1
#define PTY_LINE 2
#define PTY_SPRITE 3
#define PTY_NUM 4
extern class PObj;
class PPt
{
CD3I32 p;
};
class PCircle
{
PObj *p;
I64 radius;
};
class PLine
{
PObj *p1,*p2;
};
class PCSprite
{
PObj *p;
U8 *img;
I64 *r,
*dr; //Rounding error might eventually screw this up
}
class PObj
{
PObj *next,*last;
I64 type,color;
union {
PPt p;
PCircle c;
PLine l;
PCSprite g;
};
};
class PickFrame
{
PObj o_head;
I64 o_cnts[PTY_NUM];
I64 cx,cy;
};
#define IMGS_NUM 3
<1>/* Graphics Not Rendered in HTML */
<2>/* Graphics Not Rendered in HTML */
<3>/* Graphics Not Rendered in HTML */
U8 *imgs[IMGS_NUM]={<1>,<2>,<3>};
U0 DrawIt(CTask *task,CDC *dc)
{
I64 *r,*old_r;
PickFrame *pf=FramePtr("PickFrame",task);
PObj *tmpo=pf->o_head.next;
pf->cx=task->pix_width>>1;
pf->cy=task->pix_height>>1;
DCDepthBufAlloc(dc);
dc->color=LTRED;
dc->thick=3;
GrBorder(dc,BORDER,BORDER,2*pf->cx-BORDER,2*pf->cy-BORDER);
while (tmpo!=&pf->o_head) {
dc->color=tmpo->color;
switch (tmpo->type) {
case PTY_PT:
GrLine(dc,pf->cx+tmpo->p.p.x+2,pf->cy+tmpo->p.p.y+2,
pf->cx+tmpo->p.p.x-2,pf->cy+tmpo->p.p.y-2);
GrLine(dc,pf->cx+tmpo->p.p.x-2,pf->cy+tmpo->p.p.y+2,
pf->cx+tmpo->p.p.x+2,pf->cy+tmpo->p.p.y-2);
break;
case PTY_CIRCLE:
GrCircle(dc,pf->cx+tmpo->c.p->p.p.x,pf->cy+tmpo->c.p->p.p.y,
tmpo->c.radius);
break;
case PTY_LINE:
GrLine(dc,pf->cx+tmpo->l.p1->p.p.x,pf->cy+tmpo->l.p1->p.p.y,
pf->cx+tmpo->l.p2->p.p.x,pf->cy+tmpo->l.p2->p.p.y);
break;
case PTY_SPRITE:
old_r=dc->r;
dc->r=tmpo->g.r;
dc->x=pf->cx+tmpo->g.p->p.p.x;
dc->y=pf->cy+tmpo->g.p->p.p.y;
dc->z=GR_Z_ALL;
dc->flags|=DCF_TRANSFORMATION;
Sprite3(dc,0,0,0,tmpo->g.img);
dc->flags&=~DCF_TRANSFORMATION;
dc->r=old_r;
//Updated each refresh, not guarenteed to be uniform.
//Rounding error might corrupt, as well.
r=Mat4x4MulMat4x4New(tmpo->g.dr,tmpo->g.r,task);
Free(tmpo->g.r);
tmpo->g.r=r;
break;
}
tmpo=tmpo->next;
}
}
PObj *PObjNew(PickFrame *pf,I64 type,I64 color)
{
PObj *tmpo=CAlloc(sizeof(PObj));
tmpo->type=type;
tmpo->color=color;
pf->o_cnts[type]++;
QueIns(tmpo,pf->o_head.last);
return tmpo;
}
U0 PObjDel(PickFrame *pf,PObj *tmpo)
{
QueRem(tmpo);
switch (tmpo->type) {
case PTY_SPRITE:
Free(tmpo->g.r);
Free(tmpo->g.dr);
break;
}
pf->o_cnts[tmpo->type]--;
Free(tmpo);
}
PObj *PPtNew(PickFrame *pf,I64 x,I64 y)
{
PObj *tmpo=PObjNew(pf,PTY_PT,BLACK);
tmpo->p.p.x=x;
tmpo->p.p.y=y;
return tmpo;
}
PObj *PPtNum(PickFrame *pf,I64 num)
{
PObj *tmpo=pf->o_head.next;
while (tmpo!=&pf->o_head) {
if (tmpo->type==PTY_PT && !num--)
return tmpo;
tmpo=tmpo->next;
}
return NULL;
}
PObj *PPtFind(PickFrame *pf,I64 x,I64 y)
{
I64 dd,best_dd=I64_MAX;
PObj *tmpo=pf->o_head.next,*res=NULL;
while (tmpo!=&pf->o_head) {
if (tmpo->type==PTY_PT) {
dd=SqrI64(tmpo->p.p.x-x)+SqrI64(tmpo->p.p.y-y);
if (dd<best_dd) {
best_dd=dd;
res=tmpo;
}
}
tmpo=tmpo->next;
}
return res;
}
PObj *PCircleNew(PickFrame *pf,I64 p_num,I64 r)
{
PObj *tmpo=PObjNew(pf,PTY_CIRCLE,RED);
tmpo->c.p=PPtNum(pf,p_num);
tmpo->c.radius=r;
return tmpo;
}
PObj *PLineNew(PickFrame *pf,I64 p1_num,I64 p2_num)
{
PObj *tmpo=PObjNew(pf,PTY_LINE,GREEN);
tmpo->l.p1=PPtNum(pf,p1_num);
tmpo->l.p2=PPtNum(pf,p2_num);
return tmpo;
}
PObj *PCSpriteNew(PickFrame *pf,U8 *img,I64 p_num,I64 *r,I64 *dr)
{
PObj *tmpo=PObjNew(pf,PTY_SPRITE,BLACK);
tmpo->g.p=PPtNum(pf,p_num);
tmpo->g.img=img;
tmpo->g.r=r;
tmpo->g.dr=dr;
return tmpo;
}
PickFrame *Init()
{
PickFrame *pf=CAlloc(sizeof(PickFrame));
I64 i,*r,*dr;
pf->cx=Fs->pix_width>>1;
pf->cy=Fs->pix_height>>1;
pf->o_head.next=pf->o_head.last=&pf->o_head;
for (i=0;i<50;i++)
PPtNew(pf,RandI32%(pf->cx-BORDER),RandI32%(pf->cy-BORDER));
for (i=0;i<20;i++)
PCircleNew(pf,pf->o_cnts[PTY_PT]*RandU16/U16_MAX,6);
for (i=0;i<20;i++)
PLineNew(pf,pf->o_cnts[PTY_PT]*RandU16/U16_MAX,
pf->o_cnts[PTY_PT]*RandU16/U16_MAX);
for (i=0;i<10;i++) {
r=Mat4x4IdentNew;
dr=Mat4x4IdentNew;
Mat4x4RotZ(dr,0.05*2*(Rand-0.5));
Mat4x4RotY(dr,0.05*2*(Rand-0.5));
Mat4x4RotX(dr,0.05*2*(Rand-0.5));
PCSpriteNew(pf,imgs[IMGS_NUM*RandU16/U16_MAX],
pf->o_cnts[PTY_PT]*RandU16/U16_MAX,r,dr);
}
FramePtrSet("PickFrame",pf);
return pf;
}
U0 CleanUp(PickFrame *pf)
{
PObj *tmpo=pf->o_head.next,*tmpo1;
while (tmpo!=&pf->o_head) {
tmpo1=tmpo->next;
PObjDel(pf,tmpo);
tmpo=tmpo1;
}
Free(pf);
}
U0 Pick3D()
{
I64 msg_code,arg1,arg2;
PObj *tmpo;
PickFrame *pf=NULL;
FramePtrAdd("PickFrame");
MenuPush(
"File {"
" Abort(,CH_SHIFT_ESC);"
" Exit(,CH_ESC);"
"}"
"Play {"
" Restart(,'\n');"
"}"
);
SettingsPush; //See SettingsPush
AutoComplete;
WinBorder;
WinMax;
DocClear;
"$BK,1$Move things around.$BK,0$\n";
pf=Init;
tmpo=NULL;
Fs->win_inhibit=WIG_TASK_DFT-WIF_SELF_FOCUS
-WIF_SELF_CTRLS-WIF_FOCUS_TASK_MENU;
Fs->draw_it=&DrawIt;
try {
while (TRUE) {
switch (msg_code=GetMsg(&arg1,&arg2,
1<<MSG_KEY_DOWN|1<<MSG_MS_L_DOWN|1<<MSG_MS_L_UP|1<<MSG_MS_MOVE)) {
case MSG_KEY_DOWN:
switch (arg1) {
case '\n':
CleanUp(pf);
pf=Init;
tmpo=NULL;
break;
case CH_SHIFT_ESC:
case CH_ESC:
goto pd_done;
}
break;
case MSG_MS_L_DOWN:
tmpo=PPtFind(pf,arg1-pf->cx,arg2-pf->cy);
break;
case MSG_MS_L_UP:
if (tmpo) {
tmpo->p.p.x=arg1-pf->cx;
tmpo->p.p.y=arg2-pf->cy;
tmpo=NULL;
}
break;
case MSG_MS_MOVE:
if (tmpo) {
tmpo->p.p.x=arg1-pf->cx;
tmpo->p.p.y=arg2-pf->cy;
}
break;
}
}
pd_done:
GetMsg(,,1<<MSG_KEY_UP);
} catch
PutExcept;
SettingsPop;
MenuPop;
CleanUp(pf);
FramePtrDel("PickFrame");
}
Pick3D;
|