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

📄 main.cpp

📁 一本关于OPenGL的很好的电子书
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#define WIN32_LEAN_AND_MEAN		// trim the excess fat from Windows

/*******************************************************************
*	Program: Chapter 17 DirectX Audio - DirectMusic Example 1
*	Author: Kevin Hawkins
*	Description: Performs OpenGL rendering while playing a MIDI
*			   file in the background.
********************************************************************/

#define INITGUID				// we use GUID's with DMusic

////// Includes
#include <windows.h>			// standard Windows app include
#include <dmusicc.h>			// DirectMusic includes
#include <dmusici.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gl/gl.h>				// standard OpenGL include
#include <gl/glu.h>				// OpenGL utilties

////// Global Variables
HDC g_HDC;					// global device context
bool fullScreen = false;			// true = fullscreen; false = windowed
bool keyPressed[256];			// holds true for keys that are pressed	

float angle = 0.0f;
unsigned int listBase;			// display list base
GLYPHMETRICSFLOAT gmf[256];		// holds orientation and placement
							// info for display lists

////// DirectMusic variables
IDirectMusicLoader8 *dmusicLoader = NULL;			// the loader
IDirectMusicPerformance8 *dmusicPerformance = NULL;	// the performance
IDirectMusicSegment8 *dmusicSegment = NULL;			// the segment

/*******************************************************
*	DirectMusic Interfaces
*******************************************************/

// InitDirectXAudio()
// desc: initializes the DirectX Audio component for playback
bool InitDirectXAudio(HWND hwnd)
{
	char pathStr[MAX_PATH];		// path for audio file
	WCHAR wcharStr[MAX_PATH];

	// create the loader object
	if (FAILED(CoCreateInstance(CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC,
						   IID_IDirectMusicLoader8, (void**)&dmusicLoader)))
	{
		MessageBox(hwnd, "Unable to create the IDirectMusicLoader8 object!\nPress OK to exit",
				 "ERROR!", MB_OK);
		return false;
	}

	// create the performance object
	if (FAILED(CoCreateInstance(CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC,
						   IID_IDirectMusicPerformance8, (void**)&dmusicPerformance)))
	{
		MessageBox(hwnd, "Unable to create the IDirectMusicPerformance8 object!\nPress OK to exit",
				 "ERROR!", MB_OK);
		return false;
	}

	// initialize the performance with the standard audio path
	dmusicPerformance->InitAudio(NULL, NULL, hwnd, DMUS_APATH_SHARED_STEREOPLUSREVERB, 64,
						    DMUS_AUDIOF_ALL, NULL);

	// retrieve the current directory
	GetCurrentDirectory(MAX_PATH, pathStr);

	// convert to unicode string
	MultiByteToWideChar(CP_ACP, 0, pathStr, -1, wcharStr, MAX_PATH);

	// set the search directory
	dmusicLoader->SetSearchDirectory(GUID_DirectMusicAllTypes, wcharStr, FALSE);

	return true;
}

// LoadSegment()
// desc: load a segment from a file
bool LoadSegment(HWND hwnd, char *filename)
{
	WCHAR wcharStr[MAX_PATH];

	// convert filename to unicode string
	MultiByteToWideChar(CP_ACP, 0, filename, -1, wcharStr, MAX_PATH);

	// load the segment from file
	if (FAILED(dmusicLoader->LoadObjectFromFile(CLSID_DirectMusicSegment,
									    IID_IDirectMusicSegment8,
									    wcharStr,
									    (void**)&dmusicSegment)))
	{
		MessageBox(hwnd, "Audio file not found! Press OK to exit",
				 "ERROR!", MB_OK);

		return false;
	}

	// download the segment's instruments to the synthesizer
	dmusicSegment->Download(dmusicPerformance);

	return true;
}

// PlaySegment()
// desc: start playing a segment
void PlaySegment(IDirectMusicPerformance8* dmPerf, IDirectMusicSegment8* dmSeg)
{
	// play the segment
	dmPerf->PlaySegmentEx(dmSeg, NULL, NULL, 0, 0, NULL, NULL, NULL);
}

// StopSegment()
// desc: stop a segment from playing
void StopSegment(IDirectMusicPerformance8* dmPerf, IDirectMusicSegment8* dmSeg)
{
	// stop the dmSeg from playing
	dmPerf->StopEx(dmSeg, 0, 0);
}

// CloseDown()
// desc: shutdown music performance
void CloseDown(IDirectMusicPerformance8* dmPerf)
{
	// stop the music
	dmPerf->Stop(NULL, NULL, 0, 0);

	// close down DirectMusic
	dmPerf->CloseDown();
}

/******************************************************
*	OpenGL Interfaces
******************************************************/

// CreateOutlineFont()
// desc: creates the outline font using the CreateFont() function
unsigned int CreateOutlineFont(char *fontName, int fontSize, float depth)
{
	HFONT hFont;         // windows font
	unsigned int base;

	base = glGenLists(256);      // create storage for 96 characters

	if (stricmp(fontName, "symbol") == 0)
	{
	     hFont = CreateFont(fontSize, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, SYMBOL_CHARSET, 
							OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
							FF_DONTCARE | DEFAULT_PITCH, fontName);
	}
	else
	{
		 hFont = CreateFont(fontSize, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET, 
							OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
							FF_DONTCARE | DEFAULT_PITCH, fontName);
	}

	if (!hFont)
		return 0;

	SelectObject(g_HDC, hFont);
	wglUseFontOutlines(g_HDC, 0, 255, base, 0.0f, depth, WGL_FONT_POLYGONS, gmf);

	return base;
}

// ClearFont()
// desc: deletes the display list for the font
void ClearFont(unsigned int base)
{
	glDeleteLists(base, 256);
}

// PrintString()
// desc: displays the text in str from the font indicated by base
void PrintString(unsigned int base, char *str)
{
	float length = 0;

	if ((str == NULL))
		return;

	// center the text
	for (unsigned int loop=0;loop<(strlen(str));loop++)	// find length of text
	{
		length+=gmf[str[loop]].gmfCellIncX;		        // increase length by character's width
	}
	glTranslatef(-length/2,0.0f,0.0f);                  // translate to center text

	// draw the text
	glPushAttrib(GL_LIST_BIT);
		glListBase(base);
		glCallLists(strlen(str), GL_UNSIGNED_BYTE, str);
	glPopAttrib();
}

// CleanUp()
// desc: application cleanup
void CleanUp()
{
	ClearFont(listBase);
	dmusicLoader->Release();
	dmusicPerformance->Release();
	dmusicSegment->Release();
}

// Initialize
// desc: initializes OpenGL
void Initialize()
{
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);		// clear to black

	glShadeModel(GL_SMOOTH);					// use smooth shading
	glEnable(GL_DEPTH_TEST);					// hidden surface removal
	glEnable(GL_LIGHT0);						// enable light0
	glEnable(GL_LIGHTING);						// enable lighting
	glEnable(GL_COLOR_MATERIAL);				// enable color for material

	listBase = CreateOutlineFont("Arial", 10, 0.25f);  // load 10pt Arial font
}

// Render
// desc: handles drawing of scene
void Render()
{
	// clear screen and depth buffer
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		
	glLoadIdentity();

	// move 10 units into the screen and rotate along all axes
	glTranslatef(0.0f, 0.0f, -15.0f);
	glRotatef(angle*0.9f, 1.0f, 0.0f, 0.0f);
	glRotatef(angle*1.5f, 0.0f, 1.0f, 0.0f);
	glRotatef(angle, 0.0f, 0.0f, 1.0f);

	// set color to blue-ish color
	glColor3f(0.3f, 0.4f, 0.8f);

	// display the text
	PrintString(listBase, "DirectX Audio!");

	// yellow-green color
	glColor3f(0.6f, 0.8f, 0.5f);

	// display text
	glPushMatrix();
		glTranslatef(-3.0f, -1.0f, 0.0f);
		PrintString(listBase, "P - Play");
	glPopMatrix();
	glPushMatrix();
		glTranslatef(-3.0f, -2.0f, 0.0f);
		PrintString(listBase, "S - Stop");
	glPopMatrix();
	glPushMatrix();
		glTranslatef(-3.0f, -3.0f, 0.0f);
		PrintString(listBase, "ESC - Quit");
	glPopMatrix();

	angle += 0.4f;

	SwapBuffers(g_HDC);			// bring backbuffer to foreground
}

// function to set the pixel format for the device context
void SetupPixelFormat(HDC hDC)
{

⌨️ 快捷键说明

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