diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..9732d84 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "third-party/uuid_v4"] + path = third-party/uuid_v4 + url = https://github.com/crashoz/uuid_v4.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 9839fcf..5dd2813 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,28 @@ 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) add_subdirectory(src/CollisionManager) @@ -27,22 +49,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/Camera/CMakeLists.txt b/src/Camera/CMakeLists.txt index 43da228..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} ) @@ -45,4 +48,6 @@ target_link_libraries( character vector map + scene + texture_manager ) diff --git a/src/Camera/include/Camera/camera.h b/src/Camera/include/Camera/camera.h index a773f35..068d59e 100644 --- a/src/Camera/include/Camera/camera.h +++ b/src/Camera/include/Camera/camera.h @@ -13,12 +13,16 @@ #define CAMERA_CAMERA_H #include "Camera/raycaster.h" +#include "Graphics/scene.h" #include #include #include #include #include #include +#include +#include +#include namespace wolfenstein { @@ -29,15 +33,18 @@ struct Camera2DConfig double depth; }; +typedef std::pair RayPair; + class Camera2D { public: 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::optional GetObjectRay(std::string id); std::shared_ptr GetCrosshairRay() const; Position2D GetPosition() const; double GetFov() const; @@ -45,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 c6670f6..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, @@ -22,15 +32,30 @@ 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 + 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_; } @@ -50,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/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/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 bd9904a..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); @@ -101,6 +101,7 @@ void Game::Run() { CheckEvent(); time_manager_->CalculateDeltaTime(); scene_->Update(time_manager_->GetDeltaTime()); + camera_->Update(scene_); switch (render_type_) { case RenderType::TEXTURE: renderer_->RenderScene(scene_, camera_); 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; } 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 92014ac..299540a 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; @@ -146,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; @@ -219,7 +214,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); @@ -238,7 +232,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; } @@ -262,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); @@ -284,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); @@ -299,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, @@ -314,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; 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