mirror of
https://github.com/lov3b/Pong.git
synced 2024-09-20 05:40:14 +02:00
58 lines
1.5 KiB
C
58 lines
1.5 KiB
C
//
|
|
// Created by love on 2024-01-18.
|
|
//
|
|
|
|
#pragma once
|
|
|
|
#include "SDL.h"
|
|
|
|
|
|
int roundUpToMultipleOfEight(int v) {
|
|
return (v + (8 - 1)) & -8;
|
|
}
|
|
|
|
// https://stackoverflow.com/questions/38334081/how-to-draw-circles-arcs-and-vector-graphics-in-sdl
|
|
void DrawCircle(SDL_Renderer *renderer, SDL_Point ¢er, int radius) {
|
|
// 35 / 49 is a slightly biased approximation of 1/sqrt(2)
|
|
const int arrSize = roundUpToMultipleOfEight(radius * 8 * 35 / 49);
|
|
SDL_Point points[arrSize];
|
|
int drawCount = 0;
|
|
|
|
const int32_t diameter = (radius * 2);
|
|
|
|
int32_t x = (radius - 1);
|
|
int32_t y = 0;
|
|
int32_t tx = 1;
|
|
int32_t ty = 1;
|
|
int32_t error = (tx - diameter);
|
|
|
|
while (x >= y) {
|
|
// Each of the following renders an octant of the circle
|
|
points[drawCount + 0] = {center.x + x, center.y - y};
|
|
points[drawCount + 1] = {center.x + x, center.y + y};
|
|
points[drawCount + 2] = {center.x - x, center.y - y};
|
|
points[drawCount + 3] = {center.x - x, center.y + y};
|
|
points[drawCount + 4] = {center.x + y, center.y - x};
|
|
points[drawCount + 5] = {center.x + y, center.y + x};
|
|
points[drawCount + 6] = {center.x - y, center.y - x};
|
|
points[drawCount + 7] = {center.x - y, center.y + x};
|
|
|
|
drawCount += 8;
|
|
|
|
if (error <= 0) {
|
|
++y;
|
|
error += ty;
|
|
ty += 2;
|
|
}
|
|
|
|
if (error > 0) {
|
|
--x;
|
|
tx += 2;
|
|
error += (tx - diameter);
|
|
}
|
|
}
|
|
|
|
SDL_RenderDrawPoints(renderer, points, drawCount);
|
|
}
|
|
|