Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * \file kit_core.h
- * \brief Header file for KIT SDL2's core library
- */
- #ifndef _KIT_CORE_H
- #define _KIT_CORE_H
- #ifndef _KIT_SDL2_CORE_H
- #define _KIT_SDL2_CORE_H
- #include <SDL2/SDL.h>
- #include "kit_macroconst.h"
- #ifdef __cplusplus
- extern "C" {
- #endif
- /* ++++++++++ */
- /* +kit_core+ */
- /* ++++++++++ */
- #if defined(_KIT_CORE_DEBUG) || defined(_KIT_ALL_DEBUG)
- #define kit_coreLog(...) SDL_Log(__VA_ARGS__)
- #else
- #define kit_coreLog(...) ;
- #endif
- #define NO_MEMSET (0xFFFFFFFEEEEEEEEE) ///< \brief used for kit_coreRealloc
- extern const SDL_bool kit_coreIsDebug;
- extern const char boolstr[2][6]; //"false" and "true"
- /**
- * \name Inverses of numbers (useful for normalizing floats quickly!)
- */
- /** @{ */
- //used to multiply an int by the inverse of an int to get a normalized float
- extern const float inv_i_8; ///< \brief 1.0f/0x7f = 0.007874015748031496062992125984251968503937
- extern const float inv_i16; ///< \brief 1.0f/0x7fff = 0.000030518509475997192297128208258308664204
- extern const float inv_i32; ///< \brief 1.0f/0x7fffffff = 0.000000000465661287524579692410575082716799
- //same thing, but for pi
- extern const float inv_qpi; ///< \brief 1.0f/(pi/4) = 1.27323954474
- extern const float inv_hpi; ///< \brief 1.0f/(pi/2) = 0.63661977236
- extern const float inv_pi ; ///< \brief 1.0f/(pi ) = 0.31830988618
- extern const float inv_pi2; ///< \brief 1.0f/(pi*2) = 0.15915494309
- /** @} */
- extern float kit_coreSawf(float x); //like sinf, but for a sawtooth wave
- extern float kit_coreTrif(float x); //like sinf, but for a triangle wave
- //extern void* kit_coreMemcpy(void* dst, const void* src, size_t size);
- static inline void* kit_coreMemcpy(void* dst, const void* src, size_t size){
- return SDL_memcpy(dst,src,size);
- }
- //extern void* kit_coreMemset(void* dst, int value, size_t size);
- static inline void* kit_coreMemset(void* dst, int value, size_t size){
- return SDL_memset(dst, value, size);
- }
- /**
- * Reallocate memory, optionally setting any new allocated memory to 0
- * \param[in,out] ptr_p A pointer to the void* to realloc
- * \param[in] size_old The old size of the allocated memory (set to NO_MEMSET to not call memset)
- * \param[in] size_new The target size of the new memory
- * \return 0 on success, or <0 on error (call SDL_GetError() for more info)
- *
- * \remark (*ptr_p will only be set on success!)
- */
- extern int kit_coreRealloc(void* ptr_p, size_t size_old, size_t size_new);
- /**
- * Initialize core
- * \param[in] flags What flags to use for SDL_Init() (0 to not initialize SDL at all)
- * \return 0 on success, >0 on warning, or <0 on error (call SDL_GetError() for more info)
- *
- * \sa kit_coreQuit
- */
- extern int kit_coreInit(Uint32 flags);
- /**
- * Shut down core
- * \return 0 on success, >0 on warning, <0 on error (call SDL_GetError() for more info)
- *
- * \sa kit_coreInit
- */
- extern int kit_coreQuit();
- /* ---------- */
- /* -kit_core- */
- /* ---------- */
- /* ++++++++++++++++ */
- /* +kit_coreThread+ */
- /* ++++++++++++++++ */
- typedef struct { //32B
- SDL_Thread* thread;
- void* data;
- SDL_mutex* lock;
- int _,returnStatus;
- } kit_coreThread;
- /* ---------------- */
- /* -kit_coreThread- */
- /* ---------------- */
- /* +++++++++++++++++++++ */
- /* +kit_sdl2_coreVector+ */
- /* +++++++++++++++++++++ */
- //assumes that _vector is a vector pointer
- //(also assumes that datasize is < 2^32)
- #define PRINT_VECTOR(_pref, _vector) { \
- kit_coreLog(_pref"->type.s = \"%s\"",(_vector)->type.s); \
- kit_coreLog(_pref"->datasize = %u",(Uint32)(_vector)->datasize); \
- kit_coreLog(_pref"->x = %u",(_vector)->x); \
- kit_coreLog(_pref"->y = %u",(_vector)->y); \
- kit_coreLog(_pref"->z = %u",(_vector)->z); \
- kit_coreLog(_pref"->unit = %u",(_vector)->unit); \
- kit_coreLog(_pref"->z_block = %u",(_vector)->z_block); \
- kit_coreLog(_pref"->lenslen = %u",(_vector)->lenslen); \
- kit_coreLog(_pref"->lens = %p",(_vector)->lens); \
- kit_coreLog(_pref"->data = %p",(_vector)->data); }
- /**
- * \name Macros for accessing data elements of a kit_coreVector*
- */
- /** @{ */
- #define VECTOR_INDEX_A(_type, _vector, _x,_y,_z) \
- ((_type*)(_vector)->data)[ (_x) + (_y)*(_vector)->x + (_z)*(_vector)->z_block ]
- #define VECTOR_INDEX_B(_type, _vector, _x_raw,_y_raw,_z_raw) \
- ((_type*)(_vector)->data)[ (_x_raw) + (_y_raw) + (_z_raw) ]
- #define VECTOR_INDEX_C(_type, _vector, _index_raw) \
- ((_type*)(_vector)->data)[ (_index_raw) ]
- /** @} */
- /**
- * \name Macros for accessing lengths of a kit_coreVector* 's axes
- */
- /** @{ */
- #define VECTOR_LENS_A(_vector, _y,_z) \
- (_vector)->lens[ (_y) + (_z)*(_vector)->y ]
- #define VECTOR_LENS_B(_vector, _y_raw,_z_raw) \
- (_vector)->lens[ (_y_raw) + (_z_raw) ]
- #define VECTOR_LENS_C(_vector, _index_raw) \
- (_vector)->lens[ (_index_raw) ]
- /** @} */
- /**
- * The callback type used in kit_coreVectorTrim, and kit_coreVectorInsert
- * \param[in] unit A pointer to the location of the unit to be compared
- * \param[in] size The size of that unit, in bytes
- * \return SDL_TRUE if unit should be trimmed (or replaced if inserting), SDL_FALSE otherwise
- */
- typedef SDL_bool (*kit_coreVectorUnitCallback) (void* unit, Uint32 size);
- /**
- * \brief The struct for a contiguous dynamic array
- */
- typedef struct kit_coreVector { //56B (assuming that a void* is 8 bytes)
- union {
- char s[8]; ///< \brief String portion of ID (albeit a short string)
- Uint64 n; ///< \brief Integer portion of ID
- } /*----*/ type; ///< \brief A user-defined type identifier
- Uint64 datasize; ///< \brief Total allocated data size, in bytes (does not include the struct itself)
- Uint32 x; ///< \brief Length of the vector's (allocated) x axis
- Uint32 y; ///< \brief Length of the vector's (allocated) y axis
- Uint32 z; ///< \brief Length of the vector's (allocated) z axis
- Uint32 unit; ///< \brief Size of each data element
- Uint32 z_block; ///< \brief Is equal to x*y
- Uint32 lenslen; ///< \brief Allocated size of lens, in # of (Uint32) elements
- Uint32* lens; ///< \brief lengths for individual axes (must be <= the actual allocated size)
- void* data; ///< \brief The actual array portion of the vector (technically a 1D block of memory)
- } kit_coreVector;
- /**
- * Set the size of a kit_coreVector
- * \param[in,out] Vector_p A pointer to the kit_coreVector* to be altered
- * \param[in] x_new New size for the x axis (set to 0 to leave x unchanged)
- * \param[in] y_new New size for the y axis (set to 0 to leave y unchanged)
- * \param[in] z_new New size for the z axis (set to 0 to leave z unchanged)
- * \return 0 on success, or -1 on error (call SDL_GetError() for more info)
- *
- * \sa kit_coreVectorAdd
- * \sa kit_coreVectorTrim
- * \sa kit_coreVectorAppend
- * \sa kit_coreVectorInsert
- */
- extern int kit_coreVectorSet(kit_coreVector** Vector_p, Uint32 x_new, Uint32 y_new, Uint32 z_new);
- /**
- * Add to or subtract from size of a kit_coreVector
- * \param[in,out] Vector_p A pointer to the kit_coreVector* to be altered
- * \param[in] x_add How much to increase or decrease the x axis
- * \param[in] y_add How much to increase or decrease the y axis
- * \param[in] z_add How much to increase or decrease the z axis
- * \return 0 on success, or -1 on error (call SDL_GetError() for more info)
- *
- * \sa kit_coreVectorSet
- * \sa kit_coreVectorTrim
- * \sa kit_coreVectorAppend
- * \sa kit_coreVectorInsert
- */
- extern int kit_coreVectorAdd(kit_coreVector** Vector_p, Sint32 x_add, Sint32 y_add, Sint32 z_add);
- /**
- * Trim a kit_coreVector to the last unit that returns a true comparison
- * \param[in,out] Vector_p A pointer to the kit_coreVector* to be altered
- * \param[in] axis Which axis to trim; one of 'x', 'y', 'z', or 0 (case-insensitive; 0 means all valid axes)
- * \param[in] callback A kit_coreVectorUnitCallback function pointer used for comparing units (can be NULL; see remarks)
- * \return 0 on success, or -1 on error (call SDL_GetError() for more info)
- *
- * \remark If callback is NULL, comparisons return true if a unit's bytes are all set to 0
- * \sa kit_coreVectorSet
- * \sa kit_coreVectorAdd
- * \sa kit_coreVectorAppend
- * \sa kit_coreVectorInsert
- */
- extern int kit_coreVectorTrim(kit_coreVector** Vector_p, char axis,
- kit_coreVectorUnitCallback callback);
- /**
- * Append an element to the end of a kit_coreVector x axis
- * \param[in,out] Vector_p A pointer to the kit_coreVector* to be altered
- * \param[in] src A pointer to the data to be copied to the end of the vector
- * \param[in] y_pos The y index to append to (set to 0 if vector is <2D)
- * \param[in] z_pos The z index to append to (set to 0 if vector is <3D)
- * \return The newly-created x index, or 0xffffffff (-1 when interpreted as signed) on error (call SDL_GetError() for more info)
- *
- * \remark *src must be *Vector_p->unit bytes in size or bad things will happen
- * \sa kit_coreVectorSet
- * \sa kit_coreVectorAdd
- * \sa kit_coreVectorTrim
- * \sa kit_coreVectorInsert
- */
- extern Uint32 kit_coreVectorAppend(kit_coreVector** Vector_p, void* src, Uint32 y_pos, Uint32 z_pos);
- /**
- * Insert an element into a kit_coreVector x axis, appending if necessary
- * \param[in,out] Vector_p A pointer to the kit_coreVector* to be altered
- * \param[in] src A pointer to the data to be inserted
- * \param[in] y_pos The y index to insert into (set to 0 if vector is <2D)
- * \param[in] z_pos The z index to insert into (set to 0 if vector is <3D)
- * \param[in] callback A kit_coreVectorUnitCallback function pointer used for comparing units (can be NULL; see remarks)
- * \return The resulting x index, or 0xffffffff (-1 when interpreted as signed) on error (call SDL_GetError() for more info)
- *
- * \remark If callback is NULL, comparisons return true if a unit's bytes are all set to 0. \n
- * \remark Like kit_coreVectorAppend, *src must be *Vector_p->unit bytes in size or bad things will happen
- * \sa kit_coreVectorSet
- * \sa kit_coreVectorAdd
- * \sa kit_coreVectorTrim
- * \sa kit_coreVectorAppend
- */
- extern Uint32 kit_coreVectorInsert(kit_coreVector** Vector_p, void* src,
- Uint32 y_pos, Uint32 z_pos,
- kit_coreVectorUnitCallback callback);
- /**
- * Destroy a kit_coreVector
- * \param[in,out] Vector_p A pointer to the kit_coreVector* to be destroyed (before being set to NULL)
- * \return 0 on success, or -1 on error (call SDL_GetError() for more info)
- *
- * \sa kit_coreVectorCreate
- * \sa kit_coreVectorCopy
- */
- extern int kit_coreVectorDestroy(kit_coreVector** Vector_p);
- /**
- * Create a new kit_coreVector
- * \param[in] x Size of the vector on the x axis
- * \param[in] y Size of the vector on the y axis
- * \param[in] z Size of the vector on the z axis
- * \param[in] unit The size of each data element, in bytes
- * \param[in] type_n A user-defined number which acts as the vector's type identifier
- * \return A pointer to a newly-created Vector struct, or NULL on error (call SDL_GetError() for more info)
- *
- * \sa kit_coreVectorDestroy
- * \sa kit_coreVectorCopy
- */
- extern kit_coreVector* kit_coreVectorCreate(Uint32 x, Uint32 y, Uint32 z, Uint32 unit, Uint64 type_n);
- /**
- * Create a duplicate of a kit_coreVector
- * \param[in] Vector The vector to copy
- * \return A pointer to a newly-copied Vector struct, or NULL on error (call SDL_GetError() for more info)
- *
- * \sa kit_coreVectorDestroy
- * \sa kit_coreVectorCreate
- */
- extern kit_coreVector* kit_coreVectorCopy(kit_coreVector* Vector);
- extern void kit_coreVectorPrintInt(kit_coreVector* Vector,const char* prefix); //debug
- extern void kit_coreVectorPrintLens(kit_coreVector* Vector,const char* prefix); //debug
- extern int kit_coreVector_Test(); //debug
- /* --------------------- */
- /* -kit_sdl2_coreVector- */
- /* --------------------- */
- /* +++++++++++++++++++ */
- /* +kit_sdl2_coreFile+ */
- /* +++++++++++++++++++ */
- #define DEFAULT_RW_CHUNK_SIZE (4096) ///> \brief The default chunk size for file i/o
- /**
- * Get the size of a file
- * \param[in] filePath The file to get the size of
- * \return The size of that file, in bytes
- */
- extern size_t kit_coreFileSize(const char* filePath);
- /**
- * Read the contents of a binary file
- * \param[in] filePath The file to read from
- * \param[out] buffer_p The location of a pointer to be filled in with the file data
- * \param[in] chunkSize How many bytes to read at a time (set to 0 to use the default of 4096)
- * \return The size of the file's data, in bytes (or 0 in the event of an error; call SDL_GetError() for details)
- *
- * \remark Since the buffer uses heap memory, make sure to free the buffer when it's no longer in use.
- * \sa kit_coreFileWriteBin
- */
- extern size_t kit_coreFileReadBin(const char* filePath, void* buffer_p, size_t chunkSize);
- /**
- * Write the contents of a buffer to a binary file
- * \param[in] filePath The file name to write to
- * \param[in] buffer A pointer to the memory that'll be written
- * \param[in] bufferSize The size of that buffer, in bytes
- * \param[in] chunkSize How many bytes to write at a time (set to 0 to use the default of 4096)
- * \return 0 on success, or -1 on failure
- *
- * \sa kit_coreFileReadBin
- */
- extern int kit_coreFileWriteBin(const char* filePath, void* buffer,
- size_t bufferSize, size_t chunkSize);
- /* ------------------- */
- /* -kit_sdl2_coreFile- */
- /* ------------------- */
- /* ++++++++++++++ */
- /* +kit_coreFstr+ */
- /* ++++++++++++++ */
- #ifndef _WCHAR_T_DEFINED
- # include <wchar.h>
- #endif
- #ifndef _FSTR
- # define _FSTR
- # define fstr kit_coreFstr
- #endif
- #ifndef _FSTRW
- # define _FSTRW
- # define fstrw kit_coreFstrw
- #endif
- /**
- * \brief This struct contains buffer information for fstr
- */
- typedef struct kit_coreFstr_t {
- union {
- char* s; ///< \brief The char portion of the string union
- wchar_t* w; ///< \brief The wchar portion of the string union
- } /* ----- */ b; ///< \brief The actual string buffer union
- Uint32 mem_size; ///< \brief The size of the string buffer, in bytes
- Uint32 _padding; ///< \brief (unused) Another Uint32 to pad to a multiple of 8 bytes
- } kit_coreFstr_t;
- /**
- * Format a string, before returning that string
- * \param[in,out] buffer A pointer to a kit_coreFstr_t that contains buffer information
- * \param[in] fmt The format string; used the same way as the first argument to printf
- * \param[in] ... List of variables to be formatted, if any
- * \return A pointer to the newly-formatted string, or NULL on error (call SDL_GetError() for more info)
- *
- * \sa kit_coreFstrw
- */
- extern char* kit_coreFstr(kit_coreFstr_t* buffer, const char* fmt,...);
- /**
- * Format a wide string, before returning that wide string
- * \param[in,out] buffer A pointer to a kit_coreFstr_t that contains buffer information
- * \param[in] fmt The format string; used the same way as the first argument to wprintf
- * \param[in] ... List of variables to be formatted, if any
- * \return A pointer to the newly-formatted wide string, or NULL on error (call SDL_GetError() for more info)
- *
- * \sa kit_coreFstr
- */
- extern wchar_t* kit_coreFstrw(kit_coreFstr_t* buffer, const wchar_t* fmt,...);
- /**
- * Destroy a kit_coreFstr_t buffer
- * \param[in,out] buffer_p A pointer to the kit_coreFstr_t* to be destroyed (before being set to NULL)
- * \return 0 on success, or a negative error code (call SDL_GetError() for more info)
- *
- * \sa kit_coreFstrCreate
- */
- extern int kit_coreFstrDestroy(kit_coreFstr_t** buffer_p);
- /**
- * Create a new kit_coreFstr_t
- * \param[in] buffer_size the size of the string's buffer, in bytes
- * \return A pointer to a newly-created Fstr_t struct, or NULL on error (call SDL_GetError() for more info)
- *
- * \sa kit_coreFstrDestroy
- */
- extern kit_coreFstr_t* kit_coreFstrCreate(Uint32 buffer_size);
- /* -------------- */
- /* -kit_coreFstr- */
- /* -------------- */
- #ifdef __cplusplus
- }
- #endif
- #endif /* _KIT_SDL2_CORE_H */
- #endif /* _KIT_CORE_H */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement