add utilities
This commit is contained in:
parent
8679fa5aa2
commit
ab840a903c
38
src/generic_vector.h
Normal file
38
src/generic_vector.h
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
|
||||||
|
#define DEFINE_VECTOR_STRUCT(vector_name, item_t) \
|
||||||
|
struct vector_name { \
|
||||||
|
item_t *items; \
|
||||||
|
size_t len; \
|
||||||
|
size_t capacity; \
|
||||||
|
};
|
||||||
|
|
||||||
|
#define DEFINE_VECTOR_ENSURE_METHOD_CAPACITY(func_name, list_t, item_t) \
|
||||||
|
void func_name(list_t *list, size_t expected_capacity) \
|
||||||
|
{ \
|
||||||
|
if (list->capacity < expected_capacity) { \
|
||||||
|
size_t larger_capacity = MAX(MAX(list->capacity*2, expected_capacity), 4); \
|
||||||
|
item_t *larger_items = realloc(list->items, larger_capacity * sizeof(item_t)); \
|
||||||
|
if (larger_items == NULL) { \
|
||||||
|
PANIC_OOM(); \
|
||||||
|
} \
|
||||||
|
list->items = larger_items; \
|
||||||
|
list->capacity = larger_capacity; \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define DEFINE_VECTOR_METHOD_FREE(func_name, list_t) \
|
||||||
|
void func_name(list_t *list) \
|
||||||
|
{ \
|
||||||
|
free(list->items); \
|
||||||
|
list->len = 0; \
|
||||||
|
list->capacity = 0; \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define DEFINE_VECTOR_METHOD_CLEAR_RETAINING_CAPACITY(func_name, list_t) \
|
||||||
|
void func_name(list_t *list) \
|
||||||
|
{ \
|
||||||
|
list->len = 0; \
|
||||||
|
}
|
||||||
@ -36,3 +36,4 @@ extern struct Resources g_resources;
|
|||||||
|
|
||||||
Color rgb(uint8_t r, uint8_t g, uint8_t b);
|
Color rgb(uint8_t r, uint8_t g, uint8_t b);
|
||||||
Color hex(const char *str);
|
Color hex(const char *str);
|
||||||
|
int signf(float value);
|
||||||
|
|||||||
57
src/rect.c
Normal file
57
src/rect.c
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
#include "rect.h"
|
||||||
|
|
||||||
|
float rect_get_left(Rectangle rect)
|
||||||
|
{
|
||||||
|
return rect.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
float rect_get_right(Rectangle rect)
|
||||||
|
{
|
||||||
|
return rect.x + rect.width;
|
||||||
|
}
|
||||||
|
|
||||||
|
float rect_get_top(Rectangle rect)
|
||||||
|
{
|
||||||
|
return rect.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
float rect_get_bottom(Rectangle rect)
|
||||||
|
{
|
||||||
|
return rect.y + rect.height;
|
||||||
|
}
|
||||||
|
|
||||||
|
Rectangle rect_add_position(Rectangle rect, Vector2 pos)
|
||||||
|
{
|
||||||
|
rect.x += pos.x;
|
||||||
|
rect.y += pos.y;
|
||||||
|
return rect;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 rect_get_center(Rectangle rect)
|
||||||
|
{
|
||||||
|
return (Vector2){
|
||||||
|
.x = rect.x + rect.width / 2,
|
||||||
|
.y = rect.y + rect.height / 2,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
bool rect_is_overlap(Rectangle rect1, Rectangle rect2)
|
||||||
|
{
|
||||||
|
return CheckCollisionRecs(rect1, rect2);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool rect_is_inside_rect(Rectangle outer_rect, Rectangle inner_rect)
|
||||||
|
{
|
||||||
|
return rect_get_left(outer_rect) <= rect_get_left(inner_rect) &&
|
||||||
|
rect_get_right(outer_rect) >= rect_get_right(inner_rect) &&
|
||||||
|
rect_get_top(outer_rect) <= rect_get_top(inner_rect) &&
|
||||||
|
rect_get_bottom(outer_rect) >= rect_get_bottom(inner_rect);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool rect_is_inside_point(Rectangle outer_rect, Vector2 inner_point)
|
||||||
|
{
|
||||||
|
bool is_x_inside = rect_get_left(outer_rect) <= inner_point.x && inner_point.x <= rect_get_right(outer_rect);
|
||||||
|
bool is_y_inside = rect_get_top(outer_rect) <= inner_point.y && inner_point.y <= rect_get_bottom(outer_rect);
|
||||||
|
|
||||||
|
return is_x_inside && is_y_inside;
|
||||||
|
}
|
||||||
13
src/rect.h
Normal file
13
src/rect.h
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
|
||||||
|
float rect_get_left(Rectangle rect);
|
||||||
|
float rect_get_right(Rectangle rect);
|
||||||
|
float rect_get_top(Rectangle rect);
|
||||||
|
float rect_get_bottom(Rectangle rect);
|
||||||
|
Rectangle rect_add_position(Rectangle rect, Vector2 pos);
|
||||||
|
Vector2 rect_get_center(Rectangle rect);
|
||||||
|
bool rect_is_inside_point(Rectangle outer_rect, Vector2 inner_point);
|
||||||
|
bool rect_is_inside_rect(Rectangle outer_rect, Rectangle inner_rect);
|
||||||
|
bool rect_is_overlap(Rectangle rect1, Rectangle rect2);
|
||||||
11
src/utils.c
11
src/utils.c
@ -37,3 +37,14 @@ Color hex(const char *str)
|
|||||||
uint8_t b = parse_hex_byte(&str[5]);
|
uint8_t b = parse_hex_byte(&str[5]);
|
||||||
return rgb(r, g, b);
|
return rgb(r, g, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int signf(float value)
|
||||||
|
{
|
||||||
|
if (value > 0) {
|
||||||
|
return 1;
|
||||||
|
} else if (value < 0) {
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user