Standard setup for writing C inspired by Casey Muratori, Ryan Fleury, Mr. 4th Programmer, and others in the handmade community.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

279 строки
8.0 KiB

  1. #ifndef CORE_H
  2. #define CORE_H
  3. // cstdlib includes
  4. #include <math.h>
  5. #include <stdint.h> // necessary for int type sizes
  6. #include <stdio.h>
  7. #include <time.h> // TODO(djledda): try not to depend on this one
  8. // ### Misc macros ###
  9. #if ENABLE_ASSERT
  10. #define Assert(expression) if (!(expression)) {*(volatile int *)0 = 0;}
  11. #else
  12. #define Assert(expression)
  13. #endif
  14. #define function static
  15. #define global static
  16. #define local_persist static
  17. // ### Types ###
  18. typedef int8_t int8;
  19. typedef int16_t int16;
  20. typedef int32_t int32;
  21. typedef int64_t int64;
  22. typedef uint8_t uint8;
  23. typedef uint16_t uint16;
  24. typedef uint32_t uint32;
  25. typedef uint64_t uint64;
  26. typedef uint8_t byte;
  27. typedef float real32;
  28. typedef double real64;
  29. // ### Sizes and Numbers ###
  30. #define Bytes(n) (n)
  31. #define Kilobytes(n) (n << 10)
  32. #define Megabytes(n) (n << 20)
  33. #define Gigabytes(n) (((uint64)n) << 30)
  34. #define Terabytes(n) (((uint64)n) << 40)
  35. #define Thousand(n) ((n)*1000)
  36. #define Million(n) ((n)*1000000)
  37. #define Billion(n) ((n)*1000000000LL)
  38. #define ArrayCount(arr) (sizeof(arr) / sizeof((arr)[0]))
  39. // ### Arenas ###
  40. struct Arena {
  41. void *memory;
  42. size_t capacity;
  43. size_t head;
  44. };
  45. struct Scratch {
  46. Arena *arena;
  47. size_t start;
  48. };
  49. void *pushSize(Arena *arena, size_t bytes);
  50. void *pushSizeFill(Arena *arena, size_t bytes, byte fill);
  51. Arena *arenaAlloc(size_t capacity);
  52. void arenaFree(Arena *arena);
  53. void arenaFreeFrom(Arena *arena, size_t pos);
  54. void arenaPopTo(Arena *arena, void *pos);
  55. void initialiseCore();
  56. Scratch scratchStart(Arena **conflicts, size_t conflictCount);
  57. void scratchEnd(Scratch scratch);
  58. #define PushArray(arena, type, size) (type *)pushSize(arena, sizeof(type) * (size))
  59. #define PushArrayZero(arena, type, size) (type *)pushSizeFill(arena, sizeof(type) * (size), 0)
  60. #define PushStruct(arena, type) (type *)pushSize(arena, sizeof(type))
  61. #define PushStructZero(arena, type) (type *)pushSizeFill(arena, sizeof(type), 0)
  62. // ### Vectors ###
  63. template <typename T>
  64. union Vector2 {
  65. struct {
  66. T x;
  67. T y;
  68. };
  69. T vec[2];
  70. };
  71. template <typename T>
  72. inline function Vector2<T> vec2(T x, T y) {
  73. Vector2<T> result = {0};
  74. result.x = x;
  75. result.y = y;
  76. return result;
  77. }
  78. template <typename T>
  79. union Vector3 {
  80. struct {
  81. T x;
  82. T y;
  83. T z;
  84. };
  85. T vec[3];
  86. };
  87. template <typename T>
  88. inline function Vector3<T> vec3(T x, T y, T z) {
  89. Vector3<T> result = {0};
  90. result.x = x;
  91. result.y = y;
  92. result.z = z;
  93. return result;
  94. }
  95. template <typename T>
  96. union Vector4 {
  97. struct {
  98. T r;
  99. T g;
  100. T b;
  101. T a;
  102. };
  103. struct {
  104. T x;
  105. T y;
  106. T z;
  107. T w;
  108. };
  109. T vec[4];
  110. };
  111. template <typename T>
  112. inline function Vector4<T> vec4(T x, T y, T z, T w) {
  113. Vector4<T> result = {0};
  114. result.x = x;
  115. result.y = y;
  116. result.z = z;
  117. result.w = w;
  118. return result;
  119. }
  120. // ### Lists ###
  121. template <typename T>
  122. struct list {
  123. T* data;
  124. size_t length;
  125. size_t head;
  126. };
  127. #define PushList(arena, type, size) (list<type>{ PushArray(arena, type, size), size, 0 })
  128. #define PushListZero(arena, type, size) (list<type>{ PushArrayZero(arena, type, size), size, 0 })
  129. #define PushFullList(arena, type, size) (list<type>{ PushArray(arena, type, size), size, size })
  130. #define PushFullListZero(arena, type, size) (list<type>{ PushArrayZero(arena, type, size), size, size })
  131. template <typename T> T *appendList(list<T> *list, T element);
  132. template <typename T> void zeroList(list<T> *list);
  133. template <typename T> void zeroListFull(list<T> *list);
  134. template <typename T> list<T> listSlice(list<T> l, size_t start, size_t stop = 0);
  135. // ### Strings ###
  136. struct string {
  137. char *str;
  138. size_t length;
  139. };
  140. #define STB_SPRINTF_DECORATE(name) stb_##name // define this before including if you want to change the names
  141. #include "vendor/stb_sprintf.h"
  142. #define strlit(lit) (string{(char *)(lit), sizeof(lit) - 1})
  143. #define PushString(arena, length) (string{ (char *)pushSize(arena, length), (length) })
  144. #define PushStringFill(arena, length, characterByte) (string{ (char *)pushSizeFill(arena, length, characterByte), (length) })
  145. string operator""_s(const char *cstrLiteral, size_t length);
  146. // C Strings
  147. const char *cstring(Arena *arena, list<char> buf);
  148. const char *cstring(Arena *arena, string str);
  149. size_t calcStringLen(const char *str);
  150. string strFromCString(Arena *arena, const char *str);
  151. bool strEql(string s1, string s2);
  152. bool strStartsWith(string str, string testStr);
  153. bool stringContains(string str, char c);
  154. string strReverse(Arena *arena, string str);
  155. string strSlice(string str, size_t start, size_t stop = 0);
  156. string strSlice(char *data, size_t start, size_t stop = 0);
  157. list<string> strSplit(Arena *arena, string splitStr, string inputStr);
  158. string strPrintfv(Arena *arena, const char *fmt, va_list args);
  159. string strPrintf(Arena *arena, const char *fmt, ...);
  160. struct ParsePositiveIntResult { uint8 result; bool valid; };
  161. ParsePositiveIntResult parsePositiveInt(string str, size_t *lengthPointer);
  162. struct ParsePositiveReal32Result { real32 result; bool valid; };
  163. ParsePositiveReal32Result parsePositiveReal32(Arena *arena, string str, size_t *lengthPointer);
  164. inline function bool isNumeric(char c);
  165. // ### Cmdline ###
  166. list<string> getArgs(Arena *arena, int argc, char **argv);
  167. // ### Time ###
  168. typedef uint64 UnixTimestamp;
  169. typedef tm Timestamp;
  170. UnixTimestamp getSystemUnixTime();
  171. Timestamp timestampFromUnixTime(UnixTimestamp *unixTimestamp);
  172. string formatTimeHms(Arena *arena, UnixTimestamp time);
  173. string formatTimeHms(Arena *arena, Timestamp *time);
  174. string formatTimeYmd(Arena *arena, UnixTimestamp time);
  175. string formatTimeYmd(Arena *arena, Timestamp *time);
  176. // ### Linked Lists ###
  177. // TODO(djledda): implement basic linked lists (based on arenas?)
  178. // ### Logging ###
  179. enum StdStream {
  180. StdStream_stdout,
  181. StdStream_stdin,
  182. StdStream_stderr,
  183. };
  184. #define ANSI_INSTRUCTION_FROM_ENUM(ansiCodeEnum) ANSI_INSTRUCTION(ansiCodeEnum)
  185. #define ANSI_INSTRUCTION(ansiCode) "\u001b[" #ansiCode "m"
  186. #define ANSI_INSTRUCTION_STR(ansiCodeStr) "\u001b[" ansiCodeStr "m"
  187. #define ANSI_RESET ANSI_INSTRUCTION(0)
  188. #define ANSI_fg_black 30
  189. #define ANSI_fg_red 31
  190. #define ANSI_fg_green 32
  191. #define ANSI_fg_yellow 33
  192. #define ANSI_fg_blue 34
  193. #define ANSI_fg_magenta 35
  194. #define ANSI_fg_cyan 36
  195. #define ANSI_fg_white 37
  196. #define ANSI_fg_bblack 90
  197. #define ANSI_fg_bred 91
  198. #define ANSI_fg_bgreen 92
  199. #define ANSI_fg_byellow 93
  200. #define ANSI_fg_bblue 94
  201. #define ANSI_fg_bmagenta 95
  202. #define ANSI_fg_bcyan 96
  203. #define ANSI_fg_bwhite 97
  204. #define ANSI_bg_black 40
  205. #define ANSI_bg_red 41
  206. #define ANSI_bg_green 42
  207. #define ANSI_bg_yellow 43
  208. #define ANSI_bg_blue 44
  209. #define ANSI_bg_magenta 45
  210. #define ANSI_bg_cyan 46
  211. #define ANSI_bg_white 47
  212. #define ANSI_bg_bblack 100
  213. #define ANSI_bg_bred 101
  214. #define ANSI_bg_bgreen 102
  215. #define ANSI_bg_byellow 103
  216. #define ANSI_bg_bblue 104
  217. #define ANSI_bg_bmagenta 105
  218. #define ANSI_bg_bcyan 106
  219. #define ANSI_bg_bwhite 107
  220. #define COLOR_TEXT(text, foregroundcolor) ANSI_INSTRUCTION_FROM_ENUM(foregroundcolor) text ANSI_RESET
  221. #define COLOR_TEXT_BG(text, backgroundcolor) ANSI_INSTRUCTION_FROM_ENUM(backgroundcolor) text ANSI_RESET
  222. #define COLOR_TEXT_FG_BG(text, foregroundcolor, backgroundcolor) ANSI_INSTRUCTION_FROM_ENUM(foregroundcolor) ANSI_INSTRUCTION_FROM_ENUM(backgroundcolor) text ANSI_RESET
  223. #define COLOR_TEXT_RGB(text, red, green, blue) ANSI_INSTRUCTION_STR("38;2;" #red ";" #green ";" #blue) text ANSI_RESET
  224. void print(list<int> l, StdStream target = StdStream_stdout);
  225. void print(list<string> l, StdStream target = StdStream_stdout);
  226. void print(list<Vector2<real32>> l, StdStream target = StdStream_stdout);
  227. void print(list<Vector3<real32>> l, StdStream target = StdStream_stdout);
  228. void print(list<Vector4<real32>> l, StdStream target = StdStream_stdout);
  229. void print(const char *fmt, ...);
  230. void printErr(const char *fmt, ...);
  231. // ### Loops ###
  232. #define EachIn(list, it) size_t it = 0; it < (list).head; it++
  233. #define EachInReversed(list, it) size_t it = (list).head - 1; it >= 0 && it < (list).head; it--
  234. #define EachInArray(arr, it) size_t it = 0; it < ArrayCount(arr); ++it
  235. // ### Misc ###
  236. int intCompare(const void *a, const void *b);
  237. #endif