Author: Odd Gunnar Malin
Date: 05:29:12 04/26/04
Go up one level in this thread
-------------------- TalkChess.h ---------------------- #ifndef __TalkChess_h #define __TalkChess_h #include <list> #include <string> class TalkChess { HANDLE hThread; static void threadLoop(void*lpv); std::list<std::string> que; public: std::string path; TalkChess(); virtual ~TalkChess(); void speak(const std::string& s); bool read(std::string& s); bool start(); void stop(); }; #endif // __TalkChess_h --------------------- end of TalkChess.h ----------------- -------------------- TalkChess.cpp ---------------------- // This pragma is only because of a bug in MSVC that gives warning // with too long variable name (>255). #ifdef _DEBUG #pragma warning( disable : 4786 ) #endif #include <windows.h> #include <process.h> #include <CppWin/Utility.h> #include <CppChess/TalkChess.h> using namespace std; CRITICAL_SECTION talkChessCS; HANDLE hTalkChessEvent; void TalkChess::threadLoop(void* lpv) { string s; string wavfile; int index; char c; TalkChess* tc=(TalkChess*)lpv; string path=tc->path; if (path.length()) if (path.at(path.length()-1)!='\\') path+='\\'; while (1) { WaitForSingleObject(hTalkChessEvent,INFINITE); while (tc->read(s)) { if (s=="quit") { _endthread(); return; } if (s=="O-O") { wavfile=path; wavfile+="ck.wav"; PlaySound(wavfile.c_str(),NULL,SND_FILENAME|SND_NODEFAULT); continue; }else if (s=="O-O-O") { wavfile=path; wavfile+="cq.wav"; PlaySound(wavfile.c_str(),NULL,SND_FILENAME|SND_NODEFAULT); continue; }else { index=0; while (index<s.length()) { c=s.at(index); if (existIn(c,"abcdefghPNBRQK12345678+x-#")) { wavfile=path; if (isupper(c)) wavfile+="p"; if (c=='+') wavfile+="check"; else if (c=='-') wavfile+="to"; else if (c=='#') wavfile+="mate"; else wavfile+=c; wavfile+=".wav"; PlaySound(wavfile.c_str(),NULL,SND_FILENAME|SND_NODEFAULT); } index++; } } } } _endthread(); } TalkChess::TalkChess() { hThread=NULL; path=""; hTalkChessEvent=CreateEvent(NULL, FALSE, FALSE, NULL); InitializeCriticalSection(&talkChessCS); } TalkChess::~TalkChess() { stop(); CloseHandle(hTalkChessEvent); DeleteCriticalSection(&talkChessCS); } void TalkChess::speak(const std::string& s) { EnterCriticalSection(&talkChessCS); que.push_back(s); SetEvent(hTalkChessEvent); LeaveCriticalSection(&talkChessCS); } bool TalkChess::read(std::string& s) { bool ret; EnterCriticalSection(&talkChessCS); ret=false; if (que.size()>0) { ret=true; s=que.front(); que.pop_front(); }else { ret=false; } LeaveCriticalSection(&talkChessCS); return ret; } bool TalkChess::start() { if (hThread) return true; hThread=(HANDLE)_beginthread(TalkChess::threadLoop,0,this); if ((int)hThread==-1) { hThread=NULL; return true; } return false; } void TalkChess::stop() { if (!hThread) return; speak(string("quit")); if (WaitForSingleObject(hThread,1000)==WAIT_TIMEOUT) TerminateThread(hThread,0); hThread=NULL; } -------------------- end of TalkChess.cpp -----------------
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.