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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
#include "global.h"
#include "data_struct.h"
DDSURFACEDESC sfdesc1; // 0x0048a068
DDSURFACEDESC sfdesc2; // 0x0048a0f8
IDirectDraw *pddraw; // 0x0048a0d8
IDirectDrawSurface *pddrawsf1; // 0x0048a0dc
IDirectDrawSurface *pddrawsf2; // 0x0048a0e0
IDirectDrawSurface *pddrawsf3; // 0x004762cc
int mkf_data; // 0x48a0e4
int mkf_speaking; // 0x48a054
int mkf_effect; // 0x48a058
int mkf_panel; // 0x48a05c
HHOOK ghook; // 0x48a050
LPDIRECTSOUND pdsound; // 0x47e748
char bool_47e76c;
DSBUFFERDESC soundbuf_desc; // 0x48cb28
LPDIRECTSOUNDBUFFER *pdsoundbuf; // 0x47e74c
WAVEFORMATEX wav_format; // 0x48cb3c
int dw_48cae4, dw_47e750, dw_47e754;
int *array_48cae8[16];
int32_t dw_48a164, dw_48a168;
MMRESULT gTimerEvent; // 0x48a16c
struct st st_46cb14 = {640, 480, 0, 0, NULL}; // 0x46cb14, 12 bytes
uint8_t speed_tab[3] = {6, 4, 2}; // 0x64cb20
uint32_t dw_46cb23 = 0;
void fcn_00456280(struct st *s, int a1, int a2, int a3)
{
fcn_00455b3a((uint32_t)s->f0, (uint32_t)s->f2, s->f8, a1, a2, a3);
}
void fcn_00401f5e()
{
if (dw_48a168 == -1)
return;
fcn_00456280(&st_46cb14, dw_48a0e8, dw_48a0ec, dw_48a0f0);
dw_48a168 = -1;
}
void CALLBACK *timeProc(
UINT uTimerID,
UINT uMsg,
DWORD_PTR dwUser,
DWORD_PTR dw1,
DWORD_PTR dw2)
{
POINT p;
ebx = 0;
if (b_46cb01 == 0)
return;
dw_46cb23++;
if (dw_46cb23 >= speed_tab[global_rich4_cfg.game_speed]) {
dw_46cb23 = 0;
b_46cafa = 1;
}
if ((b_48a179 & 1) == 0)
return;
if (b_48a17a != 0)
return;
if (b_46caf9 != 0)
return;
b_48a17a = 1;
if (w_48a170 > 1) {
w_48a176++;
edx = w_48a176; /* sign extend */
eax = w_48a174; /* sign extend */
if (edx >= eax) {
w_48a176 = 0;
w_48a172++;
eax = w_48a172; /* sign extend */
edx = w_48a170; /* sign extend */
if (eax == edx) {
w_48a172 = 0;
}
ebx = 1;
}
}
GetCursorPos(&p);
if (p.x == dw_48a168 && p.y == dw_48a164 && ebx == 0) {
b_48a17a = 0;
return;
}
if ((b_48a179 & 2) == 0) {
IDirectDraw_WaitForVerticalBlank(pddraw, 1, NULL);
}
IDirectDrawSurface_Lock(pddrawsf1, NULL, &sfdesc2, 1, NULL);
st_46cb14.f8 = dw_48a11c;
fcn_00401f5e();
fcn_00401e59(p.x, p.y);
IDirectDrawSurface_Unlock(pddrawsf1, NULL);
b_48a17a = 0;
}
LRESULT CALLBACK KbdProc(
int nCode,
WPARAM wParam,
LPARAM lParam);
struct riff_chunk
{
char sig[4];
uint32_t cksize;
char buffer[0]; /* buffer size = cksize, so that
the size of this chunk is cksize + 8 */
};
/* the RIFF header */
struct riff_header
{
char sig[4]; /* "RIFF" */
uint32_t filesz; /* filesz = whole RIFF file size - 8B (sig & filesz) */
char filefmt[4]; /* e.g. AVI, WAVE */
/* the following is the first chunk */
struct riff_chunk chunk1;
};
LPDIRECTSOUNDBUFFER* dsound_load_wav(void *a1)
{
struct riff_header *wvbuf = (struct riff_header*)a1;
struct riff_chunk *aubuf;
if (pdsound == NULL || a1 == NULL)
return NULL;
char *sig = wvbuf->sig;
if (sig[0] != 'R' || sig[1] != 'I' || sig[2] != 'F' || sig[3] != 'F')
return NULL;
memcpy(&wav_format, &wvbuf->chunk1.buffer, 16);
wav_format.cbSize = 0;
uint32_t data_offset = wvbuf->chunk1.cksize + 20; /* offset of the second chunk */
while (1) {
aubuf = a1 + data_offset;
sig = aubuf->sig;
if (sig[0] == 'd' && sig[1] == 'a' && sig[2] == 't' && sig[3] == 'a') {
break;
}
data_offset += aubuf->cksize + 8; /* go to the next chunk */
}
uint32_t bufbytes = aubuf->cksize;
soundbuf_desc.dwSize = 20;
soundbuf_desc.dwFlags = 0xe2;
soundbuf_desc.dwBufferBytes = bufbytes;
soundbuf_desc.dwReserved = 0;
soundbuf_desc.lpwfxFormat = &wav_format;
LPDIRECTSOUNDBUFFER *local_buf; /* esp */
DWORD pvAudioPtr[2]; /* esp+4, esp+8 */
DWORD dwAudioBytes[2]; /* esp+c, esp+0x10 */
if ((*pdsound)->CreateSoundBuffer(pdsound, &soundbuf_desc, &local_buf, NULL) != 0)
return 0;
/* STDMETHOD(Lock)(THIS_ DWORD dwOffset, DWORD dwBytes, LPVOID *ppvAudioPtr1, LPDWORD pdwAudioBytes1, LPVOID *ppvAudioPtr2, LPDWORD pdwAudioBytes2, DWORD dwFlags) PURE;
*/
int res = (*local_buf)->Lock(local_buf, 0, bufbytes,
&pvAudioPtr[0], &dwAudioBytes[0],
&pvAudioPtr[1], &dwAudioBytes[1], 0);
if (res == 0x88780096) {
(*local_buf)->Restore(local_buf);
res = (*local_buf)->Lock(local_buf, 0, bufbytes,
&pvAudioPtr[0], &dwAudioBytes[0],
&pvAudioPtr[1], &dwAudioBytes[1], 0);
}
if (res != 0)
return 0;
memcpy(pvAudioPtr[0], aubuf->buffer, dwAudioBytes[0]);
if (bufbytes != dwAudioBytes[0]) {
memcpy(pvAudioPtr[1], aubuf->buffer + dwAudioBytes[0], dwAudioBytes[1]);
}
/* STDMETHOD(Unlock)(THIS_ LPVOID pvAudioPtr1, DWORD dwAudioBytes1, LPVOID pvAudioPtr2, DWORD dwAudioPtr2) PURE; */
(*local_buf)->Unlock(local_buf, &pvAudioPtr[0], dwAudioBytes[0], &pvAudioPtr[1], dwAudioBytes[1]);
return local_buf;
}
void fcn_004541e3()
{
if (pdsound == NULL)
return;
for (int i = 0; i < 16; i++) {
int *ptr = array_48cae8[i];
if (ptr == NULL)
continue;
while (1) {
int t = *ptr;
if (t == -1)
break;
data = read_mkf(mkf_effect, t, NULL, NULL);
ptr[1] = dsound_load_wav(data);
free(data);
ptr += 2;
}
}
}
void direct_sound_init(int a0)
{
if (pdsound == NULL && bool_47e76c == 0) {
if (DirectSoundCreate(NULL, &pdsound, NULL) != 0) {
MessageBoxA(NULL, "DirectSound Initial Error or SoundCard Not Found!", "WARNING", 0x10);
pdsound = NULL;
bool_47e76c = 1;
return 0;
}
if ((*pdsound)->SetCooperativeLevel(pdsound, gwindowHandle, 3) != 0) {
MessageBoxA(NULL, "DirectSound SetCooperativeLevel Error!", "ERROR", 0x10);
(*pdsound)->Release(pdsound);
pdsound = NULL;
bool_47e76c = 1;
return 0;
}
soundbuf_desc.dwSize = 20;
soundbuf_desc.dwFlags = 1;
soundbuf_desc.dwBufferBytes = 0;
soundbuf_desc.dwReserved = 0;
soundbuf_desc.lpwfxFormat = NULL;
if ((*pdsound)->CreateSoundBuffer(pdsound, &soundbuf_desc, &pdsoundbuf, NULL) != 0) {
MessageBoxA(NULL, "DirectSound CreateSoundPrimaryBuffer Error!", "ERROR", 0x10);
(*pdsound)->Release(pdsound);
pdsound = NULL;
bool_47e76c = 1;
return 0;
}
/* 0x00453c5a */
wav_format.wFormatTag = 1;
wav_format.nChannels = 1;
wav_format.nSamplesPerSec = 22050;
wav_format.nAvgBytesPerSec = 22050;
wav_format.nBlockAlign = 1;
wav_format.wBitsPerSample = 8;
wav_format.cbSize = 0;
if ((*pdsoundbuf)->SetFormat(pdsoundbuf, &wav_format) != 0) {
MessageBoxA(NULL, "DirectSound SetFormat Error!", "ERROR", 0x10);
(*pdsound)->Release(pdsound);
pdsound = NULL;
(*pdsoundbuf)->Release(pdsoundbuf);
pdsoundbuf = NULL;
bool_47e76c = 1;
return 0;
}
}
if (a0 != 0) {
fcn_004541e3();
} else {
memset(array_48cae8, 0, sizeof(array_48cae8));
}
dw_48cae4 = dw_47e750 = dw_47e754 = 0;
return 1;
}
void fcn_0044f935()
{
sfdesc1.dwFlags = 7;
sfdesc1.ddsCaps = 0x840;
sfdesc1.dwWidth = 0x200;
sfdesc1.dwHeight = 200;
(*pddraw)->CreateSurface(pddraw, &sfdesc1, &pddrawsf3, NULL);
(*pddrawsf3)->Lock(pddrawsf3, NULL, &sfdesc1, 1, NULL);
memcpy(*(uint32_t*)0x48a08c, 0, 0x32000);
(*pddrawsf3)->Unlock(pddrawsf3, NULL);
}
void fcn_0045175d()
{
DDPIXELFORMAT fmt;
fmt.dwSize = 0x20;
(*pddrawsf1)->GetPixelFormat(pddrawsf1, &fmt);
if (fmt.DUMMYUNIONNAME2.dwRBitMask == 0x7c00
&& fmt.DUMMYUNIONNAME3.dwGBitMask == 0x03e0)
dw_47637c = 0;
if (fmt.DUMMYUNIONNAME2.dwRBitMask == 0xf800
&& fmt.DUMMYUNIONNAME3.dwGBitMask == 0x07e0)
dw_47637c = 1;
if (fmt.DUMMYUNIONNAME2.dwRBitMask == 0x001f
&& fmt.DUMMYUNIONNAME3.dwGBitMask == 0x07e0)
dw_47637c = 2;
if (fmt.DUMMYUNIONNAME2.dwRBitMask == 0x0f00
&& fmt.DUMMYUNIONNAME3.dwGBitMask == 0x00f0)
dw_47637c = 3;
}
void load_sound_from_mkf(int32_t *a1)
{
int32_t *t = a1;
if (pdsound == NULL)
return;
while (t[0] != -1) {
char *data = read_mkf(mkf_effect, t[0], NULL, NULL);
t[1] = dsound_load_wav(data);
free(data);
t += 2;
}
for (int i = 0; i < 16; i++) {
if (array_48cae8[i] != NULL)
continue;
array_48cae8[i] = a1;
break;
}
}
void init_data_and_timer()
{
if (dw_46cb10 != 0)
return;
/* edx = 0 */
dw_46cb10 = read_mkf(mkf_data, 0, NULL, NULL);
dw_48a0e8 = allocate_some_struct(32, 32, 0, 0);
b_48a179 = b_48a17a = b_48a178 = 0;
dw_48a168 = -1;
ShowCursor(0);
sfdesc2.dwSize = 0x6c;
IDirectDrawSurface_Lock(pddrawsf1, NULL, &sfdesc2, 1, 0);
w_46cb14 = sfdesc2.DUMMYUNIONNAME1.lPitch >> 1;
IDirectDrawSurface_Unlock(pddrawsf1, NULL);
gTimerEvent = timeSetEvent(20 /* uDelay */, 5 /* uResolution */, timeProc /* TODO @ 0x401f98 */,
0 /* dwUser */, 1 /* fuEvent */);
}
bool initialize()
{
direct_sound_init(0);
HRESULT res = DirectDrawCreate(NULL, &pddraw, NULL);
if (res != 0) {
MessageBoxA(0, "DirectDraw Initial Error!", "ERROR", 16);
return 0;
}
(*pddraw)->SetCooperativeLevel(pddraw, gwindowHandle, 17);
if ((*pddraw)->SetDisplayMode(pddraw, 640, 480, 16) != 0) {
MessageBoxA(0, "DirectDraw SetMode Error!", "ERROR", 16);
return 0;
}
sfdesc1.dwSize = 0x6c;
sfdesc1.dwFlags = 1;
sfdesc1.ddsCaps = 0x200;
(*pddraw)->CreateSurface(pddraw, &sfdesc1, &pddrawsf1, NULL);
(*pddrawsf1)->Restore(pddrawsf1);
(*pddrawsf1)->Lock(pddrawsf1, NULL, &sfdesc1, 1, NULL);
dw_48a060 = sfdesc1.DUMMYUNIONNAME1.dwLinearSize;
(*pddrawsf1)->Unlock(pddrawsf1, NULL);
sfdesc1.dwFlags = 7;
sfdesc1.ddsCaps = 0x840;
sfdesc1.dwWidth = 640;
sfdesc1.dwHeight = 480;
(*pddraw)->CreateSurface(pddraw, &sfdesc1, &pddrawsf2, NULL);
srand(GetTicketCount());
fcn_0044f935();
fcn_0045175d();
mkf_data = load_mkf("data.mkf");
mkf_speaking = load_mkf("speaking.mkf");
mkf_panel = load_mkf("panel.mkf");
mkf_effect = load_mkf("effect.mkf");
load_sound_from_mkf(0x48231a);
config_rich4();
ghook = SetWindowsHookExA(WH_KEYBOARD, KbdProc /* TODO @ 0x401010 */, ghInstance, 0);
init_data_and_timer();
mciSendStringA_5ba(); /* 0x4545ba */
fcn_004021f8(0x29, 1, 0);
b_46caf8 = 0;
b_46caf9 = 0;
b_46cafd = 0;
mid_status[5] = 0;
memset(0x4990f0, 0, 4);
dw_474938 = malloc(387200);
dw_474930 = dw_474934 = dw_48a010 = dw_46cad8 = 0;
b_47e771 = 0;
return 1;
}
|