-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
EnumFlags
, replace all raw bitsets.
- Loading branch information
Showing
12 changed files
with
70 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
#include <bave/core/enum_t.hpp> | ||
#include <bitset> | ||
|
||
namespace bave { | ||
/// \brief Enum-indexed wrapper around std::bitset. | ||
template <EnumT E, std::size_t Size = std::size_t(E::eCOUNT_)> | ||
class EnumFlags : public std::bitset<Size> { | ||
public: | ||
using std::bitset<Size>::bitset; | ||
|
||
/// \brief Constructor taking multiple flags. | ||
/// \param to_set flags to set. | ||
template <std::same_as<E>... T> | ||
EnumFlags(T const... to_set) { | ||
(this->set(to_set), ...); | ||
} | ||
|
||
/// \brief Test if a flag is set. | ||
/// \param e Flag to test for. | ||
/// \returns true if set. | ||
[[nodiscard]] auto test(E const e) const -> bool { return std::bitset<Size>::test(static_cast<std::size_t>(e)); } | ||
/// \brief Set a flag. | ||
/// \param e Flag to set. | ||
void set(E const e) { std::bitset<Size>::set(static_cast<std::size_t>(e)); } | ||
/// \brief Reset a flag. | ||
/// \param e Flag to reset. | ||
void reset(E const e) { std::bitset<Size>::reset(static_cast<std::size_t>(e)); } | ||
|
||
[[nodiscard]] auto operator[](E const e) -> decltype(auto) { return std::bitset<Size>::operator[](static_cast<std::size_t>(e)); } | ||
[[nodiscard]] auto operator[](E const e) const -> decltype(auto) { return std::bitset<Size>::operator[](static_cast<std::size_t>(e)); } | ||
}; | ||
} // namespace bave |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
#pragma once | ||
#include <bave/core/enum_flags.hpp> | ||
#include <bave/input/key.hpp> | ||
#include <bitset> | ||
|
||
namespace bave { | ||
/// \brief Stateful wrapper for keys held down over multiple frames. | ||
struct KeyState { | ||
static constexpr auto max_keys_v = static_cast<std::size_t>(Key::eCOUNT_); | ||
|
||
std::bitset<max_keys_v> held_keys{}; | ||
EnumFlags<Key> held_keys{}; | ||
|
||
/// \brief Check if a key is held down (pressed). | ||
/// \brief Check if a key is pressed (held down). | ||
/// \param key The key to check for. | ||
/// \returns true if key has been pressed but not yet released. | ||
/// \pre key must be non-negative and less than Key::eCOUNT_. | ||
[[nodiscard]] auto is_held(Key const key) const -> bool { return held_keys.test(static_cast<std::size_t>(key)); } | ||
[[nodiscard]] auto is_pressed(Key const key) const -> bool { return held_keys.test(key); } | ||
}; | ||
} // namespace bave |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,9 @@ | ||
#pragma once | ||
#include <bave/core/make_bitset.hpp> | ||
#include <bave/core/enum_flags.hpp> | ||
|
||
namespace bave { | ||
enum class Mod : int { eShift, eCtrl, eAlt, eSuper, eCapsLock, eNumLock, eCOUNT_ }; | ||
|
||
/// \brief Bit flags for modifier keys. | ||
using KeyMods = std::bitset<static_cast<std::size_t>(Mod::eCOUNT_)>; | ||
|
||
/// \brief Create KeyMods with mods set. | ||
/// \param mods mods to set. | ||
/// \returns KeyMods with mods set. | ||
template <std::same_as<Mod>... I> | ||
auto make_key_mods(I const... mods) -> KeyMods { | ||
return make_bitset<KeyMods>(mods...); | ||
} | ||
using KeyMods = EnumFlags<Mod>; | ||
} // namespace bave |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters