Source Code (Use browser search to find items of interest.)

Class Index

kabalone'Ball (./kdegames/kabalone/Ball.h:29)

class Ball {
  
 public:
  Ball(const QColor& c, double a = 0.0, int t=TEX_RIPPLE );
  ~Ball();

  QPixmap* pixmap();

  double angle() { return an; }
  QColor ballColor() { return bColor; }
  void setSpecials(double z, double f, double l)
    { zoom = z, flip=f, limit=l; }

  static int w() { return sizeX; }
  static int h() { return sizeY; }
  static void setSize(int x,int y);
  static void setLight(int x=5, int y=3, int z=10, 
		       const QColor& c = QColor(200,230,255) );
  static void setTexture(double c=13., double d=.2);

 private:

  void render();
  static void invalidate();

  //static QImage back;
  static int sizeX, sizeY;
  static double lightX, lightY, lightZ;
  static QColor lightColor;
  static double rippleCount, rippleDepth;

  QPixmap pm;
  QColor bColor;
  double an, sina, cosa;
  double zoom, flip, limit;
  int tex;

  Ball *next;
  static Ball* first;
};



kabalone'Ball::setSize() (./kdegames/kabalone/Ball.cpp:19)

void Ball::setSize(int x, int y)
{
  sizeX = x;
  sizeY = y;
  
  invalidate();
}


kabalone'Ball::invalidate() (./kdegames/kabalone/Ball.cpp:27)

void Ball::invalidate()
{
  Ball *b;

  /* invalidate all Balls... */
  for(b=first;b!=0;b=b->next)
    b->pm.resize(0,0);
}


kabalone'Ball::setLight() (./kdegames/kabalone/Ball.cpp:36)

void Ball::setLight(int x, int y, int z, const QColor& c)
{
  double len = sqrt(x*x + y*y + z*z);
  
  lightX = x/len;
  lightY = y/len;
  lightZ = z/len;

  lightColor = c;

  invalidate();
}



kabalone'Ball::setTexture() (./kdegames/kabalone/Ball.cpp:50)

void Ball::setTexture(double c, double d)
{
  rippleCount = c;
  rippleDepth = d;

  invalidate();
}
  
  


kabalone'Ball::Ball() (./kdegames/kabalone/Ball.cpp:60)

Ball::Ball(const QColor& c, double a, int t)
{
  if (first ==0) {
    sizeX = sizeY = -1;
    setLight();
    setTexture(7,.3);
  }

  bColor = c;
  an = a;
  sina = sin(a), cosa = cos(a);

  zoom= 1.05, flip = 2.0, limit = 0;
  tex = TEX_RIPPLE;

  next = first;
  first = this;
}


kabalone'Ball::~Ball() (./kdegames/kabalone/Ball.cpp:79)

Ball::~Ball()
{
  Ball* b;

  if (first == this)
    first = next;
  else {
    for(b = first; b!=0; b=b->next)
      if (b->next == this) break;
    if (b!=0)
      b->next = next;
  }
}
    

kabalone'Ball::pixmap() (./kdegames/kabalone/Ball.cpp:93)

QPixmap* Ball::pixmap()
{
  if (pm.isNull() && sizeX>0 && sizeY>0)
    render();
  return ±
}


kabalone'Ball::render() (./kdegames/kabalone/Ball.cpp:100)

void Ball::render()
{
  int x,y;
  double xx,yy,zz, ll,lll, red,green,blue, tr;

  if (sizeX==0 || sizeY==0) 
    return;

  QImage image(sizeX,sizeY,32);
  image.fill(0);

  double vv=2./(sizeX+sizeY);

  /* Go through all pixels, mapping x/y to (-1..1,-1..1) */
  for(y=0;y<sizeY;y++) {
    yy = (2.*y-sizeY)/(sizeY-2) *zoom;
    for(x=0;x<sizeX;x++) {
      xx = (2.*x-sizeX)/(sizeX-2) *zoom;

      /* Change only if inside the ball */
      zz = 1 - (xx*xx + yy*yy);
      
      if (zz>flip) zz=2*flip-zz;
      else {
	zz -= limit;
      }

      if (zz>-vv) {
	zz = (zz<0) ? 0 : sqrt(zz);

	/* ll: light intensity at this point */ 
	ll = xx*lightX + yy*lightY + zz*lightZ;

	/* some face modification */
	double mapx = xx*(2-zz);
	double mapy = yy*(2-zz);
	double rmapx =  cosa*mapx + sina*mapy; /* rotate */
	double rmapy = -sina*mapx + cosa*mapy;	

	if (tex>0)
	  ll += rippleDepth* cos(rippleCount*rmapx)*cos(rippleCount*rmapy);

	ll = (ll<0.01) ? 0.0 : (ll>.99) ? 1.0 : ll;
	lll = ll*ll;

	//	printf("x %f, y %f, z %f : ll %f lll %f\n", xx,yy,zz,ll,lll);


	/* mix ball+light */
	red   = lll * lightColor.red() +   (1-lll) * bColor.red();
	green = lll * lightColor.green() + (1-lll) * bColor.green();
	blue  = lll * lightColor.blue() +  (1-lll) * bColor.blue();

	/* lightness */
	red   = .2 * bColor.red()   + .8 * ll * red;
	green = .2 * bColor.green() + .8 * ll * green;
	blue  = .2 * bColor.blue()  + .8 * ll * blue;

	image.setPixel(x,y, qRgb( (int)red,  (int)green, (int)blue ));
      }
    }
  }
  const QImage iMask = image.createHeuristicMask();
  QBitmap bMask;
  bMask = iMask;
  pm.convertFromImage( image, 0 );
  pm.setMask(bMask);
}


/* Class BallAnimation */