If this page looks like garbage then you need one of these browsers...Please sign my programmer's page guestbook. |
Sprites, the main focus of games.Most of the video games you will write will consist of bitmaps moving around over some type of background. This background can consist of just a single color, as in a black background, it can be a picture or anything else for that matter. Our main topic for now will be that of sprites. So what exactly are sprites?In its simplest form, sprites consist of a rectangular shaped bitmapped image. In fact, most sprites do not have much more information than that. The bitmap can be an image of a car, a boat, a ball, a person, or anything else that you can imagine. These images can be moved around the screen and animated. In this section, we will look into ways of moving and animating sprites and how not to destroy complex background images that they're moving on. This picture illustrates the basic characteristics of a sprite and its relationship with the background, or screen, itself. The rectangular border around the image is known as its bounding box. You cannot actually see this bounding box on an actual sprite, however you can make where the borders are by using the coordinates of the upper left corner of the bitmap (X,Y) and adding the values of both the width and the height of the image. This is basically all the information you need to store a sprite but generally most sprites also contain other types of information such as its status, other images for animation, etc.. Of course now we're leading up to the actual code that we need to write
to make our sprites. First off, we need to figure out what kind of data
that we need to store for each sprite. We can put all of the data
pertaining to a sprite into a data structure and acheive some sort of
encapsulation. For C programmer's such as ourselves, we will use the
C's struct keyword to accomplish this. So let's look at the data that we
will put into a sprite.
struct Sprite { int startXoff, startYoff; /* Offset copying position */ int lenXcopy, lenYcopy; /* Actual dimensions to copy */ char far *displayMem; /* Buffer sprite is modifying */ int X, Y; /* Sprite's current location */ int lastX, lastY; /* Sprite's last location */ int width, height; /* Sprite's dimensions */ int minX, minY; /* Minimum limits */ int maxX, maxY; /* Maximum limits */ int currFrame; /* current frame in animation */ int noOfFrames; /* total number of frames in animation */ char far **frames; /* array of sprite images for animation */ char far *background; /* background stored for dirty rectangle */ int imgDynamic; /* flags if images are static or dynamic */ int visible; /* flags if Sprite is or isn't visible */ int backTransparent; /* opaque or transparent background */ int status; /* Sprite status, varies by game rules */ int animThreshold; /* animation threshold, varies */ int moveThreshold; /* move threshold, varies */ int clock; /* sprite's clock */ void far *otherData; /* Extra data */ };
Wow! I beleive that is quite a bit of information for a sprite. Is all of this information really necessary? Well, yes and no, the best answer to that depends on what exactly we need the sprite to do in our particular video game. I feel that for most games this struct will be more than sufficient, besides, in the world of programming games it is better sometimes to have more than is needed that to come up short and make programming the game even tougher. Now let's examine what each field does and then write some functions that will complete our sprite engine. First off, let's examine each and every element one by one to see why they exist.
Writing the code for our Sprite EngineOf course, now we've kind of gotten to a point where there is a kind of fork in our path to writing our sprite engine. Many of you who are reading this have a C++ compiler. I myself use a C++ compiler to do all of these programming examples (Turbo C++ 3.0). It allows us to compile C and C++ programs, this presents us with two opportunities. One is that we can continue to use the struct that we have just written and write a library of C functions that kind of encapsulate the functionality of a sprite. On the other hand, we could acheive total encapsulation by binding the data and functions altogether and creating a single class (or object) that has all of the functionality. The choice here is yours, some people do not like to use C++ at all and stick with just C code. If you are not very familiar with the C language itself, then it will not be to your best interest to jump into the C++ arena and try to encapsulate everything into a single object. For now, we will just continue using C and not use any C++ while working on a sprite. After we get it working, we will see how by just making a few modifications we will have learned C++, if you don't know it already, and managed to create a true object at the same time. Of course, for you diehard C fans, you can just keep the functions as they are and use you sprite library and just skip over the section where we go into creating a sprite object. So without any further hesitation, let's build our functions and get our sprite to work. The choice is yours on which path to take.Send your questions, comments, or ideas here. This page hosted by
Get your own Free Home Page |