CG-Project2
Loading...
Searching...
No Matches
TextManager.hpp
1#pragma once
2
3#include "../../include/Buffer/VertexArray.hpp"
4#include "../../include/Buffer/VertexBuffer.hpp"
5#include "../../include/Core/Layer.hpp"
6#include "../../include/Core/Utils.hpp"
7#include "../../include/Shader/Shader.hpp"
8
9#include <glm/glm.hpp>
10#include <map>
11#include <vector>
12
13namespace ogl {
14 struct TextSettings {
15 std::string fontFace = "./resources/fonts/arial.ttf";
16 unsigned int pixelWidth = 0;
17 unsigned int pixelHeight = 48;
18 unsigned int numChars = 128;
19 };
20
21 struct TextHelper {
22 std::string text = "Simple Text";
23 glm::vec2 position = {0, 0};
24 float scale = 1.f;
25 glm::vec3 color = {1, 0, 0};
26 };
27
28 class Text {
29 public:
30 void init();
31
32 inline void bindVAO() { this->m_vao.bind(); }
33
34 inline void bindVBO() { this->m_vbo.bind(); }
35
36 // inline unsigned int getVboId() { return this->m_vbo.getId(); }
37
38 inline void setText(std::string text) { this->m_config.text = std::move(text); }
39
40 inline std::string getText() const { return this->m_config.text; }
41
42 inline void setColor(const glm::vec4 &color) { this->m_config.color = color; }
43
44 inline glm::vec3 getColor() const { return this->m_config.color; }
45
46 inline glm::vec2 getPosition() const { return this->m_config.position; }
47
48 inline void setPosition(const glm::vec2 &pos) { this->m_config.position = pos; }
49
50 inline float getScale() const { return this->m_config.scale; }
51
52 inline void setScale(const float &scale) { this->m_config.scale = scale; }
53
54 Text() = delete;
55 Text(const TextHelper &config) :
56 m_config(config) {}
57
58 private:
59 TextHelper m_config{};
60
61 VertexArray m_vao{};
62 VertexBuffer m_vbo{};
63
64 bool m_created = false;
65 };
66
67 class TextManager : public Layer {
68
69 struct Character {
70 unsigned int textureId;
71 glm::ivec2 size;
72 glm::ivec2 bearing;
73 long long advance;
74 };
75
76 public:
77 virtual void onAttach() override;
78
79 virtual void onRender() override;
80
81 Shared<Text> addText(const TextHelper &config);
82
83 TextManager(TextManager &other) = delete;
84
85 void operator=(const TextManager &other) = delete;
86
87 /*
88 * Retrieves the instance of the TextManager if it's not created.
89 *
90 * @return `TextManager` unique object.
91 */
92 inline static Shared<TextManager> instance() {
93 if (s_pointer == nullptr) {
94 Shared<TextManager> copy(new TextManager());
95 copy.swap(s_pointer);
96 }
97
98 return s_pointer;
99 }
100
101 virtual ~TextManager() override = default;
102
103 private:
104 TextManager() = default;
105
106 inline static Shared<TextManager> s_pointer = nullptr;
107
108 std::map<unsigned long long, Character> m_characters{};
109 std::vector<Shared<Text>> m_text{};
110
111 TextSettings m_settings{};
112 };
113} // namespace ogl
Definition VertexArray.hpp:6
Definition VertexBuffer.hpp:7
Definition TextManager.hpp:21
Definition TextManager.hpp:14