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

  1. // AgentCtl.cpp : Implementation of CAgentCtl
  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. #include "stdafx.h"
  14. #include "AtlAgent.h"
  15. #include "AgentCtl.h"
  16. #include <stdio.h>
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CAgentCtl
  20.  
  21. STDMETHODIMP CAgentCtl::Play()
  22. {
  23.     CCommand<CAccessor<CInstruction> > instruction;
  24.  
  25.     instruction.Open(m_session, _T("SELECT * FROM Instruction"));
  26.  
  27.     while (instruction.MoveNext() == S_OK)
  28.         ProcessInstruction(instruction);
  29.  
  30.     return S_OK;
  31. }
  32.  
  33. void CAgentCtl::ProcessInstruction(const CInstruction& instruction)
  34. {
  35.     USES_CONVERSION;
  36.  
  37.     if (lstrcmpi(instruction.szCommand, _T("Speak")) == 0)
  38.         SpeakText(CComBSTR(instruction.szText));
  39.     else if (lstrcmpi(instruction.szCommand, _T("Play")) == 0)
  40.         m_pCharacter->Play((BSTR)CComBSTR(instruction.szText), &m_nRequestID);
  41.     else if (lstrcmpi(instruction.szCommand, _T("Hide")) == 0)
  42.         m_pCharacter->Hide(FALSE, &m_nRequestID);
  43.     else if (lstrcmpi(instruction.szCommand, _T("Show")) == 0)
  44.         m_pCharacter->Show(FALSE, &m_nRequestID);
  45.     else if (lstrcmpi(instruction.szCommand, _T("MoveTo")) == 0)
  46.     {
  47.         short x, y;
  48. #ifdef _UNICODE
  49.         wscanf(instruction.szText, _T("%d, %d"), &x, &y);
  50. #else
  51.         sscanf(instruction.szText, _T("%d, %d"), &x, &y);
  52. #endif
  53.         m_pCharacter->MoveTo(x, y, (100-m_nSpeed)*10+100, &m_nRequestID);
  54.     }
  55.     else if (lstrcmpi(instruction.szCommand, _T("GestureAt")) == 0)
  56.     {
  57.         short x, y;
  58. #ifdef _UNICODE
  59.         wscanf(instruction.szText, _T("%d, %d"), &x, &y);
  60. #else
  61.         sscanf(instruction.szText, _T("%d, %d"), &x, &y);
  62. #endif
  63.         m_pCharacter->GestureAt(x, y, &m_nRequestID);
  64.     }
  65. }
  66.  
  67. // Override function so the COM support doesnt throw exeptions
  68. void __stdcall _com_raise_error(HRESULT hr, IErrorInfo* perrinfo) //throw(_com_error)
  69. {
  70. }
  71.  
  72. STDMETHODIMP CAgentCtl::SpeakText(BSTR Text)
  73. {
  74.     USES_CONVERSION;
  75.     TCHAR szText[256];
  76.  
  77.     wsprintf(szText, _T("\\spd=%d\\"), m_nSpeed+100);
  78.     CComBSTR strText = szText;
  79.     strText.AppendBSTR(Text);
  80.     m_pCharacter->Show(FALSE, &m_nRequestID);
  81.     m_pCharacter->Speak((BSTR)strText, "", &m_nRequestID);
  82.  
  83.     return S_OK;
  84. }
  85.  
  86. STDMETHODIMP CAgentCtl::SpeakFile(BSTR FileName)
  87. {
  88.     USES_CONVERSION;
  89.     HANDLE hFile = CreateFile(OLE2T(FileName), GENERIC_READ, FILE_SHARE_READ,
  90.         NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  91.     if (hFile == INVALID_HANDLE_VALUE)
  92.         return Error("Couldn't open file");
  93.  
  94.     char  szBuffer[100000];
  95.     DWORD dwBytesRead;
  96.  
  97.     m_pCharacter->Show(FALSE, &m_nRequestID);
  98.     ReadFile(hFile, szBuffer, 99999, &dwBytesRead, NULL);
  99.     CloseHandle(hFile);
  100.  
  101.     *(szBuffer+dwBytesRead) = 0;    // Add the NULL byte
  102.     SpeakText((BSTR)CComBSTR(szBuffer));
  103.  
  104.     return S_OK;
  105. }
  106.  
  107. STDMETHODIMP CAgentCtl::get_Speed(long * pVal)
  108. {
  109.     *pVal = m_nSpeed;
  110.  
  111.     return S_OK;
  112. }
  113.  
  114. STDMETHODIMP CAgentCtl::put_Speed(long newVal)
  115. {
  116.     if (newVal > 100)
  117.         m_nSpeed = 100;
  118.     else if (newVal < 0)
  119.         m_nSpeed = 0;
  120.     else
  121.         m_nSpeed = newVal;
  122.  
  123.     return S_OK;
  124. }
  125.  
  126. STDMETHODIMP CAgentCtl::Animate(BSTR Animation)
  127. {
  128.     if (m_pCharacter != NULL)
  129.         m_pCharacter->Play(Animation, &m_nRequestID);
  130.  
  131.     return S_OK;
  132. }
  133.  
  134. STDMETHODIMP CAgentCtl::Stop()
  135. {
  136.     if (m_pCharacter != NULL)
  137.         m_pCharacter->Stop(m_nRequestID);
  138.  
  139.     return S_OK;
  140. }
  141.