ranges done, cool stuff

This commit is contained in:
2021-12-12 12:06:31 -05:00
parent f360be0c8d
commit d5fd89e228
5 changed files with 35 additions and 26 deletions

View File

@@ -1,7 +1,6 @@
// //
// Created by m on 12/11/21. // Created by m on 12/11/21.
// //
#include "camera.hpp" #include "camera.hpp"
Rectangle Camera::get_bounds() { Rectangle Camera::get_bounds() {
@@ -31,3 +30,16 @@ Point Camera::camera_coords_to_screen_coords(Point camera_coord) {
auto bounds = get_bounds(); auto bounds = get_bounds();
return Point{camera_coord.x-bounds.x, camera_coord.y-bounds.y}; return Point{camera_coord.x-bounds.x, camera_coord.y-bounds.y};
} }
ranges::any_view<Point> 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;
}

View File

@@ -8,6 +8,7 @@
#include "point.hpp" #include "point.hpp"
#include "rectangle.hpp" #include "rectangle.hpp"
#include <range/v3/view.hpp>
#include <algorithm> #include <algorithm>
class Camera { class Camera {
@@ -25,6 +26,7 @@ public:
void move_camera(Point new_center); void move_camera(Point new_center);
void set_bounds(int max_x, int max_y); void set_bounds(int max_x, int max_y);
Point camera_coords_to_screen_coords(Point camera_coord); Point camera_coords_to_screen_coords(Point camera_coord);
ranges::any_view<Point> get_range();
Camera()=default; Camera()=default;
Camera(const Point &center, int tilesRenderedX, int tilesRenderedY, int maxX, int maxY) : center(center), Camera(const Point &center, int tilesRenderedX, int tilesRenderedY, int maxX, int maxY) : center(center),

View File

@@ -29,15 +29,13 @@ public:
return grid[y][x]; return grid[y][x];
} }
auto get_range() { auto get_range() {
auto res = grid | ranges::views::enumerate | ranges::views::transform([](auto it) { int height = get_height();
int index_y = std::get<0>(it); int width = get_width();
std::vector<T> element = std::get<1>(it); return ranges::views::iota(0, height) | ranges::views::transform([height, width](int y) {
return element | ranges::views::enumerate | ranges::views::transform([&](auto pair) { return ranges::views::iota(0, width) | ranges::views::transform([y](int x) {
int index_x = std::get<0>(pair); return Point{x,y};
return Point{index_x, index_y};
}); });
}); }) | ranges::views::join;
return ranges::views::join(res);
} }
}; };

View File

@@ -29,23 +29,20 @@ void Visualizer::render(Renderer *renderer) {
auto grass = Sprite{&tiles, 0}; auto grass = Sprite{&tiles, 0};
auto wall = Sprite{&tiles, 497}; auto wall = Sprite{&tiles, 497};
auto character = Sprite{&characters, this->sprite_index}; auto character = Sprite{&characters, this->sprite_index};
auto g = tile_map.get_range();
auto bounds = this->camera.get_bounds(); auto bounds = this->camera.get_bounds();
for(int ty : ranges::views::iota(bounds.y, bounds.y+bounds.height)) { for(Point p : camera.get_range()) {
for(int tx : ranges::views::iota(bounds.x, bounds.x+bounds.width)) { auto wp = camera.camera_coords_to_screen_coords(p);
auto wp = camera.camera_coords_to_screen_coords(Point{tx, ty});
int wx = wp.x, wy = wp.y; int wx = wp.x, wy = wp.y;
if(this->tile_map.get(tx, ty) == FLOOR_CHAR) { if(this->tile_map.get(p.x, p.y) == FLOOR_CHAR) {
renderer->draw_sprite(grass, wx*TILE_WIDTH, wy*TILE_HEIGHT); renderer->draw_sprite(grass, wx*TILE_WIDTH, wy*TILE_HEIGHT);
} }
else { else {
renderer->draw_sprite(wall, wx*TILE_WIDTH, wy*TILE_HEIGHT); renderer->draw_sprite(wall, wx*TILE_WIDTH, wy*TILE_HEIGHT);
} }
if(tx == x && ty == y) { if(p.x == x && p.y == y) {
renderer->draw_sprite(character, wx*TILE_WIDTH, wy*TILE_HEIGHT); renderer->draw_sprite(character, wx*TILE_WIDTH, wy*TILE_HEIGHT);
} }
} }
}
return; // for breaking return; // for breaking
} }

View File

@@ -10,8 +10,8 @@
#include <range/v3/view.hpp> #include <range/v3/view.hpp>
#ifndef RLA_IIPP_VISUALIZER_HPP #ifndef RLA_IIPP_VISUALIZER_HPP
#define RLA_IIPP_VISUALIZER_HPP #define RLA_IIPP_VISUALIZER_HPP
#define MAP_SIZE_W 20 #define MAP_SIZE_W 10
#define MAP_SIZE_H 20 #define MAP_SIZE_H 10
#define TILE_WIDTH 48 #define TILE_WIDTH 48
#define TILE_HEIGHT 48 #define TILE_HEIGHT 48
#define WALL_CHAR '#' #define WALL_CHAR '#'