| 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 | #include <tetris/scene/title.hh> |
| 36 | #include <tetris/sys/imgman.hh> |
| 37 | #include <tetris/sys/assman.hh> |
| 38 | #include <tetris/sys/sndman.hh> |
| 39 | #include <raylib.h> |
| 40 | |
| 41 | #include <tetris/ass/tex_title_main.hh> |
| 42 | #include <tetris/ass/tex_title_htp.hh> |
| 43 | |
| 44 | #include <tetris/ass/bgm_title_start.hh> |
| 45 | #include <tetris/ass/bgm_title_loop.hh> |
| 46 | |
| 47 | #include <cassert> |
| 48 | #include <cmath> |
| 49 | |
| 50 | namespace tetris::scene { |
| 51 | Texture2D titleBackground; |
| 52 | Texture2D htpBackground; |
| 53 | |
| 54 | void Title::Init(Game *game) { |
| 55 | m_Game = game; |
| 56 | titleBackground = sys::ImgMan::Get().LoadTexture("tx_titlebg", ass_TitleGamen_png, ass_TitleGamen_png_len); |
| 57 | htpBackground = sys::ImgMan::Get().LoadTexture("tx_htpbg", ass_TitleGamen_htp_png, ass_TitleGamen_htp_png_len); |
| 58 | sys::ImgMan::Get().SetBackground(m_Bg[0]); |
| 59 | |
| 60 | auto &snd = sys::SndMan::Get(); |
| 61 | if (!snd.LoadBgm("bgm_title", ass_title_intro_ogg, ass_title_intro_ogg_len, ass_title_loop_ogg, ass_title_loop_ogg_len)) { |
| 62 | TraceLog(LOG_WARNING, "Failed to load BGM!"); |
| 63 | } else { |
| 64 | snd.PlayBgm("bgm_title"); |
| 65 | } |
| 66 | |
| 67 | snd.LoadSfx("sfx_confirm", __ass_confirm_ogg, __ass_confirm_ogg_len); |
| 68 | snd.LoadSfx("sfx_quit", __ass_quit_ogg, __ass_quit_ogg_len); |
| 69 | snd.LoadSfx("sfx_select", __ass_select_ogg, __ass_select_ogg_len); |
| 70 | } |
| 71 | |
| 72 | void Title::FuncUpdate() { |
| 73 | handleInput(); |
| 74 | sys::SndMan::Get().Update(GetFrameTime()); |
| 75 | } |
| 76 | |
| 77 | void Title::DrawUpdate() { |
| 78 | sys::ImgMan::Get().DrawBackground(); |
| 79 | |
| 80 | if (m_CurMenu == Menu::TitleScreen) { |
| 81 | Font font = GetFontDefault(); |
| 82 | |
| 83 | Vector2 menuPos = { |
| 84 | GetScreenWidth() / 2.f - 120.f, |
| 85 | GetScreenHeight() / 2.f + 20.f, |
| 86 | }; |
| 87 | |
| 88 | constexpr f32 spacing = 40.f; |
| 89 | |
| 90 | auto DrawButton = [](Font font, cstr text, Vector2 pos, bool selected) { |
| 91 | f32 scale = selected ? 1.05f + 0.03f * sinf(GetTime() * 8.f) : 1.f; |
| 92 | Color color = selected ? WHITE : Fade(WHITE, 0.5f); |
| 93 | f32 sizePx = 20.f * scale; |
| 94 | Vector2 textSize = MeasureTextEx(font, text, sizePx, 0); |
| 95 | |
| 96 | DrawTextPro(font, text, pos, { textSize.x / 2.f, textSize.y / 2.f }, 0.f, sizePx, 2.f, color); |
| 97 | if (selected) DrawTextPro(font, ">", { pos.x - textSize.x / 2.f - 30.f, pos.y }, { 0.f, textSize.y / 2.f }, 0.f, sizePx, 4.f, color); |
| 98 | }; |
| 99 | |
| 100 | DrawButton(font, "GAME START", { menuPos.x, menuPos.y + spacing * 0 }, m_MenuSelect == Menu::TitleScreen); |
| 101 | DrawButton(font, "HOW TO PLAY", { menuPos.x, menuPos.y + spacing * 1 }, m_MenuSelect == Menu::HowToPlay); |
| 102 | DrawButton(font, "CREDITS", { menuPos.x, menuPos.y + spacing * 2 }, m_MenuSelect == Menu::Credits); |
| 103 | DrawButton(font, "QUIT", { menuPos.x, menuPos.y + spacing * 3 }, m_MenuSelect == Menu::Quit); |
| 104 | } else if (m_CurMenu == Menu::Credits) { |
| 105 | auto DrawText = [](Font font, cstr text, Vector2 pos) { |
| 106 | f32 scale = 1.f; |
| 107 | Color color = WHITE; |
| 108 | f32 sizePx = 20.f * scale; |
| 109 | Vector2 textSize = MeasureTextEx(font, text, sizePx, 0); |
| 110 | |
| 111 | DrawTextPro(font, text, pos, { textSize.x / 2.f, textSize.y / 2.f }, 0.f, sizePx, 2.f, color); |
| 112 | }; |
| 113 | Font font = GetFontDefault(); |
| 114 | constexpr f32 spacing = 30.f; |
| 115 | |
| 116 | const i32 lastLine = 15; |
| 117 | const f32 blockHeight = lastLine * spacing; |
| 118 | const f32 startY = GetScreenHeight() / 2.f - blockHeight / 2.f; |
| 119 | Vector2 pos = { GetScreenWidth() / 2.f, startY }; |
| 120 | |
| 121 | DrawText(font, "CODE/DESIGN/ART:", { pos.x, pos.y + spacing * 0 }); |
| 122 | DrawText(font, "TechnicalSuwako", { pos.x, pos.y + spacing * 1 }); |
| 123 | |
| 124 | DrawText(font, "MUSIC:", { pos.x, pos.y + spacing * 3 }); |
| 125 | DrawText(font, "hiiragi_magne (x.com/hiiragi_magne)", { pos.x, pos.y + spacing * 4 }); |
| 126 | DrawText(font, "Jutenks (youtube.com/@JutenksViak)", { pos.x, pos.y + spacing * 5 }); |
| 127 | DrawText(font, "Summoning Beatz (youtube.com/@summoningbeatz)", { pos.x, pos.y + spacing * 6 }); |
| 128 | |
| 129 | DrawText(font, "HAIKEI:", { pos.x, pos.y + spacing * 8 }); |
| 130 | DrawText(font, "brzozkamarcin", { pos.x, pos.y + spacing * 9 }); |
| 131 | DrawText(font, "(pixabay.com/users/brzozkamarcin-10835605)", { pos.x, pos.y + spacing * 10 }); |
| 132 | |
| 133 | DrawText(font, "SFX:", { pos.x, pos.y + spacing * 12 }); |
| 134 | DrawText(font, "PhatPhrogStudio", { pos.x, pos.y + spacing * 13 }); |
| 135 | DrawText(font, "(pixabay.com/users/phatphrogstudio-54178146)", { pos.x, pos.y + spacing * 14 }); |
| 136 | DrawText(font, "PRESS ESC FOR RETURN TO THE TITLE SCREEN", { pos.x, pos.y + spacing * 16 }); |
| 137 | } else if (m_CurMenu == Menu::HowToPlay) { |
| 138 | auto DrawText = [](Font font, cstr text, Vector2 pos) { |
| 139 | f32 scale = 1.f; |
| 140 | Color color = WHITE; |
| 141 | f32 sizePx = 20.f * scale; |
| 142 | Vector2 textSize = MeasureTextEx(font, text, sizePx, 0); |
| 143 | |
| 144 | DrawTextPro(font, text, pos, { 0.f, textSize.y / 2.f }, 0.f, sizePx, 2.f, color); |
| 145 | }; |
| 146 | Font font = GetFontDefault(); |
| 147 | constexpr f32 spacing = 30.f; |
| 148 | |
| 149 | const i32 lastLine = 12; |
| 150 | const f32 blockHeight = lastLine * spacing; |
| 151 | const f32 startY = GetScreenHeight() / 2.f - blockHeight / 2.f; |
| 152 | Vector2 pos = { 120.f, startY }; |
| 153 | |
| 154 | DrawText(font, "MOVING: LEFT/RIGHT/UP/DOWN OR WASD", { pos.x, pos.y + spacing * 0 }); |
| 155 | DrawText(font, "CONFIRM: ENTER", { pos.x, pos.y + spacing * 1 }); |
| 156 | DrawText(font, "PAUSE: ESC", { pos.x, pos.y + spacing * 2 }); |
| 157 | DrawText(font, "ROTATE: SPACE", { pos.x, pos.y + spacing * 3 }); |
| 158 | DrawText(font, "HOLD: CTRL", { pos.x, pos.y + spacing * 4 }); |
| 159 | DrawText(font, "PRESS ESC FOR RETURN TO THE TITLE SCREEN", { pos.x, pos.y + spacing * 10 }); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void Title::Close() { |
| 164 | sys::SndMan::Get().StopBgm(); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | |
| 169 | void Title::handleInput() { |
| 170 | if (IsKeyPressed(KEY_HOME) && m_MenuSelect != Menu::TitleScreen) { |
| 171 | sys::SndMan::Get().PlaySfx("sfx_select"); |
| 172 | m_MenuSelect = Menu::TitleScreen; |
| 173 | } |
| 174 | |
| 175 | if (IsKeyPressed(KEY_END) && m_MenuSelect != Menu::Quit) { |
| 176 | sys::SndMan::Get().PlaySfx("sfx_select"); |
| 177 | m_MenuSelect = Menu::Quit; |
| 178 | } |
| 179 | |
| 180 | switch (m_MenuSelect) { |
| 181 | case Menu::TitleScreen: { |
| 182 | if (m_CurMenu == Menu::TitleScreen) { |
| 183 | if (IsKeyPressed(KEY_DOWN) || IsKeyPressed(KEY_S)) { |
| 184 | sys::SndMan::Get().PlaySfx("sfx_select"); |
| 185 | m_MenuSelect = Menu::HowToPlay; |
| 186 | } |
| 187 | |
| 188 | if (IsKeyPressed(KEY_ENTER)) { |
| 189 | sys::SndMan::Get().PlaySfx("sfx_confirm"); |
| 190 | assert(m_Game != nullptr); |
| 191 | m_Game->SetNextScene(Scene::GameMain); |
| 192 | } |
| 193 | } |
| 194 | break; |
| 195 | } |
| 196 | case Menu::HowToPlay: { |
| 197 | if (m_CurMenu == Menu::TitleScreen) { |
| 198 | if (IsKeyPressed(KEY_DOWN) || IsKeyPressed(KEY_S)) { |
| 199 | sys::SndMan::Get().PlaySfx("sfx_select"); |
| 200 | m_MenuSelect = Menu::Credits; |
| 201 | } |
| 202 | if (IsKeyPressed(KEY_UP) || IsKeyPressed(KEY_W)) { |
| 203 | sys::SndMan::Get().PlaySfx("sfx_select"); |
| 204 | m_MenuSelect = Menu::TitleScreen; |
| 205 | } |
| 206 | |
| 207 | if (m_CurMenu == Menu::TitleScreen && IsKeyPressed(KEY_ENTER)) { |
| 208 | sys::SndMan::Get().PlaySfx("sfx_confirm"); |
| 209 | sys::ImgMan::Get().SetBackground(m_Bg[1]); |
| 210 | m_CurMenu = Menu::HowToPlay; |
| 211 | } |
| 212 | } else { |
| 213 | if (m_CurMenu == Menu::HowToPlay && IsKeyPressed(KEY_ESCAPE)) { |
| 214 | sys::SndMan::Get().PlaySfx("sfx_quit"); |
| 215 | sys::ImgMan::Get().SetBackground(m_Bg[0]); |
| 216 | m_CurMenu = Menu::TitleScreen; |
| 217 | } |
| 218 | } |
| 219 | break; |
| 220 | } |
| 221 | case Menu::Credits: { |
| 222 | if (m_CurMenu == Menu::TitleScreen) { |
| 223 | if (IsKeyPressed(KEY_DOWN) || IsKeyPressed(KEY_S)) { |
| 224 | sys::SndMan::Get().PlaySfx("sfx_select"); |
| 225 | m_MenuSelect = Menu::Quit; |
| 226 | } |
| 227 | if (IsKeyPressed(KEY_UP) || IsKeyPressed(KEY_W)) { |
| 228 | sys::SndMan::Get().PlaySfx("sfx_select"); |
| 229 | m_MenuSelect = Menu::HowToPlay; |
| 230 | } |
| 231 | |
| 232 | if (m_CurMenu == Menu::TitleScreen && IsKeyPressed(KEY_ENTER)) { |
| 233 | sys::SndMan::Get().PlaySfx("sfx_confirm"); |
| 234 | sys::ImgMan::Get().SetBackground(m_Bg[1]); |
| 235 | m_CurMenu = Menu::Credits; |
| 236 | } |
| 237 | } else { |
| 238 | if (m_CurMenu == Menu::Credits && IsKeyPressed(KEY_ESCAPE)) { |
| 239 | sys::SndMan::Get().PlaySfx("sfx_quit"); |
| 240 | sys::ImgMan::Get().SetBackground(m_Bg[0]); |
| 241 | m_CurMenu = Menu::TitleScreen; |
| 242 | } |
| 243 | } |
| 244 | break; |
| 245 | } |
| 246 | case Menu::Quit: { |
| 247 | if (m_CurMenu == Menu::TitleScreen) { |
| 248 | if (IsKeyPressed(KEY_UP) || IsKeyPressed(KEY_W)) { |
| 249 | sys::SndMan::Get().PlaySfx("sfx_select"); |
| 250 | m_MenuSelect = Menu::Credits; |
| 251 | } |
| 252 | |
| 253 | if (m_CurMenu == Menu::TitleScreen && IsKeyPressed(KEY_ENTER)) { |
| 254 | sys::SndMan::Get().PlaySfx("sfx_quit"); |
| 255 | m_Game->isRunning = false; |
| 256 | } |
| 257 | } |
| 258 | break; |
| 259 | } |
| 260 | default: |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | } |