home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / gchsrc31.lzh / INCLUDE / SPRITE.H < prev   
C/C++ Source or Header  |  1992-04-27  |  10KB  |  365 lines

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  This file is part of the Atari Machine Specific Library,
  4. //  and is Copyright 1992 by Warwick W. Allison.
  5. //
  6. //  You are free to copy and modify these sources, provided you acknoledge
  7. //  the origin by retaining this notice, and adhere to the conditions
  8. //  described in the file COPYING.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11.  
  12. #ifndef _Sprite_h
  13. #define _Sprite_h
  14. //////////////////////////////////////////////////////////////////////////////
  15. //
  16. //  Sprites
  17. //  -------
  18. //
  19. //  Small objects, double buffered.
  20. //
  21. //  Sprites have position (x,y), and shape (an index into a list of
  22. //  Incarnations).  They can be drawn, wiped, moved and reshaped.
  23. //
  24. //  Incarnations are positionless images that can be drawn and wiped.
  25. //  They come in many forms, each tuned to give certain qualities:
  26. //
  27. //    class Incarnation
  28. //        Base class from which others are derived.
  29. //
  30. //    class MonochromeIncarnation
  31. //        16 pixels wide (any height), monochrome image (For STHigh and TTHigh).
  32. //
  33. //    class PreshiftedMonochromeIncarnation
  34. //        16 pixels wide, Faster than regular images, but use 32x memory.
  35. //
  36. //    class WideMonochromeIncarnation
  37. //        32 pixels wide, monochrome image.
  38. //
  39. //    class ColourIncarnation
  40. //        16 pixels wide, 16 colour (for STLow and TTMedium).
  41. //
  42. //    class PreshiftedColourIncarnation
  43. //        16 pixels wide, Faster than regular colour sprites.
  44. //
  45. //    class WideColourIncarnation
  46. //        32 pixels wide.
  47. //
  48. //////////////////////////////////////////////////////////////////////////////
  49.  
  50. #include <stdio.h>
  51. #include <bool.h>
  52. #include "DoubleBuffer.h"
  53.  
  54. class Incarnation
  55. {
  56. public:
  57.     // Set hot spot (default (0,0))
  58.     void        SetHotSpot(short x, short y);
  59.  
  60.     // I/O
  61.     virtual int    fput(FILE *fp);
  62.  
  63.     // Draw/Wipe image from double buffer page.  Coordinates are in pixels.
  64.     virtual void    Draw(short x, short y, long *Store)=0;
  65.     virtual int    TouchDraw(short x, short y, long *Store)=0; // not implemented
  66.     virtual void    Wipe(long *Store)=0;
  67.  
  68.     // Take the area at (x,y) on the given screen as the image.
  69.     // x must be multiple of 16.
  70.     virtual void    GetImage(Screen&, int x, int y)=0;
  71.  
  72.     // Return width and height of the incarnation.
  73.     // Width is actual pixel width from the left.
  74.     short    Width() { return width; }
  75.     short    Height() { return height; }
  76.  
  77.     // Words of backing store used by the incarnmation (used by Sprite)
  78.     int    BackingRequired() { return Backing; }
  79.  
  80. protected:
  81.     Incarnation(FILE *fp);
  82.     Incarnation(short h, int b);
  83.     short    width,height; // In pixels
  84.     short    HotX=0,HotY=0;
  85.     int    Backing;
  86. };
  87.  
  88.  
  89. class Sprite
  90. {
  91. public:
  92.     // Constructors:
  93.  
  94.     //  A sprite with one shape.
  95.     Sprite(Incarnation *OnlyOne);
  96.  
  97.     //  A sprite with an array of shapes.
  98.     Sprite(Incarnation **ListOfThem,int Count);
  99.  
  100.     //  A sprite with a list of (undefined) shapes.
  101.     Sprite(short maxinca);
  102.  
  103.     //  A sprite with the same shapes as another sprite.
  104.     Sprite(Sprite& Copy);
  105.  
  106.     //  A sprite from a file.
  107.     Sprite(const char *filename);
  108.     Sprite(FILE *);
  109.  
  110.     // Destructor
  111.     ~Sprite();
  112.  
  113.     // I/O
  114.     int        Load(const char *filename);
  115.     int        Save(const char *filename);
  116.  
  117.     int        fput(FILE *);
  118.     int        fget(FILE *);
  119.  
  120.     // Draw on current page
  121.     void        Draw();
  122.     int        TouchDraw(); // not implemented
  123.     // Remove from page.  If already wiped, does nothing.
  124.     void        Wipe();
  125.  
  126.     // Choose incarnation, starting from 0.
  127.     void        ShapeTo(short);
  128.  
  129.     // Scale of pixels to coords.  Coord = 2**Scale * Pixel
  130.     // All values below are scaled by this amount
  131.     void        Scale(short s) { Shift=s; }
  132.  
  133.     // Move
  134.     void        MoveTo(int x, int y);
  135.     void        MoveBy(int x, int y);
  136.  
  137.     // Set one of the possible shapes to a given Incarnation
  138.     void        SetImage(int i, Incarnation* In);
  139.  
  140.     // Inspectors
  141.     int        X() { return x; }
  142.     int        Y() { return y; }
  143.     int        Shape() { return shape; }
  144.     int        Width() { return Inca[shape]->Width() << Shift; }
  145.     int        Height() { return Inca[shape]->Height() << Shift; }
  146.  
  147. protected:
  148.     int    x,y;
  149.     short    shape;
  150.     short    Shift=0;
  151.  
  152.     long        *BackingStore[2];
  153.     unsigned int    BackingSize;
  154.     char        *OverlayMask;
  155.  
  156.     short    MaxInca;
  157.     Incarnation    **Inca; // Dynamically allocated array of *Incarnation
  158.  
  159. private:
  160.     bool ExternalInca;
  161. };
  162.  
  163.  
  164. //
  165. // Mobile Sprite - an experimental derived class of Sprite.
  166. //
  167. enum BoundaryEffect {Bounce, Wrap, Stop, Watch};
  168. const int HitNorth=1;
  169. const int HitSouth=2;
  170. const int HitEast=4;
  171. const int HitWest=8;
  172.  
  173. class MobileSprite : public Sprite
  174. {
  175. public:
  176.     MobileSprite(Incarnation **Them,int Count) : Sprite(Them,Count) {}
  177.     MobileSprite(Incarnation *OnlyOne) : Sprite(OnlyOne) {}
  178.     MobileSprite(short maxinca) : Sprite(maxinca) {}
  179.     MobileSprite(const char *filename) : Sprite(filename) {}
  180.  
  181.     void Accelerate(int, int);
  182.     bool Stationary() { return (!xi && !yi); }
  183.     void Speed(int, int);
  184.     void XSpeed(int);
  185.     void YSpeed(int);
  186.     int XSpeed();
  187.     int YSpeed();
  188.     void Range(int, int, int, int);
  189.  
  190.     short Move(); // Efftected Edges returned
  191.  
  192.     void Unmove();    // Just undo the move
  193.     void Rebound(short Walls);    // Undo and rebound against given walls
  194.  
  195.     void Frictate(short fric);
  196.     void AtBoundary(BoundaryEffect, short Bouncy=256);
  197.     void AtBoundary(BoundaryEffect North,
  198.             BoundaryEffect South,
  199.             BoundaryEffect East,
  200.             BoundaryEffect West, short Bouncy=256);
  201.  
  202. private:
  203.     int    xi=0,yi=0;
  204.     int    Bounciness=256;
  205.     int    MinX= -10000;
  206.     int    MinY= -10000;
  207.     int    MaxX= +10000;
  208.     int    MaxY= +10000;
  209.     BoundaryEffect AtEdge[4]={Watch,Watch,Watch,Watch};
  210. };
  211.  
  212.  
  213.  
  214. /////////////////////////////
  215. //
  216. //  Below are all the derived Incarnations,
  217. //  see main comment for details.
  218. //
  219. /////////////////////////////
  220.  
  221.  
  222. class MonochromeIncarnation : public Incarnation
  223. {
  224. public:
  225.     MonochromeIncarnation(int height);
  226.     virtual ~MonochromeIncarnation();
  227.  
  228.     virtual void    Draw(short x, short y, long *Store);
  229.     virtual int    TouchDraw(short x, short y, long* Store);
  230.     virtual void    Wipe(long *Store);
  231.  
  232.     virtual int    fput(FILE *fp);
  233.  
  234.     virtual void    GetImage(Screen&, int x, int y);
  235.  
  236. private:
  237.     friend Incarnation* IncarnationReader(FILE *fp);
  238.     MonochromeIncarnation(FILE*);
  239.     short unsigned *Data,*Mask;
  240. };
  241.  
  242.  
  243. class WideMonochromeIncarnation : public Incarnation
  244. {
  245. public:
  246.     WideMonochromeIncarnation(int height);
  247.     virtual ~WideMonochromeIncarnation();
  248.  
  249.     virtual void    Draw(short x, short y, long *Store);
  250.     virtual int    TouchDraw(short x, short y, long* Store);
  251.     virtual void    Wipe(long *Store);
  252.  
  253.     virtual int    fput(FILE *fp);
  254.  
  255.     virtual void    GetImage(Screen&, int x, int y);
  256.  
  257. private:
  258.     friend Incarnation* IncarnationReader(FILE *fp);
  259.     WideMonochromeIncarnation(FILE*);
  260.     unsigned long *Data,*Mask;
  261. };
  262.  
  263.  
  264. class PreshiftedMonochromeIncarnation : public Incarnation
  265. {
  266. public:
  267.     PreshiftedMonochromeIncarnation(int height);
  268.     virtual ~PreshiftedMonochromeIncarnation();
  269.  
  270.     virtual void    Draw(short x, short y, long *Store);
  271.     virtual int    TouchDraw(short x, short y, long* Store);
  272.     virtual void    Wipe(long *Store);
  273.  
  274.     virtual int    fput(FILE *fp);
  275.  
  276.     virtual void    GetImage(Screen&, int x, int y);
  277.  
  278. private:
  279.     friend Incarnation* IncarnationReader(FILE *fp);
  280.     PreshiftedMonochromeIncarnation(FILE*);
  281.     unsigned long *Data[16],*Mask[16];
  282. };
  283.  
  284.  
  285.  
  286. class WideColourIncarnation : public Incarnation
  287. {
  288. public:
  289.     WideColourIncarnation(int height);
  290.     virtual ~WideColourIncarnation();
  291.  
  292.     virtual void    Draw(short x, short y, long *Store);
  293.     virtual int    TouchDraw(short x, short y, long* Store);
  294.     virtual void    Wipe(long *Store);
  295.  
  296.     virtual int    fput(FILE *fp);
  297.  
  298.     virtual void    GetImage(Screen&, int x, int y);
  299.  
  300. private:
  301.     friend Incarnation* IncarnationReader(FILE *fp);
  302.     WideColourIncarnation(FILE*);
  303.     long unsigned *Data,*Mask;
  304. };
  305.  
  306. class ColourIncarnation : public Incarnation
  307. {
  308. public:
  309.     ColourIncarnation(int height);
  310.     virtual ~ColourIncarnation();
  311.  
  312.     virtual void    Draw(short x, short y, long *Store);
  313.     virtual int    TouchDraw(short x, short y, long* Store);
  314.     virtual void    Wipe(long *Store);
  315.  
  316.     virtual int    fput(FILE *fp);
  317.  
  318.     virtual void    GetImage(Screen&, int x, int y);
  319.  
  320. private:
  321.     friend Incarnation* IncarnationReader(FILE *fp);
  322.     ColourIncarnation(FILE*);
  323.     short unsigned *Data,*Mask;
  324. };
  325.  
  326. class PreshiftedColourIncarnation : public Incarnation
  327. {
  328. public:
  329.     PreshiftedColourIncarnation(int height);
  330.     virtual ~PreshiftedColourIncarnation();
  331.  
  332.     virtual void    Draw(short x, short y, long *Store);
  333.     virtual int    TouchDraw(short x, short y, long* Store);
  334.     virtual void    Wipe(long *Store);
  335.  
  336.     virtual int    fput(FILE *fp);
  337.  
  338.     virtual void    GetImage(Screen&, int x, int y);
  339.  
  340. private:
  341.     friend Incarnation* IncarnationReader(FILE *fp);
  342.     PreshiftedColourIncarnation(FILE*);
  343.     short unsigned *Data[16],*Mask[16];
  344. };
  345.  
  346.  
  347. // Some inlines for efficiency...
  348.  
  349. inline void    Sprite::MoveTo(int X, int Y) { x=X; y=Y; }
  350. inline void    Sprite::MoveBy(int X, int Y) { x+=X; y+=Y; }
  351. inline void    Sprite::ShapeTo(short s) { shape=s; }
  352.  
  353. inline void    MobileSprite::Range(int minx, int miny, int maxx, int maxy)
  354.             { MinX=minx; MaxX=maxx; MinY=miny; MaxY=maxy; }
  355. inline void    MobileSprite::Speed(int x, int y) { xi=x; yi=y; }
  356. inline void    MobileSprite::XSpeed(int x) { xi=x; }
  357. inline void    MobileSprite::YSpeed(int y) { yi=y; }
  358. inline int MobileSprite::XSpeed() { return xi; }
  359. inline int MobileSprite::YSpeed() { return yi; }
  360. inline void    MobileSprite::Accelerate(int x, int y) { xi+=x; yi+=y; }
  361. inline void    MobileSprite::Unmove() { MoveBy(-xi,-yi); }
  362. inline void    MobileSprite::Frictate(short f) { f=256-f; xi=xi*f/256; yi=yi*f/256; }
  363.  
  364. #endif // !Sprite_h
  365.