From 38bf4a4197e6c201bddf0e3365f8d512d2500307 Mon Sep 17 00:00:00 2001 From: ValeriiKoniushenko Date: Sat, 14 Oct 2023 20:38:29 +0300 Subject: [PATCH] Added camera supporting --- CMakeLists.txt | 1 + .../libs/statechart/example/Camera/Camera.cpp | 3 +- lib/core/camera/CMakeLists.txt | 24 ++++++++ lib/core/camera/include/Camera.h | 48 +++++++++++++++ lib/core/camera/source/Camera.cpp | 59 +++++++++++++++++++ 5 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 lib/core/camera/CMakeLists.txt create mode 100644 lib/core/camera/include/Camera.h create mode 100644 lib/core/camera/source/Camera.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 238083246..6f804eb7e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ add_subdirectory(lib/glm) add_subdirectory(lib/misc) add_subdirectory(lib/core/animations) +add_subdirectory(lib/core/camera) add_subdirectory(lib/core/core-wrappers) add_subdirectory(lib/core/delegate) add_subdirectory(lib/core/freetype-wrapper) diff --git a/dependencies/boost_1_80_0/libs/statechart/example/Camera/Camera.cpp b/dependencies/boost_1_80_0/libs/statechart/example/Camera/Camera.cpp index 8e4a984c4..6021761f1 100644 --- a/dependencies/boost_1_80_0/libs/statechart/example/Camera/Camera.cpp +++ b/dependencies/boost_1_80_0/libs/statechart/example/Camera/Camera.cpp @@ -10,8 +10,7 @@ #include "Camera.hpp" #include "Configuring.hpp" #include "Shooting.hpp" - - +#include "../../../../../../lib/core/camera/include/Camera.h" ////////////////////////////////////////////////////////////////////////////// NotShooting::NotShooting() diff --git a/lib/core/camera/CMakeLists.txt b/lib/core/camera/CMakeLists.txt new file mode 100644 index 000000000..c6dd86942 --- /dev/null +++ b/lib/core/camera/CMakeLists.txt @@ -0,0 +1,24 @@ +file( + GLOB Sources + "include/*.h" + "source/*.cpp" +) + +add_library( + Camera STATIC + ${Sources} +) + +target_include_directories( + Camera PUBLIC + include +) + +target_link_libraries( + Camera PUBLIC + glfw + Glad + Utils + Glm + Core-Wrappers +) \ No newline at end of file diff --git a/lib/core/camera/include/Camera.h b/lib/core/camera/include/Camera.h new file mode 100644 index 000000000..5535cadcc --- /dev/null +++ b/lib/core/camera/include/Camera.h @@ -0,0 +1,48 @@ +// MIT License +// +// Copyright (c) 2023 Valerii Koniushenko +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#pragma once + +#include "CopyableAndMoveable.h" +#include "Size.h" +#include "glm/glm.hpp" +#include "Updateable.h" + +class Camera : public Utils::CopyableAndMoveable, public Updateable +{ +public: + void setSize(const Utils::ISize2D& size); + [[nodiscard]] const Utils::ISize2D& getSize() const; + + void setPosition(const glm::ivec2& position); + void move(const glm::ivec2& offset); + [[nodiscard]] const glm::ivec2& getPosition() const; + + void update() override; + + void zoom(float factor); + +private: + Utils::ISize2D size_; + glm::ivec2 position_{}; + float zoomFactor_ = 1.f; +}; \ No newline at end of file diff --git a/lib/core/camera/source/Camera.cpp b/lib/core/camera/source/Camera.cpp new file mode 100644 index 000000000..cad112181 --- /dev/null +++ b/lib/core/camera/source/Camera.cpp @@ -0,0 +1,59 @@ +// MIT License +// +// Copyright (c) 2023 Valerii Koniushenko +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#include "Camera.h" +#include "Window.h" + +void Camera::setSize(const Utils::ISize2D& size) +{ + size_ = size; +} + +const Utils::ISize2D& Camera::getSize() const +{ + return size_; +} + +void Camera::setPosition(const glm::ivec2& position) +{ + position_ = position; +} + +void Camera::move(const glm::ivec2& offset) +{ + position_ += offset; +} + +const glm::ivec2& Camera::getPosition() const +{ + return position_; +} + +void Camera::update() +{ + GetWindow().viewport(-position_.x, position_.y, size_.width * zoomFactor_, size_.height * zoomFactor_); +} + +void Camera::zoom(float factor) +{ + zoomFactor_ = factor; +} \ No newline at end of file