[comp.sys.mac.programmer] How can I change look up table without getting glitches?

hilary@snll-arpagw.UUCP (Hilary Jones) (08/01/90)

My application needs to have complete control over the color table, so I use 
RestoreEntries to load it.  In the good old days, I had the routine load all 256
ColorTable entries at one time, and everything worked fine -- except, 
whenever I loaded the table, I got an annoying glitch on the screen.  After 
some experimentation, I found I could make the problem go away by never 
loading more than 64 entries at a time.  Presumably the idea is to have the 
load done before the vertical retrace is complete.

However I now have another problem.  Every so often, the load isn't done 
accurately.  When that happens, most levels _are_ loaded correctly but one 
entry (usually the first one) gets set to black.  That means I can't see the 
menus, so it's pretty hard to overlook the error!  This happens quite
unpredictably.  I can call the routine repeatedly without doing anything 
significant (like allocating memory) between calls, and it will fail perhaps 
once out of every ten times.

I have tried debugging with TC's debugger, but it's pretty hard to prove that 
the device table is loaded wrong when the screen is showing black on black!  
I've tried using TMON, but that's too tedious.  At least it doesn't reveal 
problems when I scramble the heap or look for bad arguments to toolbox 
routines.  I've removed all my INIT's and tried running  under MonoFinder, 
but the problem remains.  I've tried waiting for the vertical blanking 
interval, since it seems like it has to be a timing problem of some sort, 
but that didn't help.

So, I need help!  This is all being done on a Mac II using System 6.0.3 and
a standard Apple Video Card.  Here's a rough idea of what the code does:

   size = sizeof(ColorTable) + 63*sizeof(ColorSpec);   /* 64-level table */
   shortLut = (CTabHandle) NewHandle(size);
   HLock(shortLut);
   (**shortLut).ctSize = 63;
   (**shortLut).ctFlags = 0;

   /* Build new lut and load it in 64-level chunks */
   for (j=0;j<256;j+=64) {
      reqList[0] = 64;
      for (i=1;i<=64;i++) {
         reqList[i]=i-1+j;
         (**shortLut).ctTable[i-1].value = i-1;
         (**shortLut).ctTable[i-1].rgb = ....; /* e.g., Apple's defaults */
      }
      RestoreEntries(shortLut,0,&reqList);
   }

What am I doing wrong?  Or, to rephrase the question, how do I load the lookup
table without getting glitches?  I don't want to use the Palette Manager if it 
forces all of my windows to try to match the colors in the top window.  That
would make the background windows update too slowly.