Skip to content

Instantly share code, notes, and snippets.

@Gumichan01
Last active August 20, 2024 14:29
Show Gist options
  • 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;
}
@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.

@dziobex
Copy link

dziobex commented Jun 17, 2024

Thank you!

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