1 /* $Id: winAudio.c,v 5.0 2001/04/07 20:00:59 dik Exp $
2 *
3 * XPilot, a multiplayer gravity war game. Copyright (C) 1991-2001 by
4 *
5 * Bj�rn Stabell <bjoern@xpilot.org>
6 * Ken Ronny Schouten <ken@xpilot.org>
7 * Bert Gijsbers <bert@xpilot.org>
8 * Dick Balaska <dick@xpilot.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 /***************************************************************************\
26 * winAudio.c - XPilotNT Windoze audio interface module *
27 \***************************************************************************/
28 #ifdef SOUND
29
30 #include "windows.h"
31
32 #include "winAudio.h"
33 #include <io.h>
34 #include <fcntl.h>
35 #include <mmsystem.h>
36
37 #define SNDQSIZE 32
38 static char *SndQ = NULL;
39 static long SndSize = 0;
40 static BOOL SndFlag = FALSE;
41 static HANDLE hPlayEvent = 0;
42
Win32PlaySounds(LPVOID Arg)43 static DWORD Win32PlaySounds(LPVOID Arg)
44 {
45 while (1)
46 {
47 if (WAIT_OBJECT_0 == WaitForSingleObject(hPlayEvent, 100000))
48 {
49 char *pFileName = SndQ;
50 ResetEvent(hPlayEvent);
51 SndQ = NULL;
52 PlaySound(pFileName, NULL, SND_SYNC | SND_FILENAME | SND_NODEFAULT);
53 }
54 }
55 return 0;
56 }
57
Win32GetFileSize(const char * filename)58 static long Win32GetFileSize(const char *filename)
59 {
60 int fd = _open(filename, O_BINARY | O_RDONLY);
61 long fileSize = -1L;
62 if (fd >= 0)
63 {
64 fileSize = _filelength(fd);
65 close(fd);
66 }
67 return fileSize;
68 }
69
audioDeviceInit(char * display)70 int audioDeviceInit(char *display)
71 {
72 DWORD ThreadId;
73 HANDLE hThread;
74
75 hPlayEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
76 if (hPlayEvent == 0)
77 return -1;
78
79 if ((hThread = CreateThread(NULL, 1000, (LPTHREAD_START_ROUTINE)Win32PlaySounds,
80 NULL, 0, &ThreadId)) == NULL)
81 {
82 CloseHandle(hPlayEvent);
83 return -1;
84 }
85 SetThreadPriority(hThread, THREAD_PRIORITY_LOWEST);
86 return 0;
87 }
88
audioDeviceEvents()89 void audioDeviceEvents()
90 {}
91
audioDevicePlay(char * filename,int type,int volume,void ** private)92 void audioDevicePlay(char *filename, int type, int volume, void **private)
93 {
94 if (SndQ == NULL)
95 {
96 SndQ = filename;
97 SndSize = Win32GetFileSize(filename);
98 }
99 else
100 {
101 long size = Win32GetFileSize(filename);
102 if (size > SndSize)
103 {
104 SndQ = filename;
105 SndSize = size;
106 }
107 }
108 SetEvent(hPlayEvent);
109 }
110
111
112 #endif
113