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

Feature/rb 40 scene manager class #28

Merged
merged 14 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/Client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ target_include_directories(
target_sources(
${PROJECT_NAME_CLIENT}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/SceneManager.cpp
)

add_subdirectory(Systems)
Expand Down
90 changes: 90 additions & 0 deletions src/Client/SceneManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
** EPITECH PROJECT, 2023
** R-Bus
** File description:
** SceneManager implementation
*/

#include "SceneManager.hpp"
#include "raylib.h"
#include "ClientSystems.hpp"
#include "Registry.hpp"
#include "SystemManagersDirector.hpp"

constexpr int screenWidth = 1920;
constexpr int screenHeight = 1080;
constexpr int fps = 60;

// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
bool SceneManager::_init = false;
SceneManager SceneManager::_instance = SceneManager();
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)

static void initRaylib()
{
InitWindow(screenWidth, screenHeight, "R-Bus");
SetTargetFPS(fps);
InitAudioDevice();
}

static void initSystemManagers()
{
auto &director = Systems::SystemManagersDirector::getInstance();

for (auto systems : Systems::getSystemsGroups()) {
director.addSystemManager(systems);
}
initRaylib();
}

SceneManager &SceneManager::getInstance()
{
if (!_init) {
_init = true;
initSystemManagers();
}
return _instance;
}

SceneManager::SceneManager() : _currentScene(Scene::MAIN_GAME), _stop(false)
Saverio976 marked this conversation as resolved.
Show resolved Hide resolved
{
}

static void destroyRaylib()
{
CloseAudioDevice();
CloseWindow();
}

int SceneManager::run()
{
auto &director = Systems::SystemManagersDirector::getInstance();

try {
while (!_stop && !WindowShouldClose()) {
BeginDrawing();
ClearBackground(RAYWHITE);
auto scene = _scenes.at(_currentScene);
for (auto &systemManager : scene) {
director.getSystemManager(systemManager).updateSystems();
}
EndDrawing();
}
destroyRaylib();
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
return ReturnValue::ERROR;
}
return ReturnValue::OK;
}

void SceneManager::changeScene(Scene scene)
{
_currentScene = scene;
Registry::getInstance().clear();
}

void SceneManager::stop()
{
_stop = true;
}
43 changes: 43 additions & 0 deletions src/Client/SceneManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
** EPITECH PROJECT, 2023
** R-Bus
** File description:
** SceneManager
*/

#pragma once

#include <array>
#include <cstddef>
#include <functional>
#include <optional>
#include <vector>

enum ReturnValue { OK = 0, ERROR = 84 };

enum Scene { MAIN_GAME, MENU, SCENE_MAX };

enum SystemManagers { GAME, EVENTS, DISPLAY, MANAGER_MAX };

class SceneManager {
public:
static SceneManager &getInstance();
int run();
void changeScene(Scene scene);
void stop();

private:
SceneManager();

Scene _currentScene;
bool _stop;
const std::array<std::vector<SystemManagers>, 2> _scenes = {
std::vector<SystemManagers> {EVENTS, GAME, DISPLAY},
std::vector<SystemManagers> {EVENTS, DISPLAY }
};

// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
static bool _init;
static SceneManager _instance;
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
};
3 changes: 3 additions & 0 deletions src/Client/Systems/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ target_sources(
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/ClientSystems.cpp
)

add_subdirectory(Graphic)
add_subdirectory(Events)
Loading