Skip to content

Instantly share code, notes, and snippets.

@Gumichan01
Last active April 1, 2024 14:56
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Gumichan01/332c26f6197a432db91cc4327fcabb1c to your computer and use it in GitHub Desktop.
Save Gumichan01/332c26f6197a432db91cc4327fcabb1c to your computer and use it in GitHub Desktop.
[SDL2] Draw and fill a circle
int
SDL_RenderDrawCircle(SDL_Renderer * renderer, int x, int y, int radius)
{
int offsetx, offsety, d;
int status;
CHECK_RENDERER_MAGIC(renderer, -1);
offsetx = 0;
offsety = radius;
d = radius -1;
status = 0;
while (offsety >= offsetx) {
status += SDL_RenderDrawPoint(renderer, x + offsetx, y + offsety);
status += SDL_RenderDrawPoint(renderer, x + offsety, y + offsetx);
status += SDL_RenderDrawPoint(renderer, x - offsetx, y + offsety);
status += SDL_RenderDrawPoint(renderer, x - offsety, y + offsetx);
status += SDL_RenderDrawPoint(renderer, x + offsetx, y - offsety);
status += SDL_RenderDrawPoint(renderer, x + offsety, y - offsetx);
status += SDL_RenderDrawPoint(renderer, x - offsetx, y - offsety);
status += SDL_RenderDrawPoint(renderer, x - offsety, y - offsetx);
if (status < 0) {
status = -1;
break;
}
if (d >= 2*offsetx) {
d -= 2*offsetx + 1;
offsetx +=1;
}
else if (d < 2 * (radius - offsety)) {
d += 2 * offsety - 1;
offsety -= 1;
}
else {
d += 2 * (offsety - offsetx - 1);
offsety -= 1;
offsetx += 1;
}
}
return status;
}
int
SDL_RenderFillCircle(SDL_Renderer * renderer, int x, int y, int radius)
{
int offsetx, offsety, d;
int status;
CHECK_RENDERER_MAGIC(renderer, -1);
offsetx = 0;
offsety = radius;
d = radius -1;
status = 0;
while (offsety >= offsetx) {
status += SDL_RenderDrawLine(renderer, x - offsety, y + offsetx,
x + offsety, y + offsetx);
status += SDL_RenderDrawLine(renderer, x - offsetx, y + offsety,
x + offsetx, y + offsety);
status += SDL_RenderDrawLine(renderer, x - offsetx, y - offsety,
x + offsetx, y - offsety);
status += SDL_RenderDrawLine(renderer, x - offsety, y - offsetx,
x + offsety, y - offsetx);
if (status < 0) {
status = -1;
break;
}
if (d >= 2*offsetx) {
d -= 2*offsetx + 1;
offsetx +=1;
}
else if (d < 2 * (radius - offsety)) {
d += 2 * offsety - 1;
offsety -= 1;
}
else {
d += 2 * (offsety - offsetx - 1);
offsety -= 1;
offsetx += 1;
}
}
return status;
}
@Gumichan01
Copy link
Author

Gumichan01 commented Jul 30, 2016

I implemented these two function into SDL_render.c. But I couldn't push my commits in the mercurial repository (^.^').

SDL_RenderDrawCircle is based on the midpoint circle algorithm.
SDL_RenderFillCircle : the idea behind this function can be found here and here

@Gumichan01
Copy link
Author

You can remove CHECK_RENDERER_MAGIC, it was used because it was planned to be used in SDL2.

The source code is under the MIT Licence

@Wakeelfahmed
Copy link

where did chech_render_magic come from?

@mustardfrog
Copy link

where did chech_render_magic come from?

They already mentioned it here. Just comment that line out.

You can remove CHECK_RENDERER_MAGIC, it was used because it was planned to be used in SDL2.

In case you wanna see the source code:
https://github.com/libsdl-org/SDL/blob/228d9ae7915ec2c841605aedfaa627c441626740/src/render/SDL_render.c#L48

@Wakeelfahmed
Copy link

where did chech_render_magic come from?

They already mentioned it here. Just comment that line out.

You can remove CHECK_RENDERER_MAGIC, it was used because it was planned to be used in SDL2.

In case you wanna see the source code: https://github.com/libsdl-org/SDL/blob/228d9ae7915ec2c841605aedfaa627c441626740/src/render/SDL_render.c#L48

oh ok, i missed

@Rickodesea
Copy link

Thanks. This is really helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment