Compiler: Default compiler
Building Makefile: "C:\WINDOWS\Desktop\Guru Practice\Chapter 10\Direct Music Basics\Makefile.win"
Finding dependencies for file: C:\WINDOWS\Desktop\Guru Practice\Chapter 10\Direct Music Basics\Main.cpp
Executing make...
make.exe -f "C:\WINDOWS\Desktop\Guru Practice\Chapter 10\Direct Music Basics\Makefile.win" all
g++.exe Main.o -o "DMusic.exe" -L"C:/DEV-CPP/lib" -mwindows ../../../../../Dev-Cpp/lib/libddraw.a ../../../../../Dev-Cpp/lib/libdinput.a ../../../../../Dev-Cpp/lib/libdinput8.a ../../../../../Dev-Cpp/lib/libdsound.a ../../../../../Dev-Cpp/lib/libdxguid.a
Main.o(.text+0x10de):Main.cpp: undefined reference to `CoCreateInstance@20'
Main.o(.text+0x1132):Main.cpp: undefined reference to `CoCreateInstance@20'
Main.o(.text+0x1471):Main.cpp: undefined reference to `CoUninitialize@0'
Main.o(.text+0x1497):Main.cpp: undefined reference to `CoInitialize@4'
Execution terminated
//#define WIN32_LEAN_AND_MEAN
#define INITGUID
#include <windows.h>
#include <windowsx.h>
#include <mmsystem.h>
#include <conio.h>
#include <cstdlib>
#include <memory>
#include <cstdarg>
#include <cstdio>
#include <cmath>
#include <io.h>
#include <direct.h>
#include <cwchar>
#include <fcntl.h>
#include <ddraw.h>
#include <dsound.h>
#include <dmksctrl.h>
#include <dmusici.h>
#include <dmusicc.h>
#include <dmusicf.h>
#ifndef DSBCAPS_CTRLDEFAULT
#define DSBCAPS_CTRLDEFAULT (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME )
#endif
#define class_name TEXT("Direct Music Test")
HWND g_hWnd = NULL;
HINSTANCE g_hInstance = NULL;
IDirectMusicPerformance *lpdmperf = NULL;
IDirectMusicLoader *lpdmloader = NULL;
IDirectMusicSegment *lpdmsegment = NULL;
IDirectMusicSegmentState *lpdmsegstate = NULL;
#define ddrawInitStructure(ddstruct) { memset(&ddstruct, 0, sizeof(ddstruct)); ddstruct.dwSize = sizeof(ddstruct); }
#define multibyteToWide(x, y) MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, y, -1, x, _MAX_PATH);
inline int keyUp(int key) { return !(GetAsyncKeyState(key) & 0x8000); }
inline int keyDown(int key) { return (GetAsyncKeyState(key) & 0x8000); }
IDirectMusicPerformance *initPerformance();
IDirectMusicLoader *initLoader();
IDirectMusicSegment *loadMIDI(IDirectMusicPerformance*, IDirectMusicLoader*, WCHAR*);
HRESULT freeDirectMusic();
int game_init(int = 0, void* = NULL);
int game_main(int = 0, void* = NULL);
int game_over(int = 0, void* = NULL);
LRESULT CALLBACK wndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR cmdLine, int cmdShow)
{
g_hInstance = hInstance;
WNDCLASSEX wndclsEx;
wndclsEx.hInstance = hInstance;
wndclsEx.lpfnWndProc = wndProc;
wndclsEx.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
wndclsEx.hCursor = LoadCursor(0, IDC_ARROW);
wndclsEx.hIcon = LoadIcon(0, IDI_APPLICATION);
wndclsEx.hIconSm = LoadIcon(0, IDI_WINLOGO);
wndclsEx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndclsEx.lpszMenuName = NULL;
wndclsEx.lpszClassName = class_name;
wndclsEx.cbSize = sizeof(WNDCLASSEX);
wndclsEx.cbWndExtra = 0;
wndclsEx.cbClsExtra = 0;
if (!RegisterClassEx(&wndclsEx))
{
MessageBox(0, TEXT("Could not register window class."), class_name, MB_OK | MB_ICONERROR);
return 1;
}
HWND hWnd = CreateWindowEx(0, class_name, class_name, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
MessageBox(0, TEXT("Could not register create window."), class_name, MB_OK | MB_ICONERROR);
UnregisterClass(class_name, hInstance);
return 1;
}
g_hWnd = hWnd;
ShowWindow(hWnd, cmdShow);
UpdateWindow(hWnd);
if (!game_init())
{
UnregisterClass(class_name, hInstance);
return 1;
}
MSG msg;
while (1)
{
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (!game_main())
break;
}
UnregisterClass(class_name, hInstance);
return (game_over() ? msg.wParam : 1);
}
IDirectMusicPerformance *initPerformance()
{
IDirectMusicPerformance *pPerf = NULL;
if (FAILED(CoCreateInstance(CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC,
IID_IDirectMusicPerformance, (void **)&pPerf)))
return NULL;
return pPerf;
}
IDirectMusicLoader *initLoader()
{
IDirectMusicLoader *pLoader = NULL;
if (FAILED(CoCreateInstance(CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC,
IID_IDirectMusicLoader, (void **)&pLoader)))
return NULL;
return pLoader;
}
IDirectMusicSegment *loadMIDI(IDirectMusicPerformance *pPerf, IDirectMusicLoader *pLoader,
WCHAR *file_name)
{
IDirectMusicSegment *pSegment = NULL;
char szDir[_MAX_PATH];
WCHAR wzDir[_MAX_PATH];
if (!_getcwd(szDir, _MAX_PATH))
return NULL;
multibyteToWide(wzDir, szDir);
if (FAILED(pLoader->SetSearchDirectory(GUID_DirectMusicAllTypes, wzDir, false)))
return NULL;
DMUS_OBJECTDESC objDesc;
memset(&objDesc, 0, sizeof(DMUS_OBJECTDESC));
objDesc.dwSize = sizeof(DMUS_OBJECTDESC);
objDesc.guidClass = CLSID_DirectMusicSegment;
wcscpy(objDesc.wszFileName, file_name);
objDesc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME;
if (FAILED(pLoader->GetObject(&objDesc, IID_IDirectMusicSegment, (void **)&pSegment)))
return NULL;
if (FAILED(pSegment->SetParam(GUID_StandardMIDIFile, -1, 0, 0, (void *)pPerf)))
return NULL;
if (FAILED(pSegment->SetParam(GUID_Download, -1, 0, 0, (void *)pPerf)))
return NULL;
return pSegment;
}
HRESULT freeDirectMusic()
{
lpdmperf->Stop(NULL, NULL, 0, 0);
lpdmsegment->SetParam(GUID_Unload, -1, 0, 0, (void *)lpdmperf);
lpdmsegment->Release();
lpdmsegment = NULL;
lpdmperf->CloseDown();
lpdmperf->Release();
lpdmperf = NULL;
lpdmloader->Release();
lpdmloader = NULL;
CoUninitialize();
return S_OK;
}
int game_init(int nParams, void *pParams)
{
srand(GetTickCount());
if (FAILED(CoInitialize(NULL)))
return 0;
if (!(lpdmperf = initPerformance()))
return 0;
if (FAILED(lpdmperf->Init(NULL, NULL, g_hWnd)))
return 0;
if (FAILED(lpdmperf->AddPort(NULL)))
return 0;
if (!(lpdmloader = initLoader()))
return 0;
if (lpdmsegment)
{
lpdmsegment->Release();
lpdmsegment = NULL;
}
ShowCursor(0);
return 1;
}
int game_main(int nParams, void *pParams)
{
static int window_closed = 0;
if (window_closed)
return 1;
if (keyDown(VK_ESCAPE))
{
window_closed = 1;
SendMessage(g_hWnd, WM_DESTROY, 0, 0);
}
return 1;
}
int game_over(int nParams, void *pParams)
{
if (FAILED(freeDirectMusic()))
return 0;
return 1;
}
LRESULT CALLBACK wndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE:
if (MessageBox(hWnd, TEXT("Going so soon?"), class_name, MB_YESNO | MB_ICONWARNING) != IDYES)
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}