Skip to content

Commit 0eab021

Browse files
committed
[Render/Window] Reworked callbacks to be more readable
- Declared actual structures instead of using tuples - Made use of C++20 ranges in several places
1 parent 538d8ec commit 0eab021

File tree

19 files changed

+155
-159
lines changed

19 files changed

+155
-159
lines changed

include/RaZ/Data/Graph.inl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ void GraphNode<T>::addParents(GraphNode& node, OtherNodesTs&&... otherNodes) {
3434
if (&node == this)
3535
throw std::invalid_argument("Error: A graph node cannot be a parent of itself");
3636

37-
if (std::find(m_parents.cbegin(), m_parents.cend(), &node) == m_parents.cend())
37+
if (std::ranges::find(m_parents, &node) == m_parents.cend())
3838
m_parents.emplace_back(static_cast<T*>(&node));
3939

40-
if (std::find(node.m_children.cbegin(), node.m_children.cend(), this) == node.m_children.cend())
40+
if (std::ranges::find(node.m_children, this) == node.m_children.cend())
4141
node.m_children.emplace_back(static_cast<T*>(this));
4242

4343
// Stop the recursive unpacking if no more nodes are to be added as parents
@@ -62,10 +62,10 @@ void GraphNode<T>::addChildren(GraphNode& node, OtherNodesTs&&... otherNodes) {
6262
if (&node == this)
6363
throw std::invalid_argument("Error: A graph node cannot be a child of itself");
6464

65-
if (std::find(m_children.cbegin(), m_children.cend(), &node) == m_children.cend())
65+
if (std::ranges::find(m_children, &node) == m_children.cend())
6666
m_children.emplace_back(static_cast<T*>(&node));
6767

68-
if (std::find(node.m_parents.cbegin(), node.m_parents.cend(), this) == node.m_parents.cend())
68+
if (std::ranges::find(node.m_parents, this) == node.m_parents.cend())
6969
node.m_parents.emplace_back(static_cast<T*>(this));
7070

7171
// Stop the recursive unpacking if no more nodes are to be added as children
@@ -89,7 +89,7 @@ void GraphNode<T>::unlinkParent(const GraphNode& node) {
8989
if (&node == this)
9090
throw std::invalid_argument("Error: A graph node cannot be unlinked from itself");
9191

92-
const auto parentIt = std::find(m_parents.cbegin(), m_parents.cend(), &node);
92+
const auto parentIt = std::ranges::find(m_parents, &node);
9393
if (parentIt != m_parents.cend())
9494
m_parents.erase(parentIt);
9595
}
@@ -99,7 +99,7 @@ void GraphNode<T>::unlinkChild(const GraphNode& node) {
9999
if (&node == this)
100100
throw std::invalid_argument("Error: A graph node cannot be unlinked from itself");
101101

102-
const auto childIt = std::find(m_children.cbegin(), m_children.cend(), &node);
102+
const auto childIt = std::ranges::find(m_children, &node);
103103
if (childIt != m_children.cend())
104104
m_children.erase(childIt);
105105
}
@@ -125,7 +125,7 @@ NodeT& Graph<NodeT>::addNode(Args&& ... args) {
125125

126126
template <typename NodeT>
127127
void Graph<NodeT>::removeNode(NodeT& node) {
128-
const auto nodeIt = std::find_if(m_nodes.cbegin(), m_nodes.cend(), [&node] (const NodePtr& nodePtr) { return (nodePtr.get() == &node); });
128+
const auto nodeIt = std::ranges::find_if(m_nodes, [&node] (const NodePtr& nodePtr) noexcept { return (nodePtr.get() == &node); });
129129

130130
if (nodeIt == m_nodes.cend())
131131
throw std::invalid_argument("Error: The graph node to be removed does not exist");

include/RaZ/Math/Matrix.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class Matrix {
197197
/// \param mat Matrix to be compared with.
198198
/// \return True if matrices are [nearly] equal, else otherwise.
199199
constexpr bool operator==(const Matrix& mat) const noexcept;
200-
/// Output stream operator.
200+
/// Matrix output stream operator.
201201
/// \param stream Stream to output into.
202202
/// \param mat Matrix to be output.
203203
friend std::ostream& operator<< <>(std::ostream& stream, const Matrix& mat);

include/RaZ/Math/Quaternion.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class Quaternion {
116116
/// Matrix conversion operator; computes the rotation matrix represented by the quaternion.
117117
/// \return Rotation matrix.
118118
constexpr operator Mat4<T>() const noexcept { return computeMatrix(); }
119-
/// Output stream operator.
119+
/// Quaternion output stream operator.
120120
/// \param stream Stream to output into.
121121
/// \param quat Quaternion to be output.
122122
friend std::ostream& operator<< <>(std::ostream& stream, const Quaternion& quat);

include/RaZ/Math/Vector.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ class Vector {
222222
/// \tparam T2 Type to convert the vector's values to.
223223
/// \return Vector object of the new type.
224224
template <typename T2> constexpr explicit operator Vector<T2, Size>() const noexcept;
225-
/// Output stream operator.
225+
/// Vector output stream operator.
226226
/// \param stream Stream to output into.
227227
/// \param vec Vector to be output.
228228
friend std::ostream& operator<< <>(std::ostream& stream, const Vector& vec);
@@ -234,16 +234,16 @@ class Vector {
234234
/// Element-wise value-vector addition operator (of the form val + vec).
235235
/// \tparam T Type of the vector's data.
236236
/// \tparam Size Vector's size.
237-
/// \param val Value to be added to the vectors's element.
238-
/// \param vec Vector to be additioned.
239-
/// \return Additioned vector.
237+
/// \param val Value to be added to the vector's elements.
238+
/// \param vec Vector to be added.
239+
/// \return Added vector.
240240
template <typename T, std::size_t Size>
241241
constexpr Vector<T, Size> operator+(T val, const Vector<T, Size>& vec) noexcept { return vec + val; }
242242

243243
/// Element-wise value-vector multiplication operator (of the form val * vec).
244244
/// \tparam T Type of the vector's data.
245245
/// \tparam Size Vector's size.
246-
/// \param val Value to be multiplied by the vectors's element.
246+
/// \param val Value to multiply the vector's elements by.
247247
/// \param vec Vector to be multiplied.
248248
/// \return Multiplied vector.
249249
template <typename T, std::size_t Size>

include/RaZ/Math/Vector.inl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ constexpr Vector<T, Size>& Vector<T, Size>::operator*=(ValT val) noexcept {
248248
template <typename T, std::size_t Size>
249249
constexpr Vector<T, Size>& Vector<T, Size>::operator/=(const Vector& vec) noexcept {
250250
if constexpr (std::is_integral_v<T>)
251-
assert("Error: Integer vector division by 0 is undefined." && (std::find(vec.m_data.cbegin(), vec.m_data.cend(), 0) == vec.m_data.cend()));
251+
assert("Error: Integer vector division by 0 is undefined." && (std::ranges::find(vec.m_data, 0) == vec.m_data.cend()));
252252

253253
for (std::size_t i = 0; i < Size; ++i)
254254
m_data[i] /= vec[i];

include/RaZ/Render/ShaderProgram.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ShaderProgram {
4545
/// Checks if an attribute has been set with the given uniform name.
4646
/// \param uniformName Uniform name to be checked.
4747
/// \return True if an attribute exists with the given name, false otherwise.
48-
bool hasAttribute(const std::string& uniformName) const noexcept;
48+
bool hasAttribute(const std::string& uniformName) const noexcept { return m_attributes.contains(uniformName); }
4949
/// Checks if an attribute has been set with the given uniform name and type.
5050
/// \tparam T Type to be checked.
5151
/// \param uniformName Uniform name to be checked.

include/RaZ/Render/Window.hpp

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ class RenderSystem;
1919
class Window;
2020
using WindowPtr = std::unique_ptr<Window>;
2121

22-
using KeyboardCallbacks = std::vector<std::tuple<int, std::function<void(float)>, Input::ActionTrigger, std::function<void()>>>;
23-
using MouseButtonCallbacks = std::vector<std::tuple<int, std::function<void(float)>, Input::ActionTrigger, std::function<void()>>>;
24-
using MouseScrollCallback = std::function<void(double, double)>;
25-
using MouseMoveCallback = std::tuple<double, double, std::function<void(double, double)>>;
26-
using InputActions = std::unordered_map<int, std::pair<std::function<void(float)>, Input::ActionTrigger>>;
27-
using InputCallbacks = std::tuple<KeyboardCallbacks, MouseButtonCallbacks, MouseScrollCallback, MouseMoveCallback, InputActions>;
28-
using CloseCallback = std::function<void()>;
29-
3022
enum class WindowSetting : unsigned int {
3123
FOCUSED = 1, ///< Forces the window to take the focus.
3224
RESIZABLE = 2, ///< Makes the window able to be resized, either by dragging the edges & corners or by maximizing it.
@@ -92,7 +84,7 @@ class Window {
9284
/// \param width New window width.
9385
/// \param height New window height.
9486
/// \note The width & height are to be considered just hints; the window manager remains responsible for the actual dimensions, which may be lower.
95-
/// This can notably happen when the requested window size exceeds what the screens can display. The actual window's size can be queried afterward.
87+
/// This can notably happen when the requested window size exceeds what the screens can display. The actual size can be queried afterward.
9688
/// \see getWidth(), getHeight()
9789
void resize(unsigned int width, unsigned int height);
9890
/// Sets the window in a fullscreen mode, taking the whole main monitor's screen.
@@ -155,7 +147,7 @@ class Window {
155147
/// Sets the action to be executed on window close.
156148
/// \param func Action to be executed when the window is closed.
157149
void setCloseCallback(std::function<void()> func);
158-
/// Associates all of the callbacks, making them active.
150+
/// Associates all the callbacks, making them active.
159151
void updateCallbacks() const;
160152
#if !defined(RAZ_NO_OVERLAY)
161153
/// Changes the overlay's enabled state.
@@ -178,6 +170,31 @@ class Window {
178170
~Window() { close(); }
179171

180172
private:
173+
struct KeyboardCallback {
174+
Keyboard::Key key;
175+
std::function<void(float)> actionPress;
176+
Input::ActionTrigger frequency;
177+
std::function<void()> actionRelease;
178+
};
179+
180+
struct MouseButtonCallback {
181+
Mouse::Button button;
182+
std::function<void(float)> actionPress;
183+
Input::ActionTrigger frequency;
184+
std::function<void()> actionRelease;
185+
};
186+
187+
struct MouseMoveCallback {
188+
double xPrevPos {};
189+
double yPrevPos {};
190+
std::function<void(double, double)> action;
191+
};
192+
193+
struct InputAction {
194+
std::function<void(float)> action;
195+
Input::ActionTrigger frequency;
196+
};
197+
181198
/// Processes actions corresponding to keyboard & mouse inputs.
182199
/// \param deltaTime Amount of time elapsed since the last frame.
183200
void processInputs(float deltaTime);
@@ -196,8 +213,12 @@ class Window {
196213
int m_posX {};
197214
int m_posY {};
198215

199-
InputCallbacks m_callbacks {};
200-
CloseCallback m_closeCallback {};
216+
std::vector<KeyboardCallback> m_keyboardCallbacks;
217+
std::vector<MouseButtonCallback> m_mouseButtonCallbacks;
218+
std::function<void(double, double)> m_mouseScrollCallback;
219+
MouseMoveCallback m_mouseMoveCallback;
220+
std::unordered_map<int, InputAction> m_inputActions;
221+
std::function<void()> m_closeCallback;
201222

202223
#if !defined(RAZ_NO_OVERLAY)
203224
Overlay m_overlay {};

include/RaZ/Utils/StrUtils.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ inline std::wstring toUppercaseCopy(std::wstring text) {
211211
/// \param text String to be trimmed.
212212
/// \return Reference to the trimmed string.
213213
inline std::string& trimLeft(std::string& text) {
214-
text.erase(text.begin(), std::find_if_not(text.begin(), text.end(), [] (unsigned char c) {
214+
text.erase(text.begin(), std::ranges::find_if_not(text, [] (unsigned char c) noexcept(noexcept(std::isspace(c))) {
215215
return std::isspace(c);
216216
}));
217217
return text;
@@ -221,7 +221,7 @@ inline std::string& trimLeft(std::string& text) {
221221
/// \param text Wide string to be trimmed.
222222
/// \return Reference to the trimmed wide string.
223223
inline std::wstring& trimLeft(std::wstring& text) {
224-
text.erase(text.begin(), std::find_if_not(text.begin(), text.end(), [] (wchar_t c) {
224+
text.erase(text.begin(), std::ranges::find_if_not(text, [] (wchar_t c) noexcept(noexcept(std::iswspace(c))) {
225225
return std::iswspace(c);
226226
}));
227227
return text;

src/RaZ/Render/Framebuffer.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "GL/glew.h" // Needed by TracyOpenGL.hpp
1111
#include "tracy/TracyOpenGL.hpp"
1212

13+
#include <ranges>
14+
1315
namespace Raz {
1416

1517
namespace {
@@ -96,7 +98,7 @@ void Framebuffer::addColorBuffer(Texture2DPtr texture, unsigned int index) {
9698
if (texture->getColorspace() == TextureColorspace::DEPTH || texture->getColorspace() == TextureColorspace::INVALID)
9799
throw std::invalid_argument("Error: Invalid color buffer");
98100

99-
const auto bufferIt = std::find_if(m_colorBuffers.cbegin(), m_colorBuffers.cend(), [&texture, index] (const auto& colorBuffer) {
101+
const auto bufferIt = std::ranges::find_if(m_colorBuffers, [&texture, index] (const auto& colorBuffer) noexcept {
100102
return (texture == colorBuffer.first && index == colorBuffer.second);
101103
});
102104

@@ -111,10 +113,9 @@ void Framebuffer::removeTextureBuffer(const Texture2DPtr& texture) {
111113
if (texture == m_depthBuffer) {
112114
m_depthBuffer.reset();
113115
} else {
114-
const auto bufferIt = std::remove_if(m_colorBuffers.begin(), m_colorBuffers.end(), [&texture] (const auto& buffer) {
116+
std::erase_if(m_colorBuffers, [&texture] (const auto& buffer) noexcept {
115117
return (texture == buffer.first);
116118
});
117-
m_colorBuffers.erase(bufferIt, m_colorBuffers.end());
118119
}
119120

120121
mapBuffers();
@@ -131,7 +132,7 @@ void Framebuffer::resizeBuffers(unsigned int width, unsigned int height) {
131132
if (m_depthBuffer)
132133
m_depthBuffer->resize(width, height);
133134

134-
for (const auto& [colorBuffer, _] : m_colorBuffers)
135+
for (const Texture2DPtr& colorBuffer : m_colorBuffers | std::views::keys)
135136
colorBuffer->resize(width, height);
136137
}
137138

src/RaZ/Render/RenderGraph.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "RaZ/Math/Transform.hpp"
2-
#include "RaZ/Render/Camera.hpp"
32
#include "RaZ/Render/MeshRenderer.hpp"
43
#include "RaZ/Render/RenderGraph.hpp"
54
#include "RaZ/Render/RenderSystem.hpp"
@@ -11,7 +10,7 @@
1110
namespace Raz {
1211

1312
bool RenderGraph::isValid() const {
14-
return std::all_of(m_nodes.cbegin(), m_nodes.cend(), [] (const std::unique_ptr<RenderPass>& renderPass) {
13+
return std::ranges::all_of(m_nodes, [] (const std::unique_ptr<RenderPass>& renderPass) {
1514
return renderPass->isValid();
1615
});
1716
}

0 commit comments

Comments
 (0)