MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
mutatris_window.hpp
Go to the documentation of this file.
1#ifndef MUTATRIS_WINDOW_HPP
2#define MUTATRIS_WINDOW_HPP
3
4#include <SDL3/SDL.h>
5
6#include <array>
7#include <memory>
8#include <random>
9#include <string>
10#include <vector>
11
12#include "mxvk/mxvk.hpp"
13#include "mxvk/mxvk_console.hpp"
15#if defined(MXVK_WITH_MIXER) || defined(WITH_MIXER)
16#include "mxvk/mxvk_sound.hpp"
17#endif
18
19#include "common.hpp"
20#include "puzzle_game.hpp"
21
22namespace mutatris {
23
24 class MutatrisWindow final : public mxvk::VK_Window {
25 public:
26 MutatrisWindow(const std::string &path, int width, int height, bool fullscreen, bool enableVsync, bool enableCrt);
27
28 void event(SDL_Event &e) override;
29 void proc() override;
30
31 private:
32 struct BoardLayout {
33 int x = 0;
34 int y = 0;
35 int gridIndex = 0;
36 };
37
38 std::string assetRoot;
39 std::string dataRoot;
40 std::string shaderRoot;
41 std::string fontPath;
42 std::array<mxvk::VK_Sprite *, 8> blocks{};
43 std::array<mxvk::VK_Sprite *, 11> backgrounds{};
44 std::vector<std::string> effectShaders;
46 mxvk::VK_Controller controller{};
47 mxvk::VK_Sprite *intro = nullptr;
48 mxvk::VK_Sprite *start = nullptr;
49 mxvk::VK_Sprite *lostLogo = nullptr;
50 mxvk::VK_Sprite *jbLogo = nullptr;
51 mxvk::VK_Sprite *gameOver = nullptr;
52 mxvk::VK_Sprite *highScore = nullptr;
53 mxvk::Font titleFont;
54 mxvk::Font titleLargeFont;
55 mxvk::Font startMediumFont;
56 mxvk::Font startSmallFont;
57 mxvk::Font difficultyFont;
58 std::unique_ptr<PuzzleGame> game;
59 Screen screen = Screen::Startup;
60 int difficulty = 0;
61 int focus = 0;
62 Uint32 startupStartTick = 0;
63 int uiFontSize = 0;
64 int introFontSize = 0;
65 Uint32 lastDropTick = 0;
66 Uint32 lastInputTick = 0;
67 Uint32 lastKeyboardInputTick = 0;
68 Uint32 lastClearAnimationTick = 0;
69 int finalScore = 0;
70 int finalClears = 0;
71 int width = DESIGN_WIDTH;
72 int height = DESIGN_HEIGHT;
73 std::mt19937 shaderRandom{std::random_device{}()};
74 int shaderLevel = -1;
75 int shaderIndex = -1;
76 int backgroundIndex = -1;
77 bool crtEnabled = false;
78#if defined(MXVK_WITH_MIXER) || defined(WITH_MIXER)
79 std::unique_ptr<mxvk::VK_Mixer> soundEffects{};
80#endif
81 int musicTrack = -1;
82 int lineSound = -1;
83 int openSound = -1;
84
85 void configureConsole();
86 void logMutatris(const std::string &message);
87 [[nodiscard]] const char *screenName(Screen value) const;
88 [[nodiscard]] const char *focusName(int value) const;
89 void setScreen(Screen nextScreen, const std::string &reason);
90 void loadSprites();
91 void loadEffectShaders();
92 void loadSoundEffects();
93 void playSound(int soundId);
94 void ensureMusicPlaying();
95 void handleKey(SDL_Keycode key);
96 void handleGamepad(Uint8 button);
97 void handleGamepadDpad(Uint8 button);
98 void handleConfirm();
99 [[nodiscard]] bool isStartupTitleFullyVisible() const;
100 void pollKeyboardInput();
101 void handleDirectionalKey(SDL_Keycode key);
102 void softDropActivePiece();
103 [[nodiscard]] bool activePiecePlacementBlocked() const;
104 void triggerGameOver(const std::string &reason);
105 bool openController();
106 void syncControllerConnection();
107 void processGrid();
108 void advanceFocus();
109 void startGame();
110 void drawStartup();
111 void drawStartupLogo(mxvk::VK_Sprite *sprite, float alphaValue);
112 void drawTitle();
113 void drawTitleWithAlpha(float alphaValue);
114 void drawDifficulty();
115 void updatePlaying();
116 void updateBackgroundShaderForLevel();
117 std::string switchBackgroundShader(bool force);
118 void selectRandomBackground();
119 [[nodiscard]] mxvk::VK_Sprite *currentBackground();
120 [[nodiscard]] static std::string backgroundName(int index);
121 void drawGameOver();
122 void twistClearedBlocks();
123 void drawGrid(int gridIndex);
124 [[nodiscard]] BoardLayout boardLayout(int gridIndex) const;
125 void drawGridFrame(const BoardLayout &layout, bool selected);
126 void drawCell(const BoardLayout &layout, int cellX, int cellY, int color);
127 [[nodiscard]] float layoutScale() const;
128 void ensureUiFont();
129 void ensureIntroFonts();
130 void resetScaledFont(mxvk::Font &font, int designSize, float scale);
131 void drawTextCentered(const std::string &text, int y, SDL_Color color);
132 void drawTextCenteredInRect(const std::string &text, int x, int y, int w, int h, SDL_Color color, const mxvk::Font &font);
133 void printScaledText(const std::string &text, int x, int y, SDL_Color color);
134 void printScaledText(const std::string &text, int x, int y, SDL_Color color, const mxvk::Font &font);
135 [[nodiscard]] int scaleX(int value) const;
136 [[nodiscard]] int scaleY(int value) const;
137 };
138
139} // namespace mutatris
140
141#endif
void event(SDL_Event &e) override
Handle one SDL event.
MutatrisWindow(const std::string &path, int width, int height, bool fullscreen, bool enableVsync, bool enableCrt)
void proc() override
Execute one processing/update step.
Small RAII wrapper for an SDL_ttf font handle.
Definition mxvk_text.hpp:46
Lightweight command console renderer for mxvk::VK_Window.
RAII wrapper for SDL_Gamepad (standard layout mapping).
Main Vulkan window wrapper for MXVK.
Definition mxvk.hpp:37
In-game command console for MXVK / Vulkan windows.
SDL3 joystick and gamepad RAII wrappers.
SDL3_mixer audio subsystem wrapper.
constexpr int DESIGN_WIDTH
Definition common.hpp:8
constexpr int DESIGN_HEIGHT
Definition common.hpp:9