home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 11.ddi / OWLSRC.PAK / CELARRAY.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  2.2 KB  |  87 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1993 by Borland International
  3. //   source\owl\celarray.cpp
  4. //   Implementation of a bitmap Cel array class.
  5. //----------------------------------------------------------------------------
  6. #include <owl\owlpch.h>
  7. #include <owl\celarray.h>
  8.  
  9. //
  10. // Construct a TCelArray from a bitmap, slicing a portion of the bitmap up
  11. // into a horizontal array of specified sized cels.
  12. // The CelArray can own (& delete) the bitmap or not, determined by autoDelete
  13. //
  14. TCelArray::TCelArray(TBitmap* bmp, int numCels, TSize celSize,
  15.                      TPoint offset, TAutoDelete autoDelete)
  16. {
  17.   PRECONDITION(bmp);
  18.   Bitmap = bmp;
  19.   ShouldDelete = autoDelete == AutoDelete;
  20.   
  21.   NCels = numCels < 1 ? 1 : numCels;
  22.   CSize = celSize ? celSize : TSize(bmp->Width() / NCels, bmp->Height());
  23.   Offs = offset;
  24. }
  25.  
  26. //
  27. // Construct a TCelArray from a dib, slicing the dib up into a horizontal
  28. // array of even sized cels
  29. //
  30. TCelArray::TCelArray(TDib& dib, int numCels)
  31. {
  32.   Bitmap = new TBitmap(dib, &TPalette((HPALETTE)::GetStockObject(DEFAULT_PALETTE)));
  33.   ShouldDelete = TRUE;
  34.  
  35.   NCels = numCels < 1 ? 1 : numCels;
  36.   CSize = TSize(dib.Width() / NCels, dib.Height());
  37.   Offs = 0;
  38. }
  39.  
  40. //
  41. // Construct a TCelArray as a copy of an existing one. Copy Bitmap iff
  42. // original owned its Bitmap, else just keep a ref to it also.
  43. //
  44. TCelArray::TCelArray(const TCelArray& src)
  45. {
  46.   ShouldDelete = src.ShouldDelete;
  47.   Bitmap = ShouldDelete ? new TBitmap(*src.Bitmap) : src.Bitmap;
  48.   
  49.   NCels = src.NCels;
  50.   CSize =  src.CSize;
  51.   Offs = src.Offs;
  52. }
  53.  
  54. TCelArray::~TCelArray()
  55. {
  56.   if (ShouldDelete)
  57.     delete Bitmap;
  58. }
  59.  
  60. TCelArray&
  61. TCelArray::operator =(const TCelArray& src)
  62. {
  63.   if (ShouldDelete)
  64.     delete Bitmap;
  65.  
  66.   ShouldDelete = src.ShouldDelete;
  67.   Bitmap = ShouldDelete ? new TBitmap(*src.Bitmap) : src.Bitmap;
  68.   
  69.   NCels = src.NCels;
  70.   CSize =  src.CSize;
  71.   Offs = src.Offs;
  72.  
  73.   return *this;
  74. }
  75.  
  76. TPoint
  77. TCelArray::CelOffset(int cel) const
  78. {
  79.   return TPoint(Offs.x+cel*CSize.cx, Offs.y);
  80. }
  81.  
  82. TRect
  83. TCelArray::CelRect(int cel) const
  84. {
  85.   return TRect(TPoint(Offs.x+cel*CSize.cx, Offs.y), CSize);
  86. }
  87.