Added some helpers to camera and range lib to replace the standard ranges library due to some issues clangd has with the gcc implementation
This commit is contained in:
33
src/engine/utility/camera.cpp
Normal file
33
src/engine/utility/camera.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// 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};
|
||||
}
|
||||
Reference in New Issue
Block a user