Back to index

kdeartwork  4.3.2
Classes | Defines | Functions | Variables
swarm.cpp File Reference
#include <qslider.h>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <kglobal.h>
#include <kconfig.h>
#include <krandomsequence.h>
#include "xlock.h"
#include <qcheckbox.h>
#include <qlabel.h>
#include <qcolor.h>
#include <qlayout.h>
#include <QX11Info>
#include <klocale.h>
#include <kmessagebox.h>
#include "swarm.h"
#include "swarm.moc"
#include "helpers.h"

Go to the source code of this file.

Classes

struct  swarmstruct

Defines

#define MAXSPEED   100
#define MINSPEED   0
#define DEFSPEED   50
#define MAXBATCH   200
#define MINBATCH   0
#define DEFBATCH   20
#define TIMES   4 /* number of time positions recorded */
#define BEEACC   2 /* acceleration of bees */
#define WASPACC   5 /* maximum acceleration of wasp */
#define BEEVEL   12 /* maximum bee velocity */
#define WASPVEL   10 /* maximum wasp velocity */
#define X(t, b)   (sp->x[(t)*sp->beecount+(b)])
#define Y(t, b)   (sp->y[(t)*sp->beecount+(b)])
#define balance_rand(v)   (rnd.getLong(v)-((v)/2)) /* random number around 0 */

Functions

void initswarm (Window win, KRandomSequence &rnd)
void drawswarm (Window win, KRandomSequence &rnd)
void startScreenSaver (Drawable d)
void stopScreenSaver ()
int setupScreenSaver ()

Variables

static swarmstruct swarms [MAXSCREENS]
static kSwarmSaversaver = NULL

Class Documentation

struct swarmstruct

Definition at line 47 of file swarm.cpp.

Class Members
int beecount
int border
int height
XSegment old_segs
int pix
XSegment segs
int width
short wx
short wxv
short wy
short wyv
short x
short xv
short y
short yv

Define Documentation

#define balance_rand (   v)    (rnd.getLong(v)-((v)/2)) /* random number around 0 */

Definition at line 43 of file swarm.cpp.

#define BEEACC   2 /* acceleration of bees */

Definition at line 35 of file swarm.cpp.

#define BEEVEL   12 /* maximum bee velocity */

Definition at line 37 of file swarm.cpp.

#define DEFBATCH   20

Definition at line 22 of file swarm.cpp.

#define DEFSPEED   50

Definition at line 19 of file swarm.cpp.

#define MAXBATCH   200

Definition at line 20 of file swarm.cpp.

#define MAXSPEED   100

Definition at line 17 of file swarm.cpp.

#define MINBATCH   0

Definition at line 21 of file swarm.cpp.

#define MINSPEED   0

Definition at line 18 of file swarm.cpp.

#define TIMES   4 /* number of time positions recorded */

Definition at line 34 of file swarm.cpp.

#define WASPACC   5 /* maximum acceleration of wasp */

Definition at line 36 of file swarm.cpp.

#define WASPVEL   10 /* maximum wasp velocity */

Definition at line 38 of file swarm.cpp.

#define X (   t,
 
)    (sp->x[(t)*sp->beecount+(b)])

Definition at line 41 of file swarm.cpp.

#define Y (   t,
 
)    (sp->y[(t)*sp->beecount+(b)])

Definition at line 42 of file swarm.cpp.


Function Documentation

void drawswarm ( Window  win,
KRandomSequence &  rnd 
)

Definition at line 118 of file swarm.cpp.

{
       swarmstruct *sp = &swarms[screen];
       int         b;

       /* <=- Wasp -=> */
       /* Age the arrays. */
       sp->wx[2] = sp->wx[1];
       sp->wx[1] = sp->wx[0];
       sp->wy[2] = sp->wy[1];
       sp->wy[1] = sp->wy[0];
       /* Accelerate */
       sp->wxv += balance_rand(WASPACC);
       sp->wyv += balance_rand(WASPACC);

       /* Speed Limit Checks */
       if (sp->wxv > WASPVEL)
              sp->wxv = WASPVEL;
       if (sp->wxv < -WASPVEL)
              sp->wxv = -WASPVEL;
       if (sp->wyv > WASPVEL)
              sp->wyv = WASPVEL;
       if (sp->wyv < -WASPVEL)
              sp->wyv = -WASPVEL;

       /* Move */
       sp->wx[0] = sp->wx[1] + sp->wxv;
       sp->wy[0] = sp->wy[1] + sp->wyv;

       /* Bounce Checks */
       if ((sp->wx[0] < sp->border) || (sp->wx[0] > sp->width - sp->border - 1)) {
              sp->wxv = -sp->wxv;
              sp->wx[0] += sp->wxv;
       }
       if ((sp->wy[0] < sp->border) || (sp->wy[0] > sp->height - sp->border - 1)) {
              sp->wyv = -sp->wyv;
              sp->wy[0] += sp->wyv;
       }
       /* Don't let things settle down. */
       sp->xv[rnd.getLong(sp->beecount)] += balance_rand(3);
       sp->yv[rnd.getLong(sp->beecount)] += balance_rand(3);

       /* <=- Bees -=> */
       for (b = 0; b < sp->beecount; b++) {
              int         distance, dx, dy;

              /* Age the arrays. */
              X(2, b) = X(1, b);
              X(1, b) = X(0, b);
              Y(2, b) = Y(1, b);
              Y(1, b) = Y(0, b);

              /* Accelerate */
              dx = sp->wx[1] - X(1, b);
              dy = sp->wy[1] - Y(1, b);
              distance = abs(dx) + abs(dy);      /* approximation */
              if (distance == 0)
                     distance = 1;
              sp->xv[b] += (dx * BEEACC) / distance;
              sp->yv[b] += (dy * BEEACC) / distance;

              /* Speed Limit Checks */
              if (sp->xv[b] > BEEVEL)
                     sp->xv[b] = BEEVEL;
              if (sp->xv[b] < -BEEVEL)
                     sp->xv[b] = -BEEVEL;
              if (sp->yv[b] > BEEVEL)
                     sp->yv[b] = BEEVEL;
              if (sp->yv[b] < -BEEVEL)
                     sp->yv[b] = -BEEVEL;

              /* Move */
              X(0, b) = X(1, b) + sp->xv[b];
              Y(0, b) = Y(1, b) + sp->yv[b];

              /* Fill the segment lists. */
              sp->segs[b].x1 = X(0, b);
              sp->segs[b].y1 = Y(0, b);
              sp->segs[b].x2 = X(1, b);
              sp->segs[b].y2 = Y(1, b);
              sp->old_segs[b].x1 = X(1, b);
              sp->old_segs[b].y1 = Y(1, b);
              sp->old_segs[b].x2 = X(2, b);
              sp->old_segs[b].y2 = Y(2, b);
       }

       XSetForeground(dsp, Scr[screen].gc, BlackPixel(dsp, screen));
       XDrawLine(dsp, win, Scr[screen].gc,
                sp->wx[1], sp->wy[1], sp->wx[2], sp->wy[2]);
       XDrawSegments(dsp, win, Scr[screen].gc, sp->old_segs, sp->beecount);

       XSetForeground(dsp, Scr[screen].gc, WhitePixel(dsp, screen));
       XDrawLine(dsp, win, Scr[screen].gc,
                sp->wx[0], sp->wy[0], sp->wx[1], sp->wy[1]);
       if (!mono && Scr[screen].npixels > 2) {
              XSetForeground(dsp, Scr[screen].gc, Scr[screen].pixels[sp->pix]);
              if (++sp->pix >= Scr[screen].npixels)
                     sp->pix = 0;
       }
       XDrawSegments(dsp, win, Scr[screen].gc, sp->segs, sp->beecount);
}

Here is the caller graph for this function:

void initswarm ( Window  win,
KRandomSequence &  rnd 
)

Definition at line 68 of file swarm.cpp.

{
       swarmstruct *sp = &swarms[screen];
       int         b;
       XWindowAttributes xwa;

       sp->beecount = batchcount;
       (void) XGetWindowAttributes(dsp, win, &xwa);
       sp->width = xwa.width;
       sp->height = xwa.height;

       sp->border = (sp->width + sp->height) / 50;

       /* Clear the background. */
       XSetForeground(dsp, Scr[screen].gc, BlackPixel(dsp, screen));
       XFillRectangle(dsp, win, Scr[screen].gc, 0, 0, sp->width, sp->height);

       /*  Now static data structures. epirker */
       //if (!sp->segs) {
       //sp->segs = (XSegment *) malloc(sizeof (XSegment) * sp->beecount);
       //sp->old_segs = (XSegment *) malloc(sizeof (XSegment) * sp->beecount);
       //sp->x = (short *) malloc(sizeof (short) * sp->beecount * TIMES);
       //sp->y = (short *) malloc(sizeof (short) * sp->beecount * TIMES);
       //sp->xv = (short *) malloc(sizeof (short) * sp->beecount);
       //sp->yv = (short *) malloc(sizeof (short) * sp->beecount);
       //}
       /* Initialize point positions, velocities, etc. */

       /* wasp */
       sp->wx[0] = sp->border + rnd.getLong(sp->width - 2 * sp->border);
       sp->wy[0] = sp->border + rnd.getLong(sp->height - 2 * sp->border);
       sp->wx[1] = sp->wx[0];
       sp->wy[1] = sp->wy[0];
       sp->wxv = 0;
       sp->wyv = 0;

       /* bees */
       for (b = 0; b < sp->beecount; b++) {
              X(0, b) = rnd.getLong(sp->width);
              X(1, b) = X(0, b);
              Y(0, b) = rnd.getLong(sp->height);
              Y(1, b) = Y(0, b);
              sp->xv[b] = balance_rand(7);
              sp->yv[b] = balance_rand(7);
       }
}

Here is the caller graph for this function:

Definition at line 252 of file swarm.cpp.

{
       kSwarmSetup dlg;

       return dlg.exec();
}

Here is the caller graph for this function:

void startScreenSaver ( Drawable  d)

Definition at line 238 of file swarm.cpp.

{
       if ( saver )
              return;
       saver = new kSwarmSaver( d );
}

Here is the caller graph for this function:

void stopScreenSaver ( )

Definition at line 245 of file swarm.cpp.

{
       if ( saver )
              delete saver;
       saver = NULL;
}

Here is the caller graph for this function:


Variable Documentation

kSwarmSaver* saver = NULL [static]

Definition at line 236 of file swarm.cpp.

Definition at line 65 of file swarm.cpp.