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

Time for a demo

Great, now we can create sprites, load images, show and hide those images, and finally destroy the sprite when we no longer need them. We now have everything we need to make some real use out of them. Of course, in the interest of being thorough, I've also written out all of the other functions that you may or may not need to use on the sprites.

Find the sprite header by clicking here.
Find the sprite source by clicking here.

Other than the first few functions we've already been over here, I do not explain the other functions in too great of detail. However, here is a brief rundown on what each of the undocumented functions do.

  int SpriteRedimension(struct Sprite *thisSpr, int w, int h, int drtyRect);
    

This function redimensions a sprite that has already been created and initialized. This allows you to change the values of width and height and adjust the memory allocated to all of the images in order to reflect this change. This eliminates the need to destroy and re-create the sprite just to change it's dimensions.

  int SpriteChangeNumFrames(struct Sprite *thisSpr, int numFrames);
    

This function changes the number of frames by properly reallocating memory to reflect the new number of frames in the frames array and by changing the value of noOfFrames. Use this function on sprites that have already been initialized.


Using the directory tree outline given at the completion of our basic graphics functions, save the Sprite.h file in your INCLUDE directory and save the Sprite.c file in your SOURCE directory.

At the parent directory of the SOURCE and INCLUDE directories, we'll write a small demo program that bounces a ball around the screen for an indefinite amount of time until a key is pressed. This is the program I came up with. Since it is fully commented (unlike most of my code) I will not go over how this program works in detail. The purpose of this program is to show you the functions in action and how they can be used in future programs that we plan to write using them.

  /* TESTSPR.C
   *
   * This program tests the sprite functions by creating
   * a sprite and storing an image of a ball within it.
   *
   * Then it bounces the ball around the screen for animation.
   */

  #include "include\vga256.h"
  #include "include\sprite.h"
  #include <conio.h>

  /* The bitmap image of the ball. */
  char BallBitmap[] = {
      0, 0, 0, 2, 2, 2, 2, 0, 0, 0,
      0, 2, 2, 2, 2, 2, 2, 2, 2, 0,
      0, 2, 2, 0, 0, 2, 2, 2, 2, 0,
      2, 2, 0, 0, 2, 2, 2, 2, 2, 2,
      2, 2, 0, 2, 2, 2, 2, 2, 2, 2,
      2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
      2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
      0, 2, 2, 2, 2, 2, 2, 2, 2, 0,
      0, 2, 2, 2, 2, 2, 2, 2, 2, 0,
      0, 0, 0, 2, 2, 2, 2, 0, 0, 0
  };

  int main(void)
  {
      struct Sprite Ball; /* Create a sprite for the ball */
      int xDir, yDir;     /* X and Y axes directions */

      /* Attempt to initialize the sprite */
      /* Use a 10x10 size image, 1 frame */
      /* images are not dynamic, and use dirty rectangles */
      if (!InitSprite(&Ball, 10, 10, 1, 0, 1)) {
          cputs("Could not initialize Sprite ball, closing...");
          return 1;
      }

      /* Set the first (and only) image to the bitmap image */
      SpriteSetImage(&Ball, BallBitmap, 0);

      /* Switch to Mode 13H */
      SetVideoMode(VGA256);
      Ball.X = (rand() % 200) + 50;  /* Set Ball's X axis */
      Ball.Y = (rand() % 100) + 50;  /* Set Ball's Y axis */
      xDir = -1;                     /* Set X axis offset */
      yDir = 1;                      /* Set Y axis offset */

      /* Show ball on screen */
      SpriteShow(&Ball);
      getch();  /* Wait for a key */

      /* Loop while no key is pressed */
      while (!kbhit()) {
          SpriteHide(&Ball);  /* Hide ball's image */
          Ball.X += xDir;     /* Add offset to X position */
          Ball.Y += yDir;     /* Add offset to Y position */
          /* Check X and Y axes limits */
          /* Negate offsets if limits are reached */
          if (Ball.Y < 5 || Ball.Y > 185) yDir = -yDir;
          if (Ball.X < 5 || Ball.X > 305) xDir = -xDir;
          SpriteShow(&Ball);  /* Show ball's image */
      }

      /* Eat key pressed */
      getch();
      SetVideoMode(TEXT_MODE);  /* Reset text screen mode */
      DestroySprite(&Ball);  /* Destroy sprite */
      return 0;  /* Return to OS */
  }
    


If you have been following the pattern for our directory tree, your directory listing should look like this:

Parent directory where the samples are stored
Examples include "A:\", "C:\MYDEMOS", etc.
|
|   TESTVGA.C
|   TESTVGA.OBJ
|   TESTVGA.EXE
|   TESTSPR.C
|
+---INCLUDE
|       VGA256.H
|       SPRITE.H
|
+---LIB
|       VGA256.OBJ
|       GAME_13H.TXT
|       GAME_13H.LIB
|
\---SOURCE
        VGA256.C
        SPRITE.C
    

First, let's add the sprite functions to the game library, switch to the parent directory of the INCLUDE, LIB and SOURCE directories and type the following commands at the command prompt.

  tcc -mm -c -1 source\sprite.c
  move sprite.obj lib
  cd lib
  tlib game_13h.lib +sprite.obj, game_13h.txt
    

If you did everything correctly, the directory tree should now look like this.

Parent directory where the samples are stored
Examples include "A:\", "C:\MYDEMOS", etc.
|
|   TESTVGA.C
|   TESTVGA.OBJ
|   TESTVGA.EXE
|   TESTSPR.C
|
+---INCLUDE
|       VGA256.H
|       SPRITE.H
|
+---LIB
|       VGA256.OBJ
|       GAME_13H.TXT
|       GAME_13H.LIB
|       SPRITE.OBJ
|
\---SOURCE
        VGA256.C
        SPRITE.C
    

Switch back to the parent directory and compile the TESTSPR.C program at the command prompt using the following command.

  tcc -mm -1 testspr.c lib\game_13h.lib
    

Now your directory tree should look like this.

Parent directory where the samples are stored
Examples include "A:\", "C:\MYDEMOS", etc.
|
|   TESTVGA.C
|   TESTVGA.OBJ
|   TESTVGA.EXE
|   TESTSPR.C
|   TESTSPR.OBJ
|   TESTSPR.EXE
|
+---INCLUDE
|       VGA256.H
|       SPRITE.H
|
+---LIB
|       VGA256.OBJ
|       GAME_13H.TXT
|       GAME_13H.LIB
|       SPRITE.OBJ
|
\---SOURCE
        VGA256.C
        SPRITE.C
    

Now you should be able to run the TESTSPR.EXE program and watch the green ball move around the screen (at a very fast rate). If you are getting errors trying to compile the TESTSPR.C program, you can compile, test and debug the program from within the Turbo C++ IDE by changing the following 2 lines of the TESTSPR.C program from this:

  #include "include\vga256.h"
  #include "include\sprite.h"
    

to this:

  #include "source\vga256.c"
  #include "source\sprite.c"
    

...and make sure you use the File/Change Dir... menu and change to the parent directory.

Well that covers the sprite functions themselves, next we're going to look into combining these functions into a class or sometimes referred to an object. There are some distinct advantages to programming with classes, many of which can be very easily recognized. As we learn how to translate our struct and functions into a class we will take a look into both the advantages and disadvantages of doing so.


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

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