summaryrefslogtreecommitdiff
path: root/src/common.h
blob: 4d0df2974f4b63287f1283826aadc05e26d74f50 (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
59
60
61
#ifndef COMMON_H
#define COMMON_H

#include <stddef.h>  // for size_t

typedef unsigned int uint;

#define min(a, b)      (((a) < (b)) ? (a) : (b)) // choose smaller
#define max(a, b)      (((a) > (b)) ? (a) : (b)) // choose bigger
#define clamp(x, l, h) (min(max((x), (l)), (h))) // clamp x between a range
#define vxs(x0,y0, x1,y1)    ((x0)*(y1) - (x1)*(y0)) // vector cross product

// Overlap:  Determine whether the two number ranges overlap.
#define overlap(a0,a1,b0,b1) (min(a0,a1) <= max(b0,b1) && min(b0,b1) <= max(a0,a1))
// IntersectBox: Determine whether two 2D-boxes intersect.
#define intersectbox(x0,y0, x1,y1, x2,y2, x3,y3) (overlap(x0,x1,x2,x3) && overlap(y0,y1,y2,y3))
// PointSide: Determine which side of a line the point is on. Return value: <0, =0 or >0.
#define pointside(px,py, x0,y0, x1,y1) vxs((x1)-(x0), (y1)-(y0), (px)-(x0), (py)-(y0))
// Intersect: Calculate the point of intersection between two lines.
#define intersect(x1,y1, x2,y2, x3,y3, x4,y4) ((struct xy) { \
    vxs(vxs(x1,y1, x2,y2), (x1)-(x2), vxs(x3,y3, x4,y4), (x3)-(x4)) / vxs((x1)-(x2), (y1)-(y2), (x3)-(x4), (y3)-(y4)), \
    vxs(vxs(x1,y1, x2,y2), (y1)-(y2), vxs(x3,y3, x4,y4), (y3)-(y4)) / vxs((x1)-(x2), (y1)-(y2), (x3)-(x4), (y3)-(y4)) })


struct xy {
    float x, y;
};

struct xyz {
    float x, y, z;
};

#define COLOR_WHITE 0xFFFFFF
#define COLOR_BLACK 0x000000
#define COLOR_RED   0xFF0000
#define COLOR_GREEN 0x00FF00
#define COLOR_BLUE  0x0000FF
#define COLOR_LIGHTWHITE 0xF5F5F5
#define color(col) (((col)>>16) & 0xFF), (((col)>>8) & 0xFF), ((col) & 0xFF)

// macro wrapper for the sdl logging system
#include <SDL2/SDL.h>
#define LOG_APPLICATION SDL_LOG_CATEGORY_APPLICATION
#define LOG_ERROR       SDL_LOG_CATEGORY_ERROR
#define LOG_ASSERT      SDL_LOG_CATEGORY_ASSERT
#define LOG_SYSTEM      SDL_LOG_CATEGORY_SYSTEM
#define LOG_AUDIO       SDL_LOG_CATEGORY_AUDIO
#define LOG_VIDEO       SDL_LOG_CATEGORY_VIDEO
#define LOG_RENDER      SDL_LOG_CATEGORY_RENDER
#define LOG_INPUT       SDL_LOG_CATEGORY_INPUT
#define LOG_TEST        SDL_LOG_CATEGORY_TEST
#define LOG_CUSTOM      SDL_LOG_CATEGORY_CUSTOM

#define log(f, ...) SDL_Log(f, ##__VA_ARGS__)
#define log_critical(c, f, ...) SDL_LogCritical(c, f, ##__VA_ARGS__)
#define log_error(c, f, ...)    SDL_LogError(c, f, ##__VA_ARGS__)
#define log_warn(c, f, ...)     SDL_LogWarn(c, f, ##__VA_ARGS__)
#define log_info(c, f, ...)     SDL_LogInfo(c, f, ##__VA_ARGS__)
#define log_debug(c, f, ...)    SDL_LogDebug(c, f, ##__VA_ARGS__)
#define log_verbose(c, f, ...)  SDL_LogVerbose(c, f, ##__VA_ARGS__)
#endif