-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSoundFunction.cpp
More file actions
192 lines (178 loc) · 4.21 KB
/
SoundFunction.cpp
File metadata and controls
192 lines (178 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "stdafx.h"
#include "GGchess.h"
#include <sys/stat.h>
#include <commdlg.h>
#include <dlgs.h>
#include <commctrl.h>
#include "chess.h"
#include "data.h"
#include "pregen.h"
void
RingBell(){
MyPlaySound(&appData.Asounds[(int)SoundMove]);
}
void
PlayIcsWinSound(){
MyPlaySound(&appData.Asounds[(int)SoundIcsWin]);
}
void
PlayCheckSound(){ //将军的声音
MyPlaySound(&appData.Asounds[(int)SoundCheck]);
}
void
PlayEatSound(){ //将军的声音
MyPlaySound(&appData.Asounds[(int)SoundEat]);
}
void
PlayIcsLossSound(){
MyPlaySound(&appData.Asounds[(int)SoundIcsLoss]);
}
void
PlayIcsDrawSound(){
MyPlaySound(&appData.Asounds[(int)SoundIcsDraw]);
}
void
PlayIcsUnfinishedSound(){
MyPlaySound(&appData.Asounds[(int)SoundIcsUnfinished]);
}
void
PlayAlarmSound(){
MyPlaySound(&appData.Asounds[(int)SoundAlarm]);
}
BOOLEAN
MyLoadSound(MySound *ms){
char buf[MSG_SIZ];
BOOL ok = FALSE;
struct stat st;
FILE *f;
if (ms->data){
free(ms->data);
}
ms->data = NULL;
switch (ms->name[0]) {
case NULLCHAR:
/* Silence */
ok = TRUE;
break;
case '$':
/* System sound from Control Panel. Don't preload here. */
ok = TRUE;
break;
case '!':
if (ms->name[1] == NULLCHAR) {
/* "!" alone = silence */
ok = TRUE;
}
else {
/* Builtin wave resource. Error if not found. */
HANDLE h = FindResource(hInst, ms->name + 1, "WAVE");
if (h == NULL){
break;
}
ms->data = (void *)LoadResource(hInst, (HRSRC)h);
if (h == NULL){
break;
}
ok = TRUE;
}
break;
default:
/* .wav file. Error if not found. */
//StringCbPrintf(buf,MSG_SIZ,"%schessboard\\%d\\qp.bmp",
//installDir,appData.pieceStyle);
StringCbPrintf(buf,MSG_SIZ,"%s\\snd\\%s",installDir,ms->name);
//f = fopen(ms->name, "rb");
//f = fopen(buf, "rb");
fopen_s(&f,buf,"rb");
if (f == NULL){
break;
}
if (fstat(_fileno(f), &st) < 0){
break;
}
ms->data = malloc(st.st_size);
if (fread(ms->data, st.st_size, 1, f) < 1){
break;
}
fclose(f);
ok = TRUE;
break;
}
if (!ok) {
char buf[MSG_SIZ];
//sprintf(buf, "Error loading sound %s", ms->name);
StringCbPrintf(buf,MSG_SIZ,"Error loading sound %s", ms->name);
DisplayError(buf, GetLastError());
}
return ok;
}
void //设置缺省的声音
SetDefaultSounds(){
int cc; //ColorClass cc;
int sc; //SoundClass sc;
for (cc = (ColorClass)0; cc < NColorClasses; cc++) {
appData.AtextAttribs[cc].sound.name = _strdup("");
appData.AtextAttribs[cc].sound.data = NULL;
}
for (sc = (SoundClass)0; sc < NSoundClasses; sc++) {
appData.Asounds[sc].name = _strdup("");
appData.Asounds[sc].data = NULL;
}
appData.Asounds[(int)SoundBell].name = _strdup(SOUND_BELL);
}
BOOLEAN
MyPlaySound(MySound *ms){
BOOLEAN ok = FALSE;
switch (ms->name[0]) {
case NULLCHAR:
/* Silence */
ok = TRUE;
break;
case '$':
/* System sound from Control Panel (deprecated feature).
"$" alone or an unset sound name gets default beep (still in use). */
if (ms->name[1]) {
ok = PlaySound(ms->name + 1, NULL, SND_ALIAS|SND_ASYNC);
}
if (!ok) ok = MessageBeep(MB_OK);
break;
case '!':
/* Builtin wave resource, or "!" alone for silence */
if (ms->name[1]) {
if (ms->data == NULL) return FALSE;
ok = PlaySound((LPCSTR)ms->data, NULL, SND_MEMORY|SND_ASYNC);
} else {
ok = TRUE;
}
break;
default:
/* .wav file. Error if not found. */
if (ms->data == NULL) return FALSE;
ok = PlaySound((LPCSTR)ms->data, NULL, SND_MEMORY|SND_ASYNC);
break;
}
/* Don't print an error: this can happen innocently if the sound driver
is busy; for instance, if another instance of WinBoard is playing
a sound at about the same time. */
#if 0
if (!ok) {
char buf[MSG_SIZ];
//sprintf(buf, "Error playing sound %s", ms->name);
StringCbPrintf(buf,MSG_SIZ,"Error playing sound %s", ms->name);
DisplayError(buf, GetLastError());
}
#endif
return ok;
}
void
LoadAllSounds(void)
{
int cc; //ColorClass cc;
int sc; //SoundClass sc;
for (cc = (ColorClass)0; cc < NColorClasses; cc++) {
MyLoadSound(&appData.AtextAttribs[cc].sound);
}
for (sc = (SoundClass)0; sc < NSoundClasses; sc++) {
MyLoadSound(&appData.Asounds[sc]);
}
}