techkaczynski.dev / code

summaryrefslogtreecommitdiff
path: root/common.h
diff options
context:
space:
mode:
Diffstat (limited to 'common.h')
-rw-r--r--common.h111
1 files changed, 111 insertions, 0 deletions
diff --git a/common.h b/common.h
new file mode 100644
index 0000000..a33e3c5
--- /dev/null
+++ b/common.h
@@ -0,0 +1,111 @@
+#ifndef COMMON_H
+#define COMMON_H
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <float.h>
+
+#include <raylib.h>
+#include <raymath.h>
+#include <rlgl.h>
+
+#include <box3d/box3d.h>
+#include <box3d/collision.h>
+#include <box3d/math_functions.h>
+#include <box3d/types.h>
+
+#include <luajit-2.1/luajit.h>
+
+#include "ext/hashmap.h"
+#include "ext/fuzzy_match.h"
+#include "ext/tinyfiledialogs/tinyfiledialogs.h"
+
+#define MIN(x, y) (((x) < (y)) ? (x) : (y))
+#define MAX(x, y) (((x) > (y)) ? (x) : (y))
+#define CLAMP(x, lo, hi) ((x) < (lo) ? (lo) : ((x) > (hi) ? (hi) : (x)))
+
+#define BIT(x) (1ULL << (x))
+#define FLAG_SET(n, f) ((n) |= (f))
+#define FLAG_CLEAR(n, f) ((n) &= ~(f))
+#define FLAG_TOGGLE(n, f) ((n) ^= (f))
+#define FLAG_CHECK(n, f) ((n) & (f))
+
+#define BYTES_TO_KB(b) ((b) / 1024)
+#define BYTES_TO_MB(b) ((b) / (1024 * 1024))
+#define KILOBYTES(kb) ((kb) * 1024)
+#define MEGABYTES(mb) ((mb) * 1024 * 1024)
+
+typedef uint8_t u8;
+typedef uint16_t u16;
+typedef uint32_t u32;
+typedef uint64_t u64;
+
+typedef int8_t i8;
+typedef int16_t i16;
+typedef int32_t i32;
+typedef int64_t i64;
+
+typedef float f32;
+typedef double f64;
+typedef const char *cstr;
+
+typedef u8 Flags8;
+typedef u32 Flags32;
+typedef u64 Flags64;
+
+typedef Rectangle Rect;
+typedef Vector2 Vec2;
+typedef Vector3 Vec3;
+
+/* POSIX/LINUX SPECIFIC PERFORMANCE MEASURE STUFF, DELETE LATER */
+#include <time.h>
+
+static inline double
+timespec_seconds(struct timespec start, struct timespec end) {
+ return (double)(end.tv_sec - start.tv_sec) +
+ (double)(end.tv_nsec - start.tv_nsec) / 1000000000.0;
+}
+
+#define PERF(...) \
+ do { \
+ struct timespec perf_start_; \
+ struct timespec perf_end_; \
+ double perf_elapsed_; \
+ \
+ clock_gettime(CLOCK_MONOTONIC, &perf_start_); \
+ __VA_ARGS__; \
+ clock_gettime(CLOCK_MONOTONIC, &perf_end_); \
+ \
+ perf_elapsed_ = timespec_seconds(perf_start_, perf_end_); \
+ fprintf(stderr, "%s: %.9f s (%.3f ms)\n", #__VA_ARGS__, perf_elapsed_, \
+ perf_elapsed_ * 1000.0); \
+ } while (0)
+
+#define PERF_N(count, ...) \
+ do { \
+ struct timespec perf_start_; \
+ struct timespec perf_end_; \
+ double perf_elapsed_; \
+ size_t perf_count_ = (count); \
+ \
+ clock_gettime(CLOCK_MONOTONIC, &perf_start_); \
+ for (size_t perf_i_ = 0; perf_i_ < perf_count_; perf_i_++) { \
+ __VA_ARGS__; \
+ } \
+ clock_gettime(CLOCK_MONOTONIC, &perf_end_); \
+ \
+ perf_elapsed_ = timespec_seconds(perf_start_, perf_end_); \
+ fprintf(stderr, \
+ "%s:\n" \
+ "\t%zu calls\n" \
+ "\t%.9f s total\n" \
+ "\t%.3f ns per call\n", \
+ #__VA_ARGS__, perf_count_, perf_elapsed_, \
+ perf_elapsed_ * 1000000000.0 / (double)perf_count_); \
+ } while (0)
+
+#endif