common.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #ifndef _COMMON_H_
  2. #define _COMMON_H_
  3. #ifndef LINUX
  4. #ifdef __linux__
  5. #define LINUX
  6. #endif
  7. #endif
  8. #ifdef _POSIX_C_SOURCE
  9. #undef _POSIX_C_SOURCE
  10. #endif
  11. #define _POSIX_C_SOURCE 200112L
  12. #ifdef _XOPEN_SOURCE
  13. #undef _XOPEN_SOURCE
  14. #endif
  15. #define _XOPEN_SOURCE 700
  16. #ifdef LINUX
  17. #ifdef _DEFAULT_SOURCE
  18. #undef _DEFAULT_SOURCE
  19. #endif
  20. #define _DEFAULT_SOURCE
  21. #else
  22. #ifdef _BSD_SOURCE
  23. #undef _BSD_SOURCE
  24. #endif
  25. #define _BSD_SOURCE
  26. #endif
  27. #include <stdlib.h>
  28. #include <stdint.h>
  29. #ifndef _HAVE_COLOUR_TYPE
  30. #define _HAVE_COLOUR_TYPE
  31. typedef struct colour_s {
  32. uint8_t r;
  33. uint8_t g;
  34. uint8_t b;
  35. uint8_t a;
  36. } colour_t;
  37. #endif
  38. #ifndef _HAVE_V3_TYPE
  39. #define _HAVE_V3_TYPE
  40. typedef struct v3_s {
  41. float x;
  42. float y;
  43. float z;
  44. } __attribute__((packed)) v3_t;
  45. #endif
  46. #ifndef _HAVE_V2_TYPE
  47. #define _HAVE_V2_TYPE
  48. typedef struct v2_s {
  49. float x;
  50. float y;
  51. } __attribute__((packed)) v2_t;
  52. #endif
  53. #ifndef _HAVE_AABOX_TYPE
  54. #define _HAVE_AABOX_TYPE
  55. typedef struct aabox_s {
  56. v3_t min;
  57. v3_t max;
  58. } aabox_t;
  59. #endif
  60. #ifndef _HAVE_RECTF_TYPE
  61. #define _HAVE_RECTF_TYPE
  62. typedef struct rectf_s {
  63. float x;
  64. float y;
  65. float w;
  66. float h;
  67. } rectf_t;
  68. #endif
  69. #ifndef _HAVE_POS_TYPE
  70. #define _HAVE_POS_TYPE
  71. typedef struct pos_s {
  72. int16_t x;
  73. int16_t y;
  74. int16_t z;
  75. } __attribute__((packed)) pos_t;
  76. #endif
  77. #ifndef _HAVE_QUATERNION_TYPE
  78. #define _HAVE_QUATERNION_TYPE
  79. typedef struct quaternion_s {
  80. float w;
  81. float x;
  82. float y;
  83. float z;
  84. } __attribute__((packed)) quaternion_t;
  85. #endif
  86. #define PI_OVER_180 0.0174532925
  87. #define CN_ERROR "\1"
  88. #define CN_WARN "\2"
  89. #define CN_ACTION "\3"
  90. #define CN_CHAT "\4"
  91. #define CN_INFO ""
  92. #define CN_DEBUG "\6"
  93. /* defined in math.c */
  94. int math_next_pow2(int a);
  95. int math_rand_range(int low, int high);
  96. float math_rand_rangef(float low, float high);
  97. /* defined in math_quaternion.c */
  98. quaternion_t *quat_create_quat(float x, float y, float z, float w);
  99. quaternion_t *quat_create_euler(float x, float y, float z);
  100. quaternion_t *quat_create_axis(v3_t *v, float angle);
  101. void quat_init_quat(quaternion_t *q, float x, float y, float z, float w);
  102. void quat_init_euler(quaternion_t *q, float x, float y, float z);
  103. void quat_init_axis(quaternion_t *q, v3_t *v, float angle);
  104. void quat_normalise(quaternion_t *q);
  105. void quat_get_axis(quaternion_t *q, v3_t *v, float *angle);
  106. void quat_multiply_vector(quaternion_t *q, quaternion_t *rq, v3_t *v);
  107. void quat_multiply(quaternion_t *q1, quaternion_t *q2, quaternion_t *rq);
  108. void quat_print(quaternion_t *q);
  109. void quat_computew(quaternion_t *q);
  110. void quat_rotate(quaternion_t *q, v3_t *in, v3_t *out);
  111. /* defined in math_vector.c */
  112. void vect_create(v3_t *start, v3_t *end, v3_t *v);
  113. float vect_length(v3_t *v);
  114. void vect_normalise(v3_t *v);
  115. float vect_scalarproduct(v3_t *v1, v3_t *v2);
  116. void vect_crossproduct(v3_t *v1, v3_t *v2, v3_t *v3);
  117. void vect_dotproduct(v3_t *v1, v3_t *v2, v3_t *v3);
  118. void vect_subtract(v3_t *v1, v3_t *v2, v3_t *v3);
  119. float vect_diameter(v3_t *v);
  120. float math_dotproduct(v3_t *v1, v3_t *v2);
  121. float math_distance(v3_t *v1, v3_t *v2);
  122. /* defined in string.c */
  123. char* trim(char* str);
  124. char* strdup(const char* str);
  125. /* defined in config.c */
  126. char* config_get(char* name);
  127. int config_get_int(char* name);
  128. float config_get_float(char* name);
  129. int config_get_bool(char* name);
  130. void config_set(char* name, char* value);
  131. void config_set_int(char* name, int value);
  132. void config_set_float(char* name, float value);
  133. void config_set_default(char* name, char* value, int (*setter)(char* v));
  134. void config_set_default_int(char* name, int value, int (*setter)(char* v));
  135. void config_set_default_float(char* name, float value, int (*setter)(char* v));
  136. void config_load(char* file);
  137. void config_init(int argc, char** argv);
  138. void config_save(void);
  139. /* defined in config_default.c */
  140. void config_default_init(void);
  141. /* defined in log.c */
  142. int log_minlevel_setter(char* v);
  143. int log_maxlevel_setter(char* v);
  144. int log_sminlevel_setter(char* v);
  145. int log_smaxlevel_setter(char* v);
  146. int log_cminlevel_setter(char* v);
  147. int log_cmaxlevel_setter(char* v);
  148. int log_file_setter(char* v);
  149. void vlprintf(char* fmt,...);
  150. /* defined in utf8.c */
  151. int utf8_seqlen(char* str);
  152. uint32_t utf8_nextchar(char* str, int *i);
  153. int utf8_offset(char* str, int i);
  154. int utf8_charindex(char* str, int o);
  155. int utf8_strlen(char* str);
  156. void utf8_inc(char* str, int *i);
  157. void utf8_dec(char* str, int *i);
  158. char* utf8_strchr(char* str, uint32_t ch, int *charn);
  159. char* utf8_memchr(char* str, uint32_t ch, size_t sz, int *charn);
  160. /* defined in sys_console.c */
  161. void sys_console_print(char* str, int newline);
  162. void sys_console_printf(char* fmt, ...);
  163. void sys_console_init(void);
  164. void sys_console_exit(void);
  165. /* defined in intl.c */
  166. char* gettext(const char *s);
  167. char* ngettext(const char* s1, const char* s2, int n);
  168. void intl_init(void);
  169. #endif