ShishGL  1.0
A simple modular cross-platform graphics library
Platform.hpp
Go to the documentation of this file.
1 /*============================================================================*/
2 #ifndef SHISHGL_SFML_PLATFORM_HPP
3 #define SHISHGL_SFML_PLATFORM_HPP
4 /*============================================================================*/
5 #include <cstddef>
6 #include <string_view>
7 
8 #include <SFML/Graphics.hpp>
9 
10 #include "IPlatform.hpp"
11 /*============================================================================*/
12 namespace Sh {
13 
14  class SfmlPlatform : public IPlatform {
15  public:
16 
17  SfmlPlatform();
18 
19  bool isRunning() override;
20 
21  /* Input */
22  /*--------------------------------------------------------------------*/
23  Vector2<double> getMousePos() override;
24  /*--------------------------------------------------------------------*/
25 
26  /* View */
27  /*--------------------------------------------------------------------*/
28  void setViewport(const Frame& frame) override;
29 
30  const Vector2<double>& getDisplaySize() override;
31  /*--------------------------------------------------------------------*/
32 
33  /* Draw */
34  /*--------------------------------------------------------------------*/
35  class SfmlContext : public IContext {
36  public:
37 
38  explicit SfmlContext(const Vector2<size_t>& size,
39  const Color& color);
40 
41  explicit SfmlContext(const std::string_view& filename);
42 
43  void update(const Color* data) override;
44 
45  void updateAt(const Vector2<size_t>& pos,
46  const Color& color) override;
47 
48  ~SfmlContext() override = default;
49 
50  private:
51 
52  sf::Texture texture;
53 
54  friend class SfmlPlatform;
55  };
56 
58  const Color& color) override;
59 
60  IContext* loadContextFromImage(const std::string_view& filename) override;
61 
62  void saveContextAsImage(IContext* context, const std::string_view& filename) override;
63 
64  /*--------------------------------------------------------------------*/
65  void setColor(const Color& color) override;
66 
67  void setTexture(const ResourceManager::Resource& texture) override;
68 
69  void drawPoint(const Vector2<double>& pos) override;
70 
71  void drawLine(const Vector2<double>& start,
72  const Vector2<double>& end) override;
73 
74  void drawRectangle(const Vector2<double>& pos,
75  const Vector2<double>& size) override;
76 
77  void drawCircle(const Vector2<double>& pos,
78  const double& radius) override;
79  /*--------------------------------------------------------------------*/
80 
81  /* Image */
82  /*--------------------------------------------------------------------*/
83  void displayContext(const IPlatform::IContext* context,
84  const Vector2<double>& pos) override;
85 
86  /* Text */
87  /*--------------------------------------------------------------------*/
88  void setFont(const ResourceManager::Resource& font) override;
89 
90  void setFontSize(const size_t& font_size) override;
91 
92  void displayText(const std::string_view& text,
93  const Frame& frame,
94  Align align) override;
95  /*--------------------------------------------------------------------*/
96 
97 
98  ~SfmlPlatform() override = default;
99 
100  protected:
101 
102  /* Essential */
103  /*--------------------------------------------------------------------*/
104  bool initDisplay(int* argc_ptr, char* argv[]) override;
105 
106  bool closeDisplay() override;
107  /*--------------------------------------------------------------------*/
108 
109  /* Rendering */
110  /*--------------------------------------------------------------------*/
111  void clear(const Color& color) override;
112 
113  void display() override;
114  /*--------------------------------------------------------------------*/
115 
116  /* Events */
117  /*--------------------------------------------------------------------*/
118  bool pollEvent() override;
119  /*--------------------------------------------------------------------*/
120 
121  private:
122 
123  /* IMPLEMENTATION SPECIAL */
124  /*====================================================================*/
125 
126  Vector2<double> display_size;
127 
128  bool is_running;
129 
130  sf::RenderWindow* canvas;
131 
132  Color active_color;
133 
134  sf::Texture* active_texture;
135  std::unordered_map<std::string_view, sf::Texture*> textures;
136 
137  sf::Font* active_font;
138  std::unordered_map<std::string_view, sf::Font*> fonts;
139 
140  size_t font_size;
141 
142  };
143 
144 }
145 /*============================================================================*/
146 #endif //SHISHGL_SFML_PLATFORM_HPP
147 /*============================================================================*/
Sh::SfmlPlatform
Definition: Platform.hpp:14
Sh::IPlatform::Align
Align
Definition: IPlatform.hpp:83
Sh::SfmlPlatform::isRunning
bool isRunning() override
Definition: Platform.cpp:37
Sh::SfmlPlatform::drawCircle
void drawCircle(const Vector2< double > &pos, const double &radius) override
Definition: Draw.cpp:170
prettify_colors.text
text
Definition: prettify_colors.py:6
Sh::Frame
Definition: Frame.hpp:9
Sh::SfmlPlatform::SfmlContext::updateAt
void updateAt(const Vector2< size_t > &pos, const Color &color) override
Definition: Draw.cpp:27
Sh::SfmlPlatform::drawLine
void drawLine(const Vector2< double > &start, const Vector2< double > &end) override
Definition: Draw.cpp:117
Sh::SfmlPlatform::getMousePos
Vector2< double > getMousePos() override
Definition: Platform.cpp:81
Sh::SfmlPlatform::loadContextFromImage
IContext * loadContextFromImage(const std::string_view &filename) override
Definition: Draw.cpp:39
Sh::SfmlPlatform::initDisplay
bool initDisplay(int *argc_ptr, char *argv[]) override
Definition: Platform.cpp:19
Sh::SfmlPlatform::drawRectangle
void drawRectangle(const Vector2< double > &pos, const Vector2< double > &size) override
Definition: Draw.cpp:141
Sh::Vector2< double >
Sh::SfmlPlatform::setTexture
void setTexture(const ResourceManager::Resource &texture) override
Definition: Draw.cpp:85
Sh::SfmlPlatform::saveContextAsImage
void saveContextAsImage(IContext *context, const std::string_view &filename) override
Definition: Draw.cpp:43
Sh::SfmlPlatform::closeDisplay
bool closeDisplay() override
Definition: Platform.cpp:43
Sh::SfmlPlatform::~SfmlPlatform
~SfmlPlatform() override=default
Sh::SfmlPlatform::pollEvent
bool pollEvent() override
Definition: Events.cpp:139
Sh::SfmlPlatform::setColor
void setColor(const Color &color) override
Definition: Draw.cpp:78
Sh::SfmlPlatform::setFontSize
void setFontSize(const size_t &font_size) override
Definition: Draw.cpp:233
Sh::SfmlPlatform::drawPoint
void drawPoint(const Vector2< double > &pos) override
Definition: Draw.cpp:101
Sh::SfmlPlatform::SfmlContext::update
void update(const Color *data) override
Definition: Draw.cpp:23
Sh
Definition: CoreApplication.hpp:10
Sh::SfmlPlatform::displayContext
void displayContext(const IPlatform::IContext *context, const Vector2< double > &pos) override
Definition: Draw.cpp:198
Sh::SfmlPlatform::getDisplaySize
const Vector2< double > & getDisplaySize() override
Definition: Platform.cpp:75
Sh::SfmlPlatform::display
void display() override
Definition: Platform.cpp:69
Sh::SfmlPlatform::SfmlContext::~SfmlContext
~SfmlContext() override=default
Sh::SfmlPlatform::clear
void clear(const Color &color) override
Definition: Platform.cpp:63
Sh::SfmlPlatform::SfmlPlatform
SfmlPlatform()
Definition: Platform.cpp:7
Sh::SfmlPlatform::setFont
void setFont(const ResourceManager::Resource &font) override
Definition: Draw.cpp:217
Sh::IPlatform
Definition: IPlatform.hpp:13
Sh::Color
Definition: Color.hpp:9
Sh::SfmlPlatform::setViewport
void setViewport(const Frame &frame) override
Definition: Draw.cpp:57
Sh::ResourceManager::Resource
Definition: ResourceManager.hpp:14
IPlatform.hpp
Sh::IPlatform::IContext
Definition: IPlatform.hpp:32
Sh::SfmlPlatform::createContext
IContext * createContext(const Vector2< size_t > &size, const Color &color) override
Definition: Draw.cpp:34
Sh::SfmlPlatform::displayText
void displayText(const std::string_view &text, const Frame &frame, Align align) override
Definition: Draw.cpp:239
Sh::SfmlPlatform::SfmlContext::SfmlContext
SfmlContext(const Vector2< size_t > &size, const Color &color)
Definition: Draw.cpp:8
Sh::SfmlPlatform::SfmlContext
Definition: Platform.hpp:35