Article Preview
Buy Now
COLUMN
Graphics Tips
All you wanted to know about graphics
Issue: 1.4 (February/March 2003)
Author: Thomas Reed
Author Bio: Thomas Reed has been programming as a hobbyist for more than 20 years, and fell in love with the Mac in 1984.
Article Description: No description available.
Article Length (in bytes): 9,838
Starting Page Number: 30
Article Number: 1415
Resource File(s):
1415.zip Updated: 2013-03-11 19:07:56
Related Link(s): None
Excerpt of article text...
Many kinds of applications can be built without understanding the details of graphics programming. However, most moderately serious programmers will eventually need to know more about graphics programming than just the DrawPicture command. This article will help you learn a variety of graphics-related tasks.
One of the most frequently asked questions about graphics is how to prevent unattractive flicker when drawing graphics. In Mac OS 9 and earlier, flicker occurs when two different things are drawn in the same space at nearly the same time. For example, if you call a Canvas' Refresh method, the Canvas will first be erased (by drawing the background over it), then its Paint event will trigger, drawing something else in that same space. To avoid flicker, you must draw into an intermediate Picture object. When that Picture contains exactly what you want displayed on the screen, you can draw it into the Canvas, overwriting the previous contents. This technique is called "buffered drawing."
In OS X, all drawing within a window is buffered automatically. This feature is convenient, especially if you're only supporting OS X; however, it can lead to unexpected problems. For example, if you try to draw a quick animation from within a tight loop (a simple loop not running within a thread), you'll find that only the final frame of the animation is displayed. This is because OS X does not draw the buffer to the screen until after you've exited the loop, thus losing all but the last frame. To prevent this, you need to force an update of the window. In REALbasic 4.5, simply call the window's UpdateNow method each time you want to draw a frame of the animation. This "flushes the buffer" (draws the buffer to the screen). In older versions of REALbasic, the following code can accomplish the task:
Sub FlushWindow(theWindow as Window)
#if TargetCarbon
Declare Function GetWindowPort Lib "CarbonLib" (window as WindowPtr) as Integer
...End of Excerpt. Please purchase the magazine to read the full article.