Skip to content

Commit 0bceae9

Browse files
committed
Fix emscripten memory issue
1 parent 45277ba commit 0bceae9

16 files changed

Lines changed: 193 additions & 115 deletions

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ elseif (WIN32)
9696
elseif(EMSCRIPTEN)
9797
message("Platform: Emscripten")
9898
add_compile_definitions(
99-
"GL_GLEXT_PROTOTYPES"
100-
"GL3_PROTOTYPES"
99+
"GL_GLEXT_PROTOTYPES=1"
100+
"GL3_PROTOTYPES=1"
101+
"EGL_EGLEXT_PROTOTYPES=1"
101102
)
102103
add_compile_options("-pthread" "-fexceptions")
103104
add_link_options(

hellotext.cpp

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
#include <span>
22

3+
#include "txt/utils.hpp"
34
#include "txt/window.hpp"
45
#include "txt/renderer.hpp"
56
#include "txt/text/fonts.hpp"
67

78
using namespace txt::types;
89

9-
static auto entry([[maybe_unused]]std::span<char const*> const& args) -> void {
10-
fmt::print("hello\n");
11-
auto win = txt::make_window({"Hello, Text!"});
12-
auto ren = txt::make_renderer(win);
10+
auto entry([[maybe_unused]]std::span<char const*> const& args) -> void {
11+
static auto win = txt::make_window({"Hello, Text!"});
12+
static auto ren = txt::make_renderer(win);
13+
// ren->load_font({
14+
// "./res/fonts/Cozette/CozetteVector.ttf",
15+
// 128,
16+
// txt::text_render_mode::raster,
17+
// });
1318
ren->load_font({
14-
"./res/fonts/Cozette/CozetteVector.ttf",
15-
13,
19+
"./res/fonts/RobotoMono/RobotoMonoNerdFontMono-Medium.ttf",
20+
16,
1621
txt::text_render_mode::raster,
22+
{0, 0xFFFF}
1723
});
1824

19-
auto mouse_x = 0.0f;
20-
auto mouse_y = 0.0f;
25+
static auto mouse_x = 0.0f;
26+
static auto mouse_y = 0.0f;
27+
static auto rot = 0.0f;
2128

29+
win->add_event_listener([&](txt::touch_start_event const& e) {
30+
if (e.points().empty()) return;
31+
mouse_x = f32(e.points()[0].x());
32+
mouse_y = f32(win->height() - e.points()[0].y());
33+
fmt::print("{}\n", e.str());
34+
});
35+
win->add_event_listener([&](txt::touch_move_event const& e) {
36+
if (e.points().empty()) return;
37+
mouse_x = f32(e.points()[0].x());
38+
mouse_y = f32(win->height() - e.points()[0].y());
39+
fmt::print("{} {}: {}={}\n", e.str(), mouse_y, win->height(), win->buffer_width());
40+
});
2241
win->add_event_listener([&](txt::mouse_move_event const& e) {
2342
fmt::print("{}\n", e.str());
24-
mouse_x = f32(e.x());
25-
mouse_y = f32(win->buffer_height() - e.y());
2643
});
2744
win->add_event_listener([&](txt::key_down_event const& e) {
2845
fmt::print("{}\n", e.str());
@@ -33,13 +50,23 @@ static auto entry([[maybe_unused]]std::span<char const*> const& args) -> void {
3350
});
3451

3552
txt::loop(win, [&] {
53+
rot += txt::pi_f32 / 180.0f * 3.0f;
54+
3655
ren->begin();
3756
ren->viewport(0, 0, win->buffer_width(), win->buffer_height());
3857
ren->clear_color(0x000000);
3958
ren->clear();
4059

4160
ren->text("Hello, World!", {0.0f, 0.0f});
4261

62+
ren->rect(glm::vec2{mouse_x, mouse_y}, glm::vec2{1.0f, 100.0f},
63+
rot, glm::vec4{1.0f, 1.0f, 1.0f, 1.0f}, {});
64+
ren->rect(glm::vec2{mouse_x, mouse_y}, glm::vec2{1.0f, 100.0f},
65+
rot+txt::pi_f32/2.0f, glm::vec4{1.0f, 1.0f, 1.0f, 1.0f}, {});
66+
67+
ren->rect(glm::vec2{mouse_x, mouse_y}, glm::vec2{32.0f, 32.0f},
68+
rot, glm::vec4{1.0f, 0.0f, 0.0f, 1.0f}, {});
69+
4370
ren->end();
4471

4572
win->swap();

txt/event.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ class touch_point {
647647

648648
[[nodiscard]]auto str() const -> std::string {
649649
using namespace std::string_literals;
650-
std::string str{"touch_point {"};
650+
std::string str{"touch_point { "};
651651
str += "id: " + std::to_string(m_id) + ", ";
652652
str += "x: " + fmt::format("{:.3f}", m_x) + ", ";
653653
str += "y: " + fmt::format("{:.3f}", m_y) + " }";
@@ -683,7 +683,7 @@ class touch_event : public event {
683683
str += "size: " + std::to_string(m_size) + ", ";
684684
for (std::size_t i = 0; i < m_size; ++i) {
685685
str += "[" + std::to_string(i) + "]: " + m_points[i].str();
686-
if (i < m_points.size() - 1) str += ", ";
686+
if (i < m_size - 1) str += ", ";
687687
else str += " }";
688688
}
689689
return str;

txt/graphics/buffer.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
#include <vector>
55

66
#ifdef __EMSCRIPTEN__
7-
#define GL_GLEXT_PROTOTYPES 1
8-
#define GL3_PROTOTYPES 1
9-
#define EGL_EGLEXT_PROTOTYPES 1
107
#include "GL/gl.h"
118
#else
129
#include "glad/glad.h"

txt/graphics/renderer.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ auto renderer::begin() -> void {
8989
for (auto& [st, data] : m_shader_texture_rects)
9090
st.size = 0;
9191

92-
m_text_engine->begin();
92+
m_depth = 0.0f;
9393
}
9494

9595
auto renderer::end() -> void {
@@ -105,7 +105,9 @@ auto renderer::end() -> void {
105105
m_rect_default_shader->upload_mat4("u_projection", m_projection);
106106
m_rect_descriptor->bind();
107107
m_rect_index_buffer->bind();
108-
glDrawElementsInstanced(GL_TRIANGLES, GLsizei(m_rect_index_buffer->size()), gl_type(m_rect_index_buffer->type()), nullptr, GLsizei(m_color_rect_size));
108+
glDrawElementsInstanced(GL_TRIANGLES, GLsizei(m_rect_index_buffer->size()),
109+
gl_type(m_rect_index_buffer->type()), nullptr,
110+
GLsizei(m_color_rect_size));
109111
}
110112
for (auto const& [st, vec] : m_shader_texture_rects) {
111113
auto const bytes = st.size * sizeof(rect_instance);
@@ -121,9 +123,10 @@ auto renderer::end() -> void {
121123
st.texture->bind(0);
122124
m_rect_descriptor->bind();
123125
m_rect_index_buffer->bind();
124-
glDrawElementsInstanced(GL_TRIANGLES, GLsizei(m_rect_index_buffer->size()), gl_type(m_rect_index_buffer->type()), nullptr, GLsizei(m_color_rect_size));
126+
glDrawElementsInstanced(GL_TRIANGLES, GLsizei(m_rect_index_buffer->size()),
127+
gl_type(m_rect_index_buffer->type()), nullptr,
128+
GLsizei(m_color_rect_size));
125129
}
126-
m_text_engine->end();
127130
}
128131

129132
auto renderer::viewport(std::int32_t x, std::int32_t y, std::uint32_t width, std::uint32_t height) -> void {
@@ -165,10 +168,10 @@ auto renderer::rect(glm::vec2 const& position, glm::vec2 const& size,
165168
auto renderer::rect(glm::vec2 const& position, glm::vec2 const& size,
166169
float const& rotation, shader_ref_t shader,
167170
texture_ref_t texture, glm::vec2 const& uv,
168-
glm::vec2 const& uv_size, float const& z_offset) -> void {
169-
rect_instance rect{
171+
glm::vec2 const& uv_size, float const& zdepth) -> void {
172+
rect_instance tmp{
170173
.color = {1.0f, 1.0f, 1.0f, 1.0f},
171-
.position = {position, m_depth + z_offset},
174+
.position = {position, zdepth},
172175
.scale = {size, 1.0f},
173176
.rotation = {0.0f, 0.0f, rotation},
174177
.uv_offset = uv,
@@ -180,7 +183,7 @@ auto renderer::rect(glm::vec2 const& position, glm::vec2 const& size,
180183
m_shader_texture_rects[key] = {};
181184
it = m_shader_texture_rects.find(key);
182185
}
183-
it->second.push_back(rect);
186+
it->second.push_back(tmp);
184187
++it->first.size;
185188
}
186189

txt/graphics/shader.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
#include "glm/gtc/type_ptr.hpp"
77

88
#ifdef __EMSCRIPTEN__
9-
#define GL_GLEXT_PROTOTYPES 1
10-
#define GL3_PROTOTYPES 1
11-
#define EGL_EGLEXT_PROTOTYPES 1
129
#include "GL/gl.h"
1310
#else
1411
#include "glad/glad.h"

txt/graphics/texture.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#include "texture.hpp"
22

33
#ifdef __EMSCRIPTEN__
4-
#define GL_GLEXT_PROTOTYPES 1
5-
#define GL3_PROTOTYPES 1
6-
#define EGL_EGLEXT_PROTOTYPES 1
74
#include "GL/gl.h"
85
#else
96
#include "glad/glad.h"

txt/renderer.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
#include "glm/mat4x4.hpp"
2121

2222
#ifdef __EMSCRIPTEN__
23-
#define GL_GLEXT_PROTOTYPES 1
24-
#define GL3_PROTOTYPES 1
25-
#define EGL_EGLEXT_PROTOTYPES 1
2623
#include "GL/gl.h"
2724
#else
2825
#include "glad/glad.h"
@@ -98,7 +95,7 @@ class renderer {
9895
auto rect(glm::vec2 const& position, glm::vec2 const& size,
9996
float const& rotation, shader_ref_t shader,
10097
texture_ref_t texture, glm::vec2 const& uv,
101-
glm::vec2 const& uv_size, float const& z_offset = 0.0f) -> void;
98+
glm::vec2 const& uv_size, float const& zdepth) -> void;
10299

103100
auto text(std::string const& str, glm::vec2 const& position,
104101
glm::vec4 const& color = {1.0f, 1.0f, 1.0f, 1.0f},

txt/text/engine.cpp

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#include "engine.hpp"
22

3+
#include <cmath>
4+
35
#include "renderer.hpp"
46
#include "utf8.h"
57

8+
#include "graphics/image.hpp"
9+
610
namespace txt {
711
[[maybe_unused]]static constexpr float quad_vertices[]{
812
// x, y, z, u, v,
@@ -20,7 +24,24 @@ text_engine::text_engine(window_ref_t window) : m_window(window) { }
2024

2125
auto text_engine::load(font_load_params const& params) -> void {
2226
m_manager.load(params);
23-
set_default(std::begin(m_manager)->second);
27+
if (m_current == nullptr)
28+
set_default(std::begin(m_manager)->second);
29+
30+
constexpr auto round_up2 = [](auto const& value) {
31+
return std::pow(2, std::ceil(std::log2(value) / std::log2(2)));
32+
};
33+
auto cols = static_cast<usize>(std::ceil(std::sqrt(m_current->size())));
34+
cols = static_cast<usize>(round_up2(cols));
35+
auto const size = static_cast<usize>(round_up2(m_current->max_font_size() * cols));
36+
m_buffer = make_image_u8(nullptr, size, size, m_current->color_channels());
37+
m_uv.y = size;
38+
39+
for (auto const& [code, glyph] : m_current->data()) {
40+
insert_bitmap(glyph);
41+
}
42+
43+
m_buffer->fliph();
44+
write_png("test.png", *m_buffer);
2445
}
2546

2647
auto text_engine::draw(std::string const& str, glm::vec2 const& position, glm::vec4 const& color, glm::vec2 const& scale) -> void {
@@ -50,11 +71,22 @@ auto text_engine::set_default(font& font) -> void {
5071
m_current = &font;
5172
}
5273

53-
54-
auto text_engine::begin() -> void {
74+
auto text_engine::generate() -> void {
5575
}
5676

57-
auto text_engine::end() -> void {
58-
}
77+
auto text_engine::insert_bitmap(txt::glyph const& glyph) -> void {
78+
auto const& bm = glyph.bitmap;
79+
for (std::size_t i = 0; i < bm.height(); ++i) {
80+
for (std::size_t j = 0; j < bm.width(); ++j) {
81+
auto const pixel = bm.pixel(j, i);
82+
m_buffer->set(std::size_t(m_uv.x) + j, std::size_t(m_uv.y) - i, pixel);
83+
}
84+
}
5985

86+
m_uv.x += std::int32_t(m_current->max_font_size());
87+
if (m_uv.x >= std::int32_t(m_buffer->width() - m_current->max_font_size())) {
88+
m_uv.x = 0;
89+
m_uv.y -= std::int32_t(m_current->max_font_size());
90+
}
91+
}
6092
} // namespace txt

txt/text/engine.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ class text_engine {
2323
auto set_default(font& font) -> void;
2424

2525
private:
26-
friend class renderer;
27-
auto begin() -> void;
28-
auto end() -> void;
26+
auto generate() -> void;
27+
auto insert_bitmap(txt::glyph const& glyph) -> void;
2928

3029
private:
31-
window_ref_t m_window;
32-
font_manager m_manager{};
33-
font const* m_current{};
30+
window_ref_t m_window;
31+
font_manager m_manager{};
32+
font const* m_current{nullptr};
33+
image_u8_ref_t m_buffer{nullptr};
34+
glm::ivec2 m_uv{0.0f, 0.0f};
3435
};
3536

3637
using text_engine_ref_t = ref<text_engine>;

0 commit comments

Comments
 (0)