]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/sounds/sound.qh
Sounds: infer extensions
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / sounds / sound.qh
1 #ifndef SOUND_H
2 #define SOUND_H
3
4 // Play all sounds via sound7, for access to the extra channels.
5 // Otherwise, channels 8 to 15 would be blocked for a weird QW feature.
6 #ifdef SVQC
7         #define _sound(e, c, s, v, a) \
8                 do \
9                 { \
10                         entity __e = e; \
11                         if (!sound_allowed(MSG_BROADCAST, __e)) break; \
12                         sound7(__e, c, s, v, a, 0, 0); \
13                 } \
14                 while (0)
15 #else
16         #define _sound(e, c, s, v, a) sound7(e, c, s, v, a, 0, 0)
17 #endif
18 #define sound(e, c, s, v, a) _sound(e, c, Sound_fixpath(s), v, a)
19
20 CLASS(Sound, Object)
21         ATTRIB(Sound, m_id, int, 0)
22         ATTRIB(Sound, sound_str, string(), func_null)
23         CONSTRUCTOR(Sound, string() path)
24         {
25                 CONSTRUCT(Sound);
26                 this.sound_str = path;
27         }
28         #define Sound_fixpath(this) _Sound_fixpath((this).sound_str())
29         string _Sound_fixpath(string base)
30         {
31         if (base == "") return string_null;
32         #define extensions(x) \
33             x(wav) \
34             x(ogg) \
35             x(flac) \
36             /**/
37         string full, relative;
38         #define tryext(ext) { if (fexists(full = strcat("sound/", relative = strcat(base, "." #ext)))) break; }
39         do
40         {
41             extensions(tryext);
42 #undef tryext
43 #undef extensions
44             LOG_WARNINGF("Missing sound: \"%s\"\n", full);
45             return string_null;
46         }
47         while (0);
48         return relative;
49         }
50         METHOD(Sound, sound_precache, void(entity this))
51         {
52                 string s = Sound_fixpath(this);
53                 if (!s) return;
54                 LOG_TRACEF("precache_sound(\"%s\")\n", s);
55                 precache_sound(s);
56         }
57 ENDCLASS(Sound)
58
59 #endif