-
Notifications
You must be signed in to change notification settings - Fork 0
/
Visualizer.hpp
194 lines (187 loc) · 5.26 KB
/
Visualizer.hpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* @file Visualizer.hpp
* @brief Include dependencies for the Visualizer class.
* @date Created on 23-12-23.
* @author Renato Chavez
*/
#ifndef ALGOVISUALIZER_VISUALIZER_HPP
#define ALGOVISUALIZER_VISUALIZER_HPP
/**
* @brief SDL2 main header for window, rendering, and event handling.
*/
#include "SDL2/SDL.h"
/**
* @brief SDL2 extension for TrueType font rendering.
*/
#include "SDL2/SDL_ttf.h"
/**
* @brief Standard IO stream objects.
*/
#include <iostream>
/**
* @brief Maze class for maze representation and management.
*/
#include "Maze.hpp"
/**
* @brief FPSCounter class for rendering frames per second information.
*/
#include "FPSCounter.hpp"
/**
* @brief MazeRenderer class for visualizing the maze.
*/
#include "Square.hpp"
#include "Tetris.h"
#include <memory>
/**
* @brief Visualizer class for managing visualization tasks.
*
* This class encapsulates the functionality for visualizing mazes using SDL.
* It handles the creation and management of SDL windows and renderers,
* processes SDL events, and renders frames that include a maze and FPS counter.
*/
class Visualizer {
public:
Visualizer() = delete;
Visualizer(const Visualizer&) = delete;
Visualizer& operator=(const Visualizer&) = delete;
/**
* @brief Constructs a Visualizer object.
*
* Initializes SDL components, creates a window and renderer, and sets up TTF for font rendering.
*
* @param windowTitle The title of the SDL window.
* @param windowPosX The x-position of the SDL window.
* @param windowPosY The y-position of the SDL window.
* @param windowWidth The width of the SDL window.
* @param windowHeight The height of the SDL window.
* @param isFullscreen Whether the window should be fullscreen.
* @throws std::runtime_error if initialization of any SDL component fails.
*/
Visualizer(const char* windowTitle, int windowPosX, int windowPosY,
int windowWidth, int windowHeight, bool isFullscreen);
/**
* @brief Destructor for the Visualizer
*
* Cleans up SDL resources, including the window, renderer, and font.
*/
~Visualizer();
void setTetris(std::shared_ptr<Tetris> tetris);
/**
* @brief Processes SDL events.
*
* Currently handles the SDL_QUIT event to stop the application.
*/
void handleEvents();
/**
* @brief Updates the state of the visualizer.
*
* This function currently does not perform any action but can be used
* for future updates that require per-frame state changes.
*/
void update();
/**
* @brief Renders the current frame.
*
* Clears the screen, renders the maze and the FPS counter, and presents the updated frame.
*/
void render();
/**
* @brief Cleans up SDL resources.
*
* Destroys the SDL window, renderer, and closes the font.
*/
void clean();
/**
* @brief Checks if the visualizer is running.
* @param maze A shared pointer to the Maze object to be visualized.
*/
void setMaze(std::shared_ptr<Maze> maze);
/**
* @brief Checks if the visualizer is running.
* @return true if the visualizer is currently running, false otherwise.
*/
[[nodiscard]] bool running() const;
void addRenderable(std::shared_ptr<IRenderable> renderable);
[[nodiscard]] SDL_Window* getWindow() const {
return sdlWindow_;
}
private:
Uint32 windowID_;
/**
* @brief Initializes the SDl library.
*/
static void initializeSDL();
/**
* @brief Creates the SDL window based on stored parameters.
*/
void createWindow();
/**
* @brief Creates a SDL renderer for the window.
*/
void createRenderer();
/**
* @brief Initializes the SDL_ttf library for font rendering.
*/
void initializeTTF();
/**
* @brief Initializes all SDL components used by the Visualizer.
*/
void initializeSDLComponents();
/**
* @brief Shared pointer to the maze being visualized.
*/
std::shared_ptr<Maze> maze_;
/**
* @brief Flag indicating whether the visualizer is running.
*/
bool running_;
/**
* @brief Pointer to the SDL window.
*/
SDL_Window *sdlWindow_;
/**
* @brief Pointer to the SDL renderer
*/
SDL_Renderer *sdlRenderer_;
/**
* @brief Width of the SDL window.
*/
int windowWidth_;
/**
* @brief Height of the SDL window.
*/
int windowHeight_;
/**
* @brief Pointer to the TTF font used for rendering text.
*/
TTF_Font *fpsFont_;
/**
* @brief FPSCounter object for displaying frame rate.
*/
FPSCounter fpsCounter_;
/**
* @brief Renderer for the maze.
*/
//std::unique_ptr<MazeRenderer> mazeRenderer_;
/**
* @brief Title of the SDL window.
*/
const char* windowTitle_;
/**
* @brief X-position of the SDL window.
*/
int windowPosX_;
/**
* @brief Y-position of the SDL window.
*/
int windowPosY_;
/**
* @brief Indicates if the window is fullscreen.
*/
bool isFullscreen_;
std::vector<Square> squares_;
std::vector<std::shared_ptr<IRenderable>> renderables_;
std::shared_ptr<Tetris> tetrisGame_;
bool isFocused;
};
#endif //ALGOVISUALIZER_VISUALIZER_HPP