Camera implemented
This commit is contained in:
34
src/engine/utility/grid2d.hpp
Normal file
34
src/engine/utility/grid2d.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// Created by m on 12/11/21.
|
||||
//
|
||||
|
||||
#ifndef RLA_IIPP_GRID2D_HPP
|
||||
#define RLA_IIPP_GRID2D_HPP
|
||||
|
||||
|
||||
#include <vector>
|
||||
|
||||
template<typename T>
|
||||
class Grid2D {
|
||||
private:
|
||||
std::vector<std::vector<T>> grid{};
|
||||
public:
|
||||
Grid2D(int width, int height) {
|
||||
grid = std::vector<std::vector<T>>(height, std::vector<T>(width));
|
||||
}
|
||||
int get_width() {
|
||||
return grid[0].size();
|
||||
}
|
||||
int get_height() {
|
||||
return grid.size();
|
||||
}
|
||||
void insert(int x, int y, T arg) {
|
||||
grid[y][x] = arg;
|
||||
}
|
||||
T get(int x, int y) {
|
||||
return grid[y][x];
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //RLA_IIPP_GRID2D_HPP
|
||||
16
src/engine/utility/point.hpp
Normal file
16
src/engine/utility/point.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// Created by m on 12/11/21.
|
||||
//
|
||||
|
||||
#ifndef RLA_IIPP_POINT_HPP
|
||||
#define RLA_IIPP_POINT_HPP
|
||||
|
||||
|
||||
struct Point {
|
||||
int x;
|
||||
int y;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //RLA_IIPP_POINT_HPP
|
||||
17
src/engine/utility/rectangle.hpp
Normal file
17
src/engine/utility/rectangle.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// Created by m on 12/11/21.
|
||||
//
|
||||
|
||||
#ifndef RLA_IIPP_RECTANGLE_HPP
|
||||
#define RLA_IIPP_RECTANGLE_HPP
|
||||
|
||||
|
||||
struct Rectangle {
|
||||
int x;
|
||||
int y;
|
||||
int width;
|
||||
int height;
|
||||
};
|
||||
|
||||
|
||||
#endif //RLA_IIPP_RECTANGLE_HPP
|
||||
22
src/game/camera.cpp
Normal file
22
src/game/camera.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// Created by m on 12/11/21.
|
||||
//
|
||||
|
||||
#include "camera.hpp"
|
||||
|
||||
Rectangle Camera::get_bounds() {
|
||||
int minx = std::clamp(center.x-(tiles_rendered_x/2), 0, max_x - tiles_rendered_x);
|
||||
int miny = std::clamp(center.y-(tiles_rendered_y/2), 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};
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
37
src/game/camera.hpp
Normal file
37
src/game/camera.hpp
Normal file
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// Created by m on 12/11/21.
|
||||
//
|
||||
|
||||
#ifndef RLA_IIPP_CAMERA_HPP
|
||||
#define RLA_IIPP_CAMERA_HPP
|
||||
|
||||
|
||||
#include "../engine/utility/point.hpp"
|
||||
#include "../engine/utility/rectangle.hpp"
|
||||
#include <algorithm>
|
||||
|
||||
class Camera {
|
||||
private:
|
||||
// center of the camera
|
||||
Point center=Point();
|
||||
int tiles_rendered_x=0;
|
||||
int tiles_rendered_y=0;
|
||||
int max_x=0;
|
||||
int max_y=0;
|
||||
public:
|
||||
Rectangle get_bounds();
|
||||
void move_camera(Point new_center);
|
||||
void set_bounds(int max_x, int max_y);
|
||||
Camera()=default;
|
||||
|
||||
Camera(const Point ¢er, int tilesRenderedX, int tilesRenderedY, int maxX, int maxY) : center(center),
|
||||
tiles_rendered_x(
|
||||
tilesRenderedX),
|
||||
tiles_rendered_y(
|
||||
tilesRenderedY),
|
||||
max_x(maxX),
|
||||
max_y(maxY) {}
|
||||
};
|
||||
|
||||
|
||||
#endif //RLA_IIPP_CAMERA_HPP
|
||||
@@ -5,6 +5,8 @@
|
||||
#include "visualizer.hpp"
|
||||
#define TILE_WIDTH 48
|
||||
#define TILE_HEIGHT 48
|
||||
#define WALL_CHAR '#'
|
||||
#define FLOOR_CHAR '.'
|
||||
bool Visualizer::update(InputResult input) {
|
||||
if(input.keycode == KEY_ESCAPE) {
|
||||
return true;
|
||||
@@ -17,33 +19,34 @@ bool Visualizer::update(InputResult input) {
|
||||
default: break;
|
||||
}
|
||||
|
||||
this->x = std::clamp(this->x, 0, (int)this->tile_map[0].size()-1);
|
||||
this->y = std::clamp(this->y, 0, (int)this->tile_map.size()-1);
|
||||
this->x = std::clamp(this->x, 0, (int)this->tile_map.get_width()-1);
|
||||
this->y = std::clamp(this->y, 0, (int)this->tile_map.get_height()-1);
|
||||
this->camera.move_camera(Point{x,y});
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void Visualizer::render(Renderer *renderer) {
|
||||
Color color = COLOR_WHITE;
|
||||
SpriteSheet tiles = SpriteSheet{"sprites/map1.bmp", TILE_WIDTH, TILE_HEIGHT};
|
||||
SpriteSheet characters = SpriteSheet{"sprites/character.bmp", TILE_WIDTH, TILE_HEIGHT, COLOR_BLACK};
|
||||
auto grass = Sprite{&tiles, 0};
|
||||
auto wall = Sprite{&tiles, 497};
|
||||
|
||||
for(int ty=0;ty<this->tile_map.size();ty++) {
|
||||
for(int tx=0;tx<tile_map[0].size();tx++) {
|
||||
if(tx == 0 || ty == 0 || ty == this->tile_map.size()-1 || tx == this->tile_map[0].size()-1) {
|
||||
renderer->draw_sprite(wall, tx*TILE_WIDTH, ty*TILE_HEIGHT);
|
||||
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++) {
|
||||
if(this->tile_map.get(tx, ty) == FLOOR_CHAR) {
|
||||
renderer->draw_sprite(grass, (tx-bounds.x)*TILE_WIDTH, (ty-bounds.y)*TILE_HEIGHT);
|
||||
}
|
||||
else {
|
||||
renderer->draw_sprite(grass, tx*TILE_WIDTH, ty*TILE_HEIGHT);
|
||||
renderer->draw_sprite(wall, (tx-bounds.x)*TILE_WIDTH, (ty-bounds.y)*TILE_HEIGHT);
|
||||
}
|
||||
if(tx == x && ty == y) {
|
||||
renderer->draw_sprite(character, (tx-bounds.x)*TILE_WIDTH, (ty-bounds.y)*TILE_HEIGHT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
SpriteSheet characters = SpriteSheet{"sprites/character.bmp", TILE_WIDTH, TILE_HEIGHT, COLOR_BLACK};
|
||||
auto character = Sprite{&characters, this->sprite_index};
|
||||
|
||||
renderer->draw_sprite(character, x*TILE_WIDTH, y*TILE_HEIGHT);
|
||||
return;
|
||||
}
|
||||
|
||||
void Visualizer::initialize(GameInitArgs args) {
|
||||
@@ -51,7 +54,22 @@ void Visualizer::initialize(GameInitArgs args) {
|
||||
this->window_height = args.window_height;
|
||||
int tilesx = (window_width / TILE_WIDTH)+1;
|
||||
int tilesy = (window_height / TILE_HEIGHT)+1;
|
||||
this->tile_map = std::vector<std::vector<char>>(tilesy, std::vector<char>(tilesx));
|
||||
initialize_map(MAP_SIZE_W, MAP_SIZE_H);
|
||||
this->camera = Camera(Point{x,y}, tilesx, tilesy, tile_map.get_width(), tile_map.get_height());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Visualizer::~Visualizer() = default;
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
//
|
||||
#include "../engine/game/game.hpp"
|
||||
#include "../engine/rendering/renderer.hpp"
|
||||
#include "camera.hpp"
|
||||
#include "../engine/utility/grid2d.hpp"
|
||||
#include <vector>
|
||||
#ifndef RLA_IIPP_VISUALIZER_HPP
|
||||
#define RLA_IIPP_VISUALIZER_HPP
|
||||
#define TITLE "test"
|
||||
#define WIN_WIDTH 800
|
||||
#define WIN_HEIGHT 600
|
||||
#define TARGET_FPS 61
|
||||
#define MAP_SIZE_W 20
|
||||
#define MAP_SIZE_H 20
|
||||
|
||||
class Visualizer : public Game {
|
||||
public:
|
||||
@@ -22,15 +22,19 @@ public:
|
||||
|
||||
void initialize(GameInitArgs args) override;
|
||||
|
||||
|
||||
private:
|
||||
int window_width = 0;
|
||||
int window_height = 0;
|
||||
std::vector<std::vector<char>> tile_map;
|
||||
Grid2D<char> tile_map = Grid2D<char>(0, 0);
|
||||
Camera camera = Camera();
|
||||
|
||||
int x = 0;
|
||||
int y=0;
|
||||
int sprite_index = 1;
|
||||
|
||||
void initialize_map(int width, int height);
|
||||
|
||||
};
|
||||
|
||||
#endif //RLA_IIPP_VISUALIZER_HPP
|
||||
|
||||
Reference in New Issue
Block a user