Compare commits

...

2 Commits

Author SHA1 Message Date
d5fd89e228 ranges done, cool stuff 2021-12-12 12:06:31 -05:00
f360be0c8d About to test out ranges of points 2021-12-12 10:42:53 -05:00
5 changed files with 38 additions and 18 deletions

View File

@@ -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<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 "rectangle.hpp"
#include <range/v3/view.hpp>
#include <algorithm>
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<Point> get_range();
Camera()=default;
Camera(const Point &center, int tilesRenderedX, int tilesRenderedY, int maxX, int maxY) : center(center),

View File

@@ -7,7 +7,7 @@
#include <vector>
#include <range/v3/view.hpp>
template<typename T>
class Grid2D {
private:
@@ -28,6 +28,15 @@ public:
T get(int x, int y) {
return grid[y][x];
}
auto get_range() {
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;
}
};

View File

@@ -30,20 +30,17 @@ void Visualizer::render(Renderer *renderer) {
auto wall = Sprite{&tiles, 497};
auto character = Sprite{&characters, this->sprite_index};
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

View File

@@ -10,8 +10,8 @@
#include <range/v3/view.hpp>
#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 '#'