diff --git a/CMakeLists.txt b/CMakeLists.txt index 77dea5f..adb1808 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,6 +62,7 @@ set(ARCHEUS_STD_SOURCES src/std/defaults/config.c src/math/circle.c + src/math/obround.c src/math/rectangle.c src/math/vector2.c @@ -78,6 +79,7 @@ set(ARCHEUS_STD_SDL_SOURCES src/graphics/sdl/circle.c src/graphics/sdl/config.c src/graphics/sdl/line.c + src/graphics/sdl/obround.c src/graphics/sdl/rectangle.c src/graphics/sdl/renderer.c src/graphics/sdl/sprite.c diff --git a/include/arc/graphics/obround.h b/include/arc/graphics/obround.h new file mode 100644 index 0000000..d0f8a5a --- /dev/null +++ b/include/arc/graphics/obround.h @@ -0,0 +1,21 @@ +#ifndef ARC_GRAPHICS_OBROUND_H_ +#define ARC_GRAPHICS_OBROUND_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "arc/graphics/color.h" +#include "arc/graphics/renderer.h" +#include "arc/math/obround.h" +#include + +void ARC_Obround_Render(ARC_Obround *obround, ARC_Renderer *renderer, ARC_Color *color); + +// void ARC_Rect_RenderFill(ARC_Rect *rect, ARC_Renderer *renderer, ARC_Color *color); + +#ifdef __cplusplus +} +#endif + +#endif // !ARC_GRAPHICS_OBROUND_H_ \ No newline at end of file diff --git a/include/arc/math/obround.h b/include/arc/math/obround.h new file mode 100644 index 0000000..233417d --- /dev/null +++ b/include/arc/math/obround.h @@ -0,0 +1,22 @@ +#ifndef ARC_MATH_OBROUND_H_ +#define ARC_MATH_OBROUND_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct ARC_Obround { + int32_t x; + int32_t y; + int32_t r; + int32_t h; +} ARC_Obround; + +#ifdef __cplusplus +} +#endif + +#endif // ARC_MATH_OBROUND_H_ + diff --git a/src/graphics/sdl/obround.c b/src/graphics/sdl/obround.c new file mode 100644 index 0000000..36c0775 --- /dev/null +++ b/src/graphics/sdl/obround.c @@ -0,0 +1,46 @@ +#ifdef ARC_SDL +#include "arc/graphics/obround.h" +#include "arc/graphics/sdl/renderer.h" +#include + +//Modified from https://stackoverflow.com/questions/38334081/how-to-draw-circles-arcs-and-vector-graphics-in-sdl +void ARC_Obround_Render(ARC_Obround *obround, ARC_Renderer *renderer, ARC_Color *color){ + SDL_SetRenderDrawColor((SDL_Renderer *)renderer, color->r, color->g, color->b, color->a); + + int32_t diameter = (obround->r * 2); + + int32_t x = (obround->r - 1); + int32_t y = 0; + int32_t tx = 1; + int32_t ty = 1; + int32_t error = (tx - diameter); + + SDL_RenderDrawLine((SDL_Renderer *)renderer, obround->x - obround->r, obround->y - (obround->h / 2), obround->x - obround->r, obround->y + (obround->h / 2)); + SDL_RenderDrawLine((SDL_Renderer *)renderer, obround->x + obround->r, obround->y - (obround->h / 2), obround->x + obround->r, obround->y + (obround->h / 2)); + + while(x >= y){ + // Each of the following renders an octant of the circle + SDL_RenderDrawPoint((SDL_Renderer *)renderer, obround->x + x, obround->y - y - (obround->h / 2)); + SDL_RenderDrawPoint((SDL_Renderer *)renderer, obround->x + x, obround->y + y + (obround->h / 2)); + SDL_RenderDrawPoint((SDL_Renderer *)renderer, obround->x - x, obround->y - y - (obround->h / 2)); + SDL_RenderDrawPoint((SDL_Renderer *)renderer, obround->x - x, obround->y + y + (obround->h / 2)); + SDL_RenderDrawPoint((SDL_Renderer *)renderer, obround->x + y, obround->y - x - (obround->h / 2)); + SDL_RenderDrawPoint((SDL_Renderer *)renderer, obround->x + y, obround->y + x + (obround->h / 2)); + SDL_RenderDrawPoint((SDL_Renderer *)renderer, obround->x - y, obround->y - x - (obround->h / 2)); + SDL_RenderDrawPoint((SDL_Renderer *)renderer, obround->x - y, obround->y + x + (obround->h / 2)); + + if(error <= 0){ + ++y; + error += ty; + ty += 2; + } + + if(error > 0){ + --x; + tx += 2; + error += (tx - diameter); + } + } +} + +#endif // ARC_SDL \ No newline at end of file diff --git a/src/math/obround.c b/src/math/obround.c new file mode 100644 index 0000000..e69de29