common.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #ifndef _COMMON_H_
  2. #define _COMMON_H_
  3. #ifndef LINUX
  4. #ifdef __linux__
  5. #define LINUX
  6. #endif
  7. #endif
  8. #ifdef LINUX
  9. #ifdef _POSIX_C_SOURCE
  10. #undef _POSIX_C_SOURCE
  11. #endif
  12. #define _POSIX_C_SOURCE 200112L
  13. #ifdef _XOPEN_SOURCE
  14. #undef _XOPEN_SOURCE
  15. #endif
  16. #define _XOPEN_SOURCE 700
  17. #ifdef _DEFAULT_SOURCE
  18. #undef _DEFAULT_SOURCE
  19. #endif
  20. #define _DEFAULT_SOURCE
  21. #endif
  22. #include <stdlib.h>
  23. #include <stdint.h>
  24. #ifndef _HAVE_ARRAY_TYPE
  25. #define _HAVE_ARRAY_TYPE
  26. typedef struct array_s {
  27. uint32_t type;
  28. uint32_t length;
  29. uint32_t size;
  30. void *data;
  31. } array_t;
  32. #endif
  33. #ifndef _HAVE_FILE_TYPE
  34. #define _HAVE_FILE_TYPE
  35. typedef struct file_s {
  36. struct file_s *prev;
  37. struct file_s *next;
  38. char* path;
  39. char* name;
  40. unsigned char* data;
  41. int size;
  42. int len;
  43. int pos;
  44. int modified;
  45. } file_t;
  46. #endif
  47. #ifndef _HAVE_COLOUR_TYPE
  48. #define _HAVE_COLOUR_TYPE
  49. typedef struct colour_s {
  50. uint8_t r;
  51. uint8_t g;
  52. uint8_t b;
  53. uint8_t a;
  54. } colour_t;
  55. #endif
  56. #ifndef _HAVE_V4_TYPE
  57. #define _HAVE_V4_TYPE
  58. typedef struct v4_s {
  59. float w;
  60. float x;
  61. float y;
  62. float z;
  63. } __attribute__((packed)) v4_t;
  64. #endif
  65. #ifndef _HAVE_V3_TYPE
  66. #define _HAVE_V3_TYPE
  67. typedef struct v3_s {
  68. float x;
  69. float y;
  70. float z;
  71. } __attribute__((packed)) v3_t;
  72. #endif
  73. #ifndef _HAVE_V2_TYPE
  74. #define _HAVE_V2_TYPE
  75. typedef struct v2_s {
  76. float x;
  77. float y;
  78. } __attribute__((packed)) v2_t;
  79. #endif
  80. #ifndef _HAVE_AABOX_TYPE
  81. #define _HAVE_AABOX_TYPE
  82. typedef struct aabox_s {
  83. v3_t min;
  84. v3_t max;
  85. } aabox_t;
  86. #endif
  87. #ifndef _HAVE_RECT_TYPE
  88. #define _HAVE_RECT_TYPE
  89. typedef struct rect_s {
  90. int x;
  91. int y;
  92. int w;
  93. int h;
  94. } rect_t;
  95. #endif
  96. #ifndef _HAVE_RECTF_TYPE
  97. #define _HAVE_RECTF_TYPE
  98. typedef struct rectf_s {
  99. float x;
  100. float y;
  101. float w;
  102. float h;
  103. } rectf_t;
  104. #endif
  105. #ifndef _HAVE_POS_TYPE
  106. #define _HAVE_POS_TYPE
  107. typedef struct pos_s {
  108. int16_t x;
  109. int16_t y;
  110. int16_t z;
  111. } __attribute__((packed)) pos_t;
  112. #endif
  113. #ifndef _HAVE_QUATERNION_TYPE
  114. #define _HAVE_QUATERNION_TYPE
  115. typedef v4_t quaternion_t;
  116. #endif
  117. #ifndef _HAVE_MATRIX_TYPE
  118. #define _HAVE_MATRIX_TYPE
  119. typedef struct matrix_s {
  120. float data[16];
  121. } matrix_t;
  122. #endif
  123. #define CN_ERROR 0x01
  124. #define CN_WARN 0x02
  125. #define CN_ACTION 0x03
  126. #define CN_CHAT 0x04
  127. #define CN_INFO 0x05
  128. #define CN_DEBUG 0x06
  129. #define VLSTATE_EXIT 0x00
  130. #define VLSTATE_GET 0x01
  131. #define VLSTATE_MENU 0x02
  132. #define VLSTATE_PAUSE 0x03
  133. #define VLSTATE_PLAY 0x04
  134. /* defined in math.c */
  135. int math_next_pow2(int a);
  136. int math_rand_range(int low, int high);
  137. float math_rand_rangef(float low, float high);
  138. float math_radians_to_degrees(float rads);
  139. float math_degrees_to_radians(float degrees);
  140. /* defined in math_quaternion.c */
  141. quaternion_t *quat_create_quat(float x, float y, float z, float w);
  142. quaternion_t *quat_create_euler(float x, float y, float z);
  143. quaternion_t *quat_create_axis(v3_t *v, float angle);
  144. void quat_init_quat(quaternion_t *q, float x, float y, float z, float w);
  145. void quat_init_euler(quaternion_t *q, float x, float y, float z);
  146. void quat_init_axis(quaternion_t *q, v3_t *v, float angle);
  147. void quat_normalise(quaternion_t *q);
  148. void quat_get_axis(quaternion_t *q, v3_t *v, float *angle);
  149. void quat_multiply_vector(quaternion_t *q, quaternion_t *rq, v3_t *v);
  150. void quat_multiply(quaternion_t *q1, quaternion_t *q2, quaternion_t *rq);
  151. void quat_print(quaternion_t *q);
  152. void quat_computew(quaternion_t *q);
  153. void quat_rotate(quaternion_t *q, v3_t *in, v3_t *out);
  154. /* defined in math_vector.c */
  155. void vect_create(v3_t *start, v3_t *end, v3_t *v);
  156. float vect_length(v3_t *v);
  157. void vect_normalise(v3_t *v);
  158. float vect_scalarproduct(v3_t *v1, v3_t *v2);
  159. void vect_crossproduct(v3_t *v1, v3_t *v2, v3_t *v3);
  160. void vect_dotproduct(v3_t *v1, v3_t *v2, v3_t *v3);
  161. void vect_subtract(v3_t *v1, v3_t *v2, v3_t *v3);
  162. float vect_diameter(v3_t *v);
  163. float math_dotproduct(v3_t *v1, v3_t *v2);
  164. float math_distance(v3_t *v1, v3_t *v2);
  165. /* defined in math_matrix.c */
  166. void matrix_init(matrix_t *m);
  167. matrix_t *matrix_create(void);
  168. void matrix_multiply(matrix_t *m, matrix_t *mul);
  169. void matrix_translate(matrix_t *m, float x, float y, float z);
  170. void matrix_translate_v(matrix_t *m, v3_t *v);
  171. void matrix_scale(matrix_t *m, float x, float y, float z);
  172. void matrix_scale_v(matrix_t *m, v3_t *v);
  173. void matrix_rotate(matrix_t *m, float rads, float x, float y, float z);
  174. void matrix_rotate_v(matrix_t *m, float rads, v3_t *v);
  175. void matrix_rotate_x(matrix_t *m, float rads);
  176. void matrix_rotate_y(matrix_t *m, float rads);
  177. void matrix_rotate_z(matrix_t *m, float rads);
  178. void matrix_rotate_deg(matrix_t *m, float degrees, float x, float y, float z);
  179. void matrix_rotate_deg_v(matrix_t *m, float degrees, v3_t *v);
  180. void matrix_rotate_deg_x(matrix_t *m, float degrees);
  181. void matrix_rotate_deg_y(matrix_t *m, float degrees);
  182. void matrix_rotate_deg_z(matrix_t *m, float degrees);
  183. void matrix_rotate_quat(matrix_t *m, quaternion_t *q);
  184. /* defined in string.c */
  185. char* trim(char* str);
  186. char* strdup(const char* str);
  187. int str_sanitise(char* dest, int size, char* str);
  188. int strappend(char* dest, int size, char* str);
  189. int parse_bool(char* str);
  190. /* defined in config.c */
  191. char* config_get(char* name);
  192. int config_get_int(char* name);
  193. float config_get_float(char* name);
  194. int config_get_bool(char* name);
  195. void config_set(char* name, char* value);
  196. int config_set_command(array_t *args);
  197. void config_set_int(char* name, int value);
  198. void config_set_float(char* name, float value);
  199. void config_set_default(char* name, char* value, int (*setter)(char* v));
  200. void config_set_default_int(char* name, int value, int (*setter)(char* v));
  201. void config_set_default_float(char* name, float value, int (*setter)(char* v));
  202. void config_load(char* file);
  203. int config_load_command(array_t *args);
  204. int config_ignore_command(array_t *args);
  205. void config_init(int argc, char** argv);
  206. void config_save(char* section, char* type, char* file);
  207. /* defined in config_default.c */
  208. void config_default_init(void);
  209. /* defined in log.c */
  210. int log_minlevel_setter(char* v);
  211. int log_maxlevel_setter(char* v);
  212. int log_sminlevel_setter(char* v);
  213. int log_smaxlevel_setter(char* v);
  214. int log_cminlevel_setter(char* v);
  215. int log_cmaxlevel_setter(char* v);
  216. int log_file_setter(char* v);
  217. void vlprintf(uint8_t type, char* fmt,...);
  218. /* defined in utf8.c */
  219. int utf8_seqlen(char* str);
  220. uint32_t utf8_nextchar(char* str, int *i);
  221. uint32_t utf8_toutf32(char* src, int size);
  222. int utf8_fromutf32(char *dest, int sz, uint32_t ch);
  223. uint32_t utf16_toutf32(uint16_t *str);
  224. int utf8_offset(char* str, int i);
  225. int utf8_charindex(char* str, int o);
  226. int utf8_strlen(char* str);
  227. void utf8_inc(char* str, int *i);
  228. void utf8_dec(char* str, int *i);
  229. char* utf8_strchr(char* str, uint32_t ch, int *charn);
  230. char* utf8_memchr(char* str, uint32_t ch, size_t sz, int *charn);
  231. /* defined in sys_console.c */
  232. void sys_console_print(char* str, int newline);
  233. void sys_console_printf(char* fmt, ...);
  234. void sys_console_init(void);
  235. void sys_console_exit(void);
  236. /* defined in intl.c */
  237. char* gettext(const char *s);
  238. char* ngettext(const char* s1, const char* s2, int n);
  239. void intl_init(void);
  240. /* defined in time.c */
  241. void time_init(void);
  242. uint32_t time_ticks(void);
  243. void delay(uint32_t ms);
  244. float time_dtime(uint32_t last);
  245. uint32_t interval_delay(uint32_t last, uint32_t hz);
  246. uint32_t calc_fps(uint32_t prev, uint32_t current);
  247. /* defined in command.c */
  248. int command_init(void);
  249. int command_add(char* name, int (*func)(array_t *args));
  250. int command_apply(char* name, char* value);
  251. int command_exec(char* str);
  252. int command_execf(char* str, ...);
  253. void command_save(file_t *f);
  254. /* defined in main.c */
  255. int client_state(int s);
  256. /* defined in world.c */
  257. int world_init(char* name);
  258. void world_exit(void);
  259. #endif