home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / ams__l~1.zoo / include / sprite.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-25  |  6.9 KB  |  297 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 acknowledge
  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. //  Sprites - small objects, double buffered.
  16. //
  17.  
  18. #include <stdio.h>
  19. #include <bool.h>
  20. #include "DoubleBuffer.h"
  21.  
  22. class Incarnation
  23. {
  24. public:
  25.     // Set hot spot (default (0,0))
  26.     void        SetHotSpot(short x, short y);
  27.  
  28.     // I/O
  29.     virtual int    fput(FILE *fp);
  30.  
  31.     // Draw/Wipe image from double buffer page.  Coordinates are in pixels.
  32.     virtual void    Draw(short x, short y, long *Store)=0;
  33.     virtual int    TouchDraw(short x, short y, long *Store)=0; // not implemented
  34.     virtual void    Wipe(long *Store)=0;
  35.  
  36.     // Take the area at (x,y) on the given screen as the image.
  37.     // x must be multiple of 16.
  38.     virtual void    GetImage(Screen&, int x, int y)=0;
  39.  
  40.     // Return width and height of the incarnation.
  41.     // Width is actual pixel width from the left.
  42.     short    Width() { return width; }
  43.     short    Height() { return height; }
  44.  
  45.     // Words of backing store used by the incarnmation (used by Sprite)
  46.     int    BackingRequired() { return Backing; }
  47.  
  48. protected:
  49.     Incarnation(FILE *fp);
  50.     Incarnation(short h, int b);
  51.     short    width,height; // In pixels
  52.     short    HotX,HotY;
  53.     int    Backing;
  54. };
  55.  
  56.  
  57. class Sprite
  58. {
  59. public:
  60.     // Constructors:
  61.  
  62.     //  A sprite with one shape.
  63.     Sprite(Incarnation *OnlyOne);
  64.  
  65.     //  A sprite with an array of shapes.
  66.     Sprite(Incarnation **ListOfThem,int Count);
  67.  
  68.     //  A sprite with a list of (undefined) shapes.
  69.     Sprite(short maxinca);
  70.  
  71.     //  A sprite with the same shapes as another sprite.
  72.     Sprite(Sprite& Copy);
  73.  
  74.     //  A sprite from a file.
  75.     Sprite(const char *filename);
  76.     Sprite(FILE *);
  77.  
  78.     // Destructor
  79.     ~Sprite();
  80.  
  81.     // I/O
  82.     int        Load(const char *filename);
  83.     int        Save(const char *filename);
  84.  
  85.     int        fput(FILE *);
  86.     int        fget(FILE *);
  87.  
  88.     // Draw on current page
  89.     void        Draw();
  90.     int        TouchDraw(); // not implemented
  91.     // Remove from page.  If already wiped, does nothing.
  92.     void        Wipe();
  93.  
  94.     // Choose incarnation, starting from 0.
  95.     void        ShapeTo(short);
  96.  
  97.     // Scale of pixels to coords.  Coord = 2**Scale * Pixel
  98.     // All values below are scaled by this amount
  99.     void        Scale(short s) { Shift=s; }
  100.  
  101.     // Move
  102.     void        MoveTo(int x, int y);
  103.     void        MoveBy(int x, int y);
  104.  
  105.     // Set one of the possible shapes to a given Incarnation
  106.     void        SetImage(int i, Incarnation* In);
  107.  
  108.     // Inspectors
  109.     int X() { return x; }
  110.     int Y() { return y; }
  111.     int Shape() { return shape; }
  112.     int Width() { return Inca[shape]->Width() << Shift; }
  113.     int Height() { return Inca[shape]->Height() << Shift; }
  114.  
  115. protected:
  116.     int    x,y;
  117.     short shape;
  118.     short Shift;
  119.  
  120.     long *BackingStore[2];
  121.     unsigned int BackingSize;
  122.     char *OverlayMask;
  123.  
  124.     short MaxInca;
  125.     Incarnation **Inca; // Dynamically allocated array of *Incarnation
  126.  
  127. private:
  128.     bool ExternalInca;
  129. };
  130.  
  131.  
  132. /////////////////////////////
  133. //
  134. //  Below are all the derived Incarnations,
  135. //  see main comment for details.
  136. //
  137. /////////////////////////////
  138.  
  139.  
  140. class MonochromeIncarnation : public Incarnation
  141. {
  142. public:
  143.     MonochromeIncarnation(int height);
  144.     virtual ~MonochromeIncarnation();
  145.  
  146.     virtual void    Draw(short x, short y, long *Store);
  147.     virtual int    TouchDraw(short x, short y, long* Store);
  148.     virtual void    Wipe(long *Store);
  149.  
  150.     virtual int    fput(FILE *fp);
  151.  
  152.     virtual void    GetImage(Screen&, int x, int y);
  153.  
  154. private:
  155.     friend Incarnation* IncarnationReader(FILE *fp);
  156.     MonochromeIncarnation(FILE*);
  157.     short unsigned *Data,*Mask;
  158. };
  159.  
  160.  
  161. class WideMonochromeIncarnation : public Incarnation
  162. {
  163. public:
  164.     WideMonochromeIncarnation(int height);
  165.     virtual ~WideMonochromeIncarnation();
  166.  
  167.     virtual void    Draw(short x, short y, long *Store);
  168.     virtual int    TouchDraw(short x, short y, long* Store);
  169.     virtual void    Wipe(long *Store);
  170.  
  171.     virtual int    fput(FILE *fp);
  172.  
  173.     virtual void    GetImage(Screen&, int x, int y);
  174.  
  175. private:
  176.     friend Incarnation* IncarnationReader(FILE *fp);
  177.     WideMonochromeIncarnation(FILE*);
  178.     unsigned long *Data,*Mask;
  179. };
  180.  
  181.  
  182. class PreshiftedMonochromeIncarnation : public Incarnation
  183. {
  184. public:
  185.     PreshiftedMonochromeIncarnation(int height);
  186.     virtual ~PreshiftedMonochromeIncarnation();
  187.  
  188.     virtual void    Draw(short x, short y, long *Store);
  189.     virtual int    TouchDraw(short x, short y, long* Store);
  190.     virtual void    Wipe(long *Store);
  191.  
  192.     virtual int    fput(FILE *fp);
  193.  
  194.     virtual void    GetImage(Screen&, int x, int y);
  195.  
  196. private:
  197.     friend Incarnation* IncarnationReader(FILE *fp);
  198.     PreshiftedMonochromeIncarnation(FILE*);
  199.     unsigned long *Data[16],*Mask[16];
  200. };
  201.  
  202.  
  203.  
  204. class WideColourIncarnation : public Incarnation
  205. {
  206. public:
  207.     WideColourIncarnation(int height);
  208.     virtual ~WideColourIncarnation();
  209.  
  210.     virtual void    Draw(short x, short y, long *Store);
  211.     virtual int    TouchDraw(short x, short y, long* Store);
  212.     virtual void    Wipe(long *Store);
  213.  
  214.     virtual int    fput(FILE *fp);
  215.  
  216.     virtual void    GetImage(Screen&, int x, int y);
  217.  
  218. private:
  219.     friend Incarnation* IncarnationReader(FILE *fp);
  220.     WideColourIncarnation(FILE*);
  221.     long unsigned *Data,*Mask;
  222. };
  223.  
  224. class ColourIncarnation : public Incarnation
  225. {
  226. public:
  227.     ColourIncarnation(int height);
  228.     virtual ~ColourIncarnation();
  229.  
  230.     virtual void    Draw(short x, short y, long *Store);
  231.     virtual int    TouchDraw(short x, short y, long* Store);
  232.     virtual void    Wipe(long *Store);
  233.  
  234.     virtual int    fput(FILE *fp);
  235.  
  236.     virtual void    GetImage(Screen&, int x, int y);
  237.  
  238. private:
  239.     friend Incarnation* IncarnationReader(FILE *fp);
  240.     ColourIncarnation(FILE*);
  241.     short unsigned *Data,*Mask;
  242. };
  243.  
  244. class PreshiftedColourIncarnation : public Incarnation
  245. {
  246. public:
  247.     PreshiftedColourIncarnation(int height);
  248.     virtual ~PreshiftedColourIncarnation();
  249.  
  250.     virtual void    Draw(short x, short y, long *Store);
  251.     virtual int    TouchDraw(short x, short y, long* Store);
  252.     virtual void    Wipe(long *Store);
  253.  
  254.     virtual int    fput(FILE *fp);
  255.  
  256.     virtual void    GetImage(Screen&, int x, int y);
  257.  
  258. private:
  259.     friend Incarnation* IncarnationReader(FILE *fp);
  260.     PreshiftedColourIncarnation(FILE*);
  261.     short unsigned *Data[16],*Mask[16];
  262. };
  263.  
  264.  
  265. // NOTE: TrueColourIncarnations must be initialized to an
  266. //        image always and only BEFORE being added to a sprite,
  267. //        otherwise backingstore requirements cannot be known.
  268. class TrueColourIncarnation : public Incarnation
  269. {
  270. public:
  271.     TrueColourIncarnation(int width, int height);
  272.     virtual ~TrueColourIncarnation();
  273.  
  274.     virtual void Draw(short x, short y, long *Store);
  275.     virtual int TouchDraw(short x, short y, long* Store);
  276.     virtual void Wipe(long *Store);
  277.  
  278.     virtual int fput(FILE *fp);
  279.  
  280.     virtual void GetImage(Screen&, int x, int y);
  281.  
  282. private:
  283.     friend Incarnation* IncarnationReader(FILE *fp);
  284.     TrueColourIncarnation(FILE*);
  285.     short unsigned *Data;
  286.     int DataLength;
  287. };
  288.  
  289.  
  290. // Some inlines for efficiency...
  291.  
  292. inline void    Sprite::MoveTo(int X, int Y) { x=X; y=Y; }
  293. inline void    Sprite::MoveBy(int X, int Y) { x+=X; y+=Y; }
  294. inline void    Sprite::ShapeTo(short s) { shape=s; }
  295.  
  296. #endif // !Sprite_h
  297.