Advertisement
Kitomas

kit_core.h as of 2023-9-7

Sep 8th, 2023
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.52 KB | None | 0 0
  1. /**
  2.  * \file kit_core.h
  3.  * \brief Header file for KIT SDL2's core library
  4.  */
  5. #ifndef _KIT_CORE_H
  6. #define _KIT_CORE_H
  7. #ifndef _KIT_SDL2_CORE_H
  8. #define _KIT_SDL2_CORE_H
  9.  
  10.  
  11.  
  12.  
  13. #include <SDL2/SDL.h>
  14.  
  15.  
  16.  
  17.  
  18. /* ++++++++++ */
  19. /* +kit_core+ */
  20. /* ++++++++++ */
  21.  
  22. //bitmask of a Uint16's MSb/MSB
  23. #define U16_MSb ((Uint16)1<<15)
  24. #define U16_MSbC (~U16_MSb) //ones' complement of U16_MSb
  25. #define U16_MSB  ((Uint16)0xFF<<8)
  26. #define U16_MSBC (~U16_MSB) //ones' complement of U16_MSB
  27.  
  28.  
  29. //bitmask of a Uint32's MSb/MSB
  30. #define U32_MSb ((Uint32)1<<31)
  31. #define U32_MSbC (~U32_MSb) //ones' complement of U32_MSb
  32. #define U32_MSB  ((Uint32)0xFF<<24)
  33. #define U32_MSBC (~U32_MSB) //ones' complement of U32_MSB
  34.  
  35.  
  36. //bitmask of a Uint64's MSb/MSB
  37. #define U64_MSb ((Uint64)1<<63)
  38. #define U64_MSbC (~U64_MSb) //ones' complement of U64_MSb
  39. #define U64_MSB  ((Uint64)0xFF<<56)
  40. #define U64_MSBC (~U64_MSB) //ones' complement of U64_MSB
  41.  
  42.  
  43. #define U16_MAX (0xffff)
  44. #define U32_MAX (0xffffffff)
  45. #define U64_MAX (0xffffffffffffffff)
  46.  
  47.  
  48. //use something like "0x%08X%08X" in the format string for this to work properly
  49. #define U64_PRINT(_num) ( (Uint32)((_num)>>32) ),( (Uint32)(_num)&U32_MAX )
  50.  
  51.  
  52. //string to unsigned int
  53. #define STR_U32(_s) ( *(Uint32*)(_s"\x00\x00\x00") )
  54. #define STR_U64(_s) ( *(Uint64*)(_s"\x00\x00\x00\x00\x00\x00\x00") )
  55.  
  56.  
  57. #if defined(_KIT_CORE_DEBUG) || defined(_KIT_ALL_DEBUG)
  58. #define kit_coreLog(...) SDL_Log(__VA_ARGS__)
  59. #else
  60. #define kit_coreLog(...) ;
  61. #endif
  62.  
  63.  
  64.  
  65. extern const SDL_bool kit_coreIsDebug;
  66.  
  67. extern const char kit_coreBoolStr[2][6];
  68.  
  69.  
  70.  
  71. extern int kit_coreRealloc(void* ptr_p, size_t size_old, size_t size_new);
  72.  
  73.  
  74. extern int kit_coreInit();
  75.  
  76. extern int kit_coreQuit();
  77.  
  78. /* ---------- */
  79. /* -kit_core- */
  80. /* ---------- */
  81.  
  82.  
  83.  
  84.  
  85. /* ++++++++++++++++ */
  86. /* +kit_coreThread+ */
  87. /* ++++++++++++++++ */
  88.  
  89. typedef struct { //32B
  90.   SDL_Thread* thread;
  91.   void*         data;
  92.   SDL_mutex*    lock;
  93.   int _,returnStatus;
  94. } kit_coreThread;
  95.  
  96. /* ---------------- */
  97. /* -kit_coreThread- */
  98. /* ---------------- */
  99.  
  100.  
  101.  
  102.  
  103. /* +++++++++++++++++++++ */
  104. /* +kit_sdl2_coreVector+ */
  105. /* +++++++++++++++++++++ */
  106.  
  107. //assumes that _vector is a vector pointer
  108. #define PRINT_VECTOR(_pref, _vector) {                                 \
  109.   kit_coreLog(_pref"->type.s=\"%s\"",(_vector)->type.s);               \
  110.   kit_coreLog(_pref"->x     =%u",(_vector)->x);                        \
  111.   kit_coreLog(_pref"->y     =%u",(_vector)->y);                        \
  112.   kit_coreLog(_pref"->z     =%u",(_vector)->z);                        \
  113.   kit_coreLog(_pref"->unit  =%u",(_vector)->unit);                     \
  114.   kit_coreLog(_pref"->_dims =0x%08X%08X",U64_PRINT((_vector)->_dims)); \
  115.   kit_coreLog(_pref"->lens  =%p",(_vector)->lens.r3D);                 \
  116.   kit_coreLog(_pref"->ptr   =%p",(_vector)->ptr);                      }
  117.  
  118.  
  119. //now this should be easier to change if i want to alter
  120.  //the struct's size/lengths type
  121. typedef Uint32  lens_t; ///< \brief The unsigned integer type used for kit_coreVector lengths
  122. typedef Sint32 diffs_t; ///< \brief The signed integer type used for kit_coreVector length differences
  123.  
  124.  
  125. /**
  126.  * \brief The struct for a contiguous dynamic array (dereference order is [x][y][z]).
  127.  */
  128. typedef struct { //48B (assuming that lens_t is Uint32, and a void* is 8 bytes)
  129.   union {
  130.     char    s[8]; ///< \brief String portion of ID (albeit a short string).
  131.     Uint64     n; ///< \brief Integer portion of ID.
  132.   } /* --- */ type; ///< \brief A user-defined type identifier.
  133.   lens_t         x; ///< \brief Length of the vector's (allocated) x axis.
  134.   lens_t         y; ///< \brief Length of the vector's (allocated) y axis (0 means the axis is nonexistent).
  135.   lens_t         z; ///< \brief Length of the vector's (allocated) z axis (0 means the axis is nonexistent).
  136.   lens_t      unit; ///< \brief size of each data element.
  137.   Sint64     _dims; ///< \brief (read only) 1=1D, 2=2D, 3=3D vector respectively.
  138.   union {
  139.     lens_t   r1D; ///< \brief real length of x
  140.     lens_t*  r2D; ///< \brief real lengths of each y index in x
  141.     lens_t** r3D; ///< \brief real lengths of each z index in y
  142.   } /* --- */ lens; ///< \brief lengths for individual axes (must be <= the actual allocated size)
  143.   union {
  144.     void *p1d, **p2d, ***p3d, *ptr; ///< \brief The actual array portion of the vector.
  145.   };
  146. } kit_coreVector;
  147.  
  148.  
  149.  
  150. /**
  151.  * Add to or subtract from size of a kit_coreVector.
  152.  * \param[in,out] Vector_p A pointer to the kit_coreVector* to be altered.
  153.  * \param[in] x_add How much to increase or decrease the x axis.
  154.  * \param[in] y_add How much to increase or decrease the y axis.
  155.  * \param[in] z_add How much to increase or decrease the z axis.
  156.  * \return 0 on success, or a negative error code (call SDL_GetError() for more info).
  157.  *
  158.  * \remark As stated in kit_coreVectorSet, vectors cannot change from 1D -> 2D, 2D -> 3D, etc.
  159.  * \sa kit_coreVectorSet
  160.  * \sa kit_coreVectorAppend
  161.  */
  162. extern int kit_coreVectorAdd(kit_coreVector** Vector_p, diffs_t x_add, diffs_t y_add, diffs_t z_add);
  163.  
  164. /**
  165.  * Set the size of a kit_coreVector.
  166.  * \param[in,out] Vector_p A pointer to the kit_coreVector* to be altered.
  167.  * \param[in] x_new New size for the x axis (set to 0 to leave x unchanged).
  168.  * \param[in] y_new New size for the y axis (set to 0 to leave y unchanged).
  169.  * \param[in] z_new New size for the z axis (set to 0 to leave z unchanged).
  170.  * \return 0 on success, or a negative error code (call SDL_GetError() for more info).
  171.  *
  172.  * \remark The result of a size change cannot turn a 1D vector to a 2D one, vice versa, and so on.
  173.  * \sa kit_coreVectorAdd
  174.  * \sa kit_coreVectorAppend
  175.  */
  176. extern int kit_coreVectorSet(kit_coreVector** Vector_p, lens_t x_new, lens_t y_new, lens_t z_new);
  177.  
  178.  
  179. /**
  180.  * Append an element to the end of a 1D kit_coreVector*
  181.  * \param[in,out] Vector_p A pointer to the kit_coreVector* to be altered.
  182.  * \param[in] src The data to be copied and appended to the end of the vector
  183.  * \return The newly-created index, or -1 on error (call SDL_GetError() for more info).
  184.  *
  185.  * \remark *src must be *Vector_p->unit bytes in size or bad things will happen.
  186.  * \sa kit_coreVectorAppend2D
  187.  * \sa kit_coreVectorAppend3D
  188.  * \sa kit_coreVectorAppend
  189.  * \sa kit_coreVectorAdd
  190.  * \sa kit_coreVectorSet
  191.  */
  192. extern lens_t kit_coreVectorAppend1D(kit_coreVector** Vector_p, void* src);
  193.  
  194. /**
  195.  * Append an element to the end of a 2D kit_coreVector*
  196.  * \param[in,out] Vector_p A pointer to the kit_coreVector* to be altered.
  197.  * \param[in] src The data to be copied and appended to the end of the vector
  198.  * \param[in] x_pos The x index to append to
  199.  * \return The newly-created index, or 0xffffffff on error (call SDL_GetError() for more info).
  200.  *
  201.  * \remark *src must be *Vector_p->unit bytes in size or bad things will happen.
  202.  * \sa kit_coreVectorAppend1D
  203.  * \sa kit_coreVectorAppend3D
  204.  * \sa kit_coreVectorAppend
  205.  * \sa kit_coreVectorAdd
  206.  * \sa kit_coreVectorSet
  207.  */
  208. extern lens_t kit_coreVectorAppend2D(kit_coreVector** Vector_p, void* src, lens_t x_pos);
  209.  
  210. /**
  211.  * Append an element to the end of a 2D kit_coreVector*
  212.  * \param[in,out] Vector_p A pointer to the kit_coreVector* to be altered.
  213.  * \param[in] src The data to be copied and appended to the end of the vector
  214.  * \param[in] x_pos The x index to append to
  215.  * \param[in] y_pos The y index to append to
  216.  * \return The newly-created index, or 0xffffffff on error (call SDL_GetError() for more info).
  217.  *
  218.  * \remark *src must be *Vector_p->unit bytes in size or bad things will happen.
  219.  * \sa kit_coreVectorAppend1D
  220.  * \sa kit_coreVectorAppend2D
  221.  * \sa kit_coreVectorAppend
  222.  * \sa kit_coreVectorAdd
  223.  * \sa kit_coreVectorSet
  224.  */
  225. extern lens_t kit_coreVectorAppend3D(kit_coreVector** Vector_p, void* src, lens_t x_pos, lens_t y_pos);
  226.  
  227. /**
  228.  * Append an element to the end of a kit_coreVector*
  229.  * \param[in,out] Vector_p A pointer to the kit_coreVector* to be altered.
  230.  * \param[in] src The data to be copied and appended to the end of the vector
  231.  * \param[in] x_pos The x index to append to (ignored for 1D vectors)
  232.  * \param[in] y_pos The y index to append to (ignored for <3D vectors)
  233.  * \return The newly-created index, or 0xffffffff on error (call SDL_GetError() for more info).
  234.  *
  235.  * \remark *src must be *Vector_p->unit bytes in size or bad things will happen.
  236.  * \sa kit_coreVectorAppend1D
  237.  * \sa kit_coreVectorAppend2D
  238.  * \sa kit_coreVectorAppend3D
  239.  * \sa kit_coreVectorAdd
  240.  * \sa kit_coreVectorSet
  241.  */
  242. extern lens_t kit_coreVectorAppend(kit_coreVector** Vector_p, void* src, lens_t x_pos, lens_t y_pos);
  243.  
  244.  
  245. /**
  246.  * Destroy a kit_coreVector.
  247.  * \param[in,out] Vector_p A pointer to the kit_coreVector* to be destroyed (before being set to NULL).
  248.  * \return 0 on success, or a negative error code (call SDL_GetError() for more info).
  249.  *
  250.  * \sa kit_coreVectorCreate
  251.  */
  252. extern int kit_coreVectorDestroy(kit_coreVector** Vector_p);
  253.  
  254. /**
  255.  * Create a new kit_coreVector.
  256.  * \param[in] x Size of the vector on the x axis.
  257.  * \param[in] y Size of the vector on the y axis (set to 0 to disable the axis entirely).
  258.  * \param[in] z Size of the vector on the z axis (set to 0 to disable the axis entirely).
  259.  * \param[in] unit The size of each data element, in bytes.
  260.  * \param[in] type_n A user-defined number which acts as the vector's type identifier.
  261.  * \return A pointer to a newly-created Vector struct, or NULL on error (call SDL_GetError() for more info).
  262.  *
  263.  * \remark "unit" determines of each DATA element, which is to say, while a 1D vector's element size is
  264.            equal to unit on the x axis, a 2D vector's element size on the x axis will be that of a void*.
  265.  * \remark Whether or not returnStatus_p is NULL, SDL_GetError() will be set in the event of an error.
  266.  * \sa kit_coreVectorDestroy
  267.  */
  268. extern kit_coreVector* kit_coreVectorCreate(lens_t x, lens_t y, lens_t z, lens_t unit, Uint64 type_n);
  269.  
  270.  
  271. extern void _kit_coreVectorPrintInt(kit_coreVector* Vector,const char* prefix); //debug
  272.  
  273. extern int kit_coreVectorTest(); //debug
  274.  
  275. /* --------------------- */
  276. /* -kit_sdl2_coreVector- */
  277. /* --------------------- */
  278.  
  279.  
  280.  
  281.  
  282. /* ++++++++++++++ */
  283. /* +kit_coreFstr+ */
  284. /* ++++++++++++++ */
  285.  
  286. #ifndef _WCHAR_T_DEFINED
  287. #  include <wchar.h>
  288. #endif
  289. #ifndef _FSTR
  290. # define _FSTR
  291. #  define fstr kit_coreFstr
  292. #endif
  293. #ifndef _FSTRW
  294. # define _FSTRW
  295. #  define fstrw kit_coreFstrw
  296. #endif
  297.  
  298.  
  299.  
  300. /**
  301.  * \brief This struct contains buffer information for fstr.
  302.  */
  303. typedef struct {
  304.   union {
  305.     char*     s; ///< \brief The char portion of the string union.
  306.     wchar_t*  w; ///< \brief The wchar portion of the string union.
  307.   } /* ----- */ b; ///< \brief The actual string buffer union.
  308.   Uint32 mem_size; ///< \brief The size of the string buffer, in bytes.
  309.   Uint32 _padding; ///< \brief (unused) Another Uint32 to pad to a multiple of 8 bytes.
  310. } kit_coreFstr_t;
  311.  
  312.  
  313.  
  314. /**
  315.  * Format a string, before returning that string.
  316.  * \param[in,out] buffer A pointer to a kit_coreFstr_t that contains buffer information.
  317.  * \param[in] fmt The format string; used the same way as the first argument to printf.
  318.  * \param[in] ... List of variables to be formatted, if any.
  319.  * \return A pointer to the newly-formatted string, or NULL on error (call SDL_GetError() for more info).
  320.  *
  321.  * \sa kit_coreFstrw
  322.  */
  323. extern char* kit_coreFstr(kit_coreFstr_t* buffer, const char* fmt,...);
  324.  
  325. /**
  326.  * Format a wide string, before returning that wide string.
  327.  * \param[in,out] buffer A pointer to a kit_coreFstr_t that contains buffer information.
  328.  * \param[in] fmt The format string; used the same way as the first argument to wprintf.
  329.  * \param[in] ... List of variables to be formatted, if any.
  330.  * \return A pointer to the newly-formatted wide string, or NULL on error (call SDL_GetError() for more info).
  331.  *
  332.  * \sa kit_coreFstr
  333.  */
  334. extern wchar_t* kit_coreFstrw(kit_coreFstr_t* buffer, const wchar_t* fmt,...);
  335.  
  336.  
  337. /**
  338.  * Destroy a kit_coreFstr_t buffer.
  339.  * \param[in,out] buffer_p A pointer to the kit_coreFstr_t* to be destroyed (before being set to NULL).
  340.  * \return 0 on success, or a negative error code (call SDL_GetError() for more info).
  341.  *
  342.  * \sa kit_coreFstrCreate
  343.  */
  344. extern int kit_coreFstrDestroy(kit_coreFstr_t** buffer_p);
  345.  
  346. /**
  347.  * Create a new kit_coreFstr_t.
  348.  * \param[in] buffer_size the size of the string's buffer, in bytes.
  349.  * \return A pointer to a newly-created Fstr_t struct, or NULL on error (call SDL_GetError() for more info).
  350.  *
  351.  * \sa kit_coreFstrDestroy
  352.  */
  353. extern kit_coreFstr_t* kit_coreFstrCreate(Uint32 buffer_size);
  354.  
  355. /* -------------- */
  356. /* -kit_coreFstr- */
  357. /* -------------- */
  358.  
  359.  
  360.  
  361.  
  362. #endif /* _KIT_SDL2_CORE_H */
  363. #endif /* _KIT_CORE_H */
  364.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement