Home |
Obtaining a Pointer to PegPresentationManager Determining the Position of an Object Overriding the Message() Method Drawing to the Screen |
Drawing to the ScreenDrawing on the screen is performed through methods of class PegScreen. This is most often done from within an overridden Draw() function. Drawing always starts with a call to BeginDraw() and completes by calling EndDraw(). When RTPEG-32 recognizes that an object needs to be re-drawn (i.e. the object was just Add()-ed, or the object has been moved), it re-draws the object by calling the object's Draw() function. Drawing can also be performed by functions other than Draw(). Such functions must be members of a PegThing derived class, or at least have access to a PegThing object, since all of the PegScreen drawing functions require as a parameter a pointer to the PegThing object calling the drawing function. PegScreen requires this pointer to insure that an object is not allowed to draw outside of the area it owns on the screen. PegScreen only allows drawing to occur to areas of the screen which have been invalidated. Areas of the screen are invalidated by calling the Invalidate() function, which is a member of PegScreen but also provided in inline form as a member of PegThing. Under most circumstances the screen invalidation is handled automatically by RTPEG-32 as the user moves things around on the screen, or as the program adds and removes visible objects. If all drawing is done from with an overridden Draw() function, there is no need to invalidate the screen, since the Draw() function is called specifically because an area of the screen has been invalidated. If you need to draw on the screen outside at random times, or for example based on a periodic timer, you must invalidate the area you are going to draw to before you start drawing. If you want to be allowed to draw anywhere within the client area of your object, you can simply call the Invalidate() function with no parameters, which invalidates the area of the screen corresponding to an object's client area. You can also calculate and specify a more limiting rectangle to clip your drawing, and pass that rectangle to the Invalidate() function. No matter how large the invalidated rectangle on the screen, you are never allowed to draw outside of an object's borders. The following function is an example function that could be used to draw a series of lines to the screen at any time. This example will paint the entire client area of the object black, and then fill the client area of the object with RED horizontal lines, 1 pixel wide, spaced 4 pixels apart. void MyObject::DrawLines(void) { PegColor LineColor(RED, BLACK, CF_FILL); PEGINT yPos = mClient.wTop; Invalidate(); // invalidate my client area BeginDraw(); // prepare for drawing Rectangle(mClient, Color, 0); // fill with black while (yPos <= mClient.wBottom) { // draw red lines: Line(mClient.wLeft, yPos, mClient.wRight, yPos, Color); YPos += 4; } EndDraw(); // Done } Overriding the Message() Method
|