/* 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
Get your own Free Home Page