Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🏗️ Rendering engine architecture (EPI-70) #13

Merged
merged 25 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
743a22f
:zap: Add comparison operator in IObject
AubaneNourry Sep 30, 2024
0138f81
:sparkles: Add Graphics and IGraphicsComponent classes definition
AubaneNourry Sep 30, 2024
2c8b920
:memo: Add documentation for Graphics and IGraphicsComponent classes
AubaneNourry Sep 30, 2024
a7a035f
:rotating_light: Fix trailling whitespaces in IGraphicsComponent.hpp
AubaneNourry Sep 30, 2024
fdc7fc3
:zap: Use a reference to the current scene instead of the Scene Manager
AubaneNourry Sep 30, 2024
ec2b8cc
Merge remote-tracking branch 'origin/develop' into feature/epi-70-ren…
AubaneNourry Sep 30, 2024
e8c0983
:goal_net: Create error class for Graphics classes
AubaneNourry Oct 1, 2024
0adc60a
:twisted_rightwards_arrows: Merge remote-tracking branch 'origin/deve…
AubaneNourry Oct 2, 2024
f27b4c0
:bug: Remove defaulting comparison operator of IObject
AubaneNourry Oct 2, 2024
ec5a801
:test_tube: Add SDL2 to conan file
AubaneNourry Oct 2, 2024
7a51e97
:bug: Remove worg/system from conan requirements
AubaneNourry Oct 2, 2024
a69f25b
:test_tube: Try to add egl to conan requirements
AubaneNourry Oct 2, 2024
2313c58
:test_tube: Add suggested dependencies to conan file
AubaneNourry Oct 2, 2024
9792e55
:test_tube: Downgrade SDL2 version in conan file to avoid dependencies
AubaneNourry Oct 2, 2024
1514144
:zap: Use raw pointer in addAndSortObject for coherence
AubaneNourry Oct 2, 2024
85e7a11
Merge remote-tracking branch 'origin/develop' into feature/epi-70-ren…
AubaneNourry Oct 7, 2024
17ff283
:building_construction: Switch from SDL2 to SFML
AubaneNourry Oct 7, 2024
c3e4288
:fire: Remove SDL2 in conan file
AubaneNourry Oct 7, 2024
355a63a
:fire: Remove useless deleted functions in IObject
AubaneNourry Oct 7, 2024
3d11dad
:pencil2: Rename initialize to prepare to be more explicit
AubaneNourry Oct 7, 2024
8cb2c16
:construction: Add SFML to CMakeLists and conanfile
AubaneNourry Oct 7, 2024
7fe9684
:construction: Update Graphics constructor
AubaneNourry Oct 7, 2024
e3bacba
:construction: WIP SFML lib include
AubaneNourry Oct 7, 2024
5add582
:twisted_rightwards_arrows: Merge remote-tracking branch 'origin/deve…
AubaneNourry Oct 7, 2024
02cf047
:memo: Remove all SDL related comments
AubaneNourry Oct 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/common/IObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ class IObject {
*/
IObject &operator=(const IObject &other) = default;

/**
* @brief The comparison operator of the object
* @param other The object to compare
* @return True if the objects are the same, false otherwise
* @version v0.1.0
* @since v0.1.0
* @author Aubane NOURRY
*/
bool operator==(const IObject &other) const = default;

/**
* @brief The move constructor of the object
* @details Is deleted because the object should not be moved
Expand Down
3 changes: 3 additions & 0 deletions src/graphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ add_dependencies(StellarForgeGraphics StellarForgeCommon)
target_sources(StellarForgeGraphics
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/Graphics.hpp
${CMAKE_CURRENT_SOURCE_DIR}/GraphicsException.hpp
${CMAKE_CURRENT_SOURCE_DIR}/components/IGraphicsComponent.hpp
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/Graphics.cpp
${CMAKE_CURRENT_SOURCE_DIR}/GraphicsException.cpp
)
118 changes: 114 additions & 4 deletions src/graphics/Graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,123 @@
#ifndef GRAPHICS_HPP
#define GRAPHICS_HPP

#include <SDL2/SDL.h>
#include <vector>
#include <memory>
#include "../common/IScene.hpp"
#include "../common/IObject.hpp"


/**
* @class Graphics
* @brief This class handles all graphics-related operations.
* @details The Graphics class is responsible for initializing and managing the rendering
* of objects in the scenes, managing an SDL window and renderer, and ensuring
* objects are sorted by their z-index for proper rendering order.
* @version v0.1.0
* @since v0.1.0
* @author Aubane NOURRY
*/
class Graphics {
public:
Graphics();
};
/**
* @brief Constructor for the Graphics class.
* @param width The width of the window.
* @param height The height of the window.
* @param title The title of the window. Default is "Game".
* @param precharge Whether to preload all objects in all scenes. Default is false.
* @details This initializes the window with the specified dimensions and title,
* and optionally precharges all objects if the precharge flag is set to true.
* @since v0.1.0
* @author Aubane NOURRY
*/
Graphics(int width, int height, const char *title = "Game", bool precharge = false);

/**
* @brief Destructor for the Graphics class.
* @details Cleans up and releases all resources used by the Graphics class, including
* the SDL window and renderer.
* @since v0.1.0
* @author Aubane NOURRY
*/
~Graphics();

/**
* @brief Initializes the SDL window and renderer.
* @details This method initializes the SDL window and renderer based on the provided
* width, height, and title. It should be called before any rendering operations.
* @since v0.1.0
* @author Aubane NOURRY
*/
void initialize();
AubaneNourry marked this conversation as resolved.
Show resolved Hide resolved

/**
* @brief Renders the objects in the current scene.
* @details This method renders all objects in the current scene in the correct order
* based on their z-index. It should be called once per frame to render the
* scene.
* @since v0.1.0
* @author Aubane NOURRY
*/
void render();

/**
* @brief Clears the current rendering target.
* @details Clears the renderer's drawing surface, typically called at the beginning of a frame
* before rendering the objects.
* @since v0.1.0
* @author Aubane NOURRY
*/
void clear();

/**
* @brief Presents the rendered frame to the window.
* @details This method is called at the end of the rendering process to present the
* rendered frame on the screen.
* @since v0.1.0
* @author Aubane NOURRY
*/
void present();

/**
* @brief Cleans up the SDL resources used by the Graphics class.
* @details Destroys the SDL window and renderer, and cleans up any resources used
* by the graphics system.
* @since v0.1.0
* @author Aubane NOURRY
*/
void clean();

/**
* @brief Adds an object to the sorted object list and maintains the correct order.
* @param object A shared pointer to the object to add.
* @details This method adds an object to the internal sortedObjects list and ensures
* that the objects are kept in the correct rendering order based on their
* z-index. If the object is already in the list, it won't be added again.
* @since v0.1.0
* @author Aubane NOURRY
*/
void addAndSortObject(std::shared_ptr<IObject> object);

/**
* @brief Changes the current scene to the specified scene.
* @param scene A pointer to the scene to set as the current scene.
* @details This method changes the current scene to the specified scene and sets the
* scene's objects to be rendered in the correct order.
* @since v0.1.0
* @author Aubane NOURRY
*/
void setScene(IScene *scene);

private:
SDL_Window *window; /**< The SDL window used for rendering. */
SDL_Renderer *renderer; /**< The SDL renderer used to draw objects. */
int width; /**< The width of the window. */
int height; /**< The height of the window. */
const char *title; /**< The title of the window. */
bool initialized; /**< Flag indicating if the graphics system is initialized. */
std::vector<std::shared_ptr<IObject>> sortedObjects; /**< List of objects sorted by z-index for rendering. */
bool precharge; /**< Flag to indicate if all objects should be preloaded in all scenes. */
IScene *currentScene; /**< The current scene being rendered. */
};

#endif //GRAPHICS_HPP
#endif // GRAPHICS_HPP
18 changes: 18 additions & 0 deletions src/graphics/GraphicsException.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
** EPITECH PROJECT, 2024
** Stellar-Forge
** File description:
** GraphicsException
*/

#include "GraphicsException.hpp"
#include <utility>

GraphicsException::GraphicsException(std::string message): _message{std::move(message)}
{
}

const char* GraphicsException::what() const noexcept
{
return _message.c_str();
}
61 changes: 61 additions & 0 deletions src/graphics/GraphicsException.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
** EPITECH PROJECT, 2024
** Stellar-Forge
** File description:
** GraphicsException
*/

#ifndef GRAPHICSEXCEPTION_HPP
#define GRAPHICSEXCEPTION_HPP

#include "../common/IError.hpp"
#include <string>

/**
* @class GraphicsException
* @brief Exception class for Graphics errors
* @details This class is used to throw Graphics errors
* @version 0.1.0
* @since 0.1.0
* @author Aubane NOURRY
*/
class GraphicsException final : public IError
{
public:
/**
* @brief Default constructor
* @details This constructor initializes the GraphicsException with a message
* @param message std::string, the message
* @version 0.1.0
* @since 0.1.0
* @author Aubane NOURRY
*/
explicit GraphicsException(std::string message);

/**
* @brief Default destructor
* @details This destructor clears the GraphicsException
* @version 0.1.0
* @since 0.1.0
* @author Aubane NOURRY
*/
~GraphicsException() override = default;

/**
* @brief Get the error message
* @details This function returns the error message
* @return const char*, the error message
* @version 0.1.0
* @since 0.1.0
* @author Aubane NOURRY
*/
[[nodiscard]] const char* what() const noexcept override;

private:
/**
* @brief The error message
*/
std::string _message;
};

#endif // GRAPHICSEXCEPTION_HPP
55 changes: 55 additions & 0 deletions src/graphics/components/IGraphicsComponent.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
** EPITECH PROJECT, 2024
** StellarForge
** File description:
** No file there , just an epitech header example .
** You can even have multiple lines if you want !
*/

#ifndef IGRAPHICSCOMPONENT_HPP
#define IGRAPHICSCOMPONENT_HPP

#include "../common/IComponent.hpp"
#include <SDL2/SDL.h>

/**
* @class IGraphicsComponent
* @brief Interface for graphics components in the game engine.
* @details This interface defines the basic structure and functionality for graphics-related components.
* Any component that handles graphics or rendering should inherit from this interface and implement
* its methods.
* @version v0.1.0
* @since v0.1.0
* @author Aubane NOURRY
*/
class IGraphicsComponent : public IComponent {
public:
/**
* @brief Virtual destructor for the IGraphicsComponent class.
* @details This ensures that derived classes can be properly destroyed, releasing all resources
* when an object is deleted.
* @since v0.1.0
*/
virtual ~IGraphicsComponent() = default;

/**
* @brief Runs the logic associated with the graphics component.
* @details This pure virtual function should contain any logic that the component needs to execute
* during each frame, such as updating its state or preparing for rendering. It must be implemented
* by any derived class.
* @since v0.1.0
*/
virtual void runComponent() = 0;

/**
* @brief Renders the component using the provided SDL_Renderer.
* @param renderer A pointer to the SDL_Renderer object used for rendering.
* @details This pure virtual function must be implemented by derived classes to define how the component
* should be drawn on the screen. It is called each frame to render the visual aspects of the
* component.
* @since v0.1.0
*/
virtual void render(SDL_Renderer *renderer) = 0;
};

#endif //IGRAPHICALCOMPONENT_HPP