Advertisement
Kitomas

kit_core.h as of 2023/9/13

Sep 15th, 2023
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.74 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. #if defined(_KIT_CORE_DEBUG) || defined(_KIT_ALL_DEBUG)
  23. #define kit_coreLog(...) SDL_Log(__VA_ARGS__)
  24. #else
  25. #define kit_coreLog(...) ;
  26. #endif
  27.  
  28.  
  29.  
  30. extern const SDL_bool kit_coreIsDebug;
  31.  
  32. extern const char kit_coreBoolStr[2][6];
  33.  
  34.  
  35. extern int kit_coreMemset(void* ptr, int value, size_t size);
  36.  
  37. extern int kit_coreRealloc(void* ptr_p, size_t size_old, size_t size_new);
  38.  
  39.  
  40. extern int kit_coreInit();
  41.  
  42. extern int kit_coreQuit();
  43.  
  44. /* ---------- */
  45. /* -kit_core- */
  46. /* ---------- */
  47.  
  48.  
  49.  
  50.  
  51. /* ++++++++++++++++ */
  52. /* +kit_coreThread+ */
  53. /* ++++++++++++++++ */
  54.  
  55. typedef struct { //32B
  56.   SDL_Thread* thread;
  57.   void*         data;
  58.   SDL_mutex*    lock;
  59.   int _,returnStatus;
  60. } kit_coreThread;
  61.  
  62. /* ---------------- */
  63. /* -kit_coreThread- */
  64. /* ---------------- */
  65.  
  66.  
  67.  
  68.  
  69. /* +++++++++++++++++++++ */
  70. /* +kit_sdl2_coreVector+ */
  71. /* +++++++++++++++++++++ */
  72.  
  73. //assumes that _vector is a vector pointer
  74.  //(also assumes that z_block, lensize, and datasize are < 2^32)
  75. #define PRINT_VECTOR(_pref, _vector) {                           \
  76.   kit_coreLog(_pref"->type.s  =\"%s\"",(_vector)->type.s);       \
  77.   kit_coreLog(_pref"->z_block =%u",(Uint32)(_vector)->z_block);  \
  78.   kit_coreLog(_pref"->lensize =%u",(Uint32)(_vector)->lensize);  \
  79.   kit_coreLog(_pref"->datasize=%u",(Uint32)(_vector)->datasize); \
  80.   kit_coreLog(_pref"->x       =%u",(_vector)->x);                \
  81.   kit_coreLog(_pref"->y       =%u",(_vector)->y);                \
  82.   kit_coreLog(_pref"->z       =%u",(_vector)->z);                \
  83.   kit_coreLog(_pref"->unit    =%u",(_vector)->unit);             \
  84.   kit_coreLog(_pref"->lens    =%p",(_vector)->lens);             \
  85.   kit_coreLog(_pref"->data    =%p",(_vector)->data);             }
  86.  
  87.  
  88. /**
  89.  * \name Macros for accessing data elements of a kit_coreVector*
  90.  */
  91. /** @{ */
  92. #define VECTOR_INDEX_A(_type, _vector, _x,_y,_z) \
  93.   ((_type*)(_vector)->data)[ (_x) + (_y)*(_vector)->x + (_z)*(_vector)->z_block ]
  94.  
  95. #define VECTOR_INDEX_B(_type, _vector, _x_raw,_y_raw,_z_raw) \
  96.   ((_type*)(_vector)->data)[ (_x_raw) + (_y_raw) + (_z_raw) ]
  97.  
  98. #define VECTOR_INDEX_C(_type, _vector, _index_raw) \
  99.   ((_type*)(_vector)->data)[ (_index_raw) ]
  100. /** @} */
  101.  
  102.  
  103. /**
  104.  * \name Macros for accessing lengths of a kit_coreVector* 's axes
  105.  */
  106. /** @{ */
  107. #define VECTOR_LENS_A(_vector, _x,_y) \
  108.   (_vector)->lens[ (_x) + (_y)*(_vector)->x ]
  109.  
  110. #define VECTOR_LENS_B(_vector, _x_raw,_y_raw) \
  111.   (_vector)->lens[ (_x_raw) + (_y_raw) ]
  112.  
  113. #define VECTOR_LENS_C(_vector, _index_raw) \
  114.   (_vector)->lens[ (_index_raw) ]
  115. /** @} */
  116.  
  117.  
  118.  
  119. /**
  120.  * \brief The struct for a contiguous dynamic array
  121.  */
  122. typedef struct { //64B (assuming that a void* is 8 bytes)
  123.   union {
  124.     char   s[8]; ///< \brief String portion of ID (albeit a short string)
  125.     Uint64    n; ///< \brief Integer portion of ID
  126.   } /*----*/ type; ///< \brief A user-defined type identifier
  127.   Uint64  z_block; ///< \brief Is equal to x*y
  128.   Uint64 datasize; ///< \brief Total allocated data size, in bytes (does not include the struct itself)
  129.   Uint32        x; ///< \brief Length of the vector's (allocated) x axis
  130.   Uint32        y; ///< \brief Length of the vector's (allocated) y axis
  131.   Uint32        z; ///< \brief Length of the vector's (allocated) z axis
  132.   Uint32     unit; ///< \brief Size of each data element
  133.   Uint32  lenslen; ///< \brief Allocated size of lens, in # of (Uint32) elements
  134.   Uint32 _padding; ///< \brief (reserved) Used for padding to a multiple of 8 bytes
  135.   Uint32*    lens; ///< \brief lengths for individual axes (must be <= the actual allocated size)
  136.   void*      data; ///< \brief The actual array portion of the vector
  137. } kit_coreVector;
  138.  
  139.  
  140.  
  141. /**
  142.  * Set the size of a kit_coreVector
  143.  * \param[in,out] Vector_p A pointer to the kit_coreVector* to be altered
  144.  * \param[in] x_new New size for the x axis (set to 0 to leave x unchanged)
  145.  * \param[in] y_new New size for the y axis (set to 0 to leave y unchanged)
  146.  * \param[in] z_new New size for the z axis (set to 0 to leave z unchanged)
  147.  * \return 0 on success, or -1 on error (call SDL_GetError() for more info)
  148.  *
  149.  * \sa kit_coreVectorAdd
  150.  * \sa kit_coreVectorAppend
  151.  */
  152. extern int kit_coreVectorSet(kit_coreVector** Vector_p, Uint32 x_new, Uint32 y_new, Uint32 z_new);
  153.  
  154. /**
  155.  * Add to or subtract from size of a kit_coreVector
  156.  * \param[in,out] Vector_p A pointer to the kit_coreVector* to be altered
  157.  * \param[in] x_add How much to increase or decrease the x axis
  158.  * \param[in] y_add How much to increase or decrease the y axis
  159.  * \param[in] z_add How much to increase or decrease the z axis
  160.  * \return 0 on success, or -1 on error (call SDL_GetError() for more info)
  161.  *
  162.  * \sa kit_coreVectorSet
  163.  * \sa kit_coreVectorAppend
  164.  */
  165. extern int kit_coreVectorAdd(kit_coreVector** Vector_p, Sint32 x_add, Sint32 y_add, Sint32 z_add);
  166.  
  167. /**
  168.  * Append an element to the end of a kit_coreVector
  169.  * \param[in,out] Vector_p A pointer to the kit_coreVector* to be altered
  170.  * \param[in] src A pointer to the data to be copied to the end of the vector
  171.  * \param[in] x_pos The x index to append to
  172.  * \param[in] y_pos The y index to append to
  173.  * \return The newly-created index, or -1 (0xffffffff) on error (call SDL_GetError() for more info)
  174.  *
  175.  * \remark *src must be *Vector_p->unit bytes in size or bad things will happen
  176.  * \sa kit_coreVectorSet
  177.  * \sa kit_coreVectorAdd
  178.  */
  179. extern Uint32 kit_coreVectorAppend(kit_coreVector** Vector_p, void* src, Uint32 x_pos, Uint32 y_pos);
  180.  
  181.  
  182. /**
  183.  * Destroy a kit_coreVector
  184.  * \param[in,out] Vector_p A pointer to the kit_coreVector* to be destroyed (before being set to NULL)
  185.  * \return 0 on success, or -1 on error (call SDL_GetError() for more info)
  186.  *
  187.  * \sa kit_coreVectorCreate
  188.  * \sa kit_coreVectorCopy
  189.  */
  190. extern int kit_coreVectorDestroy(kit_coreVector** Vector_p);
  191.  
  192. /**
  193.  * Create a new kit_coreVector
  194.  * \param[in] x Size of the vector on the x axis
  195.  * \param[in] y Size of the vector on the y axis
  196.  * \param[in] z Size of the vector on the z axis
  197.  * \param[in] unit The size of each data element, in bytes
  198.  * \param[in] type_n A user-defined number which acts as the vector's type identifier
  199.  * \return A pointer to a newly-created Vector struct, or NULL on error (call SDL_GetError() for more info)
  200.  *
  201.  * \sa kit_coreVectorDestroy
  202.  * \sa kit_coreVectorCopy
  203.  */
  204. extern kit_coreVector* kit_coreVectorCreate(Uint32 x, Uint32 y, Uint32 z, Uint32 unit, Uint64 type_n);
  205.  
  206. /**
  207.  * Create a duplicate of a kit_coreVector
  208.  * \param[in] Vector The vector to copy
  209.  * \return A pointer to a newly-copied Vector struct, or NULL on error (call SDL_GetError() for more info)
  210.  *
  211.  * \sa kit_coreVectorDestroy
  212.  * \sa kit_coreVectorCreate
  213.  */
  214. extern kit_coreVector* kit_coreVectorCopy(kit_coreVector* Vector);
  215.  
  216.  
  217. extern void kit_coreVectorPrintInt(kit_coreVector* Vector,const char* prefix); //debug
  218.  
  219. extern void kit_coreVectorPrintLens(kit_coreVector* Vector,const char* prefix); //debug
  220.  
  221. extern int kit_coreVectorTest(); //debug
  222.  
  223. /* --------------------- */
  224. /* -kit_sdl2_coreVector- */
  225. /* --------------------- */
  226.  
  227.  
  228.  
  229.  
  230. /* ++++++++++++++ */
  231. /* +kit_coreFstr+ */
  232. /* ++++++++++++++ */
  233.  
  234. #ifndef _WCHAR_T_DEFINED
  235. #  include <wchar.h>
  236. #endif
  237. #ifndef _FSTR
  238. # define _FSTR
  239. #  define fstr kit_coreFstr
  240. #endif
  241. #ifndef _FSTRW
  242. # define _FSTRW
  243. #  define fstrw kit_coreFstrw
  244. #endif
  245.  
  246.  
  247.  
  248. /**
  249.  * \brief This struct contains buffer information for fstr
  250.  */
  251. typedef struct {
  252.   union {
  253.     char*     s; ///< \brief The char portion of the string union
  254.     wchar_t*  w; ///< \brief The wchar portion of the string union
  255.   } /* ----- */ b; ///< \brief The actual string buffer union
  256.   Uint32 mem_size; ///< \brief The size of the string buffer, in bytes
  257.   Uint32 _padding; ///< \brief (unused) Another Uint32 to pad to a multiple of 8 bytes
  258. } kit_coreFstr_t;
  259.  
  260.  
  261.  
  262. /**
  263.  * Format a string, before returning that string
  264.  * \param[in,out] buffer A pointer to a kit_coreFstr_t that contains buffer information
  265.  * \param[in] fmt The format string; used the same way as the first argument to printf
  266.  * \param[in] ... List of variables to be formatted, if any
  267.  * \return A pointer to the newly-formatted string, or NULL on error (call SDL_GetError() for more info)
  268.  *
  269.  * \sa kit_coreFstrw
  270.  */
  271. extern char* kit_coreFstr(kit_coreFstr_t* buffer, const char* fmt,...);
  272.  
  273. /**
  274.  * Format a wide string, before returning that wide string
  275.  * \param[in,out] buffer A pointer to a kit_coreFstr_t that contains buffer information
  276.  * \param[in] fmt The format string; used the same way as the first argument to wprintf
  277.  * \param[in] ... List of variables to be formatted, if any
  278.  * \return A pointer to the newly-formatted wide string, or NULL on error (call SDL_GetError() for more info)
  279.  *
  280.  * \sa kit_coreFstr
  281.  */
  282. extern wchar_t* kit_coreFstrw(kit_coreFstr_t* buffer, const wchar_t* fmt,...);
  283.  
  284.  
  285. /**
  286.  * Destroy a kit_coreFstr_t buffer
  287.  * \param[in,out] buffer_p A pointer to the kit_coreFstr_t* to be destroyed (before being set to NULL)
  288.  * \return 0 on success, or a negative error code (call SDL_GetError() for more info)
  289.  *
  290.  * \sa kit_coreFstrCreate
  291.  */
  292. extern int kit_coreFstrDestroy(kit_coreFstr_t** buffer_p);
  293.  
  294. /**
  295.  * Create a new kit_coreFstr_t
  296.  * \param[in] buffer_size the size of the string's buffer, in bytes
  297.  * \return A pointer to a newly-created Fstr_t struct, or NULL on error (call SDL_GetError() for more info)
  298.  *
  299.  * \sa kit_coreFstrDestroy
  300.  */
  301. extern kit_coreFstr_t* kit_coreFstrCreate(Uint32 buffer_size);
  302.  
  303. /* -------------- */
  304. /* -kit_coreFstr- */
  305. /* -------------- */
  306.  
  307.  
  308.  
  309.  
  310. #endif /* _KIT_SDL2_CORE_H */
  311. #endif /* _KIT_CORE_H */
  312.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement