aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.h
blob: 08239b8c012d027cc4405520204d08ccf95b2556 (plain) (blame)
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include <stdbool.h>

#include <raylib.h>
#include <raymath.h>
#include "raygui.h"

#ifndef UTIL_H
#define UTIL_H

#define FT_DEBUG_MODE

#define FT_NAMEMAX 256

// Memory management.
#define FT_MALLOC(size) malloc(size)
#define FT_CALLOC(nmemb, size) calloc(nmemb, size)
#define FT_REALLOC(ptr, size) realloc(ptr, size)
#define FT_REALLOCARRAY(ptr, nmemb, size) reallocarray(ptr, nmemb, size)
//#define FT_REALLOCARRAY(ptr, nmemb, size) realloc(ptr, nmemb * size)
#define FT_FREE(ptr) free(ptr)

// Errors.
#define ALLOCATION_ERROR TraceLog(LOG_ERROR, "Allocation error in %s:%d", \
                                  __FILE__, __LINE__)

// Typedef hackery.
typedef struct Game Game;
typedef struct World World;

typedef enum FTError {
  FTERROR = -1,
  FTSUCCESS = 0
} FTError;

// Bit shit.
#define SET_BIT(b, n) (b | (0x1 << n))
#define CLEAR_BIT(b, n) (b & ~(0x1 << n))
#define IS_BIT_SET(b, n) ((b >> n) & 0x1)
#define TOGGLE_BIT(b, n) (b ^ (0x1 << n))
#define HAS_FLAG(v, f) ((v & f) == f)

// https://en.wikipedia.org/wiki/Linear_congruential_generator
#define FT_RANDOM16(seed) (seed = (75 * seed + 74) % 65537)
#define FT_RANDOM32(seed) (seed = (1664525 * seed + 1013904223) % 4294967296)

// Helpful for debugging
#define PRINT_VECTOR2(v) printf("%f %f\n", v.x, v.y)
#define PRINT_VECTOR3(v) printf("%f %f %f\n", v.x, v.y, v.z)

#endif