R-Type  2
Doom but in better
Loading...
Searching...
No Matches
RealMain.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** Main.hpp
6*/
7
13#pragma once
14#include <any>
15#include <map>
16#include <tuple>
17#include <memory>
18#include <string>
19#include <cctype>
20#include <vector>
21#include <optional>
22#include <iostream>
23#include <algorithm>
24#include <typeindex>
25#include <exception>
26#include <stdexcept>
27#include <functional>
28#include <unordered_map>
29#include <toml++/toml.hpp>
30
31#include "Log.hpp"
32#include "LogMacros.hpp"
33#include "Utilities.hpp"
34#include "Constants.hpp"
35#include "TOMLLoader.hpp"
36#include "GUI/Network.hpp"
37#include "ActiveScreen.hpp"
38#include "SoundLib.hpp"
39#include "GUI/ECS/Demo.hpp"
40#include "GUI/ECS/Online.hpp"
41#include "CustomExceptions.hpp"
42#include "GUI/ECS/Systems.hpp"
48
49
54class Main {
55 public:
78 Main(const std::string &ip = "127.0.0.1", unsigned int port = 9000, unsigned int windowWidth = 800, unsigned int windowHeight = 600, bool windowCursor = true, bool windowFullscreen = false, const std::string &windowTitle = "R-Type", unsigned int windowX = 0, unsigned int windowY = 0, const std::string &windowCursorIcon = "NULL", bool imageIsSprite = false, bool spriteStartTop = false, bool spriteStartLeft = false, unsigned int spriteWidth = 20, unsigned int spriteHeight = 20, unsigned int frameLimit = 60, const std::string &configFilePath = "client_config.toml", const bool log = false, const bool debug = false, const std::string &player = "Player");
79 ~Main();
80
81 void run();
82
83 // Setters
84 void setIp(const std::string &ip);
85 void setPort(const unsigned int port);
86 void setWindowWidth(unsigned int width);
87 void setWindowCursor(bool cursorVisible);
88 void setWindowHeight(unsigned int height);
89 void setWindowFullscreen(bool fullscreen);
90 void setWindowTitle(const std::string &title);
91 void setWindowPositionX(unsigned int x);
92 void setWindowPositionY(unsigned int y);
93 void setWindowPosition(unsigned int x, unsigned int y);
94 void setWindowCursorIcon(const std::string cursorImage);
95 void setWindowCursorSprite(bool imageIsSprite);
96 void setWindowCursorSpriteWidth(unsigned int spriteWidth);
97 void setWindowCursorSpriteReadFromTop(bool spriteStartTop);
98 void setWindowCursorSpriteHeight(unsigned int spriteHeight);
99 void setWindowCursorSpriteReadFromLeft(bool spriteStartLeft);
100 void setWindowSize(unsigned int width, unsigned int height);
101 void setFrameLimit(unsigned int frameLimit);
102 void setConfigFile(const std::string &configFile);
103 void setLog(const bool debug);
104 void setDebug(const bool debug);
105 void setActiveScreen(const ActiveScreen screen);
106 void setPlayer(const std::string &player);
107
108 // Getters
109 const std::string getIp();
110 const unsigned int getPort();
111 unsigned int getWindowWidth();
112 unsigned int getWindowHeight();
113 bool getWindowCursor();
114 bool getWindowFullscreen();
115 const std::string &getWindowTitle();
116 const std::string &getWindowCursorIcon();
120 unsigned int getWindowCursorSpriteWidth();
121 unsigned int getWindowCursorSpriteHeight();
122 const bool getLog() const;
123 const bool getDebug() const;
124 unsigned int getFrameLimit() const;
125 std::string getConfigFile() const;
126 std::tuple<unsigned int, unsigned int> getWindowPosition();
127 std::tuple<unsigned int, unsigned int> getWindowSize();
128 const ActiveScreen getActiveScreen() const;
129 const std::string getActiveScreenAsString() const;
130 const std::string getPlayer() const;
131
132 private:
133 // Private helper methods
134 void _loadToml();
135 void _mainLoop();
136 std::string _lowerText(const std::string &text);
137
138 // Functions to check the state of elements
139 const bool _isIpInRange(const std::string &ip) const;
140 const bool _isPortCorrect(const unsigned int port) const;
141 const bool _isFilePresent(const std::string &filepath) const;
142 const bool _isFrameLimitCorrect(const unsigned int frameLimit) const;
143 const bool _isFontConfigurationCorrect(const TOMLLoader &node) const;
144 const bool _isIconConfigurationCorrect(const TOMLLoader &node) const;
145 const bool _isMusicConfigurationCorrect(const TOMLLoader &node) const;
146 const bool _isSpriteConfigurationCorrect(const TOMLLoader &node) const;
147 const bool _isBackgroundConfigurationCorrect(const TOMLLoader &node) const;
148 const bool _isKeyPresentAndOfCorrectType(const TOMLLoader &node, const std::string &key, const toml::node_type &valueType) const;
149
165 template <typename Exception = CustomExceptions::InvalidFontConfiguration, typename ExceptionMissing = CustomExceptions::NoFontInConfigFile>
166 const TOMLLoader _getTOMLNodeContent(const TOMLLoader &node, const std::string &key, const std::string &keyAlt)
167 {
168 TOMLLoader ItemNode(_tomlContent);
169 const std::string tomlPath = _tomlContent.getTOMLPath();
170
171 if (_tomlContent.hasKey(key)) {
172 PRETTY_INFO << "Fetching the content for '" << key << "'." << std::endl;
173 PRETTY_INFO << "Fetching the type of the content '" << _tomlContent.getValueTypeAsString(key) << "'." << std::endl;
174 if (_tomlContent.getValueType(key) == toml::node_type::table) {
175 ItemNode = _tomlContent.getTable(key);
176 } else {
177 throw Exception(tomlPath, key, _tomlContent.getValueTypeAsString(key), _tomlContent.getTypeAsString(toml::node_type::table));
178 }
179 } else if (_tomlContent.hasKey(keyAlt)) {
180 PRETTY_INFO << "Fetching the content for '" << keyAlt << "'." << std::endl;
181 PRETTY_INFO << "Fetching the type of the content '" << _tomlContent.getValueTypeAsString(keyAlt) << "'." << std::endl;
182 if (_tomlContent.getValueType(keyAlt) == toml::node_type::table) {
183 ItemNode = _tomlContent.getTable(keyAlt);
184 } else {
185 throw Exception(tomlPath, keyAlt, _tomlContent.getValueTypeAsString(keyAlt), _tomlContent.getTypeAsString(toml::node_type::table));
186 }
187 } else {
188 PRETTY_CRITICAL << "The key " << key << " is missing from the config file, " <<
189 "the other supported key " << keyAlt << " wasn't found in the config file." << std::endl;
190 throw ExceptionMissing(_tomlContent.getTOMLPath(), key);
191 }
192
193 PRETTY_DEBUG << ItemNode.getRawTOML() << std::endl;
194 return ItemNode;
195 }
196
197 // Functions in charge of loading the ressources for the program
198 std::uint32_t _initialiseAudio();
199 std::uint32_t _initialiseFonts();
200 std::uint32_t _initialiseIcon();
201 std::uint32_t _initialiseSprites();
202 std::uint32_t _initialiseBackgrounds();
203
204 // Functions in charge of initialising connections and the base ressources
205 void _initialiseConnection();
206 void _initialiseRessources();
207
208 // Function in charge of updating the text displayed on screen when the ressources are loading
209 void _updateLoadingText(const std::string &detail = "Loading...");
210
211 // Function in charge of updating the mouse collisions for all renderable components
212 void _updateMouseForAllRendererables(const GUI::ECS::Systems::MouseInfo &mouse);
213
214 // Functions in charge of sending and receiving packets with the server
215 void _sendAllPackets();
216 void _processIncommingPackets();
217
218 // Ip string processing
219 const std::string _getIpChunk(const unsigned int index, const std::string &defaultValue) const;
220
221 // Text component fetching
222 const std::optional<std::shared_ptr<GUI::ECS::Components::TextComponent>> _getTextComponent(const std::string &textComponentKey);
223
224 // Functions in charge of managing a node from an ipv4
225 const std::string _incrementIpV4Node(const std::string &v4Section);
226 const std::string _decrementIpV4Node(const std::string &v4Section);
227
228 // Functions in charge of managing the port counter
229 const std::string _incrementPortCounter(const std::string &portSection);
230 const std::string _decrementPortCounter(const std::string &portSection);
231
232 // Functions to increment a specific chunk of the address
233 void _incrementIpChunkOne();
234 void _incrementIpChunkTwo();
235 void _incrementIpChunkThree();
236 void _incrementIpChunkFour();
237 void _incrementPortChunk();
238
239 // Functions to decrement a specific chunk of the address
240 void _decrementIpChunkOne();
241 void _decrementIpChunkTwo();
242 void _decrementIpChunkThree();
243 void _decrementIpChunkFour();
244 void _decrementPortChunk();
245
246 // Function in charge of returning the Event manager
247 const std::shared_ptr<GUI::ECS::Systems::EventManager> _getEventManager();
248
249 // Function in charge of creating text components
250 const std::shared_ptr<GUI::ECS::Components::TextComponent> _createText(const std::string &application, const std::string &name, const std::string &text, const GUI::ECS::Systems::Font &font, const unsigned int size = 40, const GUI::ECS::Systems::Colour &normal = GUI::ECS::Systems::Colour::Black, const GUI::ECS::Systems::Colour &hover = GUI::ECS::Systems::Colour::Black, const GUI::ECS::Systems::Colour &clicked = GUI::ECS::Systems::Colour::Black);
251
252 // Function in charge of creating buttons
253 const std::shared_ptr<GUI::ECS::Components::ButtonComponent> _createButton(const std::string &application, const std::string &title, std::function<void()> callback, const std::string &callbackName = "callback function", const int width = 40, const int height = 20, const int textSize = 20, const GUI::ECS::Systems::Colour &bg = GUI::ECS::Systems::Colour::Black, const GUI::ECS::Systems::Colour &normal = GUI::ECS::Systems::Colour::White, const GUI::ECS::Systems::Colour &hover = GUI::ECS::Systems::Colour::Yellow, const GUI::ECS::Systems::Colour &clicked = GUI::ECS::Systems::Colour::AliceBlue, const std::shared_ptr<GUI::ECS::Systems::Font> &textFont = nullptr);
254
255 // Functions in charge of extracting the x and y coordinates of the screen
256 const unsigned int _getScreenCenterX();
257 const unsigned int _getScreenCenterY();
258
259 // Screens for the gui
260 void _gameScreen(); //GAME
261 void _demoScreen(); //DEMO
262 void _settingsMenu(); //SETTINGS
263 void _unknownScreen(); //when the enum does not cover it
264 void _gameOverScreen(); //GAME_OVER
265 void _gameWonScreen(); //GAME_WON
266 void _mainMenuScreen(); //MENU
267 void _bossFightScreen(); //BOSS_FIGHT
268 void _connectionFailedScreen(); //CONNECTION_FAILED
269 void _connectionAddressScreen(); //CONNECTION_ADDRESS
270 void _lobbyList(); //LOBBY_LIST
271 void _lobbyRoom(); //LOBBY_ROOM
272
273 // Function related to managing sub components in the windows
274
275 // Action functions (functions used in button components)
276 void _goPlay(); // Game screen
277 void _goDemo(); // Demo screen
278 void _goHome(); // Main menu screen
279 void _goExit(); // Exit program
280 void _goSettings(); // Settings screen
281 void _goGameOver(); // Game over screen
282 void _goGameWon(); // Game won screen
283 void _goBossFight(); // Boss fight screen
284 void _goUnknown(); // When the screen is unknown
285 void _goConnectionFailed(); // Connection failed screen
286 void _goConnect(); // Connection in progress (this function will redirect the user to either the game or the connection failed)
287 void _goConnectionAddress(); // Connection changer screen
288 void _goLobbyList(); // Lobby list screen
289 void _goLobbyRoom(); // Lobby room screen
290
291 // Settings
292 void _toggleMusic(); // Function that will enable/disable the playing of music
293 void _toggleSound(); // Function that will enable/disable the playing of sound effects
294
295 // Musics
296
297 // Function in charge of updating the music status
298 void _updateMusicStatus();
299
300 // Main menu music
301 void _startMainMenuMusic();
302 void _stopMainMenuMusic();
303
304 // Game loop music
305 void _startGameLoopMusic();
306 void _stopGameLoopMusic();
307
308 // Boss fight music
309 void _startBossFightMusic();
310 void _stopBossFightMusic();
311
312 // Sound effects
313
314 void _shootSound();
315 void _damageSound();
316 void _deadSound();
317 void _buttonSound();
318 void _gameOverSound();
319 void _winSound();
320
321
322 void _testContent();
323
324 void _closeConnection();
325
326 // Private members
327
328 // ecs entity holder
329 std::unordered_map<std::type_index, std::vector<std::any>> _ecsEntities;
330
331 // settings information
332 std::string _ip;
333 unsigned int _port;
334 unsigned int _windowWidth;
335 unsigned int _windowHeight;
336 bool _windowCursor;
337 bool _windowFullscreen;
338 std::string _windowTitle;
339 unsigned int _windowX;
340 unsigned int _windowY;
341 std::string _windowCursorIcon;
342 bool _imageIsSprite;
343 bool _spriteStartTop;
344 bool _spriteStartLeft;
345 bool _log;
346 bool _debug;
347 std::string _player;
348 unsigned int _spriteWidth;
349 unsigned int _spriteHeight;
350 unsigned int _windowFrameLimit;
351
352 // Variable in charge of setting the maximum allowed port range
353 unsigned int _maximumPortRange = 64738;
354
355 // Entity id tracking
356 std::uint32_t _baseId = 0;
357
358 // Configuration file information
359 std::string _configFilePath = "client_config.toml";
360 TOMLLoader _tomlContent;
361
362 // ecs important indexes
363 unsigned int _iconIndex = 0;
364 unsigned int _loadingIndex = 0;
365 unsigned int _mainEventIndex = 0;
366 unsigned int _mainWindowIndex = 0;
367 unsigned int _titleFontIndex = 0;
368 unsigned int _bodyFontIndex = 0;
369 unsigned int _defaultFontIndex = 0;
370 unsigned int _buttonFontIndex = 0;
371
372 // ecs important key
373 std::string _mainMenuKey = "mainMenuButton";
374
375 // Current screen in focus
376 ActiveScreen _activeScreen = ActiveScreen::LOADING;
377
378 // Music tracking variables
379 bool _gameMusicStarted = false;
380 bool _mainMenuMusicStarted = false;
381 bool _bossFightMusicStarted = false;
382
383 // Variable in charge of informing the gui if we are connected
384 bool _connected = false;
385
386 // Variables in charge of tracking the keys for the ip's
387 const std::string _ipV4FirstChunkKey = "connectionAddressScreenIpV4FirstChunk";
388 const std::string _ipV4SecondChunkKey = "connectionAddressScreenIpV4SecondChunk";
389 const std::string _ipV4ThirdChunkKey = "connectionAddressScreenIpV4ThirdChunk";
390 const std::string _ipV4FourthChunkKey = "connectionAddressScreenIpV4FourthChunk";
391 const std::string _portV4ChunkKey = "connectionAddressScreenPortV4ChunkKey";
392
393 // Network manager
394 std::shared_ptr<GUI::Network::ThreadCapsule> _networkManager;
395 std::vector<GUI::Network::MessageNode> _bufferPackets;
396
397 // Singleplayer demo
399 bool _demoInitialised = false;
400 bool _demoStarted = false;
401
402 // Multiplayer online game
404 bool _onlineInitialised = false;
405 bool _onlineStarted = false;
406 bool _networkClassSet = false;
407 bool _connectionInitialised = false;
408
409 // Settings variables
410 bool _playMusic = true;
411 // bool _playMusic = false;
412
413 // Settings tokens
414 const std::string _playMusicToken = "settingsWindowPlayMusicButton";
415 const std::string _playSoundEffectsToken = "settingsWindowPlaySoundEffectsButton";
416};
417
418int RealMain(int argc, char **argv);
419void DisplayHelp(const std::string binName);
420void DisplayVersion(bool helpMode = false);
ActiveScreen
This is the enum class in charge of the window switcher code. This enum allows the mainloop of the pr...
This is the file in charge of containing the constants that are meant to be used throughout the progr...
File in charge of containing the custom errors that are going to be used for custom description error...
The file in charge of storing the files for the internal singleplayer demo.
This is the file that links the EntityNode children.
Header file for the EventManager class, responsible for managing input events.
Macro definitions for logging messages with varying levels of detail and formatting.
#define PRETTY_DEBUG
Debug log with details and colour.
#define PRETTY_INFO
Info log with details and colour.
#define PRETTY_CRITICAL
Critical log with details and colour.
This is the file in charge of containing the Log class (the one in charge of outputing info only when...
This is the file in charge of linking the child network ressource classes.
File in charge of containing the declarations for the ressources used in the online game.
void DisplayVersion(bool helpMode=false)
Displays version information for the program.
int RealMain(int argc, char **argv)
The main function for initializing and running the application.
Definition RealMain.cpp:277
void DisplayHelp(const std::string binName)
The main function of the help display.
Declaration of the SoundLib class for managing sounds in the ECS architecture.
Contains the TOMLLoader class for handling loading and navigation of TOML data.
Header file for the Window class, which handles the graphical window and rendering logic.
The Orchestrator class manages the overall game state, including entity creation, updates,...
The Orchestrator class manages the overall game state, including entity creation, updates,...
A class for representing and manipulating colors using RGBA components. Inherits from EntityNode to a...
Definition Colour.hpp:37
static const Colour Yellow
Definition Colour.hpp:613
static const Colour Black
Definition Colour.hpp:969
static const Colour White
Definition Colour.hpp:757
static const Colour AliceBlue
Definition Colour.hpp:469
Manages font entities in the GUI ECS.
Definition Font.hpp:45
The Main class is the main class of the program.
Definition RealMain.hpp:54
void run()
This is the function used to start the program's main section.
bool getWindowCursor()
Get the status of the window cursor.
const std::string getIp()
Get the value of the ip that was set.
const unsigned int getPort()
Get the value of the port.
std::tuple< unsigned int, unsigned int > getWindowPosition()
Get the window's position.
void setWindowFullscreen(bool fullscreen)
Start the window in full screen mode.
const ActiveScreen getActiveScreen() const
Function in charge of returning the screen that is currently active.
const std::string & getWindowCursorIcon()
Get the icon to display for the window's cursor.
void setWindowCursor(bool cursorVisible)
Set if the cursor is visible when in the window.
std::tuple< unsigned int, unsigned int > getWindowSize()
Get the windows dimensions.
void setDebug(const bool debug)
Toggle the debug mode for the program.
const std::string getActiveScreenAsString() const
Function in charge of returning the screen that is currently active but here, the type will be conver...
void setWindowCursorSprite(bool imageIsSprite)
Set if the image passed is of type sprite or not.
bool getWindowCursorSprite()
Get the status if the cursor is of type image or spritesheet.
~Main()
Destroy the Main:: Main object.
bool getWindowCursorSpriteReadFromLeft()
Get if the program is supposed to read from the left or the rigth.
unsigned int getWindowCursorSpriteWidth()
Get the width of the sprite texture.
void setWindowPositionX(unsigned int x)
Set the X position of the window.
void setWindowCursorSpriteReadFromTop(bool spriteStartTop)
Inform if the animation should start from the top.
void setFrameLimit(unsigned int frameLimit)
The function in charge of setting the frame Limit.
void setWindowCursorIcon(const std::string cursorImage)
Set the icon of the cursor (if the user wishes to change the default icon)
void setWindowCursorSpriteWidth(unsigned int spriteWidth)
Set the height for the sprite's overlay texture, which is used to draw the sprite's texture during an...
void setIp(const std::string &ip)
This is the function in charge of setting the ip on which the GUI is going to use to communicate abou...
std::string getConfigFile() const
Function in charge of returning the path to the config file.
unsigned int getWindowCursorSpriteHeight()
Get the height of the sprite texture.
void setWindowSize(unsigned int width, unsigned int height)
The window width and height that will be created.
bool getWindowFullscreen()
Get the status of the window (if it is in fullscreen)
const std::string getPlayer() const
Retrieves the player's name.
void setPlayer(const std::string &player)
Sets the player's name.
void setWindowPosition(unsigned int x, unsigned int y)
Set the position of the window.
unsigned int getWindowHeight()
Get the value of the window height.
unsigned int getFrameLimit() const
Function in charge of returning the current frame limit.
void setWindowHeight(unsigned int height)
Set the height of the window.
void setWindowCursorSpriteHeight(unsigned int spriteHeight)
Set the height for the sprite's overlay texture, which is used to draw the sprite's texture during an...
void setPort(const unsigned int port)
Set the port on which the GUI is going to connect to.
void setConfigFile(const std::string &configFile)
This is the function in charge of seting the config filepath.
void setWindowPositionY(unsigned int y)
Set the Y position of the window.
void setActiveScreen(const ActiveScreen screen)
Function in charge of updating the type of screen that is supposed to be displayed as well as change ...
void setLog(const bool debug)
Toggle the logging mode for the program.
bool getWindowCursorSpriteReadFromTop()
Get if the program is supposed to read from the top or not.
const std::string & getWindowTitle()
Get the title of the window.
const bool getLog() const
The function in charge of returning the status of the logging variable.
void setWindowWidth(unsigned int width)
Set the width of the window.
void setWindowTitle(const std::string &title)
Set the title of the window.
const bool getDebug() const
The function in charge of returning the status of the debug variable.
void setWindowCursorSpriteReadFromLeft(bool spriteStartLeft)
Inform if the animation should start from the left.
Main(const std::string &ip="127.0.0.1", unsigned int port=9000, unsigned int windowWidth=800, unsigned int windowHeight=600, bool windowCursor=true, bool windowFullscreen=false, const std::string &windowTitle="R-Type", unsigned int windowX=0, unsigned int windowY=0, const std::string &windowCursorIcon="NULL", bool imageIsSprite=false, bool spriteStartTop=false, bool spriteStartLeft=false, unsigned int spriteWidth=20, unsigned int spriteHeight=20, unsigned int frameLimit=60, const std::string &configFilePath="client_config.toml", const bool log=false, const bool debug=false, const std::string &player="Player")
Constructor for the Main class.
Definition MainClass.cpp:43
unsigned int getWindowWidth()
Get the value of the window width.
A utility class for parsing, navigating, and managing TOML files and data.
const toml::node_type getValueType(const std::string &key) const
Retrieves the type of a value for a specific key as a TOML node type.
const toml::table getRawTOML() const
Retrieves the raw TOML table.
const std::string getTOMLPath() const
Retrieves the path of the loaded TOML file.
toml::table getTable(const std::string &key) const
Retrieves a TOML table for a specific key.
const bool hasKey(const std::string &key) const
Checks if a specific key exists in the TOML data.
const std::string getValueTypeAsString(const std::string &key) const
Retrieves the type of a value for a specific key as a string.
const std::string getTypeAsString(const std::string &key) const
Retrieves the type of a value for a specific key as a string (alias).
This is the file that links the Components children.
This is the file that links the utility children ressources.