Algorithmic Solutions > LEDA > LEDA Guide > Windows and Panels > Buffering > Examples

Examples Buffering

Simple Example

The following program shows how to use buffering to construct a pixrect copy of a drawing. The program uses an auxiliary window W1 in buffering mode to create a pixrect picture that is used as an icon for the primary window W.

On the right there is a screenshot of the program. Clicking on the picture shows the window in original size.

Picture of Convex Hull
#include <LEDA/graphics/window.h>

using namespace leda;

int main()
{
  window W1(100,100);
  W1.init(-1,+1,-1);	
  W1.open();

  W1.start_buffering();
  W1.draw_disc(0,0,0.8,blue);W1.draw_circle(0,0,0.8,black);
  W1.draw_disc(0,0,0.6,yellow);W1.draw_circle(0,0,0.6,black);
  W1.draw_disc(0,0,0.4,green);W1.draw_circle(0,0,0.4,black);
  W1.draw_disc(0,0,0.2,red);W1.draw_circle(0,0,0.2,black);
  char* pr;
  W1.stop_buffering(pr);

  window W(400,400);
  W.set_icon_pixrect(pr);
  W.display(window::center,window::center);
  point p;
  while (W>>p) W.put_pixrect(p,pr);
  W.del_pixrect(pr);

  return 0;
}	      

Example "Bouncing LEDA"

The following program uses buffering to move a LEDA pixrect ball smoothly across the window and to let it bounce at the window border lines.

On the right there is a screenshot of the program. Clicking on the picture shows the window in original size.

Picture of Convex Hull
#include <LEDA/graphics/window.h>
#include <LEDA/graphics/pixmaps/leda_icon.xpm>

using namespace leda;

void move_ball(window& W, circle& ball, double& dx, double& dy, char* prect)
{
  ball=ball.translate(dx,dy);
  point c=ball.center();
  
  double r=ball.radius();
  if (c.xcoord()-r<W.xmin() || c.xcoord()+r>W.xmax()) dx=-dx;
  if (c.ycoord()-r<W.ymin() || c.ycoord()+r>W.ymax()) dy=-dy;

  W.clear();
		
  W.set_clip_ellipse(c.xcoord(),c.ycoord(),r,r);
  W.center_pixrect(c.xcoord(), c.ycoord(),prect);
  W.reset_clipping();
		
  W.draw_circle(ball,black);

  for (int i=0;i<1000000;i++) ;
}

int main()
{
  window W(300,300,"bouncing leda");
  W.set_bg_color(grey1);
  W.display(window::center,window::center);

  circle ball(50,50,16);
  double dx=W.pix_to_real(2);
  double dy=W.pix_to_real(1);
  char* leda=W.create_pixrect(leda_icon);
	
  W.start_buffering();
  while (W.get_mouse()!=MOUSE_BUTTON(3)) {
    move_ball(W,ball,dx,dy,leda); 
    W.flush_buffer();
  }
  W.stop_buffering();

  W.del_pixrect(leda);

  W.screenshot("bouncing_leda");

  return 0;
}

See also:

Buffering

Pixrect (Pixmaps)

Windows and Panels

Mouse Input

Drawing Operations

Input and Output Operators


Manual Pages:

Manual Page Windows




Please send any suggestions, comments or questions to leda@algorithmic-solutions.com
© Copyright 2001-2003, Algorithmic Solutions Software GmbH