home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 October / Chip_2002-10_cd1.bin / zkuste / vbasic / Data / Utils / WMP71SDK.exe / WMEffect.awx / TEMPLATE / SAMPLE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-01  |  10.7 KB  |  370 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // $$root$$.cpp : Implementation of C$$Safe_root$$
  4. //
  5. /////////////////////////////////////////////////////////////////////////////
  6.  
  7. #include "stdafx.h"
  8. #include "i$$root$$.h"
  9. #include "$$root$$.h"
  10.  
  11.  
  12. /////////////////////////////////////////////////////////////////////////////
  13. // C$$Safe_root$$::C$$Safe_root$$
  14. // Constructor
  15.  
  16. C$$Safe_root$$::C$$Safe_root$$() :
  17. m_clrForeground(0x0000FF),
  18. m_nPreset(0)
  19. {
  20. }
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // C$$Safe_root$$::~C$$Safe_root$$
  24. // Destructor
  25.  
  26. C$$Safe_root$$::~C$$Safe_root$$()
  27. {
  28. }
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. // C$$Safe_root$$:::FinalConstruct
  32. // Called when an effect is first loaded. Use this function to do one-time
  33. // intializations that could fail (i.e. creating offscreen buffers) instead
  34. // of doing this in the constructor, which cannot return an error.
  35.  
  36. HRESULT C$$Safe_root$$::FinalConstruct()
  37. {
  38.     return S_OK;
  39. }
  40.  
  41. /////////////////////////////////////////////////////////////////////////////
  42. // C$$Safe_root$$:::FinalRelease
  43. // Called when an effect is unloaded. Use this function to free any
  44. // resources allocated in FinalConstruct.
  45.  
  46. void C$$Safe_root$$::FinalRelease()
  47. {
  48. }
  49.  
  50.  
  51. //////////////////////////////////////////////////////////////////////////////
  52. // C$$Safe_root$$::Render
  53. // Called when an effect should render itself to the screen.
  54. //////////////////////////////////////////////////////////////////////////////
  55. STDMETHODIMP C$$Safe_root$$::Render(TimedLevel *pLevels, HDC hdc, RECT *prc)
  56. {
  57.     // Fill background with black
  58.     HBRUSH hNewBrush = ::CreateSolidBrush( 0 );
  59.     HPEN hNewPen = ::CreatePen( PS_SOLID, 0, m_clrForeground );
  60.     HPEN hOldPen= static_cast<HPEN>(::SelectObject( hdc, hNewPen ));
  61.  
  62.     ::FillRect( hdc, prc, hNewBrush );
  63.     
  64.     // draw using the current preset
  65.     switch (m_nPreset)
  66.     {
  67.     case PRESET_BARS:
  68.         {
  69.             // Walk through the frequencies until we run out of levels or drawing surface.
  70.             for (int x = prc->left; x < prc->right && x < (SA_BUFFER_SIZE-1); ++x)
  71.             {
  72.                 int y = static_cast<int>(((prc->bottom - prc->top)/256.0f) * pLevels->frequency[0][x - (prc->left - 1)]);
  73.                 ::MoveToEx( hdc, x, prc->bottom, NULL );  
  74.                 ::LineTo(hdc, x, prc->bottom - y); 
  75.             }
  76.         }
  77.         break;
  78.  
  79.     case PRESET_SCOPE:
  80.         {
  81.             // Walk through the waveform data until we run out of samples or drawing surface.
  82.             int y = static_cast<int>(((prc->bottom - prc->top)/256.0f) * pLevels->waveform[0][0]);
  83.             ::MoveToEx( hdc, prc->left, y, NULL );  
  84.             for (int x = prc->left; x < prc->right && x < (SA_BUFFER_SIZE-1); ++x)
  85.             {
  86.                 y = static_cast<int>(((prc->bottom - prc->top)/256.0f) * pLevels->waveform[0][x - (prc->left - 1)]);
  87.                 ::LineTo(hdc, x, y); 
  88.             }
  89.         }
  90.         break;
  91.     }
  92.         
  93.     if (hNewBrush)
  94.     {
  95.         ::DeleteObject( hNewBrush );
  96.     }
  97.  
  98.     if (hNewPen)
  99.     {
  100.         ::SelectObject( hdc, hOldPen );
  101.         ::DeleteObject( hNewPen );
  102.     }
  103.     
  104.     return S_OK;
  105. }
  106.  
  107. //////////////////////////////////////////////////////////////////////////////
  108. // C$$Safe_root$$::MediaInfo
  109. // Everytime new media is loaded, this method is called to pass the
  110. // number of channels (mono/stereo), the sample rate of the media, and the
  111. // title of the media
  112. //////////////////////////////////////////////////////////////////////////////
  113. STDMETHODIMP C$$Safe_root$$::MediaInfo(LONG lChannelCount, LONG lSampleRate, BSTR bstrTitle )
  114. {
  115.     return S_OK;
  116. }
  117.  
  118.  
  119. //////////////////////////////////////////////////////////////////////////////
  120. // C$$Safe_root$$::GetCapabilities
  121. // Returns the capabilities of this effect. Flags that can be returned are:
  122. //    EFFECT_CANGOFULLSCREEN        -- effect supports full-screen rendering
  123. //    EFFECT_HASPROPERTYPAGE        -- effect supports a property page
  124. //////////////////////////////////////////////////////////////////////////////
  125. STDMETHODIMP C$$Safe_root$$::GetCapabilities(DWORD * pdwCapabilities)
  126. {
  127.     if (NULL == pdwCapabilities)
  128.     {
  129.         return E_POINTER;
  130.     }
  131.  
  132.     *pdwCapabilities = 0;
  133.     return S_OK;
  134. }
  135.  
  136. //////////////////////////////////////////////////////////////////////////////
  137. // C$$Safe_root$$::GetTitle
  138. // Invoked when a host wants to obtain the title of the effect
  139. //////////////////////////////////////////////////////////////////////////////
  140. STDMETHODIMP C$$Safe_root$$::GetTitle(BSTR* bstrTitle)
  141. {
  142.     USES_CONVERSION;
  143.  
  144.     if (NULL == bstrTitle)
  145.     {
  146.         return E_POINTER;
  147.     }
  148.  
  149.     CComBSTR bstrTemp;
  150.     bstrTemp.LoadString(IDS_EFFECTNAME); 
  151.         
  152.     if ((!bstrTemp) || (0 == bstrTemp.Length))
  153.     {
  154.         return E_FAIL;
  155.     }
  156.  
  157.     *bstrTitle = bstrTemp.Detach();
  158.  
  159.     return S_OK;
  160. }
  161.  
  162. //////////////////////////////////////////////////////////////////////////////
  163. // C$$Safe_root$$::GetPresetTitle
  164. // Invoked when a host wants to obtain the title of the given preset
  165. //////////////////////////////////////////////////////////////////////////////
  166. STDMETHODIMP C$$Safe_root$$::GetPresetTitle(LONG nPreset, BSTR *bstrPresetTitle)
  167. {
  168.     USES_CONVERSION;
  169.  
  170.     if (NULL == bstrPresetTitle)
  171.     {
  172.         return E_POINTER;
  173.     }
  174.  
  175.     if ((nPreset < 0) || (nPreset >= PRESET_COUNT))
  176.     {
  177.         return E_INVALIDARG;
  178.     }
  179.  
  180.     CComBSTR bstrTemp;
  181.     
  182.     switch (nPreset)
  183.     {
  184.     case PRESET_BARS:
  185.         bstrTemp.LoadString(IDS_BARSPRESETNAME); 
  186.         break;
  187.  
  188.     case PRESET_SCOPE:
  189.         bstrTemp.LoadString(IDS_SCOPEPRESETNAME); 
  190.         break;
  191.     }
  192.     
  193.     if ((!bstrTemp) || (0 == bstrTemp.Length))
  194.     {
  195.         return E_FAIL;
  196.     }
  197.  
  198.     *bstrPresetTitle = bstrTemp.Detach();
  199.  
  200.     return S_OK;
  201. }
  202.  
  203. //////////////////////////////////////////////////////////////////////////////
  204. // C$$Safe_root$$::GetPresetCount
  205. // Invoked when a host wants to obtain the number of supported presets
  206. //////////////////////////////////////////////////////////////////////////////
  207. STDMETHODIMP C$$Safe_root$$::GetPresetCount(LONG *pnPresetCount)
  208. {
  209.     if (NULL == pnPresetCount)
  210.     {
  211.         return E_POINTER;
  212.     }
  213.  
  214.     *pnPresetCount = PRESET_COUNT;
  215.  
  216.     return (S_OK);
  217. }
  218.  
  219. //////////////////////////////////////////////////////////////////////////////
  220. // C$$Safe_root$$::SetCurrentPreset
  221. // Invoked when a host wants to change the index of the current preset
  222. //////////////////////////////////////////////////////////////////////////////
  223. STDMETHODIMP C$$Safe_root$$::SetCurrentPreset(LONG nPreset)
  224. {
  225.     if ((nPreset < 0) || (nPreset >= PRESET_COUNT))
  226.     {
  227.         return E_INVALIDARG;
  228.     }
  229.  
  230.     m_nPreset = nPreset;
  231.  
  232.     return (S_OK);
  233. }
  234.  
  235. //////////////////////////////////////////////////////////////////////////////
  236. // C$$Safe_root$$::GetCurrentPreset
  237. // Invoked when a host wants to obtain the index of the current preset
  238. //////////////////////////////////////////////////////////////////////////////
  239. STDMETHODIMP C$$Safe_root$$::GetCurrentPreset(LONG *pnPreset)
  240. {
  241.     if (NULL == pnPreset)
  242.     {
  243.         return E_POINTER;
  244.     }
  245.  
  246.     *pnPreset = m_nPreset;
  247.  
  248.     return (S_OK);
  249. }
  250.  
  251. //////////////////////////////////////////////////////////////////////////////
  252. // C$$Safe_root$$::get_foregroundColor
  253. // Property get to retrieve the foregroundColor prop via the public interface.
  254. //////////////////////////////////////////////////////////////////////////////
  255. STDMETHODIMP C$$Safe_root$$::get_foregroundColor(BSTR *pVal)
  256. {
  257.     return ColorToWz( pVal, m_clrForeground);
  258. }
  259.  
  260.  
  261. //////////////////////////////////////////////////////////////////////////////
  262. // C$$Safe_root$$::put_foregroundColor
  263. // Property put to set the foregroundColor prop via the public interface.
  264. //////////////////////////////////////////////////////////////////////////////
  265. STDMETHODIMP C$$Safe_root$$::put_foregroundColor(BSTR newVal)
  266. {
  267.     return WzToColor(newVal, &m_clrForeground);
  268. }
  269.  
  270.  
  271. //////////////////////////////////////////////////////////////////////////////
  272. // C$$Safe_root$$::WzToColor
  273. // Helper function used to convert a string into a COLORREF.
  274. //////////////////////////////////////////////////////////////////////////////
  275. HRESULT C$$Safe_root$$::WzToColor(const WCHAR *pwszColor, COLORREF *pcrColor)
  276.     if (NULL == pwszColor)
  277.     {
  278.         //NULL color string passed in
  279.         return E_POINTER;
  280.     }
  281.  
  282.     if (0 == lstrlenW(pwszColor))
  283.     {
  284.         //Empty color string passed in
  285.         return E_INVALIDARG;
  286.     }
  287.  
  288.     if (NULL == pcrColor)
  289.     {
  290.         //NULL output color DWORD passed in
  291.         return E_POINTER;
  292.     }
  293.     
  294.     if (lstrlenW(pwszColor) != 7)
  295.     {
  296.         //hex color string is not of the correct length
  297.         return E_INVALIDARG;
  298.     }
  299.  
  300.     DWORD dwRet = 0;
  301.     for (int i = 1; i < 7; i++)
  302.     {
  303.         // shift dwRet by 4
  304.         dwRet <<= 4;
  305.         // and add in the value of this string
  306.  
  307.         if ((pwszColor[i] >= L'0') && (pwszColor[i] <= L'9'))
  308.         {
  309.             dwRet += pwszColor[i] - '0';
  310.         }
  311.         else if ((pwszColor[i] >= L'A') && (pwszColor[i] <= L'F'))
  312.         {
  313.             dwRet += 10 + (pwszColor[i] - L'A');
  314.         }
  315.         else if ((pwszColor[i] >= L'a') && (pwszColor[i] <= L'f'))
  316.         {
  317.             dwRet += 10 + (pwszColor[i] - L'a');
  318.         }
  319.         else
  320.         {
  321.            //Invalid hex digit in color string
  322.             return E_INVALIDARG;
  323.         }
  324.     }
  325.  
  326.     *pcrColor = SwapBytes(dwRet);
  327.  
  328.     return S_OK;
  329.  
  330.  
  331. //////////////////////////////////////////////////////////////////////////////
  332. // C$$Safe_root$$::ColorToWz
  333. // Helper function used to convert a COLORREF to a BSTR.
  334. //////////////////////////////////////////////////////////////////////////////
  335. HRESULT C$$Safe_root$$::ColorToWz( BSTR* pbstrColor, COLORREF crColor)
  336. {
  337.     _ASSERT( NULL != pbstrColor );
  338.     _ASSERT( (crColor & 0x00FFFFFF) == crColor );
  339.  
  340.     *pbstrColor = NULL;
  341.  
  342.     WCHAR wsz[8];
  343.     HRESULT hr  = S_OK;
  344.  
  345.     wsprintfW( wsz, L"#%06X", SwapBytes(crColor) );
  346.     
  347.     *pbstrColor = ::SysAllocString( wsz );
  348.  
  349.     if (!pbstrColor)
  350.     {
  351.         hr = E_FAIL;
  352.     }
  353.  
  354.     return hr;
  355. }
  356.  
  357.  
  358. //////////////////////////////////////////////////////////////////////////////
  359. // C$$Safe_root$$::SwapBytes
  360. // Used to convert between a DWORD and COLORREF.  Simply swaps the lowest 
  361. // and 3rd order bytes.
  362. //////////////////////////////////////////////////////////////////////////////
  363. inline DWORD C$$Safe_root$$::SwapBytes(DWORD dwRet)
  364. {
  365.     return ((dwRet & 0x0000FF00) | ((dwRet & 0x00FF0000) >> 16) | ((dwRet & 0x000000FF) << 16));
  366. }
  367.  
  368.