Advertisement
Kitomas

kit_core.h as of 2023-10-12

Oct 12th, 2023
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 16.09 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. #include <SDL2/SDL.h>
  12. #include "kit_macroconst.h"
  13.  
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17.  
  18.  
  19.  
  20.  
  21. /* ++++++++++ */
  22. /* +kit_core+ */
  23. /* ++++++++++ */
  24.  
  25. #if defined(_KIT_CORE_DEBUG) || defined(_KIT_ALL_DEBUG)
  26. #define kit_coreLog(...) SDL_Log(__VA_ARGS__)
  27. #else
  28. #define kit_coreLog(...) ;
  29. #endif
  30.  
  31.  
  32.  
  33. #define NO_MEMSET (0xFFFFFFFEEEEEEEEE) ///< \brief used for kit_coreRealloc
  34.  
  35.  
  36.  
  37. extern const SDL_bool kit_coreIsDebug;
  38.  
  39.  
  40. extern const char boolstr[2][6]; //"false" and "true"
  41.  
  42.  
  43. /**
  44.  * \name Inverses of numbers (useful for normalizing floats quickly!)
  45.  */
  46. /** @{ */
  47. //used to multiply an int by the inverse of an int to get a normalized float
  48. extern const float inv_i_8; ///< \brief 1.0f/0x7f       = 0.007874015748031496062992125984251968503937
  49. extern const float inv_i16; ///< \brief 1.0f/0x7fff     = 0.000030518509475997192297128208258308664204
  50. extern const float inv_i32; ///< \brief 1.0f/0x7fffffff = 0.000000000465661287524579692410575082716799
  51.  
  52. //same thing, but for pi
  53. extern const float inv_qpi; ///< \brief 1.0f/(pi/4) = 1.27323954474
  54. extern const float inv_hpi; ///< \brief 1.0f/(pi/2) = 0.63661977236
  55. extern const float inv_pi ; ///< \brief 1.0f/(pi  ) = 0.31830988618
  56. extern const float inv_pi2; ///< \brief 1.0f/(pi*2) = 0.15915494309
  57. /** @} */
  58.  
  59.  
  60.  
  61. extern float kit_coreSawf(float x); //like sinf, but for a sawtooth wave
  62.  
  63. extern float kit_coreTrif(float x); //like sinf, but for a triangle wave
  64.  
  65.  
  66. //extern void* kit_coreMemcpy(void* dst, const void* src, size_t size);
  67. static inline void* kit_coreMemcpy(void* dst, const void* src, size_t size){
  68.   return SDL_memcpy(dst,src,size);
  69. }
  70.  
  71. //extern void* kit_coreMemset(void* dst, int value, size_t size);
  72. static inline void* kit_coreMemset(void* dst, int value, size_t size){
  73.   return SDL_memset(dst, value, size);
  74. }
  75.  
  76. /**
  77.  * Reallocate memory, optionally setting any new allocated memory to 0
  78.  * \param[in,out] ptr_p A pointer to the void* to realloc
  79.  * \param[in] size_old The old size of the allocated memory (set to NO_MEMSET to not call memset)
  80.  * \param[in] size_new The target size of the new memory
  81.  * \return 0 on success, or <0 on error (call SDL_GetError() for more info)
  82.  *
  83.  * \remark (*ptr_p will only be set on success!)
  84.  */
  85. extern int kit_coreRealloc(void* ptr_p, size_t size_old, size_t size_new);
  86.  
  87.  
  88. /**
  89.  * Initialize core
  90.  * \param[in] flags What flags to use for SDL_Init() (0 to not initialize SDL at all)
  91.  * \return 0 on success, >0 on warning, or <0 on error (call SDL_GetError() for more info)
  92.  *
  93.  * \sa kit_coreQuit
  94.  */
  95. extern int kit_coreInit(Uint32 flags);
  96.  
  97. /**
  98.  * Shut down core
  99.  * \return 0 on success, >0 on warning, <0 on error (call SDL_GetError() for more info)
  100.  *
  101.  * \sa kit_coreInit
  102.  */
  103. extern int kit_coreQuit();
  104.  
  105. /* ---------- */
  106. /* -kit_core- */
  107. /* ---------- */
  108.  
  109.  
  110.  
  111.  
  112. /* ++++++++++++++++ */
  113. /* +kit_coreThread+ */
  114. /* ++++++++++++++++ */
  115.  
  116. typedef struct { //32B
  117.   SDL_Thread* thread;
  118.   void*         data;
  119.   SDL_mutex*    lock;
  120.   int _,returnStatus;
  121. } kit_coreThread;
  122.  
  123. /* ---------------- */
  124. /* -kit_coreThread- */
  125. /* ---------------- */
  126.  
  127.  
  128.  
  129.  
  130. /* +++++++++++++++++++++ */
  131. /* +kit_sdl2_coreVector+ */
  132. /* +++++++++++++++++++++ */
  133.  
  134. //assumes that _vector is a vector pointer
  135.  //(also assumes that datasize is < 2^32)
  136. #define PRINT_VECTOR(_pref, _vector) {                           \
  137.   kit_coreLog(_pref"->type.s   = \"%s\"",(_vector)->type.s);       \
  138.   kit_coreLog(_pref"->datasize = %u",(Uint32)(_vector)->datasize); \
  139.   kit_coreLog(_pref"->x        = %u",(_vector)->x);                \
  140.   kit_coreLog(_pref"->y        = %u",(_vector)->y);                \
  141.   kit_coreLog(_pref"->z        = %u",(_vector)->z);                \
  142.   kit_coreLog(_pref"->unit     = %u",(_vector)->unit);             \
  143.   kit_coreLog(_pref"->z_block  = %u",(_vector)->z_block);          \
  144.   kit_coreLog(_pref"->lenslen  = %u",(_vector)->lenslen);          \
  145.   kit_coreLog(_pref"->lens     = %p",(_vector)->lens);             \
  146.   kit_coreLog(_pref"->data     = %p",(_vector)->data);             }
  147.  
  148.  
  149. /**
  150.  * \name Macros for accessing data elements of a kit_coreVector*
  151.  */
  152. /** @{ */
  153. #define VECTOR_INDEX_A(_type, _vector, _x,_y,_z) \
  154.   ((_type*)(_vector)->data)[ (_x) + (_y)*(_vector)->x + (_z)*(_vector)->z_block ]
  155.  
  156. #define VECTOR_INDEX_B(_type, _vector, _x_raw,_y_raw,_z_raw) \
  157.   ((_type*)(_vector)->data)[ (_x_raw) + (_y_raw) + (_z_raw) ]
  158.  
  159. #define VECTOR_INDEX_C(_type, _vector, _index_raw) \
  160.   ((_type*)(_vector)->data)[ (_index_raw) ]
  161. /** @} */
  162.  
  163.  
  164. /**
  165.  * \name Macros for accessing lengths of a kit_coreVector* 's axes
  166.  */
  167. /** @{ */
  168. #define VECTOR_LENS_A(_vector, _y,_z) \
  169.   (_vector)->lens[ (_y) + (_z)*(_vector)->y ]
  170.  
  171. #define VECTOR_LENS_B(_vector, _y_raw,_z_raw) \
  172.   (_vector)->lens[ (_y_raw) + (_z_raw) ]
  173.  
  174. #define VECTOR_LENS_C(_vector, _index_raw) \
  175.   (_vector)->lens[ (_index_raw) ]
  176. /** @} */
  177.  
  178.  
  179.  
  180. /**
  181.  * The callback type used in kit_coreVectorTrim, and kit_coreVectorInsert
  182.  * \param[in] unit A pointer to the location of the unit to be compared
  183.  * \param[in] size The size of that unit, in bytes
  184.  * \return SDL_TRUE if unit should be trimmed (or replaced if inserting), SDL_FALSE otherwise
  185.  */
  186. typedef SDL_bool (*kit_coreVectorUnitCallback) (void* unit, Uint32 size);
  187.  
  188.  
  189.  
  190. /**
  191.  * \brief The struct for a contiguous dynamic array
  192.  */
  193. typedef struct kit_coreVector { //56B (assuming that a void* is 8 bytes)
  194.   union {
  195.     char   s[8]; ///< \brief String portion of ID (albeit a short string)
  196.     Uint64    n; ///< \brief Integer portion of ID
  197.   } /*----*/ type; ///< \brief A user-defined type identifier
  198.   Uint64 datasize; ///< \brief Total allocated data size, in bytes (does not include the struct itself)
  199.   Uint32        x; ///< \brief Length of the vector's (allocated) x axis
  200.   Uint32        y; ///< \brief Length of the vector's (allocated) y axis
  201.   Uint32        z; ///< \brief Length of the vector's (allocated) z axis
  202.   Uint32     unit; ///< \brief Size of each data element
  203.   Uint32  z_block; ///< \brief Is equal to x*y
  204.   Uint32  lenslen; ///< \brief Allocated size of lens, in # of (Uint32) elements
  205.   Uint32*    lens; ///< \brief lengths for individual axes (must be <= the actual allocated size)
  206.   void*      data; ///< \brief The actual array portion of the vector (technically a 1D block of memory)
  207. } kit_coreVector;
  208.  
  209.  
  210.  
  211. /**
  212.  * Set the size of a kit_coreVector
  213.  * \param[in,out] Vector_p A pointer to the kit_coreVector* to be altered
  214.  * \param[in] x_new New size for the x axis (set to 0 to leave x unchanged)
  215.  * \param[in] y_new New size for the y axis (set to 0 to leave y unchanged)
  216.  * \param[in] z_new New size for the z axis (set to 0 to leave z unchanged)
  217.  * \return 0 on success, or -1 on error (call SDL_GetError() for more info)
  218.  *
  219.  * \sa kit_coreVectorAdd
  220.  * \sa kit_coreVectorTrim
  221.  * \sa kit_coreVectorAppend
  222.  * \sa kit_coreVectorInsert
  223.  */
  224. extern int kit_coreVectorSet(kit_coreVector** Vector_p, Uint32 x_new, Uint32 y_new, Uint32 z_new);
  225.  
  226. /**
  227.  * Add to or subtract from size of a kit_coreVector
  228.  * \param[in,out] Vector_p A pointer to the kit_coreVector* to be altered
  229.  * \param[in] x_add How much to increase or decrease the x axis
  230.  * \param[in] y_add How much to increase or decrease the y axis
  231.  * \param[in] z_add How much to increase or decrease the z axis
  232.  * \return 0 on success, or -1 on error (call SDL_GetError() for more info)
  233.  *
  234.  * \sa kit_coreVectorSet
  235.  * \sa kit_coreVectorTrim
  236.  * \sa kit_coreVectorAppend
  237.  * \sa kit_coreVectorInsert
  238.  */
  239. extern int kit_coreVectorAdd(kit_coreVector** Vector_p, Sint32 x_add, Sint32 y_add, Sint32 z_add);
  240.  
  241. /**
  242.  * Trim a kit_coreVector to the last unit that returns a true comparison
  243.  * \param[in,out] Vector_p A pointer to the kit_coreVector* to be altered
  244.  * \param[in] axis Which axis to trim; one of 'x', 'y', 'z', or 0 (case-insensitive; 0 means all valid axes)
  245.  * \param[in] callback A kit_coreVectorUnitCallback function pointer used for comparing units (can be NULL; see remarks)
  246.  * \return 0 on success, or -1 on error (call SDL_GetError() for more info)
  247.  *
  248.  * \remark If callback is NULL, comparisons return true if a unit's bytes are all set to 0
  249.  * \sa kit_coreVectorSet
  250.  * \sa kit_coreVectorAdd
  251.  * \sa kit_coreVectorAppend
  252.  * \sa kit_coreVectorInsert
  253.  */
  254. extern int kit_coreVectorTrim(kit_coreVector** Vector_p, char axis,
  255.                               kit_coreVectorUnitCallback callback);
  256.  
  257. /**
  258.  * Append an element to the end of a kit_coreVector x axis
  259.  * \param[in,out] Vector_p A pointer to the kit_coreVector* to be altered
  260.  * \param[in] src A pointer to the data to be copied to the end of the vector
  261.  * \param[in] y_pos The y index to append to (set to 0 if vector is <2D)
  262.  * \param[in] z_pos The z index to append to (set to 0 if vector is <3D)
  263.  * \return The newly-created x index, or 0xffffffff (-1 when interpreted as signed) on error (call SDL_GetError() for more info)
  264.  *
  265.  * \remark *src must be *Vector_p->unit bytes in size or bad things will happen
  266.  * \sa kit_coreVectorSet
  267.  * \sa kit_coreVectorAdd
  268.  * \sa kit_coreVectorTrim
  269.  * \sa kit_coreVectorInsert
  270.  */
  271. extern Uint32 kit_coreVectorAppend(kit_coreVector** Vector_p, void* src, Uint32 y_pos, Uint32 z_pos);
  272.  
  273. /**
  274.  * Insert an element into a kit_coreVector x axis, appending if necessary
  275.  * \param[in,out] Vector_p A pointer to the kit_coreVector* to be altered
  276.  * \param[in] src A pointer to the data to be inserted
  277.  * \param[in] y_pos The y index to insert into (set to 0 if vector is <2D)
  278.  * \param[in] z_pos The z index to insert into (set to 0 if vector is <3D)
  279.  * \param[in] callback A kit_coreVectorUnitCallback function pointer used for comparing units (can be NULL; see remarks)
  280.  * \return The resulting x index, or 0xffffffff (-1 when interpreted as signed) on error (call SDL_GetError() for more info)
  281.  *
  282.  * \remark If callback is NULL, comparisons return true if a unit's bytes are all set to 0. \n
  283.  * \remark Like kit_coreVectorAppend, *src must be *Vector_p->unit bytes in size or bad things will happen
  284.  * \sa kit_coreVectorSet
  285.  * \sa kit_coreVectorAdd
  286.  * \sa kit_coreVectorTrim
  287.  * \sa kit_coreVectorAppend
  288.  */
  289. extern Uint32 kit_coreVectorInsert(kit_coreVector** Vector_p, void* src,
  290.                                    Uint32 y_pos, Uint32 z_pos,
  291.                                    kit_coreVectorUnitCallback callback);
  292.  
  293.  
  294. /**
  295.  * Destroy a kit_coreVector
  296.  * \param[in,out] Vector_p A pointer to the kit_coreVector* to be destroyed (before being set to NULL)
  297.  * \return 0 on success, or -1 on error (call SDL_GetError() for more info)
  298.  *
  299.  * \sa kit_coreVectorCreate
  300.  * \sa kit_coreVectorCopy
  301.  */
  302. extern int kit_coreVectorDestroy(kit_coreVector** Vector_p);
  303.  
  304. /**
  305.  * Create a new kit_coreVector
  306.  * \param[in] x Size of the vector on the x axis
  307.  * \param[in] y Size of the vector on the y axis
  308.  * \param[in] z Size of the vector on the z axis
  309.  * \param[in] unit The size of each data element, in bytes
  310.  * \param[in] type_n A user-defined number which acts as the vector's type identifier
  311.  * \return A pointer to a newly-created Vector struct, or NULL on error (call SDL_GetError() for more info)
  312.  *
  313.  * \sa kit_coreVectorDestroy
  314.  * \sa kit_coreVectorCopy
  315.  */
  316. extern kit_coreVector* kit_coreVectorCreate(Uint32 x, Uint32 y, Uint32 z, Uint32 unit, Uint64 type_n);
  317.  
  318. /**
  319.  * Create a duplicate of a kit_coreVector
  320.  * \param[in] Vector The vector to copy
  321.  * \return A pointer to a newly-copied Vector struct, or NULL on error (call SDL_GetError() for more info)
  322.  *
  323.  * \sa kit_coreVectorDestroy
  324.  * \sa kit_coreVectorCreate
  325.  */
  326. extern kit_coreVector* kit_coreVectorCopy(kit_coreVector* Vector);
  327.  
  328.  
  329. extern void kit_coreVectorPrintInt(kit_coreVector* Vector,const char* prefix); //debug
  330.  
  331. extern void kit_coreVectorPrintLens(kit_coreVector* Vector,const char* prefix); //debug
  332.  
  333. extern int kit_coreVector_Test(); //debug
  334.  
  335. /* --------------------- */
  336. /* -kit_sdl2_coreVector- */
  337. /* --------------------- */
  338.  
  339.  
  340.  
  341.  
  342. /* +++++++++++++++++++ */
  343. /* +kit_sdl2_coreFile+ */
  344. /* +++++++++++++++++++ */
  345.  
  346.  
  347.  
  348. #define DEFAULT_RW_CHUNK_SIZE (4096) ///> \brief The default chunk size for file i/o
  349.  
  350.  
  351.  
  352. /**
  353.  * Get the size of a file
  354.  * \param[in] filePath The file to get the size of
  355.  * \return The size of that file, in bytes
  356.  */
  357. extern size_t kit_coreFileSize(const char* filePath);
  358.  
  359.  
  360. /**
  361.  * Read the contents of a binary file
  362.  * \param[in] filePath The file to read from
  363.  * \param[out] buffer_p The location of a pointer to be filled in with the file data
  364.  * \param[in] chunkSize How many bytes to read at a time (set to 0 to use the default of 4096)
  365.  * \return The size of the file's data, in bytes (or 0 in the event of an error; call SDL_GetError() for details)
  366.  *
  367.  * \remark Since the buffer uses heap memory, make sure to free the buffer when it's no longer in use.
  368.  * \sa kit_coreFileWriteBin
  369.  */
  370. extern size_t kit_coreFileReadBin(const char* filePath, void* buffer_p, size_t chunkSize);
  371.  
  372. /**
  373.  * Write the contents of a buffer to a binary file
  374.  * \param[in] filePath The file name to write to
  375.  * \param[in] buffer A pointer to the memory that'll be written
  376.  * \param[in] bufferSize The size of that buffer, in bytes
  377.  * \param[in] chunkSize How many bytes to write at a time (set to 0 to use the default of 4096)
  378.  * \return 0 on success, or -1 on failure
  379.  *
  380.  * \sa kit_coreFileReadBin
  381.  */
  382. extern int kit_coreFileWriteBin(const char* filePath, void* buffer,
  383.                                 size_t bufferSize, size_t chunkSize);
  384.  
  385. /* ------------------- */
  386. /* -kit_sdl2_coreFile- */
  387. /* ------------------- */
  388.  
  389.  
  390.  
  391.  
  392. /* ++++++++++++++ */
  393. /* +kit_coreFstr+ */
  394. /* ++++++++++++++ */
  395.  
  396. #ifndef _WCHAR_T_DEFINED
  397. #  include <wchar.h>
  398. #endif
  399. #ifndef _FSTR
  400. # define _FSTR
  401. #  define fstr kit_coreFstr
  402. #endif
  403. #ifndef _FSTRW
  404. # define _FSTRW
  405. #  define fstrw kit_coreFstrw
  406. #endif
  407.  
  408.  
  409.  
  410. /**
  411.  * \brief This struct contains buffer information for fstr
  412.  */
  413. typedef struct kit_coreFstr_t {
  414.   union {
  415.     char*     s; ///< \brief The char portion of the string union
  416.     wchar_t*  w; ///< \brief The wchar portion of the string union
  417.   } /* ----- */ b; ///< \brief The actual string buffer union
  418.   Uint32 mem_size; ///< \brief The size of the string buffer, in bytes
  419.   Uint32 _padding; ///< \brief (unused) Another Uint32 to pad to a multiple of 8 bytes
  420. } kit_coreFstr_t;
  421.  
  422.  
  423.  
  424. /**
  425.  * Format a string, before returning that string
  426.  * \param[in,out] buffer A pointer to a kit_coreFstr_t that contains buffer information
  427.  * \param[in] fmt The format string; used the same way as the first argument to printf
  428.  * \param[in] ... List of variables to be formatted, if any
  429.  * \return A pointer to the newly-formatted string, or NULL on error (call SDL_GetError() for more info)
  430.  *
  431.  * \sa kit_coreFstrw
  432.  */
  433. extern char* kit_coreFstr(kit_coreFstr_t* buffer, const char* fmt,...);
  434.  
  435. /**
  436.  * Format a wide string, before returning that wide string
  437.  * \param[in,out] buffer A pointer to a kit_coreFstr_t that contains buffer information
  438.  * \param[in] fmt The format string; used the same way as the first argument to wprintf
  439.  * \param[in] ... List of variables to be formatted, if any
  440.  * \return A pointer to the newly-formatted wide string, or NULL on error (call SDL_GetError() for more info)
  441.  *
  442.  * \sa kit_coreFstr
  443.  */
  444. extern wchar_t* kit_coreFstrw(kit_coreFstr_t* buffer, const wchar_t* fmt,...);
  445.  
  446.  
  447. /**
  448.  * Destroy a kit_coreFstr_t buffer
  449.  * \param[in,out] buffer_p A pointer to the kit_coreFstr_t* to be destroyed (before being set to NULL)
  450.  * \return 0 on success, or a negative error code (call SDL_GetError() for more info)
  451.  *
  452.  * \sa kit_coreFstrCreate
  453.  */
  454. extern int kit_coreFstrDestroy(kit_coreFstr_t** buffer_p);
  455.  
  456. /**
  457.  * Create a new kit_coreFstr_t
  458.  * \param[in] buffer_size the size of the string's buffer, in bytes
  459.  * \return A pointer to a newly-created Fstr_t struct, or NULL on error (call SDL_GetError() for more info)
  460.  *
  461.  * \sa kit_coreFstrDestroy
  462.  */
  463. extern kit_coreFstr_t* kit_coreFstrCreate(Uint32 buffer_size);
  464.  
  465. /* -------------- */
  466. /* -kit_coreFstr- */
  467. /* -------------- */
  468.  
  469.  
  470.  
  471.  
  472. #ifdef __cplusplus
  473. }
  474. #endif
  475.  
  476. #endif /* _KIT_SDL2_CORE_H */
  477. #endif /* _KIT_CORE_H */
  478.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement