R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
RaylibGraphics.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** workspace
4** File description:
5** Raylib
6*/
7
8#pragma once
9
10#include <raylib.h>
11#include <limits>
12#include <string>
13#include <unordered_map>
14#include <vector>
15#include "IGraphics.hpp"
16
17namespace Graphics {
21 class RaylibGraphics : public IGraphics {
22 public:
23 // Constructor / Destructor
31 ~RaylibGraphics() override;
32
33 // Window management
34 void InitWindow(int width, int height, const char *title) override;
35 void ClearWindow() override;
36 void StartDrawing() override;
37 void DisplayWindow() override;
38 bool IsWindowOpen() const override;
39 void CloseWindow() override;
40 [[nodiscard]] int GetWindowWidth() const override;
41 [[nodiscard]] int GetWindowHeight() const override;
42 void SetWindowTitle(const char *title) override;
43 void SetWindowSize(int width, int height) override;
44 void ToggleFullScreen() override;
45 void SetTargetFPS(int fps) override;
46 void SetClearColor(unsigned int color) override;
47 void TakeScreenshot(const char *filepath) override;
48
49 // Time / profiling
50 float GetTime() const override;
51 float GetDeltaTime() const override;
52
53 // Basic drawing primitives
54 void DrawRect(int x, int y, int width, int height, unsigned int color) override;
55 void DrawRectFilled(int x, int y, int width, int height, unsigned int color) override;
56 void DrawCircle(int x, int y, int radius, unsigned int color) override;
57 void DrawCircleFilled(int x, int y, int radius, unsigned int color) override;
58
59 // Fonts / text
60 int LoadFont(const char *filepath, int size) override;
61 void UnloadFont(int fontHandle) override;
62 void DrawText(int fontHandle, const char *text, int x, int y, int fontSize,
63 unsigned int color) override;
64 int GetFontHeight(int fontHandle, int fontSize) override;
65
66 // Textures / sprites / images
67 int LoadTexture(const char *name, const char *filepath) override;
68 int CreateTextureFromMemory(char const *textureName, const void *pixels, int width, int height,
69 int format) override;
70 void UpdateTexture(const char *textureName, const void *pixels) override;
71 void UnloadTexture(const char *textureName) override;
72 void DrawTexture(const char *textureName, int x, int y, unsigned int tint) override;
73 void DrawTextureEx(const char *textureName, int srcX, int srcY, int srcW, int srcH, float destX,
74 float destY, float rotation, float scale, unsigned int tint) override;
75 bool GetTextureSize(const char *textureName, int &width, int &height) const override;
76 void DrawTexturePro(const char *textureName, int srcX, int srcY, int srcW, int srcH, float destX,
77 float destY, float destW, float destH, unsigned int tint) override;
78
79 // Input helpers
80 bool IsKeyPressed(int key) const override;
81 bool IsKeyDown(int key) const override;
82 bool IsKeyReleased(int key) const override;
83 bool IsGamepadAvailable(int gamepad) const override;
84 bool IsGamepadButtonPressed(int gamepad, int button) const override;
85 bool IsGamepadButtonDown(int gamepad, int button) const override;
86 float GetGamepadAxisMovement(int gamepad, int axis) const override;
87 bool IsMouseButtonPressed(int button) const override;
88 bool IsMouseButtonDown(int button) const override;
89 void GetMousePosition(float &x, float &y) const override;
90 bool WindowShouldClose() const override;
91 int GetMouseX() const override;
92 int GetMouseY() const override;
93 int GetCharPressed() const override;
94 int GetScreenWidth() const override;
95 int GetScreenHeight() const override;
96 void DrawRectangle(int x, int y, int width, int height, unsigned int color) override;
97 void DrawRectangleLines(int x, int y, int width, int height, unsigned int color) override;
98 void DrawText(const char *text, int x, int y, int fontSize, unsigned int color) override;
99
100 // Colorblind filter
101 void SetColorblindFilter(ColorblindFilterType filter) override;
102 [[nodiscard]] ColorblindFilterType GetColorblindFilter() const override;
103 void BeginColorblindCapture() override;
104 void EndColorblindCapture() override;
105 // Audio management
106 void InitAudioDevice() override;
107 void CloseAudioDevice() override;
108 bool IsAudioDeviceReady() const override;
109 bool LoadSound(const char *soundName, const char *filepath) override;
110 void UnloadSound(const char *soundName) override;
111 void PlaySound(const char *soundName) override;
112 void SetSoundVolume(const char *soundName, float volume) override;
113 bool IsSoundPlaying(const char *soundName) const override;
114
115 private:
118
119 std::vector<Font> _fonts;
120 std::unordered_map<std::string, Texture2D> _textures;
121 std::unordered_map<std::string, Sound> _sounds;
122 Color _clearColor{255, 255, 255, 255};
123 bool _windowInitialized = false;
124
125 // Colorblind filter resources
127 RenderTexture2D _colorblindRenderTexture{};
131 bool _audioInitialized = false;
132 };
133} // namespace Graphics
Abstract interface for graphics rendering operations.
Definition IGraphics.hpp:32
Raylib implementation of the IGraphics interface.
void SetClearColor(unsigned int color) override
Set the background clear color.
void DrawRectangle(int x, int y, int width, int height, unsigned int color) override
Draw a filled rectangle (alias for DrawRectFilled)
float GetDeltaTime() const override
Get the time elapsed for the last frame.
void DrawRectFilled(int x, int y, int width, int height, unsigned int color) override
Draw a filled rectangle.
int GetMouseX() const override
Get the current X position of the mouse cursor.
void DisplayWindow() override
Display the current frame to the window (end drawing and swap buffers)
void DrawTextureEx(const char *textureName, int srcX, int srcY, int srcW, int srcH, float destX, float destY, float rotation, float scale, unsigned int tint) override
Draw a texture with advanced parameters (rotation, scale, source rectangle)
bool IsKeyPressed(int key) const override
Check if a key was pressed (triggered once when key goes down)
int GetMouseY() const override
Get the current Y position of the mouse cursor.
bool IsKeyDown(int key) const override
Check if a key is currently being held down.
void DrawRect(int x, int y, int width, int height, unsigned int color) override
Draw a rectangle outline.
void DrawCircleFilled(int x, int y, int radius, unsigned int color) override
Draw a filled circle.
bool GetTextureSize(const char *textureName, int &width, int &height) const override
Get the dimensions of a loaded texture.
int LoadFont(const char *filepath, int size) override
Load a font from file.
void UnloadTexture(const char *textureName) override
Unload a previously loaded texture.
void CloseWindow() override
Close the graphics window and cleanup resources.
bool WindowShouldClose() const override
Check if the window should close.
ColorblindFilterType _colorblindFilter
void SetTargetFPS(int fps) override
Set the target frames per second for rendering.
bool IsSoundPlaying(const char *soundName) const override
Check if a sound is currently playing.
void SetSoundVolume(const char *soundName, float volume) override
Set volume for a specific sound.
std::unordered_map< std::string, Texture2D > _textures
int GetFontHeight(int fontHandle, int fontSize) override
Get the height of a font at a given size.
bool IsMouseButtonPressed(int button) const override
Check if a mouse button was pressed (triggered once when button goes down)
void BeginColorblindCapture() override
Begin capturing frame for colorblind filter processing.
int CreateTextureFromMemory(char const *textureName, const void *pixels, int width, int height, int format) override
Create a texture from raw pixel data in memory.
ColorblindFilterType GetColorblindFilter() const override
Get the current colorblind filter type.
float GetGamepadAxisMovement(int gamepad, int axis) const override
Get gamepad axis value (for analog sticks and triggers)
void DrawTexturePro(const char *textureName, int srcX, int srcY, int srcW, int srcH, float destX, float destY, float destW, float destH, unsigned int tint) override
Draw a texture with separate width/height scaling (for non-uniform scaling)
void SetWindowTitle(const char *title) override
Change the window title.
void SetColorblindFilter(ColorblindFilterType filter) override
Set the colorblind filter type.
int GetCharPressed() const override
Get the next character from the keyboard input queue.
void DrawRectangleLines(int x, int y, int width, int height, unsigned int color) override
Draw a rectangle outline (alias for DrawRect)
bool IsAudioDeviceReady() const override
Check if audio device is ready.
void StartDrawing() override
Begin drawing frame (setup canvas for drawing operations)
void ClearWindow() override
Clear the window with the current clear color.
bool IsGamepadAvailable(int gamepad) const override
Check if a gamepad is available/connected.
void SetWindowSize(int width, int height) override
Resize the window.
int GetWindowHeight() const override
Get current window height in pixels.
void UpdateTexture(const char *textureName, const void *pixels) override
Update an existing texture with new pixel data.
void DrawText(int fontHandle, const char *text, int x, int y, int fontSize, unsigned int color) override
Draw text using a loaded font.
void ToggleFullScreen() override
Toggle between fullscreen and windowed mode.
void CloseAudioDevice() override
Close the audio device and cleanup audio resources.
bool IsGamepadButtonPressed(int gamepad, int button) const override
Check if a gamepad button was pressed this frame.
bool IsMouseButtonDown(int button) const override
Check if a mouse button is currently being held down.
RaylibGraphics()
Construct a new Raylib graphics object.
std::vector< Font > _fonts
bool LoadSound(const char *soundName, const char *filepath) override
Load a sound from file.
bool IsKeyReleased(int key) const override
Check if a key was released (triggered once when key goes up)
bool IsWindowOpen() const override
Check if the window is still open.
int GetWindowWidth() const override
Get current window width in pixels.
void DrawCircle(int x, int y, int radius, unsigned int color) override
Draw a circle outline.
std::unordered_map< std::string, Sound > _sounds
void InitAudioDevice() override
Initialize the audio device.
void InitWindow(int width, int height, const char *title) override
Initialize the graphics window.
void UnloadFont(int fontHandle) override
Unload a previously loaded font.
RenderTexture2D _colorblindRenderTexture
void DrawTexture(const char *textureName, int x, int y, unsigned int tint) override
Draw a texture at the specified position.
int GetScreenWidth() const override
Get the screen width (same as window width)
void UnloadSound(const char *soundName) override
Unload a previously loaded sound.
void GetMousePosition(float &x, float &y) const override
Get the current mouse cursor position.
int GetScreenHeight() const override
Get the screen height (same as window height)
void TakeScreenshot(const char *filepath) override
Capture the current screen and save it to a file.
void EndColorblindCapture() override
End capturing and apply the colorblind filter.
float GetTime() const override
Get elapsed time since initialization.
~RaylibGraphics() override
Destroy the Raylib graphics object and cleanup resources.
bool IsGamepadButtonDown(int gamepad, int button) const override
Check if a gamepad button is currently held down.
int LoadTexture(const char *name, const char *filepath) override
Load a texture from an image file.
void PlaySound(const char *soundName) override
Play a loaded sound.
ColorblindFilterType
Colorblind filter types for accessibility.
Definition IGraphics.hpp:15
@ NONE
No filter applied.