diff --git a/apps/testgame/world.c b/apps/testgame/world.c index d5dfbd1..17bcd85 100644 --- a/apps/testgame/world.c +++ b/apps/testgame/world.c @@ -130,9 +130,9 @@ bool world_find_intersect_frect(struct world *world, t_frect rect, t_frect *inte if (intersection == NULL) { t_frect temp; - is_intersecting = intersect_frect(&rect, &tile_frect, &temp); + is_intersecting = overlap_frect(&rect, &tile_frect, &temp); } else { - is_intersecting = intersect_frect(&rect, &tile_frect, intersection); + is_intersecting = overlap_frect(&rect, &tile_frect, intersection); } if (is_intersecting) @@ -155,9 +155,9 @@ bool world_find_intersect_rect(struct world *world, t_rect rect, t_rect *interse if (intersection == NULL) { t_rect temp; - is_intersecting = intersect_rect(&rect, tile_rect, &temp); + is_intersecting = overlap_rect(&rect, tile_rect, &temp); } else { - is_intersecting = intersect_rect(&rect, tile_rect, intersection); + is_intersecting = overlap_rect(&rect, tile_rect, intersection); } if (is_intersecting) diff --git a/include/twn_util.h b/include/twn_util.h index 703ee1f..47bb2fb 100644 --- a/include/twn_util.h +++ b/include/twn_util.h @@ -109,9 +109,14 @@ _Alignas(16) } t_frect; -TWN_API bool intersect_rect(const t_rect *a, const t_rect *b, t_rect *result); +/* calculates the overlap of two rectangles and places it in result. */ +/* returns true if the rectangles are indeed intersecting. */ +TWN_API bool overlap_rect(const t_rect *a, const t_rect *b, t_rect *result); +TWN_API bool overlap_frect(const t_frect *a, const t_frect *b, t_frect *result); -TWN_API bool intersect_frect(const t_frect *a, const t_frect *b, t_frect *result); +/* returns true if two triangles are intersecting */ +TWN_API bool intersect_rect(const t_rect *a, const t_rect *b); +TWN_API bool intersect_frect(const t_frect *a, const t_frect *b); /* TODO: generics and specials (see m_to_fvec2() for an example)*/ TWN_API t_frect to_frect(t_rect rect); diff --git a/src/twn_util.c b/src/twn_util.c index c8574f5..2ff26ce 100644 --- a/src/twn_util.c +++ b/src/twn_util.c @@ -171,30 +171,50 @@ bool strends(const char *str, const char *suffix) { } -bool intersect_rect(const t_rect *a, const t_rect *b, t_rect *result) { +bool overlap_rect(const t_rect *a, const t_rect *b, t_rect *result) { SDL_Rect a_sdl = { a->x, a->y, a->w, a->h }; SDL_Rect b_sdl = { b->x, b->y, b->w, b->h }; SDL_Rect result_sdl = { 0 }; bool intersection = SDL_IntersectRect(&a_sdl, &b_sdl, &result_sdl); - *result = (t_rect){ result_sdl.x, result_sdl.y, result_sdl.w, result_sdl.h }; + if (result != NULL) + *result = (t_rect){ result_sdl.x, result_sdl.y, result_sdl.w, result_sdl.h }; + return intersection; } -bool intersect_frect(const t_frect *a, const t_frect *b, t_frect *result) { +bool overlap_frect(const t_frect *a, const t_frect *b, t_frect *result) { SDL_FRect a_sdl = { a->x, a->y, a->w, a->h }; SDL_FRect b_sdl = { b->x, b->y, b->w, b->h }; SDL_FRect result_sdl = { 0 }; bool intersection = SDL_IntersectFRect(&a_sdl, &b_sdl, &result_sdl); - *result = (t_frect){ result_sdl.x, result_sdl.y, result_sdl.w, result_sdl.h }; + if (result != NULL) + *result = (t_frect){ result_sdl.x, result_sdl.y, result_sdl.w, result_sdl.h }; + return intersection; } +bool intersect_rect(const t_rect *a, const t_rect *b) { + SDL_Rect a_sdl = { a->x, a->y, a->w, a->h }; + SDL_Rect b_sdl = { b->x, b->y, b->w, b->h }; + SDL_Rect result_sdl = { 0 }; + return SDL_IntersectRect(&a_sdl, &b_sdl, &result_sdl); +} + + +bool intersect_frect(const t_frect *a, const t_frect *b) { + SDL_FRect a_sdl = { a->x, a->y, a->w, a->h }; + SDL_FRect b_sdl = { b->x, b->y, b->w, b->h }; + SDL_FRect result_sdl = { 0 }; + return SDL_IntersectFRect(&a_sdl, &b_sdl, &result_sdl); +} + + t_frect to_frect(t_rect rect) { return (t_frect) { .h = (float)rect.h,