简体   繁体   中英

SDL2 draw and fill shapes

How can I draw different shapes (other than rectangle and line) and fill them using SDL2 in C++? I tried using SDL2_gfx, but I couldn't get SDL2_gfx compiled, so I couldn't try it. I found a compiled SDL_gfx but since I'm using renderer not surfaces I can't use it. Drawing polygons is simple, I can just calculate the point and draw lines, but how do I fill them? And how do I draw circle?

If anyone has a compiled SDL2_gfx (same way like SDL2_ttf and SDL2_image) then maybe you could send it to me?

If you want to fill a polygon a quick google search gives an efficient algorithm here . Or copying the way SDL2_gfx does it is possible, as you have access to the sources?

For drawing a circle, there is the Midpoint Circle algorithm with even some code given:

void DrawCircle(int x0, int y0, int radius)
{
    int x = 0, y = radius;
    int dp = 1 - radius;
    do
    {
        if (dp < 0)
            dp = dp + 2 * (++x) + 3;
        else
            dp = dp + 2 * (++x) - 2 * (--y) + 5;

        putpixel(x0 + x, y0 + y, 15);     //For the 8 octants
        putpixel(x0 - x, y0 + y, 15);
        putpixel(x0 + x, y0 - y, 15);
        putpixel(x0 - x, y0 - y, 15);
        putpixel(x0 + y, y0 + x, 15);
        putpixel(x0 - y, y0 + x, 15);
        putpixel(x0 + y, y0 - x, 15);
        putpixel(x0 - y, y0 - x, 15);

    } while (x < y);
}

But what should really be done is seek help in solving the problem of compiling SDL2_gfx. Worst comes to worst, can't you just put the source files of SDL2_gfx in your project directly? It's only four files that you can put in a subfolder of your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM