Home |
Obtaining a Pointer to PegPresentationManager Determining the Position of an Object Overriding the Message() Method Drawing to Memory |
Drawing to MemoryAn alternative to drawing directly to the screen is to draw to an offscreen bitmap. Such a bitmap can be displayed at any location by calling the PegScreen Bitmap() member function. This is the preferred method of displaying flicker-free animation, and can be used for many other purposes as well. Drawing to memory works almost exactly like on-screen drawing. PegScreen member functions are used to draw to an offscreen bitmap. Offscreen drawing is more of a drawing mode in which all screen output is redirected to a bitmap. Offscreen bitmaps must be created first using PegScreen member function CreateBitmap(). Then, the alternate form of the BeginDraw() function that accepts a pointer to the bitmap is called to start drawing. When drawing is done, the alternate form of the EndDraw() function must be called to put the PegScreen driver back to normal drawing mode. While the PegScreen driver is in the offscreen drawing mode, all of the normal PegScreen drawing functions draw into the bitmap. The following example is a code fragment demonstrating offscreen drawing. This example is also provided in the Gauge demo program. It draws a blank gauge bitmap offscreen, drawing the needle or position indicator on top of the background bitmap, and then copying the offscreen bitmap to the visible screen. The resulting effect is that the gauge updates smoothly without any noticeable flicker. void Gauge::Draw(void) { if (!mpBitmap) // first time? { // Create the bitmap, and draw into it: mpBitmap = Screen()->CreateBitmap(gbDialBitmap.wWidth, gbDialBitmap.wHeight); DrawToBitmap(); } // now just copy the bitmap to the screen: BeginDraw(); PegPoint Put; Put.x = mReal.wLeft; Put.y = mReal.wTop; Bitmap(Put, mpBitmap); EndDraw(); } void Gauge::DrawToBitmap(void) { PegPoint Put; PEGINT x1, y1, x2, y2; PEGINT iRadius = (gbDialBitmap.wWidth / 2) - 8; // Open the bitmap for drawing: Screen()->BeginDraw(this, mpBitmap); // Note1 Put.x = Put.y = 0; // copy the background bitmap into memory bitmap: Bitmap(Put, &gbDialBitmap); // Note2 // find the center: x1 = mReal.Width() / 2; y1 = mReal.Height() / 2; // find the end: double angle = ((4.0 * PI) / 5.0) + (((double) miCurrent / 100.0) * PI); x2 = x1 + cos(angle) * iRadius; y2 = y1 + sin(angle) * iRadius; // draw the indicator line: PegColor LineColor(RED, RED, CF_NONE); // Note3 Line(x1, y1, x2, y2, LineColor, 2); // Close the bitmap for drawing: Screen()->EndDraw(mpBitmap); // Note4 } The real work of drawing the gauge is done in the class member function DrawToBitmap. DrawToBitmap draws the background of the gauge and the gauge indicator line into an offscreen bitmap, which can then be copied to the screen at any time.
|