R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
Rendering.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Created by samuelBleau on 26/11/2025.
4** File description:
5** Rendering.hpp
6*/
7
8#ifndef RENDERING_HPP
9#define RENDERING_HPP
10
11#include <cstdint>
12#include <memory>
13#include <string>
14#include <unordered_map>
15#include "../common/Logger/Logger.hpp"
17#include "Core/EventBus/EventBus.hpp"
18#include "EntityRenderer.hpp"
19#include "Events/UIEvent.hpp"
21
22// Audio
24
25// UI library
31#include "Menu/DefeatMenu.hpp"
33#include "Menu/LoginMenu.hpp"
34#include "Menu/MainMenu.hpp"
35#include "Menu/RoomListMenu.hpp"
37#include "Menu/SettingsMenu.hpp"
38#include "Menu/VictoryMenu.hpp"
40#include "UI/ChatWidget.hpp"
41#include "UI/IButton.hpp"
42#include "UI/IMenu.hpp"
44
62class Rendering {
63 public:
71 explicit Rendering(EventBus &eventBus);
72
78 ~Rendering();
79
95 bool Initialize(uint32_t width, uint32_t height, const std::string &title);
96
103 void Shutdown();
104
110 void ClearWindow();
111
122 void Render();
123
131 [[nodiscard]] bool IsWindowOpen() const;
132
143 void StartGame();
144
159 bool LoadTexture(const std::string &textureName, const std::string &path);
160
176 void DrawSprite(const std::string &textureId, float xPosition, float yPosition, float rotation = 0.0,
177 float scale = 1.0);
178
193 void DrawText(const std::string &text, float xPosition, float yPosition, uint32_t size = 24);
194
200 [[nodiscard]] uint32_t GetWidth() const;
201
207 [[nodiscard]] uint32_t GetHeight() const;
208
209 [[nodiscard]] bool WindowShouldClose() const;
210
211 // ═══════════════════════════════════════════════════════════
212 // Entity Rendering API (delegated to EntityRenderer)
213 // ═══════════════════════════════════════════════════════════
214
230 void UpdateEntity(uint32_t id, RType::Messages::Shared::EntityType type, float x, float y, int health,
231 const std::string &currentAnimation, int srcX, int srcY, int srcW, int srcH);
232
239 void RemoveEntity(uint32_t id);
240
247 void SetMyEntityId(uint32_t id);
248
254 void ClearAllEntities();
255
265 void SetBackground(const std::string &mainBackground, const std::string &parallaxBackground,
266 float scrollSpeed, float parallaxSpeedFactor);
267
273 void ClearBackground();
274
281 void UpdateBackground(float deltaTime);
282
291 void ShowGameOver(const std::string &reason);
292
298 bool IsKeyDown(int key) const;
299
305 bool IsGamepadAvailable(int gamepad) const;
306
313 bool IsGamepadButtonDown(int gamepad, int button) const;
314
324 void UpdateInterpolation(float deltaTime);
325
333 void UpdatePingTimer(float deltaTime);
334
346 void MoveEntityLocally(uint32_t entityId, float deltaX, float deltaY);
347
352 void UpdateRoomList(const std::vector<RoomData> &rooms);
353
361 void UpdateWaitingRoom(const std::vector<Game::PlayerInfo> &players, const std::string &roomName,
362 bool isHost, bool isSpectator = false);
363
371 void AddChatMessage(uint32_t playerId, const std::string &playerName, const std::string &message,
372 uint64_t timestamp);
373
378 void SetOnChatMessageSent(std::function<void(const std::string &)> callback);
379
384
391 void SetClientSidePredictionEnabled(bool enabled);
392
410 void SetReconciliationThreshold(float threshold);
411
418 float GetReconciliationThreshold() const;
419
427 void SetLocalPlayerMoving(bool moving);
428
440 void SetPing(uint32_t pingMs);
441
445 void SetShowPing(bool enabled);
446
450 [[nodiscard]] bool GetShowPing() const;
451
455 void SetShowFps(bool enabled);
456
460 [[nodiscard]] bool GetShowFps() const;
461
466 void SetPlayerName(const std::string &name);
467
468 private:
469 enum class Scene { MENU, IN_GAME, GAME_OVER };
470
472 std::string _gameOverReason; // Stores the reason for game over
473
475 bool _initialized = false;
476 bool _quitRequested = false;
477 uint32_t _width = 0;
478 uint32_t _height = 0;
480
481 // ===== Audio =====
482 std::unique_ptr<Audio::SoundEffectManager> _soundEffectManager;
483
484 // ===== Menu UI (business) =====
485 std::unique_ptr<UI::RaylibUIFactory> _uiFactory;
486 std::unique_ptr<Game::MainMenu> _mainMenu;
487 std::unique_ptr<Game::ServerListMenu> _serverListMenu;
488 std::unique_ptr<Game::AddServerMenu> _addServerMenu;
489 std::unique_ptr<Game::RoomListMenu> _roomListMenu;
490 std::unique_ptr<Game::CreateRoomMenu> _createRoomMenu;
491 std::unique_ptr<Game::WaitingRoomMenu> _waitingRoomMenu;
492 std::unique_ptr<Game::ConnectionMenu> _connectionMenu;
493 std::unique_ptr<Game::SettingsMenu> _settingsMenu;
494 std::unique_ptr<Game::AccessibilityMenu> _accessibilityMenu;
495 std::unique_ptr<Game::KeyBindingsMenu> _keyBindingsMenu;
496 std::unique_ptr<Game::ConfirmQuitMenu> _confirmQuitMenu;
497 std::unique_ptr<Game::LoginMenu> _loginMenu;
498 std::unique_ptr<Game::DefeatMenu> _defeatMenu;
499 std::unique_ptr<Game::VictoryMenu> _victoryMenu;
500
501 bool _settingsOverlay = false;
503 bool _loginOverlay = false;
505
506 // Selected server for connection
507 std::string _selectedServerIp = "127.0.0.1";
508 uint16_t _selectedServerPort = 4242;
509 bool _isConnecting = false;
511
512 // Selected room for joining
513 std::string _selectedRoomId;
514
515 // Entity rendering subsystem
516 std::unique_ptr<EntityRenderer> _entityRenderer;
517
518 // Chat widget
519 std::unique_ptr<Game::ChatWidget> _chatWidget;
520
521 // Network stats display (updated once per second for optimization)
522 uint32_t _currentPing = 0;
523 uint32_t _displayedPing = 0; // The ping value currently displayed
524 float _pingUpdateTimer = 0.0f; // Timer for ping update throttling
525 static constexpr float PING_UPDATE_INTERVAL = 1.0f; // Update every 1 second
526
527 bool _showPing = true;
528 bool _showFps = true;
529
530 // ===== HUD stats =====
531 uint32_t _fps = 0;
532 float _fpsAccumulator = 0.0f;
533 uint32_t _fpsFrameCount = 0;
534
540 void InitializeMenus();
541
546
547 // ===== Menu initialization helpers (SOLID: Single Responsibility) =====
552 void InitializeMainMenu();
553 void InitializeLoginMenu();
564
565 // ===== Accessibility settings persistence =====
568
569 // ===== Helper methods for Render() to reduce cognitive complexity =====
570
574 void UpdateFpsCounter();
575
580
584 void UpdateUI();
585
589 void RenderGameScene();
590
594 void RenderUI();
595
599 void RenderHUD();
600};
601#endif
Type-safe event publication/subscription system.
Definition EventBus.hpp:44
Raylib implementation of the IGraphics interface.
Graphical rendering system using Raylib.
Definition Rendering.hpp:62
std::string _connectingServerName
std::unique_ptr< EntityRenderer > _entityRenderer
EventBus & _eventBus
uint32_t GetHeight() const
Get window height.
void UpdateEntity(uint32_t id, RType::Messages::Shared::EntityType type, float x, float y, int health, const std::string &currentAnimation, int srcX, int srcY, int srcW, int srcH)
Update or create an entity for rendering with animation.
Graphics::RaylibGraphics _graphics
std::string _selectedServerIp
uint32_t _fps
void ClearBackground()
Clear background configuration.
std::unique_ptr< Game::SettingsMenu > _settingsMenu
void RenderHUD()
Render HUD elements (ping, FPS).
void ClearAllEntities()
Clear all entities from the rendering cache.
void MoveEntityLocally(uint32_t entityId, float deltaX, float deltaY)
Move an entity locally (client-side prediction)
void SetMyEntityId(uint32_t id)
Set the local player's entity ID.
void InitializeAccessibilityMenu()
bool GetShowPing() const
Get ping display state.
std::unique_ptr< Game::AccessibilityMenu > _accessibilityMenu
bool IsKeyDown(int key) const
Check if a key is currently being held down.
bool WindowShouldClose() const
void InitializeLoginMenu()
static constexpr float PING_UPDATE_INTERVAL
void InitializeServerListMenu()
void InitializeMenus()
Initialize UI factory, menus and all related callbacks.
Definition Rendering.cpp:56
uint32_t _currentPing
void InitializeVictoryMenu()
float _pingUpdateTimer
void SetOnChatMessageSent(std::function< void(const std::string &)> callback)
Set callback for when a chat message is sent.
void UpdateFpsCounter()
Update FPS counter based on delta time.
void UpdateRoomList(const std::vector< RoomData > &rooms)
Update room list from server data.
bool LoadTexture(const std::string &textureName, const std::string &path)
Load a texture from file.
bool _loginOverlay
void InitializeCreateRoomMenu()
void RemoveEntity(uint32_t id)
Remove an entity from rendering.
void SetBackground(const std::string &mainBackground, const std::string &parallaxBackground, float scrollSpeed, float parallaxSpeedFactor)
Set up background layers for parallax scrolling.
bool _showFps
std::unique_ptr< Game::RoomListMenu > _roomListMenu
std::string _gameOverReason
void SubscribeToConnectionEvents()
void SetShowPing(bool enabled)
Enable/disable ping display.
std::unique_ptr< UI::RaylibUIFactory > _uiFactory
void DrawText(const std::string &text, float xPosition, float yPosition, uint32_t size=24)
Draw text on screen.
void UpdateInterpolation(float deltaTime)
Update interpolation for all entities.
void InitializeMainMenu()
uint32_t _fpsFrameCount
std::unique_ptr< Game::ConnectionMenu > _connectionMenu
std::unique_ptr< Game::KeyBindingsMenu > _keyBindingsMenu
bool GetShowFps() const
Get FPS display state.
bool _settingsOverlay
bool _initialized
bool IsWindowOpen() const
Check if window is open.
void UpdateChatVisibility()
Update chat widget visibility based on current scene.
Scene _scene
void ApplyInitialMenuSettings()
Apply runtime settings affecting rendering (target FPS, HUD visibility...).
Definition Rendering.cpp:89
bool Initialize(uint32_t width, uint32_t height, const std::string &title)
Initialize the rendering system and create window.
Definition Rendering.cpp:25
void InitializeConfirmQuitMenu()
void InitializeRoomListMenu()
void RenderGameScene()
Render the game scene (entities).
std::unique_ptr< Game::CreateRoomMenu > _createRoomMenu
std::unique_ptr< Game::AddServerMenu > _addServerMenu
std::unique_ptr< Game::VictoryMenu > _victoryMenu
void SetLocalPlayerMoving(bool moving)
Set whether the local player is currently moving.
void InitializeConnectionMenu()
std::unique_ptr< Game::ConfirmQuitMenu > _confirmQuitMenu
std::unique_ptr< Game::LoginMenu > _loginMenu
~Rendering()
Destructor.
Definition Rendering.cpp:21
bool _quitRequested
std::unique_ptr< Game::DefeatMenu > _defeatMenu
void Render()
Perform rendering of current frame.
void SetReconciliationThreshold(float threshold)
Set the reconciliation threshold for client-side prediction.
bool _keyBindingsOverlay
void InitializeKeyBindingsMenu()
bool _showPing
void Shutdown()
Stop the rendering system and destroy window.
void UpdateUI()
Update all UI elements based on current scene.
void InitializeDefeatMenu()
void AddChatMessage(uint32_t playerId, const std::string &playerName, const std::string &message, uint64_t timestamp)
Add a chat message to the chat widget.
void ShowGameOver(const std::string &reason)
Display the game over screen.
uint32_t _width
void LoadAccessibilitySettings()
void SetPlayerName(const std::string &name)
Update the displayed player name (e.g., after authentication)
void InitializeSettingsMenu()
void ClearWindow()
Clears the window before rendering its content.
std::unique_ptr< Game::MainMenu > _mainMenu
uint32_t _displayedPing
uint32_t GetWidth() const
Get window width.
std::unique_ptr< Game::WaitingRoomMenu > _waitingRoomMenu
void SetClientSidePredictionEnabled(bool enabled)
Enable or disable client-side prediction for local player.
void UpdatePingTimer(float deltaTime)
Update ping display timer (called every frame)
std::unique_ptr< Game::ChatWidget > _chatWidget
void InitializeChatWidget()
uint16_t _selectedServerPort
void StartGame()
Switch immediately to the game scene.
void HandleEscapeKeyInput()
Handle ESC key input to toggle settings overlay in-game.
void UpdateBackground(float deltaTime)
Update background scroll positions.
void SetPing(uint32_t pingMs)
Set the current ping value for display.
void SaveAccessibilitySettings()
bool _isConnecting
void DrawSprite(const std::string &textureId, float xPosition, float yPosition, float rotation=0.0, float scale=1.0)
Draw a sprite on screen.
void InitializeAddServerMenu()
std::unique_ptr< Audio::SoundEffectManager > _soundEffectManager
void RenderUI()
Render all UI menus based on current scene and overlay state.
bool IsGamepadButtonDown(int gamepad, int button) const
Check if a gamepad button is currently held down.
bool _confirmQuitOverlay
void InitializeWaitingRoomMenu()
float GetReconciliationThreshold() const
Get the current reconciliation threshold.
float _fpsAccumulator
std::string _selectedRoomId
bool IsGamepadAvailable(int gamepad) const
Check if a gamepad is available/connected.
std::unique_ptr< Game::ServerListMenu > _serverListMenu
uint32_t _height
void UpdateWaitingRoom(const std::vector< Game::PlayerInfo > &players, const std::string &roomName, bool isHost, bool isSpectator=false)
Update waiting room with player list.
void SetShowFps(bool enabled)
Enable/disable FPS display.
EntityType
Entity type enum - matches Cap'n Proto enum.