From 6b7d0e795bc57856a1bcaaab08229bb0869e8516 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 3 Feb 2023 23:53:33 +0100 Subject: Extracted from/to camera viewport transformations to camera itself --- engine/CMakeLists.txt | 5 +++++ engine/include/kurator/engine/Camera.h | 2 ++ engine/include/kurator/engine/Point.h | 2 ++ engine/src/Camera.cpp | 39 ++++++++++++++++++++++++++++++++++ engine/src/Point.cpp | 16 +++++++++++++- 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 engine/src/Camera.cpp (limited to 'engine') 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 + +#include + +#include + + +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(GetScreenWidth()), static_cast(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 -- cgit v1.1