home *** CD-ROM | disk | FTP | other *** search
/ Total Destruction / Total_Destruction.iso / addons / zipz / QDS101.ZIP / QDDev / QDTest / QDTestDlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-06  |  10.3 KB  |  445 lines

  1. // QDTestDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "QDTest.h"
  6. #include "QDTestDlg.h"
  7. #include "QDServer_i.c"
  8.  
  9. #include <math.h>
  10.  
  11. #ifndef M_PI
  12. #define M_PI        3.14159265358979323846    // matches value in gcc v2 math.h
  13. #endif
  14.  
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20.  
  21. #define POSSTATE_IDLE        0x00000000
  22. #define POSSTATE_UP            0x00000001
  23. #define POSSTATE_DOWN        0x00000002
  24. #define POSSTATE_LEFT        0x00000004
  25. #define POSSTATE_RIGHT        0x00000008
  26. #define POSSTATE_WALK        0x00000010
  27.  
  28. #define DELTA_STEP            5.0f
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CAboutDlg dialog used for App About
  31.  
  32. class CAboutDlg : public CDialog
  33. {
  34. public:
  35.     CAboutDlg();
  36.  
  37. // Dialog Data
  38.     //{{AFX_DATA(CAboutDlg)
  39.     enum { IDD = IDD_ABOUTBOX };
  40.     //}}AFX_DATA
  41.  
  42.     // ClassWizard generated virtual function overrides
  43.     //{{AFX_VIRTUAL(CAboutDlg)
  44.     protected:
  45.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  46.     //}}AFX_VIRTUAL
  47.  
  48. // Implementation
  49. protected:
  50.     //{{AFX_MSG(CAboutDlg)
  51.     //}}AFX_MSG
  52.     DECLARE_MESSAGE_MAP()
  53. };
  54.  
  55. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  56. {
  57.     //{{AFX_DATA_INIT(CAboutDlg)
  58.     //}}AFX_DATA_INIT
  59. }
  60.  
  61. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  62. {
  63.     CDialog::DoDataExchange(pDX);
  64.     //{{AFX_DATA_MAP(CAboutDlg)
  65.     //}}AFX_DATA_MAP
  66. }
  67.  
  68. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  69.     //{{AFX_MSG_MAP(CAboutDlg)
  70.         // No message handlers
  71.     //}}AFX_MSG_MAP
  72. END_MESSAGE_MAP()
  73.  
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CQDTestDlg dialog
  76.  
  77. CQDTestDlg::CQDTestDlg(CWnd* pParent /*=NULL*/)
  78. : CDialog(CQDTestDlg::IDD, pParent)
  79. ,m_pQDBspView(NULL)
  80. ,m_fStep(0)
  81. ,m_dwPosState(POSSTATE_IDLE)
  82. ,m_fFriction(1.0)
  83. ,m_nSliderPos(50)
  84. {
  85.     //{{AFX_DATA_INIT(CQDTestDlg)
  86.     m_strPakFile = _T("E:\\Quake2\\baseq2\\Pak0.pak");
  87.     m_strMapName = _T("Base1");
  88.     //}}AFX_DATA_INIT
  89.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  90.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  91.  
  92.     memset(m_fViewAngles,0x00,sizeof(m_fViewAngles));
  93.     memset(m_fViewPos,0x00,sizeof(m_fViewPos));
  94. }
  95.  
  96.  
  97. CQDTestDlg::~CQDTestDlg()
  98. {
  99.     if(m_pQDBspView)
  100.     {
  101.         m_pQDBspView->Shutdown();
  102.         m_pQDBspView->Release();
  103.     }
  104. }
  105. void CQDTestDlg::DoDataExchange(CDataExchange* pDX)
  106. {
  107.     CDialog::DoDataExchange(pDX);
  108.     //{{AFX_DATA_MAP(CQDTestDlg)
  109.     DDX_Control(pDX, IDC_SLIDER2, m_wndSlider);
  110.     DDX_Control(pDX, IDC_FLYUP, m_wndFlyUp);
  111.     DDX_Control(pDX, IDC_FLYDOWN, m_wndFlyDown);
  112.     DDX_Control(pDX, IDC_WALK, m_wndWalk);
  113.     DDX_Control(pDX, IDC_UP, m_wndUp);
  114.     DDX_Control(pDX, IDC_RIGHT, m_wndRight);
  115.     DDX_Control(pDX, IDC_LEFT, m_wndLeft);
  116.     DDX_Control(pDX, IDC_DOWN, m_wndDown);
  117.     DDX_Text(pDX, IDC_PAK, m_strPakFile);
  118.     DDX_Text(pDX, IDC_MAP, m_strMapName);
  119.     //}}AFX_DATA_MAP
  120. }
  121.  
  122. BEGIN_MESSAGE_MAP(CQDTestDlg, CDialog)
  123.     //{{AFX_MSG_MAP(CQDTestDlg)
  124.     ON_WM_SYSCOMMAND()
  125.     ON_WM_PAINT()
  126.     ON_WM_QUERYDRAGICON()
  127.     ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
  128.     ON_BN_CLICKED(IDC_VIEW, OnView)
  129.     ON_WM_TIMER()
  130.     ON_WM_MOUSEMOVE()
  131.     ON_BN_CLICKED(IDC_DOWN, OnDown)
  132.     ON_BN_CLICKED(IDC_LEFT, OnLeft)
  133.     ON_BN_CLICKED(IDC_RIGHT, OnRight)
  134.     ON_BN_CLICKED(IDC_UP, OnUp)
  135.     ON_BN_CLICKED(IDC_WALK, OnWalk)
  136.     ON_BN_CLICKED(IDC_FLYUP, OnFlyup)
  137.     ON_BN_CLICKED(IDC_FLYDOWN, OnFlydown)
  138.     //}}AFX_MSG_MAP
  139. END_MESSAGE_MAP()
  140.  
  141. /////////////////////////////////////////////////////////////////////////////
  142. // CQDTestDlg message handlers
  143.  
  144. BOOL CQDTestDlg::OnInitDialog()
  145. {
  146.     CDialog::OnInitDialog();
  147.  
  148.     // Add "About..." menu item to system menu.
  149.  
  150.     // IDM_ABOUTBOX must be in the system command range.
  151.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  152.     ASSERT(IDM_ABOUTBOX < 0xF000);
  153.  
  154.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  155.     if (pSysMenu != NULL)
  156.     {
  157.         CString strAboutMenu;
  158.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  159.         if (!strAboutMenu.IsEmpty())
  160.         {
  161.             pSysMenu->AppendMenu(MF_SEPARATOR);
  162.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  163.         }
  164.     }
  165.  
  166.     // Set the icon for this dialog.  The framework does this automatically
  167.     //  when the application's main window is not a dialog
  168.     SetIcon(m_hIcon, TRUE);            // Set big icon
  169.     SetIcon(m_hIcon, FALSE);        // Set small icon
  170.     
  171.     // TODO: Add extra initialization here
  172.     
  173.     m_wndUp.LoadBitmaps(IDB_UP,IDB_UP,IDB_UP,IDB_UP);
  174.     m_wndDown.LoadBitmaps(IDB_DOWN,IDB_DOWN,IDB_DOWN,IDB_DOWN);
  175.     m_wndLeft.LoadBitmaps(IDB_LEFT,IDB_LEFT,IDB_LEFT,IDB_LEFT);
  176.     m_wndRight.LoadBitmaps(IDB_RIGHT,IDB_RIGHT,IDB_RIGHT,IDB_RIGHT);
  177.     m_wndWalk.LoadBitmaps(IDB_WALK,IDB_WALK,IDB_WALK,IDB_WALK);
  178.  
  179.     m_wndFlyUp.LoadBitmaps(IDB_FLYUP,IDB_FLYUP,IDB_FLYUP,IDB_FLYUP);
  180.     m_wndFlyDown.LoadBitmaps(IDB_FLYDOWN,IDB_FLYDOWN,IDB_FLYDOWN,IDB_FLYDOWN);
  181.  
  182.     m_wndSlider.SetRange(0,100);
  183.     m_wndSlider.SetPos(m_nSliderPos);
  184.  
  185.     CRect rcClient;
  186.     GetClientRect(rcClient);
  187.     CWnd* pWnd = GetDlgItem(IDC_SIZERLIMIT);
  188.  
  189.     SetTimer(1,100,NULL);
  190.  
  191.     return TRUE;  // return TRUE  unless you set the focus to a control
  192. }
  193.  
  194. void CQDTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
  195. {
  196.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  197.     {
  198.         CAboutDlg dlgAbout;
  199.         dlgAbout.DoModal();
  200.     }
  201.     else
  202.     {
  203.         CDialog::OnSysCommand(nID, lParam);
  204.     }
  205. }
  206.  
  207. // If you add a minimize button to your dialog, you will need the code below
  208. //  to draw the icon.  For MFC applications using the document/view model,
  209. //  this is automatically done for you by the framework.
  210.  
  211. void CQDTestDlg::OnPaint() 
  212. {
  213.     if (IsIconic())
  214.     {
  215.         CPaintDC dc(this); // device context for painting
  216.  
  217.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  218.  
  219.         // Center icon in client rectangle
  220.         int cxIcon = GetSystemMetrics(SM_CXICON);
  221.         int cyIcon = GetSystemMetrics(SM_CYICON);
  222.         CRect rect;
  223.         GetClientRect(&rect);
  224.         int x = (rect.Width() - cxIcon + 1) / 2;
  225.         int y = (rect.Height() - cyIcon + 1) / 2;
  226.  
  227.         // Draw the icon
  228.         dc.DrawIcon(x, y, m_hIcon);
  229.     }
  230.     else
  231.     {
  232.         CDialog::OnPaint();
  233.     }
  234. }
  235.  
  236. // The system calls this to obtain the cursor to display while the user drags
  237. //  the minimized window.
  238. HCURSOR CQDTestDlg::OnQueryDragIcon()
  239. {
  240.     return (HCURSOR) m_hIcon;
  241. }
  242.  
  243. void CQDTestDlg::OnBrowse() 
  244. {
  245.     static char BASED_CODE szFilter[] = "Quake 2 Pak Files (*.pak)|*.pak||";
  246.  
  247.     CFileDialog dlg(TRUE,_T("*.pak"),
  248.                     m_strPakFile,
  249.                     OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
  250.                     szFilter
  251.                     );
  252.  
  253.     if(dlg.DoModal() == IDOK)
  254.     {
  255.         CFile fileDemo;
  256.         m_strPakFile = dlg.GetPathName();
  257.         UpdateData(FALSE);
  258.     }
  259. }
  260.  
  261. void CQDTestDlg::OnView() 
  262. {
  263.     UpdateData(TRUE);
  264.  
  265.     // yeah yeah yeah I know there probably a 
  266.     // million other ways to have this done (import or tlb wrappers)
  267.     // but well I'm a COM fan and I'm using the traditional way
  268.  
  269.     HRESULT hr = S_OK;
  270.     if(!m_pQDBspView)
  271.     {
  272.         hr = ::CoCreateInstance(CLSID_QDBspView,NULL,CLSCTX_ALL,IID_IQDBspView,(LPVOID*)&m_pQDBspView);
  273.     }
  274.  
  275.     if(SUCCEEDED(hr))
  276.     {
  277.         try
  278.         {
  279.             
  280.  
  281.             BSTR bstrCaption    = ::SysAllocString(L"QDemServer Test Caption");
  282.             BSTR bstrSky        = ::SysAllocString(L"unit1_");
  283.             BSTR bstrMap        = m_strMapName.AllocSysString();
  284.             BSTR bstrPak        = m_strPakFile.AllocSysString();
  285.  
  286.             m_pQDBspView->Init(bstrPak,TRUE,320,240);
  287.             // lets change the caption
  288.             m_pQDBspView->SetCaption(bstrCaption);
  289.             // and gamma
  290.  
  291.             m_nSliderPos = m_wndSlider.GetPos();
  292.             m_pQDBspView->SetViewOptions(QDBSPVIEW_OPTION_GAMMA,100 - m_nSliderPos,0);
  293.  
  294.             m_pQDBspView->LoadMap(bstrMap,bstrSky,bstrPak,TRUE);
  295.  
  296.             ::SysFreeString(bstrCaption);
  297.             ::SysFreeString(bstrSky);
  298.             ::SysFreeString(bstrMap);
  299.             ::SysFreeString(bstrPak);
  300.             return;
  301.         }
  302.         catch(...)
  303.         {
  304.             // this might be dangerous since our server 
  305.             // might been crashed too but well ...
  306.             m_pQDBspView->Release();
  307.             m_pQDBspView = NULL;
  308.             AfxMessageBox(_T("Helluva place to crash..."));
  309.             return;
  310.         }
  311.         
  312.     }
  313.  
  314.     if(m_pQDBspView)
  315.     {
  316.         m_pQDBspView->Release();
  317.         m_pQDBspView = NULL;
  318.     }
  319.  
  320.     CString strText;
  321.     strText.Format(_T("Something went wrong here !!!. HRESULT %X"),hr);
  322.     AfxMessageBox(strText);
  323. }
  324.  
  325. void CQDTestDlg::UpdateFrame()
  326. {
  327.     if(m_pQDBspView)
  328.     {
  329.         m_pQDBspView->SetCameraPos(m_fViewPos[0],
  330.                                     m_fViewPos[1],
  331.                                     m_fViewPos[2],
  332.                                     m_fViewAngles[0],
  333.                                     m_fViewAngles[1],
  334.                                     m_fViewAngles[2]);
  335.  
  336.     }
  337. }
  338.  
  339. void CQDTestDlg::OnTimer(UINT nIDEvent) 
  340. {
  341.     CDialog::OnTimer(nIDEvent);
  342.     
  343.     // yeah yeah dont tell me about it ...
  344.     if(m_pQDBspView && nIDEvent == 1)
  345.     {
  346.  
  347.         if(m_wndSlider.GetPos() != m_nSliderPos)
  348.         {
  349.             m_nSliderPos = m_wndSlider.GetPos();
  350.             m_pQDBspView->SetViewOptions(QDBSPVIEW_OPTION_GAMMA,100 - m_nSliderPos,0);
  351.         }
  352.  
  353.  
  354.         switch(m_dwPosState)
  355.         {
  356.         case POSSTATE_UP:
  357.             m_fViewAngles[0] -= DELTA_STEP * m_fFriction;
  358.             if(m_fViewAngles[0] < 0) m_fViewAngles[0] = 360 - m_fViewAngles[0];
  359.             break;
  360.         case POSSTATE_DOWN:
  361.             m_fViewAngles[0] += DELTA_STEP * m_fFriction;
  362.             if(m_fViewAngles[0] >= 360) m_fViewAngles[0] = m_fViewAngles[0] - 360;
  363.             break;
  364.         case POSSTATE_LEFT:
  365.             m_fViewAngles[1] += DELTA_STEP * m_fFriction;
  366.             if(m_fViewAngles[1] >= 360) m_fViewAngles[1] = m_fViewAngles[1] - 360;
  367.             break;
  368.         case POSSTATE_RIGHT:
  369.             m_fViewAngles[1] -= DELTA_STEP * m_fFriction;
  370.             if(m_fViewAngles[1] >= 360) m_fViewAngles[1] = m_fViewAngles[1] - 360;
  371.             break;
  372.         case POSSTATE_WALK:
  373.             m_fViewPos[0] += (float)(DELTA_STEP * m_fFriction * 2.0 * cos(m_fViewAngles[1] * (M_PI / 180.0)));
  374.             m_fViewPos[1] += (float)(DELTA_STEP * m_fFriction * 2.0 * sin(m_fViewAngles[1] * (M_PI / 180.0)));
  375.             break;
  376.         default:
  377.             m_fFriction = 1.0;
  378.             return;
  379.             break;
  380.         }
  381.         m_fFriction += 0.01f;
  382.         if(m_fFriction >= 3.0f)
  383.             m_fFriction == 3.0f;
  384.         UpdateFrame();
  385.     }
  386. }
  387.  
  388. void CQDTestDlg::OnMouseMove(UINT nFlags, CPoint point) 
  389. {
  390.     CDialog::OnMouseMove(nFlags, point);
  391. }
  392.  
  393. void CQDTestDlg::OnDown() 
  394. {
  395.     m_dwPosState = POSSTATE_DOWN;
  396. }
  397.  
  398. void CQDTestDlg::OnLeft() 
  399. {
  400.     m_dwPosState = POSSTATE_LEFT;
  401. }
  402.  
  403. void CQDTestDlg::OnRight() 
  404. {
  405.     m_dwPosState = POSSTATE_RIGHT;
  406. }
  407.  
  408. void CQDTestDlg::OnUp() 
  409. {
  410.     m_dwPosState = POSSTATE_UP;    
  411. }
  412.  
  413. void CQDTestDlg::OnWalk() 
  414. {
  415.     if(m_dwPosState == POSSTATE_WALK)
  416.     {
  417.         m_dwPosState = POSSTATE_IDLE;    
  418.         return;
  419.     }
  420.  
  421.     if(m_dwPosState != POSSTATE_IDLE)
  422.     {
  423.         m_dwPosState = POSSTATE_IDLE;    
  424.         return;
  425.     }
  426.  
  427.     if(m_dwPosState == POSSTATE_IDLE)
  428.     {
  429.         m_dwPosState = POSSTATE_WALK;    
  430.         return;
  431.     }
  432. }
  433.  
  434. void CQDTestDlg::OnFlyup() 
  435. {
  436.     m_fViewPos[2] += 20.0f;
  437.     UpdateFrame();
  438. }
  439.  
  440. void CQDTestDlg::OnFlydown() 
  441. {
  442.     m_fViewPos[2] -= 20.0f;
  443.     UpdateFrame();
  444. }
  445.