Skip to content

Commit b55fbeb

Browse files
Merge pull request #175
Typed Coordinate Convention
2 parents e9db998 + 70b11e7 commit b55fbeb

File tree

8 files changed

+152
-19
lines changed

8 files changed

+152
-19
lines changed

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ set(SRC_FILES
5454
${SRC_DIR}/Util/Color.cpp
5555
${SRC_DIR}/Util/Animation.cpp
5656
${SRC_DIR}/Util/MissingTexture.cpp
57+
${SRC_DIR}/Util/Position.cpp
5758
)
5859
set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
5960
set(INCLUDE_FILES
@@ -90,6 +91,7 @@ set(INCLUDE_FILES
9091
${INCLUDE_DIR}/Util/MissingTexture.hpp
9192
${INCLUDE_DIR}/Util/Base64.hpp
9293
${INCLUDE_DIR}/Util/Animation.hpp
94+
${INCLUDE_DIR}/Util/Position.hpp
9395
)
9496
set(EXAMPLE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/example)
9597
set(EXAMPLE_FILES
@@ -109,6 +111,7 @@ set(TEST_FILES
109111
${TEST_DIR}/SimpleTest.cpp
110112
${TEST_DIR}/NotSimpleTest.cpp
111113
${TEST_DIR}/TransformTest.cpp
114+
${TEST_DIR}/PositionTest.cpp
112115
)
113116

114117
add_library(PTSD STATIC
@@ -167,7 +170,6 @@ enable_testing()
167170

168171
add_executable(Tests EXCLUDE_FROM_ALL
169172
${TEST_FILES}
170-
${SRC_FILES}
171173
)
172174
target_link_libraries(Tests
173175
PTSD
@@ -185,7 +187,6 @@ target_compile_options(Tests PRIVATE
185187

186188
add_executable(AudioTest EXCLUDE_FROM_ALL
187189
${TEST_DIR}/Interactive/Audio.cpp
188-
${SRC_FILES}
189190
)
190191
target_link_libraries(AudioTest
191192
PTSD

example/src/App.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ void App::Update() {
5050
if (Util::Input::IsKeyPressed(Util::Keycode::B)) {
5151
LOG_DEBUG("B Pressed. Setting the cursor to (0, 0).");
5252
Util::Input::SetCursorPosition({0.0F, 0.0F});
53-
LOG_DEBUG("Cursor set to {}.",
54-
glm::to_string(Util::Input::GetCursorPosition()));
53+
LOG_DEBUG("Cursor set to {}.", Util::Input::GetCursorPosition());
5554
}
5655

5756
m_Giraffe->Update();

include/Util/Input.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include "Util/Keycode.hpp" // for Keycode
1010

11+
#include "Util/Position.hpp" // Util::PTSDPosition
12+
1113
namespace Util {
1214

1315
/**
@@ -50,7 +52,7 @@ class Input {
5052
*
5153
* @see Util::Input::SetCursorPosition()
5254
*/
53-
static glm::vec2 GetCursorPosition();
55+
static Util::PTSDPosition GetCursorPosition();
5456

5557
/**
5658
* @brief Check if a specific key is currently pressed.
@@ -119,7 +121,7 @@ class Input {
119121
* Util::Input::IsMouseMoving() to return true in this update-cycle.
120122
* @see Util::Input::GetCursorPosition()
121123
*/
122-
static void SetCursorPosition(const glm::vec2 &pos);
124+
static void SetCursorPosition(const Util::PTSDPosition &pos);
123125

124126
/**
125127
* @brief Updates the state of the input.
@@ -133,7 +135,7 @@ class Input {
133135

134136
static SDL_Event s_Event;
135137

136-
static glm::vec2 s_CursorPosition;
138+
static Util::PTSDPosition s_CursorPosition;
137139
static glm::vec2 s_ScrollDistance;
138140

139141
static std::unordered_map<Keycode, std::pair<bool, bool>> s_KeyState;

include/Util/Position.hpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#ifndef PTSD_UTIL_POSITION_HPP
2+
#define PTSD_UTIL_POSITION_HPP
3+
4+
#include <glm/glm.hpp>
5+
6+
namespace Util {
7+
8+
struct PTSDPosition;
9+
10+
struct SDLPosition {
11+
public:
12+
const int x;
13+
const int y;
14+
SDLPosition() = delete;
15+
16+
private:
17+
friend PTSDPosition;
18+
SDLPosition(float x, float y)
19+
: x(x),
20+
y(y) {}
21+
};
22+
23+
struct PTSDPosition {
24+
float x{};
25+
float y{};
26+
PTSDPosition() = delete;
27+
static PTSDPosition FromSDL(int sdlx, int sdly);
28+
PTSDPosition(float x, float y)
29+
: x{x},
30+
y{y} {};
31+
[[deprecated("Implicit conversion will be removed. Use explicit conversion "
32+
"instead")]] // `\_(:/)_/`
33+
PTSDPosition(glm::vec2 v)
34+
: x{v.x},
35+
y{v.y} {};
36+
37+
PTSDPosition operator+(const glm::vec2) const;
38+
PTSDPosition operator-(const glm::vec2 vec2) const {
39+
return (*this) + (vec2 * (-1.0f));
40+
}
41+
42+
[[deprecated("Implicit conversion will be removed. Use explicit conversion "
43+
"instead")]]
44+
operator glm::vec2() const {
45+
return {x, y};
46+
}
47+
48+
SDLPosition ToSDLPosition() const;
49+
};
50+
51+
} // namespace Util
52+
53+
#include "Position.inl"
54+
55+
#endif /* PTSD_UTIL_POSITION_HPP */

include/Util/Position.inl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "Util/Position.hpp"
2+
3+
#include <spdlog/spdlog.h>
4+
5+
template <>
6+
struct fmt::formatter<Util::PTSDPosition> : fmt::formatter<std::string> {
7+
auto format(const Util::PTSDPosition &p, format_context &ctx) const
8+
-> decltype(ctx.out()) {
9+
return format_to(ctx.out(), "PTSDPos ({}, {})", p.x, p.y);
10+
}
11+
};

src/Util/Input.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ namespace Util {
88

99
// init all static members
1010
SDL_Event Input::s_Event = SDL_Event();
11-
12-
glm::vec2 Input::s_CursorPosition = glm::vec2(0.0F);
11+
Util::PTSDPosition Input::s_CursorPosition = Util::PTSDPosition(0.0F, 0.0F);
1312
glm::vec2 Input::s_ScrollDistance = glm::vec2(-1.0F, -1.0F);
1413

1514
std::unordered_map<Keycode, std::pair<bool, bool>> Input::s_KeyState = {
@@ -78,12 +77,7 @@ void Input::UpdateKeyState(const SDL_Event *event) {
7877
void Input::Update() {
7978
int x, y;
8079
SDL_GetMouseState(&x, &y);
81-
s_CursorPosition.x = static_cast<float>(x);
82-
s_CursorPosition.y = static_cast<float>(y);
83-
84-
s_CursorPosition.x -= static_cast<float>(PTSD_Config::WINDOW_WIDTH) / 2;
85-
s_CursorPosition.y = -(s_CursorPosition.y -
86-
static_cast<float>(PTSD_Config::WINDOW_HEIGHT) / 2);
80+
s_CursorPosition = Util::PTSDPosition::FromSDL(x, y);
8781

8882
s_Scroll = s_MouseMoving = false;
8983

@@ -116,13 +110,13 @@ void Input::Update() {
116110
}
117111
}
118112

119-
glm::vec2 Input::GetCursorPosition() {
113+
Util::PTSDPosition Input::GetCursorPosition() {
120114
return s_CursorPosition;
121115
}
122116

123-
void Input::SetCursorPosition(const glm::vec2 &pos) {
124-
SDL_WarpMouseInWindow(nullptr, static_cast<int>(pos.x),
125-
static_cast<int>(pos.y));
117+
void Input::SetCursorPosition(const Util::PTSDPosition &ptsdPos) {
118+
auto sdlPos = ptsdPos.ToSDLPosition();
119+
SDL_WarpMouseInWindow(nullptr, sdlPos.x, sdlPos.y);
126120
}
127121

128122
} // namespace Util

src/Util/Position.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "Util/Position.hpp"
2+
#include "config.hpp"
3+
4+
namespace Util {
5+
PTSDPosition PTSDPosition::operator+(const glm::vec2 vec2) const {
6+
return PTSDPosition(x + vec2.x, y + vec2.y);
7+
}
8+
9+
PTSDPosition PTSDPosition::FromSDL(int sdlx, int sdly) {
10+
return PTSDPosition{
11+
static_cast<float>(sdlx) -
12+
static_cast<float>(PTSD_Config::WINDOW_WIDTH) / 2.0F,
13+
static_cast<float>(sdly) -
14+
static_cast<float>(PTSD_Config::WINDOW_HEIGHT) / 2.0F};
15+
}
16+
17+
SDLPosition PTSDPosition::ToSDLPosition() const {
18+
return SDLPosition(
19+
static_cast<int>(this->x +
20+
static_cast<float>(PTSD_Config::WINDOW_WIDTH) / 2.0F),
21+
static_cast<int>(
22+
this->y + static_cast<float>(PTSD_Config::WINDOW_HEIGHT) / 2.0F));
23+
}
24+
} // namespace Util

test/PositionTest.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <gtest/gtest.h>
2+
3+
#include "Util/Position.hpp"
4+
#include "config.hpp"
5+
6+
constexpr float FLOAT_TO_INT_ERROR = 1.0F;
7+
8+
// NOLINTBEGIN(readability-magic-numbers)
9+
10+
TEST(CastingTest, PTSD2SDL) {
11+
float a = 4.32;
12+
float b = 65.32;
13+
auto ptsdpos = Util::PTSDPosition(a, b);
14+
auto sdlpos = ptsdpos.ToSDLPosition();
15+
EXPECT_NEAR(sdlpos.x,
16+
a + static_cast<float>(PTSD_Config::WINDOW_WIDTH) / 2.0F,
17+
FLOAT_TO_INT_ERROR);
18+
EXPECT_NEAR(sdlpos.y,
19+
b + static_cast<float>(PTSD_Config::WINDOW_HEIGHT) / 2.0F,
20+
FLOAT_TO_INT_ERROR);
21+
}
22+
23+
TEST(CastingTest, SDL2PTSD) {
24+
int a = 74;
25+
int b = 14;
26+
auto ptsdpos = Util::PTSDPosition::FromSDL(a, b);
27+
EXPECT_EQ(ptsdpos.x,
28+
static_cast<float>(a) -
29+
static_cast<float>(PTSD_Config::WINDOW_WIDTH) / 2.0F);
30+
EXPECT_EQ(ptsdpos.y,
31+
static_cast<float>(b) -
32+
static_cast<float>(PTSD_Config::WINDOW_HEIGHT) / 2.0F);
33+
}
34+
35+
TEST(CastingTest, AddingVec2) {
36+
float a = 32;
37+
float b = 23;
38+
float c = 69;
39+
float d = 53;
40+
auto p1 = Util::PTSDPosition(a, b);
41+
auto v2 = glm::vec2(c, d);
42+
auto result = p1 - v2;
43+
EXPECT_EQ(result.x, a - c);
44+
EXPECT_EQ(result.y, b - d);
45+
}
46+
47+
// NOLINTEND(readability-magic-numbers)

0 commit comments

Comments
 (0)