| 1 | /** |
| 2 | Copyright (c) テクニカル諏訪子 |
| 3 | |
| 4 | Permission is hereby granted to any person obtaining a copy of the software |
| 5 | Tetris (the "Software") to use, modify, merge, copy, publish, distribute, |
| 6 | sublicense, and/or sell copies of the Software, subject to the following conditions: |
| 7 | |
| 8 | 1. **Origin Attribution**: |
| 9 | - You must not misrepresent the origin of the Software; you must not claim |
| 10 | you created the original Software. |
| 11 | - If the Software is used in a product, you must either: |
| 12 | a. Provide clear attribution in the product's documentation, user interface, |
| 13 | or other visible areas, **OR** |
| 14 | b. Pay the original developers a fee they specify in writing. |
| 15 | 2. **Usage Restriction**: |
| 16 | - The Software, or any derivative works, dependencies, or libraries |
| 17 | incorporating it, must not be used for censorship or to suppress freedom of |
| 18 | speech, expression, or creativity. Prohibited uses include, but are not |
| 19 | limited to: |
| 20 | - Censorship of so-called "hate speech", visuals, non-mainstream opinions, |
| 21 | ideas, or objective reality. |
| 22 | - Tools or systems designed to restrict access to information or |
| 23 | artistic works. |
| 24 | 3. **Notice Preservation**: |
| 25 | - This license and the above copyright notice must remain intact in all copies |
| 26 | of the source code. |
| 27 | |
| 28 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 29 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 30 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 31 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 32 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 33 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 34 | */ |
| 35 | #pragma once |
| 36 | |
| 37 | #include <util/types.hh> |
| 38 | #include <raylib.h> |
| 39 | #include <string_view> |
| 40 | #include <unordered_map> |
| 41 | |
| 42 | namespace tetris::sys { |
| 43 | struct SndMan { |
| 44 | public: |
| 45 | static SndMan &Get(); |
| 46 | |
| 47 | bool Init(); |
| 48 | void Shutdown(); |
| 49 | void Update(f32 dt); |
| 50 | |
| 51 | bool LoadSfx(std::string_view id, const u8 *data, i32 dataSize); |
| 52 | void PlaySfx(std::string_view id, f32 volume = 1.f); |
| 53 | |
| 54 | bool LoadBgm(std::string_view id, const u8 *intro, i32 introSize, const u8 *loop, i32 loopSize); |
| 55 | void PlayBgm(std::string_view id, f32 fadeTime = 0.f); |
| 56 | void StopBgm(f32 fadeTime = 0.f); |
| 57 | |
| 58 | void PauseBgm(); |
| 59 | void ResumeBgm(); |
| 60 | void SetBgmPitch(f32 pitch); |
| 61 | |
| 62 | public: |
| 63 | void SetSfxVolume(f32 volume) { m_SfxVolume = volume; } |
| 64 | void SetBgmVolume(f32 volume) { m_BgmVolume = volume; } |
| 65 | |
| 66 | private: |
| 67 | SndMan() = default; |
| 68 | struct Sfx { |
| 69 | Sound sound{}; |
| 70 | }; |
| 71 | |
| 72 | struct Bgm { |
| 73 | Music intro{}; |
| 74 | Music loop{}; |
| 75 | bool introDone = false; |
| 76 | }; |
| 77 | |
| 78 | std::unordered_map<string, Sfx> m_Sfx; |
| 79 | std::unordered_map<string, Bgm> m_Bgm; |
| 80 | |
| 81 | Bgm *m_Current = nullptr; |
| 82 | f32 m_SfxVolume = 1.f; |
| 83 | f32 m_BgmVolume = 1.f; |
| 84 | f32 m_LastIntroTime = 0.f; |
| 85 | }; // struct SndMan |
| 86 | } // namespace tetris::sys |