Kitomas

work for 2024-11-21 (7/10)

Nov 22nd, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 43.00 KB | None | 0 0
  1. /******************************************************************************/
  2. /******************************************************************************/
  3. //"2024-11-21\src\kit_sdl2\_kit_private.cpp":
  4. #include "_kit_common.hpp"
  5.  
  6.  
  7. namespace kit {
  8.  
  9.  
  10.  
  11.  
  12.  
  13. #define ERRORS_INVALID (_gl.errors == nullptr  ||  _gl.errors_len == 0)
  14. #define LAST_INDEX ((_gl.errors_len>0) ? _gl.errors_len-1 : 0)
  15.  
  16. //returns newest last index on success, or < 0 on failure of some kind
  17. static s64 _changeErrorsSize(s32 change){ //(change is in elements, not bytes)
  18.   if(ERRORS_INVALID) return -1;
  19.   if(change == 0   ) return LAST_INDEX;
  20.  
  21.   if(((s64)_gl.errors_len)+change <= 0) return -1;
  22.  
  23.   LOCK_GLOBALS();
  24.  
  25.  
  26.   s64 last_index = -1;
  27.  
  28.   u32    _errors_len  = _gl.errors_len + change;
  29.   size_t _errors_size = sizeof(Error) * _errors_len;
  30.  
  31.  
  32.   if(memory::realloc(&_gl.errors, _errors_size))
  33.   {
  34.     //if errors array grew, zero out any new memory
  35.      //(this loop won't even start if _errors_len <= _gl.errors_len)
  36.     for(u32 i = _gl.errors_len; i<_errors_len; ++i)
  37.       _gl.errors[i]._0 = 0,  _gl.errors[i]._1 = 0;
  38.  
  39.     _gl.errors_len = _errors_len;
  40.  
  41.     last_index = LAST_INDEX;
  42.  
  43.   }
  44.  
  45.  
  46.   UNLOCK_GLOBALS();
  47.  
  48.  
  49.   return last_index;
  50.  
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57. static char _errortext_default[] = "(FAILED TO ALLOCATE MEMORY FOR ERROR TEXT)";
  58.  
  59. //push heap error
  60.  //(this assumes only one error will exist per thread at any given time!)
  61. const char* _pushError(const char* errortext){
  62.   if(ERRORS_INVALID      ) return "(THREAD ERROR ARRAY IS NULLPTR)";
  63.   if(errortext == nullptr) return "_pushError(): errortext = nullptr";
  64.  
  65.   LOCK_GLOBALS();
  66.  
  67.  
  68.   //attempt to find an empty space inside already existing errors array
  69.   u32 i = 0;
  70.   for(; i<_gl.errors_len; ++i){
  71.     if(_gl.errors[i]._txt == nullptr) break;
  72.   }
  73.  
  74.  
  75.   //if no empty space for an error was found,
  76.    //attempt to grow errors by 1 element
  77.   if(i == _gl.errors_len){
  78.     s64 last_index = _changeErrorsSize(1);
  79.  
  80.     //if errors failed to change size, and/or last index is not empty
  81.     if(last_index < 0  ||  _gl.errors[last_index]._txt != nullptr)
  82.       i = KIT_U32_MAX; //indicates failure to find any empty space
  83.  
  84.   }
  85.  
  86.  
  87.   char* _errortext = _errortext_default;
  88.  
  89.   //if valid index was found, copy error text to new string inside errors array
  90.   if(i < KIT_U32_MAX){
  91.     size_t errortext_len = strnLen(errortext);
  92.  
  93.     char* _errortext_tmp = (char*)memory::alloc(errortext_len+1);
  94.     //(memory::set is unnecessary here, since all bytes are overwritten anyway)
  95.  
  96.     if(_errortext_tmp != nullptr){
  97.       _errortext = _errortext_tmp;
  98.  
  99.       //strcpy basically
  100.       for(size_t c=0; c<errortext_len; ++c)
  101.         _errortext[c] = errortext[c];
  102.  
  103.       _errortext[errortext_len] = 0; //manually add null terminator
  104.  
  105.       //set members of new error accordingly
  106.       _gl.errors[i]._txt       = _errortext;
  107.       _gl.errors[i]._thread_id = SDL_GetThreadID(nullptr); //error belongs to calling thread
  108.       _gl.errors[i]._heap      = true;
  109.  
  110.     }
  111.  
  112.   }
  113.  
  114.  
  115.   UNLOCK_GLOBALS();
  116.  
  117.   return (const char*)_errortext;
  118.  
  119. }
  120.  
  121.  
  122.  
  123.  
  124. //returns true if it found an error to free
  125. bool _freeError(u32 thread_id){
  126.   if(ERRORS_INVALID) return false;
  127.  
  128.   if(!thread_id) thread_id = SDL_GetThreadID(nullptr); //id of calling thread
  129.  
  130.   LOCK_GLOBALS();
  131.  
  132.  
  133.   bool error_found = false;
  134.  
  135.   //try to find error based on its thread id
  136.   u32 i = 0;
  137.   for(; i<_gl.errors_len; ++i){
  138.     if(_gl.errors[i]._thread_id == thread_id){
  139.  
  140.       if(_gl.errors[i]._txt != nullptr  &&  _gl.errors[i]._heap)
  141.         memory::free(&_gl.errors[i]._txt);
  142.  
  143.       _gl.errors[i]._txt       = nullptr;
  144.       _gl.errors[i]._thread_id = 0;
  145.       _gl.errors[i]._heap      = false;
  146.  
  147.       error_found = true;
  148.       break; //thread error found; break loop
  149.  
  150.     }
  151.   }
  152.  
  153.  
  154.   //shrink errors if the last error is now empty
  155.   if(_gl.errors[_gl.errors_len-1]._txt == nullptr)
  156.     _changeErrorsSize(-1);
  157.  
  158.  
  159.   UNLOCK_GLOBALS();
  160.  
  161.   return error_found;
  162.  
  163. }
  164.  
  165.  
  166.  
  167.  
  168. void _freeErrors(){
  169.   //(ERRORS_INVALID is not used here)
  170.   if(_gl.errors == nullptr) return;
  171.  
  172.   LOCK_GLOBALS();
  173.  
  174.  
  175.   for(u32 i=0; i<_gl.errors_len; ++i){
  176.     if(_gl.errors[i]._txt != nullptr  &&  _gl.errors[i]._heap)
  177.       memory::free(&_gl.errors[i]._txt); //automatically sets _txt to nullptr
  178.  
  179.   }
  180.  
  181.  
  182.   memory::free(&_gl.errors);
  183.  
  184.   _gl.errors_len = 0;
  185.  
  186.  
  187.   UNLOCK_GLOBALS();
  188.  
  189. }
  190.  
  191.  
  192.  
  193.  
  194.  
  195. }; /* namespace kit */
  196. /******************************************************************************/
  197. /******************************************************************************/
  198. //"2024-11-21\src\kit_sdl2\kit_load_wav.h":
  199. /*
  200.  * Copyright (c) 2024, Conlan Walker
  201.  * SPDX-License-Identifier: Zlib
  202.  *
  203.  * File: kit_load_wav.h
  204.  *
  205.  * Purpose: A parser for most common types of Microsoft Waveform Audio Files
  206.  *
  207.  * Version: r1.4
  208.  */
  209.  
  210. #ifndef KIT_LOAD_WAV_H
  211. #define KIT_LOAD_WAV_H
  212.  
  213. #ifdef __cplusplus
  214. extern "C" {
  215. #endif /* __cplusplus */
  216.  
  217. #include <stdint.h>
  218.  
  219.  
  220.  
  221.  
  222.  
  223. /* Relevant with respect to .format and .sub.format */
  224.  
  225. typedef enum {
  226.   WAVEDATA_FORMAT_PCM   = 1,
  227.   WAVEDATA_FORMAT_FLOAT = 3,
  228.   WAVEDATA_FORMAT_ALAW  = 6, /* assumes A = 87.6 (I think) */
  229.   WAVEDATA_FORMAT_ULAW  = 7, /* assumes u = 255  (I think) */
  230.   /* If ".format" equals this, the actual format tag (as in
  231.      the above four format tags) will be in ".sub.format" */
  232.   WAVEDATA_FORMAT_EXTENSIBLE = 0xFFFE,
  233. } kit_WavData_formats;
  234.  
  235.  
  236.  
  237. /* Combines info from: .format/.sub.format, .bitsPerSample, and .validBits */
  238.  
  239. #define DEF_WAVEDATA_TYPE(_sgn, _fmt, _vbt, _tbt) \
  240.   ( (_sgn)<<15 | (_fmt)<<12 | (_vbt)<<6 | (_tbt) )
  241.  
  242. typedef enum {
  243.   WAVEDATA_TYPE_UNKNOWN = 0,
  244.   WAVEDATA_TYPE_U8   = DEF_WAVEDATA_TYPE(0, WAVEDATA_FORMAT_PCM  ,  7,  7),
  245.   WAVEDATA_TYPE_S16  = DEF_WAVEDATA_TYPE(1, WAVEDATA_FORMAT_PCM  , 15, 15),
  246.   WAVEDATA_TYPE_S24  = DEF_WAVEDATA_TYPE(1, WAVEDATA_FORMAT_PCM  , 23, 23),
  247.   WAVEDATA_TYPE_S32  = DEF_WAVEDATA_TYPE(1, WAVEDATA_FORMAT_PCM  , 31, 31),
  248.   WAVEDATA_TYPE_F32  = DEF_WAVEDATA_TYPE(1, WAVEDATA_FORMAT_FLOAT, 31, 31),
  249.   WAVEDATA_TYPE_F64  = DEF_WAVEDATA_TYPE(1, WAVEDATA_FORMAT_FLOAT, 63, 63),
  250.   WAVEDATA_TYPE_ALAW = DEF_WAVEDATA_TYPE(0, WAVEDATA_FORMAT_ALAW ,  7,  7),
  251.   WAVEDATA_TYPE_ULAW = DEF_WAVEDATA_TYPE(0, WAVEDATA_FORMAT_ULAW ,  7,  7),
  252. } kit_WavData_types;
  253.  
  254. #define WAVEDATA_TYPE_ISSIGNED(_value)  ( ((_value)>>15) &  1 )
  255. #define WAVEDATA_TYPE_FORMAT(_value)    ( ((_value)>>12) &  7 )
  256. #define WAVEDATA_TYPE_VALIDBITS(_value) ( ((_value)>> 6) & 63 )
  257. #define WAVEDATA_TYPE_TOTALBITS(_value) (  (_value)      & 63 )
  258.  
  259. typedef union {
  260.   uint16_t       value;
  261.   struct {
  262.     uint16_t totalBits : 6;
  263.     uint16_t validBits : 6; /* 0 for 1-bit, 63 for 64-bits, etc. */
  264.     uint16_t    format : 3; /* one of kit_WavData_formats (minus EXTENSIBLE) */
  265.     uint16_t  isSigned : 1;
  266.   };
  267.  
  268. } kit_WavData_type;
  269.  
  270.  
  271.  
  272. typedef struct kit_WavData { /* 72B (excluding actual sample data, of course) */
  273.   /********************** PRESENT IN ALL "fmt" SUBCHUNKS **********************/
  274.   uint16_t        format; /*  one of kit_WaveData_formats                     */
  275.   uint16_t      channels; /*  # of interleaved channels; L&R for stereo (2)   */
  276.   uint32_t    sampleRate; /*  Sample frames per second                        */
  277.   uint32_t      byteRate; /*  = blockAlign*sampleRate                         */
  278.   uint16_t    blockAlign; /*  Size of smp frame = (channels*bitsPerSample)/8  */
  279.   uint16_t bitsPerSample; /*  Assumed to be unsigned if 8 and .format is PCM  */
  280.  
  281.   /********************* WAVEDATA_FORMAT_EXTENSIBLE ONLY **********************/
  282.   uint16_t       extSize; /*  Size of the extension (should = 0 OR 22)        */
  283.   uint16_t     validBits; /*  # of bits used (must bs <= bitsPerSample)       */
  284.   uint32_t   channelMask; /*  Speaker bitmask                                 */
  285.   union {                 /*                                                  */
  286.     uint32_t      format; /*  Subformat; same as .format (minus EXTENSIBLE)   */
  287.     char    GUID_str[16]; /*                                                  */
  288.     uint64_t GUID_num[2]; /*                                                  */
  289.   } /************/ sub;   /*  GUID, (+ data format tag; 1st 4 bytes of GUID)  */
  290.  
  291.   /***************************** kit_WavData ONLY *****************************/
  292.   void*         userdata; /*  = NULL when returned by kit_<load/parse>_wav()  */
  293.   uint32_t   _reserved_0; /*                                                  */
  294.   uint16_t   _reserved_1; /*                                                  */
  295.                           /*                                                  */
  296.   uint16_t  samples_type; /*  May be set to 0 if calculation attempt failed   */
  297.   uint32_t  samples_size; /*  In bytes                                        */
  298.   uint32_t   samples_len; /*  In sample frames                                */
  299.   void*          samples; /*  = &this_struct.samples + sizeof(void*)          */
  300.  
  301. } kit_WavData;
  302.  
  303.  
  304.  
  305.  
  306.  
  307. typedef enum {
  308.   WAVEERROR_SUCCESS    =  0,
  309.   WAVEERROR_NULLPTR    =  1, /* Non-optional argument pointer was NULL        */
  310.   WAVEERROR_FILE_READ  =  2, /* Failed to read file                           */
  311.   WAVEERROR_FILE_SIZE  =  3, /* FileSize was < 44 (minimum needed for header) */
  312.   WAVEERROR_NOT_RIFF   =  4, /* First 4 bytes of file != "RIFF"               */
  313.   WAVEERROR_MALFORMED  =  5, /* Inconsistent subchunk sizes, bad ids, etc.    */
  314.   WAVEERROR_NOT_WAVE   =  6, /* 4 bytes from offset 8 != "WAVE"               */
  315.   WAVEERROR_MALLOC     =  7, /* Failed to allocate memory                     */
  316.   WAVEERROR_NO_FMT     =  8, /* Wav doesn't contain a f[or]m[a]t subchunk     */
  317.   WAVEERROR_NO_DATA    =  9, /* Wav doesn't contain a data subchunk           */
  318.  
  319.   WAVEERROR_FORMAT     = 10, /* Wav uses an unsupported format tag            */
  320.   WAVEERROR_CHANNELS   = 11, /* .channels = 0                                 */
  321.   WAVEERROR_SAMPLERATE = 12, /* .sampleRate = 0                               */
  322.   WAVEERROR_BYTERATE   = 13, /* .byteRate !=  .blockAlign*.sampleRate         */
  323.   WAVEERROR_BLOCKALIGN = 14, /* .blockAlign != (.channels*.bitsPerSample)/8   */
  324.   WAVEERROR_BITSPERSMP = 15, /* .bitsPerSample%8 != 0  OR  = 0  OR  > 64      */
  325.   WAVEERROR_EXTSIZE    = 16, /* .extSize < 22 (if format is EXTENSIBLE)       */
  326.   WAVEERROR_VALIDBITS  = 17, /* .validBits > .bitsPerSample  OR  = 0          */
  327.  
  328.   WAVEERROR_UNKNOWN    = -1,
  329. } kit_load_wav_errors;
  330.  
  331.  
  332.  
  333.  
  334.  
  335. /* Read and parse a .wav file (Calls kit_read_wav and kit_parse_wav internally!)
  336.  *
  337.  * Returns a pointer to a kit_WavData struct that contains
  338.  * the .wav's header and sample data, or NULL on failure.
  339.  *
  340.  * if error_out != NULL, the int it points to will be
  341.  * filled with the resulting kit_load_wav_errors code.
  342.  *
  343.  * The returned pointer should be freed after use (if successful).
  344.  *
  345.  * Only PCM, float, A-law, and u-law samples are supported.
  346.  *
  347.  * (.samples is contiguous with the returned struct's memory block,
  348.  *  so when the struct is freed, the samples are too!)
  349.  */
  350.  
  351. kit_WavData* kit_load_wav(const char* filePath,
  352.                           int32_t* error_out);
  353.  
  354.  
  355.  
  356.  
  357.  
  358. /* Allocate memory for and read a .wav file.
  359.  *
  360.  * if fileSize_out != NULL, the int it points to will be
  361.  * filled with the size of the read file in bytes.
  362.  *
  363.  * Returns a pointer to the raw file data, or NULL on failure.
  364.  *
  365.  * The returned pointer should be freed after use (if successful).
  366.  *
  367.  * (#define KIT_LOAD_WAV_CUSTOM_READ to make
  368.  *  kit_load_wav() use your own implementation!)
  369.  * (Files > 2^31-1 bytes will fail to read!)
  370.  */
  371.  
  372. void* kit_read_wav(const char* filePath, int32_t* fileSize_out);
  373.  
  374.  
  375.  
  376. /* Parse the file data of a .wav in memory.
  377.  *
  378.  * Returns a pointer to a kit_WavData struct that contains
  379.  * the .wav's header and sample data, or NULL on failure.
  380.  *
  381.  * if error_out != NULL, the int it points to will be
  382.  * filled with the resulting kit_load_wav_errors code.
  383.  *
  384.  * The returned pointer should be freed after use.
  385.  *
  386.  * Only PCM, float, A-law, and u-law samples are supported.
  387.  *
  388.  * (.samples is contiguous with the struct's memory block,
  389.  *  so when the struct is freed, the samples are too!)
  390.  * (Sample data is copied from fileData, not referenced!)
  391.  */
  392.  
  393. kit_WavData* kit_parse_wav(const void* fileData, int32_t fileSize,
  394.                            int32_t* error_out);
  395.  
  396.  
  397.  
  398.  
  399.  
  400. #ifdef __cplusplus
  401. }
  402. #endif /* __cplusplus */
  403.  
  404. #endif /* KIT_LOAD_WAV_H */
  405.  
  406. /******************************************************************************/
  407.  
  408. #ifdef KIT_LOAD_WAV_IMPLEMENTATION
  409.  
  410.  
  411.  
  412.  
  413.  
  414. #ifndef KIT_LOAD_WAV_MALLOC
  415. #define KIT_LOAD_WAV_MALLOC(_ptr) malloc(_ptr)
  416. #include <stdlib.h>
  417. #endif /* KIT_LOAD_WAV_MALLOC */
  418.  
  419. #ifndef KIT_LOAD_WAV_FREE
  420. #define KIT_LOAD_WAV_FREE(_ptr) free(_ptr)
  421. #include <stdlib.h>
  422. #endif /* KIT_LOAD_WAV_FREE */
  423.  
  424. #ifndef KIT_LOAD_WAV_MEMSET
  425. #define KIT_LOAD_WAV_MEMSET(_ptr_or_arr, _value, _size) \
  426.   memset((_ptr_or_arr), (_value), (_size))
  427. #include <string.h>
  428. #endif /* KIT_LOAD_WAV_MEMSET */
  429.  
  430. #ifndef KIT_LOAD_WAV_MEMCPY
  431. #define KIT_LOAD_WAV_MEMCPY(_dst_ptr, _src_ptr, _size) \
  432.   memcpy((_dst_ptr), (_src_ptr), (_size))
  433. #include <string.h>
  434. #endif /* KIT_LOAD_WAV_MEMCPY */
  435.  
  436. #ifndef KIT_LOAD_WAV_CHUNKSIZE
  437. #define KIT_LOAD_WAV_CHUNKSIZE 4096 /* How many bytes to read at a time */
  438. #endif /* KIT_LOAD_WAV_CHUNKSIZE */
  439.  
  440.  
  441.  
  442.  
  443.  
  444. #define LDWAV_ASSERT(_success, _errcode) \
  445.   if(!(_success)){ error = (_errcode); goto _return_wavData; }
  446.  
  447.  
  448.  
  449.  
  450.  
  451. kit_WavData* kit_load_wav(const char* filePath,
  452.                           int32_t* error_out)
  453. {
  454.   int32_t        error = WAVEERROR_SUCCESS;
  455.   kit_WavData* wavData = NULL;
  456.  
  457.   if(0){
  458.     _return_wavData:
  459.     if(error_out != NULL) *error_out = error;
  460.     return (error == WAVEERROR_SUCCESS) ? wavData : NULL;
  461.  
  462.   }
  463.  
  464.  
  465.   LDWAV_ASSERT(filePath, WAVEERROR_NULLPTR);
  466.  
  467.   int32_t fileSize = -1;
  468.   void* fileData = kit_read_wav(filePath, &fileSize);
  469.   LDWAV_ASSERT(fileData, WAVEERROR_FILE_READ);
  470.  
  471.  
  472.   wavData = kit_parse_wav(fileData, fileSize, &error);
  473.   KIT_LOAD_WAV_FREE(fileData);
  474.  
  475.   goto _return_wavData;
  476.  
  477. }
  478.  
  479.  
  480.  
  481.  
  482.  
  483. #ifndef KIT_LOAD_WAV_CUSTOM_READ
  484. #include <stdio.h>
  485.  
  486. void* kit_read_wav(const char* filePath, int32_t* fileSize_out){
  487.   if(filePath == NULL) return NULL;
  488.  
  489.   FILE* file = fopen(filePath, "rb");
  490.   if(file == NULL) return NULL;
  491.  
  492.  
  493.  
  494.   /* get size of file */
  495.  
  496.   if(fseek(file, 0L, SEEK_END)){
  497.     _fclose_err:
  498.     fclose(file);
  499.     return NULL;
  500.   }
  501.  
  502.   int32_t fileSize = ftell(file);
  503.   if(fileSize == -1L) goto _fclose_err;
  504.  
  505.   if(fseek(file, 0L, SEEK_SET) != 0) goto _fclose_err;
  506.  
  507.  
  508.  
  509.   /* allocate memory for file data */
  510.  
  511.   void* fileData = (void*)KIT_LOAD_WAV_MALLOC(fileSize);
  512.   if(fileData == NULL) goto _fclose_err;
  513.  
  514.  
  515.  
  516.   /* read data from file */
  517.  
  518.   size_t chunkSize  = fileSize%KIT_LOAD_WAV_CHUNKSIZE;
  519.   size_t chunkCount = fileSize/KIT_LOAD_WAV_CHUNKSIZE;
  520.  
  521.   if(!chunkSize) chunkSize = KIT_LOAD_WAV_CHUNKSIZE;
  522.   else           ++chunkCount;
  523.  
  524.   uint8_t* fileDataPtr = (uint8_t*)fileData;
  525.  
  526.   for(size_t i=0; i<chunkCount; ++i){
  527.     if(fread(fileDataPtr, 1, chunkSize, file) != chunkSize)
  528.       goto _free_fclose_err;
  529.  
  530.     fileDataPtr += chunkSize;
  531.  
  532.     chunkSize = KIT_LOAD_WAV_CHUNKSIZE;
  533.  
  534.   }
  535.  
  536.  
  537.  
  538.   /* tidy up before returning file data */
  539.  
  540.   if(fclose(file) != 0){
  541.     _free_fclose_err:
  542.     KIT_LOAD_WAV_FREE(fileData);
  543.     goto _fclose_err;
  544.  
  545.   }
  546.  
  547.   if(fileSize_out != NULL) *fileSize_out = fileSize;
  548.  
  549.   return fileData;
  550.  
  551. }
  552.  
  553. #endif /* KIT_LOAD_WAV_CUSTOM_READ */
  554.  
  555.  
  556.  
  557.  
  558.  
  559. #include <stdbool.h>
  560.  
  561.  
  562.  
  563.  
  564.  
  565. typedef enum {
  566.   wid_RIFF = 0x46464952, /*  = "RIFF"  */
  567.   wid_WAVE = 0x45564157, /*  = "WAVE"  */
  568.   wid_fmt_ = 0x20746D66, /*  = "fmt "  */
  569.   wid_data = 0x61746164, /*  = "data"  */
  570.   wid_fact = 0x74636166, /*  = "fact"  */
  571.   wid_PEAK = 0x4B414550, /*  = "PEAK"  */
  572. } _wav_ids;
  573.  
  574.  
  575.  
  576.  
  577.  
  578. typedef struct _wav_PEAK_pos {
  579.   float       value; /* Value of peak (-1.0f -> 1.0f) */
  580.   uint32_t position; /* Sample frame of peak          */
  581. } _wav_PEAK_pos;
  582.  
  583. typedef struct _wav_PEAK {
  584.   uint32_t       version; /* Version of the PEAK subchunk */
  585.   uint32_t     timeStamp; /* Seconds since 1970-1-1       */
  586.   _wav_PEAK_pos peaks[1]; /* Peak info array              */
  587. } _wav_PEAK;
  588.  
  589.  
  590.  
  591. typedef struct _wav_fmtEx {
  592.   struct {
  593.     /********************** PRESENT IN ALL "fmt" SUBCHUNKS **********************/
  594.     uint16_t        format; /*  one of kit_WaveData_formats                     */
  595.     uint16_t      channels; /*  # of interleaved channels; L&R for stereo (2)   */
  596.     uint32_t    sampleRate; /*  Sample frames per second                        */
  597.     uint32_t      byteRate; /*  = blockAlign*sampleRate                         */
  598.     uint16_t    blockAlign; /*  Size of smp frame = (channels*bitsPerSample)/8  */
  599.     uint16_t bitsPerSample; /*  Assumed to be unsigned if 8 and .format is PCM  */
  600.   } a;
  601.  
  602.   struct {
  603.     /********************* WAVEDATA_FORMAT_EXTENSIBLE ONLY **********************/
  604.     uint16_t       extSize; /*  Size of the extension (should = 0 OR 22)        */
  605.     uint16_t     validBits; /*  # of bits used (must bs <= bitsPerSample)       */
  606.     uint32_t   channelMask; /*  Speaker bitmask                                 */
  607.     union {                 /*                                                  */
  608.       uint32_t      format; /*  Subformat; same as .format (minus EXTENSIBLE)   */
  609.       char    GUID_str[16]; /*                                                  */
  610.       uint64_t GUID_num[2]; /*                                                  */
  611.     } /************/ sub;   /*  GUID, (+ data format tag; 1st 4 bytes of GUID)  */
  612.   } x;
  613.  
  614. } _wav_fmtEx;
  615.  
  616.  
  617.  
  618. typedef struct _wav_chunk {
  619.   uint32_t       id;
  620.   uint32_t     size;
  621.   /* fmt and data are the only subchunks that kit_parse_wav
  622.      recognizes, so no "fact" or "PEAK" subchunks! */
  623.   union {
  624.     _wav_PEAK     PEAK; /* Currently unused */
  625.     _wav_fmtEx     fmt;
  626.     uint8_t    data[1];
  627.     uint32_t value_u32;
  628.   };
  629.  
  630. } _wav_chunk;
  631.  
  632.  
  633.  
  634.  
  635.  
  636. static inline uint32_t _check_format(kit_WavData headerInfo){
  637.   uint32_t formatTag = headerInfo.format;
  638.   if(formatTag == WAVEDATA_FORMAT_EXTENSIBLE)
  639.     formatTag = headerInfo.sub.format;
  640.  
  641.   switch(formatTag){
  642.     case WAVEDATA_FORMAT_PCM  :
  643.     case WAVEDATA_FORMAT_FLOAT:
  644.     case WAVEDATA_FORMAT_ALAW :
  645.     case WAVEDATA_FORMAT_ULAW :
  646.     break;
  647.     default: return 0; /* Invalid format */
  648.   }
  649.  
  650.   return formatTag;
  651.  
  652. }
  653.  
  654.  
  655.  
  656.  
  657.  
  658. #define CHUNK_ADVANCE(_bytes) \
  659.   ( chunk = (const _wav_chunk*)(data+=(_bytes)) )
  660.  
  661. kit_WavData* kit_parse_wav(const void* fileData, int32_t fileSize,
  662.                            int32_t* error_out)
  663. {
  664.   int32_t        error = WAVEERROR_SUCCESS;
  665.   kit_WavData* wavData = NULL;
  666.  
  667.   if(0){
  668.     _return_wavData:
  669.     if(error_out != NULL) *error_out = error;
  670.     if(error == WAVEERROR_SUCCESS){
  671.       return wavData;
  672.  
  673.     } else {
  674.       if(wavData != NULL) KIT_LOAD_WAV_FREE(wavData);
  675.       return NULL;
  676.  
  677.     }
  678.  
  679.   }
  680.  
  681.   LDWAV_ASSERT(fileData, WAVEERROR_NULLPTR);
  682.   LDWAV_ASSERT(fileSize >= 44, WAVEERROR_FILE_SIZE);
  683.  
  684.  
  685.  
  686.   bool has_fmt_ = false;
  687.   bool has_data = false;
  688.  
  689.   kit_WavData hdr; /* AKA h[ea]d[e]r [info] */
  690.   KIT_LOAD_WAV_MEMSET(&hdr, 0, sizeof(kit_WavData));
  691.  
  692.   const uint8_t*     data = (const uint8_t*)fileData;
  693.   const uint8_t*  dataEnd = (const uint8_t*)fileData + fileSize;
  694.   const _wav_chunk* chunk = (const _wav_chunk*)data;
  695.  
  696.   LDWAV_ASSERT(chunk->id == wid_RIFF, WAVEERROR_NOT_RIFF);
  697.   LDWAV_ASSERT(chunk->size == (uint32_t)(fileSize-8), WAVEERROR_MALFORMED);
  698.   LDWAV_ASSERT(chunk->value_u32 == wid_WAVE, WAVEERROR_NOT_WAVE);
  699.  
  700.   CHUNK_ADVANCE(sizeof(uint32_t)*3);
  701.  
  702.  
  703.  
  704.   while(data < dataEnd){
  705.     /* Safeguard to make sure heap isn't overshot (hopefully) */
  706.     LDWAV_ASSERT((data + 8+chunk->size) <= dataEnd, WAVEERROR_MALFORMED);
  707.  
  708.  
  709.     switch(chunk->id){
  710.       case wid_fmt_: {
  711.         LDWAV_ASSERT(!has_fmt_, WAVEERROR_MALFORMED);
  712.  
  713.         ((_wav_fmtEx*)&hdr)->a = chunk->fmt.a;
  714.  
  715.         if(hdr.format == WAVEDATA_FORMAT_EXTENSIBLE)
  716.           ((_wav_fmtEx*)&hdr)->x = chunk->fmt.x;
  717.  
  718.         has_fmt_ = true;
  719.       } break;
  720.  
  721.  
  722.       case wid_data: {
  723.         LDWAV_ASSERT(!has_data, WAVEERROR_MALFORMED);
  724.  
  725.         size_t wavDataSize = sizeof(kit_WavData)+chunk->size;
  726.         LDWAV_ASSERT(wavDataSize > sizeof(kit_WavData), WAVEERROR_MALFORMED);
  727.  
  728.         wavData = (kit_WavData*)KIT_LOAD_WAV_MALLOC(wavDataSize);
  729.         LDWAV_ASSERT(wavData, WAVEERROR_MALLOC);
  730.  
  731.         hdr.samples_size = chunk->size;
  732.         hdr.samples      = (uint8_t*)(wavData)+sizeof(kit_WavData);
  733.         KIT_LOAD_WAV_MEMCPY(hdr.samples, chunk->data, chunk->size);
  734.  
  735.         has_data = true;
  736.       } break;
  737.  
  738.  
  739.       default:; /* Other subchunks are ignored */
  740.  
  741.     }
  742.  
  743.  
  744.     CHUNK_ADVANCE(8+chunk->size);
  745.  
  746.   }
  747.  
  748.  
  749.  
  750.   LDWAV_ASSERT(data == dataEnd, WAVEERROR_MALFORMED);
  751.   LDWAV_ASSERT(has_fmt_, WAVEERROR_NO_FMT);
  752.   LDWAV_ASSERT(has_data, WAVEERROR_NO_DATA);
  753.   LDWAV_ASSERT(wavData, WAVEERROR_MALLOC); /* Just in case */
  754.  
  755.   uint32_t formatTag = _check_format(hdr);
  756.   LDWAV_ASSERT(formatTag != 0, WAVEERROR_FORMAT);
  757.  
  758.   LDWAV_ASSERT(hdr.channels != 0, WAVEERROR_CHANNELS);
  759.  
  760.   LDWAV_ASSERT(hdr.sampleRate != 0, WAVEERROR_SAMPLERATE);
  761.  
  762.   LDWAV_ASSERT((hdr.bitsPerSample&7) == 0  &&  hdr.bitsPerSample != 0  &&
  763.                hdr.bitsPerSample <= 64, WAVEERROR_BITSPERSMP);
  764.  
  765.   LDWAV_ASSERT(hdr.blockAlign == (hdr.channels*hdr.bitsPerSample)/8,
  766.                WAVEERROR_BLOCKALIGN);
  767.  
  768.   LDWAV_ASSERT(hdr.byteRate == hdr.blockAlign*hdr.sampleRate,
  769.                WAVEERROR_BYTERATE);
  770.  
  771.   if(hdr.format == WAVEDATA_FORMAT_EXTENSIBLE){
  772.     LDWAV_ASSERT(hdr.extSize >= 22,
  773.                  WAVEERROR_EXTSIZE);
  774.  
  775.     LDWAV_ASSERT(hdr.validBits <= hdr.bitsPerSample  &&  hdr.validBits != 0  &&
  776.                  hdr.validBits <= 64, WAVEERROR_VALIDBITS);
  777.  
  778.   }
  779.  
  780.  
  781.  
  782.   kit_WavData_type smps_type;
  783.  
  784.   smps_type.isSigned  =  formatTag == WAVEDATA_FORMAT_FLOAT;
  785.  
  786.   if(formatTag == WAVEDATA_FORMAT_PCM)
  787.     smps_type.isSigned  =  hdr.bitsPerSample > 8;
  788.  
  789.   smps_type.format    = formatTag;
  790.   smps_type.validBits = hdr.bitsPerSample-1;
  791.   smps_type.totalBits = hdr.bitsPerSample-1;
  792.  
  793.   if(hdr.format == WAVEDATA_FORMAT_EXTENSIBLE)
  794.     smps_type.validBits = hdr.validBits-1;
  795.  
  796.   hdr.samples_type = smps_type.value;
  797.   /* (.samples_size should already be set) */
  798.   hdr.samples_len = hdr.samples_size/hdr.blockAlign;
  799.  
  800.  
  801.  
  802.   *((_wav_fmtEx*)wavData) = *((_wav_fmtEx*)&hdr);
  803.  
  804.   wavData->userdata     = hdr.userdata;
  805.   wavData->_reserved_0  = hdr._reserved_0;
  806.   wavData->_reserved_1  = hdr._reserved_1;
  807.  
  808.   wavData->samples_type = hdr.samples_type;
  809.   wavData->samples_size = hdr.samples_size;
  810.   wavData->samples_len  = hdr.samples_len;
  811.   wavData->samples      = hdr.samples;
  812.  
  813.   goto _return_wavData;
  814.  
  815. }
  816.  
  817.  
  818.  
  819.  
  820.  
  821. #endif /* KIT_LOAD_WAV_IMPLEMENTATION */
  822.  
  823. /* Revision log:
  824.  *
  825.  * r1.4  2024-11-06 @ 15:56, Conlan
  826.  * Fixed memory leak in kit_load_wav (whoops!)
  827.  *
  828.  * r1.3  2024-11-05 @ 21:04, Conlan
  829.  * Fixed description of WAVEERROR_BITSPERSMP in kit_load_wav_errors
  830.  *
  831.  * r1.2  2024-11-05 @ 13:44, Conlan
  832.  * Fixed stuff related to 'uninitialized member'
  833.  * and 'signedness comparison' warnings
  834.  *
  835.  * r1.1  2024-10-28 @ 21:14, Conlan
  836.  * Removed const keyword from kit_WavData.samples,
  837.  * and every punning hack that was the direct result of that keyword
  838.  *
  839.  * r1.0  2024-10-28 @ 18:40, Conlan
  840.  * Finished kit_load_wav(), kit_read_wav(), and kit_parse_wav()
  841.  */
  842. /******************************************************************************/
  843. /******************************************************************************/
  844. //"2024-11-21\include\demo\include_all.hpp":
  845. #ifndef _INCLUDE_ALL_HPP
  846. #define _INCLUDE_ALL_HPP
  847.  
  848.  
  849. #pragma GCC diagnostic ignored "-Wreorder"
  850. #include <box2d-lite/World.h>
  851. #include <box2d-lite/Body.h>
  852. #include <box2d-lite/Joint.h>
  853. #pragma GCC diagnostic pop
  854.  
  855. #include <kit/all.hpp>
  856.  
  857.  
  858.  
  859.  
  860.  
  861. #define WIN_X 1280 //640
  862. #define WIN_Y 720 //480
  863. #define CNV_DIV 4 //1, 2, 4, 5, 8, 10, 16, 20, 40, 80
  864.  
  865. #define CNV_X (WIN_X/CNV_DIV)
  866. #define CNV_Y (WIN_Y/CNV_DIV)
  867.  
  868. #define BLACK 0xFF000000
  869.  
  870. #define FALLTHROUGH __attribute__((fallthrough))
  871.  
  872.  
  873.  
  874.  
  875.  
  876. #define DEF_KIT(_thing, _equal_to) extern kit::_thing;
  877.  
  878. #define DEF_GLOBAL_PTRS /* listed by destruction order */ \
  879. \
  880. DEF_KIT(Window*        win        , nullptr)\
  881. DEF_KIT(Surface*       canvas     , nullptr)\
  882. DEF_KIT(Mutex*         canvas_lock, nullptr)\
  883. DEF_KIT(BFont_Surface* font0      , nullptr)\
  884. DEF_KIT(BFont_Surface* font1      , nullptr)\
  885. \
  886. DEF_KIT(AudioData*   sfx_tick , nullptr)\
  887. DEF_KIT(AudioData*   sfx_alert, nullptr)\
  888. DEF_KIT(SoundEngine* sfx      , nullptr)\
  889. DEF_KIT(AudioDevice* audio    , nullptr)\
  890. DEF_KIT(SoundEngine* music    , nullptr)\
  891.  
  892. DEF_GLOBAL_PTRS
  893.  
  894.  
  895.  
  896.  
  897.  
  898. extern kit::f64 simSpeed;
  899.  
  900. extern kit::f32 timeStep;
  901.  
  902. extern World world;
  903.  
  904.  
  905.  
  906.  
  907.  
  908. kit::f64 frand();  // 0.0f -> 1.0f
  909. kit::f64 frand2(); //-1.0f -> 1.0f
  910.  
  911. kit::f32 frandf();  // 0.0f -> 1.0f
  912. kit::f32 frandf2(); //-1.0f -> 1.0f
  913.  
  914. static inline void clearCanvas(){ canvas->fillRects(BLACK); }
  915. void presentCanvas();
  916.  
  917. void drawLine(kit::s32 x_0, kit::s32 y_0, kit::s32 x_1, kit::s32 y_1,
  918.               kit::colors::ABGR lineColor);
  919.  
  920. void drawBody(Body& body);
  921.  
  922. //draws whatever is currently in font's format buffer
  923. void drawCursorText(kit::s32 x, kit::s32 y, const char* fmt, ...);
  924.  
  925. void drawSpeedBar(kit::f32 speed = simSpeed);
  926.  
  927.  
  928.  
  929. void setSimSpeed(kit::f64 newSpeed);
  930.  
  931.  
  932.  
  933.  
  934.  
  935. struct Box : kit::shape::rect {
  936.   kit::colors::ABGR c = 0xFFC0C0C0;
  937.  
  938.   Box(){}
  939.   Box(kit::f32 _x, kit::f32 _y, kit::f32 _w, kit::f32 _h){ x=_x; y=_y; w=_w; h=_h; }
  940.   Box(kit::s32 _x, kit::s32 _y, kit::s32 _w, kit::s32 _h){ x=_x; y=_y; w=_w; h=_h; }
  941.  
  942.   void draw(){
  943.     drawLine(x    , y    ,   x+w-1, y    ,   c);
  944.     drawLine(x+w-1, y    ,   x+w-1, y+h-1,   c);
  945.     drawLine(x+w-1, y+h-1,   x    , y+h-1,   c);
  946.     drawLine(x    , y+h-1,   x    , y    ,   c);
  947.   }
  948. };
  949.  
  950.  
  951.  
  952.  
  953.  
  954. #undef DEF_KIT
  955. #define DEF_KIT(_thing, _equal_to) _thing = _equal_to;
  956.  
  957. #endif /* _INCLUDE_ALL_HPP */
  958. /******************************************************************************/
  959. /******************************************************************************/
  960. //"2024-11-21\include\kit\all.hpp":
  961. //THIS LIBRARY WAS MADE TO USE SDL2 VERSION 2.28.2
  962.  
  963. #ifndef _INC_ALL_HPP
  964. #define _INC_ALL_HPP
  965.  
  966. #include "commondef.hpp"
  967. #include "misc.hpp"
  968. #include "video.hpp"
  969. #include "audio.hpp"
  970.  
  971.  
  972. #endif /* _INC_ALL_HPP */
  973. /******************************************************************************/
  974. /******************************************************************************/
  975. //"2024-11-21\include\kit\audio.hpp":
  976. //THIS LIBRARY WAS MADE TO USE SDL2 VERSION 2.28.2
  977.  
  978. #ifndef _INC_AUDIO_HPP
  979. #define _INC_AUDIO_HPP
  980.  
  981. #include "commondef.hpp"
  982. #include "_audio_types.hpp"
  983. #include "_audio_func.hpp"
  984. #include "_audio_AudioDevice.hpp"
  985. #include "_audio_AudioStream.hpp"
  986. #include "_audio_AudioData.hpp"
  987. #include "_audio_SoundEngine.hpp"
  988.  
  989.  
  990. #endif /* _INC_AUDIO_HPP */
  991. /******************************************************************************/
  992. /******************************************************************************/
  993. //"2024-11-21\include\kit\commondef.hpp":
  994. //THIS LIBRARY WAS MADE TO USE SDL2 VERSION 2.28.2
  995.  
  996. #ifndef _COMMONDEF_HPP
  997. #define _COMMONDEF_HPP
  998.  
  999. namespace kit {
  1000.  
  1001.  
  1002.  
  1003.  
  1004.  
  1005. #ifndef   MIN
  1006. #define   MIN(a,b)  ( ((a)<(b)) ? (a) : (b) )
  1007. #endif /* MIN(a,b) */
  1008.  
  1009. #ifndef   MAX
  1010. #define   MAX(a,b)  ( ((a)>(b)) ? (a) : (b) )
  1011. #endif /* MAX(a,b) */
  1012.  
  1013. #ifndef   CLAMP
  1014. #define   CLAMP(n, mn, mx)  MIN(MAX(n,mn),mx)
  1015. #endif /* CLAMP(n, mn, mx) */
  1016.  
  1017.  
  1018.  
  1019. //precise version
  1020. #ifndef   LERP
  1021. #define   LERP(_v0, _v1, _t)  (  (1.0f-(_t)) * (_v0)  +  (_t) * (_v1)  )
  1022. #endif /* LERP */
  1023.  
  1024. //imprecise version
  1025. #ifndef   LERP2
  1026. #define   LERP2(_v0, _v1, _t)  ( (_v0) + (_t) * ((_v1)-(_v0))  )
  1027. #endif /* LERP2 */
  1028.  
  1029.  
  1030.  
  1031. //creates an ABGR color in the form of a u32, not colors::ABGR
  1032. #ifndef   ABGR_U32
  1033. #define   ABGR_U32(_r,_g,_b,_a)                (\
  1034.   ((kit::u32)(_a)<<24) | ((kit::u32)(_b)<<16)  |\
  1035.   ((kit::u32)(_g)<< 8) | ((kit::u32)(_r)    )  )
  1036. #endif /* ABGR_U32 */
  1037.  
  1038. //creates an ARGB color in the form of a u32, not colors::ARGB
  1039. #ifndef   ARGB_U32
  1040. #define   ARGB_U32(_r,_g,_b,_a)                (\
  1041.   ((kit::u32)(_a)<<24) | ((kit::u32)(_r)<<16)  |\
  1042.   ((kit::u32)(_g)<< 8) | ((kit::u32)(_b)    )  )
  1043. #endif /* ABGR_U32 */
  1044.  
  1045.  
  1046.  
  1047. #ifndef   NULLDELETE
  1048. #define   NULLDELETE(_ptr_to_thing, _type_of_thing) { \
  1049.   _type_of_thing* _long_ptr_name_dont_name_anything_else_this = (_type_of_thing*)_ptr_to_thing; \
  1050.   _ptr_to_thing = nullptr; \
  1051.   delete _long_ptr_name_dont_name_anything_else_this; \
  1052. }
  1053. #endif /* NULLDELETE */
  1054.  
  1055.  
  1056.  
  1057. #ifndef   PTR_OFFSET
  1058. #define   PTR_OFFSET(_ptr, _offset, _type)               ( \
  1059.   (_type*)( ((kit::u64)(_ptr)) + ((kit::s64)(_offset)) )   )
  1060. #endif /* PTR_OFFSET */
  1061.  
  1062. #ifndef   PTR_OFFSETB
  1063. #define   PTR_OFFSETB(_ptr, _offset, _type)                            ( \
  1064.   (_type*)( ((kit::u64)(_ptr)) + ((kit::s64)(_offset)*sizeof(_type)) )   )
  1065. #endif /* PTR_OFFSETB */
  1066.  
  1067.  
  1068.  
  1069.  
  1070.  
  1071. // integer bounds
  1072. #define KIT_U8_MAX  0xFF
  1073. #define KIT_U16_MAX 0xFFFF
  1074. #define KIT_U24_MAX 0xFFFFFF
  1075. #define KIT_U32_MAX 0xFFFFFFFF
  1076. #define KIT_U64_MAX 0xFFFFFFFFFFFFFFFF
  1077.  //
  1078. #define KIT_S8_MIN  0x80
  1079. #define KIT_S8_MAX  0x7F
  1080. #define KIT_S16_MIN 0x8000
  1081. #define KIT_S16_MAX 0x7FFF
  1082. #define KIT_S24_MIN 0x800000
  1083. #define KIT_S24_MAX 0x7FFFFF
  1084. #define KIT_S32_MIN 0x80000000
  1085. #define KIT_S32_MAX 0x7FFFFFFF
  1086. #define KIT_S64_MIN 0x8000000000000000
  1087. #define KIT_S64_MAX 0x7FFFFFFFFFFFFFFF
  1088.  
  1089.  
  1090. // most significant bits/Bytes
  1091. #define KIT_MSb_8  0x80
  1092. #define KIT_MSb_16 0x8000
  1093. #define KIT_MSb_24 0x800000
  1094. #define KIT_MSb_32 0x80000000
  1095. #define KIT_MSb_64 0x8000000000000000
  1096.  //
  1097. #define KIT_MSB_8  0xFF
  1098. #define KIT_MSB_16 0xFF00
  1099. #define KIT_MSB_24 0xFF0000
  1100. #define KIT_MSB_32 0xFF000000
  1101. #define KIT_MSB_64 0xFF00000000000000
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107. #define BOOLSTR(_bool_value) ((_bool_value) ? _boolStr_true : _boolStr_false)
  1108. extern const char _boolStr_false[]; // = "false"
  1109. extern const char _boolStr_true[];  // = "true"
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115. #if defined(_STDINT) || defined(_CSTDINT_)
  1116. typedef uint8_t  u8 ;
  1117. typedef uint16_t u16;
  1118. typedef uint32_t u32;
  1119. typedef uint64_t u64;
  1120. typedef  int8_t  s8 ;
  1121. typedef  int16_t s16;
  1122. typedef  int32_t s32;
  1123. typedef  int64_t s64;
  1124.  
  1125. #else
  1126. typedef unsigned char      u8 ;
  1127. typedef unsigned short     u16;
  1128. typedef unsigned int       u32;
  1129. typedef unsigned long long u64;
  1130. typedef   signed char      s8 ;
  1131. typedef   signed short     s16;
  1132. typedef   signed int       s32;
  1133. typedef   signed long long s64;
  1134.  
  1135. #endif
  1136.  
  1137. // for consistency
  1138. typedef float  f32;
  1139. typedef double f64;
  1140.  
  1141.  
  1142.  
  1143. #if !defined(_STDDEF_H) && !defined(_STDDEF_H_) && !defined(_GLIBCXX_CSTDDEF)
  1144. #ifndef _SIZE_T_DEFINED
  1145. #define _SIZE_T_DEFINED
  1146.  
  1147. typedef u64 size_t;
  1148.  
  1149. #endif /* _SIZE_T_DEFINED */
  1150. #endif
  1151.  
  1152.  
  1153.  
  1154. //short for Generic Opaque Pointer; mostly (if not solely) used internally
  1155. typedef void* GenOpqPtr;
  1156.  
  1157.  
  1158.  
  1159.  
  1160.  
  1161. //used for parameters which require a value between 0 and 0x7FFFFFFF
  1162. union u31 {
  1163.     u32 v;
  1164.     struct {
  1165.       u32 n : 31;
  1166.       u32 s :  1;
  1167.     };
  1168.  
  1169.     inline u31(){}
  1170.  
  1171.     template <typename T>
  1172.     inline u31(T _v) : v(static_cast<u32>(_v)) {}
  1173.  
  1174. };
  1175.  
  1176.  
  1177.  
  1178.  
  1179.  
  1180. namespace colors {
  1181.  
  1182.   union ARGB8888 { //4B; 0xAARRGGBB
  1183.   //what GDI uses (save for the alpha channel)
  1184.     u32 v; //entire color [v]alue
  1185.     struct { u8 b, g, r, a; };
  1186.  
  1187.     ARGB8888() : v(0) {}
  1188.     ARGB8888(u32 _v) : v(_v) {}
  1189.     ARGB8888(u8 _r, u8 _g,
  1190.              u8 _b, u8 _a) : b(_b), g(_g), r(_r), a(_a) {}
  1191.     inline bool operator==(const ARGB8888& c ){ return (v == c.v); }
  1192.     inline bool operator!=(const ARGB8888& c ){ return (v != c.v); }
  1193.     inline bool operator==(const u32     & cv){ return (v == cv ); }
  1194.     inline bool operator!=(const u32     & cv){ return (v != cv ); }
  1195.     inline void operator =(const u32       cv){        (v  = cv ); }
  1196.   };
  1197.  
  1198.   typedef ARGB8888 ARGB;
  1199.  
  1200.  
  1201.   union ABGR8888 { //4B; 0xAABBGGRR
  1202.     u32 v; //entire color [v]alue
  1203.     struct { u8 r, g, b, a; };
  1204.  
  1205.     ABGR8888() : v(0) {}
  1206.     ABGR8888(u32 _v) : v(_v) {}
  1207.     ABGR8888(u8 _r, u8 _g,
  1208.              u8 _b, u8 _a) : r(_r), g(_g), b(_b), a(_a) {}
  1209.     inline bool operator==(const ABGR8888& c ){ return (v == c.v); }
  1210.     inline bool operator!=(const ABGR8888& c ){ return (v != c.v); }
  1211.     inline bool operator==(const u32     & cv){ return (v == cv ); }
  1212.     inline bool operator!=(const u32     & cv){ return (v != cv ); }
  1213.     inline void operator =(const u32       cv){        (v  = cv ); }
  1214.   };
  1215.  
  1216.   typedef ABGR8888 ABGR;
  1217.  
  1218.  
  1219.   struct BGR888 { //3B; 0xBBGGRR
  1220.     //can't use a v of u32 here, or else this turns into a 4 byte struct
  1221.      //(which makes assignments from and comparisons of u32 values annoying)
  1222.     u8 r, g, b;
  1223.  
  1224.     BGR888() : r(0), g(0), b(0) {}
  1225.     BGR888(u32 v) : r(v&255), g((v>>8)&255), b((v>>16)&255) {}
  1226.     BGR888(u8 _r, u8 _g, u8 _b) : r(_r), g(_g), b(_b) {}
  1227.     #define _BGR888_U32_ ((u32)(b)<<16|(u32)(g)<<8|(u32)(r))
  1228.     inline bool operator==(const BGR888& c ){ return (r==c.r && g==c.g && b==c.b); }
  1229.     inline bool operator!=(const BGR888& c ){ return (r!=c.r && g!=c.g && b!=c.b); }
  1230.     inline bool operator==(const u32   & cv){ return (_BGR888_U32_ == cv ); }
  1231.     inline bool operator!=(const u32   & cv){ return (_BGR888_U32_ != cv ); }
  1232.     inline void operator =(const u32     cv){ r=cv&255, g=(cv>>8)&255, b=(cv>>16)&255; }
  1233.     #undef _BGR888_U32_
  1234.   };
  1235.  
  1236.   typedef BGR888 BGR;
  1237.  
  1238.  
  1239.   union ARGB1555 { //2B; 0bARRRRRGGGGGBBBBB
  1240.     u16 v; //entire color [v]alue
  1241.     struct {
  1242.       u16 b : 5;
  1243.       u16 g : 5;
  1244.       u16 r : 5;
  1245.       u16 a : 1;
  1246.     };
  1247.  
  1248.     ARGB1555() : v(0) {}
  1249.     ARGB1555(u16 _v) : v(_v) {}
  1250.     ARGB1555(u8 _r, u8 _g,
  1251.              u8 _b, u8 _a) : b(_b>>3), g(_g>>3), r(_r>>3), a(_a>>7) {}
  1252.     inline bool operator==(const ARGB1555& c ){ return (v == c.v); }
  1253.     inline bool operator!=(const ARGB1555& c ){ return (v != c.v); }
  1254.     inline bool operator==(const u16     & cv){ return (v == cv ); }
  1255.     inline bool operator!=(const u16     & cv){ return (v != cv ); }
  1256.     inline void operator =(const u16       cv){        (v  = cv ); }
  1257.   };
  1258.  
  1259. }; /* namespace color */
  1260.  
  1261.  
  1262.  
  1263.  
  1264.  
  1265. namespace shape {
  1266.  
  1267.   struct spoint {
  1268.     s16 x, y;
  1269.     spoint() : x(0), y(0) {}
  1270.     spoint(s16 _x, s16 _y) : x(_x), y(_y) {}
  1271.     inline bool operator==(const spoint& p){ return (x==p.x && y==p.y); };
  1272.     inline bool operator!=(const spoint& p){ return (x!=p.x && y!=p.y); };
  1273.     inline void operator-=(const spoint& p){ x -= p.x;  y -= p.y; }
  1274.     inline void operator+=(const spoint& p){ x += p.x;  y += p.y; }
  1275.   };
  1276.  
  1277.   struct srect {
  1278.     s16 x, y; //x & y position of the rectangle's top-left corner
  1279.     s16 w, h; //the rectangle's width & height
  1280.     srect() : x(0), y(0), w(0), h(0) {}
  1281.     srect(s16 _x, s16 _y) : x(_x), y(_y) {}
  1282.     srect(s16 _x, s16 _y,
  1283.           s16 _w, s16 _h) : x(_x), y(_y), w(_w), h(_h) {}
  1284.     inline bool operator==(const srect& r){ return (x==r.x && y==r.y && w==r.w && h==r.h); };
  1285.     inline bool operator!=(const srect& r){ return (x!=r.x && y!=r.y && w!=r.w && h!=r.h); };
  1286.     inline void operator-=(const spoint& p){ x -= p.x;  y -= p.y; }
  1287.     inline void operator+=(const spoint& p){ x += p.x;  y += p.y; }
  1288.   };
  1289.  
  1290.   struct sline {
  1291.     s16 x0, y0;
  1292.     s16 x1, y1;
  1293.     sline() : x0(0), y0(0), x1(0), y1(0) {}
  1294.     sline(s16 _x0, s16 _y0,
  1295.           s16 _x1, s16 _y1) : x0(_x0), y0(_y0), x1(_x1), y1(_y1) {}
  1296.     inline bool operator==(const sline& l){ return (x0==l.x0 && y0==l.y0 && x1==l.x1 && y1==l.y1); };
  1297.     inline bool operator!=(const sline& l){ return (x0!=l.x0 && y0!=l.y0 && x1!=l.x1 && y1!=l.y1); };
  1298.   };
  1299.  
  1300.   struct scircle {
  1301.     s16 x, y, r;
  1302.     scircle() : x(0), y(0), r(0) {}
  1303.     scircle(s16 _x, s16 _y, s16 _r) : x(_x), y(_y), r(_r) {}
  1304.     inline bool operator==(const scircle& c){ return (x==c.x && y==c.y && r==c.r); };
  1305.     inline bool operator!=(const scircle& c){ return (x!=c.x && y!=c.y && r!=c.r); };
  1306.     inline void operator-=(const spoint& p){ x -= p.x;  y -= p.y; }
  1307.     inline void operator+=(const spoint& p){ x += p.x;  y += p.y; }
  1308.   };
  1309.  
  1310.   struct spoint3d {
  1311.     s16 x, y, z;
  1312.     spoint3d() : x(0), y(0), z(0) {}
  1313.     spoint3d(s16 _x, s16 _y, s16 _z) : x(_x), y(_y), z(_z) {}
  1314.     inline bool operator==(const spoint3d& p){ return (x==p.x && y==p.y && z==p.z); }
  1315.     inline bool operator!=(const spoint3d& p){ return (x!=p.x && y!=p.y && z!=p.z); }
  1316.   };
  1317.  
  1318.  
  1319.  
  1320.   struct point {
  1321.     s32 x, y;
  1322.     point() : x(0), y(0) {}
  1323.     point(s32 _x, s32 _y) : x(_x), y(_y) {}
  1324.     inline bool operator==(const point& p){ return (x==p.x && y==p.y); };
  1325.     inline bool operator!=(const point& p){ return (x!=p.x && y!=p.y); };
  1326.     inline void operator-=(const point& p){ x -= p.x;  y -= p.y; }
  1327.     inline void operator+=(const point& p){ x += p.x;  y += p.y; }
  1328.   };
  1329.  
  1330.   struct rect {
  1331.     s32 x, y; //x & y position of the rectangle's top-left corner
  1332.     s32 w, h; //the rectangle's width & height
  1333.     rect() : x(0), y(0), w(0), h(0) {}
  1334.     rect(s32 _x, s32 _y) : x(_x), y(_y) {}
  1335.     rect(s32 _x, s32 _y,
  1336.          s32 _w, s32 _h) : x(_x), y(_y), w(_w), h(_h) {}
  1337.     inline bool operator==(const rect& r){ return (x==r.x && y==r.y && w==r.w && h==r.h); };
  1338.     inline bool operator!=(const rect& r){ return (x!=r.x && y!=r.y && w!=r.w && h!=r.h); };
  1339.     inline void operator-=(const point& p){ x -= p.x;  y -= p.y; }
  1340.     inline void operator+=(const point& p){ x += p.x;  y += p.y; }
  1341.   };
  1342.  
  1343.   struct line {
  1344.     s32 x0, y0;
  1345.     s32 x1, y1;
  1346.     line() : x0(0), y0(0), x1(0), y1(0) {}
  1347.     line(s32 _x0, s32 _y0,
  1348.          s32 _x1, s32 _y1) : x0(_x0), y0(_y0), x1(_x1), y1(_y1) {}
  1349.     inline bool operator==(const line& l){ return (x0==l.x0 && y0==l.y0 && x1==l.x1 && y1==l.y1); };
  1350.     inline bool operator!=(const line& l){ return (x0!=l.x0 && y0!=l.y0 && x1!=l.x1 && y1!=l.y1); };
  1351.   };
  1352.  
  1353.   struct circle {
  1354.     s32 x, y, r;
  1355.     circle() : x(0), y(0), r(0) {}
  1356.     circle(s32 _x, s32 _y, s32 _r) : x(_x), y(_y), r(_r) {}
  1357.     inline bool operator==(const circle& c){ return (x==c.x && y==c.y && r==c.r); };
  1358.     inline bool operator!=(const circle& c){ return (x!=c.x && y!=c.y && r!=c.r); };
  1359.     inline void operator-=(const point& p){ x -= p.x;  y -= p.y; }
  1360.     inline void operator+=(const point& p){ x += p.x;  y += p.y; }
  1361.   };
  1362.  
  1363.   struct point3d {
  1364.     s32 x, y, z;
  1365.     point3d() : x(0), y(0), z(0) {}
  1366.     point3d(s32 _x, s32 _y, s32 _z) : x(_x), y(_y), z(_z) {}
  1367.     inline bool operator==(const point3d& p){ return (x==p.x && y==p.y && z==p.z); }
  1368.     inline bool operator!=(const point3d& p){ return (x!=p.x && y!=p.y && z!=p.z); }
  1369.   };
  1370.  
  1371.  
  1372.  
  1373.   struct fpoint {
  1374.     f32 x, y;
  1375.     fpoint() : x(0.0f), y(0.0f) {}
  1376.     fpoint(f32 _x, f32 _y) : x(_x), y(_y) {}
  1377.     inline bool operator==(const fpoint& p){ return (x==p.x && y==p.y); };
  1378.     inline bool operator!=(const fpoint& p){ return (x!=p.x && y!=p.y); };
  1379.     inline void operator-=(const fpoint& p){ x -= p.x;  y -= p.y; }
  1380.     inline void operator+=(const fpoint& p){ x += p.x;  y += p.y; }
  1381.   };
  1382.  
  1383.   struct frect {
  1384.     f32 x, y; //x & y position of rectangle's top-left corner
  1385.     f32 w, h; //the rectangle's width & height
  1386.     frect() : x(0.0f), y(0.0f), w(0.0f), h(0.0f) {}
  1387.     frect(f32 _x, f32 _y) : x(_x), y(_y) {}
  1388.     frect(f32 _x, f32 _y,
  1389.           f32 _w, f32 _h) : x(_x), y(_y), w(_w), h(_h) {}
  1390.     inline bool operator==(const frect& r){ return (x==r.x && y==r.y && w==r.w && h==r.h); };
  1391.     inline bool operator!=(const frect& r){ return (x!=r.x && y!=r.y && w!=r.w && h!=r.h); };
  1392.     inline void operator-=(const fpoint& p){ x -= p.x;  y -= p.y; }
  1393.     inline void operator+=(const fpoint& p){ x += p.x;  y += p.y; }
  1394.   };
  1395.  
  1396.   struct fline {
  1397.     f32 x0, y0;
  1398.     f32 x1, y1;
  1399.     fline() : x0(0.0f), y0(0.0f), x1(0.0f), y1(0.0f) {}
  1400.     fline(f32 _x0, f32 _y0,
  1401.           f32 _x1, f32 _y1) : x0(_x0), y0(_y0), x1(_x1), y1(_y1) {}
  1402.     inline bool operator==(const fline& l){ return (x0==l.x0 && y0==l.y0 && x1==l.x1 && y1==l.y1); };
  1403.     inline bool operator!=(const fline& l){ return (x0!=l.x0 && y0!=l.y0 && x1!=l.x1 && y1!=l.y1); };
  1404.   };
  1405.  
  1406.   struct fcircle {
  1407.     f32 x, y, r;
  1408.     fcircle() : x(0.0f), y(0.0f), r(0.0f) {}
  1409.     fcircle(f32 _x, f32 _y, f32 _r) : x(_x), y(_y), r(_r) {}
  1410.     inline bool operator==(const fcircle& c){ return (x==c.x && y==c.y && r==c.r); };
  1411.     inline bool operator!=(const fcircle& c){ return (x!=c.x && y!=c.y && r!=c.r); };
  1412.     inline void operator-=(const fpoint& p){ x -= p.x;  y -= p.y; }
  1413.     inline void operator+=(const fpoint& p){ x += p.x;  y += p.y; }
  1414.   };
  1415.  
  1416.   struct fpoint3d {
  1417.     f32 x, y, z;
  1418.     fpoint3d() : x(0.0f), y(0.0f), z(0.0f) {}
  1419.     fpoint3d(f32 _x, f32 _y, f32 _z) : x(_x), y(_y), z(_z) {}
  1420.     inline bool operator==(const fpoint3d& p){ return (x==p.x && y==p.y && z==p.z); }
  1421.     inline bool operator!=(const fpoint3d& p){ return (x!=p.x && y!=p.y && z!=p.z); }
  1422.   };
  1423.  
  1424.  
  1425.  
  1426.   struct dpoint {
  1427.     f64 x, y;
  1428.     dpoint() : x(0.0), y(0.0) {}
  1429.     dpoint(f64 _x, f64 _y) : x(_x), y(_y) {}
  1430.     inline bool operator==(const dpoint& p){ return (x==p.x && y==p.y); };
  1431.     inline bool operator!=(const dpoint& p){ return (x!=p.x && y!=p.y); };
  1432.     inline void operator-=(const dpoint& p){ x -= p.x;  y -= p.y; }
  1433.     inline void operator+=(const dpoint& p){ x += p.x;  y += p.y; }
  1434.   };
  1435.  
  1436.   struct drect {
  1437.     f64 x, y; //x & y position of rectangle's top-left corner
  1438.     f64 w, h; //the rectangle's width & height
  1439.     drect() : x(0.0), y(0.0), w(0.0), h(0.0) {}
  1440.     drect(f64 _x, f64 _y) : x(_x), y(_y) {}
  1441.     drect(f64 _x, f64 _y,
  1442.           f64 _w, f64 _h) : x(_x), y(_y), w(_w), h(_h) {}
  1443.     inline bool operator==(const drect& r){ return (x==r.x && y==r.y && w==r.w && h==r.h); };
  1444.     inline bool operator!=(const drect& r){ return (x!=r.x && y!=r.y && w!=r.w && h!=r.h); };
  1445.     inline void operator-=(const dpoint& p){ x -= p.x;  y -= p.y; }
  1446.     inline void operator+=(const dpoint& p){ x += p.x;  y += p.y; }
  1447.   };
  1448.  
  1449.   struct dline {
  1450.     f64 x0, y0;
  1451.     f64 x1, y1;
  1452.     dline() : x0(0.0), y0(0.0), x1(0.0), y1(0.0) {}
  1453.     dline(f64 _x0, f64 _y0,
  1454.           f64 _x1, f64 _y1) : x0(_x0), y0(_y0), x1(_x1), y1(_y1) {}
  1455.     inline bool operator==(const dline& l){ return (x0==l.x0 && y0==l.y0 && x1==l.x1 && y1==l.y1); };
  1456.     inline bool operator!=(const dline& l){ return (x0!=l.x0 && y0!=l.y0 && x1!=l.x1 && y1!=l.y1); };
  1457.   };
  1458.  
  1459.   struct dcircle {
  1460.     f64 x, y;
  1461.     f64 r;
  1462.     dcircle() : x(0.0), y(0.0), r(0.0) {}
  1463.     dcircle(f64 _x, f64 _y, f64 _r) : x(_x), y(_y), r(_r) {}
  1464.     inline bool operator==(const dcircle& c){ return (x==c.x && y==c.y && r==c.r); };
  1465.     inline bool operator!=(const dcircle& c){ return (x!=c.x && y!=c.y && r!=c.r); };
  1466.     inline void operator-=(const dpoint& p){ x -= p.x;  y -= p.y; }
  1467.     inline void operator+=(const dpoint& p){ x += p.x;  y += p.y; }
  1468.   };
  1469.  
  1470.   struct dpoint3d {
  1471.     f64 x, y, z;
  1472.     dpoint3d() : x(0.0), y(0.0), z(0.0) {}
  1473.     dpoint3d(f64 _x, f64 _y, f64 _z) : x(_x), y(_y), z(_z) {}
  1474.     inline bool operator==(const dpoint3d& p){ return (x==p.x && y==p.y && z==p.z); }
  1475.     inline bool operator!=(const dpoint3d& p){ return (x!=p.x && y!=p.y && z!=p.z); }
  1476.   };
  1477.  
  1478. }; /* namespace shape */
  1479.  
  1480.  
  1481.  
  1482.  
  1483.  
  1484. //turns a byte into an integer that is printf'able with the format secifier "%08x"
  1485. static inline u32 bin_hex(char n){
  1486.     return (n&128)<<21|(n&64)<<18|(n&32)<<15|(n&16)<<12|(n&8)<<9|(n&4)<<6|(n&2)<<3|(n&1);
  1487. }
  1488.  
  1489.  
  1490.  
  1491.  
  1492.  
  1493. }; /* namespace kit */
  1494.  
  1495. #endif /* _COMMONDEF_HPP */
  1496.  
Add Comment
Please, Sign In to add comment