diff --git a/headers/enduro2d/high/node.inl b/headers/enduro2d/high/node.inl index b0e82a5d..7619a17a 100644 --- a/headers/enduro2d/high/node.inl +++ b/headers/enduro2d/high/node.inl @@ -100,63 +100,107 @@ namespace e2d::nodes void for_extracted_nodes(const node_iptr& root, F&& f) { //TODO(BlackMat): replace it to frame allocator static thread_local vector nodes; + const std::size_t begin_index = nodes.size(); + try { - extract_nodes(root, std::back_inserter(nodes)); - for ( const node_iptr& n : nodes ) { - f(n); + extract_nodes( + root, + std::back_inserter(nodes)); + + const std::size_t end_index = nodes.size(); + for ( std::size_t i = begin_index; i < end_index; ++i ) { + f(nodes[i]); } } catch (...) { - nodes.clear(); + nodes.erase( + nodes.begin() + begin_index, + nodes.end()); throw; } - nodes.clear(); + + nodes.erase( + nodes.begin() + begin_index, + nodes.end()); } template < typename F > void for_extracted_nodes(const const_node_iptr& root, F&& f) { //TODO(BlackMat): replace it to frame allocator static thread_local vector nodes; + const std::size_t begin_index = nodes.size(); + try { - extract_nodes(root, std::back_inserter(nodes)); - for ( const const_node_iptr& n : nodes ) { - f(n); + extract_nodes( + root, + std::back_inserter(nodes)); + + const std::size_t end_index = nodes.size(); + for ( std::size_t i = begin_index; i < end_index; ++i ) { + f(nodes[i]); } } catch (...) { - nodes.clear(); + nodes.erase( + nodes.begin() + begin_index, + nodes.end()); throw; } - nodes.clear(); + + nodes.erase( + nodes.begin() + begin_index, + nodes.end()); } template < typename F > void for_extracted_nodes_reversed(const node_iptr& root, F&& f) { //TODO(BlackMat): replace it to frame allocator static thread_local vector nodes; + const std::size_t begin_index = nodes.size(); + try { - extract_nodes_reversed(root, std::back_inserter(nodes)); - for ( const node_iptr& n : nodes ) { - f(n); + extract_nodes_reversed( + root, + std::back_inserter(nodes)); + + const std::size_t end_index = nodes.size(); + for ( std::size_t i = begin_index; i < end_index; ++i ) { + f(nodes[i]); } } catch (...) { - nodes.clear(); + nodes.erase( + nodes.begin() + begin_index, + nodes.end()); throw; } - nodes.clear(); + + nodes.erase( + nodes.begin() + begin_index, + nodes.end()); } template < typename F > void for_extracted_nodes_reversed(const const_node_iptr& root, F&& f) { //TODO(BlackMat): replace it to frame allocator static thread_local vector nodes; + const std::size_t begin_index = nodes.size(); + try { - extract_nodes_reversed(root, std::back_inserter(nodes)); - for ( const const_node_iptr& n : nodes ) { - f(n); + extract_nodes_reversed( + root, + std::back_inserter(nodes)); + + const std::size_t end_index = nodes.size(); + for ( std::size_t i = begin_index; i < end_index; ++i ) { + f(nodes[i]); } } catch (...) { - nodes.clear(); + nodes.erase( + nodes.begin() + begin_index, + nodes.end()); throw; } - nodes.clear(); + + nodes.erase( + nodes.begin() + begin_index, + nodes.end()); } } diff --git a/headers/enduro2d/high/systems/_systems.hpp b/headers/enduro2d/high/systems/_systems.hpp index 28fae2d6..66c0e587 100644 --- a/headers/enduro2d/high/systems/_systems.hpp +++ b/headers/enduro2d/high/systems/_systems.hpp @@ -70,19 +70,28 @@ namespace e2d::systems //TODO(BlackMat): replace it to frame allocator static thread_local vector< std::tuple> components; + const std::size_t begin_index = components.size(); + try { extract_components( owner, std::back_inserter(components), std::forward(opts)...); - for ( auto& t : components ) { - std::apply(f, t); + + const std::size_t end_index = components.size(); + for ( std::size_t i = begin_index; i < end_index; ++i ) { + std::apply(f, components[i]); } } catch (...) { - components.clear(); + components.erase( + components.begin() + begin_index, + components.end()); throw; } - components.clear(); + + components.erase( + components.begin() + begin_index, + components.end()); } template < typename... Ts, typename F, typename... Opts > @@ -90,19 +99,28 @@ namespace e2d::systems //TODO(BlackMat): replace it to frame allocator static thread_local vector< std::tuple> components; + const std::size_t begin_index = components.size(); + try { extract_components( owner, std::back_inserter(components), std::forward(opts)...); - for ( const auto& t : components ) { - std::apply(f, t); + + const std::size_t end_index = components.size(); + for ( std::size_t i = begin_index; i < end_index; ++i ) { + std::apply(f, components[i]); } } catch (...) { - components.clear(); + components.erase( + components.begin() + begin_index, + components.end()); throw; } - components.clear(); + + components.erase( + components.begin() + begin_index, + components.end()); } } @@ -113,20 +131,33 @@ namespace e2d::systems //TODO(BlackMat): replace it to frame allocator static thread_local vector< std::tuple> components; + const std::size_t begin_index = components.size(); + try { extract_components( owner, std::back_inserter(components), std::forward(opts)...); - std::sort(components.begin(), components.end(), comp); - for ( auto& t : components ) { - std::apply(f, t); + + std::sort( + components.begin() + begin_index, + components.end(), + comp); + + const std::size_t end_index = components.size(); + for ( std::size_t i = begin_index; i < end_index; ++i ) { + std::apply(f, components[i]); } } catch (...) { - components.clear(); + components.erase( + components.begin() + begin_index, + components.end()); throw; } - components.clear(); + + components.erase( + components.begin() + begin_index, + components.end()); } template < typename... Ts, typename Comp, typename F, typename... Opts > @@ -134,19 +165,32 @@ namespace e2d::systems //TODO(BlackMat): replace it to frame allocator static thread_local vector< std::tuple> components; + const std::size_t begin_index = components.size(); + try { extract_components( owner, std::back_inserter(components), std::forward(opts)...); - std::sort(components.begin(), components.end(), comp); - for ( const auto& t : components ) { - std::apply(f, t); + + std::sort( + components.begin() + begin_index, + components.end(), + comp); + + const std::size_t end_index = components.size(); + for ( std::size_t i = begin_index; i < end_index; ++i ) { + std::apply(f, components[i]); } } catch (...) { - components.clear(); + components.erase( + components.begin() + begin_index, + components.end()); throw; } - components.clear(); + + components.erase( + components.begin() + begin_index, + components.end()); } } diff --git a/samples/bin/library/scripts/emmy/high/world.lua b/samples/bin/library/scripts/emmy/high/world.lua index 3848e316..8c2849a8 100644 --- a/samples/bin/library/scripts/emmy/high/world.lua +++ b/samples/bin/library/scripts/emmy/high/world.lua @@ -2,11 +2,8 @@ local world = { } ----@overload fun(): gobject ---@overload fun(prefab: prefab): gobject ----@overload fun(parent: node): gobject ---@overload fun(prefab: prefab, parent: node): gobject ----@overload fun(parent: node, transform: t3f): gobject ---@overload fun(prefab: prefab, parent: node, transform: t3f): gobject ---@return gobject function world:instantiate(...) end diff --git a/sources/enduro2d/high/bindings/high_binds/world_binds.cpp b/sources/enduro2d/high/bindings/high_binds/world_binds.cpp index 1d0e13b2..eba22d3f 100644 --- a/sources/enduro2d/high/bindings/high_binds/world_binds.cpp +++ b/sources/enduro2d/high/bindings/high_binds/world_binds.cpp @@ -15,21 +15,12 @@ namespace e2d::bindings::high sol::no_constructor, "instantiate", sol::overload( - [](world& w) -> gobject { - return w.instantiate(); - }, [](world& w, const prefab& prefab) -> gobject { return w.instantiate(prefab); }, - [](world& w, const node_iptr& parent) -> gobject { - return w.instantiate(parent); - }, [](world& w, const prefab& prefab, const node_iptr& parent) -> gobject { return w.instantiate(prefab, parent); }, - [](world& w, const node_iptr& parent, const t3f& transform) -> gobject { - return w.instantiate(parent, transform); - }, [](world& w, const prefab& prefab, const node_iptr& parent, const t3f& transform) -> gobject { return w.instantiate(prefab, parent, transform); } diff --git a/sources/enduro2d/high/systems/label_system.cpp b/sources/enduro2d/high/systems/label_system.cpp index 5b00eed3..0164deaa 100644 --- a/sources/enduro2d/high/systems/label_system.cpp +++ b/sources/enduro2d/high/systems/label_system.cpp @@ -210,7 +210,10 @@ namespace //TODO(BlackMat): replace it to frame allocator static thread_local vector glyphs; glyphs.clear(); - glyphs.reserve(text.size()); + + if ( glyphs.capacity() < text.size() ) { + glyphs.reserve(math::max(glyphs.capacity() * 2u, text.size())); + } for ( std::size_t i = 0, e = text.size(); i < e; ++i ) { glyph_desc desc; @@ -248,7 +251,11 @@ namespace //TODO(BlackMat): replace it to frame allocator static thread_local vector strings; strings.clear(); - strings.reserve(calculate_string_count(text)); + + const std::size_t string_count = calculate_string_count(text); + if ( strings.capacity() < string_count ) { + strings.reserve(math::max(strings.capacity() * 2u, string_count)); + } f32 last_space_width = 0.f; std::size_t last_space_index = std::size_t(-1); diff --git a/sources/enduro2d/high/systems/render_system_impl/render_system_drawer.cpp b/sources/enduro2d/high/systems/render_system_impl/render_system_drawer.cpp index dcb58c28..6a4d84c7 100644 --- a/sources/enduro2d/high/systems/render_system_impl/render_system_drawer.cpp +++ b/sources/enduro2d/high/systems/render_system_impl/render_system_drawer.cpp @@ -187,10 +187,10 @@ namespace e2d::render_system_impl continue; } - int vertex_count = 0; float* uvs = nullptr; unsigned short* indices = nullptr; - int index_count = 0; + std::size_t index_count = 0; + std::size_t vertex_count = 0; const spAtlasPage* atlas_page = nullptr; const spColor* attachment_color = nullptr; @@ -205,8 +205,8 @@ namespace e2d::render_system_impl try { vertex_count = 8; - if ( vertex_count > math::numeric_cast(temp_vertices.size()) ) { - temp_vertices.resize(vertex_count); + if ( temp_vertices.size() < vertex_count ) { + temp_vertices.resize(math::max(temp_vertices.size() * 2u, vertex_count)); } } catch (...) { property_cache_.clear(); @@ -236,8 +236,8 @@ namespace e2d::render_system_impl try { vertex_count = mesh->super.worldVerticesLength; - if ( vertex_count > math::numeric_cast(temp_vertices.size()) ) { - temp_vertices.resize(vertex_count); + if ( temp_vertices.size() < vertex_count ) { + temp_vertices.resize(math::max(temp_vertices.size() * 2u, vertex_count)); } } catch (...) { property_cache_.clear(); @@ -383,10 +383,10 @@ namespace e2d::render_system_impl } try { - const std::size_t batch_vertex_count = - math::numeric_cast(vertex_count >> 1); - if ( batch_vertex_count > batch_vertices.size() ) { - batch_vertices.resize(batch_vertex_count); + const std::size_t batch_vertex_count = vertex_count >> 1; + + if ( batch_vertices.size() < batch_vertex_count ) { + batch_vertices.resize(math::max(batch_vertices.size() * 2u, batch_vertex_count)); } for ( std::size_t j = 0; j < batch_vertex_count; ++j ) {