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

Creating Structs and Functions

Now that we have an idea as to the kind of data that we plan to use for a sprite we can now concentrate on writing the functions that make use of this data and well, actually make something useful here. So where to begin? Hmm, well, how about writing a function that will initialize our data, got to have that. Let's consider what it is that we need to set our variables to.

First off, we need assign the frames array and the background pointer to an area of memory where we wish to store our images. How much memory are we talking about here? Well that depends on the height and width of the sprite. Then that brings up another question as to what do we want to assign those variables? This can get tedious so rather than go over every nugget of initializing the data here let's instead look at the initialization code that I came up with and examine it.

int InitSprite(struct Sprite *thisSpr, int w, int h,
               int numFrames, int dynamic, int drtyRect)
{
  int i, retVal = 1;

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

  thisSpr->imgDynamic = dynamic;
  if (numFrames > 0) {
    thisSpr->frames=(char far **)malloc(numFrames*sizeof(char far *));
    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;
  } else
    thisSpr->frames = NULL;

  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;
}
    

This process seems a bit lengthy huh? This function gives a value to every element of the sprite data structure that you pass to it. This function accepts 6 parameters. The first parameter (struct Sprite *thisSpr) is a pointer to the data structure itself. All of you who are already familiar with C should already know the purpose of this parameter so let's move on.

Parameters 2 and 3 (int w and int h) are the initial width and height of the bitmap images that the sprite is going to hold. Set one or both of these parameters to 0 (or a negative number) and the function will not reserve any memory for the images.

The fourth parameter (int numFrames) specify the number of frames (images) that the sprite will hold. Multiple image sprites are used primarily for animation.

The fifth parameter (int dynamic) specifies whether the function should reserve memory from the heap for images (dynamic != 0) or whether it should set the pointers to an image that is already loaded in memory (dynamic == 0). Normally, we would set this parameter to 1, there are special cases when it is appropriate to set it to zero. For now, we'll just stick with 1.

Which finally brings us to our last parameter (int drtyRect). If set to a non-zero number, it will reserve an area of memory used to hold a copy of the background area the way it looked prior to a sprite being drawn. This will allow us to clean up the dirty rectangle by allowing us to restore the background back on top of the sprite's image (erasing it). More on this later.


Send your questions, comments, or ideas here.

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

1