Skip to content

Commit e7bf3ff

Browse files
committed
format
1 parent a48127b commit e7bf3ff

9 files changed

Lines changed: 138 additions & 82 deletions

File tree

src/common/mesh/mesh.hpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ namespace our {
3535
// Generate VBO and send data
3636
glGenBuffers(1, &VBO);
3737
glBindBuffer(GL_ARRAY_BUFFER, VBO);
38-
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), isDynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
38+
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(),
39+
isDynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
3940

4041
// Setting all the vertex attribute pointers
4142
glVertexAttribPointer(ATTRIB_LOC_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
@@ -80,17 +81,18 @@ namespace our {
8081

8182
void update(const std::vector<Vertex>& vertices, const std::vector<unsigned int>& elements) {
8283
if (!isDynamic) return;
83-
84+
8485
glBindVertexArray(VAO);
85-
86+
8687
glBindBuffer(GL_ARRAY_BUFFER, VBO);
8788
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_DYNAMIC_DRAW);
88-
89+
8990
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
90-
glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements.size() * sizeof(unsigned int), elements.data(), GL_DYNAMIC_DRAW);
91-
91+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements.size() * sizeof(unsigned int), elements.data(),
92+
GL_DYNAMIC_DRAW);
93+
9294
elementCount = static_cast<GLsizei>(elements.size());
93-
95+
9496
glBindVertexArray(0);
9597
}
9698

src/common/text/font.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "font.hpp"
2-
#include <texture/texture-utils.hpp>
2+
33
#include <fstream>
44
#include <iostream>
5+
#include <texture/texture-utils.hpp>
56

67
namespace our {
78

@@ -28,14 +29,15 @@ namespace our {
2829
}
2930

3031
// font size and line height
31-
if(data.contains("info")) {
32+
if (data.contains("info")) {
3233
fontSize = data["info"].value("size", 32.0f);
3334
}
34-
if(data.contains("common")) {
35+
if (data.contains("common")) {
3536
lineHeight = data["common"].value("lineHeight", 32.0f);
3637
}
3738

38-
// Parses the characters (by identified the texture atlas info per character, that is where in the texture is this character exactly, how big is it, and how much to advance the cursor after)
39+
// Parses the characters (by identified the texture atlas info per character, that is where in the texture is
40+
// this character exactly, how big is it, and how much to advance the cursor after)
3941
if (data.contains("chars")) {
4042
for (const auto& charData : data["chars"]) {
4143
int id = charData["id"];
@@ -46,7 +48,7 @@ namespace our {
4648
info.height = charData["height"];
4749
info.xoffset = charData.value("xoffset", 0.0f);
4850
info.yoffset = charData.value("yoffset", 0.0f);
49-
info.xadvance = charData.value("xadvance", (float)info.width); // defaults to letter width
51+
info.xadvance = charData.value("xadvance", (float)info.width); // defaults to letter width
5052
characters[(char)id] = info;
5153
}
5254
}
@@ -55,4 +57,4 @@ namespace our {
5557
return textureAtlas != nullptr;
5658
}
5759

58-
}
60+
} // namespace our

src/common/text/font.hpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#pragma once
22

3-
#include <string>
4-
#include <unordered_map>
53
#include <json/json.hpp>
4+
#include <string>
65
#include <texture/texture2d.hpp>
6+
#include <unordered_map>
77

88
namespace our {
99

@@ -26,18 +26,24 @@ namespace our {
2626

2727
void destroy();
2828
bool load(const std::string& jsonPath, const std::string& texturePath);
29-
29+
3030
const CharacterInfo* getCharacter(char c) const {
3131
auto it = characters.find(c);
3232
return (it != characters.end()) ? &it->second : nullptr;
3333
}
3434

35-
our::Texture2D* getTexture() const { return textureAtlas; }
36-
float getLineHeight() const { return lineHeight; }
37-
float getFontSize() const { return fontSize; }
35+
our::Texture2D* getTexture() const {
36+
return textureAtlas;
37+
}
38+
float getLineHeight() const {
39+
return lineHeight;
40+
}
41+
float getFontSize() const {
42+
return fontSize;
43+
}
3844

3945
Font(const Font&) = delete;
4046
Font& operator=(const Font&) = delete;
4147
};
4248

43-
}
49+
} // namespace our

src/common/text/text-renderer.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "text-renderer.hpp"
2+
23
#include <glm/gtc/matrix_transform.hpp>
34

45
namespace our {
@@ -21,7 +22,8 @@ namespace our {
2122
material->pipelineState.blending.destinationFactor = GL_ONE_MINUS_SRC_ALPHA;
2223
}
2324

24-
void TextRenderer::drawText(Font* font, const std::string& text, float startX, float startY, float scale, const glm::mat4& VP, glm::vec4 color) {
25+
void TextRenderer::drawText(Font* font, const std::string& text, float startX, float startY, float scale,
26+
const glm::mat4& VP, glm::vec4 color) {
2527
if (!font || !font->getTexture() || text.empty()) return;
2628

2729
std::vector<Vertex> vertices;
@@ -52,9 +54,9 @@ namespace our {
5254

5355
// Calculate the Texture Coordinates from the BMFont Atlas
5456
float u0 = (float)info->x / texWidth;
55-
float v0 = 1.0f - ((float)info->y / texHeight); // Top (inverted for OpenGL)
57+
float v0 = 1.0f - ((float)info->y / texHeight); // Top (inverted for OpenGL)
5658
float u1 = (float)(info->x + info->width) / texWidth;
57-
float v1 = 1.0f - ((float)(info->y + info->height) / texHeight); // Bottom
59+
float v1 = 1.0f - ((float)(info->y + info->height) / texHeight); // Bottom
5860

5961
// Generate the vertices for this letter
6062
vertices.push_back({{x0, y0, 0}, {255, 255, 255, 255}, {u0, v0}, {0, 0, 1}, {1, 0, 0}});
@@ -71,21 +73,22 @@ namespace our {
7173
indices.push_back(vertexOffset + 0);
7274

7375
vertexOffset += 4;
74-
cursorX += info->xadvance * scale; // Push the cursor forward for the next letter
76+
cursorX += info->xadvance * scale; // Push the cursor forward for the next letter
7577
}
7678

7779
dynamicMesh->update(vertices, indices);
7880

7981
material->texture = font->getTexture();
8082
material->tint = color;
8183
material->setup();
82-
84+
8385
material->shader->set("transform", VP);
84-
86+
8587
dynamicMesh->draw();
8688
}
8789

88-
void TextRenderer::drawText(Font* font, const std::string& text, UIRect rect, glm::vec2 windowSize, float scale, const glm::mat4& VP, glm::vec4 color) {
90+
void TextRenderer::drawText(Font* font, const std::string& text, UIRect rect, glm::vec2 windowSize, float scale,
91+
const glm::mat4& VP, glm::vec4 color) {
8992
if (!font || text.empty()) return;
9093

9194
// compute the width of the bounding box to put it into UIRect.size.x
@@ -101,17 +104,26 @@ namespace our {
101104
}
102105
}
103106
}
104-
105-
rect.size.x = trueTextWidth;
106-
107+
108+
rect.size.x = trueTextWidth;
109+
107110
glm::vec2 pos = rect.getScreenPosition(windowSize);
108111
drawText(font, text, pos.x, pos.y, scale, VP, color);
109112
}
110113

111114
void TextRenderer::destroy() {
112-
if (dynamicMesh) { delete dynamicMesh; dynamicMesh = nullptr; }
113-
if (shader) { delete shader; shader = nullptr; }
114-
if (material) { delete material; material = nullptr; }
115+
if (dynamicMesh) {
116+
delete dynamicMesh;
117+
dynamicMesh = nullptr;
118+
}
119+
if (shader) {
120+
delete shader;
121+
shader = nullptr;
122+
}
123+
if (material) {
124+
delete material;
125+
material = nullptr;
126+
}
115127
}
116128

117-
}
129+
} // namespace our

src/common/text/text-renderer.hpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
#pragma once
2+
#include <material/material.hpp>
3+
#include <mesh/mesh.hpp>
24
#include <string>
5+
36
#include "font.hpp"
4-
#include <mesh/mesh.hpp>
5-
#include <material/material.hpp>
67

78
namespace our {
89

910
struct UIRect {
10-
glm::vec2 anchor = {0.0f, 0.0f}; // Normalized screen attachment point (0 to 1)
11-
// pivot should always be EQUAL to anchor for static elements, for dynamic elements like if we need some 'achievement/powerup' text to slide in from the top center for example, we set anchor to (0.5, 0), top center of the screen, and pivot to (0.5, 1), bottom center of the rectangle, so the rectangle is completely hidden above the screen, we then change offset gradually to bring it into view
12-
glm::vec2 pivot = {0.0f, 0.0f}; // Normalized element origin (0 to 1).
13-
glm::vec2 offset = {0.0f, 0.0f}; // Pixel offset (X, Y)
14-
glm::vec2 size = {0.0f, 0.0f}; // Width and Height in pixels
11+
glm::vec2 anchor = {0.0f, 0.0f}; // Normalized screen attachment point (0 to 1)
12+
// pivot should always be EQUAL to anchor for static elements, for dynamic elements like if we need some
13+
// 'achievement/powerup' text to slide in from the top center for example, we set anchor to (0.5, 0), top center
14+
// of the screen, and pivot to (0.5, 1), bottom center of the rectangle, so the rectangle is completely hidden
15+
// above the screen, we then change offset gradually to bring it into view
16+
glm::vec2 pivot = {0.0f, 0.0f}; // Normalized element origin (0 to 1).
17+
glm::vec2 offset = {0.0f, 0.0f}; // Pixel offset (X, Y)
18+
glm::vec2 size = {0.0f, 0.0f}; // Width and Height in pixels
1519

1620
glm::vec2 getScreenPosition(glm::vec2 windowSize) const {
1721
glm::vec2 screenAnchorPos = windowSize * anchor;
@@ -21,19 +25,21 @@ namespace our {
2125
};
2226

2327
class TextRenderer {
24-
our::Mesh* dynamicMesh = nullptr; // just our normal mesh with gl_dynamic_draw
28+
our::Mesh* dynamicMesh = nullptr; // just our normal mesh with gl_dynamic_draw
2529
our::ShaderProgram* shader = nullptr;
2630
our::TexturedMaterial* material = nullptr;
2731

28-
void drawText(Font* font, const std::string& text, float startX, float startY, float scale, const glm::mat4& orthoVP, glm::vec4 color = glm::vec4(1.0f));
32+
void drawText(Font* font, const std::string& text, float startX, float startY, float scale,
33+
const glm::mat4& orthoVP, glm::vec4 color = glm::vec4(1.0f));
2934

3035
public:
3136
void initialize();
3237

3338
// an abstraction over private drawText using the UIRect
34-
void drawText(Font* font, const std::string& text, UIRect rect, glm::vec2 windowSize, float scale, const glm::mat4& orthoVP, glm::vec4 color = glm::vec4(1.0f));
39+
void drawText(Font* font, const std::string& text, UIRect rect, glm::vec2 windowSize, float scale,
40+
const glm::mat4& orthoVP, glm::vec4 color = glm::vec4(1.0f));
3541

3642
void destroy();
3743
};
3844

39-
}
45+
} // namespace our

src/common/texture/texture2d.hpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <glad/gl.h>
4+
45
#include <glm/glm.hpp>
56

67
namespace our {
@@ -18,7 +19,7 @@ namespace our {
1819
class Texture2D {
1920
// The OpenGL object name of this texture
2021
GLuint name = 0;
21-
22+
2223
// Needed for the text renderer, we use atlas textures so we calculate the UVs from the size
2324
int width = 0;
2425
int height = 0;
@@ -34,10 +35,19 @@ namespace our {
3435
glDeleteTextures(1, &name);
3536
}
3637

37-
void setSize(int w, int h) { width = w; height = h; }
38-
int getWidth() const { return width; }
39-
int getHeight() const { return height; }
40-
glm::ivec2 getSize() const { return {width, height}; }
38+
void setSize(int w, int h) {
39+
width = w;
40+
height = h;
41+
}
42+
int getWidth() const {
43+
return width;
44+
}
45+
int getHeight() const {
46+
return height;
47+
}
48+
glm::ivec2 getSize() const {
49+
return {width, height};
50+
}
4151

4252
// Get the internal OpenGL name of the texture which is useful for use with framebuffers
4353
GLuint getOpenGLName() const {

0 commit comments

Comments
 (0)