BGMの追加
守矢諏訪子 <suwako@076.moe>、2026-07-12 01:27
コミット bd29d77f5a3b0a682b39f1606f19459026f6bc01
・親 5c4764f
・ファイルを見る
8個のファイル、+199 -1
バイナリファイル
バイナリファイル
大き過ぎるので表示しません
大き過ぎるので表示しません
| @@ -0,0 +1,50 @@ | ||
| 1 | +#pragma once | |
| 2 | + | |
| 3 | +#include <util/types.hh> | |
| 4 | +#include <raylib.h> | |
| 5 | +#include <string_view> | |
| 6 | +#include <unordered_map> | |
| 7 | + | |
| 8 | +namespace tetris::sys { | |
| 9 | + struct SndMan { | |
| 10 | + public: | |
| 11 | + static SndMan &Get(); | |
| 12 | + | |
| 13 | + bool Init(); | |
| 14 | + void Shutdown(); | |
| 15 | + void Update(f32 dt); | |
| 16 | + | |
| 17 | + bool LoadSfx(std::string_view id, const u8 *data, i32 dataSize); | |
| 18 | + void PlaySfx(std::string_view id, f32 volume = 1.f); | |
| 19 | + | |
| 20 | + bool LoadBgm(std::string_view id, const u8 *intro, i32 introSize, const u8 *loop, i32 loopSize); | |
| 21 | + void PlayBgm(std::string_view id, f32 fadeTime = 0.f); | |
| 22 | + void StopBgm(f32 fadeTime = 0.f); | |
| 23 | + | |
| 24 | + void PauseBgm(); | |
| 25 | + void ResumeBgm(); | |
| 26 | + | |
| 27 | + void SetSfxVolume(f32 volume) { m_SfxVolume = volume; } | |
| 28 | + void SetBgmVolume(f32 volume) { m_BgmVolume = volume; } | |
| 29 | + | |
| 30 | + private: | |
| 31 | + SndMan() = default; | |
| 32 | + struct Sfx { | |
| 33 | + Sound sound{}; | |
| 34 | + }; | |
| 35 | + | |
| 36 | + struct Bgm { | |
| 37 | + Music intro{}; | |
| 38 | + Music loop{}; | |
| 39 | + bool introDone = false; | |
| 40 | + }; | |
| 41 | + | |
| 42 | + std::unordered_map<string, Sfx> m_Sfx; | |
| 43 | + std::unordered_map<string, Bgm> m_Bgm; | |
| 44 | + | |
| 45 | + Bgm *m_Current = nullptr; | |
| 46 | + f32 m_SfxVolume = 1.f; | |
| 47 | + f32 m_BgmVolume = 1.f; | |
| 48 | + f32 m_LastIntroTime = 0.f; | |
| 49 | + }; // struct SndMan | |
| 50 | +} // namespace tetris::sys |
変更
main.cc
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | #include <tetris/tetris.hh> |
| 2 | 2 | #include <util/types.hh> |
| 3 | +#include <tetris/sys/sndman.hh> | |
| 3 | 4 | #include <raylib.h> |
| 4 | 5 | |
| 5 | 6 | constexpr i32 screenWidth = 900; |
| @@ -11,6 +12,7 @@ | ||
| 11 | 12 | #endif |
| 12 | 13 | SetConfigFlags(FLAG_WINDOW_RESIZABLE); |
| 13 | 14 | InitWindow(screenWidth, screenHeight, "テトリス"); |
| 15 | + tetris::sys::SndMan::Get().Init(); | |
| 14 | 16 | SetExitKey(KEY_NULL); |
| 15 | 17 | SetTargetFPS(120); |
| 16 | 18 | |
| @@ -36,6 +38,7 @@ | ||
| 36 | 38 | } |
| 37 | 39 | |
| 38 | 40 | game.Close(); |
| 41 | + tetris::sys::SndMan::Get().Shutdown(); | |
| 39 | 42 | CloseWindow(); |
| 40 | 43 | |
| 41 | 44 | return 0; |
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | #include <tetris/scene/gamemain.hh> |
| 2 | 2 | #include <tetris/sys/imgman.hh> |
| 3 | +#include <tetris/sys/sndman.hh> | |
| 3 | 4 | #include <raylib.h> |
| 4 | 5 | #include <vector> |
| 5 | 6 | #include <cmath> |
| @@ -15,7 +16,12 @@ | ||
| 15 | 16 | #include <tetris/ass/vfx_smoke.hh> |
| 16 | 17 | #include <tetris/ass/vfx_sparkle.hh> |
| 17 | 18 | |
| 19 | +#include <tetris/ass/bgm_tetorisu_start.hh> | |
| 20 | +#include <tetris/ass/bgm_tetorisu_loop.hh> | |
| 21 | + | |
| 18 | 22 | namespace tetris::scene { |
| 23 | + i8 levelUpThreshold = 10; | |
| 24 | + | |
| 19 | 25 | Color tetrominoColors[7] = { |
| 20 | 26 | BLUE, |
| 21 | 27 | YELLOW, |
| @@ -99,6 +105,14 @@ | ||
| 99 | 105 | m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke15", __ass_whitePuff15_png, __ass_whitePuff15_png_len)); |
| 100 | 106 | m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke16", __ass_whitePuff16_png, __ass_whitePuff16_png_len)); |
| 101 | 107 | |
| 108 | + auto &snd = sys::SndMan::Get(); | |
| 109 | + if (!snd.LoadBgm("bgm_tetorisu", __ass_tetorisu_start_ogg, __ass_tetorisu_start_ogg_len, __ass_tetorisu_loop_ogg, __ass_tetorisu_loop_ogg_len)) { | |
| 110 | + TraceLog(LOG_WARNING, "Failed to load BGM!"); | |
| 111 | + } else { | |
| 112 | + snd.PlayBgm("bgm_tetorisu"); | |
| 113 | + } | |
| 114 | + //snd.LoadSfx("drop", __ass_sfx_drop_wav, __ass_sfx_drop_wav_len); | |
| 115 | + | |
| 102 | 116 | nextPiece = GetRandomValue(0, 6); |
| 103 | 117 | spawn(); |
| 104 | 118 | updateBoardLayout(); |
| @@ -109,10 +123,12 @@ | ||
| 109 | 123 | // TODO: ゲームオーバー |
| 110 | 124 | return; |
| 111 | 125 | } |
| 126 | + | |
| 112 | 127 | updateBoardLayout(); |
| 113 | 128 | handleInput(); |
| 114 | 129 | updateLandingEffects(); |
| 115 | 130 | updateClearEffects(); |
| 131 | + sys::SndMan::Get().Update(GetFrameTime()); | |
| 116 | 132 | |
| 117 | 133 | m_FallTimer += GetFrameTime(); |
| 118 | 134 | |
| @@ -134,6 +150,7 @@ | ||
| 134 | 150 | |
| 135 | 151 | void GameMain::DrawUpdate() { |
| 136 | 152 | sys::ImgMan::Get().DrawBackground(); |
| 153 | + | |
| 137 | 154 | Rectangle gameArea = { |
| 138 | 155 | boardPos.x - 160, |
| 139 | 156 | boardPos.y - 30, |
| @@ -145,9 +162,11 @@ | ||
| 145 | 162 | i32 titleSize = std::max(30, (i32)(m_SquareSize * 1.2f)); |
| 146 | 163 | DrawText("TETRIS", GetScreenWidth() / 2 - titleSize * 2, 30, titleSize, WHITE); |
| 147 | 164 | |
| 165 | +#ifndef NDEBUG | |
| 148 | 166 | if (m_ShakeTime > 0.f) { |
| 149 | 167 | DrawText(TextFormat("SHAKE: %.2f", m_ShakeTime), 20, 20, 20, RED); |
| 150 | 168 | } |
| 169 | +#endif | |
| 151 | 170 | |
| 152 | 171 | Vector2 drawPos = boardPos; |
| 153 | 172 | if (m_ShakeTime > 0.f) { |
| @@ -392,7 +411,7 @@ | ||
| 392 | 411 | m_Score += clearLine * clearLine * 100; |
| 393 | 412 | |
| 394 | 413 | i8 oldLevel = m_Level; |
| 395 | - m_Level = 1 + (m_Lines / 10); | |
| 414 | + m_Level = 1 + (m_Lines / levelUpThreshold); | |
| 396 | 415 | |
| 397 | 416 | if (m_Level > oldLevel && m_Level - 1 < (i8)m_Bg.size()) { |
| 398 | 417 | m_CurBg = m_Level - 1; |
| @@ -0,0 +1,126 @@ | ||
| 1 | +#include <tetris/sys/sndman.hh> | |
| 2 | + | |
| 3 | +namespace tetris::sys { | |
| 4 | + SndMan &SndMan::Get() { | |
| 5 | + static SndMan instance; | |
| 6 | + return instance; | |
| 7 | + } | |
| 8 | + | |
| 9 | + bool SndMan::Init() { | |
| 10 | + InitAudioDevice(); | |
| 11 | + return true; | |
| 12 | + } | |
| 13 | + | |
| 14 | + void SndMan::Shutdown() { | |
| 15 | + for (auto &pair : m_Sfx) { | |
| 16 | + UnloadSound(pair.second.sound); | |
| 17 | + } | |
| 18 | + m_Sfx.clear(); | |
| 19 | + | |
| 20 | + for (auto &pair : m_Bgm) { | |
| 21 | + UnloadMusicStream(pair.second.intro); | |
| 22 | + UnloadMusicStream(pair.second.loop); | |
| 23 | + } | |
| 24 | + m_Bgm.clear(); | |
| 25 | + m_Current = nullptr; | |
| 26 | + CloseAudioDevice(); | |
| 27 | + } | |
| 28 | + | |
| 29 | + void SndMan::Update(f32 dt) { | |
| 30 | + if (!m_Current) return; | |
| 31 | + | |
| 32 | + if (!m_Current->introDone) { | |
| 33 | + UpdateMusicStream(m_Current->intro); | |
| 34 | + f32 time = GetMusicTimePlayed(m_Current->intro); | |
| 35 | + | |
| 36 | + if (time < m_LastIntroTime) { | |
| 37 | + StopMusicStream(m_Current->intro); | |
| 38 | + | |
| 39 | + m_Current->introDone = true; | |
| 40 | + PlayMusicStream(m_Current->loop); | |
| 41 | + } | |
| 42 | + | |
| 43 | + m_LastIntroTime = time; | |
| 44 | + } else { | |
| 45 | + UpdateMusicStream(m_Current->loop); | |
| 46 | + } | |
| 47 | + } | |
| 48 | + | |
| 49 | + bool SndMan::LoadSfx(std::string_view id, const u8 *data, i32 dataSize) { | |
| 50 | + if (!data || dataSize <= 0) return false; | |
| 51 | + | |
| 52 | + Wave wave = LoadWaveFromMemory(".wav", data, dataSize); | |
| 53 | + if (wave.data == nullptr) return false; | |
| 54 | + | |
| 55 | + Sound snd = LoadSoundFromWave(wave); | |
| 56 | + UnloadWave(wave); | |
| 57 | + | |
| 58 | + if (snd.stream.buffer == nullptr) return false; | |
| 59 | + m_Sfx[string(id)] = { snd }; | |
| 60 | + return true; | |
| 61 | + } | |
| 62 | + | |
| 63 | + void SndMan::PlaySfx(std::string_view id, f32 volume) { | |
| 64 | + auto it = m_Sfx.find(string(id)); | |
| 65 | + if (it == m_Sfx.end()) return; | |
| 66 | + | |
| 67 | + SetSoundVolume(it->second.sound, volume * m_SfxVolume); | |
| 68 | + PlaySound(it->second.sound); | |
| 69 | + } | |
| 70 | + | |
| 71 | + bool SndMan::LoadBgm(std::string_view id, const u8 *intro, i32 introSize, const u8 *loop, i32 loopSize) { | |
| 72 | + if (!intro || !loop || introSize <= 0 || loopSize <= 0) return false; | |
| 73 | + | |
| 74 | + Bgm bgm; | |
| 75 | + bgm.intro = LoadMusicStreamFromMemory(".ogg", intro, introSize); | |
| 76 | + bgm.loop = LoadMusicStreamFromMemory(".ogg", loop, loopSize); | |
| 77 | + | |
| 78 | + bgm.intro.looping = false; | |
| 79 | + bgm.loop.looping = true; | |
| 80 | + | |
| 81 | + if (!IsMusicValid(bgm.intro) || !IsMusicValid(bgm.loop)) { | |
| 82 | + if (IsMusicValid(bgm.intro)) UnloadMusicStream(bgm.intro); | |
| 83 | + if (IsMusicValid(bgm.loop)) UnloadMusicStream(bgm.loop); | |
| 84 | + return false; | |
| 85 | + } | |
| 86 | + | |
| 87 | + m_Bgm.emplace(string(id), std::move(bgm)); | |
| 88 | + return true; | |
| 89 | + } | |
| 90 | + | |
| 91 | + void SndMan::PlayBgm(std::string_view id, f32 fadeTime) { | |
| 92 | + auto it = m_Bgm.find(string(id)); | |
| 93 | + if (it == m_Bgm.end()) return; | |
| 94 | + | |
| 95 | + if (m_Current) { | |
| 96 | + StopMusicStream(m_Current->intro); | |
| 97 | + StopMusicStream(m_Current->loop); | |
| 98 | + } | |
| 99 | + | |
| 100 | + m_Current = &it->second; | |
| 101 | + m_Current->introDone = false; | |
| 102 | + PlayMusicStream(m_Current->intro); | |
| 103 | + } | |
| 104 | + | |
| 105 | + void SndMan::StopBgm(f32 fadeTime) { | |
| 106 | + if (m_Current) { | |
| 107 | + StopMusicStream(m_Current->intro); | |
| 108 | + StopMusicStream(m_Current->loop); | |
| 109 | + m_Current = nullptr; | |
| 110 | + } | |
| 111 | + } | |
| 112 | + | |
| 113 | + void SndMan::PauseBgm() { | |
| 114 | + if (m_Current) { | |
| 115 | + PauseMusicStream(m_Current->intro); | |
| 116 | + PauseMusicStream(m_Current->loop); | |
| 117 | + } | |
| 118 | + } | |
| 119 | + | |
| 120 | + void SndMan::ResumeBgm() { | |
| 121 | + if (m_Current) { | |
| 122 | + ResumeMusicStream(m_Current->intro); | |
| 123 | + ResumeMusicStream(m_Current->loop); | |
| 124 | + } | |
| 125 | + } | |
| 126 | +} // namespace tetris::sys |