DUDEVIEW.CPP
上传用户:abcdshs
上传日期:2007-01-07
资源大小:1858k
文件大小:16k
源码类别:

游戏

开发平台:

Visual C++

  1. // dudeView.cpp : implementation of the DudeView class
  2. //
  3. #include "stdafx.h"
  4. #include "dudes.h"
  5. #include "dudeDoc.h"
  6. #include "dudeView.h"
  7. #include "levels.h"
  8. KeyControl keyboardcontrol = NOKEYHIT;
  9. static char movebuttondown = FALSE;
  10. static char walkcount;
  11. static CPoint currpoint;
  12. #ifdef EDITOR
  13. enum selectmodetype
  14. {
  15.     STOP,
  16.     MOVING,
  17.     ROTATING
  18. };
  19. selectmodetype selectmode;
  20. #endif
  21. /////////////////////////////////////////////////////////////////////////////
  22. // DudeView
  23. IMPLEMENT_DYNCREATE(DudeView, CView)
  24. BEGIN_MESSAGE_MAP(DudeView, CView)
  25.     //{{AFX_MSG_MAP(DudeView)
  26.     ON_WM_LBUTTONDOWN()
  27.     ON_WM_MOUSEMOVE()
  28.     ON_WM_RBUTTONDOWN()
  29.     ON_WM_SIZE()
  30.     ON_WM_TIMER()
  31.     ON_WM_CREATE()
  32.     ON_WM_DESTROY()
  33.     ON_WM_LBUTTONUP()
  34.     ON_WM_RBUTTONUP()
  35. ON_COMMAND(ID_NEW_GAME, OnNewGame)
  36. ON_WM_KEYDOWN()
  37. ON_WM_KEYUP()
  38. //}}AFX_MSG_MAP
  39. END_MESSAGE_MAP()
  40. /////////////////////////////////////////////////////////////////////////////
  41. // DudeView construction/destruction
  42. DudeView::DudeView()
  43. {
  44.     hdc   = ::GetDC(m_hWnd);
  45.     Game = new DudesWrapper;
  46.     Game->InitLaunch();
  47. }
  48. DudeView::~DudeView()
  49. {
  50.     ::ReleaseDC(m_hWnd, hdc);
  51.     Game->CleanUp();
  52.     delete Game;
  53. }
  54. void DudeView::LaunchGame()
  55. {
  56.     wglMakeCurrent(Game->hdc, Game->hrc);
  57.     Game->Launch();
  58.     wglMakeCurrent(Game->hdc, NULL);
  59. }
  60. BOOL DudeView::PreCreateWindow(CREATESTRUCT& cs)
  61. {
  62.     cs.style |= WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
  63.     return CView::PreCreateWindow(cs);
  64. }
  65. /////////////////////////////////////////////////////////////////////////////
  66. // DudeView 
  67. void DudeView::OnDraw(CDC* pDC)
  68. {
  69.     //DudeDoc* pDoc = GetDocument();
  70.     //ASSERT_VALID(pDoc);
  71.     pDC = pDC;
  72.     SetTimer(1, 1, NULL);
  73.     //::ReleaseDC(m_hWnd, hdc);
  74.     //hdc   = ::GetDC(m_hWnd);
  75.     //LaunchGame();
  76. }
  77. /////////////////////////////////////////////////////////////////////////////
  78. // DudeView diagnostics
  79. #ifdef _DEBUG
  80. void DudeView::AssertValid() const
  81. {
  82.     CView::AssertValid();
  83. }
  84. void DudeView::Dump(CDumpContext& dc) const
  85. {
  86.     CView::Dump(dc);
  87. }
  88. DudeDoc* DudeView::GetDocument() // non-debug version is inline
  89. {
  90.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(DudeDoc)));
  91.     return (DudeDoc*)m_pDocument;
  92. }
  93. #endif //_DEBUG
  94. /////////////////////////////////////////////////////////////////////////////
  95. // DudeView message handlers
  96. void DudeView::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType) 
  97. {
  98.     CView::CalcWindowRect(lpClientRect, nAdjustType);
  99. }
  100. LRESULT DudeView::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
  101. {
  102.     return CView::DefWindowProc(message, wParam, lParam);
  103. }
  104. void DudeView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
  105. {
  106.     ::ReleaseDC(m_hWnd, hdc);
  107.     hdc   = ::GetDC(m_hWnd);
  108.     CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
  109. }
  110. BOOL DudeView::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) 
  111. {
  112.     
  113.     return CView::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
  114. }
  115. BOOL DudeView::OnCommand(WPARAM wParam, LPARAM lParam) 
  116. {
  117.     
  118.     return CView::OnCommand(wParam, lParam);
  119. }
  120. LRESULT DudeView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
  121. {
  122.     // TODO: Add your specialized code here and/or call the base class
  123.     return CView::WindowProc(message, wParam, lParam);
  124. }
  125. void DudeView::WinHelp(DWORD dwData, UINT nCmd) 
  126. {
  127.     // TODO: Add your specialized code here and/or call the base class
  128.     
  129.     CView::WinHelp(dwData, nCmd);
  130. }
  131. void DudeView::OnLButtonDown(UINT nFlags, CPoint point) 
  132. {
  133.     CView::OnLButtonDown(nFlags, point);
  134.     currpoint = point;
  135. #ifdef EDITOR
  136.     if (Game->LevelPtr()->selection)
  137.         selectmode = MOVING;
  138.     else
  139.         movebuttondown = TRUE;
  140. #else
  141.     movebuttondown = TRUE;
  142. #endif
  143. }
  144. void DudeView::OnMouseMove(UINT nFlags, CPoint point) 
  145. {
  146.     CView::OnMouseMove(nFlags, point);
  147. #ifdef EDITOR
  148.     if (Game->LevelPtr()->selection && selectmode != STOP)
  149.     {
  150.         view newloc = Game->LevelPtr()->selection->location();
  151.         float xdif = (float)(currpoint.x - point.x);
  152.         float zdif = (float)(currpoint.y - point.y);
  153.         if (selectmode == MOVING)
  154.         {
  155.             newloc.setx(newloc.x() + xdif/100.0f);
  156.             newloc.setz(newloc.z() + zdif/100.0f);
  157.         }
  158.         else if (selectmode == ROTATING)
  159.         {
  160.             newloc.setxdir(newloc.xdirection()+(direction)xdif);
  161.             newloc.setzdir(newloc.zdirection()+(direction)zdif);
  162.         }
  163.         Game->LevelPtr()->selection->setto(newloc);
  164.     }
  165. #endif
  166.     currpoint = point;
  167. }
  168. void DudeView::OnRButtonDown(UINT nFlags, CPoint point) 
  169. {
  170.     CView::OnRButtonDown(nFlags, point);
  171. #ifdef EDITOR
  172.     if (Game->LevelPtr()->selection)
  173.         selectmode = ROTATING;
  174. #else
  175.     if (!Game->Motionless())
  176.         Game->LevelPtr()->usercontrol->block();
  177. #endif
  178. }
  179. void DudeView::OnTimer(UINT nIDEvent) 
  180. {
  181.     static char turning;
  182.     LaunchGame();
  183.     
  184.     CView::OnTimer(nIDEvent);
  185.     char motion = (char) ((movebuttondown || keyboardcontrol != NOKEYHIT)
  186.                     && !Game->Motionless());
  187. #ifdef EDITOR
  188.     if (!Game->LevelPtr()->selection)
  189. #endif
  190.     if (motion)
  191.     {
  192.         if (keyboardcontrol != NOKEYHIT)
  193.         {
  194.             switch(keyboardcontrol)
  195.             {
  196.             case FOWARD:
  197.                 if (++walkcount > 7)
  198.                    Game->LevelPtr()->usercontrol->runfoward();
  199.                 else
  200.                    Game->LevelPtr()->usercontrol->stepfoward();
  201.                 break;
  202.             case BACK:
  203.                 walkcount = 0;
  204.                 Game->LevelPtr()->usercontrol->stepbackward();
  205.                 break;
  206.             case LEFT:
  207.                 walkcount = 0;
  208.                 Game->LevelPtr()->usercontrol->turnleft();
  209.                 break;
  210.             case RIGHT:
  211.                 walkcount = 0;
  212.                 Game->LevelPtr()->usercontrol->turnright();
  213.                 break;
  214.             case HIT:
  215.                 if (random(10) == 3)
  216.                     Game->LevelPtr()->usercontrol->selectpunch(random(100),Game->Hero()->punchlist());
  217.                 else
  218.                     Game->LevelPtr()->usercontrol->selectpunch(random(100),Game->Hero()->normalpunch());
  219.                 break;
  220.             case BLOCK:
  221.                 Game->LevelPtr()->usercontrol->block();
  222.                 break;
  223.             }
  224.             keyboardcontrol = NOKEYHIT;
  225.         }
  226.         else
  227.         {
  228.             direction tmpdir, tcdir, tmp;
  229.             tmpdir = getdirection((float)Game->Centx(),
  230.                                   (float)Game->Centy(),
  231.                                   (float)currpoint.x,
  232.                                   (float)currpoint.y) +
  233.                                   Game->Aim().ydirectionto(camera);
  234.             tcdir = Game->LevelPtr()->usercontrol->location().ydirection();
  235.             tmp = tmpdir - tcdir;
  236.             if (tmp < (direction) TC_PI_8 || tmp > (direction) -TC_PI_8)
  237.             {
  238.                 if (turning)
  239.                 {
  240.                     turning = FALSE;
  241.                     walkcount = 0;
  242.                     Game->LevelPtr()->usercontrol->stopeverything();
  243.                 }
  244.                 if (movebuttondown)
  245.                 {
  246.                     if (++walkcount > 7)
  247.                        Game->LevelPtr()->usercontrol->runfoward();
  248.                     else
  249.                        Game->LevelPtr()->usercontrol->stepfoward();
  250.                 }
  251.             }
  252.             else
  253.             {
  254.                 if (!turning)
  255.                 {
  256.                     turning = TRUE;
  257.                     Game->LevelPtr()->usercontrol->stopeverything();
  258.                 }
  259.                 if (tcdir < tmpdir)
  260.                     Game->LevelPtr()->usercontrol->turnleft();
  261.                 else
  262.                     Game->LevelPtr()->usercontrol->turnright();
  263.             }
  264.         }
  265.     }
  266. }
  267. static float lightAmbient[] = {1.0f,1.0f,1.0f,0.0f};
  268. int DudeView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  269. {
  270.     if (CView::OnCreate(lpCreateStruct) == -1)
  271.         return -1;
  272.     
  273.     PIXELFORMATDESCRIPTOR pfd;
  274.     int         n;
  275.     m_pDC = new CClientDC(this);
  276.     ASSERT(m_pDC != NULL);
  277.     Game->hdc = m_pDC->GetSafeHdc();
  278.     if (!bSetupPixelFormat())
  279.         return 1;
  280.     n = ::GetPixelFormat(Game->hdc);
  281.     ::DescribePixelFormat(Game->hdc, n, sizeof(pfd), &pfd);
  282.     if (pfd.dwFlags & PFD_NEED_PALETTE)
  283.         SetupLogicalPalette();
  284.     Game->hrc = wglCreateContext(Game->hdc);
  285.     wglMakeCurrent(Game->hdc, Game->hrc);
  286.     glEnable(GL_LIGHT0);
  287.     glEnable(GL_LIGHTING);
  288.     glEnable(GL_DEPTH_TEST);
  289.     //glDisable(GL_LIGHTING);
  290.     glDepthFunc(GL_LEQUAL);
  291.     glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
  292.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  293.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  294.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  295.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  296.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  297.     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  298.     glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_FASTEST);
  299.     glHint(GL_POINT_SMOOTH_HINT,GL_FASTEST);
  300.     glHint(GL_POLYGON_SMOOTH_HINT,GL_FASTEST);
  301.     glHint(GL_LINE_SMOOTH_HINT,GL_FASTEST);
  302.     glHint(GL_FOG_HINT,GL_FASTEST);
  303.     glShadeModel(GL_FLAT);
  304.     wglMakeCurrent(Game->hdc, NULL);
  305.     Game->hwnd = m_hWnd;
  306.     return 0;
  307. }
  308. void DudeView::OnDestroy() 
  309. {
  310.     KillTimer(1);
  311.     Game->hrc = ::wglGetCurrentContext();
  312.     ::wglMakeCurrent(NULL,  NULL);
  313.     if (Game->hrc)
  314.         ::wglDeleteContext(Game->hrc);
  315.     if (m_pDC)
  316.         delete m_pDC;
  317.     CView::OnDestroy();
  318.     
  319. }
  320. BOOL DudeView::bSetupPixelFormat()
  321. {
  322.     static PIXELFORMATDESCRIPTOR pfd = 
  323.     {
  324.         sizeof(PIXELFORMATDESCRIPTOR),  // size of this pfd
  325.         1,                              // version number
  326.         PFD_DRAW_TO_WINDOW |            // support window
  327.           PFD_SUPPORT_OPENGL |          // support OpenGL
  328.           PFD_DOUBLEBUFFER,             // double buffered
  329.         PFD_TYPE_RGBA,                  // RGBA type
  330.         24,                             // 24-bit color depth
  331.         0, 0, 0, 0, 0, 0,               // color bits ignored
  332.         0,                              // no alpha buffer
  333.         0,                              // shift bit ignored
  334.         0,                              // no accumulation buffer
  335.         0, 0, 0, 0,                     // accum bits ignored
  336.         32,                             // 32-bit z-buffer
  337.         0,                              // no stencil buffer
  338.         0,                              // no auxiliary buffer
  339.         PFD_MAIN_PLANE,                 // main layer
  340.         0,                              // reserved
  341.         0, 0, 0                         // layer masks ignored
  342.     };
  343.     int pixelformat;
  344.     if ( (pixelformat = ChoosePixelFormat(Game->hdc, &pfd)) == 0 )
  345.     {
  346.         MessageBox("ChoosePixelFormat failed");
  347.         return FALSE;
  348.     }
  349.     if (SetPixelFormat(Game->hdc, pixelformat, &pfd) == FALSE)
  350.     {
  351.         MessageBox("SetPixelFormat failed");
  352.         return FALSE;
  353.     }
  354.     return TRUE;
  355. }
  356. void DudeView::OnSize(UINT nType, int cx, int cy) 
  357. {
  358.     CView::OnSize(nType, cx, cy);
  359.     
  360.     if(cy > 0)
  361.     {    
  362.         wglMakeCurrent(Game->hdc, Game->hrc);
  363.         glViewport(0, 0, cx, cy);
  364.         if((m_oldRect.right > cx) || (m_oldRect.bottom > cy))
  365.             RedrawWindow();
  366.         m_oldRect.right = cx;
  367.         m_oldRect.bottom = cy;
  368.         glMatrixMode(GL_PROJECTION);
  369.         glLoadIdentity();
  370.         gluPerspective(45.0f, (GLdouble)cx/cy, 1.0f, 20.0f);
  371.         glMatrixMode(GL_MODELVIEW);
  372.         wglMakeCurrent(Game->hdc, NULL);
  373.     }
  374.     Game->SetCx     ((short)cx);
  375.     Game->SetCy     ((short)cy);
  376.     Game->SetCentx  ((short)(cx/2));
  377.     Game->SetCenty  ((short)(cy/2));
  378. }
  379. void DudeView::OnLButtonUp(UINT nFlags, CPoint point) 
  380. {
  381.     CView::OnLButtonUp(nFlags, point);
  382.     movebuttondown = FALSE;
  383.     if (!Game->Motionless())
  384.         Game->LevelPtr()->usercontrol->stopeverything();
  385. #ifdef EDITOR
  386.     selectmode = STOP;
  387. #endif
  388. }
  389. void DudeView::OnRButtonUp(UINT nFlags, CPoint point) 
  390. {
  391.     CView::OnRButtonUp(nFlags, point);
  392. #ifndef EDITOR
  393.     short xtmpval = (short)((point.x * 100) / Game->Cx());
  394.     short ytmpval = (short)((point.y * 100) / Game->Cx());
  395.     if (!Game->Motionless())
  396.     {
  397.         if (ytmpval > 40 && ytmpval < 60)
  398.             Game->LevelPtr()->usercontrol->selectpunch(xtmpval,Game->Hero()->punchlist());
  399.         else
  400.             Game->LevelPtr()->usercontrol->selectpunch(xtmpval,Game->Hero()->normalpunch());
  401.     }
  402. #else
  403.     selectmode = STOP;
  404. #endif
  405. }
  406. void DudeView::SetupLogicalPalette()
  407. {
  408.     PIXELFORMATDESCRIPTOR pfd;
  409.     LOGPALETTE *pPal;
  410.     int n, i;
  411.     BYTE byRedMask, byGreenMask, byBlueMask;
  412.     n = ::GetPixelFormat(Game->hdc);
  413.     ::DescribePixelFormat(Game->hdc, n, sizeof(pfd), &pfd);
  414.     if (pfd.dwFlags & PFD_NEED_PALETTE)
  415.     {
  416.         n = 1 << pfd.cColorBits;
  417.         pPal = (PLOGPALETTE) new char[sizeof(LOGPALETTE) + n * sizeof(PALETTEENTRY)];
  418.         ASSERT(pPal != NULL);
  419.         pPal->palVersion = 0x300;
  420.         pPal->palNumEntries = (ushort) n;
  421.         byRedMask   = (uchar)((1<<pfd.cRedBits) -1);
  422.         byGreenMask = (uchar)((1<<pfd.cGreenBits) -1);
  423.         byBlueMask  = (uchar)((1<<pfd.cBlueBits) -1);
  424.         for (i=0; i<n; i++)
  425.         {
  426.             pPal->palPalEntry[i].peRed =
  427.                     (uchar)((((i>>pfd.cRedShift) & byRedMask) * 255) /byRedMask);
  428.             pPal->palPalEntry[i].peGreen =
  429.                     (uchar)((((i>>pfd.cGreenShift) & byGreenMask) * 255) /byGreenMask);
  430.             pPal->palPalEntry[i].peBlue =
  431.                     (uchar)((((i>>pfd.cBlueShift) & byBlueMask) * 255) /byBlueMask);
  432.             pPal->palPalEntry[i].peFlags = 0;
  433.         }
  434.         m_cPalette.CreatePalette(pPal);
  435.         delete pPal;
  436.         m_pDC->SelectPalette(&m_cPalette, FALSE);
  437.         m_pDC->RealizePalette();
  438.     }
  439. }
  440. void DudeView::OnNewGame() 
  441. {
  442.     Game->NewGame();
  443. #ifdef EDITOR
  444.     Game->Landscapes()->CleanUp();
  445. #endif
  446. }
  447. void DudeView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  448. {
  449.     switch(nChar)
  450.     {
  451.     case 49:
  452.         keyboardcontrol = V1;
  453.         break;
  454.     case 50:
  455.         keyboardcontrol = V2;
  456.         break;
  457.     case 51:
  458.         keyboardcontrol = V3;
  459.         break;
  460.     case 52:
  461.         keyboardcontrol = V4;
  462.         break;
  463.     case 53:
  464.         keyboardcontrol = V5;
  465.         break;
  466.     case 54:
  467.         keyboardcontrol = V6;
  468.         break;
  469.     case 55:
  470.         keyboardcontrol = V7;
  471.         break;
  472.     case 56:
  473.         keyboardcontrol = V8;
  474.         break;
  475.     case 57:
  476.         keyboardcontrol = V9;
  477.         break;
  478.     case 48:
  479.         keyboardcontrol = V0;
  480.         break;
  481.     case 38:
  482.         if (keyboardcontrol != FOWARD)
  483.         keyboardcontrol = FOWARD;
  484.         break;
  485.     case 40:
  486.         if (keyboardcontrol != BACK)
  487.         keyboardcontrol = BACK;
  488.         break;
  489.     case 37:
  490.         if (keyboardcontrol != LEFT)
  491.         keyboardcontrol = LEFT;
  492.         break;
  493.     case 39:
  494.         if (keyboardcontrol != RIGHT)
  495.         keyboardcontrol = RIGHT;
  496.         break;
  497.     case 13:
  498.         if (keyboardcontrol != HIT)
  499.         keyboardcontrol = HIT;
  500.         break;
  501.     case 32:
  502.         if (keyboardcontrol != BLOCK)
  503.         keyboardcontrol = BLOCK;
  504.         break;
  505.     }
  506. CView::OnKeyDown(nChar, nRepCnt, nFlags);
  507. }
  508. void DudeView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
  509. {
  510.     keyboardcontrol = NOKEYHIT;
  511.     Game->LevelPtr()->usercontrol->stopeverything();
  512. CView::OnKeyUp(nChar, nRepCnt, nFlags);
  513. }