1 #ifndef COMMON_H_ 2 #define COMMON_H_ 3 4 #include <stdlib.h> 5 #include <stdio.h> 6 #include <stdint.h> 7 #include "./la.h" 8 9 #define SCREEN_WIDTH 800 10 #define SCREEN_HEIGHT 600 11 #define FPS 60 12 #define DELTA_TIME (1.0f / FPS) 13 #define CURSOR_OFFSET 0.13f 14 15 typedef int Errno; 16 17 #define SWAP(T, a, b) do { T t = a; a = b; b = t; } while (0) 18 19 #define return_defer(value) do { result = (value); goto defer; } while (0) 20 21 #define UNIMPLEMENTED(...) \ 22 do { \ 23 printf("%s:%d: UNIMPLEMENTED: %s \n", __FILE__, __LINE__, __VA_ARGS__); \ 24 exit(1); \ 25 } while(0) 26 #define UNREACHABLE(...) \ 27 do { \ 28 printf("%s:%d: UNREACHABLE: %s \n", __FILE__, __LINE__, __VA_ARGS__); \ 29 exit(1); \ 30 } while(0) 31 #define UNUSED(x) (void)(x) 32 33 #define DA_INIT_CAP 256 34 35 #define da_last(da) (assert((da)->count > 0), (da)->items[(da)->count - 1]) 36 37 #define da_move(dst, src) \ 38 do { \ 39 free((dst)->items); \ 40 (dst)->items = (src).items; \ 41 (dst)->count = (src).count; \ 42 (dst)->capacity = (src).capacity; \ 43 } while (0) 44 45 #define da_append(da, item) \ 46 do { \ 47 if ((da)->count >= (da)->capacity) { \ 48 (da)->capacity = (da)->capacity == 0 ? DA_INIT_CAP : (da)->capacity*2; \ 49 (da)->items = realloc((da)->items, (da)->capacity*sizeof(*(da)->items)); \ 50 assert((da)->items != NULL && "Buy more RAM lol"); \ 51 } \ 52 \ 53 (da)->items[(da)->count++] = (item); \ 54 } while (0) 55 56 #define da_append_many(da, new_items, new_items_count) \ 57 do { \ 58 if ((da)->count + new_items_count > (da)->capacity) { \ 59 if ((da)->capacity == 0) { \ 60 (da)->capacity = DA_INIT_CAP; \ 61 } \ 62 while ((da)->count + new_items_count > (da)->capacity) { \ 63 (da)->capacity *= 2; \ 64 } \ 65 (da)->items = realloc((da)->items, (da)->capacity*sizeof(*(da)->items)); \ 66 assert((da)->items != NULL && "Buy more RAM lol"); \ 67 } \ 68 memcpy((da)->items + (da)->count, new_items, new_items_count*sizeof(*(da)->items)); \ 69 (da)->count += new_items_count; \ 70 } while (0) 71 72 char *temp_strdup(const char *s); 73 void temp_reset(void); 74 75 typedef struct { 76 char *items; 77 size_t count; 78 size_t capacity; 79 } String_Builder; 80 81 #define SB_Fmt "%.*s" 82 #define SB_Arg(sb) (int) (sb).count, (sb).items 83 84 #define sb_append_buf da_append_many 85 #define sb_append_cstr(sb, cstr) \ 86 do { \ 87 const char *s = (cstr); \ 88 size_t n = strlen(s); \ 89 da_append_many(sb, s, n); \ 90 } while (0) 91 #define sb_append_null(sb) da_append_many(sb, "", 1) 92 93 #define sb_to_sv(sb) sv_from_parts((sb).items, (sb).count) 94 95 typedef struct { 96 const char **items; 97 size_t count; 98 size_t capacity; 99 } Files; 100 101 typedef enum { 102 FT_REGULAR, 103 FT_DIRECTORY, 104 FT_OTHER, 105 } File_Type; 106 107 Errno type_of_file(const char *file_path, File_Type *ft); 108 Errno read_entire_file(const char *file_path, String_Builder *sb); 109 Errno write_entire_file(const char *file_path, const char *buf, size_t buf_size); 110 Errno read_entire_dir(const char *dir_path, Files *files); 111 112 Vec4f hex_to_vec4f(uint32_t color); 113 114 #endif // COMMON_H_
