I'm using SDL and I need to quickly fill a lot of polygons (fake 3D walls, made linking calculated points with the SDL_RenderDrawLine() function, that I need to colour).

I tried to implement the filledPolygon() function from SDL_gfx.h but since I didn't see any improvement, I gave up.

I also searched for a multithreading implementation but after some tries I understood that it wasn't the best way to deal with my problem.

Finally, I tried to make a for loop where I call SDL_RenderDrawLines() every time.

Yes, I know that's the worst thing that I could do (indeed I ascertained that whats slowing down my program is that continuous function calling in the loop) but I don't really know what to do.

What's the best way to accomplish flood filling in SDL2?

I'm using SDL_RENDERER_ACCELERATED flag and I need to render even 150/200 polygons without any problem. With the actual code a single polygon placement is capable of slowing by 4 times my program so I can barely place a small polygon without making the program useless.

Thanks in advance for any helpful answer.

  • 1
    \$\begingroup\$ Do you really need a flood fill, or do you need a filled polygon? The latter is simpler than a general flood fill: for each row of pixels, find the leftmost & rightmost columns that intersect the polygon, then fill the full span between them (For a polygon that's monotone along the y axis, that is. For non-monotone polygons it gets more complex, but we can break them into simpler parts). You can also dive deeper into the theory of software rasterization eg. here \$\endgroup\$ – DMGregory Apr 18 '17 at 4:31
  • \$\begingroup\$ @DMGregory thank you for your answer.I made fake 3d walls but they're not solid so I want to fill them with random colors. I took a look at software rasterizing but I think that in some manner I'm already doing it because I loop through the polygon from the left to the right filling it with vertical lines till the polygon is completely filled and this is very laggy due to the slowness of RenderDrawLine function so what would be the benefit from rasterizing? I would still need to use the same function in a loop right? \$\endgroup\$ – Fiorenzo Apr 18 '17 at 11:59
  • \$\begingroup\$ Probably I need to optimize the loop (or find another function instead of SDL default one) but I don't know how.If you need the code I will provide it but don't expect anything special it's just a short for loop with RenderDrawLines call in it. \$\endgroup\$ – Fiorenzo Apr 18 '17 at 11:59
  • \$\begingroup\$ Is switching to SDL+OpenGL an option? \$\endgroup\$ – Pharap Apr 25 '17 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.