summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-04-06 15:29:33 +0200
committerStanislaw Halik <sthalik@misaki.pl>2023-04-06 15:29:33 +0200
commit82ec1a07c79b1c96688464609d043d6ce23685b1 (patch)
tree90473a42d3799bafb4fd9fd2a23f779bf36770db
parent9703532543d528757e4635f0eb5c91ccb4c2e300 (diff)
a
-rw-r--r--editor/draw.cpp10
-rw-r--r--editor/update.cpp5
-rw-r--r--main/draw.cpp17
-rw-r--r--src/world.cpp9
-rw-r--r--src/world.hpp3
5 files changed, 30 insertions, 14 deletions
diff --git a/editor/draw.cpp b/editor/draw.cpp
index 6d07f871..285299b6 100644
--- a/editor/draw.cpp
+++ b/editor/draw.cpp
@@ -80,9 +80,10 @@ void app::draw_collision_boxes()
for (int16_t x = minx; x <= maxx; x++)
{
const chunk_coords pos{x, y};
- auto& c = world[pos];
- if (c.empty())
+ auto* c_ = world.at(pos);
+ if (!c_)
continue;
+ auto& c = *c_;
c.ensure_passability();
const with_shifted_camera_offset o{shader, pos, {minx, miny}, {maxx, maxy}};
if (floormat_main::check_chunk_visible(shader.camera_offset(), sz))
@@ -116,9 +117,10 @@ void app::draw_collision_boxes()
for (int16_t x = minx; x <= maxx; x++)
{
const chunk_coords c_pos{x, y};
- auto& c = world[c_pos];
- if (c.empty())
+ auto* c_ = world.at(c_pos);
+ if (!c_)
continue;
+ auto& c = *c_;
c.ensure_passability();
const with_shifted_camera_offset o{shader, c_pos, {minx, miny}, {maxx, maxy}};
if (floormat_main::check_chunk_visible(shader.camera_offset(), sz))
diff --git a/editor/update.cpp b/editor/update.cpp
index 069399c7..eb4029dc 100644
--- a/editor/update.cpp
+++ b/editor/update.cpp
@@ -185,7 +185,10 @@ void app::update_world(float dt)
for (int16_t y = miny; y <= maxy; y++)
for (int16_t x = minx; x <= maxx; x++)
{
- auto& c = world[chunk_coords{x, y}];
+ auto* c_ = world.at(chunk_coords{x, y});
+ if (!c_)
+ continue;
+ auto& c = *c_;
const auto& es = c.entities();
start: const auto size = es.size();
for (auto i = size-1; i != (size_t)-1; i--)
diff --git a/main/draw.cpp b/main/draw.cpp
index 37b88e7c..f87f2e9c 100644
--- a/main/draw.cpp
+++ b/main/draw.cpp
@@ -97,11 +97,12 @@ void main_impl::draw_world() noexcept
for (int16_t x = minx; x <= maxx; x++)
{
const chunk_coords pos{x, y};
- if (!_world.contains(pos))
+ if (pos == chunk_coords_{} && !_world.contains(pos))
app.maybe_initialize_chunk(pos, _world[pos]);
- auto& c = _world[pos];
- if (c.empty())
+ auto* c_ = _world.at(pos);
+ if (!c_)
continue;
+ auto& c = *c_;
const with_shifted_camera_offset o{_shader, pos, {minx, miny}, {maxx, maxy}};
if (check_chunk_visible(_shader.camera_offset(), sz))
_floor_mesh.draw(_shader, c);
@@ -113,9 +114,10 @@ void main_impl::draw_world() noexcept
for (int16_t x = minx; x <= maxx; x++)
{
const chunk_coords pos{x, y};
- auto& c = _world[pos];
- if (c.empty())
+ auto* c_ = _world.at(pos);
+ if (!c_)
continue;
+ auto& c = *c_;
const with_shifted_camera_offset o{_shader, pos, {minx, miny}, {maxx, maxy}};
if (check_chunk_visible(_shader.camera_offset(), sz))
_wall_mesh.draw(_shader, c);
@@ -129,9 +131,10 @@ void main_impl::draw_world() noexcept
for (int16_t x = minx; x <= maxx; x++)
{
const chunk_coords pos{x, y};
- auto& c = _world[pos];
- if (c.empty())
+ auto* c_ = _world.at(pos);
+ if (!c_)
continue;
+ auto& c = *c_;
const with_shifted_camera_offset o{_shader, pos, {minx, miny}, {maxx, maxy}};
if (check_chunk_visible(_shader.camera_offset(), sz))
_anim_mesh.draw(_shader, sz, c, _clickable_scenery);
diff --git a/src/world.cpp b/src/world.cpp
index cdf8c087..56a0b735 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -94,6 +94,15 @@ auto world::operator[](global_coords pt) noexcept -> pair
return { c, c[pt.local()] };
}
+chunk* world::at(chunk_coords_ c) noexcept
+{
+ auto it = _chunks.find(c);
+ if (it != _chunks.end())
+ return &it->second;
+ else
+ return nullptr;
+}
+
bool world::contains(chunk_coords_ c) const noexcept
{
return _chunks.find(c) != _chunks.cend();
diff --git a/src/world.hpp b/src/world.hpp
index 98fa7f47..47e6b01d 100644
--- a/src/world.hpp
+++ b/src/world.hpp
@@ -7,8 +7,6 @@
#include <unordered_map>
#include <memory>
-namespace floormat { struct chunk_coords_; }
-
template<>
struct std::hash<floormat::chunk_coords_> final {
floormat::size_t operator()(const floormat::chunk_coords_& coord) const noexcept;
@@ -56,6 +54,7 @@ public:
chunk& operator[](chunk_coords_ c) noexcept;
pair operator[](global_coords pt) noexcept;
+ chunk* at(chunk_coords_ c) noexcept;
bool contains(chunk_coords_ c) const noexcept;
void clear();
void collect(bool force = false);