R-Type
SfmlTypes.hpp
Go to the documentation of this file.
1 /*
2 ** EPITECH PROJECT, 2023
3 ** R-Type
4 ** File description:
5 ** SfmlTypes
6 */
7 
8 #ifndef SFMLTYPES_HPP_
9 #define SFMLTYPES_HPP_
10 #include <SFML/Audio.hpp>
11 #include <SFML/Graphics.hpp>
12 #include "utils/Vector.hpp"
13 #include "utils/Rect.hpp"
14 #include "unordered_map"
15 #include "RenderInterfaces.hpp"
16 #include "Keyboard.hpp"
17 
18 #ifdef DEBUG
19  #include "Debug.hpp"
20  #include <imgui.h>
21  #include <imgui-SFML.h>
22  #include "Error.hpp"
23 #endif
24 
25 namespace GameEngine
26 {
28  class SEvent : public IEvent<sf::Event>
29  {
30  public:
32  SEvent() = default;
35  sf::Event &getEvent() override { return _event; }
37  sf::Event::EventType &type = _event.type;
38 
39  private:
40  sf::Event _event;
41  };
42 
43  class Clock : public IClock<sf::Clock>
44  {
45  public:
47  Clock() = default;
50  sf::Clock &getClock() override { return _clock; }
51 
52  private:
53  sf::Clock _clock;
54  };
55 
57  class Texture : public ITexture<sf::Texture, sf::Rect>
58  {
59  public:
61  Texture() = default;
63  ~Texture() = default;
66  const sf::Texture &getTexture() const override { return _texture; };
70  void load(const std::string &filename, const IRect<int, sf::Rect> &area) override
71  {
72  _texture.loadFromFile(filename, area.getBaseRect());
73  }
74 
75  private:
76  sf::Texture _texture;
77  };
78 
80  class View : public IView<sf::View>
81  {
82  public:
84  View() = default;
87  View(const Rect<float> &rect) : _view{sf::Rect<float>{rect.left, rect.top, rect.width, rect.height}} {};
89  ~View() = default;
92  const sf::View &getBaseView() const override { return _view; }
95  void setCenter(const Vector2<float> &center) override { _view.setCenter(center.x, center.y); }
98  Vector2<float> getCenter() const override
99  {
100  auto center = _view.getCenter();
101  return Vector2<float>{center.x, center.y};
102  }
107  void setViewPort(const Vector2<float> &startingPoint, float width, float height) override
108  {
109  _view.setViewport(sf::FloatRect{startingPoint.x, startingPoint.y, width, height});
110  }
111 
112  private:
113  sf::View _view;
114  };
115 
117  class Window : public IWindow<sf::RenderWindow, sf::View, sf::Event, sf::Drawable>
118  {
119  public:
121 #ifdef DEBUG
123 #endif
127 #ifdef DEBUG
128  Window(
129  Debug::DebugMenu &debugMenu, int width = 1920, int height = 1080, const std::string &title = "Game window")
130  : _window(sf::VideoMode(width, height), title), _title(title), _debugMenu(debugMenu)
131  {
132  }
133 #else
134  Window(int width = 1920, int height = 1080, const std::string &title = "Game window")
135  : _window(sf::VideoMode(width, height), title), _title(title)
136  {
137  }
138 #endif
141  const sf::RenderWindow &getWindow() const override { return _window; }
144  sf::RenderWindow &getWindow() override { return _window; }
147  void draw(const sf::Drawable &drawable) override { _window.draw(drawable); }
150  void setView(const IView<sf::View> &view) override { _window.setView(view.getBaseView()); };
153  void setFramerateLimit(const float rate) override { _window.setFramerateLimit(rate); };
156  bool isOpen() const override { return _window.isOpen(); }
160  bool pollEvent(IEvent<sf::Event> &event) override
161  {
162  bool result = _window.pollEvent(event.getEvent());
163 #ifdef DEBUG
164  ImGui::SFML::ProcessEvent(_window, event.getEvent());
165 #endif
166  return result;
167  }
169  void close() override { _window.close(); }
171  void display() override { _window.display(); };
173  void clear() override { _window.clear(); };
178  void create(int width, int height, const std::string &title) override
179  {
180  _window.create(sf::VideoMode(width, height), title);
181  _title = title;
182  }
187  {
188  sf::Vector2f coord = _window.mapPixelToCoords(sf::Vector2i(pos.x, pos.y));
189  return Vector2<float>(coord.x, coord.y);
190  };
191 
192 #ifdef DEBUG
194  void initDebug() override
195  {
196  if (!ImGui::SFML::Init(_window))
197  throw Error::ImGuiSFMLInitError();
198  ImGuiIO &io = ImGui::GetIO();
199  io.IniFilename = NULL;
200  io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
201  }
203  void shutdownDebug() override { ImGui::SFML::Shutdown(); }
205  void drawDebug() override
206  {
207  ImGui::SFML::Update(_window, _debugClock.getClock().restart());
208  _debugMenu.draw();
209  ImGui::SFML::Render(_window);
210  }
211 #endif
212 
213  private:
214  sf::RenderWindow _window;
215  std::string _title;
216 #ifdef DEBUG
217  Debug::DebugMenu &_debugMenu;
218  Clock _debugClock;
219 #endif
220  };
221 
223  class Text
224  {
225  public:
227  Text(){};
229  ~Text() = default;
232  const sf::Text &getText() const { return _text; }
237  void load(const std::string &text, const sf::Font &font, size_t size)
238  {
239  _text.setString(text);
240  _text.setFont(font);
241  _text.setCharacterSize(size);
242  }
243  void setPosition(const Vector2<float> &position)
244  {
245  _text.setPosition(static_cast<int>(position.x), static_cast<int>(position.y));
246  }
247  void setString(const std::string &string) { _text.setString(string); }
248 
250  {
251  return Rectf(_text.getLocalBounds().left, _text.getLocalBounds().top, _text.getLocalBounds().width,
252  _text.getLocalBounds().height);
253  }
254 
255  private:
256  sf::Text _text;
257  };
259  class Music : public IMusic<sf::Music>
260  {
261  public:
263  Music(){};
265  ~Music() = default;
268  const sf::Music &getMusic() const override { return _music; }
271  void load(const std::string &filename)
272  {
273  _music.openFromFile(filename);
274  _filename = filename;
275  }
276  void play() override
277  {
278  if (_music.getStatus() == sf::Music::Status::Stopped) {
279  _music.play();
280  }
281  }
284  const std::string &getFilename() const { return _filename; }
285 
286  private:
287  sf::Music _music;
288  std::string _filename;
289  };
290 
292  class Font : public IFont<sf::Font>
293  {
294  public:
296  Font(){};
298  ~Font() = default;
301  const sf::Font &getFont() const override { return _font; };
304  void load(const std::string &filename) override { _font.loadFromFile(filename); }
305 
306  private:
307  sf::Font _font;
308  };
310  class Sprite : public ISprite<sf::Sprite, sf::Texture, sf::Rect>
311  {
312  public:
314  Sprite(){};
316  ~Sprite() = default;
319  const sf::Sprite &getSprite() const override { return _sprite; };
323  void load(const ITexture<sf::Texture, sf::Rect> &texture, bool resetRect = false) override
324  {
325  _sprite.setTexture(texture.getTexture(), resetRect);
326  };
329  void setPosition(const Vector2<float> position) override
330  {
331  _sprite.setPosition(sf::Vector2f{position.x, position.y});
332  };
335  void setTextureRect(const IRect<int, sf::Rect> &newRect) override
336  {
337  _sprite.setTextureRect(newRect.getBaseRect());
338  };
341  void setRect(const Recti &rect) { _sprite.setTextureRect(rect.getBaseRect()); };
342  void setScale(const float &x, const float &y) { _sprite.setScale(x, y); };
343 
344  private:
345  sf::Sprite _sprite;
346  };
347 
349  {
350  public:
352  static const std::unordered_map<Input::Keyboard::Key, sf::Keyboard::Key> sfKeys;
353 
354  static bool isKeyPressed(const Input::Keyboard::Key key) { return sf::Keyboard::isKeyPressed(sfKeys.at(key)); }
355 
356  static bool isKeyReleased(const Input::Keyboard::Key key)
357  {
358  bool isReleased = prevKeyStates[key] && sf::Keyboard::isKeyPressed(sfKeys.at(key)) == false;
359  prevKeyStates[key] = sf::Keyboard::isKeyPressed(sfKeys.at(key));
360 
361  return isReleased;
362  }
363 
364  private:
365  static std::unordered_map<Input::Keyboard::Key, bool> prevKeyStates;
366  };
367 } // namespace GameEngine
368 #endif /* !MLTYPES_HPP_ */
Definition: SfmlTypes.hpp:44
Clock()=default
Default constructor for Clock.
sf::Clock & getClock() override
Returns the wrapped sf::Clock.
Definition: SfmlTypes.hpp:50
Class representing a font wrapper for sf::Font.
Definition: SfmlTypes.hpp:293
Font()
Default constructor for Font.
Definition: SfmlTypes.hpp:296
~Font()=default
Destructor for Font.
const sf::Font & getFont() const override
Returns the wrapped sf::Font.
Definition: SfmlTypes.hpp:301
void load(const std::string &filename) override
Loads the font from a file.
Definition: SfmlTypes.hpp:304
Interface for clock.
Definition: RenderInterfaces.hpp:48
Interface for event.
Definition: RenderInterfaces.hpp:122
virtual T & getEvent()=0
Get the event stored.
Interfaces for the text font.
Definition: RenderInterfaces.hpp:19
interface for music handling
Definition: RenderInterfaces.hpp:79
Interface for rectangle.
Definition: RenderInterfaces.hpp:35
virtual const M< T > getBaseRect() const =0
Get the rectangle stored.
Interface for sprite.
Definition: RenderInterfaces.hpp:99
Interface for texture.
Definition: RenderInterfaces.hpp:62
virtual const T & getTexture() const =0
Get the texture stored.
Interface for view.
Definition: RenderInterfaces.hpp:133
virtual const T & getBaseView() const =0
Get the view stored.
Interface for the window.
Definition: RenderInterfaces.hpp:152
Definition: SfmlTypes.hpp:349
static bool isKeyPressed(const Input::Keyboard::Key key)
Definition: SfmlTypes.hpp:354
static bool isKeyReleased(const Input::Keyboard::Key key)
Definition: SfmlTypes.hpp:356
static const std::unordered_map< Input::Keyboard::Key, sf::Keyboard::Key > sfKeys
Static unordered map that maps custom keyboard keys to corresponding sf::Keyboard::Key values.
Definition: SfmlTypes.hpp:352
Class representing a music wrapper for sf::Music.
Definition: SfmlTypes.hpp:260
~Music()=default
Destructor for Music.
const sf::Music & getMusic() const override
Returns the wrapped sf::Music.
Definition: SfmlTypes.hpp:268
Music()
Default constructor for Music.
Definition: SfmlTypes.hpp:263
const std::string & getFilename() const
Returns the filename of the loaded music.
Definition: SfmlTypes.hpp:284
void load(const std::string &filename)
Loads music from a file.
Definition: SfmlTypes.hpp:271
void play() override
play the loaded music
Definition: SfmlTypes.hpp:276
T height
Definition: Rect.hpp:40
T top
Definition: Rect.hpp:38
T left
Definition: Rect.hpp:37
const sf::Rect< T > getBaseRect() const override
get the rect stored in the class
Definition: Rect.hpp:44
T width
Definition: Rect.hpp:39
Class representing a custom event wrapper for sf::Event.
Definition: SfmlTypes.hpp:29
SEvent()=default
Default constructor for SEvent.
sf::Event & getEvent() override
Returns the wrapped sf::Event.
Definition: SfmlTypes.hpp:35
sf::Event::EventType & type
Reference to the EventType of the wrapped sf::Event.
Definition: SfmlTypes.hpp:37
Class representing a sprite wrapper for sf::Sprite with additional functionality.
Definition: SfmlTypes.hpp:311
void setTextureRect(const IRect< int, sf::Rect > &newRect) override
Sets the texture rectangle of the sprite.
Definition: SfmlTypes.hpp:335
void load(const ITexture< sf::Texture, sf::Rect > &texture, bool resetRect=false) override
Loads the sprite with a specified texture and optional reset of the texture rectangle.
Definition: SfmlTypes.hpp:323
void setScale(const float &x, const float &y)
Definition: SfmlTypes.hpp:342
Sprite()
Default constructor for Sprite.
Definition: SfmlTypes.hpp:314
~Sprite()=default
Destructor for Sprite.
void setRect(const Recti &rect)
Sets the texture rectangle of the sprite using a custom rectangle.
Definition: SfmlTypes.hpp:341
void setPosition(const Vector2< float > position) override
Sets the position of the sprite.
Definition: SfmlTypes.hpp:329
const sf::Sprite & getSprite() const override
Returns the wrapped sf::Sprite.
Definition: SfmlTypes.hpp:319
Class representing a text wrapper for sf::Text with basic functionality.
Definition: SfmlTypes.hpp:224
void load(const std::string &text, const sf::Font &font, size_t size)
Loads text with specified font and size.
Definition: SfmlTypes.hpp:237
Text()
Default constructor for Text.
Definition: SfmlTypes.hpp:227
~Text()=default
Destructor for Text.
void setString(const std::string &string)
Definition: SfmlTypes.hpp:247
Rectf getLocalBounds() const
Definition: SfmlTypes.hpp:249
void setPosition(const Vector2< float > &position)
Definition: SfmlTypes.hpp:243
const sf::Text & getText() const
Returns the wrapped sf::Text.
Definition: SfmlTypes.hpp:232
Class representing a texture wrapper for sf::Texture with an associated area.
Definition: SfmlTypes.hpp:58
void load(const std::string &filename, const IRect< int, sf::Rect > &area) override
Loads a texture from a file with a specified area.
Definition: SfmlTypes.hpp:70
~Texture()=default
Destructor for Texture.
Texture()=default
Default constructor for Texture.
const sf::Texture & getTexture() const override
Returns the wrapped sf::Texture.
Definition: SfmlTypes.hpp:66
T y
Definition: Vector.hpp:101
T x
Definition: Vector.hpp:98
Class representing a view wrapper for sf::View.
Definition: SfmlTypes.hpp:81
void setViewPort(const Vector2< float > &startingPoint, float width, float height) override
set a viewport rectangle
Definition: SfmlTypes.hpp:107
View(const Rect< float > &rect)
Constructor for View.
Definition: SfmlTypes.hpp:87
Vector2< float > getCenter() const override
get the center of the view
Definition: SfmlTypes.hpp:98
View()=default
default constructor
const sf::View & getBaseView() const override
Returns the wrapped sf::View.
Definition: SfmlTypes.hpp:92
void setCenter(const Vector2< float > &center) override
set center of the view
Definition: SfmlTypes.hpp:95
~View()=default
Destructor for View.
Class representing a window wrapper for sf::RenderWindow with additional functionality.
Definition: SfmlTypes.hpp:118
Window(int width=1920, int height=1080, const std::string &title="Game window")
Constructor for Window.
Definition: SfmlTypes.hpp:134
sf::RenderWindow & getWindow() override
Returns the wrapped sf::RenderWindow.
Definition: SfmlTypes.hpp:144
Vector2< float > mapPixelToCoords(const Vector2< int > &pos)
Maps pixel coordinates to world coordinates.
Definition: SfmlTypes.hpp:186
void setFramerateLimit(const float rate) override
Set the maximum framerate of the window.
Definition: SfmlTypes.hpp:153
bool isOpen() const override
Checks if the window is open.
Definition: SfmlTypes.hpp:156
void display() override
Displays the contents of the window.
Definition: SfmlTypes.hpp:171
void draw(const sf::Drawable &drawable) override
Draws a sf::Drawable object on the window.
Definition: SfmlTypes.hpp:147
bool pollEvent(IEvent< sf::Event > &event) override
Polls and retrieves the next event.
Definition: SfmlTypes.hpp:160
void close() override
Closes the window.
Definition: SfmlTypes.hpp:169
void clear() override
Clears the contents of the window.
Definition: SfmlTypes.hpp:173
void create(int width, int height, const std::string &title) override
Creates a new window with the specified dimensions and title.
Definition: SfmlTypes.hpp:178
const sf::RenderWindow & getWindow() const override
Returns the wrapped sf::RenderWindow.
Definition: SfmlTypes.hpp:141
void setView(const IView< sf::View > &view) override
Sets the view of the window.
Definition: SfmlTypes.hpp:150
Key
Definition: Keyboard.hpp:28
Definition: AssetManager.hpp:15
std::size_t EventType
value to describe the event type
Definition: Event.hpp:23
Rect< float > Rectf
Definition: Rect.hpp:88
Event
enum of all event types
Definition: Event.hpp:30