Kitomas

work for 2024-08-30 (4/5)

Aug 29th, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 32.09 KB | None | 0 0
  1. /******************************************************************************/
  2. /******************************************************************************/
  3. //"ksdl2\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. //"ksdl2\include\kit\audio.hpp":
  199. //THIS LIBRARY WAS MADE TO USE SDL2 VERSION 2.28.2
  200.  
  201. #ifndef _INC_AUDIO_HPP
  202. #define _INC_AUDIO_HPP
  203.  
  204. #include "commondef.hpp"
  205. #include "_audio_types.hpp"
  206. #include "_audio_func.hpp"
  207. #include "_audio_AudioDevice.hpp"
  208. #include "_audio_AudioStream.hpp"
  209. #include "_audio_AudioData.hpp"
  210.  
  211.  
  212. #endif /* _INC_AUDIO_HPP */
  213. /******************************************************************************/
  214. /******************************************************************************/
  215. //"ksdl2\include\kit\commondef.hpp":
  216. //THIS LIBRARY WAS MADE TO USE SDL2 VERSION 2.28.2
  217.  
  218. #ifndef _COMMONDEF_HPP
  219. #define _COMMONDEF_HPP
  220.  
  221. namespace kit {
  222.  
  223.  
  224.  
  225.  
  226.  
  227. #ifndef   MIN
  228. #define   MIN(a,b)  ( ((a)<(b)) ? (a) : (b) )
  229. #endif /* MIN(a,b) */
  230.  
  231. #ifndef   MAX
  232. #define   MAX(a,b)  ( ((a)>(b)) ? (a) : (b) )
  233. #endif /* MAX(a,b) */
  234.  
  235. #ifndef   CLAMP
  236. #define   CLAMP(n, mn, mx)  MIN(MAX(n,mn),mx)
  237. #endif /* CLAMP(n, mn, mx) */
  238.  
  239.  
  240.  
  241. //precise version
  242. #ifndef   LERP
  243. #define   LERP(_v0, _v1, _t)  (  (1.0f-(_t)) * (_v0)  +  (_t) * (_v1)  )
  244. #endif /* LERP */
  245.  
  246. //imprecise version
  247. #ifndef   LERP2
  248. #define   LERP2(_v0, _v1, _t)  ( (_v0) + (_t) * ((_v1)-(_v0))  )
  249. #endif /* LERP2 */
  250.  
  251.  
  252.  
  253. //creates an ABGR color in the form of a u32, not colors::ABGR
  254. #ifndef   ABGR_U32
  255. #define   ABGR_U32(_r,_g,_b,_a)                (\
  256.   ((kit::u32)(_a)<<24) | ((kit::u32)(_b)<<16)  |\
  257.   ((kit::u32)(_g)<< 8) | ((kit::u32)(_r)    )  )
  258. #endif /* ABGR_U32 */
  259.  
  260. //creates an ARGB color in the form of a u32, not colors::ARGB
  261. #ifndef   ARGB_U32
  262. #define   ARGB_U32(_r,_g,_b,_a)                (\
  263.   ((kit::u32)(_a)<<24) | ((kit::u32)(_r)<<16)  |\
  264.   ((kit::u32)(_g)<< 8) | ((kit::u32)(_b)    )  )
  265. #endif /* ABGR_U32 */
  266.  
  267.  
  268.  
  269. #ifndef   NULLDELETE
  270. #define   NULLDELETE(_ptr_to_thing) { delete _ptr_to_thing;  _ptr_to_thing = nullptr; }
  271. #endif /* NULLDELETE */
  272.  
  273.  
  274.  
  275. #ifndef   PTR_OFFSET
  276. #define   PTR_OFFSET(_ptr, _offset, _type)               ( \
  277.   (_type*)( ((kit::u64)(_ptr)) + ((kit::s64)(_offset)) )   )
  278. #endif /* PTR_OFFSET */
  279.  
  280. #ifndef   PTR_OFFSETB
  281. #define   PTR_OFFSETB(_ptr, _offset, _type)                            ( \
  282.   (_type*)( ((kit::u64)(_ptr)) + ((kit::s64)(_offset)*sizeof(_type)) )   )
  283. #endif /* PTR_OFFSETB */
  284.  
  285.  
  286.  
  287.  
  288.  
  289. // integer bounds
  290. #define KIT_U8_MAX  (0xFF)
  291. #define KIT_U16_MAX (0xFFFF)
  292. #define KIT_U32_MAX (0xFFFFFFFF)
  293. #define KIT_U64_MAX (0xFFFFFFFFFFFFFFFF)
  294.  //
  295. #define KIT_S8_MIN  (0x80)
  296. #define KIT_S8_MAX  (0x7F)
  297. #define KIT_S16_MIN (0x8000)
  298. #define KIT_S16_MAX (0x7FFF)
  299. #define KIT_S32_MIN (0x80000000)
  300. #define KIT_S32_MAX (0x7FFFFFFF)
  301. #define KIT_S64_MIN (0x8000000000000000)
  302. #define KIT_S64_MAX (0x7FFFFFFFFFFFFFFF)
  303.  
  304.  
  305. // most significant bits/Bytes
  306. #define KIT_MSb_8  (0x80)
  307. #define KIT_MSb_16 (0x8000)
  308. #define KIT_MSb_32 (0x80000000)
  309. #define KIT_MSb_64 (0x8000000000000000)
  310.  //
  311. #define KIT_MSB_8  (0xFF)
  312. #define KIT_MSB_16 (0xFF00)
  313. #define KIT_MSB_32 (0xFF000000)
  314. #define KIT_MSB_64 (0xFF00000000000000)
  315.  
  316.  
  317.  
  318.  
  319.  
  320. #define BOOLSTR(_bool_value) ((_bool_value) ? _boolStr_true : _boolStr_false)
  321. extern const char _boolStr_false[]; // = "false"
  322. extern const char _boolStr_true[];  // = "true"
  323.  
  324.  
  325.  
  326.  
  327.  
  328. #if defined(_STDINT) || defined(_CSTDINT_)
  329. typedef uint8_t  u8 ;
  330. typedef uint16_t u16;
  331. typedef uint32_t u32;
  332. typedef uint64_t u64;
  333. typedef  int8_t  s8 ;
  334. typedef  int16_t s16;
  335. typedef  int32_t s32;
  336. typedef  int64_t s64;
  337.  
  338. #else
  339. typedef unsigned char      u8 ;
  340. typedef unsigned short     u16;
  341. typedef unsigned int       u32;
  342. typedef unsigned long long u64;
  343. typedef   signed char      s8 ;
  344. typedef   signed short     s16;
  345. typedef   signed int       s32;
  346. typedef   signed long long s64;
  347.  
  348. #endif
  349.  
  350. // for consistency
  351. typedef float  f32;
  352. typedef double f64;
  353.  
  354.  
  355.  
  356.  
  357.  
  358. #if !defined(_STDDEF_H) && !defined(_STDDEF_H_) && !defined(_GLIBCXX_CSTDDEF)
  359. #ifndef _SIZE_T_DEFINED
  360. #define _SIZE_T_DEFINED
  361.  
  362. typedef u64 size_t;
  363.  
  364. #endif /* _SIZE_T_DEFINED */
  365. #endif
  366.  
  367.  
  368.  
  369.  
  370.  
  371. //short for Generic Opaque Pointer; mostly used internally
  372. typedef void* GenOpqPtr;
  373.  
  374.  
  375.  
  376.  
  377.  
  378. namespace colors {
  379.  
  380.   union ARGB8888 { //4B; 0xAARRGGBB
  381.   //what GDI uses (save for the alpha channel)
  382.     u32 v; //entire color [v]alue
  383.     struct { u8 b, g, r, a; };
  384.  
  385.     ARGB8888() : v(0) {}
  386.     ARGB8888(u32 _v) : v(_v) {}
  387.     ARGB8888(u8 _r, u8 _g,
  388.              u8 _b, u8 _a) : b(_b), g(_g), r(_r), a(_a) {}
  389.     inline bool operator==(const ARGB8888& c ){ return (v == c.v); }
  390.     inline bool operator!=(const ARGB8888& c ){ return (v != c.v); }
  391.     inline bool operator==(const u32     & cv){ return (v == cv ); }
  392.     inline bool operator!=(const u32     & cv){ return (v != cv ); }
  393.     inline void operator =(const u32       cv){        (v  = cv ); }
  394.   };
  395.  
  396.   typedef ARGB8888 ARGB;
  397.  
  398.  
  399.   union ABGR8888 { //4B; 0xAABBGGRR
  400.     u32 v; //entire color [v]alue
  401.     struct { u8 r, g, b, a; };
  402.  
  403.     ABGR8888() : v(0) {}
  404.     ABGR8888(u32 _v) : v(_v) {}
  405.     ABGR8888(u8 _r, u8 _g,
  406.              u8 _b, u8 _a) : r(_r), g(_g), b(_b), a(_a) {}
  407.     inline bool operator==(const ABGR8888& c ){ return (v == c.v); }
  408.     inline bool operator!=(const ABGR8888& c ){ return (v != c.v); }
  409.     inline bool operator==(const u32     & cv){ return (v == cv ); }
  410.     inline bool operator!=(const u32     & cv){ return (v != cv ); }
  411.     inline void operator =(const u32       cv){        (v  = cv ); }
  412.   };
  413.  
  414.   typedef ABGR8888 ABGR;
  415.  
  416.  
  417.   struct BGR888 { //3B; 0xBBGGRR
  418.     //can't use a v of u32 here, or else this turns into a 4 byte struct
  419.      //(which makes assignments from and comparisons of u32 values annoying)
  420.     u8 r, g, b;
  421.  
  422.     BGR888() : r(0), g(0), b(0) {}
  423.     BGR888(u32 v) : r(v&255), g((v>>8)&255), b((v>>16)&255) {}
  424.     BGR888(u8 _r, u8 _g, u8 _b) : r(_r), g(_g), b(_b) {}
  425.     #define _BGR888_U32_ ((u32)(b)<<16|(u32)(g)<<8|(u32)(r))
  426.     inline bool operator==(const BGR888& c ){ return (r==c.r && g==c.g && b==c.b); }
  427.     inline bool operator!=(const BGR888& c ){ return (r!=c.r && g!=c.g && b!=c.b); }
  428.     inline bool operator==(const u32   & cv){ return (_BGR888_U32_ == cv ); }
  429.     inline bool operator!=(const u32   & cv){ return (_BGR888_U32_ != cv ); }
  430.     inline void operator =(const u32     cv){ r=cv&255, g=(cv>>8)&255, b=(cv>>16)&255; }
  431.     #undef _BGR888_U32_
  432.   };
  433.  
  434.   typedef BGR888 BGR;
  435.  
  436.  
  437.   union ARGB1555 { //2B; 0bARRRRRGGGGGBBBBB
  438.     u16 v; //entire color [v]alue
  439.     struct {
  440.       u16 b : 5;
  441.       u16 g : 5;
  442.       u16 r : 5;
  443.       u16 a : 1;
  444.     };
  445.  
  446.     ARGB1555() : v(0) {}
  447.     ARGB1555(u16 _v) : v(_v) {}
  448.     ARGB1555(u8 _r, u8 _g,
  449.              u8 _b, u8 _a) : b(_b>>3), g(_g>>3), r(_r>>3), a(_a>>7) {}
  450.     inline bool operator==(const ARGB1555& c ){ return (v == c.v); }
  451.     inline bool operator!=(const ARGB1555& c ){ return (v != c.v); }
  452.     inline bool operator==(const u16     & cv){ return (v == cv ); }
  453.     inline bool operator!=(const u16     & cv){ return (v != cv ); }
  454.     inline void operator =(const u16       cv){        (v  = cv ); }
  455.   };
  456.  
  457. }; /* namespace color */
  458.  
  459.  
  460.  
  461.  
  462.  
  463. namespace shape {
  464.  
  465.   struct spoint {
  466.     s16 x, y;
  467.     spoint() : x(0), y(0) {}
  468.     spoint(s16 _x, s16 _y) : x(_x), y(_y) {}
  469.     inline bool operator==(const spoint& p){ return (x==p.x && y==p.y); };
  470.     inline bool operator!=(const spoint& p){ return (x!=p.x && y!=p.y); };
  471.     inline void operator-=(const spoint& p){ x -= p.x;  y -= p.y; }
  472.     inline void operator+=(const spoint& p){ x += p.x;  y += p.y; }
  473.   };
  474.  
  475.   struct srect {
  476.     s16 x, y; //x & y position of the rectangle's top-left corner
  477.     s16 w, h; //the rectangle's width & height
  478.     srect() : x(0), y(0), w(0), h(0) {}
  479.     srect(s16 _x, s16 _y) : x(_x), y(_y) {}
  480.     srect(s16 _x, s16 _y,
  481.           s16 _w, s16 _h) : x(_x), y(_y), w(_w), h(_h) {}
  482.     inline bool operator==(const srect& r){ return (x==r.x && y==r.y && w==r.w && h==r.h); };
  483.     inline bool operator!=(const srect& r){ return (x!=r.x && y!=r.y && w!=r.w && h!=r.h); };
  484.     inline void operator-=(const spoint& p){ x -= p.x;  y -= p.y; }
  485.     inline void operator+=(const spoint& p){ x += p.x;  y += p.y; }
  486.   };
  487.  
  488.   struct sline {
  489.     s16 x0, y0;
  490.     s16 x1, y1;
  491.     sline() : x0(0), y0(0), x1(0), y1(0) {}
  492.     sline(s16 _x0, s16 _y0,
  493.           s16 _x1, s16 _y1) : x0(_x0), y0(_y0), x1(_x1), y1(_y1) {}
  494.     inline bool operator==(const sline& l){ return (x0==l.x0 && y0==l.y0 && x1==l.x1 && y1==l.y1); };
  495.     inline bool operator!=(const sline& l){ return (x0!=l.x0 && y0!=l.y0 && x1!=l.x1 && y1!=l.y1); };
  496.   };
  497.  
  498.   struct scircle {
  499.     s16 x, y, r;
  500.     scircle() : x(0), y(0), r(0) {}
  501.     scircle(s16 _x, s16 _y, s16 _r) : x(_x), y(_y), r(_r) {}
  502.     inline bool operator==(const scircle& c){ return (x==c.x && y==c.y && r==c.r); };
  503.     inline bool operator!=(const scircle& c){ return (x!=c.x && y!=c.y && r!=c.r); };
  504.     inline void operator-=(const spoint& p){ x -= p.x;  y -= p.y; }
  505.     inline void operator+=(const spoint& p){ x += p.x;  y += p.y; }
  506.   };
  507.  
  508.   struct spoint3d {
  509.     s16 x, y, z;
  510.     spoint3d() : x(0), y(0), z(0) {}
  511.     spoint3d(s16 _x, s16 _y, s16 _z) : x(_x), y(_y), z(_z) {}
  512.     inline bool operator==(const spoint3d& p){ return (x==p.x && y==p.y && z==p.z); }
  513.     inline bool operator!=(const spoint3d& p){ return (x!=p.x && y!=p.y && z!=p.z); }
  514.   };
  515.  
  516.  
  517.  
  518.   struct point {
  519.     s32 x, y;
  520.     point() : x(0), y(0) {}
  521.     point(s32 _x, s32 _y) : x(_x), y(_y) {}
  522.     inline bool operator==(const point& p){ return (x==p.x && y==p.y); };
  523.     inline bool operator!=(const point& p){ return (x!=p.x && y!=p.y); };
  524.     inline void operator-=(const point& p){ x -= p.x;  y -= p.y; }
  525.     inline void operator+=(const point& p){ x += p.x;  y += p.y; }
  526.   };
  527.  
  528.   struct rect {
  529.     s32 x, y; //x & y position of the rectangle's top-left corner
  530.     s32 w, h; //the rectangle's width & height
  531.     rect() : x(0), y(0), w(0), h(0) {}
  532.     rect(s32 _x, s32 _y) : x(_x), y(_y) {}
  533.     rect(s32 _x, s32 _y,
  534.          s32 _w, s32 _h) : x(_x), y(_y), w(_w), h(_h) {}
  535.     inline bool operator==(const rect& r){ return (x==r.x && y==r.y && w==r.w && h==r.h); };
  536.     inline bool operator!=(const rect& r){ return (x!=r.x && y!=r.y && w!=r.w && h!=r.h); };
  537.     inline void operator-=(const point& p){ x -= p.x;  y -= p.y; }
  538.     inline void operator+=(const point& p){ x += p.x;  y += p.y; }
  539.   };
  540.  
  541.   struct line {
  542.     s32 x0, y0;
  543.     s32 x1, y1;
  544.     line() : x0(0), y0(0), x1(0), y1(0) {}
  545.     line(s32 _x0, s32 _y0,
  546.          s32 _x1, s32 _y1) : x0(_x0), y0(_y0), x1(_x1), y1(_y1) {}
  547.     inline bool operator==(const line& l){ return (x0==l.x0 && y0==l.y0 && x1==l.x1 && y1==l.y1); };
  548.     inline bool operator!=(const line& l){ return (x0!=l.x0 && y0!=l.y0 && x1!=l.x1 && y1!=l.y1); };
  549.   };
  550.  
  551.   struct circle {
  552.     s32 x, y, r;
  553.     circle() : x(0), y(0), r(0) {}
  554.     circle(s32 _x, s32 _y, s32 _r) : x(_x), y(_y), r(_r) {}
  555.     inline bool operator==(const circle& c){ return (x==c.x && y==c.y && r==c.r); };
  556.     inline bool operator!=(const circle& c){ return (x!=c.x && y!=c.y && r!=c.r); };
  557.     inline void operator-=(const point& p){ x -= p.x;  y -= p.y; }
  558.     inline void operator+=(const point& p){ x += p.x;  y += p.y; }
  559.   };
  560.  
  561.   struct point3d {
  562.     s32 x, y, z;
  563.     point3d() : x(0), y(0), z(0) {}
  564.     point3d(s32 _x, s32 _y, s32 _z) : x(_x), y(_y), z(_z) {}
  565.     inline bool operator==(const point3d& p){ return (x==p.x && y==p.y && z==p.z); }
  566.     inline bool operator!=(const point3d& p){ return (x!=p.x && y!=p.y && z!=p.z); }
  567.   };
  568.  
  569.  
  570.  
  571.   struct fpoint {
  572.     f32 x, y;
  573.     fpoint() : x(0.0f), y(0.0f) {}
  574.     fpoint(f32 _x, f32 _y) : x(_x), y(_y) {}
  575.     inline bool operator==(const fpoint& p){ return (x==p.x && y==p.y); };
  576.     inline bool operator!=(const fpoint& p){ return (x!=p.x && y!=p.y); };
  577.     inline void operator-=(const fpoint& p){ x -= p.x;  y -= p.y; }
  578.     inline void operator+=(const fpoint& p){ x += p.x;  y += p.y; }
  579.   };
  580.  
  581.   struct frect {
  582.     f32 x, y; //x & y position of rectangle's top-left corner
  583.     f32 w, h; //the rectangle's width & height
  584.     frect() : x(0.0f), y(0.0f), w(0.0f), h(0.0f) {}
  585.     frect(f32 _x, f32 _y) : x(_x), y(_y) {}
  586.     frect(f32 _x, f32 _y,
  587.           f32 _w, f32 _h) : x(_x), y(_y), w(_w), h(_h) {}
  588.     inline bool operator==(const frect& r){ return (x==r.x && y==r.y && w==r.w && h==r.h); };
  589.     inline bool operator!=(const frect& r){ return (x!=r.x && y!=r.y && w!=r.w && h!=r.h); };
  590.     inline void operator-=(const fpoint& p){ x -= p.x;  y -= p.y; }
  591.     inline void operator+=(const fpoint& p){ x += p.x;  y += p.y; }
  592.   };
  593.  
  594.   struct fline {
  595.     f32 x0, y0;
  596.     f32 x1, y1;
  597.     fline() : x0(0.0f), y0(0.0f), x1(0.0f), y1(0.0f) {}
  598.     fline(f32 _x0, f32 _y0,
  599.           f32 _x1, f32 _y1) : x0(_x0), y0(_y0), x1(_x1), y1(_y1) {}
  600.     inline bool operator==(const fline& l){ return (x0==l.x0 && y0==l.y0 && x1==l.x1 && y1==l.y1); };
  601.     inline bool operator!=(const fline& l){ return (x0!=l.x0 && y0!=l.y0 && x1!=l.x1 && y1!=l.y1); };
  602.   };
  603.  
  604.   struct fcircle {
  605.     f32 x, y, r;
  606.     fcircle() : x(0.0f), y(0.0f), r(0.0f) {}
  607.     fcircle(f32 _x, f32 _y, f32 _r) : x(_x), y(_y), r(_r) {}
  608.     inline bool operator==(const fcircle& c){ return (x==c.x && y==c.y && r==c.r); };
  609.     inline bool operator!=(const fcircle& c){ return (x!=c.x && y!=c.y && r!=c.r); };
  610.     inline void operator-=(const fpoint& p){ x -= p.x;  y -= p.y; }
  611.     inline void operator+=(const fpoint& p){ x += p.x;  y += p.y; }
  612.   };
  613.  
  614.   struct fpoint3d {
  615.     f32 x, y, z;
  616.     fpoint3d() : x(0.0f), y(0.0f), z(0.0f) {}
  617.     fpoint3d(f32 _x, f32 _y, f32 _z) : x(_x), y(_y), z(_z) {}
  618.     inline bool operator==(const fpoint3d& p){ return (x==p.x && y==p.y && z==p.z); }
  619.     inline bool operator!=(const fpoint3d& p){ return (x!=p.x && y!=p.y && z!=p.z); }
  620.   };
  621.  
  622.  
  623.  
  624.   struct dpoint {
  625.     f64 x, y;
  626.     dpoint() : x(0.0), y(0.0) {}
  627.     dpoint(f64 _x, f64 _y) : x(_x), y(_y) {}
  628.     inline bool operator==(const dpoint& p){ return (x==p.x && y==p.y); };
  629.     inline bool operator!=(const dpoint& p){ return (x!=p.x && y!=p.y); };
  630.     inline void operator-=(const dpoint& p){ x -= p.x;  y -= p.y; }
  631.     inline void operator+=(const dpoint& p){ x += p.x;  y += p.y; }
  632.   };
  633.  
  634.   struct drect {
  635.     f64 x, y; //x & y position of rectangle's top-left corner
  636.     f64 w, h; //the rectangle's width & height
  637.     drect() : x(0.0), y(0.0), w(0.0), h(0.0) {}
  638.     drect(f64 _x, f64 _y) : x(_x), y(_y) {}
  639.     drect(f64 _x, f64 _y,
  640.           f64 _w, f64 _h) : x(_x), y(_y), w(_w), h(_h) {}
  641.     inline bool operator==(const drect& r){ return (x==r.x && y==r.y && w==r.w && h==r.h); };
  642.     inline bool operator!=(const drect& r){ return (x!=r.x && y!=r.y && w!=r.w && h!=r.h); };
  643.     inline void operator-=(const dpoint& p){ x -= p.x;  y -= p.y; }
  644.     inline void operator+=(const dpoint& p){ x += p.x;  y += p.y; }
  645.   };
  646.  
  647.   struct dline {
  648.     f64 x0, y0;
  649.     f64 x1, y1;
  650.     dline() : x0(0.0), y0(0.0), x1(0.0), y1(0.0) {}
  651.     dline(f64 _x0, f64 _y0,
  652.           f64 _x1, f64 _y1) : x0(_x0), y0(_y0), x1(_x1), y1(_y1) {}
  653.     inline bool operator==(const dline& l){ return (x0==l.x0 && y0==l.y0 && x1==l.x1 && y1==l.y1); };
  654.     inline bool operator!=(const dline& l){ return (x0!=l.x0 && y0!=l.y0 && x1!=l.x1 && y1!=l.y1); };
  655.   };
  656.  
  657.   struct dcircle {
  658.     f64 x, y;
  659.     f64 r;
  660.     dcircle() : x(0.0), y(0.0), r(0.0) {}
  661.     dcircle(f64 _x, f64 _y, f64 _r) : x(_x), y(_y), r(_r) {}
  662.     inline bool operator==(const dcircle& c){ return (x==c.x && y==c.y && r==c.r); };
  663.     inline bool operator!=(const dcircle& c){ return (x!=c.x && y!=c.y && r!=c.r); };
  664.     inline void operator-=(const dpoint& p){ x -= p.x;  y -= p.y; }
  665.     inline void operator+=(const dpoint& p){ x += p.x;  y += p.y; }
  666.   };
  667.  
  668.   struct dpoint3d {
  669.     f64 x, y, z;
  670.     dpoint3d() : x(0.0), y(0.0), z(0.0) {}
  671.     dpoint3d(f64 _x, f64 _y, f64 _z) : x(_x), y(_y), z(_z) {}
  672.     inline bool operator==(const dpoint3d& p){ return (x==p.x && y==p.y && z==p.z); }
  673.     inline bool operator!=(const dpoint3d& p){ return (x!=p.x && y!=p.y && z!=p.z); }
  674.   };
  675.  
  676. }; /* namespace shape */
  677.  
  678.  
  679.  
  680.  
  681.  
  682. //turns a byte into an integer that is printf'able with the format secifier "%08x"
  683. static inline u32 bin_hex(char n){
  684.     return (n&128)<<21|(n&64)<<18|(n&32)<<15|(n&16)<<12|(n&8)<<9|(n&4)<<6|(n&2)<<3|(n&1);
  685. }
  686.  
  687.  
  688.  
  689.  
  690.  
  691. }; /* namespace kit */
  692.  
  693. #endif /* _COMMONDEF_HPP */
  694. /******************************************************************************/
  695. /******************************************************************************/
  696. //"ksdl2\include\kit\misc.hpp":
  697. //THIS LIBRARY WAS MADE TO USE SDL2 VERSION 2.28.2
  698.  
  699. #ifndef _INC_MISC_HPP
  700. #define _INC_MISC_HPP
  701.  
  702. #include "commondef.hpp"
  703. #include "_misc_memory.hpp"
  704. #include "_misc_Mutex.hpp"
  705. #include "_misc_Thread.hpp"
  706. #include "_misc_func.hpp"
  707. #include "_misc_time.hpp"
  708. #include "_misc_Event.hpp"
  709. #include "_misc_EventWatch.hpp"
  710. #include "_misc_fileio.hpp"
  711.  
  712.  
  713. #endif /* _INC_MISC_HPP */
  714. /******************************************************************************/
  715. /******************************************************************************/
  716. //"ksdl2\include\kit\video.hpp":
  717. //THIS LIBRARY WAS MADE TO USE SDL2 VERSION 2.28.2
  718.  
  719. #ifndef _INC_VIDEO_HPP
  720. #define _INC_VIDEO_HPP
  721.  
  722. #include "commondef.hpp"
  723. #include "_video_Window.hpp"
  724. #include "_video_PixelFmt.hpp"
  725. #include "_video_Surface.hpp"
  726.  
  727.  
  728. #endif /* _INC_VIDEO_HPP */
  729. /******************************************************************************/
  730. /******************************************************************************/
  731. //"ksdl2\include\kit\_audio_AudioData.hpp":
  732. #ifndef _INC__AUDIO_AUDIODATA_HPP
  733. #define _INC__AUDIO_AUDIODATA_HPP
  734.  
  735. #include "commondef.hpp"
  736. #include "_audio_types.hpp"
  737.  
  738.  
  739. namespace kit {
  740.  
  741.  
  742.  
  743.  
  744.  
  745. #define KIT_MAGIC_KPCM (0x4D43506B) // = 'kPCM'
  746.  
  747. struct AudioDataHeader { //72B (0x48B)
  748.   u32       magic; // (0x00) = KIT_MAGIC_KPCM = 0x4D43506B = "kPCM"
  749.   u16      format; // (0x04) = one of AudioSampleFormatEnum if fmt_version == 1
  750.   u16  headerSize; // (0x06) = must be >=sizeof(AudioDataHeader)
  751.   u64    dataSize; // (0x08) = size of audio data, in bytes
  752.  
  753.   u64   loopStart; // (0x10) = which sample to loop back to
  754.   u64     loopEnd; // (0x18) = which sample to jump back to loopStart on
  755.  
  756.   u64  numSamples; // (0x20) = # of sample frames in audio data
  757.   u32  sampleRate; // (0x28) = the audio data's sample rate, in Hz
  758.   u32     bitRate; // (0x2C) = the audio's bit rate (per second)
  759.  
  760.   u16   loopCount; // (0x30) = # of times to loop audio (0 = no loop, 0xFFFF = inf loop)
  761.   u16    channels; // (0x32) = # of interlaced channels in the audio data
  762.   u8 bitRemainder; // (0x34) = <bits per sample> % 8
  763.   u8  fmt_version; // (0x35) = 0=kit_w32, 1=kit_sdl2
  764.   u16        mode; // (0x36) = 0 for normal PCM data
  765.   void*   samples; // (0x38) = the audio's sample data (appears as nullptr in file)
  766.   void*  userdata; // (0x40) = user-defined (also appears nullptr in file)
  767.                    // (0x48) = (start of sample data, assuming a .kpcm file)
  768. };
  769.  
  770.  
  771.  
  772.  
  773.  
  774. class AudioData { //24B
  775.   u32          _type;
  776.   bool        _valid = false;
  777.   bool _constructing = true;
  778.   u16     _padding16;
  779.  
  780.   void _allocate_hdr_unstructured(size_t )
  781.   void _allocate_hdr_structured(u16 format)
  782.  
  783. public:
  784.   //(allocated using memory::allocSIMD)
  785.   AudioDataHeader* const hdr;
  786.  
  787.   f32 volumeL = 1.0f;
  788.   f32 volumeR = 1.0f;
  789.  
  790.   //AudioData
  791.  
  792.  
  793. };
  794.  
  795.  
  796.  
  797.  
  798.  
  799. }; /* namespace kit */
  800.  
  801. #endif /* _INC__AUDIO_AUDIODATA_HPP */
  802. /******************************************************************************/
  803. /******************************************************************************/
  804. //"ksdl2\include\kit\_audio_AudioDevice.hpp":
  805. #ifndef _INC__AUDIO_AUDIODEVICE_HPP
  806. #define _INC__AUDIO_AUDIODEVICE_HPP
  807.  
  808. #include "commondef.hpp"
  809. #include "_audio_types.hpp"
  810.  
  811.  
  812. namespace kit {
  813.  
  814.  
  815.  
  816.  
  817.  
  818. //if disableFadeDelay == false, audio will be muted for 90ms
  819.  //before fading in over the course of 10ms (100ms in total).
  820.  //if disableFadeDelay == true, only the 10ms fade-in will occur
  821.  
  822. class AudioDevice { //64B
  823.   u32          _type;
  824.   bool        _valid = false;
  825.   bool _constructing = true;
  826.   u16     _padding16;
  827.   GenOpqPtr     _opq = nullptr;
  828.  
  829.  
  830.   //(device starts in a paused state!)
  831.   void _construct(const char* name,
  832.                   const AudioDeviceInfo* desired,
  833.                   bool disableFadeDelay = false,
  834.                   bool indexed = true);
  835.  
  836.  
  837. public:
  838.  
  839.   const AudioDeviceInfo info;
  840.  
  841.  
  842.   //(internally calls SDL_GetAudioDeviceName to get device's name)
  843.   AudioDevice(s32 index, //-1 to use default device
  844.               const AudioDeviceInfo* desired,
  845.               bool disableFadeDelay = false);
  846.  
  847.   AudioDevice(const char* name, //nullptr to use default device
  848.               const AudioDeviceInfo* desired,
  849.               bool disableFadeDelay = false)
  850.   { _construct(name, desired, disableFadeDelay, false); }
  851.  
  852.   //this will abruptly stop audio playback with no fade-out, so make sure
  853.    //to have the device be completely paused by the time this is called!
  854.    //(it's technically fine to do this w/o pausing, but it sounds terrible)
  855.   ~AudioDevice();
  856.  
  857.  
  858.   inline bool isValid()       { return _valid;        }
  859.   inline bool isConstructing(){ return _constructing; }
  860.   bool isPlaying(); //'is the user callback actually getting called?'
  861.   bool isActive(); //'is the underlying SDL device actually writing to DAC?'
  862.   //(^^returning true does not necessarily mean
  863.    //that the callback is actually being called!)
  864.  
  865.  
  866.   void setCallback(AudioCallback callback);
  867.   void setUserdata(void* userdata);
  868.   void setPlayback(bool playing);
  869.   void setPlaybackAndWait(bool playing); //false,true = wait 10ms, wait 100ms
  870.   inline void play() { setPlayback(true ); }
  871.   inline void pause(){ setPlayback(false); }
  872.   inline void playAndWait() { setPlaybackAndWait(true ); }
  873.   inline void pauseAndWait(){ setPlaybackAndWait(false); }
  874.  
  875.  
  876.   void lock(bool locked = true);
  877.   inline void unlock(){ lock(false); }
  878.  
  879. };
  880.  
  881.  
  882.  
  883.  
  884.  
  885. }; /* namespace kit */
  886.  
  887. #endif /* _INC__AUDIO_AUDIODEVICE_HPP */
  888. /******************************************************************************/
  889. /******************************************************************************/
  890. //"ksdl2\include\kit\_audio_AudioStream.hpp":
  891. #ifndef _INC__AUDIO_AUDIOSTREAM_HPP
  892. #define _INC__AUDIO_AUDIOSTREAM_HPP
  893.  
  894. #include "commondef.hpp"
  895. #include "_audio_types.hpp"
  896.  
  897.  
  898. namespace kit {
  899.  
  900.  
  901.  
  902.  
  903.  
  904. //converts an audio buffer to that of a different type,
  905.  //for example, mono s16 @ 44.1kHz  ->  stereo f32 @ 48kHz
  906. class AudioStream { //112B
  907.   u32          _type;
  908.   bool        _valid = false;
  909.   bool _constructing = true;
  910.   u16     _padding16;
  911.   GenOpqPtr     _opq = nullptr;
  912.  
  913.  
  914. public:
  915.  
  916.   //(only .sampleRate, .sampleFormat, and .numChannels are actually used here)
  917.   const AudioDeviceInfo src; //what will be converted to dst
  918.   const AudioDeviceInfo dst; //what to convert src to
  919.  
  920.  
  921.   AudioStream(const AudioDeviceInfo* src_p, const AudioDeviceInfo* dst_p);
  922.  
  923.   ~AudioStream();
  924.  
  925.  
  926.   inline bool isValid()       { return _valid; }
  927.   inline bool isConstructing(){ return _constructing; }
  928.  
  929.  
  930.   //returns # of converted bytes ready to be read from with a call to get()
  931.    //(if 0 is returned, you can call flush() to make available whatever is
  932.    // left in the buffer. though this might cause audio dropouts if you
  933.    // intend on making additional calls to put() to the stream!)
  934.   u32 getAvailableBytes();
  935.  
  936.  
  937.   //tell the stream that you're done sending data, and anything being
  938.    //buffered should be converted/resampled and made available immediately
  939.   void flush();
  940.  
  941.   //clear any pending data in the stream without converting it
  942.   void clear();
  943.  
  944.  
  945.   //read from converted data into buffer_dst
  946.    //returns number of bytes read from stream (to a max of buffer_size bytes)
  947.   u32 get(void* buffer_dst, u32 buffer_size);
  948.  
  949.   //write samples (from buffer_src) to stream to be converted
  950.    //(buffer_size is also in bytes here)
  951.   void put(void* buffer_src, u32 buffer_size);
  952.  
  953. };
  954.  
  955.  
  956.  
  957.  
  958.  
  959. }; /* namespace kit */
  960.  
  961. #endif /* _INC__AUDIO_AUDIOSTREAM_HPP */
  962. /******************************************************************************/
  963. /******************************************************************************/
  964. //"ksdl2\include\kit\_audio_types.hpp":
  965. #ifndef _INC__AUDIO_TYPES_HPP
  966. #define _INC__AUDIO_TYPES_HPP
  967.  
  968. #include "commondef.hpp"
  969.  
  970.  
  971. namespace kit {
  972.  
  973.  
  974.  
  975.  
  976.  
  977. #define KIT_AUDIO_BITSIZE(x)     (x & (0xFF ))
  978. #define KIT_AUDIO_ISFLOAT(x)     (x & (1<< 8))
  979. //#define KIT_AUDIO_ISBIGENDIAN(x) (x & (1<<12))
  980. #define KIT_AUDIO_ISSIGNED(x)    (x & (1<<15))
  981.  
  982. #define KIT_AUDIO_BYTESIZE(x)    (KIT_AUDIO_BITSIZE(x)/8)
  983.  
  984.  
  985.  
  986.  
  987.  
  988. //(big-endian sample formats are not supported currently)
  989.  
  990. enum AudioSampleFormatEnum {
  991.   SMPFMT_U8     = 0x0008,  //unsigned 8-bit samples
  992.   SMPFMT_S8     = 0x8008,  //  signed 8-bit samples
  993.   SMPFMT_U16LSB = 0x0010,  //unsigned 16-bit samples
  994.   SMPFMT_S16LSB = 0x8010,  //  signed 16-bit samples
  995. //SMPFMT_U16MSB = 0x1010,  //as above, but big-endian byte order
  996. //SMPFMT_S16MSB = 0x9010,  //as above, but big-endian byte order
  997.  
  998.   SMPFMT_S32LSB = 0x8020,  //  signed 32-bit samples
  999. //SMPFMT_S32MSB = 0x9020,  //As above, but big-endian byte order
  1000.  
  1001.   SMPFMT_F32LSB = 0x8120,  //32-bit floating point samples
  1002. //SMPFMT_F32MSB = 0x9120,  //As above, but big-endian byte order
  1003.  
  1004.   SMPFMT_U16    = SMPFMT_U16LSB,
  1005.   SMPFMT_S16    = SMPFMT_S16LSB,
  1006.   SMPFMT_S32    = SMPFMT_S32LSB,
  1007.   SMPFMT_F32    = SMPFMT_F32LSB,
  1008.  
  1009.   SMPFMT_UKNOWN = 0xFFFF,
  1010. };
  1011.  
  1012.  
  1013.  
  1014.  
  1015.  
  1016. struct AudioDeviceInfo; //forward declaration
  1017.  
  1018. //returns:
  1019.  //< 0: abort playback (pause without fade-out)
  1020.  //= 0: continue playing
  1021.  //> 0: pause playback (pause with fade-out)
  1022. //(also, _buffer should already be aligned and padded for use
  1023.  //with vector operations, like those in SSE and AVX!)
  1024. typedef s32 (*AudioCallback)(void* _buffer, const AudioDeviceInfo* info);
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030. /* (taken from the SDL2 wiki):
  1031.  
  1032. For multi-channel audio, the default SDL channel mapping is:
  1033.  
  1034. 2:  FL  FR                          (stereo)
  1035. 3:  FL  FR LFE                      (2.1 surround)
  1036. 4:  FL  FR  BL  BR                  (quad)
  1037. 5:  FL  FR LFE  BL  BR              (4.1 surround)
  1038. 6:  FL  FR  FC LFE  SL  SR          (5.1 surround - last two can also be BL BR)
  1039. 7:  FL  FR  FC LFE  BC  SL  SR      (6.1 surround)
  1040. 8:  FL  FR  FC LFE  BL  BR  SL  SR  (7.1 surround)
  1041.  
  1042. */
  1043.  
  1044. struct AudioDeviceInfo { //48B
  1045.   u64     timeStartTicks = 0; //timestamp (in ticks) at start of callback (read-only)
  1046.   u64        timeStartMS = 0; //timestamp (in ms) at start of callback (read-only)
  1047.   u32           deviceID = 0; //(read-only)
  1048.   u32         sampleRate = 0; //0 to use device's default
  1049.   u16       sampleFrames = 0; //0 to use device's default (should always be a power of 2)
  1050.   u16       sampleFormat = SMPFMT_F32; //samples are f32 by default
  1051.   u8     sampleFrameSize = 0; //size of a single sample frame, in bytes (read only)
  1052.   u8         numChannels = 0; //0 to use device's default
  1053.   bool           isInput = false; //is this a recording device? (rendering/output otherwise)
  1054.   bool        zeroBuffer = false; //'memset 0 the audio buffer before calling callback?' (output devs only)
  1055.   AudioCallback callback = nullptr;
  1056.   void*         userdata = nullptr; //optional, unlike callback
  1057. };
  1058.  
  1059.  
  1060.  
  1061.  
  1062.  
  1063. union Mono_smp {
  1064.   u8*   u8_ ;
  1065.   s8*   s8_ ;
  1066.   u16*  u16_;
  1067.   s16*  s16_;
  1068.   s32*  s32_;
  1069.   f32*  f32_;
  1070.  
  1071.   void* data;
  1072.  
  1073.   Mono_smp(void* _data) : data(_data) {}
  1074.  
  1075. };
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081. struct Stereo_u8  { u8  l, r; };
  1082. struct Stereo_s8  { s8  l, r; };
  1083. struct Stereo_u16 { u16 l, r; };
  1084. struct Stereo_s16 { s16 l, r; };
  1085. struct Stereo_s32 { s32 l, r; };
  1086. struct Stereo_f32 { f32 l, r; };
  1087.  
  1088. union Stereo_smp {
  1089.   Stereo_u8*  u8_ ;
  1090.   Stereo_s8*  s8_ ;
  1091.   Stereo_u16* u16_;
  1092.   Stereo_s16* s16_;
  1093.   Stereo_s32* s32_;
  1094.   Stereo_f32* f32_;
  1095.  
  1096.   void*       data;
  1097.  
  1098.   Stereo_smp(void* _data) : data(_data) {}
  1099.  
  1100. };
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106. }; /* namespace kit */
  1107.  
  1108. #endif /* _INC__AUDIO_TYPES_HPP */
  1109.  
Add Comment
Please, Sign In to add comment