TweetFollow Us on Twitter

Sep 95 Challenge
Volume Number:11
Issue Number:9
Column Tag:Programmer’s Challenge

Programmer’s Challenge

By Bob Boonstra, Westford, Massachusetts

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

Reversible Scrambling Algorithm

According to tradition, September is Assembly Language Challenge month here at MacTech, and we continue that tradition this month. Your challenge is to do some simple arithmetic - raising a number to a power, and taking the remainder of the result modulo another number. Simple, right? To make things interesting, though, the numbers are going to be a little larger than you are used to dealing with. Hundreds of decimal digits long, in fact. “Why,” you may ask? We’ll get into that in a minute, but there are a couple of hints in the title of this month’s challenge.

The data structure to be used for the large numbers in this Challenge, and the prototype for the code you should write are:

typedef struct BigNum {
 short numDig;   /* the number of bytes in the BigNum */
 unsigned char *dig; /* dig[0] is the most significant byte */
    /* dig[numDig-1] is least significant */
} BigNum;

void PowerAndRemainder( 
 BigNum *msg,    
 BigNum *exp,    /* calculate msg to the exp power, */
 BigNum *n, /* take the remainder modulo n */
 BigNum *res/* and store the result in res */
);

For example, the value 1048573 (0xFFFFD) would be provided to you in a BigNum b with the values b.numDig=3, b.dig[0] = 0x0F, b.dig[1]=0xFF, and b.dig[2]=0xFD. The first three arguments will be provided as input when PowerAndRemainder is called; you are to generate both elements of the BigNum struct for the res argument. The storage for all of the BigNums in the call to PowerAndRemainder will be allocated by the caller. All BigNums will be positive integers, and none of the BigNums will be larger than 128 bytes in length (i.e., b.numDig will be no larger than 128). There is no restriction on the amount of memory you may use (within reason).

Those of you with some number theory in your background may recognize what a function like this might be used for. If the modulus n is the product of two large primes p and q, one can find values e and d for the exponent with the property that they are inverses of one another, but that neither can be easily derived from the other, provided prime numbers p and q are not divulged. If you calculate PowerAndRemainder(msg,e,n,c), and I then calculate PowerAndRemainder(c,d,n,x), then the result x turns out to be identical to the original value msg if e and d are relatively prime to (p-1)*(q-1). Now what do you suppose such a function might be useful for?

Your solution may use any combination of ANSI C and/or 68K assembly language, along with your choice of either the THINK C or MetroWerks C 68K compilers. I considered making this a PowerPC challenge, but I wasn’t confident that enough people are proficient with PPC assembly just yet - perhaps next September. In the meantime, you can look forward to a native PPC challenge next month.

If you are interested in some sample values to test your code, send me email and I’ll provide some.

Challenge Deadline

Several people wrote to point out that the deadline for submitting Challenge solutions was missing from the Rules box during July and August. Unfortunately, when the rules were revised to accommodate multiple compilers and target instruction sets, the deadline was inadvertently omitted. The Challenge deadline remains the 10th of the month printed on the cover of the magazine. I received several submissions for the Chess challenge after the deadline (and after the article was submitted for publication). Because of the problem with the deadline, I would have awarded points to any fast and correct entries, but all of the late entries had problems with correctness so no additional points were awarded.

Two Months Ago Winner

Of the nine entries to the Sprite Blitz challenge, seven of them worked correctly. Congratulations to Xan Gregg (Durham, NC) for having the fastest solution, some 30% faster than the second place entry, submitted by John Nevard. Despite the variation in run time performance, there were a number of clever and creative solutions among the top entries.

Here are the times and code sizes for the entries that worked correctly. Numbers in parens after a person’s name indicate that person’s cumulative point total for all previous Challenges, not including this one.

Name time (68K)

Xan Gregg (31) 908

John Nevard 1300

Bill Karsh (71) 3363

Jim Bumgardner (4) 3495

Jeremy Vineyard (40) 5789

Norman Basham 10164

Steve Israelson 75846

Like most of the top entries, Xan composed his screen updates offscreen. Xan uses one offscreen GWorld to hold the background and another to prepare the next animation frame. One clever trick is that the offscreen image is large enough to contain all of a sprite that overlaps a window boundary, so that clipping need only be done when updating the window. Drawing is done directly to the screen, taking advantage of alignment conditions guaranteed to hold by the problem statement. Xan does all of his copying to the screen using unrolled loops, avoiding the overhead incurred when using CopyBits or CopyMask for small copies. When reading the code, take note of the switch statement in the COPY4 macro that copies the icon based on the value of the mask, and of the longword copies in the FastCopyChunk routine.

Bill Karsh pointed out in his entry that the relative performance of CopyBits and CopyMask varies between his 68K machine and his PPC 7100, with CopyBits being faster on the former machine and CopyMask being faster on the latter. I didn’t have time to measure native performance on the PowerPC, but there was a 15% difference between the two versions in my 68K tests. Of course, as Xan’s solution shows, avoiding both can have its advantages also.

Does Performance Matter?

I’ve received some email suggesting that the emphasis on performance in this column ought to be replaced by emphases on other things, like code portability, readability, reliability, encapsulation, or object orientation. The argument is that improvements in hardware performance make efficiency less important than it has been in the past. This is certainly a valid point of view, and there is no question that processor improvements have enabled us to sacrifice some machine cycles to achieve objectives other than performance. However, I contend that the performance of several popular personal computer applications demonstrates that software developers are capable of adding enough functionality (or generating poor enough code) to degrade performance to an unacceptable level, despite hardware advances. In my opinion, this will always be so. Certainly the techniques demonstrated in this column should not be used in all software, but they have their place in time-critical areas, and it is worth devoting more attention to efficiency than we typically do. Besides, squeezing instructions out of code is great fun! But if you are interested in seeing a column that focuses on something besides efficiency, drop me a note.

Top 20 Contestants of All Time

Here are the Top 20 Contestants for the Programmer’s Challenges to date. The numbers below include points awarded for this month’s entrants. (Note: ties are listed alphabetically by last name - there are more than 20 people listed this month because of ties.)

1. [Name deleted] 176

2. Karsh, Bill 78

3. Munter, Ernst 70

4. Stenger, Allen 65

5. Larsson, Gustav 60

6. Gregg, Xan 51

7. Riha, Stepan 51

8. Goebel, James 49

9. Nepsund, Ronald 47

10. Cutts, Kevin 46

11. Mallett, Jeff 44

12. Kasparian, Raffi 42

13. Vineyard, Jeremy 42

14. Darrah, Dave 31

15. Landry, Larry 29

16. Elwertowski, Tom 24

17. Lee, Johnny 22

18. Noll, Robert 22

19. Anderson, Troy 20

20. Beith, Gary 20

21. Burgoyne, Nick 20

22. Galway, Will 20

23. Israelson, Steve 20

24. Landweber, Greg 20

25. Pinkerton, Tom 20

There are three ways to earn points: (1) scoring in the top 5 of any Challenge, (2) being the first person to find a bug in a published winning solution or, (3) being the first person to suggest a Challenge that I use. The points you can win are:

1st place 20 points

2nd place 10 points

3rd place 7 points

4th place 4 points

5th place 2 points

finding bug 2 points

suggesting Challenge 2 points

Here is Xan’s winning solution:

Sprite Blitz

Xan Gregg, July 1995
/*       
Since “correctness” is considered before speed in judging solutions, this solution makes correctness the 
top priority at the cost of speed.

I use two offscreen GWorlds.  One has the background, and another has the image to be displayed on the 
screen next.  The “on deck” image is updated sprite by sprite, then it is copied to the screen for minimum 
flicker.

The GWorlds are a little bigger than the screen so I don’t have to worry about sprites that overlap the edges 
until copying to the screen.

Memory usage:
    2 GWorlds, each 64 pixels wider and taller than window.
    1K of pixel data for each sprite.
    128 bytes of mask data for each sprite.
    16 bytes of info for each sprite.
I set the number of sprites to 400.  The problem states a maximum of 200 present at a time, but because 
a deleted sprite stays around until the next UpdateScreen() call,
I allow for 400 in case you delete all 200 then add 200 more before calling UpdateScreen().  Paranoid, but 
if you’ve got the memory...

Assumptions not stated in the problem:
    Enough memory available for above usage.
    Window width is a multiple of 4 (confirmed by BB).
    Window does not move during play.

*/

#include <QDOffscreen.h>

typedef struct
 {
 short  nextSlot;
 short  status;
 short  width;
 short  height;
 Point  position;
 Point  lastPosition;
 } SpriteInfo, *SpriteInfoPtr;

typedef struct
 {
 char pixData[1024];
 } SpritePixData, *SpritePixDataPtr;

typedef struct
 {
 char maskData[128];
 } SpriteMaskData, *SpriteMaskDataPtr;


#define kMaxSprites400L
#define kMaxSpriteWidth   32L
#define kMaxSpriteHeight  32L

static CWindowPtrgScreenWindowP;
static GWorldPtr gBackgroundGW;
static PixMapHandlegBackgroundPixMapH;
static GWorldPtr gOnDeckGW;
static PixMapHandlegOnDeckPixMapH;
static shortgLastSpriteSlot;
static shortgFirstSpriteSlot;
static shortgSpriteCount;
static shortgWindowWidth;
static shortgWindowHeight;
static SpriteInfoPtr gSpriteInfo;
static SpritePixDataPtr gSpritePixData;
static SpriteMaskDataPtr gSpriteMaskData;
static long gOnDeckRowBytes;
static PtrgOnDeckBaseAddr;
static long gBkgRowBytes;
static PtrgBkgBaseAddr;
static long gScreenRowBytes;
static PtrgScreenBaseAddr;
static Ptr*gBkgRowAddr;
static Ptr*gOnDeckRowAddr;
static Ptr*gScreenRowAddr;
static shortgDeletionCount;

StartGame
void StartGame(CWindowPtr windowP)
{
 Rect   r;
 PixMapPtrbkgPixMapP;
 PixMapPtronDeckPixMapP;
 PixMapPtrscreenPixMapP;
 
 gLastSpriteSlot = -1;
 gFirstSpriteSlot = -1;
 gSpriteCount = 0;
 gDeletionCount = 0;
 gScreenWindowP = windowP;
 r = windowP->portRect;
 OffsetRect(&r, -r.left, -r.top);
 gWindowWidth = r.right;
 gWindowHeight = r.bottom;

 InsetRect(&r, -kMaxSpriteWidth, -kMaxSpriteHeight);
 NewGWorld(&gBackgroundGW, 0, &r, 0, 0, 0);
 gBackgroundPixMapH = GetGWorldPixMap(gBackgroundGW);
 LockPixels(gBackgroundPixMapH); /* always locked */
 NewGWorld(&gOnDeckGW, 0, &r, 0, 0, 0);
 gOnDeckPixMapH = GetGWorldPixMap(gOnDeckGW);
 LockPixels(gOnDeckPixMapH);/* always locked */
 
 gSpriteInfo = (SpriteInfoPtr) NewPtrClear
 (sizeof(SpriteInfo) * kMaxSprites);
 gSpritePixData = (SpritePixDataPtr) NewPtrClear
 (sizeof(SpritePixData) * kMaxSprites);
 gSpriteMaskData = (SpriteMaskDataPtr) NewPtrClear
 (sizeof(SpriteMaskData) * kMaxSprites);
 gBkgRowAddr = (Ptr *) NewPtr(sizeof(Ptr) *
 (gWindowHeight + kMaxSpriteHeight * 2));
 gOnDeckRowAddr = (Ptr *) NewPtr(sizeof(Ptr) *
 (gWindowHeight + kMaxSpriteHeight * 2));
 gScreenRowAddr = (Ptr *) NewPtr(sizeof(Ptr)
 * (long) gWindowHeight);
 if (gSpriteInfo == 0 || gSpritePixData == 0
 || gSpriteMaskData == 0 || gScreenRowAddr == 0
 || gBkgRowAddr == 0 || gOnDeckRowAddr == 0
 || gBackgroundGW == 0 || gOnDeckGW == 0)
 DebugStr("\p out of memory!");
 InsetRect(&r, kMaxSpriteWidth, kMaxSpriteHeight);
 OffsetRect(&r, kMaxSpriteWidth, kMaxSpriteHeight);
 CopyBits(&((WindowPtr)windowP)->portBits,
 &((WindowPtr)gBackgroundGW)->portBits,
 &windowP->portRect, &r, srcCopy, NULL);
 CopyBits(&((WindowPtr)windowP)->portBits,
 &((WindowPtr)gOnDeckGW)->portBits,
 &windowP->portRect, &r, srcCopy, NULL);
 
 bkgPixMapP = *gBackgroundPixMapH;
 onDeckPixMapP = *gOnDeckPixMapH;
 gOnDeckRowBytes = onDeckPixMapP->rowBytes & 0x7fff;
 gOnDeckBaseAddr = onDeckPixMapP->baseAddr
 + gOnDeckRowBytes * kMaxSpriteHeight
 + kMaxSpriteWidth;
 gBkgRowBytes = bkgPixMapP->rowBytes & 0x7fff;
 gBkgBaseAddr = bkgPixMapP->baseAddr
 + gBkgRowBytes * kMaxSpriteHeight
 + kMaxSpriteWidth;
 screenPixMapP = *gScreenWindowP->portPixMap;
 gScreenRowBytes = screenPixMapP->rowBytes & 0x7fff;
 gScreenBaseAddr = screenPixMapP->baseAddr
 - screenPixMapP->bounds.left
 - screenPixMapP->bounds.top
  * gScreenRowBytes;
 
 { /* initialize rowAddr’s */
 long row;
 
 gOnDeckRowAddr += kMaxSpriteHeight;
 gBkgRowAddr += kMaxSpriteHeight;
 for (row = -kMaxSpriteHeight;
 row < gWindowHeight + kMaxSpriteHeight; row++)
  {
 gBkgRowAddr[row] = gBkgBaseAddr
 + row * gBkgRowBytes;
 gOnDeckRowAddr[row] = gOnDeckBaseAddr
 + row * gOnDeckRowBytes;
  }
 for (row = 0; row < gWindowHeight; row++)
 gScreenRowAddr[row] = gScreenBaseAddr
 + row * gScreenRowBytes;
 }
}

AddSprite
/* make a copy of CIcon’s pixel and mask data */
short AddSprite(CIconPtr cIconP, Point startPt)
{
 short  slot;
 short  i;
 short  pixWidth;
 short  maskWidth;
 short  pixBytes;
 short  maskBytes;
 short  bitBytes;
 short  height;
 Ptr    pixSrcAddr, pixDstAddr;
 long   *maskSrcAddr, *maskDstAddr;
 
 slot = gLastSpriteSlot + 1;
 if (slot == kMaxSprites)
 slot = 0;
 while (gSpriteInfo[slot].status != 0)
  {
 slot++;
 if (slot == kMaxSprites)
 slot = 0;
  }
 gSpriteInfo[slot].status = 1;/* occupied */
 height = cIconP->iconPMap.bounds.bottom
 - cIconP->iconPMap.bounds.top;
 pixWidth = cIconP->iconPMap.bounds.right
 - cIconP->iconPMap.bounds.left;
 maskWidth = (pixWidth + 7) >> 3;
 gSpriteInfo[slot].width = pixWidth;
 gSpriteInfo[slot].height = height;
 pixBytes = cIconP->iconPMap.rowBytes & 0x7fff;
 maskBytes = cIconP->iconMask.rowBytes;
 bitBytes = cIconP->iconBMap.rowBytes;
 pixSrcAddr = ((Ptr) &cIconP->iconMaskData)
 + bitBytes * height
 + maskBytes * height
 + 256 * 8 + 8;  /* 8-bit color table */

 pixDstAddr = (char *) &gSpritePixData[slot];
 maskSrcAddr = (long *) &cIconP->iconMaskData;
 maskDstAddr = (long *) &gSpriteMaskData[slot];
 pixWidth = pixWidth >> 2;
 for (i = 0; i < height; i++)
  {
 {
 register long *q = (long *) pixDstAddr;
 register long *p = (long *) pixSrcAddr;
 register short  j = pixWidth;
 while (j > 0)
  {
 *q++ = *p++;
 j--;
  }
 }
 *maskDstAddr++ = *maskSrcAddr;
 pixDstAddr += 32;
 pixSrcAddr += pixBytes;
 maskSrcAddr = (long *) (((Ptr) maskSrcAddr)
 + maskBytes);
  }
 
 if (gLastSpriteSlot >= 0)
  {
 gSpriteInfo[gLastSpriteSlot].nextSlot = slot;
  }
 else
  {
 gFirstSpriteSlot = slot;
  }
 gLastSpriteSlot = slot;
 gSpriteInfo[slot].nextSlot = -1;
 gSpriteInfo[slot].position = startPt;
 gSpriteInfo[slot].lastPosition = startPt;
 gSpriteCount ++;
 return slot;
}

EraseSprite
/* replace sprite with chunk from the bkg gworld */
static void EraseSprite(SpriteInfoPtr spriteInfoP)
{
 short  numRows;
 short  numCols;
 register long *p;
 register long *q;
 short  h, v;
 register long srcRowBytes;
 register long dstRowBytes;
 
 numRows = spriteInfoP->height;
 numCols = spriteInfoP->width;
 h = spriteInfoP->lastPosition.h;
 v = spriteInfoP->lastPosition.v;
 if (h + numCols <= 0 || h >= gWindowWidth
 || v + numRows <= 0 || v >= gWindowHeight)
 return;/* totally offscreen, so skip it */
 
 p = (long *) (gBkgRowAddr[v] + h);
 q = (long *) (gOnDeckRowAddr[v] + h);
 srcRowBytes = gBkgRowBytes - numCols;
 dstRowBytes = gOnDeckRowBytes - numCols;
 if (numCols >= 16)
 if (numCols == 32)
  {
 while (numRows != 0)
  {
 numRows--;
 *q++ = *p++;
 *q++ = *p++;
 *q++ = *p++;
 *q++ = *p++;
 *q++ = *p++;
 *q++ = *p++;
 *q++ = *p++;
 *q++ = *p++;
 p = (long *) (((Ptr) p) + srcRowBytes);
 q = (long *) (((Ptr) q) + dstRowBytes);
  }
  }
 else
  {
 while (numRows != 0)
  {
 numRows--;
 *q++ = *p++;
 *q++ = *p++;
 *q++ = *p++;
 *q++ = *p++;
 p = (long *) (((Ptr) p) + srcRowBytes);
 q = (long *) (((Ptr) q) + dstRowBytes);
  }
  }
 else
  {
 if (numCols < 8)
 while (numRows != 0)
  {
 numRows--;
 *q = *p;
 p = (long *) (((Ptr) p) + gBkgRowBytes);
 q = (long *) (((Ptr) q) + gOnDeckRowBytes);
  }
 else /* erase 4 pixels, even if its smaller */
 while (numRows != 0)
  {
 numRows--;
 *q++ = *p++;
 *q++ = *p++;
 p = (long *) (((Ptr) p) + srcRowBytes);
 q = (long *) (((Ptr) q) + dstRowBytes);
  }
  }
}

DeleteSprite

/* Don’t actually do the delete, just mark for deletion -- because we still 
   need to erase it in UpdateScreen()
*/
void DeleteSprite(short spriteID)
{
 gSpriteInfo[spriteID].status = -1;/* to be deleted */
 gDeletionCount++;
}

RemoveDeletedSprites
/* only called when there is at least one deletion */
static void RemoveDeletedSprites(void)
{
 short  prevSlot = -1;
 short  slot = gFirstSpriteSlot;
 short  count = gDeletionCount;
 
 while (1)
  {
 if (gSpriteInfo[slot].status < 0)
  {/* needs to be removed */
 if (prevSlot >= 0)
 gSpriteInfo[prevSlot].nextSlot
  = gSpriteInfo[slot].nextSlot;
 else
 gFirstSpriteSlot
  = gSpriteInfo[slot].nextSlot;
 if (slot == gLastSpriteSlot)
 gLastSpriteSlot = prevSlot;
 gSpriteInfo[slot].status = 0;/* available */
 gSpriteCount--;
 count--;
 if (count == 0)
 break;
  }
 else
  {
 prevSlot = slot;
  }
 slot = gSpriteInfo[slot].nextSlot;
  }
 gDeletionCount = 0;
}

MoveSprite
void MoveSprite(short spriteID, Point deltaPt)
{
 gSpriteInfo[spriteID].position.h += deltaPt.h;
 gSpriteInfo[spriteID].position.v += deltaPt.v;
}

EraseOldSprites
static void EraseOldSprites(void)
{
 short  slot;
 SpriteInfoPtr spriteInfoP;
 
 slot = gFirstSpriteSlot;
 while (slot >= 0)
  {
 spriteInfoP = &gSpriteInfo[slot];
 EraseSprite(spriteInfoP);
 slot = spriteInfoP->nextSlot;
  }
 
}

COPY4
/* copy 4 pixels based on bits of the mask */
#define COPY4(q,p,m) \
switch ((m) & 0x0f)\
 { \
 case 0x0: break;\
 case 0x1: *(q+3) = *(p+3); break; \
 case 0x2: *(q+2) = *(p+2); break; \
 case 0x3: *(short*)(q+2) = *(short*)(p+2); break; \
 case 0x4: *(q+1) = *(p+1); break; \
 case 0x5: *(q+1) = *(p+1); *(q+3) = *(p+3); break;      \
 case 0x6: *(short*)(q+1) = *(short*)(p+1); break; \
 case 0x7: *(q+1) = *(p+1); \
   *(short*)(q+2) = *(short*)(p+2); break;   \
 case 0x8: *(q) = *(p); break;\
 case 0x9: *(q) = *(p); *(q+3) = *(p+3); break;    \
 case 0xA: *(q) = *(p); *(q+2) = *(p+2); break;    \
 case 0xB: *(q) = *(p);   \
   *(short*)(q+2) = *(short*)(p+2); break;   \
 case 0xC: *(short*)(q) = *(short*)(p); break;     \
 case 0xD: *(short*)(q) = *(short*)(p);\
   *(q+3) = *(p+3); break;\
 case 0xE: *(short*)(q) = *(short*)(p);\
   *(q+2) = *(p+2); break;\
 case 0xF: *(long*)(q) = *(long*)(p); break; \
 }

COPY8
#define COPY8(q,p,mask)   \
 COPY4(q, p, mask >> 4)   \
 COPY4(q+4,p+4, mask)

DrawSprite
static void DrawSprite(short slot)
{
 SpriteInfoPtr spriteInfoP;
 short  numRows;
 short  numCols;
 register Ptr  p;
 register Ptr  q;
 Ptr    maskP;
 short  srcRowBytes;
 short  h, v;
 short  mask;
 short  maskMask;
 short  maskRowBytes;
 short  numMaskBytes;
 short  i;
 long   dstRowBytes;

 spriteInfoP = &gSpriteInfo[slot];
 h = spriteInfoP->position.h;
 v = spriteInfoP->position.v;
 numRows = spriteInfoP->height;
 numCols = spriteInfoP->width;
 p = (char *) &gSpritePixData[slot];
 q = gOnDeckRowAddr[v] + h;
 maskP = (char *) &gSpriteMaskData[slot];
 
 if (numCols >= 8)
  {
 numMaskBytes = numCols >> 3;
 maskRowBytes = 4 - numMaskBytes;
 srcRowBytes = 40 - numCols;
 dstRowBytes = gOnDeckRowBytes - numCols + 8;
 while (1)
  {
 i = numMaskBytes;
 while (1)
  {
 mask = *maskP++;
 COPY8(q, p, mask)
 if (--i == 0)
 break;
 p += 8;
 q += 8;
  }
 if (--numRows == 0)
 break;
 maskP += maskRowBytes;
 p += srcRowBytes;
 q += dstRowBytes;
  }
  }
 else
  {
 maskMask = 0xf00 >> numCols;
 while (1)
  {
 mask = (*maskP) & maskMask;
 COPY8(q, p, mask)
 if (--numRows == 0)
 break;
 maskP += 4;
 p += 32;
 q += gOnDeckRowBytes;
  }
  }
}

DrawNewSprites
static void DrawNewSprites(void)
{
 register short  slot;
 SpriteInfoPtr spriteInfoP;
 
 slot = gFirstSpriteSlot;
 while (slot >= 0)
  {
 register short  numRows;
 register short  numCols;
 register short  h;
 register short  v;
 
 spriteInfoP = &gSpriteInfo[slot];
 if (spriteInfoP->status < 0)
 goto nextSlot;  /* deleted, so skip it */
 numRows = spriteInfoP->height;
 numCols = spriteInfoP->width;
 h = spriteInfoP->position.h;
 v = spriteInfoP->position.v;
 if (h + numCols <= 0 || h >= gWindowWidth
 || v + numRows <= 0 || v >= gWindowHeight)
 goto nextSlot;  /* totally offscreen */

 DrawSprite(slot);
nextSlot:
 slot = spriteInfoP->nextSlot;
  }
}

FastCopyChunk
/* count is a multiple of 4 in the range [4..44] */
static void FastCopyChunk(long *q, long *p,
 short count, short rows)
{
 register short  srcRowBytes;
 register short  dstRowBytes;
 register short  rowsLeft = rows;
 register short  copy8 = count & 8;
 register short  copy4 = count & 4;
 
 srcRowBytes = gOnDeckRowBytes - count;
 dstRowBytes = gScreenRowBytes - count;
 if (count & 32)
  {
 while (rowsLeft > 0)
  {
 rowsLeft--;
 *q++ = *p++;
 *q++ = *p++;
 *q++ = *p++;
 *q++ = *p++;
 *q++ = *p++;
 *q++ = *p++;
 *q++ = *p++;
 *q++ = *p++;
 if (copy8)
  {
 *q++ = *p++;
 *q++ = *p++;
  }
 if (copy4)
 *q++ = *p++;
 p = (long *) (((Ptr) p) + srcRowBytes);
 q = (long *) (((Ptr) q) + dstRowBytes);
  }
  }
 else if (count & 16)
  {
 while (rowsLeft > 0)
  {
 rowsLeft--;
 *q++ = *p++;
 *q++ = *p++;
 *q++ = *p++;
 *q++ = *p++;
 if (copy8)
  {
 *q++ = *p++;
 *q++ = *p++;
  }
 if (copy4)
 *q++ = *p++;
 p = (long *) (((Ptr) p) + srcRowBytes);
 q = (long *) (((Ptr) q) + dstRowBytes);
    }
  }
 else
  {
 while (rowsLeft > 0)
  {
 rowsLeft--;
 if (copy8)
  {
 *q++ = *p++;
 *q++ = *p++;
  }
 if (copy4)
 *q++ = *p++;
 p = (long *) (((Ptr) p) + srcRowBytes);
 q = (long *) (((Ptr) q) + dstRowBytes);
    }
  }
}

DrawNewSpritesToScreen

/* Here we do have to watch out for sprites that overlap the edges of the window.  We copy a rectangluar 
region that includes the sprites previous and current positions.  We know they will be close sionce sprites 
move at most 8 pixels per turn.
*/
static void DrawNewSpritesToScreen(void)
{
 short  slot;
 SpriteInfoPtr spriteInfoP;
 short  numRows;
 short  numCols;
 register long   *p;
 register long   *q;
 short  hStart, hEnd;
 short  vStart, vEnd;
 
 slot = gFirstSpriteSlot;
 while (slot >= 0)
  {
 spriteInfoP = &gSpriteInfo[slot];
 numRows = spriteInfoP->height;
 numCols = spriteInfoP->width;
 if (spriteInfoP->position.h
  < spriteInfoP->lastPosition.h)
  {
 hStart = spriteInfoP->position.h;
 hEnd = spriteInfoP->lastPosition.h + numCols;
  }
 else
  {
 hStart = spriteInfoP->lastPosition.h;
 hEnd = spriteInfoP->position.h + numCols;
  }
 if (hStart < 0)
 hStart = 0;
 else if (hEnd > gWindowWidth)
 hEnd = gWindowWidth;
 if (spriteInfoP->position.v
  < spriteInfoP->lastPosition.v)
  {
 vStart = spriteInfoP->position.v;
 vEnd = spriteInfoP->lastPosition.v + numRows;
  }
 else
  {
 vStart = spriteInfoP->lastPosition.v;
 vEnd = spriteInfoP->position.v + numRows;
  }
 if (vStart < 0)
 vStart = 0;
 else if (vEnd > gWindowHeight)
 vEnd = gWindowHeight;
 hStart = hStart & -4;  /* make it a mult of 4 */
 hEnd = (hEnd + 3) & -4;  /* make it a mult of 4 */
 
 p = (long *) (gOnDeckRowAddr[vStart] + hStart);
 q = (long *) (gScreenRowAddr[vStart] + hStart);
 
 vEnd -= vStart; /* now it’s a count */
 hEnd -= hStart; /* now it’s a count */
 if (hEnd >= 0)
 FastCopyChunk(q, p, hEnd, vEnd);
 
 spriteInfoP->lastPosition = spriteInfoP->position;
 slot = spriteInfoP->nextSlot;
  }
}

UpdateScreen
void UpdateScreen(void)
{
 EraseOldSprites();
 DrawNewSprites();
 DrawNewSpritesToScreen();
 if (gDeletionCount != 0)
 RemoveDeletedSprites();
}

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.