From 3eb9505bdc48355cfa93a55c28a10c148978da84 Mon Sep 17 00:00:00 2001 From: Bilal Kahraman Date: Mon, 12 Aug 2024 23:52:59 +0300 Subject: [PATCH 1/7] Update camera in main loop --- src/Core/src/game.cpp | 1 + src/Graphics/src/renderer.cpp | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Core/src/game.cpp b/src/Core/src/game.cpp index bd9904a..5179526 100644 --- a/src/Core/src/game.cpp +++ b/src/Core/src/game.cpp @@ -101,6 +101,7 @@ void Game::Run() { CheckEvent(); time_manager_->CalculateDeltaTime(); scene_->Update(time_manager_->GetDeltaTime()); + camera_->Update(map_); switch (render_type_) { case RenderType::TEXTURE: renderer_->RenderScene(scene_, camera_); diff --git a/src/Graphics/src/renderer.cpp b/src/Graphics/src/renderer.cpp index 92014ac..bd13c7e 100644 --- a/src/Graphics/src/renderer.cpp +++ b/src/Graphics/src/renderer.cpp @@ -91,7 +91,6 @@ void Renderer::RenderBackground() { void Renderer::RenderWalls(const std::shared_ptr& map_ptr, const std::shared_ptr& camera_ptr, RenderQueue& render_queue) { - camera_ptr->Update(map_ptr); const auto rays = camera_ptr->GetRays(); int horizontal_slice = 0; @@ -219,7 +218,6 @@ void Renderer::ClearScreen() { void Renderer::RenderScene2D(const std::shared_ptr& scene_ptr, const std::shared_ptr& camera_ptr) { - camera_ptr->Update(scene_ptr->GetMap()); ClearScreen(); RenderMap(scene_ptr->GetMap()); RenderObjects(scene_ptr->GetObjects(), camera_ptr); From 95ef69e721114dea52ed82191a534d42a8b302b4 Mon Sep 17 00:00:00 2001 From: Bilal Kahraman Date: Wed, 14 Aug 2024 01:19:45 +0300 Subject: [PATCH 2/7] Change update function and take scene --- src/Camera/CMakeLists.txt | 1 + src/Camera/include/Camera/camera.h | 3 ++- src/Camera/src/camera.cpp | 6 ++++-- src/Core/src/game.cpp | 2 +- src/Graphics/src/renderer.cpp | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Camera/CMakeLists.txt b/src/Camera/CMakeLists.txt index 43da228..1277a2c 100644 --- a/src/Camera/CMakeLists.txt +++ b/src/Camera/CMakeLists.txt @@ -45,4 +45,5 @@ target_link_libraries( character vector map + scene ) diff --git a/src/Camera/include/Camera/camera.h b/src/Camera/include/Camera/camera.h index a773f35..fc24110 100644 --- a/src/Camera/include/Camera/camera.h +++ b/src/Camera/include/Camera/camera.h @@ -13,6 +13,7 @@ #define CAMERA_CAMERA_H #include "Camera/raycaster.h" +#include "Graphics/scene.h" #include #include #include @@ -35,7 +36,7 @@ class Camera2D explicit Camera2D(const Camera2DConfig& config); ~Camera2D() = default; - void Update(const std::shared_ptr& map); + void Update(const std::shared_ptr& scene); std::shared_ptr GetRays() const; std::shared_ptr GetCrosshairRay() const; diff --git a/src/Camera/src/camera.cpp b/src/Camera/src/camera.cpp index c6670f6..8edd41f 100644 --- a/src/Camera/src/camera.cpp +++ b/src/Camera/src/camera.cpp @@ -22,9 +22,11 @@ Camera2D::Camera2D(const Camera2DConfig& config) InitRays(); } -void Camera2D::Update(const std::shared_ptr& map_ptr) { - ray_cast_->Update(map_ptr, position_, rays_); +void Camera2D::Update(const std::shared_ptr& scene) { + ray_cast_->Update(scene->GetMap(), position_, rays_); crosshair_ray_ = std::make_shared(rays_->at(config_.width / 4)); + + // Update object rays } std::shared_ptr Camera2D::GetRays() const { diff --git a/src/Core/src/game.cpp b/src/Core/src/game.cpp index 5179526..a3ebffb 100644 --- a/src/Core/src/game.cpp +++ b/src/Core/src/game.cpp @@ -101,7 +101,7 @@ void Game::Run() { CheckEvent(); time_manager_->CalculateDeltaTime(); scene_->Update(time_manager_->GetDeltaTime()); - camera_->Update(map_); + camera_->Update(scene_); switch (render_type_) { case RenderType::TEXTURE: renderer_->RenderScene(scene_, camera_); diff --git a/src/Graphics/src/renderer.cpp b/src/Graphics/src/renderer.cpp index bd13c7e..e3ca4b7 100644 --- a/src/Graphics/src/renderer.cpp +++ b/src/Graphics/src/renderer.cpp @@ -236,7 +236,7 @@ void Renderer::RenderObjects( SetDrawColor({00, 0xA5, 0, 1}); const auto rays = *camera_ptr->GetRays(); - for (int i = 0; i < rays.size(); i++) { + for (unsigned int i = 0; i < rays.size(); i++) { if (!rays[i].is_hit || i % 3 != 0) { continue; } From 1819c53b3403cbdd860c2b2a9983b87835a8e0f5 Mon Sep 17 00:00:00 2001 From: Bilal Kahraman Date: Thu, 15 Aug 2024 23:49:16 +0300 Subject: [PATCH 3/7] Add uuid external library, create uuid_generator and update utility --- .gitmodules | 3 ++ CMakeLists.txt | 11 ++++-- src/Utilities/src/logger.cpp | 1 - src/{Utilities => Utility}/CMakeLists.txt | 17 +++++++-- .../include/Utility}/logger.h | 0 src/Utility/include/Utility/uuid_generator.h | 37 +++++++++++++++++++ src/Utility/src/logger.cpp | 1 + src/Utility/src/uuid_generator.cpp | 16 ++++++++ third-party/uuid_v4 | 1 + 9 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 .gitmodules delete mode 100644 src/Utilities/src/logger.cpp rename src/{Utilities => Utility}/CMakeLists.txt (60%) rename src/{Utilities/include/Utilities => Utility/include/Utility}/logger.h (100%) create mode 100644 src/Utility/include/Utility/uuid_generator.h create mode 100644 src/Utility/src/logger.cpp create mode 100644 src/Utility/src/uuid_generator.cpp create mode 160000 third-party/uuid_v4 diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..f51dc77 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "third-party/uuid_v4"] + path = third-party/uuid_v4 + url = git@github.com:crashoz/uuid_v4.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 9839fcf..ea5d35e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ add_definitions( set(CMAKE_BUILD_TYPE Debug) # set(CMAKE_BUILD_TYPE Release) +# internal libraries add_subdirectory(app) add_subdirectory(src/Camera) add_subdirectory(src/CollisionManager) @@ -27,22 +28,24 @@ add_subdirectory(src/Map) add_subdirectory(src/Math) add_subdirectory(src/TextureManager) add_subdirectory(src/TimeManager) -add_subdirectory(src/Utilities) +add_subdirectory(src/Utility) + +# external libraries +add_subdirectory(third-party/uuid_v4) find_package(SDL2 REQUIRED) include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${PROJECT_SOURCE_DIR} - ${PROJECT_SOURCE_DIR}/include + ${PROJECT_SOURCE_DIR}/third-party ${SDL2_INCLUDE_DIRS} ) link_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${PROJECT_SOURCE_DIR} - ${PROJECT_SOURCE_DIR}/include - ${SDL2_INCLUDE_DIRS} + ${PROJECT_SOURCE_DIR}/third-party ) set(LIBRARIES ${SDL2_LIBRARIES} diff --git a/src/Utilities/src/logger.cpp b/src/Utilities/src/logger.cpp deleted file mode 100644 index 2e7db3c..0000000 --- a/src/Utilities/src/logger.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "Utilities/logger.h" \ No newline at end of file diff --git a/src/Utilities/CMakeLists.txt b/src/Utility/CMakeLists.txt similarity index 60% rename from src/Utilities/CMakeLists.txt rename to src/Utility/CMakeLists.txt index 22f553d..33efc64 100644 --- a/src/Utilities/CMakeLists.txt +++ b/src/Utility/CMakeLists.txt @@ -3,16 +3,25 @@ link_directories( ${CMAKE_CURRENT_SOURCE_DIR}/include ) +include_directories( + ${CMAKE_CURRENT_SOURCE_DIR}/include +) + add_library(utilities src/logger.cpp ) -include_directories( +# Specify include directories for this library +target_include_directories(utilities PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ) +add_library(uuid_generator + src/uuid_generator.cpp +) + # Specify include directories for this library -target_include_directories(utilities PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR} +target_include_directories(uuid_generator PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include -) + ${PROJECT_SOURCE_DIR} +) \ No newline at end of file diff --git a/src/Utilities/include/Utilities/logger.h b/src/Utility/include/Utility/logger.h similarity index 100% rename from src/Utilities/include/Utilities/logger.h rename to src/Utility/include/Utility/logger.h diff --git a/src/Utility/include/Utility/uuid_generator.h b/src/Utility/include/Utility/uuid_generator.h new file mode 100644 index 0000000..e668ddb --- /dev/null +++ b/src/Utility/include/Utility/uuid_generator.h @@ -0,0 +1,37 @@ +/** + * @file uuid_generator.h + * @author Bilal Kahraman (kahramannbilal@gmail.com) + * @brief + * @version 0.1 + * @date 2024-08-15 + * + * @copyright Copyright (c) 2024 + * + */ + +#ifndef UTILITY_UUID_GENERATOR_H +#define UTILITY_UUID_GENERATOR_H + +#include + +namespace wolfenstein { + +class UuidGenerator +{ + public: + static UuidGenerator& GetInstance(); + UuidGenerator(const UuidGenerator&) = delete; + UuidGenerator& operator=(const UuidGenerator&) = delete; + ~UuidGenerator() = default; + + UUIDv4::UUID GenerateUuid(); + + private: + UuidGenerator(); + UUIDv4::UUIDGenerator generator_; + static UuidGenerator* instance_; +}; + +} // namespace wolfenstein + +#endif // UTILITY_UUID_GENERATOR_H \ No newline at end of file diff --git a/src/Utility/src/logger.cpp b/src/Utility/src/logger.cpp new file mode 100644 index 0000000..8fbc7dc --- /dev/null +++ b/src/Utility/src/logger.cpp @@ -0,0 +1 @@ +#include "Utility/logger.h" \ No newline at end of file diff --git a/src/Utility/src/uuid_generator.cpp b/src/Utility/src/uuid_generator.cpp new file mode 100644 index 0000000..c5f1624 --- /dev/null +++ b/src/Utility/src/uuid_generator.cpp @@ -0,0 +1,16 @@ +#include "Utility/uuid_generator.h" + +namespace wolfenstein { + +UuidGenerator& UuidGenerator::GetInstance() { + static UuidGenerator instance; + return instance; +} + +UuidGenerator::UuidGenerator() : generator_(13U) {} + +UUIDv4::UUID UuidGenerator::GenerateUuid() { + return generator_.getUUID(); +} + +} // namespace wolfenstein diff --git a/third-party/uuid_v4 b/third-party/uuid_v4 new file mode 160000 index 0000000..8fffbb1 --- /dev/null +++ b/third-party/uuid_v4 @@ -0,0 +1 @@ +Subproject commit 8fffbb16be1d884c01fcaa816008065a6e2329a6 From c1796aac6b8f3e7e90da255be0e688b75e70aee3 Mon Sep 17 00:00:00 2001 From: Bilal Kahraman Date: Thu, 15 Aug 2024 23:55:39 +0300 Subject: [PATCH 4/7] Add uuid interface to game objects --- src/Characters/include/Characters/enemy.h | 4 ++++ src/Characters/include/Characters/player.h | 3 +++ src/Characters/src/enemy.cpp | 8 ++++++++ src/Characters/src/player.cpp | 17 +++++++++++++++-- src/GameObjects/CMakeLists.txt | 3 +++ .../include/GameObjects/dynamic_object.h | 3 +++ .../include/GameObjects/game_object.h | 2 ++ .../include/GameObjects/static_object.h | 4 ++++ src/GameObjects/src/dynamic_object.cpp | 8 +++++++- src/GameObjects/src/static_object.cpp | 9 ++++++++- 10 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/Characters/include/Characters/enemy.h b/src/Characters/include/Characters/enemy.h index a3dacb2..20160b6 100644 --- a/src/Characters/include/Characters/enemy.h +++ b/src/Characters/include/Characters/enemy.h @@ -13,12 +13,14 @@ #define CHARACTERS_ENEMY_H_ #include "character.h" +#include namespace wolfenstein { class Enemy : public ICharacter, public IGameObject { public: + explicit Enemy(); ~Enemy() = default; void Update(double delta_time) override; @@ -28,11 +30,13 @@ class Enemy : public ICharacter, public IGameObject vector2d GetPose() const override; void SetPosition(Position2D position) override; Position2D GetPosition() const override; + std::string GetId() const override; private: void Move(double delta_time); Position2D position_; + std::string id_; }; } // namespace wolfenstein diff --git a/src/Characters/include/Characters/player.h b/src/Characters/include/Characters/player.h index db3340c..aa171ee 100644 --- a/src/Characters/include/Characters/player.h +++ b/src/Characters/include/Characters/player.h @@ -13,6 +13,7 @@ #define CHARACTERS_PLAYER_H #include "character.h" +#include #include namespace wolfenstein { @@ -31,6 +32,7 @@ class Player : public ICharacter, public IGameObject vector2d GetPose() const override; void SetPosition(Position2D position) override; Position2D GetPosition() const override; + std::string GetId() const override; void SetCameraPositionUpdator(std::function updator); @@ -41,6 +43,7 @@ class Player : public ICharacter, public IGameObject Position2D position_; double rotation_speed_; double translation_speed_; + std::string id_; std::function camera_position_updator_; }; diff --git a/src/Characters/src/enemy.cpp b/src/Characters/src/enemy.cpp index 57f4f60..fe9567b 100644 --- a/src/Characters/src/enemy.cpp +++ b/src/Characters/src/enemy.cpp @@ -2,6 +2,10 @@ namespace wolfenstein { +Enemy::Enemy() { + id_ = UuidGenerator::GetInstance().GenerateUuid().bytes(); +} + void Enemy::Update(double delta_time) { Move(delta_time); } @@ -26,6 +30,10 @@ Position2D Enemy::GetPosition() const { return position_; } +std::string Enemy::GetId() const { + return id_; +} + void Enemy::Move(double delta_time) { (void)delta_time; // Move enemy diff --git a/src/Characters/src/player.cpp b/src/Characters/src/player.cpp index e40fe20..511e527 100644 --- a/src/Characters/src/player.cpp +++ b/src/Characters/src/player.cpp @@ -8,7 +8,9 @@ namespace wolfenstein { Player::Player(CharacterConfig& config) : position_(config.initial_position), rotation_speed_(config.rotation_speed), - translation_speed_(config.translation_speed) {} + translation_speed_(config.translation_speed) { + id_ = UuidGenerator::GetInstance().GenerateUuid().bytes(); +} void Player::Update(double delta_time) { Move(delta_time); @@ -36,6 +38,10 @@ Position2D Player::GetPosition() const { return position_; } +std::string Player::GetId() const { + return id_; +} + void Player::SetCameraPositionUpdator(std::function updator) { camera_position_updator_ = updator; } @@ -77,7 +83,14 @@ void Player::Rotate(double delta_time) { if (SDL_ShowCursor(SDL_QUERY) == SDL_DISABLE) { int x, y; SDL_GetMouseState(&x, &y); - position_.theta = SumRadian(position_.theta, (x - 400) * 0.001); + position_.theta = SumRadian(position_.theta, + (x - 400) * rotation_speed_ * delta_time); + if (position_.theta > M_PI) { + position_.theta -= 2 * M_PI; + } + else if (position_.theta < -M_PI) { + position_.theta += 2 * M_PI; + } SDL_WarpMouseInWindow(nullptr, 400, 300); } } diff --git a/src/GameObjects/CMakeLists.txt b/src/GameObjects/CMakeLists.txt index d11d7a3..c3f4b50 100644 --- a/src/GameObjects/CMakeLists.txt +++ b/src/GameObjects/CMakeLists.txt @@ -1,6 +1,7 @@ link_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/include + ${PROJECT_SOURCE_DIR} ) # Game object library @@ -15,6 +16,7 @@ add_library( target_include_directories(game_object PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/include + ${PROJECT_SOURCE_DIR} ) target_link_libraries( @@ -22,4 +24,5 @@ target_link_libraries( PUBLIC vector character + uuid_generator ) diff --git a/src/GameObjects/include/GameObjects/dynamic_object.h b/src/GameObjects/include/GameObjects/dynamic_object.h index bdfc4a9..00fa89a 100644 --- a/src/GameObjects/include/GameObjects/dynamic_object.h +++ b/src/GameObjects/include/GameObjects/dynamic_object.h @@ -13,6 +13,7 @@ #define GAME_OBJECTS_DYNAMIC_OBJECT_H #include "GameObjects/game_object.h" +#include namespace wolfenstein { @@ -27,11 +28,13 @@ class DynamicObject : public IGameObject void SetPose(const vector2d& pose) override; ObjectType GetObjectType() const override; vector2d GetPose() const override; + std::string GetId() const override; protected: // Dynamic object specific data vector2d pose; std::string texture_path; + std::string id; }; } // namespace wolfenstein diff --git a/src/GameObjects/include/GameObjects/game_object.h b/src/GameObjects/include/GameObjects/game_object.h index c0803c7..f0ace64 100644 --- a/src/GameObjects/include/GameObjects/game_object.h +++ b/src/GameObjects/include/GameObjects/game_object.h @@ -13,6 +13,7 @@ #define GAME_OBJECTS_GAME_OBJECT_H #include +#include namespace wolfenstein { @@ -33,6 +34,7 @@ class IGameObject virtual void SetPose(const vector2d& pose) = 0; virtual ObjectType GetObjectType() const = 0; virtual vector2d GetPose() const = 0; + virtual std::string GetId() const = 0; }; } // namespace wolfenstein diff --git a/src/GameObjects/include/GameObjects/static_object.h b/src/GameObjects/include/GameObjects/static_object.h index b6fb279..8adb9d9 100644 --- a/src/GameObjects/include/GameObjects/static_object.h +++ b/src/GameObjects/include/GameObjects/static_object.h @@ -14,7 +14,9 @@ #include "GameObjects/game_object.h" #include "Math/vector.h" +#include "third-party/uuid_v4/uuid_v4.h" #include +#include namespace wolfenstein { @@ -32,6 +34,7 @@ class StaticObject : public IGameObject ObjectType GetObjectType() const override; vector2d GetPose() const override; + std::string GetId() const override; int GetTextureId() const; double GetWidth() const; @@ -42,6 +45,7 @@ class StaticObject : public IGameObject int texture_id; double width; double height; + std::string id; }; } // namespace wolfenstein diff --git a/src/GameObjects/src/dynamic_object.cpp b/src/GameObjects/src/dynamic_object.cpp index 388a955..553e1a4 100644 --- a/src/GameObjects/src/dynamic_object.cpp +++ b/src/GameObjects/src/dynamic_object.cpp @@ -3,7 +3,9 @@ namespace wolfenstein { DynamicObject::DynamicObject(const vector2d& pose_, std::string texture_path_) - : pose(pose_), texture_path(texture_path_) {} + : pose(pose_), texture_path(texture_path_) { + id = UuidGenerator::GetInstance().GenerateUuid().bytes(); +} DynamicObject::~DynamicObject() {} @@ -24,4 +26,8 @@ vector2d DynamicObject::GetPose() const { return pose; } +std::string DynamicObject::GetId() const { + return id; +} + } // namespace wolfenstein diff --git a/src/GameObjects/src/static_object.cpp b/src/GameObjects/src/static_object.cpp index 981b482..6075336 100644 --- a/src/GameObjects/src/static_object.cpp +++ b/src/GameObjects/src/static_object.cpp @@ -5,7 +5,9 @@ namespace wolfenstein { StaticObject::StaticObject(const vector2d& pose_, const int texture_id_, const double width_, const double height_) - : pose(pose_), texture_id(texture_id_), width(width_), height(height_) {} + : pose(pose_), texture_id(texture_id_), width(width_), height(height_) { + id = UuidGenerator::GetInstance().GenerateUuid().bytes(); +} StaticObject::~StaticObject() {} @@ -33,6 +35,11 @@ vector2d StaticObject::GetPose() const { int StaticObject::GetTextureId() const { return texture_id; } + +std::string StaticObject::GetId() const { + return id; +} + double StaticObject::GetWidth() const { return width; } From 6f4a0401d2f12fa1825e1a1abea567ad49b03a30 Mon Sep 17 00:00:00 2001 From: Bilal Kahraman Date: Fri, 16 Aug 2024 00:01:23 +0300 Subject: [PATCH 5/7] Update camera object ray calculation, use only right and left edge angles --- src/Camera/CMakeLists.txt | 4 + src/Camera/include/Camera/camera.h | 11 +- src/Camera/src/camera.cpp | 126 ++++++++++++++++------- src/Core/CMakeLists.txt | 1 - src/Core/src/game.cpp | 4 +- src/Graphics/include/Graphics/renderer.h | 2 +- src/Graphics/src/renderer.cpp | 86 ++++++---------- src/Math/include/Math/vector.h | 1 + src/Math/src/vector.cpp | 4 + 9 files changed, 146 insertions(+), 93 deletions(-) diff --git a/src/Camera/CMakeLists.txt b/src/Camera/CMakeLists.txt index 1277a2c..e35aab0 100644 --- a/src/Camera/CMakeLists.txt +++ b/src/Camera/CMakeLists.txt @@ -1,6 +1,7 @@ link_directories( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/include + ${PROJECT_SOURCE_DIR} ) @@ -14,6 +15,7 @@ add_library( target_include_directories(ray PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/include + ${PROJECT_SOURCE_DIR} ) target_link_libraries( @@ -34,6 +36,7 @@ add_library( target_include_directories(camera PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/include + ${PROJECT_SOURCE_DIR} ) @@ -46,4 +49,5 @@ target_link_libraries( vector map scene + texture_manager ) diff --git a/src/Camera/include/Camera/camera.h b/src/Camera/include/Camera/camera.h index fc24110..068d59e 100644 --- a/src/Camera/include/Camera/camera.h +++ b/src/Camera/include/Camera/camera.h @@ -20,6 +20,9 @@ #include #include #include +#include +#include +#include namespace wolfenstein { @@ -30,6 +33,8 @@ struct Camera2DConfig double depth; }; +typedef std::pair RayPair; + class Camera2D { public: @@ -39,6 +44,7 @@ class Camera2D void Update(const std::shared_ptr& scene); std::shared_ptr GetRays() const; + std::optional GetObjectRay(std::string id); std::shared_ptr GetCrosshairRay() const; Position2D GetPosition() const; double GetFov() const; @@ -46,12 +52,13 @@ class Camera2D void SetPosition(const Position2D& position); - Ray CalculateObjectRay(const std::shared_ptr& object) const; - private: void InitRays(); + void Calculate(const std::shared_ptr& object); + double WorldAngleToCameraAngle(double angle) const; Camera2DConfig config_; std::shared_ptr rays_; + std::unordered_map objects_; std::shared_ptr ray_cast_; std::shared_ptr crosshair_ray_; Position2D position_; diff --git a/src/Camera/src/camera.cpp b/src/Camera/src/camera.cpp index 8edd41f..de47f94 100644 --- a/src/Camera/src/camera.cpp +++ b/src/Camera/src/camera.cpp @@ -10,11 +10,21 @@ */ #include "Camera/camera.h" +#include "Camera/ray.h" #include "Math/vector.h" +#include #include +#include namespace wolfenstein { +void Camera2D::InitRays() { + rays_ = std::make_shared(); + for (int i = 0; i < config_.width / 2; i++) { + rays_->emplace_back(Ray()); + } +} + Camera2D::Camera2D(const Camera2DConfig& config) : config_(config), ray_cast_(std::make_shared(config.width / 2, config.fov, @@ -27,12 +37,25 @@ void Camera2D::Update(const std::shared_ptr& scene) { crosshair_ray_ = std::make_shared(rays_->at(config_.width / 4)); // Update object rays + objects_.clear(); + for (const auto& object : scene->GetObjects()) { + if (object->GetObjectType() == ObjectType::STATIC_OBJECT) { + Calculate(object); + } + } } std::shared_ptr Camera2D::GetRays() const { return rays_; } +std::optional Camera2D::GetObjectRay(std::string id) { + if (objects_.find(id) != objects_.end()) { + return objects_[id]; + } + return std::nullopt; +} + std::shared_ptr Camera2D::GetCrosshairRay() const { return crosshair_ray_; } @@ -52,44 +75,77 @@ void Camera2D::SetPosition(const Position2D& position) { position_ = position; } -Ray Camera2D::CalculateObjectRay( - const std::shared_ptr& object) const { - Ray object_ray{}; - auto static_object = std::dynamic_pointer_cast(object); - const auto object_pose = static_object->GetPose(); - const auto w = static_object->GetWidth(); - - // check if object is in the camera view - auto object_distance = Distance(object_pose, position_.pose); - if (object_distance > config_.depth) { - return object_ray; +void Camera2D::Calculate(const std::shared_ptr& object) { + if (object->GetObjectType() == ObjectType::STATIC_OBJECT) { + + auto static_object = std::dynamic_pointer_cast(object); + const auto object_pose = static_object->GetPose(); + const auto width = static_object->GetWidth(); + + // check if object is in the camera view + auto object_distance = object_pose.Distance(position_.pose); + if (object_distance > config_.depth) { + return; + } + + // Object center angle + const auto object_center_angle = std::atan2( + object_pose.y - position_.pose.y, object_pose.x - position_.pose.x); + + // Object left edge point and angle + auto object_left_edge_angle = + SubRadian(object_center_angle, ToRadians(90.0)); + const auto left_edge_point = + object_pose + + vector2d{width / 2 * std::cos(object_left_edge_angle), + width / 2 * std::sin(object_left_edge_angle)}; + const auto left_edge_angle = + std::atan2(left_edge_point.y - position_.pose.y, + left_edge_point.x - position_.pose.x); + const auto camera_angle_left = WorldAngleToCameraAngle(left_edge_angle); + + // Object right edge point and angle + auto object_right_edge_angle = + SumRadian(object_center_angle, ToRadians(90.0)); + const auto right_edge_point = + object_pose + + vector2d{width / 2 * std::cos(object_right_edge_angle), + width / 2 * std::sin(object_right_edge_angle)}; + const auto right_edge_angle = + std::atan2(right_edge_point.y - position_.pose.y, + right_edge_point.x - position_.pose.x); + const auto camera_angle_right = + WorldAngleToCameraAngle(right_edge_angle); + + // Check if object is in the camera view + if (camera_angle_right < -config_.fov / 2 || + camera_angle_left > config_.fov / 2) { + return; + } + const auto texture_id = static_object->GetTextureId(); + + // Calculate object raypair + RayPair object_ray_pair; + object_ray_pair.first.Reset(position_.pose, camera_angle_left); + object_ray_pair.first.is_hit = true; + object_ray_pair.first.perpendicular_distance = object_distance; + object_ray_pair.first.wall_id = texture_id; + + object_ray_pair.second.Reset(position_.pose, camera_angle_right); + object_ray_pair.second.is_hit = true; + object_ray_pair.second.perpendicular_distance = object_distance; + object_ray_pair.second.wall_id = texture_id; + + objects_[object->GetId()] = object_ray_pair; } - - const auto object_center_angle = std::atan2( - object_pose.y - position_.pose.y, object_pose.x - position_.pose.x); - - auto object_left_edge_angle = - SubRadian(object_center_angle, ToRadians(90.0)); - const auto left_edge_point = - object_pose + vector2d{w * std::cos(object_left_edge_angle), - w * std::sin(object_left_edge_angle)}; - const auto left_edge_angle = - std::atan2(left_edge_point.y - position_.pose.y, - left_edge_point.x - position_.pose.x); - object_ray.theta = - left_edge_angle > 0 ? left_edge_angle : 2 * M_PI + left_edge_angle; - object_ray.is_hit = true; - object_ray.perpendicular_distance = object_distance; - object_ray.wall_id = static_object->GetTextureId(); - - return object_ray; } -void Camera2D::InitRays() { - rays_ = std::make_shared(); - for (int i = 0; i < config_.width / 2; i++) { - rays_->emplace_back(Ray()); - } +double Camera2D::WorldAngleToCameraAngle(double angle) const { + const auto vector_of_crosshair = crosshair_ray_->direction; + const auto vector_of_ray = vector2d{std::cos(angle), std::sin(angle)}; + const auto angle_between = CalculateAngleBetweenTwoVectorsSigned( + vector_of_ray, vector_of_crosshair); + return angle_between; } } // namespace wolfenstein \ No newline at end of file diff --git a/src/Core/CMakeLists.txt b/src/Core/CMakeLists.txt index 153c11f..247e98d 100644 --- a/src/Core/CMakeLists.txt +++ b/src/Core/CMakeLists.txt @@ -28,7 +28,6 @@ target_link_libraries( game_object map vector - utilities renderer SDL2 ) \ No newline at end of file diff --git a/src/Core/src/game.cpp b/src/Core/src/game.cpp index a3ebffb..218a235 100644 --- a/src/Core/src/game.cpp +++ b/src/Core/src/game.cpp @@ -36,7 +36,7 @@ void Game::Init() { config_.view_distance}; camera_ = std::make_shared(camera_config); - CharacterConfig player_config = {Position2D({3, 1.5}, 1.50), 2.0, 5.5}; + CharacterConfig player_config = {Position2D({3, 1.5}, 1.50), 2.0, 0.4}; player_ = std::make_shared(player_config); auto camera_position_updator = [this](Position2D position) { @@ -48,7 +48,7 @@ void Game::Init() { const auto candlebra = std::make_shared(vector2d(3.5, 1.5), 6, 0.2, 0.2); const auto candlebra1 = - std::make_shared(vector2d(12.5, 9.5), 8, 0.2, 0.2); + std::make_shared(vector2d(12.5, 9.5), 8, 0.4, 0.8); const auto candlebra2 = std::make_shared(vector2d(3.5, 7.5), 6, 0.2, 0.2); diff --git a/src/Graphics/include/Graphics/renderer.h b/src/Graphics/include/Graphics/renderer.h index 878c1ff..4561737 100644 --- a/src/Graphics/include/Graphics/renderer.h +++ b/src/Graphics/include/Graphics/renderer.h @@ -89,7 +89,7 @@ class Renderer void RenderObjects(const std::vector>& objects, const std::shared_ptr& camera_ptr, RenderQueue& render_queue); - int CalculateHorizontalSlice(const Ray& ray, + int CalculateHorizontalSlice(const double& angle, const std::shared_ptr camera_ptr); std::tuple CalculateVerticalSlice(const double& distance); void RenderTextures(RenderQueue& render_queue); diff --git a/src/Graphics/src/renderer.cpp b/src/Graphics/src/renderer.cpp index e3ca4b7..299540a 100644 --- a/src/Graphics/src/renderer.cpp +++ b/src/Graphics/src/renderer.cpp @@ -145,48 +145,44 @@ void Renderer::RenderObjects( const auto static_object = std::dynamic_pointer_cast(object); - const auto object_ray = camera_ptr->CalculateObjectRay(object); - if (!object_ray.is_hit) { + const auto ray_pair = camera_ptr->GetObjectRay(object->GetId()); + if (!ray_pair.has_value()) { continue; } - const auto w = static_object->GetWidth(); + const auto first = ray_pair.value().first; + const auto last = ray_pair.value().second; const auto [line_height, draw_start, draw_end] = - CalculateVerticalSlice(object_ray.perpendicular_distance); - - SDL_Rect src_rect = {0, 0, - TextureManager::GetInstance() - .GetTexture(object_ray.wall_id) - .width, - TextureManager::GetInstance() - .GetTexture(object_ray.wall_id) - .height}; - const auto horizontal_slice = - CalculateHorizontalSlice(object_ray, camera_ptr); - - SDL_Rect dest_rect = {horizontal_slice, draw_start, - static_cast(w * line_height), - line_height}; - render_queue.push({object_ray.wall_id, src_rect, dest_rect, - object_ray.perpendicular_distance}); + CalculateVerticalSlice(first.perpendicular_distance); + + const auto texture_height = + TextureManager::GetInstance().GetTexture(first.wall_id).height; + const auto texture_width = + TextureManager::GetInstance().GetTexture(first.wall_id).width; + + const auto first_slice = + CalculateHorizontalSlice(first.theta, camera_ptr); + + const auto last_slice = + CalculateHorizontalSlice(last.theta, camera_ptr); + + SDL_Rect src_rect = {0, 0, texture_width, texture_height}; + + SDL_Rect dest_rect = {first_slice, draw_start, + last_slice - first_slice, line_height}; + + render_queue.push({first.wall_id, src_rect, dest_rect, + first.perpendicular_distance}); } } } int Renderer::CalculateHorizontalSlice( - const Ray& ray, const std::shared_ptr camera_ptr) { - auto cam_theta = camera_ptr->GetPosition().theta; - const auto vector_of_crosshair = - vector2d{std::cos(cam_theta), std::sin(cam_theta)}; - const auto vector_of_ray = - vector2d{std::cos(ray.theta), std::sin(ray.theta)}; - - const auto angle_between = CalculateAngleBetweenTwoVectorsSigned( - vector_of_ray, vector_of_crosshair); + const double& angle, const std::shared_ptr camera_ptr) { const auto horizontal_slice = - static_cast(angle_between / camera_ptr->GetDeltaAngle()) * 2 + + static_cast(angle / camera_ptr->GetDeltaAngle()) * 2 + config_.width / 2; return horizontal_slice; @@ -260,21 +256,6 @@ void Renderer::RenderObjects( } if (object->GetObjectType() == ObjectType::STATIC_OBJECT) { - const auto object_ray = camera_ptr->CalculateObjectRay(object); - if (!object_ray.is_hit) { - continue; - } - auto cam_theta = camera_ptr->GetPosition().theta; - const auto vector_of_crosshair = - vector2d{std::cos(cam_theta), std::sin(cam_theta)}; - const auto vector_of_ray = vector2d{std::cos(object_ray.theta), - std::sin(object_ray.theta)}; - - const auto angle_between = CalculateAngleBetweenTwoVectorsSigned( - vector_of_ray, vector_of_crosshair); - if (std::abs(angle_between) > ToRadians(45.0)) { - continue; - } auto static_object = std::dynamic_pointer_cast(object); @@ -282,8 +263,9 @@ void Renderer::RenderObjects( const auto object_pose = static_object->GetPose(); const auto w = static_object->GetWidth(); - const auto object_points = GenerateCirclePoints( - ToVector2i(object_pose * config_.scale), 10, 20); + const auto object_points = + GenerateCirclePoints(ToVector2i(object_pose * config_.scale), + config_.scale * w / 2, 20); for (unsigned int i = 0; i < object_points.size(); i++) { SDL_RenderDrawPoint(renderer_, object_points[i].x, object_points[i].y); @@ -297,14 +279,14 @@ void Renderer::RenderObjects( const auto to_right = SumRadian(object_angle, ToRadians(90.0)); const auto left_vertex = object_pose + - vector2d{w * std::cos(to_left), w * std::sin(to_left)}; + vector2d{w / 2 * std::cos(to_left), w / 2 * std::sin(to_left)}; const auto right_vertex = - object_pose + - vector2d{w * std::cos(to_right), w * std::sin(to_right)}; + object_pose + vector2d{w / 2 * std::cos(to_right), + w / 2 * std::sin(to_right)}; DrawLine(ToVector2i(left_vertex * config_.scale), ToVector2i(right_vertex * config_.scale)); const auto left_point = GenerateCirclePoints( - ToVector2i(left_vertex * config_.scale), 5, 20); + ToVector2i(left_vertex * config_.scale), 1, 20); SetDrawColor({0xFF, 0, 0, 255}); for (unsigned int i = 0; i < left_point.size(); i++) { SDL_RenderDrawPoint(renderer_, left_point[i].x, @@ -312,7 +294,7 @@ void Renderer::RenderObjects( } const auto right_point = GenerateCirclePoints( - ToVector2i(right_vertex * config_.scale), 5, 20); + ToVector2i(right_vertex * config_.scale), 1, 20); SetDrawColor({0, 0xFF, 0, 255}); for (unsigned int i = 0; i < right_point.size(); i++) { SDL_RenderDrawPoint(renderer_, right_point[i].x, diff --git a/src/Math/include/Math/vector.h b/src/Math/include/Math/vector.h index 25bd86e..378e730 100644 --- a/src/Math/include/Math/vector.h +++ b/src/Math/include/Math/vector.h @@ -57,6 +57,7 @@ struct vector2d double Dot(const vector2d& v) const; double Magnitude() const; double Determinant(const vector2d& v) const; + double Distance(const vector2d& v) const; void Norm(); }; diff --git a/src/Math/src/vector.cpp b/src/Math/src/vector.cpp index d6e3637..9ed49be 100644 --- a/src/Math/src/vector.cpp +++ b/src/Math/src/vector.cpp @@ -121,6 +121,10 @@ double vector2d::Determinant(const vector2d& v) const { return x * v.y - y * v.x; } +double vector2d::Distance(const vector2d& v) const { + return std::sqrt(std::pow(x - v.x, 2) + std::pow(y - v.y, 2)); +} + void vector2d::Norm() { double size = std::sqrt(x * x + y * y); x /= size; From 71b8175ec94bbee7461f8893630421077ce82c6d Mon Sep 17 00:00:00 2001 From: Bilal Kahraman Date: Fri, 16 Aug 2024 00:04:56 +0300 Subject: [PATCH 6/7] Pull submodules with cmake --- CMakeLists.txt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index ea5d35e..5dd2813 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,27 @@ add_definitions( set(CMAKE_BUILD_TYPE Debug) # set(CMAKE_BUILD_TYPE Release) + +find_package(Git QUIET) + +if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git") +# Update submodules as needed + option(GIT_SUBMODULE "Check submodules during build" ON) + if(GIT_SUBMODULE) + message(STATUS "Submodule update") + execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + RESULT_VARIABLE GIT_SUBMOD_RESULT) + if(NOT GIT_SUBMOD_RESULT EQUAL "0") + message(FATAL_ERROR "git submodule update --init --recursive failed with ${GIT_SUBMOD_RESULT}, please checkout submodules") + endif() + endif() +endif() + +if(NOT EXISTS "${PROJECT_SOURCE_DIR}/third-party/uuid_v4/CMakeLists.txt") + message(FATAL_ERROR "The submodules were not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.") +endif() + # internal libraries add_subdirectory(app) add_subdirectory(src/Camera) From 947cbcdc2074835db80a786269bf64d49cd570bc Mon Sep 17 00:00:00 2001 From: Bilal Kahraman Date: Fri, 16 Aug 2024 00:10:45 +0300 Subject: [PATCH 7/7] Use https instead of ssh --- .gitmodules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index f51dc77..9732d84 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "third-party/uuid_v4"] path = third-party/uuid_v4 - url = git@github.com:crashoz/uuid_v4.git + url = https://github.com/crashoz/uuid_v4.git