Zoom fully working and code cleaned up, scaling x and y now done in game rather than rendering, closes #19

This commit is contained in:
m
2023-07-23 14:39:16 -04:00
parent 3d056481b2
commit ddfa3c1dc9
4 changed files with 15 additions and 14 deletions

View File

@@ -26,8 +26,8 @@ void Camera::set_bounds(int max_x, int max_y) {
} }
Rectangle Camera::calculate_bounds() const { Rectangle Camera::calculate_bounds() const {
auto tiles_rendered_x = this->getTilesRenderedX(); auto tiles_rendered_x = this->get_tiles_rendered_x();
auto tiles_rendered_y = this->getTilesRenderedY(); auto tiles_rendered_y = this->get_tiles_rendered_y();
int minx = std::clamp(center.x-(tiles_rendered_x/2), 0, std::max(0,max_x - tiles_rendered_x)); 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 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 width = std::min(tiles_rendered_x, max_x-minx);
@@ -53,19 +53,19 @@ ranges::any_view<Point> Camera::get_range() {
}) | ranges::views::join; }) | ranges::views::join;
} }
float Camera::getScale() const { float Camera::get_scale() const {
return scale; return scale;
} }
void Camera::setScale(float scale) { void Camera::set_scale(float scale) {
this->scale = scale; this->scale = scale;
this->cached_bounds = calculate_bounds(); this->cached_bounds = calculate_bounds();
} }
int Camera::getTilesRenderedX() const { int Camera::get_tiles_rendered_x() const {
return (int)std::ceil(tiles_rendered_x / this->scale); return (int)std::ceil(tiles_rendered_x / this->scale);
} }
int Camera::getTilesRenderedY() const { int Camera::get_tiles_rendered_y() const {
return (int)std::ceil(tiles_rendered_y / this->scale); return (int)std::ceil(tiles_rendered_y / this->scale);
} }

View File

@@ -22,12 +22,12 @@ private:
Rectangle cached_bounds=Rectangle(); Rectangle cached_bounds=Rectangle();
Rectangle calculate_bounds() const; Rectangle calculate_bounds() const;
float scale = 1; float scale = 1;
[[nodiscard]] int getTilesRenderedX() const; [[nodiscard]] int get_tiles_rendered_x() const;
[[nodiscard]] int getTilesRenderedY() const; [[nodiscard]] int get_tiles_rendered_y() const;
public: public:
[[nodiscard]] float getScale() const; [[nodiscard]] float get_scale() const;
void setScale(float scale); void set_scale(float scale);
public: public:
Rectangle get_bounds(); Rectangle get_bounds();

View File

@@ -31,7 +31,6 @@ void TileMap::fill(bool wall) {
draw_floor(p.x, p.y); draw_floor(p.x, p.y);
} }
} }
} }
bool TileMap::is_wall(int x, int y) { bool TileMap::is_wall(int x, int y) {

View File

@@ -16,8 +16,10 @@ bool Visualizer::update(InputResult input) {
case KEY_LEFT: x -= 1; break; case KEY_LEFT: x -= 1; break;
case KEY_UP: y -= 1; break; case KEY_UP: y -= 1; break;
case KEY_DOWN: y += 1; break; case KEY_DOWN: y += 1; break;
case KEY_KP_PLUS: this->camera.setScale(this->camera.getScale() * 2); break; case KEY_KP_PLUS:
case KEY_KP_MINUS: this->camera.setScale(this->camera.getScale() / 2); break; this->camera.set_scale(this->camera.get_scale() * 2); break;
case KEY_KP_MINUS:
this->camera.set_scale(this->camera.get_scale() / 2); break;
default: break; default: break;
} }
if(input.keycode >= InputProcessorKeycode::KEY_1 && input.keycode <= InputProcessorKeycode::KEY_9) { if(input.keycode >= InputProcessorKeycode::KEY_1 && input.keycode <= InputProcessorKeycode::KEY_9) {
@@ -38,7 +40,7 @@ bool Visualizer::update(InputResult input) {
void Visualizer::render(Renderer *renderer) { void Visualizer::render(Renderer *renderer) {
SpriteSheet tiles = SpriteSheet{"sprites/map1.bmp", tile_width, tile_height}; SpriteSheet tiles = SpriteSheet{"sprites/map1.bmp", tile_width, tile_height};
SpriteSheet characters = SpriteSheet{"sprites/character.bmp", tile_width, tile_height, COLOR_BLACK}; SpriteSheet characters = SpriteSheet{"sprites/character.bmp", tile_width, tile_height, COLOR_BLACK};
float scale = this->camera.getScale(); float scale = this->camera.get_scale();
int scaled_tile_width = (int)std::ceil(tile_width * scale); int scaled_tile_width = (int)std::ceil(tile_width * scale);
int scaled_tile_height = (int)std::ceil(tile_height * scale); int scaled_tile_height = (int)std::ceil(tile_height * scale);
auto grass = Sprite{&tiles, 0}; auto grass = Sprite{&tiles, 0};