CSFML icon indicating copy to clipboard operation
CSFML copied to clipboard

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

Open ChrisThrasher opened this issue 1 year ago • 2 comments

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());

ChrisThrasher avatar Feb 04 '24 18:02 ChrisThrasher

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

eXpl0it3r avatar Apr 15 '24 21:04 eXpl0it3r

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 avatar Apr 15 '24 21:04 ChrisThrasher