Skip to content

Commit af415c3

Browse files
committed
Importing text engine
1 parent fa53099 commit af415c3

File tree

9 files changed

+1432
-0
lines changed

9 files changed

+1432
-0
lines changed

common/quad/QuadBatch.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "common/quad/QuadBatch.h"
2+
3+
namespace chr
4+
{
5+
namespace gl
6+
{
7+
void QuadBatch::clear()
8+
{
9+
vertexBuffer.clear();
10+
colorBuffer.clear();
11+
}
12+
13+
void QuadBatch::pack()
14+
{
15+
vertices.shrink_to_fit();
16+
colors.shrink_to_fit();
17+
}
18+
19+
void QuadBatch::flush(const ShaderProgram &shaderProgram, Buffer<GLushort> &indexBuffer, bool useColor)
20+
{
21+
if (!empty())
22+
{
23+
vertexBuffer.bind(shaderProgram);
24+
25+
if (useColor)
26+
{
27+
colorBuffer.bind(shaderProgram);
28+
}
29+
30+
indexBuffer.bind(shaderProgram);
31+
indexBuffer.draw(GL_TRIANGLES, size() * 6);
32+
33+
vertexBuffer.unbind(shaderProgram);
34+
35+
if (useColor)
36+
{
37+
colorBuffer.unbind(shaderProgram);
38+
}
39+
}
40+
}
41+
}
42+
}

common/quad/QuadBatch.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#pragma once
2+
3+
#include "chr/gl/Matrix.h"
4+
#include "chr/gl/Buffer.h"
5+
6+
namespace chr
7+
{
8+
namespace gl
9+
{
10+
class QuadBatch
11+
{
12+
public:
13+
Buffer<Vertex<XYZ.UV>> vertexBuffer;
14+
Buffer<Vertex<RGBA>> colorBuffer;
15+
16+
std::vector<Vertex<XYZ.UV>> &vertices;
17+
std::vector<Vertex<RGBA>> &colors;
18+
19+
QuadBatch()
20+
:
21+
vertices(vertexBuffer->storage),
22+
colors(colorBuffer->storage)
23+
{}
24+
25+
void clear();
26+
void pack();
27+
void flush(const ShaderProgram &shader, Buffer<GLushort> &indexBuffer, bool useColor = false);
28+
29+
inline int size() const
30+
{
31+
return vertices.size() >> 2;
32+
}
33+
34+
inline bool empty() const
35+
{
36+
return vertices.empty();
37+
}
38+
39+
inline void addQuad(const Matrix &matrix, const Quad<XYZ.UV> &quad)
40+
{
41+
matrix.addTransformedQuad(quad, vertices);
42+
}
43+
44+
inline void addQuad(const Quad<XYZ.UV> &quad, float z = 0)
45+
{
46+
vertices.emplace_back(quad.x1, quad.y1, z, quad.u1, quad.v1);
47+
vertices.emplace_back(quad.x2, quad.y1, z, quad.u2, quad.v1);
48+
vertices.emplace_back(quad.x2, quad.y2, z, quad.u2, quad.v2);
49+
vertices.emplace_back(quad.x1, quad.y2, z, quad.u1, quad.v2);
50+
}
51+
52+
inline void addColor(const glm::vec4 &color)
53+
{
54+
colors.emplace_back(color);
55+
colors.emplace_back(color);
56+
colors.emplace_back(color);
57+
colors.emplace_back(color);
58+
}
59+
};
60+
}
61+
}

common/quad/QuadBatchMap.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#pragma once
2+
3+
#include "common/quad/QuadBatch.h"
4+
5+
#include <map>
6+
#include <memory>
7+
8+
namespace chr
9+
{
10+
namespace gl
11+
{
12+
template<typename T> class QuadBatchMap
13+
{
14+
public:
15+
std::map<T*, std::unique_ptr<QuadBatch>> map;
16+
17+
QuadBatchMap() = default;
18+
QuadBatchMap(const QuadBatchMap &other) = delete; // QuadBatchMap CAN'T BE COPIED (I.E. BECAUSE OF THE map OF unique_ptr)
19+
20+
void clear()
21+
{
22+
for (auto &it : map)
23+
{
24+
it.second->clear();
25+
}
26+
}
27+
28+
void pack()
29+
{
30+
for (auto it = map.begin(); it != map.end();)
31+
{
32+
if (it->second->empty())
33+
{
34+
it = map.erase(it);
35+
}
36+
else
37+
{
38+
it->second->pack();
39+
++it;
40+
}
41+
}
42+
}
43+
44+
QuadBatch* getBatch(T *texture)
45+
{
46+
auto it = map.find(texture);
47+
48+
if (it == map.end())
49+
{
50+
auto batch = new QuadBatch;
51+
map.emplace(texture, std::unique_ptr<QuadBatch>(batch));
52+
53+
return batch;
54+
}
55+
else
56+
{
57+
return it->second.get();
58+
}
59+
}
60+
61+
void flush(const ShaderProgram &shader, Buffer<GLushort> &indexBuffer, bool useColor = false) const
62+
{
63+
for (auto &it : map)
64+
{
65+
it.first->bind(); // RELOADS THE TEXTURE, IF NECESSARY
66+
it.second->flush(shader, indexBuffer, useColor);
67+
it.first->unbind();
68+
}
69+
}
70+
};
71+
}
72+
}

0 commit comments

Comments
 (0)