home *** CD-ROM | disk | FTP | other *** search
/ Media Share 13 / mediashare_13.zip / mediashare_13 / ZIPPED / PROGRAM / WTJ9403.ZIP / BARNHART / SO.HPP < prev    next >
C/C++ Source or Header  |  1993-11-04  |  13KB  |  356 lines

  1.  
  2. // so.hpp  - Screen Objects header file
  3.  
  4. // Written by Andy Barnhart
  5.  
  6. // This header file defines constants, function prototypes and objects used in a
  7. //  simple game library demonstrating object oriented principles in graphics
  8. //  animation
  9.  
  10. // This seems to work for me. I hope it works for you.
  11. // No other warranty is expressed or implied.
  12.  
  13. // The system includes section
  14. #include <windows.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18. // These are the types of objects in the library
  19. // The base type returns the variant types, so that other objects have
  20. //  some idea what they are dealing with, even if they don't access
  21. //  subtype methods or variables.
  22. #define TYPE_PLAYER 1
  23. #define TYPE_WALL 2
  24. #define TYPE_MISSILE 3
  25. #define TYPE_DOOR 4
  26. #define TYPE_HAZARD 5
  27. #define TYPE_PLATFORM 6
  28. #define TYPE_PRIZE 7
  29. #define TYPE_RUNGS 8
  30.  
  31. // Player movements are in bit switches so we can get a multi directional event
  32. #define KEY_RIGHT 1
  33. #define KEY_LEFT 2
  34. #define KEY_UP 4
  35. #define KEY_DOWN 8
  36.  
  37. // Our X and Y size. This could be variable, but for our purposes (demo) a fixed
  38. //  size simplifies things. You will find some code about for sizing, though...
  39. #define GMX 640
  40. #define GMY 444
  41.  
  42. // This is a define I added to place platforms at a given level or floor. Nothing
  43. //   magic; just a little trial and error with my hero and his jumping ability.
  44. #define LEV(a) ((a * 74)-16)
  45.  
  46. // Prototypes for the non object related functions
  47. // I only have one screen and one scene in progress at a time, so I do it the
  48. //   old fashioned way.
  49. void InitScreen( HWND hw, HDC hd, HANDLE hi);
  50. void TermScreen( void);
  51. void SizeScreen( int x, int y);
  52. BOOL InitScene( int scene);
  53. void ShowScene( void);
  54. void DeleteScene( void);
  55.                        
  56. // The base class - ScreenObj
  57. class ScreenObj
  58.   {
  59.   protected:
  60.     ScreenObj *next;  // All the screen objects are linked together
  61.   public:
  62.     long Info;    // Info is dependant on object type, like score on a prize
  63.     BOOL shown;   // Is the object currently in a shown state
  64.     int SOType;   // What is it (from the type list above)
  65.     int x;        // Where is it?
  66.     int y;
  67.     int width;    // How big is the bitmap it comes from?
  68.     int wmult;    // How many time is the bitmap drawn?
  69.     int height;
  70.     int hmult; 
  71.     
  72.     // Some public object statics - That's OOP for globals ;^>  
  73.     //  but setting them up this way leaves the door open for multiple
  74.     //  windows and instances
  75.     static HDC hDC;
  76.     static HWND hWnd;
  77.     static HANDLE hInst;     
  78.     // The backdrop object is needed for every redraw so we want a short cut 
  79.     //   to find it for speed.
  80.     static ScreenObj *Background;
  81.                               
  82.     // Constructor and destructor do little; this object is largely boilerplate
  83.     //   for descendants                              
  84.     ScreenObj(int ix, int iy);
  85.     ~ScreenObj( void);  
  86.  
  87.     // The descendants implement type, but all ScreenObj's have the method
  88.     virtual int Type( void){return NULL;}                                 
  89.     
  90.     // Get pointer to next object
  91.     ScreenObj *Next(void){return next;}
  92.     
  93.     // Set the state to shown/hidden and paint the screen in its area
  94.     void Show( BOOL nCmdShow) {shown=nCmdShow;Paint();}       
  95.     
  96.     // Update is called on a timer tick so that animation can be
  97.     //   done by descendant objects as state machines
  98.     virtual BOOL Update( void ){return FALSE;}       
  99.     
  100.     // Paint the screen at the area the ScrnObj occupies, not necessarily
  101.     //   showing the object if it is covered or hidden
  102.     virtual void Paint( void){;}    
  103.     
  104.     // Bitblt into a DC the portion of your object in a given rectangle
  105.     virtual void Paint( RECT *rc, HDC hPaintDC = hDC, BOOL memdc = FALSE){;}
  106.     
  107.     // This is how other objects notify the object they run into
  108.     virtual BOOL Hit( int type) {return TRUE;}                  
  109.     
  110.     // Test to see if we are in a given rectangle 
  111.     BOOL InRect( RECT *rc)
  112.       {
  113.       if( ( x > rc->right) || (y > rc->bottom) ||
  114.         ( (x+(width * wmult)) < rc->left) ||
  115.         ( (y+(height * hmult)) < rc->top) ) return( FALSE);
  116.         return( TRUE);
  117.       }
  118.       
  119.     // Fill a RECT struct with our coordinates
  120.     void SetRect( RECT *rc)
  121.       {
  122.       rc->left = x;
  123.       rc->right = x + width;
  124.       rc->top = y;
  125.       rc->bottom = y + height;
  126.       }
  127.       
  128.     // Move the object - not to be confused with setting a target
  129.     //  (discussed later) for animation; this is an instant move
  130.     void Move( int newx, int newy)
  131.       {
  132.       if( shown)
  133.         {
  134.         Show(FALSE);
  135.         x = newx;
  136.         y = newy;
  137.         Show(TRUE);
  138.         }
  139.       else
  140.         {
  141.         x = newx;
  142.         y = newy;
  143.         }
  144.       }
  145.       
  146.      // Move an object up by its own height
  147.     void OnTop( int newy)
  148.       {
  149.       Move( x, newy - height);
  150.       }
  151.   };                                       
  152.  
  153. // Calculate the distance between two objects
  154. int SODist( ScreenObj * SO1, ScreenObj * SO2);
  155.                        
  156. // The Screen background object only needs paint, but could certainly be
  157. //   extended to include bitmap assignment (hint, hint...)                       
  158. class ScreenBack : public ScreenObj
  159.   {
  160.   public:
  161.     ScreenBack( int ix, int iy);
  162.     virtual void Paint( RECT *rc, HDC hPaintDC = hDC, BOOL memdc = FALSE);
  163.   };
  164.                                                           
  165. // Screen Item - The subtype that is the base for all the actual game pieces
  166. //   with common properies such as bitmap images, speed and direction for
  167. //   movement, etc.                                                          
  168. class ScreenItem : public ScreenObj
  169.   {
  170.   protected:       
  171.     HBITMAP hbmImage;   // The image currently displayed
  172.     int oldx;           // Where we were at the beginning of a single motion
  173.     int oldy;
  174.     int targetx;        // Where we are headed (may take a few motions)
  175.     int targety;
  176.     int period;         // How often to update (every Nth timer event)
  177.     int periodct;       // How many ticks since last update
  178.     int xinc;           // How far to move on each update
  179.     int yinc;
  180.     HANDLE hbits;       // Bitmap handling caca
  181.     BYTE far * bits;
  182.     int linewidth;
  183.     int planes;
  184.     int bitslen;
  185.   public:                                      
  186.     // Our constructor; note that we get a resource name and movement amounts
  187.     ScreenItem( char *res, int ix = 0, int iy = 0, int incx = 24, int incy = 24);
  188.     
  189.     // Our destructor
  190.     ~ScreenItem( void);
  191.     
  192.     // At the ScreenItem level, type is relevant
  193.     virtual int Type( void){return SOType;}     
  194.     
  195.     // Paint functions
  196.     void Paint( void);
  197.     void Paint( RECT *rc, HDC hPaintDC = hDC, BOOL memdc = FALSE);
  198.     
  199.     // Select current bitmap for item
  200.     void SelectBM ( HBITMAP hb);     
  201.     
  202.     // Update item (on timer tick)
  203.     BOOL Update( void);
  204.     
  205.     // Set destination to begin moving toward on Update calls
  206.     void SetTarget( int tx, int ty);
  207.   };
  208.                                               
  209. // StllMotion - a conflict in terms? No; this is for hazards like fire which
  210. //   are fixed at a location but still are animated in place
  211. class StillMotion : public ScreenItem
  212.   {
  213.   protected:
  214.     int numframes;     // How many frames
  215.     int curframe;      // Current frame
  216.     HBITMAP frames[8]; // Array of handles to frame - 8 is arbitrary maximum
  217.   public:                                                                   
  218.     // Our constructor and destructor
  219.     StillMotion( char *res, int ix, int iy, int nframes, int per);
  220.     ~StillMotion( void);             
  221.                                   
  222.     // Switch frames
  223.     BOOL Update( void);
  224.   };
  225.                     
  226. // A convenience function which creates a new StillMotion object. Because we 
  227. //   chain and know object types, we can create objects without explictly 
  228. //   keeping track of them. We just turm 'em loose!
  229. StillMotion * AddMotion( char *res, int ix = 0, int iy = 0, int nframes = 2, int per = 4);
  230.  
  231. // Wall is about the simplest of all the ScreenItem subclasses; no motion
  232. class Wall : public ScreenItem
  233. {
  234.   protected:
  235.     ScreenItem * lastmem;
  236.   public:
  237.     int dir;  // Direction; vertical or horizontal
  238.     int mems; // How many times is the bitmap repeated
  239.     
  240.     // The constructor and destructor
  241.     Wall( char *res, int ix, int iy, int len, int vert);
  242.     ~Wall( void);
  243.     virtual BOOL Update(void) {return FALSE;}
  244.   };
  245.  
  246. // A convenience function for creating Walls
  247. Wall * AddWall( char *res, int ix = 0, int iy = 0, int len = 1, int vert = 1);
  248.  
  249. // A couple of functions to find all the Walls
  250. Wall * FirstWall( void);
  251. Wall * NextWall( void);
  252.                                               
  253. // A missile, bullet or some other hazardous projectile                                              
  254. class Missile : public ScreenItem
  255.   {
  256.   public:
  257.     // The constructor is simple because it remains hidden until Fired
  258.     Missile( char *res);                                              
  259.     // We over ride this so as to respond NULL if not in use
  260.     virtual int Type( void);
  261.     BOOL inuse;  // Is the missile in use (Fired and in motion)
  262.     // Our timer tick update
  263.     virtual BOOL Update( void);
  264.     // Fire the missile from a starting point to a target
  265.     void Fire( int sx, int sy, int tx, int ty);
  266.   };
  267.  
  268. // Player - the object responding to user inputs and running about
  269. class Player : public ScreenItem
  270.   {
  271.   protected:
  272.     int state;         // It's a bit more of a state machine
  273.     HBITMAP Items[16]; // A lot of possible images
  274.     int jmptop;        // If jumping, what's the top of the jump
  275.     int jmpadj;        // How much to move on Updates during jump
  276.     int jmpdir;        // Are we still on the way up?
  277.     BOOL onrungs;      // Maybe we are climbing a ladder...
  278.     Missile * shots[3];// Gotta have some fire power to shoot the Creepers
  279.     BOOL still;        // Are we sitting still - this allows us to move slow
  280.   public:                                                                   
  281.     // Our constructor and destructor
  282.     Player( char *res, int ix = 0, int iy = 0);
  283.     ~Player( void);                  
  284.  
  285.     // Fire a missile
  286.     virtual void Fire();
  287.     
  288.     // Our update has a param; the player needs to know what the user is
  289.     //  directing him to do
  290.     virtual BOOL Update( int keystate=0);
  291.     
  292.     // If somebody or something hits us, it will let us know
  293.     virtual BOOL Hit( int type);
  294.   };
  295.  
  296. // A Creeper is a baddie that patrols a Wall, moving along it constantly
  297. class Creeper : public ScreenItem
  298.   {
  299.   protected:
  300.     BOOL live;          // Still alive (not shot by missile)
  301.     int state;          // We're an unsophisticated state machine
  302.     HBITMAP frames[3];  // Only three frames, 2 for motion, 1 for dead
  303.     int bx;             // Left edge of our wall
  304.     int ex;             // Right edge of our wall
  305.   public:                                        
  306.     // Our constructor and destructor
  307.     Creeper( char *res, Wall *wall, int ibx, int iex);
  308.     ~Creeper(void);                  
  309.     
  310.     // Good old update...
  311.     virtual BOOL Update();
  312.     
  313.     // Type return will vary if alive or dead
  314.     virtual int Type();                      
  315.     
  316.     // If we get hit, we want to know about it
  317.     virtual BOOL Hit( int type);
  318.   };
  319.                                               
  320. // Convenience function for creating Creepers                                              
  321. Creeper * AddCreeper( char *res, Wall *wall, int ibx=-1, int iex=-1);
  322.                                              
  323. // A Door leads to another scene                                             
  324. class Door : public ScreenItem
  325.   {
  326.   public:
  327.     Door( char *res, int ix, int iy, long nfo) : ScreenItem(res,ix,iy)
  328.         { SOType = TYPE_DOOR; Info = nfo;}
  329.     virtual BOOL Update() { return FALSE;}
  330. };
  331.                                 
  332. // Convenience function for adding Doors                                
  333. Door * AddDoor( char *res, int ix, int iy, long nfo);
  334.                                         
  335. // A Prize - something of value?                                        
  336. class Prize : public ScreenItem
  337.   {
  338.   public:
  339.     Prize( char *res, int ix, int iy, long nfo) : ScreenItem(res,ix,iy)
  340.         { SOType = TYPE_PRIZE; Info = nfo;}
  341.     virtual BOOL Update() { return FALSE;}
  342.   };
  343.                                 
  344. // Convenience function for adding prizes
  345. Prize * AddPrize( char *res, int ix, int iy, long nfo);
  346.                                          
  347. // Rungs are Walls that can be climbed by Player                                         
  348. class Rungs : public Wall
  349.   {
  350.   public:
  351.     Rungs( char *res, int ix, int iy, int len, int vert);
  352.   };
  353.                                                 
  354. // Convenience function for adding Rungs                                                
  355. Rungs * AddRungs(char *res, int ix = 0, int iy = 0, int len = 1, int vert = 1);
  356.