Allocate SurfaceTriangles on the heap (fix wasm)
All checks were successful
Build and Publish WASM version of demo / Build-And-Deploy (push) Successful in 21s

This commit is contained in:
Stephen Seo 2023-08-21 15:07:03 +09:00
parent d8b81f29cc
commit 1b4e30be11
3 changed files with 17 additions and 13 deletions

View file

@ -247,7 +247,7 @@ post_check_click:
if (surface_reset_anim_timer > SURFACE_RESET_TIME) {
flags.reset(0);
} else {
for (auto &tri : surface_triangles) {
for (auto &tri : *surface_triangles) {
tri.update(dt);
}
}
@ -335,9 +335,9 @@ bool TRunnerScreen::draw() {
unsigned char alpha =
((1.0F - surface_reset_anim_timer / SURFACE_RESET_TIME_TRI_DRAW) *
255.0F);
surface_triangles.at(x * 2 + y * SURFACE_UNIT_WIDTH * 2)
surface_triangles->at(x * 2 + y * SURFACE_UNIT_WIDTH * 2)
.draw(Color{color.r, color.g, color.b, alpha});
surface_triangles.at(x * 2 + 1 + y * SURFACE_UNIT_WIDTH * 2)
surface_triangles->at(x * 2 + 1 + y * SURFACE_UNIT_WIDTH * 2)
.draw(Color{color.r, color.g, color.b, alpha});
}
}

View file

@ -6,6 +6,7 @@
// standard library includes
#include <array>
#include <bitset>
#include <memory>
#include <optional>
// third party includes
@ -62,8 +63,6 @@ class TRunnerScreen : public Screen {
SURFACE_UNIT_WIDTH * SURFACE_UNIT_HEIGHT>
surface;
std::array<BoundingBox, SURFACE_UNIT_WIDTH * SURFACE_UNIT_HEIGHT> surface_bbs;
std::array<SurfaceTriangle, SURFACE_UNIT_WIDTH * SURFACE_UNIT_HEIGHT * 2>
surface_triangles;
std::array<Walker, 4> walkers;
Camera3D camera;
@ -79,6 +78,9 @@ class TRunnerScreen : public Screen {
Vector3 camera_pos;
Vector3 camera_target;
Vector3 mouse_hit;
std::unique_ptr<std::array<SurfaceTriangle,
SURFACE_UNIT_WIDTH * SURFACE_UNIT_HEIGHT * 2> >
surface_triangles;
unsigned int idx_hit;
std::optional<unsigned int> controlled_walker_idx;
const int left_text_width;

View file

@ -3,6 +3,7 @@
// standard library includes
#include <array>
#include <memory>
// third party includes
#include <raylib.h>
@ -29,10 +30,11 @@ struct SurfaceTriangle {
};
template <typename SurfaceUnitOptT, std::size_t ASize>
extern std::array<SurfaceTriangle, ASize * 2> surface_to_triangles(
const std::array<SurfaceUnitOptT, ASize> &surface,
const std::size_t width) {
std::array<SurfaceTriangle, ASize * 2> triangles;
extern std::unique_ptr<std::array<SurfaceTriangle, ASize * 2> >
surface_to_triangles(const std::array<SurfaceUnitOptT, ASize> &surface,
const std::size_t width) {
std::unique_ptr<std::array<SurfaceTriangle, ASize * 2> > triangles =
std::make_unique<std::array<SurfaceTriangle, ASize * 2> >();
for (std::size_t idx = 0; idx < ASize; ++idx) {
std::size_t x = idx % width;
@ -44,20 +46,20 @@ extern std::array<SurfaceTriangle, ASize * 2> surface_to_triangles(
std::size_t toffset = x * 2 + y * width * 2;
if (!surface[idx].has_value()) {
triangles.at(toffset) = SurfaceTriangle();
triangles.at(toffset + 1) = SurfaceTriangle();
triangles->at(toffset) = SurfaceTriangle();
triangles->at(toffset + 1) = SurfaceTriangle();
continue;
}
const auto &surface_unit = surface[idx].value();
triangles.at(toffset) = SurfaceTriangle(
triangles->at(toffset) = SurfaceTriangle(
Vector3{0.5F, surface_unit.ne, -0.5F},
Vector3{-0.5F, surface_unit.nw, -0.5F},
Vector3{-0.5F, surface_unit.sw, 0.5F},
Vector3{posx,
(surface_unit.ne + surface_unit.nw + surface_unit.sw) / 3.0F,
posz});
triangles.at(toffset + 1) = SurfaceTriangle(
triangles->at(toffset + 1) = SurfaceTriangle(
Vector3{0.5F, surface_unit.ne, -0.5F},
Vector3{-0.5F, surface_unit.sw, 0.5F},
Vector3{0.5F, surface_unit.se, 0.5F},