OpenGL的MFC编程-弹球游戏


OpenGL的MFC编程
 

这是一个简单的弹球游戏,基于opengl的MFC对话框程序,移动左右键可控制木版阻挡小球,小球飞过木版后游戏自动结束。

建立一个基于对话框的工程(名称:BallGL)

并且在工程设置的Link里加入库opengl32.lib glu32.lib glaux.lib

删除对话框上的所有控件,属性中的边框设置为RESIZING,勾选最大化和最小话按钮。

为CBallGLDlg类添加两个函数BOOL SetWindowPixelFormat(HDC hDC); 和BOOL CreateViewGLContext(HDC hDC);设置OpenGL显示环境。

为CBallGLDlg类添加变量HDC hrenderDC; 在OnInitDialog函数中加入下面代码:
// TODO: Add extra initialization here
hrenderDC=::GetDC(this->m_hWnd);
if(SetWindowPixelFormat(hrenderDC)==FALSE)
return 0;
if(CreateViewGLContext(hrenderDC)==FALSE)
return 0;
在BallGLDlg.cpp中加入两个全局变量:

float pos[4] = {3,3,3,1};
float dir[3] = {-1,-1,-1};

添加PretranslateMessage消息处理函数响应方向键消息。

SetTimer(1,100,0);可控制小球的运动速度。

全部代码如下所示:

// BallGLDlg.h : header file
//

#if !defined(AFX_BALLGLDLG_H__B2B3EED7_0C38_4672_B1AB_CAA8E0CB7198__INCLUDED_)
#define AFX_BALLGLDLG_H__B2B3EED7_0C38_4672_B1AB_CAA8E0CB7198__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////
// CBallGLDlg dialog

class CBallGLDlg : public CDialog
{
// Construction
public:
CBallGLDlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data
//{{AFX_DATA(CBallGLDlg)
enum { IDD = IDD_BALLGL_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CBallGLDlg)
public:
virtual BOOL PreTranslateMessage(MSG* pMsg);
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
HICON m_hIcon;

BOOL SetWindowPixelFormat(HDC hDC); //设定象素格式
BOOL CreateViewGLContext(HDC hDC); //View GL Context
HDC hrenderDC; //DC
HGLRC hrenderRC; //RC
int PixelFormat;

void display();

int Catched();
void GetNextXY();
void DrawRacket();
void DrawBall();

// Generated message map functions
//{{AFX_MSG(CBallGLDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_BALLGLDLG_H__B2B3EED7_0C38_4672_B1AB_CAA8E0CB7198__INCLUDED_)
//////////////////////////////////

/////////////////////////////////////

// BallGLDlg.cpp : implementation file
//

#include "stdafx.h"
#include "BallGL.h"
#include "BallGLDlg.h"
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include <stdlib.h>
#include <time.h>

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

float pos[4] = {3,3,3,1};
float dir[3] = {-1,-1,-1};

#define racket_w 2
#define racket_delta racket_w/2
#define initial_vilocity 0.1
#define radius 0.5

double x, y;
double racket_x=0;
double vx, vy;
double right = 5, left = -5, top = 5, bottom = -5;

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBallGLDlg dialog

CBallGLDlg::CBallGLDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBallGLDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBallGLDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CBallGLDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBallGLDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CBallGLDlg, CDialog)
//{{AFX_MSG_MAP(CBallGLDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_WM_SIZE()
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBallGLDlg message handlers

BOOL CBallGLDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here
PixelFormat=0;
hrenderDC=::GetDC(this->m_hWnd);
if(SetWindowPixelFormat(hrenderDC)==FALSE)
return 0;
if(CreateViewGLContext(hrenderDC)==FALSE)
return 0;

CRect rect;
GetClientRect(&rect);
glViewport(0,0,rect.Width(),rect.Height());
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(-5,5, -5,5, 2,12);
gluLookAt( 0,0,5, 0,0,0, 0,1,0 );
glMatrixMode( GL_MODELVIEW );

glEnable(GL_DEPTH_TEST);

glEnable(GL_COLOR_MATERIAL);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glEnable(GL_AUTO_NORMAL);

glLightfv(GL_LIGHT0, GL_POSITION, pos);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, dir);

srand(time(0));

vx=initial_vilocity+((double)(rand()%50))/1000.0;
vy=initial_vilocity+((double)(rand()%50))/1000.0;

SetTimer(1,100,0);
return TRUE; // return TRUE unless you set the focus to a control
}

void CBallGLDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CBallGLDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
display();
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CBallGLDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

BOOL CBallGLDlg::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pixelDesc;
pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixelDesc.nVersion = 1;
pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER |
PFD_TYPE_RGBA;
pixelDesc.iPixelType = PFD_TYPE_RGBA;
pixelDesc.cColorBits = 32;
pixelDesc.cRedBits = 0;
pixelDesc.cRedShift = 0;
pixelDesc.cGreenBits = 0;
pixelDesc.cGreenShift = 0;
pixelDesc.cBlueBits = 0;
pixelDesc.cBlueShift = 0;
pixelDesc.cAlphaBits = 0;
pixelDesc.cAlphaShift = 0;
pixelDesc.cAccumBits = 0;
pixelDesc.cAccumRedBits = 0;
pixelDesc.cAccumGreenBits = 0;
pixelDesc.cAccumBlueBits = 0;
pixelDesc.cAccumAlphaBits = 0;
pixelDesc.cDepthBits = 0;
pixelDesc.cStencilBits = 1;
pixelDesc.cAuxBuffers = 0;
pixelDesc.iLayerType = PFD_MAIN_PLANE;
pixelDesc.bReserved = 0;
pixelDesc.dwLayerMask = 0;
pixelDesc.dwVisibleMask = 0;
pixelDesc.dwDamageMask = 0;
PixelFormat = ChoosePixelFormat(hDC,&pixelDesc);
if(PixelFormat==0) // Choose default
{
PixelFormat = 1;
if(DescribePixelFormat(hDC,PixelFormat,
sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0)
{
return FALSE;
}
}
if(SetPixelFormat(hDC,PixelFormat,&pixelDesc)==FALSE)
{
return FALSE;
}
return TRUE;
}
BOOL CBallGLDlg::CreateViewGLContext(HDC hDC)
{
hrenderRC = wglCreateContext(hDC);
if(hrenderRC==NULL)
return FALSE;
if(wglMakeCurrent(hDC,hrenderRC)==FALSE)
return FALSE;
return TRUE;
}

void CBallGLDlg::display()
{

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

GetNextXY();
DrawBall();
DrawRacket();

if(y-radius<bottom)
this->EndDialog(0);

SwapBuffers(hrenderDC);
}

void CBallGLDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
Invalidate(0);
CDialog::OnTimer(nIDEvent);

// Eat spurious WM_TIMER message
MSG msg;
while( ::PeekMessage( & msg, m_hWnd, WM_TIMER, WM_TIMER, PM_REMOVE ) )
;
}

void CBallGLDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);

// TODO: Add your message handler code here
CRect rect;
GetClientRect(&rect);
glViewport(0,0,rect.Width(),rect.Height());
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(-5,5, -5,5, 2,12);
gluLookAt( 0,0,5, 0,0,0, 0,1,0 );
glMatrixMode( GL_MODELVIEW );
}

int CBallGLDlg::Catched()
{
if(x>racket_x && x<racket_x+racket_w)
return 1;
else
return 0;
}

 

void CBallGLDlg::GetNextXY()
{

x += vx;
y += vy;

if(x-radius<left || x+radius>right)
{
vx = -vx;
x += 2*vx;
}


if(y+radius>top || (y-radius<bottom && Catched()) )
{
vy = -vy;
y += 2*vy;
}

}

 

void CBallGLDlg::DrawRacket()
{
glPushMatrix();
glTranslated(racket_x+racket_w/2, bottom, 0);
glColor3ub(0,0,255);
auxSolidBox(racket_w, 0.5, racket_w);
glPopMatrix();
}

void CBallGLDlg::DrawBall()
{
glPushMatrix();
glTranslated(x,y,0);
glColor3d(1,0,0);
auxSolidSphere(radius);
glPopMatrix();
}

void CBallGLDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if(nChar == 'l')
{

}

CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}

BOOL CBallGLDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
switch (pMsg->message)
{
case WM_KEYDOWN:
switch (pMsg->wParam)
{
case VK_UP:
//MessageBox("UP","msg",MB_OK);
break;
case VK_DOWN:
//MessageBox("DOWN","msg",MB_OK);
break;
case VK_LEFT:
if(racket_x>left)
racket_x -= racket_delta;
break;
case VK_RIGHT:
if(racket_x+racket_w<right)
racket_x += racket_delta;
break;
default:
break;
}
case WM_KEYUP:
break;
default:
break;
}

return CDialog::PreTranslateMessage(pMsg);
}

 


首页