From c388759ed67b792201b99bf7d73d036c34b47d87 Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Wed, 7 Nov 2018 10:09:47 -0800 Subject: Working on some consistency --- Source/Memory.h | 4 +-- Source/Panel.cpp | 16 ++++++----- Source/Panel.h | 21 ++++++++++++++- Test/PanelExtractionTests.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++ Test/Test.vcxproj | 1 + 5 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 Test/PanelExtractionTests.cpp diff --git a/Source/Memory.h b/Source/Memory.h index e7f8ff3..d14d58d 100644 --- a/Source/Memory.h +++ b/Source/Memory.h @@ -3,8 +3,8 @@ #include #include -// #define GLOBALS 0x5B28C0 -#define GLOBALS 0x62A080 +#define GLOBALS 0x5B28C0 +// #define GLOBALS 0x62A080 // https://github.com/erayarslan/WriteProcessMemory-Example // http://stackoverflow.com/q/32798185 diff --git a/Source/Panel.cpp b/Source/Panel.cpp index 8642461..b6aa504 100644 --- a/Source/Panel.cpp +++ b/Source/Panel.cpp @@ -22,6 +22,9 @@ Panel::Panel(int id) { ReadDecorations(id); } +// For testing +Panel::Panel() {} + void Panel::Write(int id) { WriteIntersections(id); WriteDecorations(id); @@ -147,8 +150,7 @@ void Panel::ReadIntersections(int id) { } else { dir = Endpoint::Direction::DOWN; } - int x = 2 * (location % ((_width + 1) / 2)); - int y = (_height - 1) - 2 * (location / ((_width + 1) / 2)); + auto [x, y] = loc_to_xy(location); _endpoints.push_back(Endpoint(x, y, dir)); } } @@ -163,15 +165,15 @@ void Panel::WriteIntersections(int id) { double min = 0.1; double max = 0.9; - double width_interval = (max - min) / (_width - 1); - double height_interval = (max - min) / (_height - 1); + double width_interval = (max - min) / (_width/2); + double height_interval = (max - min) / (_height/2); - for (int y=0; y<_height; y++) { - for (int x=0; x<_width; x++) { + for (int y=0; y<_height/2; y++) { + for (int x=0; x<_width/2; x++) { intersections.push_back(static_cast(min + x * width_interval)); intersections.push_back(static_cast(min + y * height_interval)); int flags = 0; - if (find(_startpoints, {x, y}) != -1) flags |= IntersectionFlags::IS_STARTPOINT; + if (find(_startpoints, {2*x, 2*y}) != -1) flags |= IntersectionFlags::IS_STARTPOINT; intersectionFlags.push_back(flags); if (y > 0) { connections.first.push_back(y * _width + x); diff --git a/Source/Panel.h b/Source/Panel.h index c9b4910..565b4c3 100644 --- a/Source/Panel.h +++ b/Source/Panel.h @@ -90,7 +90,7 @@ private: class Panel { public: - explicit Panel(int id); + Panel(int id); // explicit Panel(nlohmann::json json); void Write(int id); @@ -109,6 +109,9 @@ public: }; private: + // For testing + Panel(); + void ReadIntersections(int id); void WriteIntersections(int id); void ReadDecorations(int id); @@ -117,6 +120,20 @@ private: // TODO: Reflection data // TODO: Decoration colors + std::tuple loc_to_xy(int location) { + int x = 2 * (location % ((_width + 1) / 2)); + int y = (_height - 1) - 2 * (location / ((_width + 1) / 2)); + return {x, y}; + } + + int xy_to_loc(int x, int y) { + int height2 = (_height - 1) / 2; + int width2 = (_width + 1) / 2; + + int rowsFromBottom = height2 - y/2; + return rowsFromBottom * width2 + x/2; + } + Memory _memory = Memory("witness64_d3d11.exe"); int _width, _height; @@ -125,4 +142,6 @@ private: std::vector _endpoints; std::vector> _startpoints; int _style; + + friend class PanelExtractionTests; }; \ No newline at end of file diff --git a/Test/PanelExtractionTests.cpp b/Test/PanelExtractionTests.cpp new file mode 100644 index 0000000..dfb9b9e --- /dev/null +++ b/Test/PanelExtractionTests.cpp @@ -0,0 +1,63 @@ +#include "gtest/gtest.h" +#include "Panel.h" + +class PanelExtractionTests : public testing::Test +{ +protected: + Panel _panel; + // std::shared_ptr _panel; + + void SetPanelSize(int width, int height) { + // _panel = std::make_shared(); + _panel._width = width; + _panel._height = height; + } + + std::tuple loc_to_xy(int location) { + return _panel.loc_to_xy(location); + } + + int xy_to_loc(int x, int y) { + return _panel.xy_to_loc(x, y); + } +}; + +TEST_F(PanelExtractionTests, LocToXY_7x5) { + SetPanelSize(7, 5); + /* Here's the panel, with the correct location order + 8 . 9 . 10. 11 (_width = 7) + . . . . . . . + 4 . 5 . 6 . 7 + . . . . . . . + 0 . 1 . 2 . 3 + (_height = 5) + */ + ASSERT_EQ(0, xy_to_loc(0, 4)); + ASSERT_EQ(1, xy_to_loc(2, 4)); + ASSERT_EQ(2, xy_to_loc(4, 4)); + ASSERT_EQ(3, xy_to_loc(6, 4)); + + ASSERT_EQ(4, xy_to_loc(0, 2)); + ASSERT_EQ(5, xy_to_loc(2, 2)); + ASSERT_EQ(6, xy_to_loc(4, 2)); + ASSERT_EQ(7, xy_to_loc(6, 2)); + + ASSERT_EQ(8, xy_to_loc(0, 0)); + ASSERT_EQ(9, xy_to_loc(2, 0)); + ASSERT_EQ(10, xy_to_loc(4, 0)); + ASSERT_EQ(11, xy_to_loc(6, 0)); +} + +TEST_F(PanelExtractionTests, LocToXY_3x3) { + SetPanelSize(3, 3); + /* Here's the panel, with the correct location order + 2 . 3 (_width = 3) + . . . + 0 . 1 + (_height = 3) + */ + ASSERT_EQ(0, xy_to_loc(0, 2)); + ASSERT_EQ(1, xy_to_loc(2, 2)); + ASSERT_EQ(2, xy_to_loc(0, 0)); + ASSERT_EQ(3, xy_to_loc(2, 0)); +} \ No newline at end of file diff --git a/Test/Test.vcxproj b/Test/Test.vcxproj index a6d8f75..55f3ce8 100644 --- a/Test/Test.vcxproj +++ b/Test/Test.vcxproj @@ -33,6 +33,7 @@ + -- cgit 1.4.1