I’m new to SDL and this forum, so not sure if this is the right place to ask, feel free to remove if this is not appropriate. But looking at the source code of SDL_RenderDrawLinesF there is something that is bugging me and I can’t let it go
There is a line that checks for scaling and uses RenderDrawLinesWithRectsF in that case.
But when there is no scaling it skips to this loop.
for (i = 0; i < count; ++i) {
fpoints[i].x = points[i].x * renderer->scale.x;
fpoints[i].y = points[i].y * renderer->scale.y;
}
But if renderer->scale.x != 1.0f || renderer->scale.y != 1.0f is not true, that must mean they are in fact equal to 1.0f, so what’s the point of this loop? Wouldn’t just passing points down or doing a memcpy from points to fpoints be faster? I am just beginning with C and SDL, so I am probably not understanding it well. This same loop is present in the original non-float version, but my guess there it is a method to convert the point coordinates from int to float.
Well, that’s really two different operations, so it can’t really be compared.
For example :
int i = 100;
float f;
f = i * 1.0f;
The last line does exactly the same thing as doing :
f = (float)i * 1.0f;
C++ standard, paragraph 10, section 5 states that if either operand is float, the other shall be converted to float.
So asking if multiplying by 1.0f is faster than casting… well, the answer is no, since casting always happens, no matter what you do.
A compiler MAY optimize (int)var1 *= (const float)1.0f; into (float)var1, but don’t rely on it. Code should be written as to convey intent. You want to multiply, write a multiplication. You want to cast, write a cast. Leave the optimization to the compiler ; he knows much better than you what it’s doing.
Then probably it’s a copy/paste thing from SDL_RenderDrawLines to SDL_RenderDrawLinesF? I don’t think SDL_RenderDrawLinesF needs to convert to float, since SDL_FPoint is already floating point. Additionally, it would be nicer to use explicit conversion to make the intent clear.