⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameview.cpp

📁 这是一个台球游戏的源代码
💻 CPP
字号:
// GameView.cpp : implementation of the CGameView class

#include "stdafx.h"
#include "Pool3D.h"
#include "GameView.h"

#include "Game.h"
#include "Camera.h"
#include <stdio.h>

extern CPoolGame theGame;
extern CCamera theCamera;
extern char hint[];

bool power=false;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CGameView

CGameView::CGameView()
{
}

CGameView::~CGameView()
{
}


BEGIN_MESSAGE_MAP(CGameView,CWnd )
	//{{AFX_MSG_MAP(CGameView)
	ON_WM_PAINT()
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_WM_ERASEBKGND()
	ON_WM_KEYDOWN()
	ON_WM_KEYUP()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_RBUTTONDOWN()
	ON_WM_RBUTTONUP()
	ON_WM_MOVE()
	ON_WM_SETCURSOR()
	ON_COMMAND(IDM_RESET, OnReset)
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CGameView message handlers

BOOL CGameView::PreCreateWindow(CREATESTRUCT& cs) 
{
	if (!CWnd::PreCreateWindow(cs))
		return FALSE;

	cs.dwExStyle |= WS_EX_CLIENTEDGE;
	cs.style &= ~WS_BORDER;
	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
		::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW+1), NULL);

	return TRUE;
}

void CGameView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	theGame.Render();

	SwapBuffers(dc.m_hDC);
	// Do not call CWnd::OnPaint() for painting messages
}


int CGameView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CWnd ::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	// Set pixel format
	PIXELFORMATDESCRIPTOR pfd;
	ZeroMemory(&pfd, sizeof(PIXELFORMATDESCRIPTOR));
	pfd.nSize=sizeof(PIXELFORMATDESCRIPTOR);
	pfd.nVersion=1;
	pfd.dwFlags=PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL| PFD_DOUBLEBUFFER;
	pfd.iPixelType=PFD_TYPE_RGBA;
	pfd.cColorBits=24;
	pfd.cDepthBits=16;
	pfd.cStencilBits=0;

	CClientDC clientDC(this);

	int pixelFormat = ChoosePixelFormat(clientDC.m_hDC,&pfd);
	if(!SetPixelFormat(clientDC.m_hDC, pixelFormat, &pfd))
		return -1;      

	// Obtain OpenGL rendering context
	m_hRC=wglCreateContext(clientDC.m_hDC);
	if(!wglMakeCurrent(clientDC.m_hDC,m_hRC))
		return -1; 

	/*===ADD OpenGL initialization code here===*/
	theGame.GL_init();

	return 0;
}

void CGameView::OnDestroy() 
{
	CWnd ::OnDestroy();
	
	// TODO: Add your message handler code here
	theGame.GL_cleanup();

	wglMakeCurrent(NULL,NULL);
	wglDeleteContext(m_hRC);
}

void CGameView::OnSize(UINT nType, int cx, int cy) 
{
	CWnd ::OnSize(nType, cx, cy);

	// TODO: Add your message handler code here
	theGame.GL_resize(cx, cy);
}

BOOL CGameView::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	return TRUE;	
}

void CGameView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	theGame.keymap[nChar]=true;
	
	CWnd ::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CGameView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	theGame.keymap[nChar]=false;
	
	CWnd ::OnKeyUp(nChar, nRepCnt, nFlags);
}

void CGameView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if (theGame.status==GS_AIMING)
	{
		power=true;
		SetCapture();
	}

	if (theGame.status==GS_STATIONARY)
	{
		if (theGame.rotation_flag)
		{
			theGame.rotation_flag=false;
			ReleaseCapture();
		}
		theGame.status=GS_AIMING;
		power=false;

		theGame.rod_ball_dist=theGame.min_rod_ball_dist;
		CBall *pball;
		pball=theGame.balls;
		for (int i=0; i<3; ++i)	
			theCamera.m_lookat[i]=pball->x[i];
		theCamera.m_phi=-30.0;
		theCamera.m_r=1.3;
		theCamera.get_pos_from_lookat();

		theGame.hit_speed=theGame.min_hit_speed;

		RECT rect1;
		GetWindowRect(&rect1);
		SetCursorPos((rect1.left + rect1.right)/2, (rect1.top + rect1.bottom)/2);

		CPoint p;
		GetCursorPos(&p);
		theGame.mousex=p.x;
		theGame.mousey=p.y;
		SetCapture();
	}

	CWnd ::OnLButtonDown(nFlags, point);
}

void CGameView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if (theGame.status==GS_AIMING && power==true)
	{
		ReleaseCapture();
		theGame.status=GS_HITTING;
	}
	else if (theGame.status==GS_REPOSITION)
	{
		if (theGame.reposition_valid)
			theGame.status=GS_STATIONARY;
	}

	CWnd ::OnLButtonUp(nFlags, point);
}

void CGameView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if (!theGame.rotation_flag && 
		(theGame.status==GS_STATIONARY || theGame.status==GS_MOVING || theGame.status==GS_OVER))
	{
		theGame.rotation_flag=true;
	}
	if(theGame.status==GS_AIMING && power==true)
	{
		power=false;
		theGame.hit_speed=theGame.min_hit_speed;
	}

	RECT rect1;
	GetWindowRect(&rect1);
	SetCursorPos((rect1.left + rect1.right)/2, (rect1.top + rect1.bottom)/2);

	CPoint p;
	GetCursorPos(&p);
	theGame.mousex=p.x;
	theGame.mousey=p.y;
	SetCapture();
	CWnd ::OnRButtonDown(nFlags, point);
}

void CGameView::OnRButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if (theGame.rotation_flag)
	{
		theGame.rotation_flag=false;
		ReleaseCapture();
	}
	
	CWnd ::OnRButtonUp(nFlags, point);
}

void CGameView::OnMove(int x, int y) 
{
	CWnd ::OnMove(x, y);

	// TODO: Add your message handler code here
	
}

BOOL CGameView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	SetCursor(NULL);
	
	return TRUE;
	//return CWnd ::OnSetCursor(pWnd, nHitTest, message);
}

void CGameView::OnReset() 
{
	// TODO: Add your command handler code here
	theGame.reset();
	if(theGame.player1.m_bTurn) 
		sprintf(hint,"It's %s's turn",theGame.player1.m_Name);
	else 
		sprintf(hint,"It's %s's turn",theGame.player2.m_Name);
}

void CGameView::OnMouseMove(UINT nFlags, CPoint point) 
{
	RECT rect1;
	GetWindowRect(&rect1);
	CPoint p;
	GetCursorPos(&p);
	if(p.x < rect1.left+10 || p.x > rect1.right-10 || p.y < rect1.top || p.y > rect1.bottom)
	{
		SetCursorPos((rect1.left + rect1.right)/2, (rect1.top + rect1.bottom)/2);

		GetCursorPos(&p);
		theGame.mousex=p.x;
		theGame.mousey=p.y;
	}

	CWnd ::OnMouseMove(nFlags, point);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -