home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Media Share 13
/
mediashare_13.zip
/
mediashare_13
/
ZIPPED
/
PROGRAM
/
WTJ9403.ZIP
/
BARNHART
/
SO.HPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-11-04
|
13KB
|
356 lines
// so.hpp - Screen Objects header file
// Written by Andy Barnhart
// This header file defines constants, function prototypes and objects used in a
// simple game library demonstrating object oriented principles in graphics
// animation
// This seems to work for me. I hope it works for you.
// No other warranty is expressed or implied.
// The system includes section
#include <windows.h>
#include <stdlib.h>
#include <string.h>
// These are the types of objects in the library
// The base type returns the variant types, so that other objects have
// some idea what they are dealing with, even if they don't access
// subtype methods or variables.
#define TYPE_PLAYER 1
#define TYPE_WALL 2
#define TYPE_MISSILE 3
#define TYPE_DOOR 4
#define TYPE_HAZARD 5
#define TYPE_PLATFORM 6
#define TYPE_PRIZE 7
#define TYPE_RUNGS 8
// Player movements are in bit switches so we can get a multi directional event
#define KEY_RIGHT 1
#define KEY_LEFT 2
#define KEY_UP 4
#define KEY_DOWN 8
// Our X and Y size. This could be variable, but for our purposes (demo) a fixed
// size simplifies things. You will find some code about for sizing, though...
#define GMX 640
#define GMY 444
// This is a define I added to place platforms at a given level or floor. Nothing
// magic; just a little trial and error with my hero and his jumping ability.
#define LEV(a) ((a * 74)-16)
// Prototypes for the non object related functions
// I only have one screen and one scene in progress at a time, so I do it the
// old fashioned way.
void InitScreen( HWND hw, HDC hd, HANDLE hi);
void TermScreen( void);
void SizeScreen( int x, int y);
BOOL InitScene( int scene);
void ShowScene( void);
void DeleteScene( void);
// The base class - ScreenObj
class ScreenObj
{
protected:
ScreenObj *next; // All the screen objects are linked together
public:
long Info; // Info is dependant on object type, like score on a prize
BOOL shown; // Is the object currently in a shown state
int SOType; // What is it (from the type list above)
int x; // Where is it?
int y;
int width; // How big is the bitmap it comes from?
int wmult; // How many time is the bitmap drawn?
int height;
int hmult;
// Some public object statics - That's OOP for globals ;^>
// but setting them up this way leaves the door open for multiple
// windows and instances
static HDC hDC;
static HWND hWnd;
static HANDLE hInst;
// The backdrop object is needed for every redraw so we want a short cut
// to find it for speed.
static ScreenObj *Background;
// Constructor and destructor do little; this object is largely boilerplate
// for descendants
ScreenObj(int ix, int iy);
~ScreenObj( void);
// The descendants implement type, but all ScreenObj's have the method
virtual int Type( void){return NULL;}
// Get pointer to next object
ScreenObj *Next(void){return next;}
// Set the state to shown/hidden and paint the screen in its area
void Show( BOOL nCmdShow) {shown=nCmdShow;Paint();}
// Update is called on a timer tick so that animation can be
// done by descendant objects as state machines
virtual BOOL Update( void ){return FALSE;}
// Paint the screen at the area the ScrnObj occupies, not necessarily
// showing the object if it is covered or hidden
virtual void Paint( void){;}
// Bitblt into a DC the portion of your object in a given rectangle
virtual void Paint( RECT *rc, HDC hPaintDC = hDC, BOOL memdc = FALSE){;}
// This is how other objects notify the object they run into
virtual BOOL Hit( int type) {return TRUE;}
// Test to see if we are in a given rectangle
BOOL InRect( RECT *rc)
{
if( ( x > rc->right) || (y > rc->bottom) ||
( (x+(width * wmult)) < rc->left) ||
( (y+(height * hmult)) < rc->top) ) return( FALSE);
return( TRUE);
}
// Fill a RECT struct with our coordinates
void SetRect( RECT *rc)
{
rc->left = x;
rc->right = x + width;
rc->top = y;
rc->bottom = y + height;
}
// Move the object - not to be confused with setting a target
// (discussed later) for animation; this is an instant move
void Move( int newx, int newy)
{
if( shown)
{
Show(FALSE);
x = newx;
y = newy;
Show(TRUE);
}
else
{
x = newx;
y = newy;
}
}
// Move an object up by its own height
void OnTop( int newy)
{
Move( x, newy - height);
}
};
// Calculate the distance between two objects
int SODist( ScreenObj * SO1, ScreenObj * SO2);
// The Screen background object only needs paint, but could certainly be
// extended to include bitmap assignment (hint, hint...)
class ScreenBack : public ScreenObj
{
public:
ScreenBack( int ix, int iy);
virtual void Paint( RECT *rc, HDC hPaintDC = hDC, BOOL memdc = FALSE);
};
// Screen Item - The subtype that is the base for all the actual game pieces
// with common properies such as bitmap images, speed and direction for
// movement, etc.
class ScreenItem : public ScreenObj
{
protected:
HBITMAP hbmImage; // The image currently displayed
int oldx; // Where we were at the beginning of a single motion
int oldy;
int targetx; // Where we are headed (may take a few motions)
int targety;
int period; // How often to update (every Nth timer event)
int periodct; // How many ticks since last update
int xinc; // How far to move on each update
int yinc;
HANDLE hbits; // Bitmap handling caca
BYTE far * bits;
int linewidth;
int planes;
int bitslen;
public:
// Our constructor; note that we get a resource name and movement amounts
ScreenItem( char *res, int ix = 0, int iy = 0, int incx = 24, int incy = 24);
// Our destructor
~ScreenItem( void);
// At the ScreenItem level, type is relevant
virtual int Type( void){return SOType;}
// Paint functions
void Paint( void);
void Paint( RECT *rc, HDC hPaintDC = hDC, BOOL memdc = FALSE);
// Select current bitmap for item
void SelectBM ( HBITMAP hb);
// Update item (on timer tick)
BOOL Update( void);
// Set destination to begin moving toward on Update calls
void SetTarget( int tx, int ty);
};
// StllMotion - a conflict in terms? No; this is for hazards like fire which
// are fixed at a location but still are animated in place
class StillMotion : public ScreenItem
{
protected:
int numframes; // How many frames
int curframe; // Current frame
HBITMAP frames[8]; // Array of handles to frame - 8 is arbitrary maximum
public:
// Our constructor and destructor
StillMotion( char *res, int ix, int iy, int nframes, int per);
~StillMotion( void);
// Switch frames
BOOL Update( void);
};
// A convenience function which creates a new StillMotion object. Because we
// chain and know object types, we can create objects without explictly
// keeping track of them. We just turm 'em loose!
StillMotion * AddMotion( char *res, int ix = 0, int iy = 0, int nframes = 2, int per = 4);
// Wall is about the simplest of all the ScreenItem subclasses; no motion
class Wall : public ScreenItem
{
protected:
ScreenItem * lastmem;
public:
int dir; // Direction; vertical or horizontal
int mems; // How many times is the bitmap repeated
// The constructor and destructor
Wall( char *res, int ix, int iy, int len, int vert);
~Wall( void);
virtual BOOL Update(void) {return FALSE;}
};
// A convenience function for creating Walls
Wall * AddWall( char *res, int ix = 0, int iy = 0, int len = 1, int vert = 1);
// A couple of functions to find all the Walls
Wall * FirstWall( void);
Wall * NextWall( void);
// A missile, bullet or some other hazardous projectile
class Missile : public ScreenItem
{
public:
// The constructor is simple because it remains hidden until Fired
Missile( char *res);
// We over ride this so as to respond NULL if not in use
virtual int Type( void);
BOOL inuse; // Is the missile in use (Fired and in motion)
// Our timer tick update
virtual BOOL Update( void);
// Fire the missile from a starting point to a target
void Fire( int sx, int sy, int tx, int ty);
};
// Player - the object responding to user inputs and running about
class Player : public ScreenItem
{
protected:
int state; // It's a bit more of a state machine
HBITMAP Items[16]; // A lot of possible images
int jmptop; // If jumping, what's the top of the jump
int jmpadj; // How much to move on Updates during jump
int jmpdir; // Are we still on the way up?
BOOL onrungs; // Maybe we are climbing a ladder...
Missile * shots[3];// Gotta have some fire power to shoot the Creepers
BOOL still; // Are we sitting still - this allows us to move slow
public:
// Our constructor and destructor
Player( char *res, int ix = 0, int iy = 0);
~Player( void);
// Fire a missile
virtual void Fire();
// Our update has a param; the player needs to know what the user is
// directing him to do
virtual BOOL Update( int keystate=0);
// If somebody or something hits us, it will let us know
virtual BOOL Hit( int type);
};
// A Creeper is a baddie that patrols a Wall, moving along it constantly
class Creeper : public ScreenItem
{
protected:
BOOL live; // Still alive (not shot by missile)
int state; // We're an unsophisticated state machine
HBITMAP frames[3]; // Only three frames, 2 for motion, 1 for dead
int bx; // Left edge of our wall
int ex; // Right edge of our wall
public:
// Our constructor and destructor
Creeper( char *res, Wall *wall, int ibx, int iex);
~Creeper(void);
// Good old update...
virtual BOOL Update();
// Type return will vary if alive or dead
virtual int Type();
// If we get hit, we want to know about it
virtual BOOL Hit( int type);
};
// Convenience function for creating Creepers
Creeper * AddCreeper( char *res, Wall *wall, int ibx=-1, int iex=-1);
// A Door leads to another scene
class Door : public ScreenItem
{
public:
Door( char *res, int ix, int iy, long nfo) : ScreenItem(res,ix,iy)
{ SOType = TYPE_DOOR; Info = nfo;}
virtual BOOL Update() { return FALSE;}
};
// Convenience function for adding Doors
Door * AddDoor( char *res, int ix, int iy, long nfo);
// A Prize - something of value?
class Prize : public ScreenItem
{
public:
Prize( char *res, int ix, int iy, long nfo) : ScreenItem(res,ix,iy)
{ SOType = TYPE_PRIZE; Info = nfo;}
virtual BOOL Update() { return FALSE;}
};
// Convenience function for adding prizes
Prize * AddPrize( char *res, int ix, int iy, long nfo);
// Rungs are Walls that can be climbed by Player
class Rungs : public Wall
{
public:
Rungs( char *res, int ix, int iy, int len, int vert);
};
// Convenience function for adding Rungs
Rungs * AddRungs(char *res, int ix = 0, int iy = 0, int len = 1, int vert = 1);