From 2308ef4eb213cb411587a7873dc7af4d7f628fa9 Mon Sep 17 00:00:00 2001 From: littlefoot Date: Sat, 11 Dec 2021 21:32:43 -0500 Subject: [PATCH] Camera implemented --- CMakeLists.txt | 2 +- src/engine/utility/grid2d.hpp | 34 ++++++++++++++++++++++ src/engine/utility/point.hpp | 16 ++++++++++ src/engine/utility/rectangle.hpp | 17 +++++++++++ src/game/camera.cpp | 22 ++++++++++++++ src/game/camera.hpp | 37 +++++++++++++++++++++++ src/game/visualizer.cpp | 50 ++++++++++++++++++++++---------- src/game/visualizer.hpp | 14 +++++---- 8 files changed, 170 insertions(+), 22 deletions(-) create mode 100644 src/engine/utility/grid2d.hpp create mode 100644 src/engine/utility/point.hpp create mode 100644 src/engine/utility/rectangle.hpp create mode 100644 src/game/camera.cpp create mode 100644 src/game/camera.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index cef5608..33b4cb4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ conan_basic_setup() # Prepares the CMakeList.txt for Conan. set(CMAKE_CXX_STANDARD 20) # c++ 20 # $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. file(COPY assets DESTINATION ${CMAKE_BINARY_DIR}/bin) file(COPY config DESTINATION ${CMAKE_BINARY_DIR}/bin) \ No newline at end of file diff --git a/src/engine/utility/grid2d.hpp b/src/engine/utility/grid2d.hpp new file mode 100644 index 0000000..dcd6f65 --- /dev/null +++ b/src/engine/utility/grid2d.hpp @@ -0,0 +1,34 @@ +// +// Created by m on 12/11/21. +// + +#ifndef RLA_IIPP_GRID2D_HPP +#define RLA_IIPP_GRID2D_HPP + + +#include + +template +class Grid2D { +private: + std::vector> grid{}; +public: + Grid2D(int width, int height) { + grid = std::vector>(height, std::vector(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 diff --git a/src/engine/utility/point.hpp b/src/engine/utility/point.hpp new file mode 100644 index 0000000..dc00d01 --- /dev/null +++ b/src/engine/utility/point.hpp @@ -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 diff --git a/src/engine/utility/rectangle.hpp b/src/engine/utility/rectangle.hpp new file mode 100644 index 0000000..3789d22 --- /dev/null +++ b/src/engine/utility/rectangle.hpp @@ -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 diff --git a/src/game/camera.cpp b/src/game/camera.cpp new file mode 100644 index 0000000..8489441 --- /dev/null +++ b/src/game/camera.cpp @@ -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; +} diff --git a/src/game/camera.hpp b/src/game/camera.hpp new file mode 100644 index 0000000..6453e9d --- /dev/null +++ b/src/game/camera.hpp @@ -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 + +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 diff --git a/src/game/visualizer.cpp b/src/game/visualizer.cpp index 4cfbe6d..1daf243 100644 --- a/src/game/visualizer.cpp +++ b/src/game/visualizer.cpp @@ -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;tytile_map.size();ty++) { - for(int tx=0;txtile_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;tytile_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>(tilesy, std::vector(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(width, height); + for(int ty=0;tytile_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; diff --git a/src/game/visualizer.hpp b/src/game/visualizer.hpp index e175ba7..30d65d8 100644 --- a/src/game/visualizer.hpp +++ b/src/game/visualizer.hpp @@ -3,13 +3,13 @@ // #include "../engine/game/game.hpp" #include "../engine/rendering/renderer.hpp" +#include "camera.hpp" +#include "../engine/utility/grid2d.hpp" #include #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> tile_map; + Grid2D tile_map = Grid2D(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