DUDEVIEW.CPP
资源名称:tc3d.zip [点击查看]
上传用户:abcdshs
上传日期:2007-01-07
资源大小:1858k
文件大小:16k
源码类别:
游戏
开发平台:
Visual C++
- // dudeView.cpp : implementation of the DudeView class
- //
- #include "stdafx.h"
- #include "dudes.h"
- #include "dudeDoc.h"
- #include "dudeView.h"
- #include "levels.h"
- KeyControl keyboardcontrol = NOKEYHIT;
- static char movebuttondown = FALSE;
- static char walkcount;
- static CPoint currpoint;
- #ifdef EDITOR
- enum selectmodetype
- {
- STOP,
- MOVING,
- ROTATING
- };
- selectmodetype selectmode;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // DudeView
- IMPLEMENT_DYNCREATE(DudeView, CView)
- BEGIN_MESSAGE_MAP(DudeView, CView)
- //{{AFX_MSG_MAP(DudeView)
- ON_WM_LBUTTONDOWN()
- ON_WM_MOUSEMOVE()
- ON_WM_RBUTTONDOWN()
- ON_WM_SIZE()
- ON_WM_TIMER()
- ON_WM_CREATE()
- ON_WM_DESTROY()
- ON_WM_LBUTTONUP()
- ON_WM_RBUTTONUP()
- ON_COMMAND(ID_NEW_GAME, OnNewGame)
- ON_WM_KEYDOWN()
- ON_WM_KEYUP()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // DudeView construction/destruction
- DudeView::DudeView()
- {
- hdc = ::GetDC(m_hWnd);
- Game = new DudesWrapper;
- Game->InitLaunch();
- }
- DudeView::~DudeView()
- {
- ::ReleaseDC(m_hWnd, hdc);
- Game->CleanUp();
- delete Game;
- }
- void DudeView::LaunchGame()
- {
- wglMakeCurrent(Game->hdc, Game->hrc);
- Game->Launch();
- wglMakeCurrent(Game->hdc, NULL);
- }
- BOOL DudeView::PreCreateWindow(CREATESTRUCT& cs)
- {
- cs.style |= WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // DudeView
- void DudeView::OnDraw(CDC* pDC)
- {
- //DudeDoc* pDoc = GetDocument();
- //ASSERT_VALID(pDoc);
- pDC = pDC;
- SetTimer(1, 1, NULL);
- //::ReleaseDC(m_hWnd, hdc);
- //hdc = ::GetDC(m_hWnd);
- //LaunchGame();
- }
- /////////////////////////////////////////////////////////////////////////////
- // DudeView diagnostics
- #ifdef _DEBUG
- void DudeView::AssertValid() const
- {
- CView::AssertValid();
- }
- void DudeView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- DudeDoc* DudeView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(DudeDoc)));
- return (DudeDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // DudeView message handlers
- void DudeView::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType)
- {
- CView::CalcWindowRect(lpClientRect, nAdjustType);
- }
- LRESULT DudeView::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
- {
- return CView::DefWindowProc(message, wParam, lParam);
- }
- void DudeView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
- {
- ::ReleaseDC(m_hWnd, hdc);
- hdc = ::GetDC(m_hWnd);
- CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
- }
- BOOL DudeView::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
- {
- return CView::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
- }
- BOOL DudeView::OnCommand(WPARAM wParam, LPARAM lParam)
- {
- return CView::OnCommand(wParam, lParam);
- }
- LRESULT DudeView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
- {
- // TODO: Add your specialized code here and/or call the base class
- return CView::WindowProc(message, wParam, lParam);
- }
- void DudeView::WinHelp(DWORD dwData, UINT nCmd)
- {
- // TODO: Add your specialized code here and/or call the base class
- CView::WinHelp(dwData, nCmd);
- }
- void DudeView::OnLButtonDown(UINT nFlags, CPoint point)
- {
- CView::OnLButtonDown(nFlags, point);
- currpoint = point;
- #ifdef EDITOR
- if (Game->LevelPtr()->selection)
- selectmode = MOVING;
- else
- movebuttondown = TRUE;
- #else
- movebuttondown = TRUE;
- #endif
- }
- void DudeView::OnMouseMove(UINT nFlags, CPoint point)
- {
- CView::OnMouseMove(nFlags, point);
- #ifdef EDITOR
- if (Game->LevelPtr()->selection && selectmode != STOP)
- {
- view newloc = Game->LevelPtr()->selection->location();
- float xdif = (float)(currpoint.x - point.x);
- float zdif = (float)(currpoint.y - point.y);
- if (selectmode == MOVING)
- {
- newloc.setx(newloc.x() + xdif/100.0f);
- newloc.setz(newloc.z() + zdif/100.0f);
- }
- else if (selectmode == ROTATING)
- {
- newloc.setxdir(newloc.xdirection()+(direction)xdif);
- newloc.setzdir(newloc.zdirection()+(direction)zdif);
- }
- Game->LevelPtr()->selection->setto(newloc);
- }
- #endif
- currpoint = point;
- }
- void DudeView::OnRButtonDown(UINT nFlags, CPoint point)
- {
- CView::OnRButtonDown(nFlags, point);
- #ifdef EDITOR
- if (Game->LevelPtr()->selection)
- selectmode = ROTATING;
- #else
- if (!Game->Motionless())
- Game->LevelPtr()->usercontrol->block();
- #endif
- }
- void DudeView::OnTimer(UINT nIDEvent)
- {
- static char turning;
- LaunchGame();
- CView::OnTimer(nIDEvent);
- char motion = (char) ((movebuttondown || keyboardcontrol != NOKEYHIT)
- && !Game->Motionless());
- #ifdef EDITOR
- if (!Game->LevelPtr()->selection)
- #endif
- if (motion)
- {
- if (keyboardcontrol != NOKEYHIT)
- {
- switch(keyboardcontrol)
- {
- case FOWARD:
- if (++walkcount > 7)
- Game->LevelPtr()->usercontrol->runfoward();
- else
- Game->LevelPtr()->usercontrol->stepfoward();
- break;
- case BACK:
- walkcount = 0;
- Game->LevelPtr()->usercontrol->stepbackward();
- break;
- case LEFT:
- walkcount = 0;
- Game->LevelPtr()->usercontrol->turnleft();
- break;
- case RIGHT:
- walkcount = 0;
- Game->LevelPtr()->usercontrol->turnright();
- break;
- case HIT:
- if (random(10) == 3)
- Game->LevelPtr()->usercontrol->selectpunch(random(100),Game->Hero()->punchlist());
- else
- Game->LevelPtr()->usercontrol->selectpunch(random(100),Game->Hero()->normalpunch());
- break;
- case BLOCK:
- Game->LevelPtr()->usercontrol->block();
- break;
- }
- keyboardcontrol = NOKEYHIT;
- }
- else
- {
- direction tmpdir, tcdir, tmp;
- tmpdir = getdirection((float)Game->Centx(),
- (float)Game->Centy(),
- (float)currpoint.x,
- (float)currpoint.y) +
- Game->Aim().ydirectionto(camera);
- tcdir = Game->LevelPtr()->usercontrol->location().ydirection();
- tmp = tmpdir - tcdir;
- if (tmp < (direction) TC_PI_8 || tmp > (direction) -TC_PI_8)
- {
- if (turning)
- {
- turning = FALSE;
- walkcount = 0;
- Game->LevelPtr()->usercontrol->stopeverything();
- }
- if (movebuttondown)
- {
- if (++walkcount > 7)
- Game->LevelPtr()->usercontrol->runfoward();
- else
- Game->LevelPtr()->usercontrol->stepfoward();
- }
- }
- else
- {
- if (!turning)
- {
- turning = TRUE;
- Game->LevelPtr()->usercontrol->stopeverything();
- }
- if (tcdir < tmpdir)
- Game->LevelPtr()->usercontrol->turnleft();
- else
- Game->LevelPtr()->usercontrol->turnright();
- }
- }
- }
- }
- static float lightAmbient[] = {1.0f,1.0f,1.0f,0.0f};
- int DudeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CView::OnCreate(lpCreateStruct) == -1)
- return -1;
- PIXELFORMATDESCRIPTOR pfd;
- int n;
- m_pDC = new CClientDC(this);
- ASSERT(m_pDC != NULL);
- Game->hdc = m_pDC->GetSafeHdc();
- if (!bSetupPixelFormat())
- return 1;
- n = ::GetPixelFormat(Game->hdc);
- ::DescribePixelFormat(Game->hdc, n, sizeof(pfd), &pfd);
- if (pfd.dwFlags & PFD_NEED_PALETTE)
- SetupLogicalPalette();
- Game->hrc = wglCreateContext(Game->hdc);
- wglMakeCurrent(Game->hdc, Game->hrc);
- glEnable(GL_LIGHT0);
- glEnable(GL_LIGHTING);
- glEnable(GL_DEPTH_TEST);
- //glDisable(GL_LIGHTING);
- glDepthFunc(GL_LEQUAL);
- glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
- glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_FASTEST);
- glHint(GL_POINT_SMOOTH_HINT,GL_FASTEST);
- glHint(GL_POLYGON_SMOOTH_HINT,GL_FASTEST);
- glHint(GL_LINE_SMOOTH_HINT,GL_FASTEST);
- glHint(GL_FOG_HINT,GL_FASTEST);
- glShadeModel(GL_FLAT);
- wglMakeCurrent(Game->hdc, NULL);
- Game->hwnd = m_hWnd;
- return 0;
- }
- void DudeView::OnDestroy()
- {
- KillTimer(1);
- Game->hrc = ::wglGetCurrentContext();
- ::wglMakeCurrent(NULL, NULL);
- if (Game->hrc)
- ::wglDeleteContext(Game->hrc);
- if (m_pDC)
- delete m_pDC;
- CView::OnDestroy();
- }
- BOOL DudeView::bSetupPixelFormat()
- {
- static PIXELFORMATDESCRIPTOR pfd =
- {
- sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
- 1, // version number
- PFD_DRAW_TO_WINDOW | // support window
- PFD_SUPPORT_OPENGL | // support OpenGL
- PFD_DOUBLEBUFFER, // double buffered
- PFD_TYPE_RGBA, // RGBA type
- 24, // 24-bit color depth
- 0, 0, 0, 0, 0, 0, // color bits ignored
- 0, // no alpha buffer
- 0, // shift bit ignored
- 0, // no accumulation buffer
- 0, 0, 0, 0, // accum bits ignored
- 32, // 32-bit z-buffer
- 0, // no stencil buffer
- 0, // no auxiliary buffer
- PFD_MAIN_PLANE, // main layer
- 0, // reserved
- 0, 0, 0 // layer masks ignored
- };
- int pixelformat;
- if ( (pixelformat = ChoosePixelFormat(Game->hdc, &pfd)) == 0 )
- {
- MessageBox("ChoosePixelFormat failed");
- return FALSE;
- }
- if (SetPixelFormat(Game->hdc, pixelformat, &pfd) == FALSE)
- {
- MessageBox("SetPixelFormat failed");
- return FALSE;
- }
- return TRUE;
- }
- void DudeView::OnSize(UINT nType, int cx, int cy)
- {
- CView::OnSize(nType, cx, cy);
- if(cy > 0)
- {
- wglMakeCurrent(Game->hdc, Game->hrc);
- glViewport(0, 0, cx, cy);
- if((m_oldRect.right > cx) || (m_oldRect.bottom > cy))
- RedrawWindow();
- m_oldRect.right = cx;
- m_oldRect.bottom = cy;
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45.0f, (GLdouble)cx/cy, 1.0f, 20.0f);
- glMatrixMode(GL_MODELVIEW);
- wglMakeCurrent(Game->hdc, NULL);
- }
- Game->SetCx ((short)cx);
- Game->SetCy ((short)cy);
- Game->SetCentx ((short)(cx/2));
- Game->SetCenty ((short)(cy/2));
- }
- void DudeView::OnLButtonUp(UINT nFlags, CPoint point)
- {
- CView::OnLButtonUp(nFlags, point);
- movebuttondown = FALSE;
- if (!Game->Motionless())
- Game->LevelPtr()->usercontrol->stopeverything();
- #ifdef EDITOR
- selectmode = STOP;
- #endif
- }
- void DudeView::OnRButtonUp(UINT nFlags, CPoint point)
- {
- CView::OnRButtonUp(nFlags, point);
- #ifndef EDITOR
- short xtmpval = (short)((point.x * 100) / Game->Cx());
- short ytmpval = (short)((point.y * 100) / Game->Cx());
- if (!Game->Motionless())
- {
- if (ytmpval > 40 && ytmpval < 60)
- Game->LevelPtr()->usercontrol->selectpunch(xtmpval,Game->Hero()->punchlist());
- else
- Game->LevelPtr()->usercontrol->selectpunch(xtmpval,Game->Hero()->normalpunch());
- }
- #else
- selectmode = STOP;
- #endif
- }
- void DudeView::SetupLogicalPalette()
- {
- PIXELFORMATDESCRIPTOR pfd;
- LOGPALETTE *pPal;
- int n, i;
- BYTE byRedMask, byGreenMask, byBlueMask;
- n = ::GetPixelFormat(Game->hdc);
- ::DescribePixelFormat(Game->hdc, n, sizeof(pfd), &pfd);
- if (pfd.dwFlags & PFD_NEED_PALETTE)
- {
- n = 1 << pfd.cColorBits;
- pPal = (PLOGPALETTE) new char[sizeof(LOGPALETTE) + n * sizeof(PALETTEENTRY)];
- ASSERT(pPal != NULL);
- pPal->palVersion = 0x300;
- pPal->palNumEntries = (ushort) n;
- byRedMask = (uchar)((1<<pfd.cRedBits) -1);
- byGreenMask = (uchar)((1<<pfd.cGreenBits) -1);
- byBlueMask = (uchar)((1<<pfd.cBlueBits) -1);
- for (i=0; i<n; i++)
- {
- pPal->palPalEntry[i].peRed =
- (uchar)((((i>>pfd.cRedShift) & byRedMask) * 255) /byRedMask);
- pPal->palPalEntry[i].peGreen =
- (uchar)((((i>>pfd.cGreenShift) & byGreenMask) * 255) /byGreenMask);
- pPal->palPalEntry[i].peBlue =
- (uchar)((((i>>pfd.cBlueShift) & byBlueMask) * 255) /byBlueMask);
- pPal->palPalEntry[i].peFlags = 0;
- }
- m_cPalette.CreatePalette(pPal);
- delete pPal;
- m_pDC->SelectPalette(&m_cPalette, FALSE);
- m_pDC->RealizePalette();
- }
- }
- void DudeView::OnNewGame()
- {
- Game->NewGame();
- #ifdef EDITOR
- Game->Landscapes()->CleanUp();
- #endif
- }
- void DudeView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- switch(nChar)
- {
- case 49:
- keyboardcontrol = V1;
- break;
- case 50:
- keyboardcontrol = V2;
- break;
- case 51:
- keyboardcontrol = V3;
- break;
- case 52:
- keyboardcontrol = V4;
- break;
- case 53:
- keyboardcontrol = V5;
- break;
- case 54:
- keyboardcontrol = V6;
- break;
- case 55:
- keyboardcontrol = V7;
- break;
- case 56:
- keyboardcontrol = V8;
- break;
- case 57:
- keyboardcontrol = V9;
- break;
- case 48:
- keyboardcontrol = V0;
- break;
- case 38:
- if (keyboardcontrol != FOWARD)
- keyboardcontrol = FOWARD;
- break;
- case 40:
- if (keyboardcontrol != BACK)
- keyboardcontrol = BACK;
- break;
- case 37:
- if (keyboardcontrol != LEFT)
- keyboardcontrol = LEFT;
- break;
- case 39:
- if (keyboardcontrol != RIGHT)
- keyboardcontrol = RIGHT;
- break;
- case 13:
- if (keyboardcontrol != HIT)
- keyboardcontrol = HIT;
- break;
- case 32:
- if (keyboardcontrol != BLOCK)
- keyboardcontrol = BLOCK;
- break;
- }
- CView::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- void DudeView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- keyboardcontrol = NOKEYHIT;
- Game->LevelPtr()->usercontrol->stopeverything();
- CView::OnKeyUp(nChar, nRepCnt, nFlags);
- }