-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
49 lines (43 loc) · 1.12 KB
/
utils.c
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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef uint32_t u32;
typedef uint8_t u8;
typedef struct {
u8 *bytes;
long size;
} Buffer;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
void saveBuffer(Buffer b, const char *filename) {
FILE *f = fopen(filename, "wb");
if (!f) {
fprintf(stderr, "Cannot open file: %s", filename);
exit(1);
}
fwrite(b.bytes, b.size, 1, f);
fclose(f);
}
Buffer readFile(const char *filename) {
FILE *f = fopen(filename, "rb");
if (!f) {
fprintf(stderr, "Cannot open file: %s", filename);
exit(1);
}
Buffer res = {};
fseek(f, 0, SEEK_END);
res.size = ftell(f);
fseek(f, 0, SEEK_SET);
res.bytes = malloc(res.size);
if (1 != fread(res.bytes, res.size, 1, f)) {
fclose(f);
return (Buffer){};
}
fclose(f);
return res;
}
static bool collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2,
int h2) {
return (MAX(x1, x2) < MIN(x1 + w1, x2 + w2)) &&
(MAX(y1, y2) < MIN(y1 + h1, y2 + h2));
}