Skip to content

Commit 61ef7aa

Browse files
Merge pull request #27 from G-Epitech/24-add-sfml-providers-methods-for-textures-sounds-and-font
feat(graphics:sfml): add sfml providers and rendering
2 parents 28bcba9 + 913ad31 commit 61ef7aa

34 files changed

+949
-114
lines changed

graphics/common/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
add_subdirectory(events)
2+
add_subdirectory(exceptions)
23
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../..)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
target_sources(${PROJECT_NAME} PUBLIC
2+
GraphicsException.cpp
3+
GraphicsException.hpp
4+
SoundException.hpp
5+
TextureException.hpp
6+
WindowException.hpp
7+
FontException.hpp
8+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
** EPITECH PROJECT, 2024
3+
** arcade
4+
** File description:
5+
** FontException.hpp
6+
*/
7+
8+
#pragma once
9+
10+
#include "GraphicsException.hpp"
11+
#include "shared/graphics/exceptions/IFontException.hpp"
12+
13+
using namespace shared::graphics::exceptions;
14+
15+
namespace arcade::graphics::common::exceptions {
16+
class FontException;
17+
}
18+
19+
class arcade::graphics::common::exceptions::FontException: public GraphicsException, public IFontException {
20+
public:
21+
using GraphicsException::GraphicsException;
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
** EPITECH PROJECT, 2024
3+
** arcade
4+
** File description:
5+
** GraphicsException.cpp
6+
*/
7+
8+
#include "GraphicsException.hpp"
9+
10+
using namespace arcade::graphics::common::exceptions;
11+
12+
GraphicsException::GraphicsException(const std::string &message, const std::string &where)
13+
{
14+
this->_message = message;
15+
this->_where = where;
16+
}
17+
18+
const char *GraphicsException::what() const noexcept {
19+
return this->_message.c_str();
20+
}
21+
22+
const char *GraphicsException::where() const noexcept {
23+
return this->_where.c_str();
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
** EPITECH PROJECT, 2024
3+
** arcade
4+
** File description:
5+
** GraphicsException.hpp
6+
*/
7+
8+
#pragma once
9+
10+
#include <string>
11+
#include "shared/graphics/exceptions/IGraphicsException.hpp"
12+
13+
using namespace shared::graphics::exceptions;
14+
15+
namespace arcade::graphics::common::exceptions {
16+
class GraphicsException;
17+
}
18+
19+
class arcade::graphics::common::exceptions::GraphicsException : public virtual IGraphicsException {
20+
public:
21+
GraphicsException(const std::string &message, const std::string &where);
22+
~GraphicsException() override = default;
23+
24+
const char *what() const noexcept override;
25+
26+
const char *where() const noexcept override;
27+
28+
protected:
29+
std::string _message;
30+
std::string _where;
31+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
** EPITECH PROJECT, 2024
3+
** arcade
4+
** File description:
5+
** SoundException.hpp
6+
*/
7+
8+
#pragma once
9+
10+
#include "GraphicsException.hpp"
11+
#include "shared/graphics/exceptions/ISoundException.hpp"
12+
13+
using namespace shared::graphics::exceptions;
14+
15+
namespace arcade::graphics::common::exceptions {
16+
class SoundException;
17+
}
18+
19+
class arcade::graphics::common::exceptions::SoundException: public GraphicsException, public ISoundException {
20+
public:
21+
using GraphicsException::GraphicsException;
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
** EPITECH PROJECT, 2024
3+
** arcade
4+
** File description:
5+
** TextureException.hpp
6+
*/
7+
8+
#pragma once
9+
10+
#include "GraphicsException.hpp"
11+
#include "shared/graphics/exceptions/ITextureException.hpp"
12+
13+
using namespace shared::graphics::exceptions;
14+
15+
namespace arcade::graphics::common::exceptions {
16+
class TextureException;
17+
}
18+
19+
class arcade::graphics::common::exceptions::TextureException: public GraphicsException, public ITextureException {
20+
public:
21+
using GraphicsException::GraphicsException;
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
** EPITECH PROJECT, 2024
3+
** arcade
4+
** File description:
5+
** WindowException.hpp
6+
*/
7+
8+
#pragma once
9+
10+
#include "GraphicsException.hpp"
11+
#include "shared/graphics/exceptions/IWindowException.hpp"
12+
13+
using namespace shared::graphics::exceptions;
14+
15+
namespace arcade::graphics::common::exceptions {
16+
class WindowException;
17+
}
18+
19+
class arcade::graphics::common::exceptions::WindowException: public GraphicsException, public IWindowException {
20+
public:
21+
using GraphicsException::GraphicsException;
22+
};

graphics/sfml/CMakeLists.txt

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
project(sfml)
2-
add_library(${PROJECT_NAME} SHARED
2+
add_library(sfml SHARED
33
export.cpp
44
src/GraphicsProvider.cpp
55
src/GraphicsProvider.hpp
66
src/window/Window.cpp
77
src/window/Window.hpp
88
src/window/EventsHandler.cpp
99
src/window/EventsHandler.hpp
10+
src/sound/Sound.cpp
11+
src/sound/Sound.hpp
12+
src/texture/Texture.cpp
13+
src/texture/Texture.hpp
14+
src/font/Font.cpp
15+
src/font/Font.hpp
16+
src/window/Renderer.cpp
17+
src/window/Renderer.hpp
1018
)
1119
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../common PRIVATE)
1220

13-
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/src)
14-
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../..)
15-
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/..)
21+
target_include_directories(sfml PUBLIC ${CMAKE_CURRENT_LIST_DIR}/src)
22+
target_include_directories(sfml PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../..)
23+
target_include_directories(sfml PUBLIC ${CMAKE_CURRENT_LIST_DIR}/..)
1624

17-
target_link_libraries(sfml sfml-graphics sfml-window sfml-system)
25+
target_link_libraries(sfml sfml-graphics sfml-window sfml-system sfml-audio)

graphics/sfml/src/GraphicsProvider.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88
#include <iostream>
99
#include "GraphicsProvider.hpp"
10+
#include "utils/compiler.hpp"
1011
#include "window/Window.hpp"
12+
#include "font/Font.hpp"
13+
#include "sound/Sound.hpp"
14+
#include "texture/Texture.hpp"
1115

1216
using namespace shared::graphics;
1317
using namespace arcade::graphics::sfml;
@@ -37,17 +41,17 @@ const GraphicsManifest &GraphicsProvider::getManifest() const noexcept {
3741
}
3842

3943
std::unique_ptr<IWindow> GraphicsProvider::createWindow(const IWindow::WindowInitProps &props) {
40-
return std::make_unique<Window>(props);
44+
return std::make_unique<window::Window>(props);
4145
}
4246

4347
std::shared_ptr<ISound> GraphicsProvider::createSound(const std::string &path) {
44-
return nullptr;
48+
return std::make_shared<sound::Sound>(path);
4549
}
4650

47-
std::shared_ptr<ITexture> GraphicsProvider::createTexture(const std::string &bin, const std::string &ascii) {
48-
return nullptr;
51+
std::shared_ptr<ITexture> GraphicsProvider::createTexture(const std::string &bin, unused const std::string &ascii) {
52+
return std::make_shared<texture::Texture>(bin);
4953
}
5054

5155
std::shared_ptr<IFont> GraphicsProvider::createFont(const std::string &path) {
52-
return nullptr;
56+
return std::make_shared<font::Font>(path);
5357
}

graphics/sfml/src/font/Font.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
** EPITECH PROJECT, 2024
3+
** arcade
4+
** File description:
5+
** Font.cpp
6+
*/
7+
8+
#include "Font.hpp"
9+
#include "common/exceptions/FontException.hpp"
10+
11+
using namespace arcade::graphics::sfml::font;
12+
using namespace arcade::graphics::common::exceptions;
13+
14+
Font::Font(const std::string &path)
15+
{
16+
if (!_font.loadFromFile(path))
17+
throw FontException(
18+
"Failed to load font at: " + path,
19+
"Font constructor in SFML library"
20+
);
21+
}
22+
23+
sf::Font &Font::getInnerFont()
24+
{
25+
return _font;
26+
}

graphics/sfml/src/font/Font.hpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
** EPITECH PROJECT, 2024
3+
** arcade
4+
** File description:
5+
** Font.hpp
6+
*/
7+
8+
#pragma once
9+
10+
#include <string>
11+
#include <SFML/Graphics.hpp>
12+
#include "shared/graphics/IFont.hpp"
13+
#include "utils/compiler.hpp"
14+
15+
namespace arcade::graphics::sfml::font {
16+
class Font;
17+
}
18+
19+
class arcade::graphics::sfml::font::Font : public shared::graphics::IFont {
20+
public:
21+
explicit Font(const std::string &path);
22+
~Font() override = default;
23+
24+
/**
25+
* @brief Get the inner SFML font
26+
* @return Reference to the inner SFML font
27+
*/
28+
sf::Font &getInnerFont();
29+
30+
private:
31+
sf::Font _font;
32+
};

graphics/sfml/src/sound/Sound.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
** EPITECH PROJECT, 2024
3+
** arcade
4+
** File description:
5+
** Sound.cpp
6+
*/
7+
8+
#include "Sound.hpp"
9+
#include "common/exceptions/SoundException.hpp"
10+
11+
using namespace arcade::graphics::sfml::sound;
12+
using namespace arcade::graphics::common::exceptions;
13+
14+
Sound::Sound(const std::string &path, SoundState state) {
15+
if(!_buffer.loadFromFile(path)) {
16+
throw SoundException(
17+
"Failed to load sound at: " + path,
18+
"Sound constructor in SFML library"
19+
);
20+
}
21+
_sound.setBuffer(_buffer);
22+
_sound.stop();
23+
}
24+
25+
Sound::SoundVolume Sound::getVolume() const {
26+
return static_cast<SoundVolume>(_sound.getVolume());
27+
}
28+
29+
void Sound::setVolume(SoundVolume volume) {
30+
_sound.setVolume(volume);
31+
}
32+
33+
void Sound::setState(SoundState state) {
34+
switch (state) {
35+
case PLAY:
36+
return _sound.play();
37+
case PAUSE:
38+
return _sound.pause();
39+
case STOP:
40+
return _sound.stop();
41+
}
42+
}
43+
44+
Sound::SoundState Sound::getState() const {
45+
auto state = _sound.getStatus();
46+
47+
switch (state) {
48+
case sf::Sound::Playing:
49+
return PLAY;
50+
case sf::Sound::Paused:
51+
return PAUSE;
52+
default:
53+
return STOP;
54+
}
55+
}
56+
57+
void Sound::setLoopState(bool loop) {
58+
_sound.setLoop(loop);
59+
}
60+
61+
bool Sound::getLoopState() const {
62+
return _sound.getLoop();
63+
}

graphics/sfml/src/sound/Sound.hpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
** EPITECH PROJECT, 2024
3+
** arcade
4+
** File description:
5+
** Sound.hpp
6+
*/
7+
8+
#pragma once
9+
10+
#include <string>
11+
#include <SFML/Audio.hpp>
12+
13+
#include "shared/graphics/ISound.hpp"
14+
15+
namespace arcade::graphics::sfml::sound {
16+
class Sound;
17+
}
18+
19+
class arcade::graphics::sfml::sound::Sound : public shared::graphics::ISound {
20+
public:
21+
explicit Sound(const std::string &path, SoundState state = STOP);
22+
~Sound() override = default;
23+
24+
SoundVolume getVolume() const override;
25+
SoundState getState() const override;
26+
void setVolume(SoundVolume volume) override;
27+
void setState(SoundState state) override;
28+
void setLoopState(bool loop) override;
29+
bool getLoopState() const override;
30+
31+
private:
32+
sf::SoundBuffer _buffer;
33+
sf::Sound _sound;
34+
};

0 commit comments

Comments
 (0)