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 +++++++++- kurator/src/Battle.cpp | 54 +++++++++++++--------------------- 6 files changed, 84 insertions(+), 34 deletions(-) create mode 100644 engine/src/Camera.cpp 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 diff --git a/kurator/src/Battle.cpp b/kurator/src/Battle.cpp index 40390ee..9bb5dec 100644 --- a/kurator/src/Battle.cpp +++ b/kurator/src/Battle.cpp @@ -130,27 +130,24 @@ void Battle::draw() const { ClearBackground(BLACK); - const int hwidth = GetScreenWidth() / 2; - const int hheight = GetScreenHeight() / 2; Grid().draw(camera); auto& registry = battle->registry(); auto crosses = registry.view(); for (const auto& [entity, transform, cross] : crosses.each()) { if (cross.timer > cross.phase) continue; - const int x = hwidth + (transform.position.x - camera.offset.x) * camera.scale; - const int y = hheight + (transform.position.y - camera.offset.y) * camera.scale; + const auto pos = camera.to_screen(transform.position); DrawLine( - x - cross.hlength, - y - cross.hlength, - x + cross.hlength, - y + cross.hlength, + pos.x - cross.hlength, + pos.y - cross.hlength, + pos.x + cross.hlength, + pos.y + cross.hlength, cross.color); DrawLine( - x + cross.hlength, - y - cross.hlength, - x - cross.hlength, - y + cross.hlength, + pos.x + cross.hlength, + pos.y - cross.hlength, + pos.x - cross.hlength, + pos.y + cross.hlength, cross.color); } auto lines = registry.view(); @@ -158,35 +155,26 @@ Battle::draw() const const auto diff = line.end - line.start; const auto fstart = line.position - line.hlength; const auto fend = line.position + line.hlength; - const auto start = line.start + diff.scale(fstart > 0.0 ? fstart : 0.0); - const auto end = line.start + diff.scale(fend > 1.0 ? 1.0 : fend); - DrawLine( - hwidth + (start.x - camera.offset.x) * camera.scale, - hheight + (start.y - camera.offset.y) * camera.scale, - hwidth + (end.x - camera.offset.x) * camera.scale, - hheight + (end.y - camera.offset.y) * camera.scale, - line.color); + const auto start = camera.to_screen(line.start + diff.scale(fstart > 0.0 ? fstart : 0.0)); + const auto end = camera.to_screen(line.start + diff.scale(fend > 1.0 ? 1.0 : fend)); + DrawLine(start.x, start.y, end.x, end.y, line.color); } auto view = registry.view(); for (auto [entity, marker, transform] : view.each()) { - const int x = hwidth + (transform.position.x - camera.offset.x) * camera.scale; - const int y = hheight + (transform.position.y - camera.offset.y) * camera.scale; - DrawCircle(x, y, marker.radius, marker.color); + const auto pos = camera.to_screen(transform.position); + DrawCircle(pos.x, pos.y, marker.radius, marker.color); DrawLine( - x, - y, - x + marker.radius*std::cos(transform.angle), - y + marker.radius*std::sin(transform.angle), + pos.x, + pos.y, + pos.x + marker.radius*std::cos(transform.angle), + pos.y + marker.radius*std::sin(transform.angle), WHITE); - DrawText(marker.name.c_str(), x+10, y-5, 10.0f, GRAY); + DrawText(marker.name.c_str(), pos.x+10, pos.y-5, 10.0f, GRAY); } auto pops = registry.view(); for (const auto& [entity, text, transform, offset] : pops.each()) { - const int x = - hwidth + (transform.position.x - camera.offset.x) * camera.scale - text.width/2 + offset.x; - const int y = - hheight + (transform.position.y - camera.offset.y) * camera.scale - text.font_size/2 + offset.y; - DrawText(text.text.c_str(), x, y, text.font_size, text.color); + const auto pos = camera.to_screen(transform.position).subtract(text.width/2, text.font_size/2) + offset; + DrawText(text.text.c_str(), pos.x, pos.y, text.font_size, text.color); } balance.draw(); } -- cgit v1.1