現在、DirectSoundにて音楽を流すところまで来ました。
しかしコードを実行しても一瞬ウィンドウが表示されるだけで、
他は何も起きません。
コードとしては、
#include<windows.h> #include<d3d9.h> #include<d3dx9.h> #include<dsound.h> #include <mmsystem.h> #pragma comment(lib, "dsound.lib") #pragma comment(lib, "dxguid.lib") #pragma comment(lib, "winmm.lib") #pragma comment( lib, "d3d9.lib" ) #pragma comment( lib, "d3dx9d.lib" ) #pragma comment( lib, "d3dx9.lib" ) #pragma comment( lib, "dxerr.lib" ) #pragma comment(lib, "dsound.lib") #pragma comment(lib, "winmm.lib") LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { switch (msg) { case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, msg, wp, lp); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, int nCmdShow) { //wavファイルネーム WCHAR Music01[] = L"Input"; LPWSTR filename = Music01; //構造体 HWND hwnd; MSG msg; WNDCLASS winc; LPDIRECT3D9 m_pD3D = NULL; D3DPRESENT_PARAMETERS m_D3DPP; D3DDISPLAYMODE d3ddm; LPDIRECT3DDEVICE9 m_pD3DDevice = NULL; LPDIRECT3DTEXTURE9 g_pTexture; LPD3DXSPRITE g_pSprite = NULL; D3DXVECTOR3 center, position; HMMIO hMmio = NULL; MMIOINFO mmioInfo; WAVEFORMATEX waveFormatEx; IDirectSound8* pDS8; RECT rc; rc.left = 0; rc.top = 0; rc.right = 1280; rc.bottom = 720; center.x = 0; center.y = 0; position.x = 0; position.y = 0; ZeroMemory(&msg, sizeof(msg)); d3ddm.Width = 1280; d3ddm.Height = 720; d3ddm.Format = D3DFMT_A8R8G8B8; d3ddm.RefreshRate = 60; winc.style = CS_HREDRAW | CS_VREDRAW; winc.lpfnWndProc = WndProc; winc.cbClsExtra = winc.cbWndExtra = 0; winc.hInstance = hInstance; winc.hIcon = LoadIcon(NULL, IDI_APPLICATION); winc.hCursor = LoadCursor(NULL, IDC_ARROW); winc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); winc.lpszMenuName = NULL; winc.lpszClassName = TEXT("WCN"); if (!RegisterClass(&winc)) return 0; hwnd = CreateWindow( TEXT("WCN"), TEXT("TEST"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 175, 50, 1280, 720, NULL, NULL, hInstance, NULL ); if (hwnd == NULL) return 0; memset(&mmioInfo, 0, sizeof(MMIOINFO)); hMmio = mmioOpen(filename, &mmioInfo, MMIO_READ); if (!hMmio) return false; // ファイルオープン失敗 // RIFFチャンク検索 MMRESULT mmRes; MMCKINFO riffChunk; riffChunk.fccType = mmioFOURCC('W', 'A', 'V', 'E'); mmRes = mmioDescend(hMmio, &riffChunk, NULL, MMIO_FINDRIFF); if (mmRes != MMSYSERR_NOERROR) { mmioClose(hMmio, 0); return false; } // フォーマットチャンク検索 MMCKINFO formatChunk; formatChunk.ckid = mmioFOURCC('f', 'm', 't', ' '); mmRes = mmioDescend(hMmio, &formatChunk, &riffChunk, MMIO_FINDCHUNK); if (mmRes != MMSYSERR_NOERROR) { mmioClose(hMmio, 0); return false; } DWORD fmsize = formatChunk.cksize; DWORD size = mmioRead(hMmio, (HPSTR)&waveFormatEx, fmsize); if (size != fmsize) { mmioClose(hMmio, 0); return false; } mmioAscend(hMmio, &formatChunk, 0); // データチャンク検索 MMCKINFO dataChunk; dataChunk.ckid = mmioFOURCC('d', 'a', 't', 'a'); mmRes = mmioDescend(hMmio, &dataChunk, &riffChunk, MMIO_FINDCHUNK); if (mmRes != MMSYSERR_NOERROR) { mmioClose(hMmio, 0); return false; } char* pData = new char[dataChunk.cksize]; size = mmioRead(hMmio, (HPSTR)pData, dataChunk.cksize); if (size != dataChunk.cksize) { delete[] pData; return false; } mmioClose(hMmio, 0); DirectSoundCreate8(NULL, &pDS8, NULL); pDS8->SetCooperativeLevel(hwnd, DSSCL_NORMAL); DSBUFFERDESC DSBufferDesc; IDirectSoundBuffer* ptmpBuf = 0; IDirectSoundBuffer8* pDSBuffer; pDS8->CreateSoundBuffer(&DSBufferDesc, &ptmpBuf, NULL); ptmpBuf->QueryInterface(IID_IDirectSoundBuffer8, (void**)&pDSBuffer); ptmpBuf->Release(); WAVEFORMATEX wFmt; wFmt.wFormatTag = WAVE_FORMAT_PCM; wFmt.nChannels = 2; wFmt.nSamplesPerSec = 44100; wFmt.wBitsPerSample = 16; wFmt.nBlockAlign = wFmt.nChannels * wFmt.wBitsPerSample / 8; wFmt.nAvgBytesPerSec = wFmt.nSamplesPerSec * wFmt.nBlockAlign; wFmt.cbSize = 0; DSBufferDesc.dwSize = sizeof(DSBUFFERDESC); DSBufferDesc.dwFlags = 0; DSBufferDesc.dwBufferBytes = wFmt.nSamplesPerSec * wFmt.wBitsPerSample * wFmt.nChannels * 1000; DSBufferDesc.dwReserved = 0; DSBufferDesc.lpwfxFormat = &wFmt; DSBufferDesc.guid3DAlgorithm = GUID_NULL; // セカンダリバッファにWaveデータ書き込み LPVOID lpvWrite = 0; DWORD dwLength = 0; if (DS_OK == pDSBuffer->Lock(0, 0, &lpvWrite, &dwLength, NULL, NULL, DSBLOCK_ENTIREBUFFER)) { memcpy(lpvWrite, filename, dwLength); pDSBuffer->Unlock(lpvWrite, dwLength, NULL, 0); } BOOL isFullScreen = FALSE; for (int i = 0; i < __argc; i++) { if (_stricmp(__argv[i], "/f") == 0) { isFullScreen = TRUE; break; } } if ((m_pD3D = ::Direct3DCreate9(D3D_SDK_VERSION)) == 0) { return E_FAIL; } ZeroMemory(&m_D3DPP, sizeof(D3DPRESENT_PARAMETERS)); m_D3DPP.BackBufferCount = 1; if (isFullScreen) { m_D3DPP.Windowed = FALSE; m_D3DPP.BackBufferWidth = 1280; m_D3DPP.BackBufferHeight = 720; } else { m_D3DPP.Windowed = TRUE; } m_D3DPP.BackBufferFormat = d3ddm.Format; m_D3DPP.SwapEffect = D3DSWAPEFFECT_DISCARD; m_D3DPP.EnableAutoDepthStencil = TRUE; m_D3DPP.AutoDepthStencilFormat = D3DFMT_D16; if (FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &m_D3DPP, &m_pD3DDevice))) { if (FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &m_D3DPP, &m_pD3DDevice))) { if (FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &m_D3DPP, &m_pD3DDevice))) { return E_FAIL; } } } D3DXCreateTextureFromFile(m_pD3DDevice, TEXT("MenuBack.png"), &g_pTexture); m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); D3DXCreateSprite(m_pD3DDevice, &g_pSprite); while (msg.message != WM_QUIT) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); continue; } m_pD3DDevice->BeginScene(); if (FAILED(m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0))) { return 0; } g_pSprite->Begin(NULL); D3DXVECTOR3 center, position; //音楽再生 pDSBuffer->Play(0, 0, 0); g_pSprite->Draw( g_pTexture, &rc, ¢er, &position, 0xFFFFFFFF ); g_pSprite->End(); m_pD3DDevice->EndScene(); m_pD3DDevice->Present(NULL, NULL, NULL, NULL); } g_pTexture->Release(); g_pSprite->Release(); return msg.wParam; }
DIrect3Dにて画像を描画して、音楽を再生するソースコードです。
実行に成功しているのでエラーはなく、
画像を表示するコードも入っていますが、一瞬でウィンドウが閉じてしまいます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。