Camera implemented
This commit is contained in:
@@ -7,7 +7,7 @@ conan_basic_setup() # Prepares the CMakeList.txt for Conan.
|
|||||||
set(CMAKE_CXX_STANDARD 20) # c++ 20
|
set(CMAKE_CXX_STANDARD 20) # c++ 20
|
||||||
|
|
||||||
# $source_files is a space-delimited list of filenames.
|
# $source_files is a space-delimited list of filenames.
|
||||||
add_executable(rla_iipp src/main.cpp src/engine/engine.hpp src/engine/game/game.hpp src/engine/rendering/renderer.hpp src/game/visualizer.hpp src/game/visualizer.cpp src/engine/rendering/sdl/sdlrenderer.cpp src/engine/rendering/sdl/sdlrenderer.hpp src/engine/rendering/sdl/sdlrenderer.cpp src/engine/rendering/sdl/sdlrenderer.hpp src/engine/engine.cpp src/engine/engine.hpp src/engine/rendering/sdl/sdltexturemanager.cpp src/engine/rendering/sdl/sdltexturemanager.hpp src/engine/resources/resourcemanager.hpp src/engine/rendering/sdl/sdlfontmanager.cpp src/engine/rendering/sdl/sdlfontmanager.hpp src/engine/input/inputprocessor.hpp src/engine/input/sdlinputprocessor.cpp src/engine/input/sdlinputprocessor.hpp) # Specifies the executable to build.
|
add_executable(rla_iipp src/main.cpp src/engine/engine.hpp src/engine/game/game.hpp src/engine/rendering/renderer.hpp src/game/visualizer.hpp src/game/visualizer.cpp src/engine/rendering/sdl/sdlrenderer.cpp src/engine/rendering/sdl/sdlrenderer.hpp src/engine/rendering/sdl/sdlrenderer.cpp src/engine/rendering/sdl/sdlrenderer.hpp src/engine/engine.cpp src/engine/engine.hpp src/engine/rendering/sdl/sdltexturemanager.cpp src/engine/rendering/sdl/sdltexturemanager.hpp src/engine/resources/resourcemanager.hpp src/engine/rendering/sdl/sdlfontmanager.cpp src/engine/rendering/sdl/sdlfontmanager.hpp src/engine/input/inputprocessor.hpp src/engine/input/sdlinputprocessor.cpp src/engine/input/sdlinputprocessor.hpp src/game/camera.cpp src/game/camera.hpp src/engine/utility/point.hpp src/engine/utility/rectangle.hpp src/engine/utility/grid2d.hpp) # Specifies the executable to build.
|
||||||
target_link_libraries(rla_iipp ${CONAN_LIBS}) # Specifies what libraries to link, using Conan.
|
target_link_libraries(rla_iipp ${CONAN_LIBS}) # Specifies what libraries to link, using Conan.
|
||||||
file(COPY assets DESTINATION ${CMAKE_BINARY_DIR}/bin)
|
file(COPY assets DESTINATION ${CMAKE_BINARY_DIR}/bin)
|
||||||
file(COPY config DESTINATION ${CMAKE_BINARY_DIR}/bin)
|
file(COPY config DESTINATION ${CMAKE_BINARY_DIR}/bin)
|
||||||
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"
|
#include "visualizer.hpp"
|
||||||
#define TILE_WIDTH 48
|
#define TILE_WIDTH 48
|
||||||
#define TILE_HEIGHT 48
|
#define TILE_HEIGHT 48
|
||||||
|
#define WALL_CHAR '#'
|
||||||
|
#define FLOOR_CHAR '.'
|
||||||
bool Visualizer::update(InputResult input) {
|
bool Visualizer::update(InputResult input) {
|
||||||
if(input.keycode == KEY_ESCAPE) {
|
if(input.keycode == KEY_ESCAPE) {
|
||||||
return true;
|
return true;
|
||||||
@@ -17,33 +19,34 @@ bool Visualizer::update(InputResult input) {
|
|||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->x = std::clamp(this->x, 0, (int)this->tile_map[0].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.size()-1);
|
this->y = std::clamp(this->y, 0, (int)this->tile_map.get_height()-1);
|
||||||
|
this->camera.move_camera(Point{x,y});
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Visualizer::render(Renderer *renderer) {
|
void Visualizer::render(Renderer *renderer) {
|
||||||
Color color = COLOR_WHITE;
|
|
||||||
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};
|
||||||
auto grass = Sprite{&tiles, 0};
|
auto grass = Sprite{&tiles, 0};
|
||||||
auto wall = Sprite{&tiles, 497};
|
auto wall = Sprite{&tiles, 497};
|
||||||
|
auto character = Sprite{&characters, this->sprite_index};
|
||||||
for(int ty=0;ty<this->tile_map.size();ty++) {
|
auto bounds = this->camera.get_bounds();
|
||||||
for(int tx=0;tx<tile_map[0].size();tx++) {
|
for(int ty=bounds.y;ty<bounds.y+bounds.height;ty++) {
|
||||||
if(tx == 0 || ty == 0 || ty == this->tile_map.size()-1 || tx == this->tile_map[0].size()-1) {
|
for(int tx=bounds.x;tx<bounds.x+bounds.width;tx++) {
|
||||||
renderer->draw_sprite(wall, tx*TILE_WIDTH, ty*TILE_HEIGHT);
|
if(this->tile_map.get(tx, ty) == FLOOR_CHAR) {
|
||||||
|
renderer->draw_sprite(grass, (tx-bounds.x)*TILE_WIDTH, (ty-bounds.y)*TILE_HEIGHT);
|
||||||
}
|
}
|
||||||
else {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Visualizer::initialize(GameInitArgs args) {
|
void Visualizer::initialize(GameInitArgs args) {
|
||||||
@@ -51,7 +54,22 @@ void Visualizer::initialize(GameInitArgs args) {
|
|||||||
this->window_height = args.window_height;
|
this->window_height = args.window_height;
|
||||||
int tilesx = (window_width / TILE_WIDTH)+1;
|
int tilesx = (window_width / TILE_WIDTH)+1;
|
||||||
int tilesy = (window_height / TILE_HEIGHT)+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;
|
Visualizer::~Visualizer() = default;
|
||||||
|
|||||||
@@ -3,13 +3,13 @@
|
|||||||
//
|
//
|
||||||
#include "../engine/game/game.hpp"
|
#include "../engine/game/game.hpp"
|
||||||
#include "../engine/rendering/renderer.hpp"
|
#include "../engine/rendering/renderer.hpp"
|
||||||
|
#include "camera.hpp"
|
||||||
|
#include "../engine/utility/grid2d.hpp"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#ifndef RLA_IIPP_VISUALIZER_HPP
|
#ifndef RLA_IIPP_VISUALIZER_HPP
|
||||||
#define RLA_IIPP_VISUALIZER_HPP
|
#define RLA_IIPP_VISUALIZER_HPP
|
||||||
#define TITLE "test"
|
#define MAP_SIZE_W 20
|
||||||
#define WIN_WIDTH 800
|
#define MAP_SIZE_H 20
|
||||||
#define WIN_HEIGHT 600
|
|
||||||
#define TARGET_FPS 61
|
|
||||||
|
|
||||||
class Visualizer : public Game {
|
class Visualizer : public Game {
|
||||||
public:
|
public:
|
||||||
@@ -22,15 +22,19 @@ public:
|
|||||||
|
|
||||||
void initialize(GameInitArgs args) override;
|
void initialize(GameInitArgs args) override;
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int window_width = 0;
|
int window_width = 0;
|
||||||
int window_height = 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 x = 0;
|
||||||
int y=0;
|
int y=0;
|
||||||
int sprite_index = 1;
|
int sprite_index = 1;
|
||||||
|
|
||||||
|
void initialize_map(int width, int height);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //RLA_IIPP_VISUALIZER_HPP
|
#endif //RLA_IIPP_VISUALIZER_HPP
|
||||||
|
|||||||
Reference in New Issue
Block a user