Files
Roguelike_Algorithm_Visuali…/src/engine/utility/camera.cpp
2021-12-12 12:06:31 -05:00

46 lines
1.4 KiB
C++

//
// Created by m on 12/11/21.
//
#include "camera.hpp"
Rectangle Camera::get_bounds() {
return cached_bounds;
}
void Camera::move_camera(Point new_center) {
this->center = new_center;
this->cached_bounds = calculate_bounds();
}
void Camera::set_bounds(int max_x, int max_y) {
this->max_x = max_x;
this->max_y = max_y;
this->cached_bounds = calculate_bounds();
}
Rectangle Camera::calculate_bounds() {
int minx = std::clamp(center.x-(tiles_rendered_x/2), 0, std::max(0,max_x - tiles_rendered_x));
int miny = std::clamp(center.y-(tiles_rendered_y/2), 0, std::max(0,max_y - tiles_rendered_y));
int width = std::min(tiles_rendered_x, max_x-minx);
int height = std::min(tiles_rendered_y, max_y-miny);
return Rectangle{minx, miny, width, height};
}
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<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;
}