Click here if you are stuck in someone else's frames.

If this page looks like garbage then you need one of these browsers...

Microsoft Internet Explorer

Netscape Navigator

Please sign my programmer's page guestbook.

Please sign my guestbook

View my guestbook entries

Disecting the function

Now this is the first part of the function that we will disect...

  if (w > 0) {
    thisSpr->width = w;
    if (h > 0) thisSpr->height = h;
  }
    

We're assigning the height and width of our sprite to the values of the w and h parameters (if they're greater than zero). Nothing tricky here.

Let's look at the next if statement testing the parameter numFrames.

  thisSpr->imgDynamic = dynamic;
  if (numFrames > 0) {
    thisSpr->frames=(char far **)malloc(numFrames*sizeof(char far *));
    

If we specified for there to 1 or more images for our sprite to hold (numFrames > 0) then the first thing we need to do is create an array of numFrames elements. The malloc line accomplishes that task for us. For some of you experienced C programmers of the DOS platform, you may be wondering whether or not I should've used farmalloc instead of malloc. After all, frames is a char far ** type variable. Actually, frames is a near pointer that points to an array of far pointers. The first point (*) is far (char far **), the second pointer (*) is near because there is no far modifier. The function farmalloc would've been used only if the frames variable was declared as char far * far *frames;.

If malloc was successful in allocating the array, the number of frames will be set and each far pointer will be assigned an address to a specific image of NULL depending on whether the images are dynamic and whether both height and width are greater than zero. These are the lines that do this task:

    if (thisSpr->frames != NULL) {
      thisSpr->noOfFrames = numFrames;
      for (i=0; i<numFrames; i++)
        if (dynamic && w > 0 && h > 0) {
          thisSpr->frames[i]=(char far *)farmalloc(w*h*sizeof(char));
          if (thisSpr->frames[i] == NULL) retVal = 0;
        } else
          thisSpr->frames[i] = NULL;
    } else
      retVal = 0;
    

Of course if no frames we're asked for, that is if numFrames was zero or below, this line is read:

  } else
    thisSpr->frames = NULL;
    

Finally we try to reserve memory for storing a background copy if we decide to erase sprites using dirty rectangles or not. (drtyRect parameter) Then we assign default values to the rest of the variables.

  if (drtyRect) {
    thisSpr->background = (char far *)farmalloc(w * h * sizeof(char));
    if (thisSpr->background == NULL && w && h) retVal = 0;
    thisSpr->backTransparent = 1;
  } else
    thisSpr->background = NULL;

  thisSpr->maxX = ScreenWidth - 1;
  thisSpr->maxY = ScreenHeight - 1;

  thisSpr->lenXcopy = thisSpr->width;
  thisSpr->lenYcopy = thisSpr->height;

  thisSpr->displayMem = videoMem;

  thisSpr->X = thisSpr->Y = thisSpr->lastX = thisSpr->lastY =
  thisSpr->minX = thisSpr->minY = thisSpr->startXoff = thisSpr->startYoff =
  thisSpr->currFrame = thisSpr->status = thisSpr->visible = thisSpr->clock =
  thisSpr->animThreshold = thisSpr->moveThreshold = 0;

  thisSpr->otherData = NULL;
  return retVal;
}
    

Send your questions, comments, or ideas to: wilkeg@gamewood.net

This page hosted by GeoCities Get your own Free Home Page
 Next Back |