Ensure that sound was played after each clicking the mouse.
My idea is to create a thread that runs constantly.
#include <pthread.h>
music ding1("./ding1.wav");
music ding2("./ding2.wav");
void* pmusic(void* a)
{
while(1)
{
DWORD dw=WaitForSingleObject(hmusic, INFINITE) ;
if(ding1.busy)
{
ding2.play();
}else{
ding1.play();
}
ResetEvent(hmusic);
}
}
Create a public signal.
HANDLE hmusic=CreateEvent(nullptr,false,false,nullptr);
Play the sound using the playsound function,in music class
class music
{
public:
music(char* path)
{
//load the wav file to memory
fs.open(path...);
...
fs.readsome(buf...);
...
}
play()
{
busy=1;
PlaySoundA(buf,null,SND_MEMORY,SND_ASYNC,SND_NOSTOP,SND_NOWAIT);
busy=0;
}
char * buf;
int busy;
...
}
WndProc
LRESULT CALLBACK WndProc(hwnd,msg,wparam,lparam)
{
switch(msg)
case WM_LBUTTONDOWN:
{
SetEvent(hmusic);
break;
}
case WM_LBUTTONUP:
{
ResetEvent(hmusic);
break;
}
case WM_CREATE:
{
pthread_create(&tid,null,pmusic,null);
break;
}
}
It worked on Windows 10 after compiling, BY mingw32 with no problem.
Maybe there is another different way to achieve the above.
Thank you for sharing your wisdom and experience.