aboutsummaryrefslogtreecommitdiff
path: root/src/util.h
blob: 78d530e51ef0121adb7dcb2abbc8b23c6537207b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef COMET_UTIL_H
#define COMET_UTIL_H

#include <time.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>

typedef struct {
    char *key;
    char *value;
} pair_t;

void pair_copy(pair_t *copy, const pair_t *pair);

void pair_free(pair_t *pair);

// Color representation normalized to [0, 1]
//
typedef struct {
    double r, g, b, a;
} color_t;

static inline color_t color_rgba(int r, int g, int b, int a)
{
    color_t color = { r / 255.0, g / 255.0, b / 255.0, a / 255.0 };
    return color;
}

static inline color_t color_rgb(int r, int g, int b)
{
    return color_rgba(r, g, b, 255);
}

char *color_to_string(color_t *color);

void color_print(FILE *stream, color_t *color);

struct timespec timespec_diff(struct timespec a, struct timespec b);

bool timespec_greater(struct timespec a, struct timespec b);

void timespec_print(FILE *stream, struct timespec *ts);

// Check if point (px, py) is inside a rectangle in (x, y), (x+w, y), (x, y+h) and (w+h, y+h)
bool check_rect(int px, int py, int x, int y, int w, int h);

// Check if point (px, py) is inside a circle of radius r and center (x, y)
bool check_circle(int px, int py, int x, int y, int r);

// Check if point (px, py) is inside a capsule in (x, y), (x+w, y), (x, y+h) and (w+h, y+h)
bool check_capsule(int px, int py, int x, int y, int w, int h);

char *strslice(const char *string, size_t start, size_t end);

char *strcopy(const char *string);

#endif