home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / sys / mac / programm / 15028 < prev    next >
Encoding:
Text File  |  1992-09-07  |  3.7 KB  |  114 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!cs.utexas.edu!tamsun.tamu.edu!bpb9204
  3. From: bpb9204@tamsun.tamu.edu (Brent)
  4. Subject: Offscreen bitmap problem
  5. Message-ID: <1992Sep5.045203.25253@tamsun.tamu.edu>
  6. Organization: Texas A&M Univ., Inc.
  7. Distribution: usa
  8. Date: Sat, 5 Sep 1992 04:52:03 GMT
  9. Lines: 103
  10.  
  11.  
  12. Hello!       (I'll warn you now, this post is long)
  13.  
  14. I am writing a small fractal graphics application that generates basic B/W
  15. images.  Most of the program is done and I am now working on updating the
  16. image after it has been uncovered.  For starters, my image window is 208x208
  17. and the pixels are set as I calculate each.  When this image window gets
  18. covered and then uncovered, I need to update the newly visible area quickly.
  19. Since fractals take a while to generate, I decided on using an offscreen
  20. bitmap and draw to both bitmaps simultaneously.  Then, do a CopyBits from 
  21. the offscreen to the image window for an update.  Does this sound reasonable?
  22.  
  23. I looked up the info concerning offscreen bitmaps in the UMPG -- Glenn Austin
  24. had a note in there (the only one) about using offscreen bitmaps.  I
  25. followed his instructions, but I can't get it to work.  I am enclosing some
  26. of my code to show what is going on; please bear with me.
  27.  
  28. -----------------
  29. Global variables:
  30.  
  31. WindowPtr gMainWindow -- the main image window (208x208)
  32. BitMap    gOffBM      -- offscreen bitmap
  33. GrafPort  gOffGP      -- offscreeen port
  34.  
  35.  
  36. SetupOffscreen() is called at appl. startup, as part of the initialization
  37. process. Error detection has been removed from this code for length.
  38.  
  39. void SetupOffscreen(void) 
  40. {
  41.     gOffBM.rowBytes = 26;    // 208/8
  42.     SetRect( &gOffBM.bounds, 0,0, 207,207);
  43.     gOffBM.baseAddr = NewPtrClear( 26* 208); // 208 rows at 26 bytes each.
  44.     if (gOffBM.baseAddr != NULL)
  45.     {
  46.         OpenPort( &gOffGP);
  47.         SetPortBits( &gOffBM);
  48.         gOffGP.portRect = gOffBM.bounds;
  49.         RectRgn( gOffGP.visRgn, &gOffBM.bounds);
  50.         // SetPort( &gOffGP);
  51.     }
  52. }
  53.  
  54.  
  55. The next fragment is from my loops to calculate the fractal.  It scans from
  56. top to bottom, left to right, plotting each pixel as it calculates it.
  57. This code draws the image correctly to gMainWindow: 
  58.  
  59.  
  60. if ( (dabs(z.r) < 10.0) || (dabs(z.i) < 10.0) )
  61. {
  62.     SetPort(gMainWindow);    // plot the main window's image
  63.     PenPat( black);
  64.     MoveTo(x, y);
  65.     Line(0,0);        // plot the pixel
  66.     
  67.     SetPort( &gOffGP);    // plot the offscreen bitmap's image
  68.     PenPat( black);
  69.     MoveTo(x, y);
  70.     Line(0,0);        // plot the pixel
  71. }
  72. else
  73. {
  74.     // do the same as above, but PenPat(white) for both ports.
  75. }
  76.  
  77.  
  78. Say that the image has been drawn and some window is on top of gMainWindow.
  79. When gMainWindow is uncovered and I get the update event, here is the
  80. code that runs for the update:
  81.  
  82.  
  83. [...]
  84. else if (gMainWindow == (WindowPtr)theEvent->message)
  85. {
  86.     Print("Main update");
  87.     GetPort(&savedPort);
  88.     BeginUpdate(gMainWindow);
  89.     SetPort(gMainWindow);
  90.     EraseRect(&gMainWindow->portRect);
  91.     CopyBits( &gOffBM,        &gMainWindow->portBits,
  92.                   &gOffBM.bounds, &gMainWindow->portBits.bounds,
  93.                   srcCopy,        NULL);
  94.     EndUpdate(gMainWindow);
  95.     SetPort(savedPort);
  96. }
  97. ------------------ end of code parts.
  98.  
  99. What happens, you might ask?  Well, when I get the update event, this last
  100. section of code executes and the uncovered area is painted white.  This
  101. implies that the offscreen bitmap is not getting set, but I think I have
  102. followed directions OK.  (Inside mac is vague about offscreen ports).
  103.  
  104. All the code I have here is exactly what is in my program (pasted from it).
  105. All error checks find things are happening OK, also.
  106.  
  107. If you can find any problems with the above code, please let me know.  I'm
  108. new to this offscreen bitmap stuff and a friend of mine has my mac program-
  109. ming books.
  110.  
  111. Thanks,
  112. -Brent
  113. bpb9204@tamsun.tamu.edu
  114.