New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Show off the power of the new variadic dimensions system #124
Comments
I'm creating a game and its engine. Right now, it only has an "initial commit" with an empty Window main{Size{Width{800_px}, Heigh{600_px}}}; Looking ahead to representing the positions of "entities" in the game's world, I decided on this library's Then came the work on v3.0.0, which allows user-defined dimensions. I came up with something like this to replace namespace dimension {
struct Display_length_tag {
static constexpr gsl::czstring<> name{"display length"};
static constexpr gsl::czstring<> abbreviation{"px"};
};
using Display_length = units::make_dimension<Display_length_tag>;
} // namespace dimension
namespace display_length {
// Represents a quantity of pixels.
template <class Arithmetic>
using Pixels = units::unit<
units::unit_conversion<std::ratio<1>, dimension::Display_length>,
Arithmetic>;
} // namespace display_length
using display_length::Pixels;
inline namespace literals {
inline namespace pixels_literals { //
constexpr Pixels<double> operator""_px(long double px) noexcept
{
return Pixels<double>{gsl::narrow_cast<double>(px)};
}
constexpr Pixels<int> operator""_px(unsigned long long px) noexcept
{
return Pixels<int>{gsl::narrow_cast<int>(px)};
}
} // namespace pixels_literals
} // namespace literals The user-defined dimension display length has a base unit of pixels, as indicated by its "px" abbreviation. Of course, following the good practices when using third-party libraries, this is done within my own namespace. I mirrored this library's namespace structure and use of facilities with some added conveniences. This definition of Having mirrored this library's structure, I noticed that I could have my own display area dimension, just like this library has an area dimension. namespace dimension {
using Display_area = units::dimension_pow<Display_length, std::ratio<2>>;
} // namespace dimension How might this be useful? Well, the immediate plan for my game is to be tile-based. This means that I'll want units that represent tiles. Maybe there is a better concept than "area" out there, because it doesn't align with the fact that tiles represent a fixed size (for example, 32x32 tiles represent an area of 32 width of pixels by 32 height of pixels. That's 1024 px2. But the concept of area doesn't embed the knowledge of a fixed width and height.) Anyways, this is how I represented it in code. namespace display_area {
// Represents a tiled area of pixels.
template <int W, int H = W>
using Tiles = units::unit<
units::unit_conversion<std::ratio<W * H>, dimension::Display_area>,
int>;
} // namespace display_area
using display_area::Tiles;
static_assert(Tiles<32>{1} == 32_px * 32_px); 1024 px2 isn't obviously useful to me, so I embed the width and height in non-type template arguments. Now the logic for drawing the game's world could, for example, use the I found it amazing that the library could let me define these expressive domain units so succinctly. This got me enamoured with the library, because I know that when you can express things clearly in your domain, you can come up with things that would otherwise be hard to come up with (I've read some examples of this, but I don't have anything at hand). So I took some time appreciating the beautiful code that this library allowed me to write, and thought further. Issue #103, where this, On the same thread, pixel is quite a contested term. But one can conventionally adopt a definition of pixel to mean "the smallest addressable element in an all points addressable display device" (Wikipedia) "that contribute to the displayed or sensed color when viewed at a distance" (Wikipedia). With this definition, it is possible to define units that can represent other meanings of pixels. For example, a subpixel may be represented by the unit using Subpixel = units::unit<
units::unit_conversion<std::ratio<1, 3>, dimension::Display_length>, int>; This is an expected functionality of units libraries. That's why we have (sub)multiples of |
sort of off topic but wouldn't display length be a function of the isometric viewing angle and distance? Or is the game view 90 degrees top-down? |
The title should read "show off." |
@JohelEGP This is a great example of the use of user-defined dimensions. I'd be happy to write it up, unless you prefer to do so yourself. Also, did you ever release your game? I'd rather play this than Cyberpunk 2077. |
Where should it go?
No. I'm still building the game engine. |
At the top of the README there's a |
@JohelEGP How can I access to |
You can't get their individual values from a specialization of |
@JohelEGP I thought so, thx. |
@JohelEGP @nholthaus Could be nice to add pixel type and its conversion to meters. Of course, pixel are not yet recognize by the SI consortium ^^ but for top-down simulation application this can be interesting. I'm doing a 2D car simulator (not yet public) using SFML library https://www.sfml-dev.org When I click on the GUI it returns to me the world coordinates (meter) and when I have to rendering trajectory I have to convert to pixel. SFML does not offer If units wants to convert automatically, pixel type to meter_t, this seems to me complicated because we have to pass the transfer matrix which has a dynamic state since the user change resize the windows, do zoom ... |
Yeah. Pixel can measure a virtual distance, so conversion to an actual, physical distance (or a simulation thereof) is application-dependent. |
We have user-definable variadic dimensions now, but we need to develop some case studies we can document to guide users on how to take advantage of that. We probably also want some macros to simplify including new dimensions and making sure traits and things are auto-generated when a new dimension is defined.
If anyone comes up with something cool, post it here or in a PR and I'll include it in the docs w/ attribution.
The text was updated successfully, but these errors were encountered: