forked from tsoding/bezier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
141 lines (115 loc) · 2.82 KB
/
common.h
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifndef COMMON_H_
#define COMMON_H_
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <epoxy/gl.h>
#include <SDL.h>
#define GL_GLEXT_PROTOTYPES
#include <SDL_opengl.h>
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define SCREEN_FPS 60
#define DELTA_TIME_SEC (1.0f / SCREEN_FPS)
#define DELTA_TIME_MS ((Uint32) floorf(DELTA_TIME_SEC * 1000.0f))
#define MARKER_SIZE 15.0f
#define BACKGROUND_COLOR 0x353535FF
#define RED_COLOR 0xDA2C38FF
#define GREEN_COLOR 0x87C38FFF
#define BLUE_COLOR 0x748CABFF
#define HEX_COLOR(hex) \
((hex) >> (3 * 8)) & 0xFF, \
((hex) >> (2 * 8)) & 0xFF, \
((hex) >> (1 * 8)) & 0xFF, \
((hex) >> (0 * 8)) & 0xFF
int check_sdl_code(int code)
{
if (code < 0) {
fprintf(stderr, "SDL error: %s\n", SDL_GetError());
exit(1);
}
return code;
}
void *check_sdl_ptr(void *ptr)
{
if (ptr == NULL) {
fprintf(stderr, "SDL error: %s\n", SDL_GetError());
exit(1);
}
return ptr;
}
typedef struct {
float x;
float y;
} Vec2;
Vec2 vec2(float x, float y)
{
return (Vec2){x, y};
}
Vec2 vec2_sub(Vec2 a, Vec2 b)
{
return vec2(a.x - b.x, a.y - b.y);
}
Vec2 vec2_scale(Vec2 a, float s)
{
return vec2(a.x * s, a.y * s);
}
Vec2 vec2_add(Vec2 a, Vec2 b)
{
return vec2(a.x + b.x, a.y + b.y);
}
float vec2_length(Vec2 a)
{
return sqrtf(a.x * a.x + a.y * a.y);
}
float lerpf(float a, float b, float p)
{
return a + (b - a) * p;
}
Vec2 lerpv2(Vec2 a, Vec2 b, float p)
{
return vec2_add(a, vec2_scale(vec2_sub(b, a), p));
}
char *cstr_slurp_file(const char *file_path)
{
FILE *f = fopen(file_path, "r");
if (f == NULL) {
fprintf(stderr, "ERROR: Could not read file `%s`: %s\n",
file_path, strerror(errno));
exit(1);
}
if (fseek(f, 0, SEEK_END) < 0) {
fprintf(stderr, "ERROR: Could not read file `%s`: %s\n",
file_path, strerror(errno));
exit(1);
}
long m = ftell(f);
if (m < 0) {
fprintf(stderr, "ERROR: Could not read file `%s`: %s\n",
file_path, strerror(errno));
exit(1);
}
char *buffer = malloc(m + 1);
if (buffer == NULL) {
fprintf(stderr, "ERROR: Could not allocate memory for file: %s\n",
strerror(errno));
exit(1);
}
if (fseek(f, 0, SEEK_SET) < 0) {
fprintf(stderr, "ERROR: Could not read file `%s`: %s\n",
file_path, strerror(errno));
exit(1);
}
size_t n = fread(buffer, 1, m, f);
if (ferror(f)) {
fprintf(stderr, "ERROR: Could not read file `%s`: %s\n",
file_path, strerror(errno));
exit(1);
}
buffer[n] = '\0';
fclose(f);
return buffer;
}
#endif // COMMON_H_