From d5fd89e2283d04e38b6693f09f380797bf67b5f7 Mon Sep 17 00:00:00 2001 From: littlefoot Date: Sun, 12 Dec 2021 12:06:31 -0500 Subject: [PATCH] ranges done, cool stuff --- src/engine/utility/camera.cpp | 14 +++++++++++++- src/engine/utility/camera.hpp | 2 ++ src/engine/utility/grid2d.hpp | 16 +++++++--------- src/game/visualizer.cpp | 25 +++++++++++-------------- src/game/visualizer.hpp | 4 ++-- 5 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/engine/utility/camera.cpp b/src/engine/utility/camera.cpp index 2a13f1e..7ae526b 100644 --- a/src/engine/utility/camera.cpp +++ b/src/engine/utility/camera.cpp @@ -1,7 +1,6 @@ // // Created by m on 12/11/21. // - #include "camera.hpp" Rectangle Camera::get_bounds() { @@ -31,3 +30,16 @@ Point Camera::camera_coords_to_screen_coords(Point camera_coord) { auto bounds = get_bounds(); return Point{camera_coord.x-bounds.x, camera_coord.y-bounds.y}; } + +ranges::any_view Camera::get_range() { + auto bounds = get_bounds(); + int height = bounds.height; + int width = bounds.width; + int miny = bounds.y; + int minx = bounds.x; + return ranges::views::iota(miny, height) | ranges::views::transform([width, minx](int y) { + return ranges::views::iota(minx, width) | ranges::views::transform([y](int x) { + return Point{x,y}; + }); + }) | ranges::views::join; +} diff --git a/src/engine/utility/camera.hpp b/src/engine/utility/camera.hpp index 024c310..9e37712 100644 --- a/src/engine/utility/camera.hpp +++ b/src/engine/utility/camera.hpp @@ -8,6 +8,7 @@ #include "point.hpp" #include "rectangle.hpp" +#include #include class Camera { @@ -25,6 +26,7 @@ public: void move_camera(Point new_center); void set_bounds(int max_x, int max_y); Point camera_coords_to_screen_coords(Point camera_coord); + ranges::any_view get_range(); Camera()=default; Camera(const Point ¢er, int tilesRenderedX, int tilesRenderedY, int maxX, int maxY) : center(center), diff --git a/src/engine/utility/grid2d.hpp b/src/engine/utility/grid2d.hpp index 9dabcac..1f6a82d 100644 --- a/src/engine/utility/grid2d.hpp +++ b/src/engine/utility/grid2d.hpp @@ -29,15 +29,13 @@ public: return grid[y][x]; } auto get_range() { - auto res = grid | ranges::views::enumerate | ranges::views::transform([](auto it) { - int index_y = std::get<0>(it); - std::vector element = std::get<1>(it); - return element | ranges::views::enumerate | ranges::views::transform([&](auto pair) { - int index_x = std::get<0>(pair); - return Point{index_x, index_y}; - }); - }); - return ranges::views::join(res); + int height = get_height(); + int width = get_width(); + return ranges::views::iota(0, height) | ranges::views::transform([height, width](int y) { + return ranges::views::iota(0, width) | ranges::views::transform([y](int x) { + return Point{x,y}; + }); + }) | ranges::views::join; } }; diff --git a/src/game/visualizer.cpp b/src/game/visualizer.cpp index d82d8ea..a60fc08 100644 --- a/src/game/visualizer.cpp +++ b/src/game/visualizer.cpp @@ -29,21 +29,18 @@ void Visualizer::render(Renderer *renderer) { auto grass = Sprite{&tiles, 0}; auto wall = Sprite{&tiles, 497}; auto character = Sprite{&characters, this->sprite_index}; - auto g = tile_map.get_range(); auto bounds = this->camera.get_bounds(); - for(int ty : ranges::views::iota(bounds.y, bounds.y+bounds.height)) { - for(int tx : ranges::views::iota(bounds.x, bounds.x+bounds.width)) { - auto wp = camera.camera_coords_to_screen_coords(Point{tx, ty}); - int wx = wp.x, wy = wp.y; - if(this->tile_map.get(tx, ty) == FLOOR_CHAR) { - renderer->draw_sprite(grass, wx*TILE_WIDTH, wy*TILE_HEIGHT); - } - else { - renderer->draw_sprite(wall, wx*TILE_WIDTH, wy*TILE_HEIGHT); - } - if(tx == x && ty == y) { - renderer->draw_sprite(character, wx*TILE_WIDTH, wy*TILE_HEIGHT); - } + for(Point p : camera.get_range()) { + auto wp = camera.camera_coords_to_screen_coords(p); + int wx = wp.x, wy = wp.y; + if(this->tile_map.get(p.x, p.y) == FLOOR_CHAR) { + renderer->draw_sprite(grass, wx*TILE_WIDTH, wy*TILE_HEIGHT); + } + else { + renderer->draw_sprite(wall, wx*TILE_WIDTH, wy*TILE_HEIGHT); + } + if(p.x == x && p.y == y) { + renderer->draw_sprite(character, wx*TILE_WIDTH, wy*TILE_HEIGHT); } } return; // for breaking diff --git a/src/game/visualizer.hpp b/src/game/visualizer.hpp index 306d28c..411faec 100644 --- a/src/game/visualizer.hpp +++ b/src/game/visualizer.hpp @@ -10,8 +10,8 @@ #include #ifndef RLA_IIPP_VISUALIZER_HPP #define RLA_IIPP_VISUALIZER_HPP -#define MAP_SIZE_W 20 -#define MAP_SIZE_H 20 +#define MAP_SIZE_W 10 +#define MAP_SIZE_H 10 #define TILE_WIDTH 48 #define TILE_HEIGHT 48 #define WALL_CHAR '#'