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:
@@ -5,6 +5,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);
|
||||
@@ -12,11 +27,7 @@ Rectangle Camera::get_bounds() {
|
||||
return Rectangle{minx, miny, width, height};
|
||||
}
|
||||
|
||||
void Camera::move_camera(Point new_center) {
|
||||
this->center = new_center;
|
||||
}
|
||||
|
||||
void Camera::set_bounds(int max_x, int max_y) {
|
||||
this->max_x = max_x;
|
||||
this->max_y = max_y;
|
||||
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};
|
||||
}
|
||||
@@ -6,8 +6,8 @@
|
||||
#define RLA_IIPP_CAMERA_HPP
|
||||
|
||||
|
||||
#include "../engine/utility/point.hpp"
|
||||
#include "../engine/utility/rectangle.hpp"
|
||||
#include "point.hpp"
|
||||
#include "rectangle.hpp"
|
||||
#include <algorithm>
|
||||
|
||||
class Camera {
|
||||
@@ -18,10 +18,13 @@ private:
|
||||
int tiles_rendered_y=0;
|
||||
int max_x=0;
|
||||
int max_y=0;
|
||||
Rectangle cached_bounds=Rectangle();
|
||||
Rectangle calculate_bounds();
|
||||
public:
|
||||
Rectangle get_bounds();
|
||||
void move_camera(Point new_center);
|
||||
void set_bounds(int max_x, int max_y);
|
||||
Point camera_coords_to_screen_coords(Point camera_coord);
|
||||
Camera()=default;
|
||||
|
||||
Camera(const Point ¢er, int tilesRenderedX, int tilesRenderedY, int maxX, int maxY) : center(center),
|
||||
@@ -30,7 +33,10 @@ public:
|
||||
tiles_rendered_y(
|
||||
tilesRenderedY),
|
||||
max_x(maxX),
|
||||
max_y(maxY) {}
|
||||
max_y(maxY)
|
||||
{
|
||||
cached_bounds = calculate_bounds();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
5
src/game/dungeonalgorithm.cpp
Normal file
5
src/game/dungeonalgorithm.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by m on 12/12/21.
|
||||
//
|
||||
|
||||
#include "dungeonalgorithm.hpp"
|
||||
14
src/game/dungeonalgorithm.hpp
Normal file
14
src/game/dungeonalgorithm.hpp
Normal file
@@ -0,0 +1,14 @@
|
||||
//
|
||||
// Created by m on 12/12/21.
|
||||
//
|
||||
|
||||
#ifndef RLA_IIPP_DUNGEONALGORITHM_HPP
|
||||
#define RLA_IIPP_DUNGEONALGORITHM_HPP
|
||||
|
||||
|
||||
class DungeonAlgorithm {
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //RLA_IIPP_DUNGEONALGORITHM_HPP
|
||||
@@ -30,16 +30,19 @@ 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=bounds.y;ty<bounds.y+bounds.height;ty++) {
|
||||
for(int tx=bounds.x;tx<bounds.x+bounds.width;tx++) {
|
||||
|
||||
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, (tx-bounds.x)*TILE_WIDTH, (ty-bounds.y)*TILE_HEIGHT);
|
||||
renderer->draw_sprite(grass, wx*TILE_WIDTH, wy*TILE_HEIGHT);
|
||||
}
|
||||
else {
|
||||
renderer->draw_sprite(wall, (tx-bounds.x)*TILE_WIDTH, (ty-bounds.y)*TILE_HEIGHT);
|
||||
renderer->draw_sprite(wall, wx*TILE_WIDTH, wy*TILE_HEIGHT);
|
||||
}
|
||||
if(tx == x && ty == y) {
|
||||
renderer->draw_sprite(character, (tx-bounds.x)*TILE_WIDTH, (ty-bounds.y)*TILE_HEIGHT);
|
||||
renderer->draw_sprite(character, wx*TILE_WIDTH, wy*TILE_HEIGHT);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -59,12 +62,7 @@ void Visualizer::initialize_map(int width, int height) {
|
||||
this->tile_map = Grid2D<char>(width, height);
|
||||
for(int ty=0;ty<this->tile_map.get_height();ty++) {
|
||||
for (int tx = 0; tx < tile_map.get_width(); tx++) {
|
||||
if (tx == 0 || ty == 0 || ty == this->tile_map.get_height() - 1 || tx == this->tile_map.get_width() - 1) {
|
||||
this->tile_map.insert(tx, ty, WALL_CHAR);
|
||||
}
|
||||
else {
|
||||
this->tile_map.insert(tx, ty, FLOOR_CHAR);
|
||||
}
|
||||
this->tile_map.insert(tx, ty, FLOOR_CHAR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,15 @@
|
||||
//
|
||||
#include "../engine/game/game.hpp"
|
||||
#include "../engine/rendering/renderer.hpp"
|
||||
#include "camera.hpp"
|
||||
#include "../engine/utility/camera.hpp"
|
||||
#include "../engine/utility/grid2d.hpp"
|
||||
#include <vector>
|
||||
#include <range/v3/range.hpp>
|
||||
#include <range/v3/view.hpp>
|
||||
#ifndef RLA_IIPP_VISUALIZER_HPP
|
||||
#define RLA_IIPP_VISUALIZER_HPP
|
||||
#define MAP_SIZE_W 10
|
||||
#define MAP_SIZE_H 10
|
||||
#define MAP_SIZE_W 20
|
||||
#define MAP_SIZE_H 20
|
||||
#define TILE_WIDTH 48
|
||||
#define TILE_HEIGHT 48
|
||||
#define WALL_CHAR '#'
|
||||
|
||||
Reference in New Issue
Block a user