From f3cb626f7bcb2d181f58826e435941eb47613260 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 17 Oct 2022 13:56:52 +0200 Subject: a --- main/app.hpp | 3 +++ main/draw.cpp | 48 ++++++++++++++++++++++++++++-------------------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/main/app.hpp b/main/app.hpp index 1237f205..82d5dd73 100644 --- a/main/app.hpp +++ b/main/app.hpp @@ -50,6 +50,7 @@ struct app final : Platform::Application void event_mouse_leave(); void drawEvent() override; + std::array get_draw_bounds() const noexcept; void draw_world(); void draw_wireframe_quad(global_coords pt); void draw_wireframe_box(local_coords pt); @@ -98,6 +99,8 @@ struct app final : Platform::Application enum_bitset keys; Magnum::Timeline timeline; editor _editor; + + static constexpr std::int32_t BASE_X = 0, BASE_Y = 0; }; constexpr Vector2 app::project(const Vector3 pt) diff --git a/main/draw.cpp b/main/draw.cpp index fde48915..691095b2 100644 --- a/main/draw.cpp +++ b/main/draw.cpp @@ -34,25 +34,31 @@ void app::drawEvent() { timeline.nextFrame(); } -void app::draw_world() +std::array app::get_draw_bounds() const noexcept { - // TODO chunk offset - std::int16_t minx = 0, maxx = 0, miny = 0, maxy = 0; + std::int16_t minx = BASE_X, maxx = BASE_X, miny = BASE_Y, maxy = BASE_Y; { - const auto fn = [&](int x, int y) { - const auto pos = pixel_to_tile({(float)x, (float)y}).chunk(); - minx = std::min(minx, pos.x); - maxx = std::max(maxx, pos.x); - miny = std::min(miny, pos.y); - maxy = std::max(maxy, pos.y); + const auto fn = [&](std::int32_t x, std::int32_t y) { + const auto pos = pixel_to_tile({float(x + BASE_X), float(y + BASE_Y)}).chunk(); + minx = std::min(minx, pos.x); + maxx = std::max(maxx, pos.x); + miny = std::min(miny, pos.y); + maxy = std::max(maxy, pos.y); }; const auto sz = windowSize(); - const auto x = sz[0], y = sz[1]; + const auto x = sz[0]-1, y = sz[1]-1; fn(0, 0); fn(x, 0); fn(0, y); fn(x, y); } + fflush(stdout); + return {minx, maxx, miny, maxy}; +} + +void app::draw_world() +{ + auto [minx, maxx, miny, maxy] = get_draw_bounds(); const auto old_camera_offset = _shader.camera_offset(); _shader.set_tint({1, 1, 1, 1}); @@ -60,9 +66,14 @@ void app::draw_world() for (std::int16_t y = miny; y <= maxy; y++) for (std::int16_t x = minx; x <= maxx; x++) { - const auto offset = project({float(x)*TILE_MAX_DIM*TILE_SIZE[0], - float(y)*TILE_MAX_DIM*TILE_SIZE[1], + const auto offset = project({float(x + BASE_X)*TILE_MAX_DIM*TILE_SIZE[0], + float(y + BASE_Y)*TILE_MAX_DIM*TILE_SIZE[1], 0}); + if (x == 0 && y == 0) + { + printf("0 0 --> %f %f\n", offset[0], offset[1]); + fflush(stdout); + } _shader.set_camera_offset(offset + old_camera_offset); auto c = _world[chunk_coords{x, y}]; _floor_mesh.draw(_shader, *c); @@ -71,9 +82,9 @@ void app::draw_world() for (std::int16_t y = miny; y <= maxy; y++) for (std::int16_t x = minx; x <= maxx; x++) { - const auto offset = project({float(x)*TILE_MAX_DIM*TILE_SIZE[0], - float(y)*TILE_MAX_DIM*TILE_SIZE[1], - 0}); + const auto offset = project({float(x + BASE_X)*TILE_MAX_DIM*TILE_SIZE[0], + float(y + BASE_Y)*TILE_MAX_DIM*TILE_SIZE[1], + 0}); _shader.set_camera_offset(offset + old_camera_offset); auto c = _world[chunk_coords{x, y}]; _wall_mesh.draw(_shader, *c); @@ -86,11 +97,8 @@ void app::draw_wireframe_quad(global_coords pos) { constexpr float LINE_WIDTH = 1; - { - const auto c = _world[pos.chunk()]; - if (const auto& tile = (*c)[pos.local()]; !tile.ground_image) - return; - } + if (const auto& [c, tile] = _world[pos]; !tile.ground_image) + return; const auto pt = pos.to_signed(); constexpr auto X = TILE_SIZE[0], Y = TILE_SIZE[1]; -- cgit v1.2.3