📄 mygameview.cpp
字号:
// MyGameView.cpp : implementation of the CMyGameView class
//
#include "stdafx.h"
#include "MyGame.h"
#include "MyGameDoc.h"
#include "MyGameView.h"
#include "GameIntro.h"
#include "GameSection.h"
#include "GameSession.h"
#include "GameIntro.h"
#include "Audio.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#ifdef _WIN32
#define drand48() (((float) rand())/((float) RAND_MAX))
#define srand48(x) (srand((x)))
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyGameView
IMPLEMENT_DYNCREATE(CMyGameView, CView)
BEGIN_MESSAGE_MAP(CMyGameView, CView)
//{{AFX_MSG_MAP(CMyGameView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_TIMER()
ON_WM_KEYDOWN()
ON_WM_KEYUP()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyGameView construction/destruction
CGameSection* Game;
CGameSession GS;
CGameIntro GI;
bool AudioFlag=true;
LPAUDIOWAVE lpWaveFire;
LPAUDIOWAVE lpWaveShipExp;
LPAUDIOWAVE lpWaveExp;
LPAUDIOMODULE lpModule;
HAC hVoiceFire;
HAC hVoiceExp[3];
CMyGameView::CMyGameView()
{
// TODO: add construction code here
}
CMyGameView::~CMyGameView()
{
}
BOOL CMyGameView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
////////////////////////////////////////////////////////////////
//设置窗口类型
cs.style |=WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
////////////////////////////////////////////////////////////////
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyGameView drawing
void CMyGameView::OnDraw(CDC* pDC)
{
CMyGameDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//////////////////////////////////////////////////////////////////
RenderScene(); //渲染场景
//////////////////////////////////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////
// CMyGameView printing
BOOL CMyGameView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyGameView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMyGameView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMyGameView diagnostics
#ifdef _DEBUG
void CMyGameView::AssertValid() const
{
CView::AssertValid();
}
void CMyGameView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyGameDoc* CMyGameView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyGameDoc)));
return (CMyGameDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyGameView message handlers
int CMyGameView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
//////////////////////////////////////////////////////////////////
//初始化OpenGL和设置定时器
m_pDC = new CClientDC(this);
SetTimer(1, 20, NULL);
InitializeOpenGL(m_pDC);
//////////////////////////////////////////////////////////////////
if(AudioFlag) AudioSetup();
TimerFlag=true;
srand48(time(NULL));
Game=&GI;
Game->Init();
return 0;
}
void CMyGameView::OnDestroy()
{
CView::OnDestroy();
// TODO: Add your message handler code here
/////////////////////////////////////////////////////////////////
//删除调色板和渲染上下文、定时器
::wglMakeCurrent(0,0);
::wglDeleteContext( m_hRC);
if (m_hPalette)
DeleteObject(m_hPalette);
if ( m_pDC )
{
delete m_pDC;
}
KillTimer(1);
/////////////////////////////////////////////////////////////////
if(AudioFlag) AudioEnd();
}
void CMyGameView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
/////////////////////////////////////////////////////////////////
//添加窗口缩放时的图形变换函数
glViewport(0,0,cx,cy);
/////////////////////////////////////////////////////////////////
ScreenW=cx;
ScreenH=cy;
Game->Reshape(cx,cy);
}
void CMyGameView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
/////////////////////////////////////////////////////////////////
//添加定时器响应函数和场景更新函数
Invalidate(FALSE);
/////////////////////////////////////////////////////////////////
CView::OnTimer(nIDEvent);
}
/////////////////////////////////////////////////////////////////////
// 设置逻辑调色板
//////////////////////////////////////////////////////////////////////
void CMyGameView::SetLogicalPalette(void)
{
struct
{
WORD Version;
WORD NumberOfEntries;
PALETTEENTRY aEntries[256];
} logicalPalette = { 0x300, 256 };
BYTE reds[] = {0, 36, 72, 109, 145, 182, 218, 255};
BYTE greens[] = {0, 36, 72, 109, 145, 182, 218, 255};
BYTE blues[] = {0, 85, 170, 255};
for (int colorNum=0; colorNum<256; ++colorNum)
{
logicalPalette.aEntries[colorNum].peRed =
reds[colorNum & 0x07];
logicalPalette.aEntries[colorNum].peGreen =
greens[(colorNum >> 0x03) & 0x07];
logicalPalette.aEntries[colorNum].peBlue =
blues[(colorNum >> 0x06) & 0x03];
logicalPalette.aEntries[colorNum].peFlags = 0;
}
m_hPalette = CreatePalette ((LOGPALETTE*)&logicalPalette);
}
//////////////////////////////////////////////////////////
// 初始化openGL场景
//////////////////////////////////////////////////////////
BOOL CMyGameView::InitializeOpenGL(CDC* pDC)
{
m_pDC = pDC;
SetupPixelFormat();
//生成绘制描述表
m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc());
//置当前绘制描述表
::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
return TRUE;
}
//////////////////////////////////////////////////////////
// 设置像素格式
//////////////////////////////////////////////////////////
BOOL CMyGameView::SetupPixelFormat()
{
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // pfd结构的大小
1, // 版本号
PFD_DRAW_TO_WINDOW | // 支持在窗口中绘图
PFD_SUPPORT_OPENGL | // 支持 OpenGL
PFD_DOUBLEBUFFER, // 双缓存模式
PFD_TYPE_RGBA, // RGBA 颜色模式
24, // 24 位颜色深度
0, 0, 0, 0, 0, 0, // 忽略颜色位
0, // 没有非透明度缓存
0, // 忽略移位位
0, // 无累加缓存
0, 0, 0, 0, // 忽略累加位
32, // 32 位深度缓存
0, // 无模板缓存
0, // 无辅助缓存
PFD_MAIN_PLANE, // 主层
0, // 保留
0, 0, 0 // 忽略层,可见性和损毁掩模
};
int pixelformat;
pixelformat = ::ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd);//选择像素格式
::SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd); //设置像素格式
if(pfd.dwFlags & PFD_NEED_PALETTE)
SetLogicalPalette(); //设置逻辑调色板
return TRUE;
}
//////////////////////////////////////////////////////////
// 场景绘制与渲染
//////////////////////////////////////////////////////////
BOOL CMyGameView::RenderScene()
{
static int lasttime;
int dt,nt;
dt=glutGet(GLUT_ELAPSED_TIME)-lasttime;
lasttime=glutGet(GLUT_ELAPSED_TIME);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutGet(GLUT_ELAPSED_TIME);
Game->Update(dt);
nt=Game->Draw();
float fps=m_Utility.FPS(dt);
glColor3f(1,0,0);
m_Utility.glPrintf(ScreenW,ScreenH,10,10,0,10,"FPS %7.1f TRI %8i TRI/SEC %8.0f\n",fps,nt,nt*fps);
::SwapBuffers(m_pDC->GetSafeHdc()); //交互缓冲区
return TRUE;
}
void CMyGameView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
Game->KeyDown(nChar);
Invalidate(FALSE);
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CMyGameView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
Game->KeyUp(nChar);
CView::OnKeyUp(nChar, nRepCnt, nFlags);
}
void CMyGameView::AudioSetup()
{
AUDIOINFO info;
// 初始化音频库
AInitialize();
// 打开音频设备
info.nDeviceId = AUDIO_DEVICE_MAPPER;
info.wFormat = AUDIO_FORMAT_16BITS | AUDIO_FORMAT_STEREO;
info.nSampleRate = 44100;
AOpenAudio(&info);
// 装入音频组件和声音文件
ALoadModuleFile("data/intro.xm", &lpModule, 0);
ALoadWaveFile("data/fire.wav", &lpWaveFire, 0);
ALoadWaveFile("data/explode.wav", &lpWaveExp, 0);
ALoadWaveFile("data/shipexplode.wav", &lpWaveShipExp, 0);
assert(lpModule && lpWaveFire && lpWaveExp);
// 打开音频组件的声音
AOpenVoices(lpModule->nTracks + 4);
// 通过声音播放
ACreateAudioVoice(&hVoiceFire);
ACreateAudioVoice(&(hVoiceExp[0]));
ACreateAudioVoice(&(hVoiceExp[1]));
ACreateAudioVoice(&(hVoiceExp[2]));
assert(hVoiceFire && hVoiceFire);
}
void CMyGameView::AudioEnd()
{
AStopVoice(hVoiceFire);
AStopVoice(hVoiceExp[0]);
AStopVoice(hVoiceExp[1]);
AStopVoice(hVoiceExp[2]);
ADestroyAudioVoice(hVoiceFire);
ADestroyAudioVoice(hVoiceExp[0]);
ADestroyAudioVoice(hVoiceExp[1]);
ADestroyAudioVoice(hVoiceExp[2]);
// 停止播放声音组件
AStopModule();
ACloseVoices();
// 释放波形文件和声音组件
AFreeWaveFile(lpWaveExp);
AFreeWaveFile(lpWaveFire);
AFreeModuleFile(lpModule);
// 关闭音频设备
ACloseAudio();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -