SFML / CSFML

Official binding of SFML for C
https://www.sfml-dev.org
Other
342 stars 121 forks source link

Add private conversion utilities between corresponding C and C++ data types #226

Closed ChrisThrasher closed 4 days ago

ChrisThrasher commented 7 months ago

https://github.com/SFML/CSFML/blob/fb9b3aeab729ef58ff704b87504bc28771770fa7/src/SFML/Graphics/RenderWindow.cpp#L194-L198

A common pattern in the implementation of the library is that we have two stack variables that represent the same thing but one type is from the sf:: namespace and the other is a CSFML public data type. Having both variables coexist leads to awkward variable names and requires rewriting the same logic to convert one type to another in many places.

I propose we add function overloads that convert between CSFML and SFML data types, in particular conversions from SFML to CSFML types which seems to be a more common.

sfVector2i toCType(const sf::Vector2i& vector)
{
    sfVector2i vector = {vector.x, vector.y};
    return vector;
}

The above snippet from RenderWindow.cpp would become a one-liner.

 return toCType(renderWindow->This.getPosition());
eXpl0it3r commented 4 months ago

Can we rely on the compiler to inline effectively or do we have to consider potential additional indirections & copies?

ChrisThrasher commented 4 months ago

https://en.cppreference.com/w/cpp/language/copy_elision

C++17's copy elision rules mean I don't think this would add any copies.

ChrisThrasher commented 4 days ago

Fixed in #273!