rework timers, update overlap/intersect and other procedures, some other things i dont remember

This commit is contained in:
veclavtalica
2024-10-22 14:45:30 +03:00
parent a527036436
commit a22bcfd97e
6 changed files with 144 additions and 115 deletions

View File

@ -216,7 +216,7 @@ void render_sprite_batch(const Primitive2D primitives[],
#pragma GCC diagnostic pop
const Vec2 c = frect_center(sprite.rect);
const Vec2 c = center_rect(sprite.rect);
const Vec2 t = fast_cossine(sprite.rotation + (float)M_PI_4);
const Vec2 d = {
.x = t.x * sprite.rect.w * (float)M_SQRT1_2,
@ -231,7 +231,7 @@ void render_sprite_batch(const Primitive2D primitives[],
} else {
/* rotated non-square case*/
const Vec2 c = frect_center(sprite.rect);
const Vec2 c = center_rect(sprite.rect);
const Vec2 t = fast_cossine(sprite.rotation);
const Vec2 h = { sprite.rect.w / 2, sprite.rect.h / 2 };

View File

@ -197,49 +197,57 @@ bool strends(const char *str, const char *suffix) {
}
bool overlap_rect(const Recti *a, const Recti *b, Recti *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 };
/* TODO: have our own */
Recti overlap_recti(const Recti a, const Recti 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 };
bool intersection = SDL_IntersectRect(&a_sdl, &b_sdl, &result_sdl);
(void)SDL_IntersectRect(&a_sdl, &b_sdl, &result_sdl);
if (result != NULL)
*result = (Recti){ result_sdl.x, result_sdl.y, result_sdl.w, result_sdl.h };
return intersection;
return (Recti){ result_sdl.x, result_sdl.y, result_sdl.w, result_sdl.h };
}
bool overlap_frect(const Rect *a, const Rect *b, Rect *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 };
/* TODO: have our own */
Rect overlap_rect(const Rect a, const Rect 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 };
bool intersection = SDL_IntersectFRect(&a_sdl, &b_sdl, &result_sdl);
(void)SDL_IntersectFRect(&a_sdl, &b_sdl, &result_sdl);
if (result != NULL)
*result = (Rect){ result_sdl.x, result_sdl.y, result_sdl.w, result_sdl.h };
return intersection;
return (Rect){ result_sdl.x, result_sdl.y, result_sdl.w, result_sdl.h };
}
bool intersect_rect(const Recti *a, const Recti *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 };
/* TODO: have our own */
bool intersect_recti(const Recti a, const Recti 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 };
return SDL_HasIntersection(&a_sdl, &b_sdl);
}
bool intersect_frect(const Rect *a, const Rect *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 };
/* TODO: have our own */
bool intersect_rect(const Rect a, const Rect 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 };
return SDL_HasIntersectionF(&a_sdl, &b_sdl);
}
Rect to_frect(Recti rect) {
Recti to_recti(Rect rect) {
return (Recti) {
.h = (int32_t)rect.h,
.w = (int32_t)rect.w,
.x = (int32_t)rect.x,
.y = (int32_t)rect.y,
};
}
Rect to_rect(Recti rect) {
return (Rect) {
.h = (float)rect.h,
.w = (float)rect.w,
@ -249,7 +257,15 @@ Rect to_frect(Recti rect) {
}
Vec2 frect_center(Rect rect) {
Vec2i center_recti(Recti rect) {
return (Vec2i){
.x = rect.x + rect.w / 2,
.y = rect.y + rect.h / 2,
};
}
Vec2 center_rect(Rect rect) {
return (Vec2){
.x = rect.x + rect.w / 2,
.y = rect.y + rect.h / 2,
@ -257,25 +273,52 @@ Vec2 frect_center(Rect rect) {
}
void tick_timer(int *value) {
*value = MAX(*value - 1, 0);
int32_t timer_tick_frames(int32_t frames_left) {
SDL_assert(frames_left >= 0);
return MAX(frames_left - 1, 0);
}
void tick_ftimer(float *value) {
*value = MAX(*value - ((float)ctx.delta_time / (float)ctx.clocks_per_second), 0.0f);
float timer_tick_seconds(float seconds_left) {
SDL_assert(seconds_left >= 0);
return MAX(seconds_left - ((float)ctx.delta_time / (float)ctx.clocks_per_second), 0.0f);
}
bool repeat_ftimer(float *value, float at) {
*value -= (float)ctx.delta_time / (float)ctx.clocks_per_second;
if (*value < 0.0f) {
*value += at;
return true;
struct timer_elapse_frames_result timer_elapse_frames(int32_t frames_left, int32_t interval) {
SDL_assert(frames_left >= 0);
SDL_assert(interval > 0);
frames_left -= 1;
bool elapsed = false;
if (frames_left <= 0) {
elapsed = true;
frames_left += interval;
}
return false;
return (struct timer_elapse_frames_result) {
.elapsed = elapsed,
.frames_left = frames_left
};
}
struct timer_elapse_seconds_result timer_elapse_seconds(float seconds_left, float interval) {
SDL_assert(seconds_left >= 0);
SDL_assert(interval > 0);
seconds_left -= (float)ctx.delta_time / (float)ctx.clocks_per_second;
bool elapsed = false;
if (seconds_left <= 0.0f) {
elapsed = true;
seconds_left += interval;
}
return (struct timer_elapse_seconds_result) {
.elapsed = elapsed,
.seconds_left = seconds_left
};
}
/* TODO: handle utf8 */
char *expand_asterisk(const char *mask, const char *to) {
const char *offset = SDL_strchr(mask, '*');