home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / atl / atlfire / firewnd.h < prev    next >
C/C++ Source or Header  |  1998-03-26  |  4KB  |  153 lines

  1. // FireWnd.h : Declaration of the CFireWnd
  2. //
  3. // This is a part of the Active Template Library.
  4. // Copyright (C) 1996-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Active Template Library Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Active Template Library product.
  12.  
  13. #ifndef __FIREWND_H_
  14. #define __FIREWND_H_
  15.  
  16. #include "resource.h"       // main symbols
  17. #include "size.h"
  18. #include "propdlg.h"
  19.  
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CFireWnd
  22.  
  23. class CFireWnd :
  24.     public CDialogImpl<CFireWnd>
  25. {
  26.  
  27. public:
  28.  
  29.     CFireWnd();
  30.     ~CFireWnd();
  31.  
  32.     enum { IDD = IDD_FIREWND };
  33.  
  34. BEGIN_MSG_MAP(CFireWnd)
  35.     MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
  36.     MESSAGE_HANDLER(WM_PAINT, OnPaint)
  37.     MESSAGE_HANDLER(WM_RBUTTONDOWN, OnRButtonDown)
  38.     MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
  39.     MESSAGE_HANDLER(WM_PALETTECHANGED, OnPaletteChanged)
  40.     MESSAGE_HANDLER(WM_QUERYNEWPALETTE, OnQueryNewPalette)
  41.     MESSAGE_HANDLER(WM_TIMER, OnTimer)
  42.     MESSAGE_HANDLER(WM_SIZE, OnSize)
  43.     COMMAND_ID_HANDLER(IDM_PROPERTIES, OnProperties)
  44. END_MSG_MAP()
  45.  
  46. public:
  47.     // Message handlers
  48.     LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  49.     LRESULT OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  50.     LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  51.     LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  52.     LRESULT OnPaletteChanged(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  53.     LRESULT OnQueryNewPalette(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  54.     LRESULT OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  55.     LRESULT OnProperties(WORD wNotifyCode, WORD wID, HWND hWndCtlr, BOOL& bHandled);
  56.     LRESULT OnRButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  57.  
  58.     // Fire Attributes
  59.  
  60.     long m_nDecay;
  61.     long m_nFlammability;
  62.     long m_nMaxHeat;
  63.     long m_nSpreadRate;
  64.     long m_nSize;
  65.     long m_nSmoothness;
  66.     long m_nDistribution;
  67.     long m_nChaos;
  68.     long m_curColor;
  69.     long m_MaxBurn;
  70.     UINT m_uTimerID;
  71.  
  72.     CSize rcSize;   // the size of the rect cached from SetObjectRects
  73.     CSize bmSize;   // bitmap size cx = width, cy = height
  74. protected:
  75.  
  76.     HMENU m_hRightMenu;
  77.     HMENU m_hPropMenu;
  78.     BYTE* m_Fire;
  79.  
  80.     // Seed used by faster Rand().
  81.     static unsigned long m_RandSeed;
  82.  
  83.     // Device Context Attributes
  84.     HDC m_hMemDC;
  85.     HDC m_hWinDC;
  86.  
  87.     // Palette Attributes
  88.     RGBQUAD m_rgbPalette[256];
  89.     HPALETTE m_hPalette;
  90.     HPALETTE m_pOldPalette;
  91.  
  92.     // Bitmap Attributes
  93.     HBITMAP m_hBitmap;
  94.     HBITMAP m_pOldBitmap;
  95.  
  96.     BYTE* m_pBits;
  97.  
  98. // Operations
  99. public:
  100.     enum { red = 1, green = 2, blue = 3 };
  101.     void InitFire(int nColor);
  102.     HPALETTE* GetPalette();
  103.     void RenderFlame();
  104.     void PaintFlame(HDC hDC = NULL);
  105.     void CreatePopup();
  106.     void CreatePalette(int nColor);
  107.  
  108. protected:
  109.     void CreateBitmap();
  110.     void BurnPoint(BYTE* pRow, BYTE* pNextRow);
  111.  
  112.     // This function replaces the crt lib rand() function.
  113.     // The CRT lib function is very slow.  Since rand() is
  114.     // one of the most frequently called functions it was
  115.     // necessary to optimize it.  This function may be
  116.     // inlined and is computationally simple.
  117.     unsigned long Rand();
  118.  
  119. };
  120.  
  121.  
  122. inline unsigned long CFireWnd::Rand()
  123. {
  124.     // Using the current seed, generate a new random value
  125.     // and seed and return it.  The random value is shifted
  126.     // to reduce some of the noise and produce a more
  127.     // realistic flame.
  128.     return (m_RandSeed = 1664525L * m_RandSeed + 1013904223L) >> 5;
  129. }
  130.  
  131. inline void CFireWnd::BurnPoint(BYTE* pRow, BYTE* pNextRow)
  132. {
  133.     BYTE* pTarget;
  134.  
  135.     int off = Rand() % (m_nDistribution + 1);
  136.  
  137.     int val = m_nDecay + 1;
  138.     val = Rand() % val;
  139.     val = *pNextRow - val;
  140.  
  141.     if (Rand() & 1)
  142.         pTarget = pRow + off;
  143.     else
  144.         pTarget = pRow - off;
  145.  
  146.     if (val > 16)
  147.         *pTarget = (BYTE)val;
  148.     else
  149.         *pTarget = 16;
  150. }
  151.  
  152. #endif //__FIREWND_H_
  153.