diff options
author | P. J. McDermott <pj@pehjota.net> | 2021-08-05 01:56:17 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2021-08-05 02:03:44 (EDT) |
commit | 910c8c55521abc3b8ed415b9d5678e85abb3a408 (patch) | |
tree | bf46e7ed7c84ab84639cd5c476bfa202a46013a3 /src | |
parent | 1cce7a8f4bd2e72bb200261f87794ff804efcd69 (diff) | |
download | mazefight-910c8c55521abc3b8ed415b9d5678e85abb3a408.zip mazefight-910c8c55521abc3b8ed415b9d5678e85abb3a408.tar.gz mazefight-910c8c55521abc3b8ed415b9d5678e85abb3a408.tar.bz2 |
tk: Add circle check
Diffstat (limited to 'src')
-rw-r--r-- | src/tk/check.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/tk/check.c b/src/tk/check.c index e84d30a..c8e639c 100644 --- a/src/tk/check.c +++ b/src/tk/check.c @@ -18,6 +18,7 @@ */ #include <SDL.h> +#include <math.h> #include "../tk.h" #include "widget.h" @@ -97,6 +98,52 @@ _mftk_check_render_box(SDL_Rect *rect, SDL_Color *color, SDL_Renderer *renderer) } static int +_mftk_check_render_cir(SDL_Rect *rect, SDL_Color *color, SDL_Renderer *renderer) +{ + int e = 0; + int r; + int x; + int y; + int i; + + if (SDL_SetRenderDrawColor(renderer, + color->r, color->g, color->b, color->a) < 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Couldn't render widget: %s", + SDL_GetError()); + e = -1; + } + +#define _mftk_check_px(X, Y) \ + do { \ + if (SDL_RenderDrawPoint(renderer, \ + rect->x + r + X, rect->y + r + Y) < 0){\ + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \ + "Couldn't render widget: %s", \ + SDL_GetError()); \ + e = -1; \ + } \ + } while (0) + + r = rect->w / 2; + for (x = 0, y = r; y > x; ++x) { + y = round(sqrt((r-0.5)*(r-0.5) - (x-0.5)*(x-0.5)) + 0.5); + for (i = 0; i <= y; ++i) _mftk_check_px( x, i); + for (i = 0; i >= 0-y; --i) _mftk_check_px( x, i); + for (i = 0; i <= y; ++i) _mftk_check_px(0-x, i); + for (i = 0; i >= 0-y; --i) _mftk_check_px(0-x, i); + for (i = 0; i <= y; ++i) _mftk_check_px( i, x); + for (i = 0; i <= y; ++i) _mftk_check_px( i, 0-x); + for (i = 0; i >= 0-y; --i) _mftk_check_px( i, x); + for (i = 0; i >= 0-y; --i) _mftk_check_px( i, 0-x); + } + +#undef _mftk_check_px + + return e; +} + +static int _mftk_check_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) { struct mftk_check *c = (struct mftk_check *) w; @@ -111,6 +158,10 @@ _mftk_check_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) if (_mftk_check_render_box(&rect, c->butn_color, renderer) < 0){ e = -1; } + } else { + if (_mftk_check_render_cir(&rect, c->butn_color, renderer) < 0){ + e = -1; + } } if (c->state == SDL_TRUE) { @@ -123,6 +174,11 @@ _mftk_check_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) renderer) < 0) { e = -1; } + } else { + if (_mftk_check_render_cir(&rect, c->mark_color, + renderer) < 0) { + e = -1; + } } } |