From a63d34a43ae15cc5e579dfafd6c64a1fe4880f80 Mon Sep 17 00:00:00 2001 From: Ian C Date: Mon, 6 Jun 2011 07:59:19 +0000 Subject: Readability reformatting. --- xd.c | 1252 ++++++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 721 insertions(+), 531 deletions(-) diff --git a/xd.c b/xd.c index cc2502f..52a694a 100644 --- a/xd.c +++ b/xd.c @@ -29,6 +29,7 @@ static const char id[]="$Id$"; #include #include #include +#include #include #include @@ -66,83 +67,84 @@ typedef enum {UsesImage, UsesPixmap} DType; typedef enum {MousePress, MouseRelease, MouseMove} Mouse; typedef struct - { - const char *name; - void (*draw)(void); - DType type; - } DemoInfo; +{ + const char *name; + void (*draw)(void); + DType type; +} DemoInfo; typedef struct - { - double x,y,z; - } Point3D; +{ + double x,y,z; +} Point3D; #define MAX_POLY_POINTS 32 typedef struct - { - int p[MAX_POLY_POINTS]; - int no; - int col; - } Poly3D; +{ + int p[MAX_POLY_POINTS]; + int no; + int col; +} Poly3D; typedef struct - { - int no_points; - int no_poly; +{ + int no_points; + int no_poly; - Point3D origin; + Point3D origin; - Point3D *ps; - Poly3D *poly; + Point3D *ps; + Poly3D *poly; - Point3D *workp3; - XPoint *workxp; + Point3D *workp3; + XPoint *workxp; - int x_rot; - int y_rot; - int z_rot; + int x_rot; + int y_rot; + int z_rot; - int mode; + int mode; - int col; + int col; - int cull; - } Obj3D; + int cull; +} Obj3D; #define MODE_SOLID 0 #define MODE_WIRE 1 #define MODE_EDGE 2 typedef struct - { - Point3D origin; - int radius; - int mode; - unsigned long col; - } Sphere3D; +{ + Point3D origin; + int radius; + int mode; + unsigned long col; +} Sphere3D; typedef struct - { - Point3D origin; - int w,h; - int mode; - unsigned long col; - } Rect3D; +{ + Point3D origin; + int w; + int h; + int mode; + unsigned long col; +} Rect3D; typedef struct - { - Point3D origin; - unsigned long col; - } Particle3D; +{ + Point3D origin; + unsigned long col; +} Particle3D; typedef enum {eObj, eSphere, eRect, eParticle} ObjType; typedef struct - { - ObjType type; - void *p; - } WorldObj; +{ + ObjType type; + void *p; +} WorldObj; /* ---------------------------------------- DEMO PROTOS */ @@ -346,17 +348,21 @@ int main(int argc, char *argv[]) int f; for(f=1;f=width) - { + { *ix=-(*ix); *x+=*ix; - } + } if (*y<0 || *y>=height) - { + { *iy=-(*iy); *y+=*iy; - } + } } @@ -487,35 +498,43 @@ static void OpenX(void) /* Open display and find an appropriate visual */ if (!(disp=XOpenDisplay(NULL))) + { Error("Couldn't open X display"); + } if (!XMatchVisualInfo(disp,DefaultScreen(disp),32,TrueColor,&visual) && !XMatchVisualInfo(disp,DefaultScreen(disp),24,TrueColor,&visual) && !XMatchVisualInfo(disp,DefaultScreen(disp),16,TrueColor,&visual) && !XMatchVisualInfo(disp,DefaultScreen(disp),15,TrueColor,&visual)) - Error("Couldn't find a visual\n"); + { + Error("Couldn't find a visual"); + } font=XLoadQueryFont(disp,"fixed"); if (!font) + { Error("Couldn't load font fixed"); + } width=640; height=480; if (!use_root) + { win=XCreateWindow(disp,DefaultRootWindow(disp), 0,0,width,height,0, visual.depth, InputOutput, visual.visual, 0,NULL); + } else - { - win=DefaultRootWindow(disp); + { + win=DefaultRootWindow(disp); width=DisplayWidth(disp,DefaultScreen(disp)); height=DisplayHeight(disp,DefaultScreen(disp)); - } + } centre_x=width/2; centre_y=height/2; @@ -553,12 +572,12 @@ static void OpenX(void) &gc_val); if (use_root) - { + { /* XSetCloseDownMode(disp,RetainTemporary); XSetWindowBackgroundPixmap(disp,win,pix); */ - } + } /* TODO: XImage creation */ @@ -585,11 +604,13 @@ static void ClsCol(unsigned long col) static void Redraw(void) { if (demotbl[demo].type==UsesPixmap) + { XCopyArea(disp,pix,win,gc,0,0,width,height,0,0); + } else - { + { /* TODO: XImage copy */ - } + } /* XFlush(disp); */ XSync(disp,FALSE); } @@ -619,10 +640,10 @@ static void Plot(int x,int y,int col) static void Circle(int x,int y,int r,int col) { if (r<1) - { + { Plot(x,y,col); return; - } + } SetCol(col); XDrawArc(disp,pix,gc,x-r,y-r,r*2,r*2,0,64*360); @@ -632,10 +653,10 @@ static void Circle(int x,int y,int r,int col) static void FCircle(int x,int y,int r,int col) { if (r<1) - { + { Plot(x,y,col); return; - } + } SetCol(col); XFillArc(disp,pix,gc,x-r,y-r,r*2,r*2,0,64*360); @@ -664,10 +685,10 @@ static void Print(int x,int y,int col,char *fmt,...) static void Rect(int x,int y,int w,int h,int col) { if (w==0 && h==0) - { + { Plot(x,y,col); return; - } + } SetCol(col); XDrawRectangle(disp,pix,gc,x,y,w,h); @@ -677,10 +698,10 @@ static void Rect(int x,int y,int w,int h,int col) static void FRect(int x,int y,int w,int h,int col) { if (w==0 && h==0) - { + { Plot(x,y,col); return; - } + } SetCol(col); XFillRectangle(disp,pix,gc,x,y,w,h); @@ -692,7 +713,9 @@ static void Polygon(XPoint p[], int no, int col) int f; for(f=0;f1800) + { ang=1800-(ang-1800); + } d=(int)((no_col/1800.0)*(double)ang); @@ -826,17 +855,19 @@ static void RotateX(double cx, double cy, double cz, double dy,dz,si,co; while(a<0) + { a+=3600; + } a%=3600; if (a==0) - { + { *x=ix; *y=iy; *z=iz; return; - } + } dy=iy-cy; dz=iz-cz; @@ -858,17 +889,19 @@ static void RotateY(double cx, double cy, double cz, double dx,dz,si,co; while(a<0) + { a+=3600; + } a%=3600; if (a==0) - { + { *x=ix; *y=iy; *z=iz; return; - } + } dx=ix-cx; dz=iz-cz; @@ -890,17 +923,19 @@ static void RotateZ(double cx, double cy, double cz, double dy,dx,si,co; while(a<0) + { a+=3600; + } a%=3600; if (a==0) - { + { *x=ix; *y=iy; *z=iz; return; - } + } dy=iy-cy; dx=ix-cx; @@ -919,7 +954,7 @@ static void DrawObj(Obj3D *o, unsigned long cmap[]) int f; for(f=0;fno_points;f++) - { + { RotateX(0,0,0, o->ps[f].x,o->ps[f].y,o->ps[f].z, &o->workp3[f].x,&o->workp3[f].y,&o->workp3[f].z, @@ -939,29 +974,38 @@ static void DrawObj(Obj3D *o, unsigned long cmap[]) o->origin.y+o->workp3[f].y, o->origin.z+o->workp3[f].z, o->workxp+f); - } + } for(f=0;fno_poly;f++) - { + { static XPoint p[MAX_POLY_POINTS]; int r; int c=-1; int col; for(r=0;rpoly[f].no;r++) + { p[r]=o->workxp[o->poly[f].p[r]]; + } if (o->cull) + { c=(p[0].x-p[1].x)*(p[2].y-p[1].y)-(p[0].y-p[1].y)*(p[2].x-p[1].x); + } if (o->col!=-1) + { col=cmap[o->col]; + } else + { col=cmap[o->poly[f].col]; + } if (c<0) + { switch(o->mode) - { + { case MODE_SOLID: FPolygon(p,o->poly[f].no,col); break; @@ -973,8 +1017,9 @@ static void DrawObj(Obj3D *o, unsigned long cmap[]) default: Polygon(p,o->poly[f].no,col); break; - } + } } + } } @@ -987,7 +1032,7 @@ static int ZSort(const void *p1, const void *p2) o2=p2; switch(o1->type) - { + { case eObj: z1=((const Obj3D *)o1->p)->origin.z; break; @@ -1003,10 +1048,10 @@ static int ZSort(const void *p1, const void *p2) default: z1=-1; break; - } + } switch(o2->type) - { + { case eObj: z2=((const Obj3D *)o2->p)->origin.z; break; @@ -1022,7 +1067,7 @@ static int ZSort(const void *p1, const void *p2) default: z2=-1; break; - } + } return (int)(z2-z1); } @@ -1037,7 +1082,7 @@ static int InvZSort(const void *p1, const void *p2) o2=p2; switch(o1->type) - { + { case eObj: z1=((const Obj3D *)o1->p)->origin.z; break; @@ -1053,10 +1098,10 @@ static int InvZSort(const void *p1, const void *p2) default: z1=-1; break; - } + } switch(o2->type) - { + { case eObj: z2=((const Obj3D *)o2->p)->origin.z; break; @@ -1072,7 +1117,7 @@ static int InvZSort(const void *p1, const void *p2) default: z2=-1; break; - } + } return (int)(z1-z2); } @@ -1088,17 +1133,21 @@ static void DrawList(WorldObj *w, int no, unsigned long cmap[], Rect3D *r; if (do_sort) - { + { if (inv_z_order) + { qsort(w,no,sizeof(WorldObj),InvZSort); + } else + { qsort(w,no,sizeof(WorldObj),ZSort); } + } for(f=0;forigin.x,s->origin.y,s->origin.z,&xp)) + { if (Project(s->origin.x+s->radius,s->origin.y, s->origin.z,&xp2)) + { switch(s->mode) - { + { case MODE_SOLID: FCircle(xp.x,xp.y,xp2.x-xp.x,cmap[s->col]); break; @@ -1121,23 +1172,26 @@ static void DrawList(WorldObj *w, int no, unsigned long cmap[], FCircle(xp.x,xp.y,xp2.x-xp.x,black); Circle(xp.x,xp.y,xp2.x-xp.x,cmap[s->col]); break; - } + } + } + } break; case eRect: r=w[f].p; if (Project(r->origin.x,r->origin.y,r->origin.z,&xp)) + { if (Project(r->origin.x+r->w,r->origin.y+r->h, r->origin.z,&xp2)) - { + { int w,h; w=xp2.x-xp.x; h=xp2.y-xp.y; switch(r->mode) - { + { case MODE_SOLID: FRect(xp.x,xp.y,w,h,cmap[r->col]); break; @@ -1148,22 +1202,25 @@ static void DrawList(WorldObj *w, int no, unsigned long cmap[], FRect(xp.x,xp.y,w,h,black); Rect(xp.x,xp.y,w,h,cmap[r->col]); break; - } } + } + } break; case eParticle: p=w[f].p; if (Project(p->origin.x,p->origin.y,p->origin.z,&xp)) + { Plot(xp.x,xp.y,cmap[p->col]); + } break; default: break; - } } + } } @@ -1185,28 +1242,28 @@ static void DrawSubList(Obj3D *o, int obj_no, i=0; for(f=0;fnext=NULL; if (!head) + { head=tail=new; + } else - { + { tail->next=new; tail=new; - } + } last=time(NULL); - } + } if (clr) + { Cls(); + } d=head; while(d) - { + { if (d->c>=256) - { + { Drop *t; if (d->next) + { head=d->next; + } else + { head=tail=NULL; + } t=d; d=d->next; Release(t); - } + } else - { + { Circle(d->x,d->y,d->r,col[d->c]); d->r+=scale; d->c+=speed; d=d->next; - } } + } } @@ -1338,12 +1407,12 @@ static void AutoDropDemo(void) static time_t last=0; typedef struct Drop - { + { int x,y,r,c; int speed; int scale; struct Drop *next; - } Drop; + } Drop; static Drop *head=NULL; static Drop *tail=NULL; @@ -1352,17 +1421,19 @@ static void AutoDropDemo(void) int f; if (!init) - { + { int f; for(f=0;f<256;f++) + { col[f]=GetCol(MAX(255-f*2,0),MAX(255-f*2,0),255-f); + } init=TRUE; - } + } if (last!=time(NULL) && !head) - { + { int x,y,sc; x=RND(width); @@ -1370,7 +1441,7 @@ static void AutoDropDemo(void) sc=scale; for(f=0;fnext=NULL; if (!head) + { head=tail=new; + } else - { + { tail->next=new; tail=new; - } } + } last=time(NULL); - } + } /* Cls(); */ d=head; while(d) - { + { if (d->c>=256) - { + { Drop *t; if (d->next) - head=d->next; + { + head=d->next; + } else - head=tail=NULL; + { + head=tail=NULL; + } t=d; d=d->next; Release(t); - } + } else - { + { Circle(d->x,d->y,d->r,col[d->c]); d->r+=d->scale; d->c+=d->speed; d=d->next; - } } + } } /* ---------------------------------------- FAST DROP DEMO -*/ + */ static void FastDropDemo(void) { static const int speed=30; @@ -1437,10 +1514,10 @@ static void FastDropDemo(void) static time_t last=0; typedef struct Drop - { + { int x,y,r,c; struct Drop *next; - } Drop; + } Drop; static Drop *head=NULL; static Drop *tail=NULL; @@ -1448,28 +1525,30 @@ static void FastDropDemo(void) Drop *d; if (!init) - { + { int f; for(f=0;f<256;f++) + { col[f]=GetCol(255-f,255-f,255-f); + } init=TRUE; - } + } if (mouse_b&Button3Mask) - { + { static time_t bounce=0; if (bounce!=time(NULL)) - { + { clr=!clr; bounce=time(NULL); - } } + } if (mouse_b&Button1Mask /*&& (last!=time(NULL))*/) - { + { Drop *new; new=Grab(sizeof *new); @@ -1481,50 +1560,58 @@ static void FastDropDemo(void) new->next=NULL; if (!head) + { head=tail=new; + } else - { + { tail->next=new; tail=new; - } + } last=time(NULL); - } + } if (clr) + { Cls(); + } d=head; while(d) - { + { if (d->c>=256) - { + { Drop *t; if (d->next) - head=d->next; + { + head=d->next; + } else - head=tail=NULL; + { + head=tail=NULL; + } t=d; d=d->next; Release(t); - } + } else - { + { Rect(d->x-d->r/2,d->y-d->r/2,d->r,d->r,col[d->c]); d->r+=scale; d->c+=speed; d=d->next; - } } + } } /* ---------------------------------------- TRAIL DEMO -*/ + */ static void TrailDemo(void) { static const int NO=20; @@ -1534,15 +1621,15 @@ static void TrailDemo(void) static unsigned long col[256]; static struct - { + { int x,y,xi,yi; - } *p; + } *p; typedef struct Drop - { + { int x,y,r,c; struct Drop *next; - } Drop; + } Drop; static Drop *head=NULL; static Drop *tail=NULL; @@ -1551,28 +1638,31 @@ static void TrailDemo(void) int f; if (!init) - { + { for(f=0;f<256;f++) + { col[f]=GetCol(255-f,255-f,255-f); + } p=Grab(sizeof(*p)*NO); for(f=0;fnext=NULL; if (!head) + { head=tail=new; + } else - { + { tail->next=new; tail=new; - } + } Bounce(&p[f].x,&p[f].y,&p[f].xi,&p[f].yi); - } + } /* Cls(); */ d=head; while(d) - { + { if (d->c>=256) - { + { Drop *t; if (d->next) - head=d->next; + { + head=d->next; + } else - head=tail=NULL; + { + head=tail=NULL; + } t=d; d=d->next; Release(t); - } + } else - { + { FRect(d->x-d->r/2,d->y-d->r/2,d->r,d->r,col[d->c]); d->r+=scale; d->c+=speed; d=d->next; - } } + } } /* ---------------------------------------- TUNNEL DEMO -*/ + */ static void TunnelDemo(void) { static const int speed=100; @@ -1644,14 +1740,14 @@ static void TunnelDemo(void) static int create=0; typedef struct Drop - { + { double x[points]; double y[points]; double z; int r,c; int ang; struct Drop *next; - } Drop; + } Drop; static Drop *head=NULL; static Drop *tail=NULL; @@ -1660,44 +1756,46 @@ static void TunnelDemo(void) int f; if (!init) - { + { for(f=0;f<256;f++) + { col[f]=GetCol(255-f,255-f,255-f); + } si=Grab(sizeof(double)*points); co=Grab(sizeof(double)*points); for(f=0;fx[f]=(RND(radius>>1)+radius>>1)*si[f]; new->y[f]=(RND(radius>>1)+radius>>1)*co[f]; - } + } new->r=radius; new->z=max_z; @@ -1706,73 +1804,89 @@ static void TunnelDemo(void) new->ang=RND(20)-10; if (new->ang<0) + { new->ang+=3600; + } new->next=NULL; if (!head) + { head=tail=new; + } else - { + { tail->next=new; tail=new; - } + } create=delay; - } + } else - create--; + { + create--; + } if (clr) + { Cls(); + } d=head; while(d) - { + { if (d->z<10) - { + { Drop *t; if (d->next) - head=d->next; + { + head=d->next; + } else - head=tail=NULL; + { + head=tail=NULL; + } t=d; d=d->next; Release(t); - } + } else - { + { XPoint p[points]; if (d->c) + { d->c=MAX(0,d->c-10); + } for(f=0;fz, d->x[f],d->y[f],d->z, &d->x[f],&d->y[f],&d->z, d->ang); Project(d->x[f],d->y[f],d->z,p+f); - } + } for(f=0;fc]); + } d->z-=speed; d=d->next; - } } + } } /* ---------------------------------------- STAR DEMO -*/ + */ static void StarDemo(void) { # define TYPE Particle3D @@ -1782,36 +1896,36 @@ static void StarDemo(void) static const int no_poly=4; static Point3D pset[4]= - { - {-100,50,100}, - {100,50,100}, - {0,-50,100}, - {0,0,-300}, - }; + { + {-100,50,100}, + {100,50,100}, + {0,-50,100}, + {0,0,-300}, + }; static Poly3D plist[4]= - { - { - {0,3,1}, - 3, - 0 - }, - { - {0,1,2}, - 3, - 0 - }, - { - {0,2,3}, - 3, - 0 - }, - { - {1,3,2}, - 3, - 0 - }, - }; + { + { + {0,3,1}, + 3, + 0 + }, + { + {0,1,2}, + 3, + 0 + }, + { + {0,2,3}, + 3, + 0 + }, + { + {1,3,2}, + 3, + 0 + }, + }; static Obj3D *obj; @@ -1839,14 +1953,16 @@ static void StarDemo(void) int f; if (!init) - { + { for(f=0;f<256;f++) + { col[f]=GetCol(255-f,255-f,255-f); + } star=Grab(sizeof(TYPE)*no); for(f=0;fmin_speed) + { speed-=speed_inc; } + } Cls(); for(f=0;fmin_speed) - { + { XPoint p; XPoint p2; Project(star[f].origin.x,star[f].origin.y,star[f].origin.z,&p); Project(star[f].origin.x,star[f].origin.y,star[f].origin.z+speed*speed,&p2); LineP(&p,&p2,col[star[f].col]); - } + } star[f].origin.z-=20*speed; if (star[f].col) - star[f].col--; - } + star[f].col--; } + } for(f=0;fmin_speed) + { speed-=speed_inc; } + } Cls(); for(f=0;fMODE_EDGE) - obj.mode=MODE_SOLID; + { + obj.mode=MODE_SOLID; + } bounce=time(NULL); - } } + } Cls(); for(f=0;fradius) + { p[f].origin.y=-radius; } + } switch(rot) - { + { case 0: Print(0,20,white,"Y rotation"); obj.y_rot+=10; @@ -2240,7 +2380,7 @@ static void Test3DDemo(void) break; default: break; - } + } Print(0,30,white,"Mode : %d Culling: %s",obj.mode,obj.cull ? "on":"off"); @@ -2249,7 +2389,7 @@ static void Test3DDemo(void) /* ---------------------------------------- COL SCAPE DEMO -*/ + */ static void ColScapeDemo(void) { static const int gap=200; @@ -2263,51 +2403,51 @@ static void ColScapeDemo(void) static const double y_base=100.0; static Point3D pset[8]= - { - {-50,-20,50}, - {50,-20,50}, - {50,-20,-50}, - {-50,-20,-50}, - - {50,20,50}, - {-50,20,50}, - {-50,20,-50}, - {50,20,-50}, - }; + { + {-50,-20,50}, + {50,-20,50}, + {50,-20,-50}, + {-50,-20,-50}, + + {50,20,50}, + {-50,20,50}, + {-50,20,-50}, + {50,20,-50}, + }; static Poly3D plist[6]= - { - { - {0,1,2,3}, - 4, - 0 - }, - { - {4,5,6,7}, - 4, - 0 - }, - { - {0,5,4,1}, - 4, - 0 - }, - { - {2,1,4,7}, - 4, - 0 - }, - { - {3,2,7,6}, - 4, - 0 - }, - { - {0,3,6,5}, - 4, - 0 - }, - }; + { + { + {0,1,2,3}, + 4, + 0 + }, + { + {4,5,6,7}, + 4, + 0 + }, + { + {0,5,4,1}, + 4, + 0 + }, + { + {2,1,4,7}, + 4, + 0 + }, + { + {3,2,7,6}, + 4, + 0 + }, + { + {0,3,6,5}, + 4, + 0 + }, + }; static Obj3D *obj; @@ -2321,7 +2461,7 @@ static void ColScapeDemo(void) int f,a; if (!init) - { + { int x,z; col[0]=white; @@ -2339,7 +2479,7 @@ static void ColScapeDemo(void) a=0; for(f=0;fy_base) - { + { amp=init_amp; ang=0; dist=1; @@ -2596,50 +2740,56 @@ static void ParticleDropDemo(void) drop.origin.x=RND2(max_x,min_x); drop.origin.z=RND2(max_z,min_z); drop.origin.y=drop_y; - } + } if (amp>0.0) - { + { int a; if (ang<0) + { ang+=3600; + } a=ang; for(f=0;fy_base) - { + { amp=init_amp; ang=0; dist=1; @@ -2785,50 +2937,58 @@ static void PolyDropDemo(void) drop.origin.x=RND2(max_x,min_x); drop.origin.z=RND2(max_z,min_z); drop.origin.y=drop_y; - } + } if (amp>0.0) - { + { int a; if (ang<0) + { ang+=3600; + } a=ang; for(f=0;fMODE_EDGE) - obj.mode=MODE_SOLID; + { + obj.mode=MODE_SOLID; + } bounce=time(NULL); - } } + } Cls(); if (view) - { + { obj.x_rot=MAX(2700,MIN(3599,2700+mouse_y*3)); obj.y_rot=(centre_x-mouse_x)*3; obj.y_rot=MAX(-900,MIN(obj.y_rot,900)); - } + } else - { + { drop.origin.y+=drop_speed*speed; speed+=0.1; if (drop.origin.y>y_base) - { + { int a; speed=1.0; @@ -3038,7 +3204,7 @@ static void CraterDemo(void) drop.origin.y=drop_y; for(f=0;f=gap) - { + { for(f=no-1;f>=no_x;f--) + { pt[f].y=pt[f-no_x].y; + } for(f=0;f=gap) - { + { for(f=no-1;f>=no_x;f--) + { pt[f].y=pt[f-no_x].y; + } for(f=0;f