circle rendering, might change to make more efficient
This commit is contained in:
parent
57ff3302ad
commit
092908819d
4 changed files with 84 additions and 0 deletions
21
include/arc/graphics/circle.h
Normal file
21
include/arc/graphics/circle.h
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#ifndef ARC_GRAPHICS_CIRCLE_H_
|
||||
#define ARC_GRAPHICS_CIRCLE_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "arc/graphics/color.h"
|
||||
#include "arc/graphics/renderer.h"
|
||||
#include "arc/math/circle.h"
|
||||
#include <stdint.h>
|
||||
|
||||
void ARC_Circle_Render(ARC_Circle *circle, 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_RECT_H_
|
||||
20
include/arc/math/circle.h
Normal file
20
include/arc/math/circle.h
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#ifndef ARC_MATH_CIRCLE_H_
|
||||
#define ARC_MATH_CIRCLE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct ARC_Circle {
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
int32_t r;
|
||||
} ARC_Circle;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // ARC_MATH_CIRCLE_H_
|
||||
43
src/graphics/sdl/circle.c
Normal file
43
src/graphics/sdl/circle.c
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#ifdef ARC_SDL
|
||||
#include "arc/graphics/circle.h"
|
||||
#include "arc/graphics/sdl/renderer.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
//Modified from https://stackoverflow.com/questions/38334081/how-to-draw-circles-arcs-and-vector-graphics-in-sdl
|
||||
void ARC_Circle_Render(ARC_Circle *circle, ARC_Renderer *renderer, ARC_Color *color){
|
||||
SDL_SetRenderDrawColor((SDL_Renderer *)renderer, color->r, color->g, color->b, color->a);
|
||||
|
||||
int32_t diameter = (circle->r * 2);
|
||||
|
||||
int32_t x = (circle->r - 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
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, circle->x + x, circle->y - y);
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, circle->x + x, circle->y + y);
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, circle->x - x, circle->y - y);
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, circle->x - x, circle->y + y);
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, circle->x + y, circle->y - x);
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, circle->x + y, circle->y + x);
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, circle->x - y, circle->y - x);
|
||||
SDL_RenderDrawPoint((SDL_Renderer *)renderer, circle->x - y, circle->y + x);
|
||||
|
||||
if(error <= 0){
|
||||
++y;
|
||||
error += ty;
|
||||
ty += 2;
|
||||
}
|
||||
|
||||
if(error > 0){
|
||||
--x;
|
||||
tx += 2;
|
||||
error += (tx - diameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // ARC_SDL
|
||||
0
src/math/circle.c
Normal file
0
src/math/circle.c
Normal file
Loading…
Add table
Add a link
Reference in a new issue