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

📄 soundplayerview.cpp

📁 一个简单的类似钢琴的游戏
💻 CPP
字号:
// SoundPlayerView.cpp : implementation of the CSoundPlayerView class
//

#include "stdafx.h"
#include "SoundPlayer.h"

#include "SoundPlayerDoc.h"
#include "SoundPlayerView.h"
#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <math.h>
#include "DirectIOLib.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
void setfreq( int hz );
void sound( int Freq );
void nosound();

/////////////////////////////////////////////////////////////////////////////
// CSoundPlayerView

IMPLEMENT_DYNCREATE(CSoundPlayerView, CView)

BEGIN_MESSAGE_MAP(CSoundPlayerView, CView)
	//{{AFX_MSG_MAP(CSoundPlayerView)
	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()

/////////////////////////////////////////////////////////////////////////////
// CSoundPlayerView construction/destruction

CSoundPlayerView::CSoundPlayerView()
{
	// TODO: add construction code here
	if(openDirectIO(NULL)==-1) 
		AfxMessageBox("Failed to Open Direct IO !");
}

CSoundPlayerView::~CSoundPlayerView()
{
}

BOOL CSoundPlayerView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	BOOL bPreCreated = CView::PreCreateWindow(cs);
	cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL);	// Enable word-wrapping

	return bPreCreated;
}

/////////////////////////////////////////////////////////////////////////////
// CSoundPlayerView drawing

void CSoundPlayerView::OnDraw(CDC* pDC)
{
	CSoundPlayerDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CSoundPlayerView printing

BOOL CSoundPlayerView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default CEditView preparation
	return CView::OnPreparePrinting(pInfo);
}

void CSoundPlayerView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
	// Default CEditView begin printing.
	CView::OnBeginPrinting(pDC, pInfo);
}

void CSoundPlayerView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
	// Default CEditView end printing
	CView::OnEndPrinting(pDC, pInfo);
}

/////////////////////////////////////////////////////////////////////////////
// CSoundPlayerView diagnostics

#ifdef _DEBUG
void CSoundPlayerView::AssertValid() const
{
	CView::AssertValid();
}

void CSoundPlayerView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CSoundPlayerDoc* CSoundPlayerView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSoundPlayerDoc)));
	return (CSoundPlayerDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSoundPlayerView message handlers

void CSoundPlayerView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	char c=(char)nChar;
	switch( c )
	{
	case '1':	sound( LDO );	break;
	case '2':	sound( LRI );	break;
	case '3':	sound( LMI );	break;
	case '4':	sound( LFA );	break;
	case '5':	sound( LSO );	break;
	case '6':	sound( LLA );	break;
	case '7':	sound( LTI );	break;
	case 'Q':
	case 'q':	sound( DO );	break;
	case 'W':
	case 'w':	sound( RI );	break;
	case 'E':
	case 'e':	sound( MI );	break;
	case 'R':
	case 'r':	sound( FA );	break;
	case 'T':
	case 't':	sound( SO );	break;
	case 'Y':
	case 'y':	sound( LA );	break;
	case 'U':
	case 'u':	sound( TI );	break;
	case 'A':
	case 'a':	sound( HDO );	break;
	case 'S':
	case 's':	sound( HRI );	break;
	case 'D':
	case 'd':	sound( HMI );	break;
	case 'F':
	case 'f':	sound( HFA );	break;
	case 'G':
	case 'g':	sound( HSO );	break;
	case 'H':
	case 'h':	sound( HLA );	break;
	case 'J':
	case 'j':	sound( HTI );	break;
	default:	break;
	}	
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CSoundPlayerView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	nosound();
	
	CView::OnKeyUp(nChar, nRepCnt, nFlags);
}

void setfreq(int hz)
{
	hz = 1193180 / hz;
	_outp(0x43, 0xb6);
	_outp(0x42, hz);
	_outp(0x42, hz >> 8);
}

void sound( int Freq )
{
	_outp(0x61, _inp(0x61) | 0x03);
	setfreq( Freq );
}
void nosound()
{
	_outp( 0x61, _inp(0x61) & ~0x03 );
}

⌨️ 快捷键说明

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