home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / owlsrc.pak / CELARRAY.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  2.3 KB  |  90 lines

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