diff options
author | Aki <please@ignore.pl> | 2023-02-03 23:53:33 +0100 |
---|---|---|
committer | Aki <please@ignore.pl> | 2023-02-03 23:53:33 +0100 |
commit | 6b7d0e795bc57856a1bcaaab08229bb0869e8516 (patch) | |
tree | f22e1b1cd1499d2c4a5346be0534c305386aca58 /engine | |
parent | ec3ae653a0965c6c19920ed46030a0abde0fee1c (diff) | |
download | kurator-6b7d0e795bc57856a1bcaaab08229bb0869e8516.zip kurator-6b7d0e795bc57856a1bcaaab08229bb0869e8516.tar.gz kurator-6b7d0e795bc57856a1bcaaab08229bb0869e8516.tar.bz2 |
Extracted from/to camera viewport transformations to camera itself
Diffstat (limited to 'engine')
-rw-r--r-- | engine/CMakeLists.txt | 5 | ||||
-rw-r--r-- | engine/include/kurator/engine/Camera.h | 2 | ||||
-rw-r--r-- | engine/include/kurator/engine/Point.h | 2 | ||||
-rw-r--r-- | engine/src/Camera.cpp | 39 | ||||
-rw-r--r-- | engine/src/Point.cpp | 16 |
5 files changed, 63 insertions, 1 deletions
diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 041965a..1ffbd98 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -1,9 +1,14 @@ project(engine) add_library( ${PROJECT_NAME} STATIC + src/Camera.cpp src/Point.cpp ) target_include_directories( ${PROJECT_NAME} PUBLIC include ) +target_link_libraries( + ${PROJECT_NAME} + PRIVATE raylib +) diff --git a/engine/include/kurator/engine/Camera.h b/engine/include/kurator/engine/Camera.h index abe0ffe..2eb192a 100644 --- a/engine/include/kurator/engine/Camera.h +++ b/engine/include/kurator/engine/Camera.h @@ -13,6 +13,8 @@ struct Camera { Point offset = {}; double scale = 1.0; + Point to_world(const Point& screen) const; + Point to_screen(const Point& world) const; }; diff --git a/engine/include/kurator/engine/Point.h b/engine/include/kurator/engine/Point.h index 0e9f0ab..3dc0c8b 100644 --- a/engine/include/kurator/engine/Point.h +++ b/engine/include/kurator/engine/Point.h @@ -19,6 +19,8 @@ struct Point Point normalized() const; Point operator-(const Point& other) const; Point operator+(const Point& other) const; + Point subtract(const Point& other) const; + Point subtract(double _x, double _y) const; }; diff --git a/engine/src/Camera.cpp b/engine/src/Camera.cpp new file mode 100644 index 0000000..60e8aa1 --- /dev/null +++ b/engine/src/Camera.cpp @@ -0,0 +1,39 @@ +#include <kurator/engine/Camera.h> + +#include <raylib.h> + +#include <kurator/engine/Point.h> + + +namespace kurator +{ +namespace engine +{ + + +static auto viewport() -> Point; + + +Point +Camera::to_world(const Point& screen) const +{ + return screen - viewport().scale(1.0 / (2.0 * scale)) + offset; +} + + +Point +Camera::to_screen(const Point& world) const +{ + return (world - offset).scale(scale) + viewport().scale(0.5); +} + + +Point +viewport() +{ + return {static_cast<double>(GetScreenWidth()), static_cast<double>(GetScreenHeight())}; +} + + +} // namespace engine +} // namespace kurator diff --git a/engine/src/Point.cpp b/engine/src/Point.cpp index b5ce4eb..f02991c 100644 --- a/engine/src/Point.cpp +++ b/engine/src/Point.cpp @@ -57,7 +57,7 @@ Point::normalized() const Point Point::operator-(const Point& other) const { - return {x - other.x, y - other.y}; + return subtract(other); } @@ -68,5 +68,19 @@ Point::operator+(const Point& other) const } +Point +Point::subtract(const Point& other) const +{ + return {x - other.x, y - other.y}; +} + + +Point +Point::subtract(const double _x, const double _y) const +{ + return {x - _x, y - _y}; +} + + } // namespace engine } // namespace kurator |