Skip to content

Commit

Permalink
b
Browse files Browse the repository at this point in the history
  • Loading branch information
sthalik committed Jan 15, 2024
1 parent e850be4 commit a5acc70
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
17 changes: 9 additions & 8 deletions src/chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "world.hpp"
#include "tile-iterator.hpp"
#include <algorithm>
#include <Corrade/Containers/GrowableArray.h>
#include <Magnum/GL/Context.h>

namespace floormat {
Expand All @@ -27,11 +28,11 @@ bool chunk::empty(bool force) const noexcept
if (!force && !_maybe_empty) [[likely]]
return false;
for (auto i = 0uz; i < TILE_COUNT; i++)
if (!_objects.empty() ||
if (!_objects.isEmpty() ||
_ground && _ground->atlases[i] ||
_walls && (_walls->atlases[i*2+0] || _walls->atlases[i*2+1]))
return _maybe_empty = false;
if (!_objects.empty())
if (!_objects.isEmpty())
return false;
return true;
}
Expand Down Expand Up @@ -126,7 +127,8 @@ chunk::chunk(class world& w, chunk_coords_ ch) noexcept : _world{&w}, _coord{ch}
chunk::~chunk() noexcept
{
_teardown = true;
_objects.clear();
arrayResize(_objects, 0);
arrayShrink(_objects);
_rtree.RemoveAll();
}

Expand All @@ -142,7 +144,7 @@ void chunk::add_object_unsorted(const std::shared_ptr<object>& e)
mark_scenery_modified();
if (bbox bb; _bbox_for_scenery(*e, bb))
_add_bbox(bb);
_objects.push_back(e);
arrayAppend(_objects, e);
}

void chunk::sort_objects()
Expand All @@ -166,9 +168,8 @@ void chunk::add_object(const std::shared_ptr<object>& e)
if (bbox bb; _bbox_for_scenery(*e, bb))
_add_bbox(bb);
auto& es = _objects;
es.reserve(es.size() + 1);
auto it = std::lower_bound(es.cbegin(), es.cend(), e, object_id_lessp);
_objects.insert(it, e);
arrayInsert(es, (size_t)std::distance(es.cbegin(), it), e);
}

void chunk::remove_object(size_t i)
Expand All @@ -181,10 +182,10 @@ void chunk::remove_object(size_t i)
mark_scenery_modified();
if (bbox bb; _bbox_for_scenery(*e, bb))
_remove_bbox(bb);
es.erase(es.cbegin() + ptrdiff_t(i));
arrayRemove(es, i);
}

const std::vector<std::shared_ptr<object>>& chunk::objects() const
ArrayView<const std::shared_ptr<object>> chunk::objects() const
{
fm_assert(_objects_sorted);
return _objects;
Expand Down
5 changes: 3 additions & 2 deletions src/chunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <type_traits>
#include <array>
#include <vector>
#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/Pointer.h>
#include <Magnum/GL/Mesh.h>

Expand Down Expand Up @@ -123,7 +124,7 @@ struct chunk final
void add_object_unsorted(const std::shared_ptr<object>& e);
void sort_objects();
void remove_object(size_t i);
const std::vector<std::shared_ptr<object>>& objects() const;
ArrayView<const std::shared_ptr<object>> objects() const;

// for drawing only
static constexpr size_t max_wall_quad_count =
Expand All @@ -146,7 +147,7 @@ struct chunk final

Pointer<ground_stuff> _ground;
Pointer<wall_stuff> _walls;
std::vector<std::shared_ptr<object>> _objects;
Array<std::shared_ptr<object>> _objects;
class world* _world;
GL::Mesh ground_mesh{NoCreate}, wall_mesh{NoCreate}, scenery_mesh{NoCreate};
RTree _rtree;
Expand Down
3 changes: 2 additions & 1 deletion src/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "shaders/shader.hpp"
#include <cmath>
#include <algorithm>
#include <Corrade/Containers/GrowableArray.h>
#include <Corrade/Containers/StructuredBindings.h>
#include <Corrade/Containers/Pair.h>
#include <Magnum/Math/Functions.h>
Expand Down Expand Up @@ -257,7 +258,7 @@ void object::move_to(size_t& i, Vector2i delta, rotation new_r)
const_cast<rotation&>(r) = new_r;
const_cast<struct chunk*&>(c) = &c2;
i = (size_t)std::distance(es.cbegin(), it);
es.insert(it, std::move(e_));
arrayInsert(es, i, std::move(e_));
}
}

Expand Down
1 change: 0 additions & 1 deletion src/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "src/object-id.hpp"
#include "src/point.hpp"
#include <memory>
#include <vector>

namespace floormat {

Expand Down
3 changes: 2 additions & 1 deletion src/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "object.hpp"
#include "compat/int-hash.hpp"
#include "compat/exception.hpp"
#include <Corrade/Containers/GrowableArray.h>

using namespace floormat;

Expand Down Expand Up @@ -65,7 +66,7 @@ world::~world() noexcept
v.mark_scenery_modified();
v.mark_passability_modified();
_last_chunk = {};
v._objects.clear();
arrayResize(v._objects, 0);
}
_last_chunk = {};
_chunks.clear();
Expand Down

0 comments on commit a5acc70

Please sign in to comment.