Skip to content

Commit

Permalink
Merge pull request #50 from FppEpitech/feat/link-gamedata-event-and-r…
Browse files Browse the repository at this point in the history
…ender-to-engine

Feat/link gamedata event and render to engine
  • Loading branch information
mathieurobert1 authored Jun 5, 2024
2 parents bc2cb59 + b4992e8 commit 6dab0d2
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 12 deletions.
4 changes: 4 additions & 0 deletions gui/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ SRC = src/Error/Error.cpp \
src/Render/UserCamera.cpp \
\
src/Event/Event.cpp \
\
src/GUIUpdater/GUIUpdater.cpp \

TEST_FILES = Network/TestNetwork.cpp \
Parsing/TestParser.cpp \
Expand All @@ -37,6 +39,8 @@ TEST_FILES = Network/TestNetwork.cpp \
GameDatas/TestTeam.cpp \
GameDatas/TestGameData.cpp \
Render/TestCamera.cpp \
GUIUpdater/TestGUIUpdater.cpp \
Event/TestEvent.cpp \

OBJ = $(SRC:.cpp=.o)
MAIN_OBJ = $(MAIN:.cpp=.o)
Expand Down
10 changes: 8 additions & 2 deletions gui/include/Engine/Engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

#pragma once

#include "Event/Event.hpp"
#include "Render/Render.hpp"
#include "Network/Network.hpp"
#include "GameDatas/GameData.hpp"
#include "Parsing/ServerParser.hpp"
#include "GUIUpdater/GUIUpdater.hpp"

namespace Gui {

Expand All @@ -29,7 +32,7 @@ class Gui::Engine {
*
* @param network Network class.
*/
Engine(Network network, std::shared_ptr<Render> render);
Engine(Network network);

/**
* @brief Destroy the Engine object.
Expand All @@ -47,7 +50,10 @@ class Gui::Engine {

ServerParser _parser; // Parser class for server's command.
Network _network; // Network class to connect to the server.
std::shared_ptr<Render> _render; // Render class to draw the scene.
std::shared_ptr<Render> _render; // Render class to draw the scene.
Event _event; // Event class to listen the user's inputs.
std::shared_ptr<GameData> _gameData; // GameData class to store the game's data.
GUIUpdater _guiUpdater;// GUIUpdater class to update the GUI.

/**
* @brief Listen the server and update Engine with its commands.
Expand Down
9 changes: 8 additions & 1 deletion gui/include/Event/Event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Gui::Event {
* @brief Construct a new Event object.
*
*/
Event(std::shared_ptr<Render> render);
Event();

/**
* @brief Destroy the Event object.
Expand All @@ -46,6 +46,13 @@ class Gui::Event {
*/
void listen();

/**
* @brief Set the Render object.
*
* @param render Render class.
*/
void setRender(std::shared_ptr<Render> render);

private:

std::shared_ptr<Render> _render; // Render class to draw scene.
Expand Down
52 changes: 52 additions & 0 deletions gui/include/GUIUpdater/GUIUpdater.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
** EPITECH PROJECT, 2024
** Zappy
** File description:
** GUIUpdater
*/

#pragma once

#include "GameDatas/GameData.hpp"

#include <string>
#include <memory>

namespace Gui {

/**
* @brief GUIUpdater class
*
* This class is used to update the GUI GameData to ensure the GUI is up to date
*/
class GUIUpdater;
}

class Gui::GUIUpdater {

public:

/**
* @brief Construct a new GUIUpdater object
*
* @param gameData The GUI GameData to update
*/
GUIUpdater(std::shared_ptr<GameData> gameData);

/**
* @brief Destroy the GUIUpdater object
*/
~GUIUpdater() = default;

/**
* @brief Update the GUI GameData
*
* @param command The command to update the GUI GameData
* @param data The data to update the GUI GameData
*/
void update(const std::string &command, const std::string &data);

private:

std::shared_ptr<GameData> _gameData;
};
4 changes: 3 additions & 1 deletion gui/include/Render/Render.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "raylib.h"
#include "Render/UserCamera.hpp"
#include "GameDatas/GameData.hpp"

namespace Gui {

Expand All @@ -31,7 +32,7 @@ class Gui::Render {
* @brief Construct a new Render object.
*
*/
Render();
Render(std::shared_ptr<GameData> gameData);

/**
* @brief Destroy the Render object.
Expand Down Expand Up @@ -85,4 +86,5 @@ class Gui::Render {

UserCamera _camera; // Camera of the scene.
bool _isDebug; // Display or not the debug informations.
std::shared_ptr<GameData> _gameData; // GameData class to store the game's data.
};
18 changes: 13 additions & 5 deletions gui/src/Engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@

#include "Event/Event.hpp"
#include "Engine/Engine.hpp"
#include "GUIUpdater/GUIUpdater.hpp"

#include <iostream>
#include <sstream>

Gui::Engine::Engine(Network network, std::shared_ptr<Render> render) : _network(network), _render(render) {}
Gui::Engine::Engine(Network network) : _network(network), _gameData(std::make_shared<GameData>()), _guiUpdater(_gameData)
{
_render = std::make_shared<Render>(_gameData);
_event.setRender(_render);
}

void Gui::Engine::run(void)
{
Event event(_render);

while (_render->isOpen()) {
listenServer();
event.listen();
_event.listen();
_render->draw();
}
}
Expand All @@ -31,7 +35,11 @@ void Gui::Engine::listenServer(void)
return;
try {
std::vector<std::string> arguments = _parser.parse(command);
//TODO: #20 (https://github.com/FppEpitech/Zappy/issues/20)
std::istringstream stream(command);
std::string keyCommand;

stream >> keyCommand;
_guiUpdater.update(keyCommand, command);
}
catch (const std::exception &error) {
std::cout << error.what() << std::endl;
Expand Down
7 changes: 6 additions & 1 deletion gui/src/Event/Event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "raylib.h"
#include "Event/Event.hpp"

Gui::Event::Event(std::shared_ptr<Render> render) : _render(render)
Gui::Event::Event()
{
}

Expand All @@ -24,6 +24,11 @@ void Gui::Event::listen()
}
}

void Gui::Event::setRender(std::shared_ptr<Render> render)
{
_render = render;
}

void Gui::Event::moveUpCamera()
{
_render->getCamera()->position.y += HIGH_CAMERA_INCREASE;
Expand Down
16 changes: 16 additions & 0 deletions gui/src/GUIUpdater/GUIUpdater.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
** EPITECH PROJECT, 2024
** Zappy
** File description:
** GUIUpdater
*/

#include "GUIUpdater/GUIUpdater.hpp"

Gui::GUIUpdater::GUIUpdater(std::shared_ptr<GameData> gameData) : _gameData(gameData) {}

void Gui::GUIUpdater::update(const std::string &command, const std::string &data)
{
(void)command;
(void)data;
}
3 changes: 2 additions & 1 deletion gui/src/Render/Render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

#include <string>

Gui::Render::Render()
Gui::Render::Render(std::shared_ptr<GameData> gameData)
: _gameData(gameData)
{
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE);
DisableCursor();
Expand Down
2 changes: 1 addition & 1 deletion gui/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int main(int argc, char **argv)
Gui::ParseCommandLine parseLine(argc, argv);
Gui::Network net(parseLine.getPort(), parseLine.getHostName());
net.connectToServer();
Gui::Engine engine(net, std::make_shared<Gui::Render>());
Gui::Engine engine(net);
engine.run();
} catch (const std::exception &error) {
std::cout << error.what() << std::endl;
Expand Down
16 changes: 16 additions & 0 deletions gui/tests/Event/TestEvent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
** EPITECH PROJECT, 2024
** Zappy
** File description:
** TestEvent
*/

#include "Event/Event.hpp"
#include "CriterionHeaders.hpp"

Test(Event, constructor, .timeout = 5)
{
Gui::Event event;

cr_assert_not_null(&event);
}
26 changes: 26 additions & 0 deletions gui/tests/GUIUpdater/TestGUIUpdater.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
** EPITECH PROJECT, 2024
** Zappy
** File description:
** TestGUIUpdater
*/

#include "CriterionHeaders.hpp"
#include "GUIUpdater/GUIUpdater.hpp"

Test(GUIUpdater, constructor, .timeout = 5)
{
std::shared_ptr<Gui::GameData> gameData = std::make_shared<Gui::GameData>();
Gui::GUIUpdater guiUpdater(gameData);

cr_assert_not_null(&guiUpdater);
}

Test(GUIUpdater, update, .timeout = 5)
{
std::shared_ptr<Gui::GameData> gameData = std::make_shared<Gui::GameData>();
Gui::GUIUpdater guiUpdater(gameData);

guiUpdater.update("test", "test");
cr_assert_not_null(&guiUpdater);
}

0 comments on commit 6dab0d2

Please sign in to comment.