Advertisement
Kitomas

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

Nov 22nd, 2024
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 29.84 KB | None | 0 0
  1. /******************************************************************************/
  2. /******************************************************************************/
  3. //"2024-11-21\include\kit\_misc_EventWatch.hpp":
  4. #ifndef _INC__MISC_EVENTWATCH_HPP
  5. #define _INC__MISC_EVENTWATCH_HPP
  6.  
  7. #include "commondef.hpp"
  8. #include "_misc_Event.hpp"
  9.  
  10.  
  11. namespace kit {
  12.  
  13.  
  14.  
  15.  
  16.  
  17. typedef void (*EventWatchCallback)(Event& event, void* userdata);
  18.  
  19.  
  20. typedef void (*EventWatchDestructorCallback)(void* userdata);
  21.  
  22.  
  23.  
  24.  
  25.  
  26. class EventWatch { //40B
  27.   u32                    _type;
  28.   bool                  _valid = false;
  29.   bool           _constructing = true;
  30.   bool               _disabled = false;
  31.   u8                 _padding8;
  32.   EventWatchCallback _callback = nullptr;
  33.   void*              _userdata = nullptr;
  34.   EventWatchDestructorCallback _onDestruction = nullptr;
  35.  
  36.  
  37. public:
  38.  
  39.   EventWatch(EventWatchCallback callback, void* userdata = nullptr,
  40.              EventWatchDestructorCallback onDestruction = nullptr);
  41.  
  42.   ~EventWatch(); //will call _onDestruction if != nullptr
  43.  
  44.  
  45.   inline void setState(bool enabled){ _disabled = !enabled; }
  46.  
  47. };
  48.  
  49.  
  50.  
  51.  
  52.  
  53. };
  54.  
  55. #endif /* _INC__MISC_EVENTWATCH_HPP */
  56. /******************************************************************************/
  57. /******************************************************************************/
  58. //"2024-11-21\include\kit\_misc_fileio.hpp":
  59. #ifndef _INC__MISC_FILEIO_HPP
  60. #define _INC__MISC_FILEIO_HPP
  61.  
  62. #include "commondef.hpp"
  63.  
  64.  
  65. namespace kit {
  66.  
  67.  
  68.  
  69.  
  70.  
  71. union BinaryData_magic {
  72.   u8   n8;
  73.   u16  n16;
  74.   u32  n32;
  75.   u64  n64;
  76.   char str[8];
  77.   //(str may not be null-terminated!)
  78. };
  79.  
  80.  
  81.  
  82. class BinaryData {
  83.   u32          _type;
  84.   bool        _valid = false;
  85.   bool _constructing = true;
  86.   u16     _padding16;
  87.   char*        _data = nullptr;
  88.   size_t  _data_size = 0;
  89.  
  90.  
  91. public:
  92.  
  93.   //tbd: make this less wacky
  94.   BinaryData_magic** const magic =
  95.     (BinaryData_magic**)&this->_data;
  96.  
  97.  
  98.   BinaryData(const char* filePath);
  99.  
  100.   //this will allocate dataSize bytes for _data, before copying data to _data
  101.    //(it is safe to pass nullptr to data, though that would make the class
  102.    // instance sorta equivalent to a garbage collected memory::alloc())
  103.   BinaryData(const void* data, size_t data_size);
  104.  
  105.   ~BinaryData();
  106.  
  107.  
  108.   inline bool isValid()       { return _valid;        }
  109.   inline bool isConstructing(){ return _constructing; }
  110.  
  111.   char*  getData(){ return _data;      }
  112.   size_t getSize(){ return _data_size; }
  113.  
  114. };
  115.  
  116.  
  117.  
  118.  
  119.  
  120. #define KIT_FILE_SEEK_SET 0
  121. #define KIT_FILE_SEEK_CUR 1
  122. #define KIT_FILE_SEEK_END 2
  123.  
  124.  
  125.  
  126. class File { //16B
  127.   u32          _type;
  128.   bool        _valid = false;
  129.   bool _constructing = true;
  130.   bool       _closed = false;
  131.   bool       _zombie = false;
  132.   GenOpqPtr    _file = nullptr;
  133.  
  134.  
  135. public:
  136.  
  137.   File(const char* filePath, const char* mode);
  138.  
  139.   ~File();
  140.  
  141.  
  142.   inline bool isValid()       { return _valid;        }
  143.   inline bool isConstructing(){ return _constructing; }
  144.   inline bool isClosed()      { return _closed;       }
  145.  
  146.  
  147.   //WARNING: make sure to call clearSysError() before starting any
  148.    //string of calls to read(), otherwise an error might be thrown
  149.    //if the thread's previously set error was equal to "Error"
  150.  
  151.   //returns number of elements read (or 0 if EOF)
  152.   size_t read(void* elements, size_t elementSize, size_t elements_len);
  153.   //returns number of bytes read (or 0 if EOF)
  154.   inline size_t read(void* data, size_t dataSize){ return read(data, 1, dataSize); }
  155.  
  156.   void write(const void* elements, size_t elementSize, size_t elements_len);
  157.   inline void write(const void* data, size_t dataSize){ write(data, 1, dataSize); }
  158.  
  159.   //seek to offset relative of whence (one of KIT_FILE_SEEK_<SET/CUR/END>)
  160.    //returns new absolute offset of data stream, in bytes
  161.   size_t seek(s64 offset, u32 whence);
  162.  
  163.   //returns current file offset
  164.   inline size_t tell(){ return seek(0, KIT_FILE_SEEK_CUR); }
  165.  
  166.   //closes file handle
  167.    //(file is considered invalid after this point!)
  168.   void close();
  169.  
  170.   //returns size of file, in bytes
  171.   size_t size();
  172.  
  173. };
  174.  
  175.  
  176.  
  177.  
  178.  
  179. namespace fileio {
  180.  
  181.  
  182.  
  183.  
  184.  
  185. bool exists(const char* filePath);
  186.  
  187.  
  188. size_t size(const char* filePath);
  189.  
  190.  
  191. void remove(const char* filePath);
  192.  
  193.  
  194.  
  195.  
  196.  
  197. //heap memory is allocated here,
  198.  //and should be freed with memory::free()
  199.  //(also, while *dataSize_p won't be populated with resulting
  200.  // filesize if dataSize_p is nullptr, it won't actually error)
  201. void* readAll(const char* filePath, size_t* dataSize_p);
  202.  
  203.  
  204. //writes to a binary file from a buffer
  205. void writeAll(const char* filePath, const void* data,
  206.               size_t dataSize, bool append = false);
  207.  
  208.  
  209.  
  210.  
  211.  
  212. }; /* namespace fileio */
  213.  
  214. }; /* namespace kit */
  215.  
  216. #endif /* _INC__MISC_FILEIO_HPP */
  217. /******************************************************************************/
  218. /******************************************************************************/
  219. //"2024-11-21\include\kit\_misc_func.hpp":
  220. #ifndef _INC__MISC_FUNC_HPP
  221. #define _INC__MISC_FUNC_HPP
  222.  
  223. #include "commondef.hpp"
  224. #include <stdarg.h> //for vsnPrintf
  225.  
  226.  
  227. namespace kit {
  228.  
  229.  
  230.  
  231.  
  232.  
  233. enum InitFlagsEnum {
  234.   KINIT_TIMER          = 0x01,
  235.   KINIT_AUDIO          = 0x02,
  236.   KINIT_VIDEO          = 0x04,
  237.   KINIT_JOYSTICK       = 0x08,
  238.   KINIT_GAMECONTROLLER = 0x10,
  239.   KINIT_EVENTS         = 0x20,
  240.  
  241.   KINIT_EVERYTHING     = 0x3F,
  242.  
  243. };
  244.  
  245.  
  246.  
  247. //these should only be called from the main thread, only after
  248.  //guaranteeing that no other threads are currently running
  249. void initSubsystems(u32 flags = KINIT_EVERYTHING);
  250. void quitSubsystems(u32 flags = KINIT_EVERYTHING);
  251. //(quitting with KINIT_EVERYTHING is safe even if
  252.  //there are no currently active subsystems)
  253.  
  254.  
  255.  
  256.  
  257.  
  258. enum LogPriorityEnum {
  259.   LOG_PRIORITY_VERBOSE = 1,
  260.   LOG_PRIORITY_DEBUG,
  261.   LOG_PRIORITY_INFO,
  262.   LOG_PRIORITY_WARN,
  263.   LOG_PRIORITY_ERROR,
  264.   LOG_PRIORITY_CRITICAL,
  265. };
  266.  
  267.  
  268.  
  269. //kit::Log(), except it suppresses some format string stuff
  270. //this macro is especially useful if you're using %llu, (long long unsigned)
  271.  //and gcc would otherwise complain about an unknown format specifier
  272. //(at least, that's what happened when i used plain ol' SDL_Log,
  273.  //even though it prints 18446744073709551615 with %llu just fine)
  274. #define kit_LogS(...) \
  275.   _Pragma("GCC diagnostic push") \
  276.   _Pragma("GCC diagnostic ignored \"-Wformat=\"") \
  277.   _Pragma("GCC diagnostic push") \
  278.   _Pragma("GCC diagnostic ignored \"-Wformat-extra-args\"") \
  279.   kit::Log(__VA_ARGS__); \
  280.   _Pragma("GCC diagnostic pop") \
  281.   _Pragma("GCC diagnostic pop")
  282.  
  283. #define kit_LogVerbose(...)  kit::Log(LOG_PRIORITY_VERBOSE , __VA_ARGS__)
  284. #define kit_LogDebug(...)    kit::Log(LOG_PRIORITY_DEBUG   , __VA_ARGS__)
  285. #define kit_LogInfo(...)     kit::Log(LOG_PRIORITY_INFO    , __VA_ARGS__)
  286. #define kit_LogWarn(...)     kit::Log(LOG_PRIORITY_WARN    , __VA_ARGS__)
  287. #define kit_LogError(...)    kit::Log(LOG_PRIORITY_ERROR   , __VA_ARGS__)
  288. #define kit_LogCritical(...) kit::Log(LOG_PRIORITY_CRITICAL, __VA_ARGS__)
  289.  
  290. #define kit_LogVerboseS(...)  kit_LogS(LOG_PRIORITY_VERBOSE , __VA_ARGS__)
  291. #define kit_LogDebugS(...)    kit_LogS(LOG_PRIORITY_DEBUG   , __VA_ARGS__)
  292. #define kit_LogInfoS(...)     kit_LogS(LOG_PRIORITY_INFO    , __VA_ARGS__)
  293. #define kit_LogWarnS(...)     kit_LogS(LOG_PRIORITY_WARN    , __VA_ARGS__)
  294. #define kit_LogErrorS(...)    kit_LogS(LOG_PRIORITY_ERROR   , __VA_ARGS__)
  295. #define kit_LogCriticalS(...) kit_LogS(LOG_PRIORITY_CRITICAL, __VA_ARGS__)
  296.  
  297. //basically SDL_LogMessage, except with the category always set to application
  298. void Log(LogPriorityEnum priority, const char* fmt, ...);
  299.  
  300.  
  301.  
  302.  
  303.  
  304. //len_max does not include null-terminator!
  305. //if len_max == 0, call is analogous to str(not n)len
  306. size_t strnLen(const char* str, size_t len_max = 0);
  307.  
  308.  
  309.  
  310. //(mostly) yoinked from stackoverflow :D
  311. //len_max does not include null-terminator!
  312. //if len_max == 0, call is analogous to str(not n)cmp
  313. s32 strnCmp(const char* str_a, const char* str_b, size_t len_max = 0);
  314.  
  315.  
  316.  
  317. //null-terminator is added at the end of the copy destination!
  318. //if len_max == 0, call is analogous to str(not n)cpy
  319. char* strnCpy(char* str_dst, const char* str_src, size_t len_max);
  320.  
  321.  
  322.  
  323.  
  324.  
  325. /* taken from stb_sprintf.h (what these 2 functions use under-the-hood):
  326.  
  327. 64-BIT INTS:
  328. ============
  329. This library also supports 64-bit integers and you can use MSVC style or
  330. GCC style indicators (%I64d or %lld).  It supports the C99 specifiers
  331. for size_t and ptr_diff_t (%jd %zd) as well.
  332.  
  333. EXTRAS:
  334. =======
  335. Like some GCCs, for integers and floats, you can use a ' (single quote)
  336. specifier and commas will be inserted on the thousands: "%'d" on 12345
  337. would print 12,345.
  338.  
  339. For integers and floats, you can use a "$" specifier and the number
  340. will be converted to float and then divided to get kilo, mega, giga or
  341. tera and then printed, so "%$d" 1000 is "1.0 k", "%$.2d" 2536000 is
  342. "2.53 M", etc. For byte values, use two $:s, like "%$$d" to turn
  343. 2536000 to "2.42 Mi". If you prefer JEDEC suffixes to SI ones, use three
  344. $:s: "%$$$d" -> "2.42 M". To remove the space between the number and the
  345. suffix, add "_" specifier: "%_$d" -> "2.53M".
  346.  
  347. In addition to octal and hexadecimal conversions, you can print
  348. integers in binary: "%b" for 256 would print 100.
  349.  
  350. PERFORMANCE vs MSVC 2008 32-/64-bit (GCC is even slower than MSVC):
  351. ===================================================================
  352. "%d" across all 32-bit ints (4.8x/4.0x faster than 32-/64-bit MSVC)
  353. "%24d" across all 32-bit ints (4.5x/4.2x faster)
  354. "%x" across all 32-bit ints (4.5x/3.8x faster)
  355. "%08x" across all 32-bit ints (4.3x/3.8x faster)
  356. "%f" across e-10 to e+10 floats (7.3x/6.0x faster)
  357. "%e" across e-10 to e+10 floats (8.1x/6.0x faster)
  358. "%g" across e-10 to e+10 floats (10.0x/7.1x faster)
  359. "%f" for values near e-300 (7.9x/6.5x faster)
  360. "%f" for values near e+300 (10.0x/9.1x faster)
  361. "%e" for values near e-300 (10.1x/7.0x faster)
  362. "%e" for values near e+300 (9.2x/6.0x faster)
  363. "%.320f" for values near e-300 (12.6x/11.2x faster)
  364. "%a" for random values (8.6x/4.3x faster)
  365. "%I64d" for 64-bits with 32-bit values (4.8x/3.4x faster)
  366. "%I64d" for 64-bits > 32-bit values (4.9x/5.5x faster)
  367. "%s%s%s" for 64 char strings (7.1x/7.3x faster)
  368. "...512 char string..." ( 35.0x/32.5x faster!)
  369.  
  370. */
  371.  
  372. //if len_max == 0 OR > KIT_S32_MAX, call is analogous to s(not n)printf
  373. s32 snPrintf(char* str_dst, size_t len_max, const char* str_fmt, ...);
  374.  
  375. //^^ VV unlike normal snprintf, these'll always return a null-terminated string
  376.  
  377. //if len_max == 0 OR > KIT_S32_MAX, call is analogous to vs(not n)printf
  378. s32 vsnPrintf(char* str_dst, size_t len_max, const char* str_fmt, va_list va);
  379.  
  380.  
  381.  
  382.  
  383.  
  384. const char* getLastSysError();
  385. void clearSysError();
  386.  
  387.  
  388.  
  389.  
  390.  
  391. //frees any error strings managed by kit_sdl2 on the calling thread
  392. //this must be called whenever a const char* exception is caught,
  393.  //only after that Error has been handled by the user.
  394.  //(as in, the pointer should be considered invalid after exiting this function!)
  395. //since this will work even if there is no kit_sdl2-managed error message,
  396.  //it is best practice to call this even if you're mostly sure that
  397.  //the const char* was thrown by the user!
  398. //also, a call of quitSubsystem(KINIT_EVERYTHING)
  399.  //will free all thread errors automatically
  400. //also also, mixups might occur if another thread error is pushed before
  401.  //a call to freeThreadErrors() has the chance to free the previous one(s)
  402.  //on the same thread, resulting in the wrong reference (not an invalid
  403.  //error string, just not the right one) being accessed
  404. void freeThreadErrors();
  405. //TL;DR: best practice is to call this during any catch(const char*) exceptions,
  406.  //after you're done handling the thrown string!
  407.  
  408.  
  409.  
  410.  
  411.  
  412. enum CPUCapabilityFlagsEnum {
  413.   //(always false on CPUs that aren't using Intel instruction sets)
  414.   CPU_HAS_RDTSC   = 0x0001, //'does CPU have RDTSC instruction?'
  415.  
  416.   //(always false on CPUs that aren't using PowerPC instruction sets)
  417.   CPU_HAS_ALTIVEC = 0x0002, //'does CPU have AltiVec features?'
  418.  
  419.   //(always false on CPUs that aren't using Intel instruction sets)
  420.   CPU_HAS_MMX     = 0x0004, //'does CPU have MMX features?'
  421.  
  422.   //(always false on CPUs that aren't using AMD instruction sets)
  423.   CPU_HAS_3DNOW   = 0x0008, //'does CPU have 3DNow! features?'
  424.  
  425.   //(always false on CPUs that aren't using Intel instruction sets)
  426.   CPU_HAS_SSE     = 0x0010, //'does CPU have SSE features?'
  427.  
  428.   //(always false on CPUs that aren't using Intel instruction sets)
  429.   CPU_HAS_SSE2    = 0x0020, //'does CPU have SSE2 features?'
  430.  
  431.   //(always false on CPUs that aren't using Intel instruction sets)
  432.   CPU_HAS_SSE3    = 0x0040, //'does CPU have SSE3 features?'
  433.  
  434.   //(always false on CPUs that aren't using Intel instruction sets)
  435.   CPU_HAS_SSE41   = 0x0080, //'does CPU have SSE4.1 features?'
  436.  
  437.   //(always false on CPUs that aren't using Intel instruction sets)
  438.   CPU_HAS_SSE42   = 0x0100, //'does CPU have SSE4.2 features?'
  439.  
  440.   //(always false on CPUs that aren't using Intel instruction sets)
  441.   CPU_HAS_AVX     = 0x0200, //'does CPU have AVX features?'
  442.  
  443.   //(always false on CPUs that aren't using Intel instruction sets)
  444.   CPU_HAS_AVX2    = 0x0400, //'does CPU have AVX2 features?'
  445.  
  446.   //(always false on CPUs that aren't using Intel instruction sets)
  447.   CPU_HAS_AVX512F = 0x0800, //'does CPU have AVX-512F (foundation) features?'
  448.  
  449.   //(always false on CPUs that aren't using ARM instruction sets)
  450.   CPU_HAS_ARMSIMD = 0x1000, //'does CPU have ARM SIMD (ARMv6) features?'
  451.  
  452.   //(always false on CPUs that aren't using ARM instruction sets)
  453.   CPU_HAS_NEON    = 0x2000, //'does CPU have NEON (ARM SIMD) features?'
  454.  
  455.   //(always false on CPUs that aren't using LOONGARCH instruction sets)
  456.   CPU_HAS_LSX     = 0x4000, //'does CPU have LSX (LOONGARCH SIMD) features?'
  457.  
  458.   //(always false on CPUs that aren't using LOONGARCH instruction sets)
  459.   CPU_HAS_LASX    = 0x8000, //'does CPU have LASX (LOONGARCH SIMD) features?'
  460. };
  461.  
  462.  
  463.  
  464. u32 getCPUCapabilities(); //returns a combination of CPUCapabilityFlagsEnum flags
  465.  
  466. u32 getCPUCacheLineSize(); //in bytes (L1 cache specifically)
  467. u32 getNumLogicalCPUCores(); //(might be > core count if hyperthreading is enabled)
  468.  
  469.  
  470.  
  471.  
  472.  
  473. enum MessageBoxFlagsEnum {
  474.   MSGBOX_ERROR    = 0x0010,
  475.   MSGBOX_WARNING  = 0x0020,
  476.   MSGBOX_INFO     = 0x0040,
  477.   MSGBOX_BTNS_L2R = 0x0080, //buttons placed left-to-right (showMsgBoxEx only)
  478.   MSGBOX_BTNS_R2L = 0x0100, //buttons placed right-to-left (showMsgBoxEx only)
  479. };
  480.  
  481.  
  482.  
  483. class Window; //forward declaration
  484. void showMsgBox(const char* text = nullptr, const char* title = nullptr,
  485.                 u32 flags = MSGBOX_INFO, Window* win = nullptr);
  486.  
  487.  
  488. //showMsgBoxEx is TBD (i don't want to deal with SDL's message boxes right now)
  489.  
  490.  
  491.  
  492.  
  493.  
  494. union Event; //forward declaration
  495. //returns false if there were no events in queue
  496.  //(if event_p is left as nullptr, the next event will not be dequeued)
  497.  //(also, only call this function in the thread that set the video mode,
  498.  // which is usually the main thread!)
  499. bool pollEvent(Event* event_p = nullptr); //requires events subsystem
  500. const char* getEventText(u32 type);
  501.  
  502.  
  503.  
  504.  
  505.  
  506. //(clipboard text uses utf-8 encoding)
  507. bool  clipboardHasText();
  508. char* clipboardGetText(); //(allocates memory; returned ptr must be memory::free'd)
  509. void  clipboardSetText(const char* txt);
  510.  
  511.  
  512.  
  513.  
  514.  
  515. bool showCursor(s32 toggle); //boolean, or -1 to query state without changing
  516.  
  517.  
  518.  
  519.  
  520.  
  521. void warpMouseGlobal(s32 x, s32 y);
  522.  
  523.  
  524.  
  525.  
  526.  
  527. bool getRelativeMouseMode();
  528. void setRelativeMouseMode(bool enable);
  529.  
  530.  
  531.  
  532.  
  533.  
  534. }; /* namespace kit */
  535.  
  536. #endif /* _INC__MISC_FUNC_HPP */
  537. /******************************************************************************/
  538. /******************************************************************************/
  539. //"2024-11-21\include\kit\_misc_memory.hpp":
  540. #ifndef _INC__MISC_MEMORY_HPP
  541. #define _INC__MISC_MEMORY_HPP
  542.  
  543. #include "commondef.hpp"
  544.  
  545.  
  546. namespace kit {
  547.  
  548.  
  549. namespace memory {
  550.  
  551.  
  552.  
  553.  
  554.  
  555. void* alloc(size_t size);
  556.  
  557. //this version of free is especially useful, because it's safe to pass nullptr to!
  558.  //this rule goes for both ptr_p and *ptr_p
  559. void free(void* ptr_p);
  560.  
  561. void* realloc(void* ptr_p, size_t newSize);
  562.  
  563. //^^(for both free and realloc, a void** declared in function as void*,
  564.  //  so you don't need to explicitly cast it to void**)
  565.  
  566.  
  567.  
  568. //same as alloc and free, now with exception throwing!
  569. void* alloc2(size_t size);
  570.  
  571. void free2(void* ptr_p); //(unlike free, this will throw if nullptr is given)
  572.  
  573. void* realloc2(void* ptr_p, size_t newSize);
  574.  
  575.  
  576.  
  577. size_t getNumAllocations();
  578.  
  579.  
  580.  
  581.  
  582.  
  583. void* set(void* ptr, s32 value, size_t size);
  584.  
  585. //throws exceptions, unlike set
  586. void* set2(void* ptr, s32 value, size_t size);
  587.  
  588.  
  589.  
  590.  
  591.  
  592. void* copy(void* destination, const void* source, size_t size);
  593.  
  594. //throws exceptions, unlike copy
  595. void* copy2(void* destination, const void* source, size_t size);
  596.  
  597.  
  598.  
  599.  
  600.  
  601. u32 getSystemRAM_MiB(); //returns amount of RAM configured in the system, in MiB
  602.  
  603. u32 getSIMDAlignment(); //returns required alignment for SIMD instructions, in bytes
  604.  
  605.  
  606.  
  607.  
  608.  
  609. //xSIMD are basically the same as their non-SIMD counterparts,
  610.  //except these are aligned and padded for use with SIMD instructions.
  611. //by padded, i mean that it's safe to read/write an incomplete
  612.  //vector at the end of a memory block
  613.  
  614. //(allocSIMD pointers must be freed using freeSIMD,
  615.  //not free, and vice-versa)
  616.  
  617. void* allocSIMD(size_t size);
  618.  
  619. void freeSIMD(void* ptr_p);
  620.  
  621. void* reallocSIMD(void* ptr_p, size_t newSize);
  622.  
  623.  
  624.  
  625. void* allocSIMD2(size_t size);
  626.  
  627. void freeSIMD2(void* ptr_p);
  628.  
  629. void* reallocSIMD2(void* ptr_p, size_t newSize);
  630.  
  631.  
  632.  
  633.  
  634.  
  635. struct Wrapper {
  636.   void* ptr = nullptr;
  637.  
  638.   Wrapper(size_t size); //new memory::alloc
  639.   Wrapper(void* _ptr) : ptr(_ptr) {} //existing memory::alloc
  640.   ~Wrapper(){ memory::free(&ptr); };
  641.  
  642.   //capital A in the middle to avoid name conflict with actual memory::realloc
  643.   inline void* reAlloc(size_t newSize){ return memory::realloc(&ptr, newSize); }
  644.  
  645. };
  646.  
  647.  
  648.  
  649. struct WrapperSIMD {
  650.   void* ptr = nullptr;
  651.  
  652.   WrapperSIMD(size_t size); //new memory::allocSIMD
  653.   WrapperSIMD(void* _ptr) : ptr(_ptr) {} //existing memory::allocSIMD
  654.   ~WrapperSIMD(){ memory::freeSIMD(&ptr); };
  655.  
  656.   //capital A in the middle to remain consistent with Wrapper::reAlloc()
  657.   inline void* reAlloc(size_t newSize){ return memory::reallocSIMD(&ptr, newSize); }
  658.  
  659. };
  660.  
  661.  
  662.  
  663.  
  664.  
  665. }; /* namespace memory */
  666.  
  667.  
  668. }; /* namespace kit */
  669.  
  670. #endif /* _INC__MISC_MEMORY_HPP */
  671. /******************************************************************************/
  672. /******************************************************************************/
  673. //"2024-11-21\include\kit\_misc_Mutex.hpp":
  674. #ifndef _INC__MISC_MUTEX_HPP
  675. #define _INC__MISC_MUTEX_HPP
  676.  
  677. #include "commondef.hpp"
  678.  
  679.  
  680. namespace kit {
  681.  
  682.  
  683.  
  684.  
  685.  
  686. //this mutex is non-blocking within the same thread!
  687.  //(that means you can lock multiple times in one thread
  688.  // as long as you unlock it the same number of times)
  689. class Mutex { //16B; does not require an active subsystem
  690.   u32          _type;
  691.   bool        _valid = false;
  692.   bool _constructing = true;
  693.   u16     _padding16;
  694.   GenOpqPtr _mutex_p = nullptr;
  695.  
  696.  
  697. public:
  698.  
  699.   Mutex();
  700.  
  701.   ~Mutex();
  702.  
  703.  
  704.   inline bool isValid()       { return _valid;        }
  705.   inline bool isConstructing(){ return _constructing; }
  706.  
  707.  
  708.   void lock(bool locked = true);
  709.   inline void unlock(){ lock(false); }
  710.   bool tryLock(); //returns true if locked successfully
  711.  
  712. };
  713.  
  714.  
  715.  
  716.  
  717.  
  718. }; /* namespace kit */
  719.  
  720. #endif /* _INC__MISC_MUTEX_HPP */
  721. /******************************************************************************/
  722. /******************************************************************************/
  723. //"2024-11-21\include\kit\_misc_Thread.hpp":
  724. #ifndef _INC__MISC_THREAD_HPP
  725. #define _INC__MISC_THREAD_HPP
  726.  
  727. #include "commondef.hpp"
  728.  
  729.  
  730. namespace kit {
  731.  
  732.  
  733.  
  734.  
  735.  
  736. enum ThreadPriorityEnum {
  737.   THREAD_LOW     = -1,
  738.   THREAD_NORMAL  =  0,
  739.   THREAD_HIGH    =  1,
  740.   THREAD_HIGHEST =  2, //for time critical tasks
  741. };
  742.  
  743.  
  744.  
  745.  
  746.  
  747. //if a thread function throws an unhandled exception,
  748.  //it will be outputted to stdout
  749. typedef s32 (*ThreadFunction)(void* userdata);
  750.  
  751.  
  752.  
  753.  
  754.  
  755. //returns whether or not priority of current thread was successfully set
  756.  //may fail if setting priority requires elevated privileges (or at least when
  757.  //setting a higher priority), if it's even allowed at all
  758. bool Thread_setPriority(s32 priority, bool throwOnFailure = false);
  759.  
  760.  
  761.  
  762.  
  763.  
  764. class Thread { //16B; does not require an active subsystem
  765.   u32              _type;
  766.   bool            _valid = false;
  767.   bool     _constructing = true;
  768.   bool _waitInDestructor = false; //otherwise thread auto-detaches
  769.   bool         _detached = false;
  770.   GenOpqPtr      _thread = nullptr;
  771.   //(real opaque is separate from Thread object and is very temporary)
  772.  
  773.  
  774. public:
  775.  
  776.   //if waitInDestructor is false, the Thread object will not
  777.    //wait for func to return inside the Thread's destructor
  778.    //(stackSize = 0 to use whatever default stack size is)
  779.   Thread(ThreadFunction func, void* userdata = nullptr,
  780.          bool waitInDestructor = false, size_t stackSize = 0,
  781.          const char* threadName = nullptr);
  782.          //(threadName should be UTF-8 string if not nullptr)
  783.  
  784.   ~Thread();
  785.  
  786.  
  787.   inline bool isValid()       { return _valid;        }
  788.   inline bool isConstructing(){ return _constructing; }
  789.   inline bool isDetached()    { return _detached;     }
  790.  
  791.   u32 getID();
  792.   //returns UTF-8 string, or nullptr if thread doesn't have a name
  793.   const char* getName();
  794.  
  795.  
  796.   //returns result of func (if detached, returns 0 immediately)
  797.    //(afaik, SDL_WaitThread doesn't time out, so be careful!)
  798.   s32 waitUntilDone();
  799.  
  800.  
  801.   //make thread automatically clean up after returning
  802.    //(otherwise, you'll need to call waitUntilDone() to do that)
  803.   void detach();
  804.  
  805. };
  806.  
  807.  
  808.  
  809.  
  810.  
  811. }; /* namespace kit */
  812.  
  813. #endif /* _INC__MISC_THREAD_HPP */
  814. /******************************************************************************/
  815. /******************************************************************************/
  816. //"2024-11-21\include\kit\_misc_time.hpp":
  817. #ifndef _INC__MISC_TIME_HPP
  818. #define _INC__MISC_TIME_HPP
  819.  
  820. #include "commondef.hpp"
  821.  
  822.  
  823. namespace kit {
  824.  
  825.  
  826.  
  827.  
  828.  
  829. /* from the SDL2 wiki: "
  830. The callback function is passed the current timer interval
  831. and returns the next timer interval. If the returned value
  832. is the same as the one passed in, the periodic alarm continues,
  833. otherwise a new alarm is scheduled. If the callback returns 0,
  834. the periodic alarm is cancelled. "*/
  835. typedef u32 (*timerCallback)(u32 interval_ms, void* userdata);
  836.  
  837. typedef s32 timerID;
  838.  
  839.  
  840.  
  841. //(these 2 functions require the timer subsystem)
  842.  
  843. timerID timerAdd(timerCallback func, u32 interval_ms, void* userdata = nullptr);
  844.  
  845. void timerRemove(timerID id);
  846.  
  847.  
  848.  
  849.  
  850.  
  851. namespace time {
  852.  
  853.  
  854.  
  855.  
  856.  
  857. //returns current tick count, and how
  858.  //many ticks are in a second, respectively
  859. u64 getTicks();
  860. u64 getTicksPerSecond();
  861.  
  862.  
  863. //returns # of milliseconds since init
  864. u64 getMS();
  865. u32 getMS_32();
  866.  
  867.  
  868. //returns # of seconds since init
  869.  //(uses integer milliseconds internally;
  870.  // do "(f64)getTicks()/getTicksPerSecond()"
  871.  // if more accuracy is needed)
  872. f64 getSeconds();
  873.  
  874.  
  875.  
  876.  
  877.  
  878. //will wait AT LEAST the given number
  879.  //of milliseconds (possibly longer)
  880. void sleep(u32 milliseconds);
  881.  
  882.  
  883.  
  884.  
  885.  
  886. }; /* namespace time */
  887.  
  888. }; /* namespace kit */
  889.  
  890. #endif /* _INC__MISC_TIME_HPP */
  891. /******************************************************************************/
  892. /******************************************************************************/
  893. //"2024-11-21\include\kit\_video_BFont_Surface.hpp":
  894. #ifndef _INC__VIDEO_BFONT_SURFACE_HPP
  895. #define _INC__VIDEO_BFONT_SURFACE_HPP
  896.  
  897. #include "commondef.hpp"
  898. #include "_video_Surface.hpp"
  899.  
  900.  
  901. namespace kit {
  902.  
  903.  
  904.  
  905.  
  906.  
  907. //set x and/or y to this to center the text on that axis
  908. #ifndef KIT_CENTER_TEXT
  909. #define KIT_CENTER_TEXT (-2147483647) //aka 0x80000001
  910. #endif
  911.  
  912.  
  913.  
  914. #ifndef KIT_DEFAULT_TEXT_COLOR
  915. #define KIT_DEFAULT_TEXT_COLOR 0xFFFFFF //0xBBGGRR
  916. #endif
  917.  
  918.  
  919.  
  920.  
  921.  
  922. class Window;
  923.  
  924. //class for software rendering monospaced, 256-char, bitmap fonts
  925.  
  926. class BFont_Surface { //80B
  927.   u32               _type;
  928.   bool             _valid = false;
  929.   bool      _constructing = true;
  930.  
  931. public:
  932.   //'add 1 pixel of spacing on bottom-right side?' (affected by scale)
  933.   bool         box_padded = false; //what the default font uses
  934.   //if false, negative positions are treated normally, without using the
  935.    //bottom-right side as the text's (or text box's) origin
  936.   //(setting to false will by extension ignore KIT_CENTER_TEXT)
  937.   bool     neg_pos_margin = true;
  938.  
  939. private:
  940.   GenOpqPtr     _surf_src = nullptr;
  941.   GenOpqPtr     _surf_dst = nullptr; //a reference; swapped every draw call
  942.   u8*          _fmtBuffer = nullptr;
  943.   shape::point _glyphSize = {8, 8}; //8x8 is the size of the default font
  944.   shape::rect _lastBounds = {0, 0,  0, 0};
  945.   u31      _fmtBuffer_len = 4096;
  946.   colors::ABGR    _txt_fg = KIT_DEFAULT_TEXT_COLOR; //for the text itself; 0xBBGGRR
  947.  
  948.   void _constructor(GenOpqPtr surf_opq, colors::ABGR txt_fg, u31 fmtBuffer_len,
  949.                     const char* funcName = nullptr);
  950.  
  951.   shape::point _getTextSize(bool bordered);
  952.   shape::rect  _getRect(s32 x, s32 y, bool bordered);
  953.  
  954.   shape::rect  _draw(s32 x, s32 y, u31 maxLen);
  955.   shape::point _drawBoxNoText(shape::rect dst_rect); //(positions are absolute)
  956.   shape::rect  _drawBox(s32 x, s32 y, u31 maxLen);
  957.  
  958. public:
  959.   //color of the text box's background; 0xBBGGRR
  960.   colors::ABGR     txt_bg = 0x000000; //(alpha component is ignored)
  961.   //color of the text box's border; 0xBBGGRR
  962.   colors::ABGR     txt_bd = 0xC0C0C0; //(alpha component is ignored)
  963.   //the multiplier for text and box scaling
  964.   shape::fpoint     scale = {1.0f, 1.0f};
  965.  
  966.  
  967.  
  968.   //(WHEN CONSTRUCTING, USE BLACK (0x00000000) TO INDICATE
  969.    //TRANSPARENCY, AND NONZERO PIXELS TO INDICATE OPAQUENESS!
  970.    //ACTUAL TEXT COLOR IS DETERMINED BY "txt_fg" EVERY DRAW CALL!)
  971.  
  972.  
  973.  
  974.   //if img_filePath is nullptr, the default built-in
  975.    //font will be used (8x8, public domain :D)
  976.    //(img_callback is ignored if img_filePath is nullptr)
  977.   BFont_Surface(const char* img_filePath = nullptr,
  978.                 SurfaceLoaderCallback img_callback = nullptr,
  979.                 colors::ABGR txt_fg = KIT_DEFAULT_TEXT_COLOR,
  980.                 u31 fmtBuffer_len = 4096);
  981.  
  982.   //copies the surface and all of its pixel data
  983.   inline BFont_Surface(const Surface& surf,
  984.                        colors::ABGR txt_fg = KIT_DEFAULT_TEXT_COLOR,
  985.                        u31 fmtBuffer_len = 4096)
  986.   { _constructor((GenOpqPtr)&surf, txt_fg, fmtBuffer_len); }
  987.  
  988.   ~BFont_Surface();
  989.  
  990.  
  991.  
  992.   inline u8* getFmtBuffer(){ return _fmtBuffer; }
  993.  
  994.   inline u32 getFmtBufferLen(){ return _fmtBuffer_len.n; }
  995.  
  996.   inline shape::rect getLastBounds(){ return _lastBounds; }
  997.  
  998.   inline colors::ABGR getGlyphColor(){ return _txt_fg; }
  999.  
  1000.   inline shape::point getGlyphSize(){ return _glyphSize; }
  1001.  
  1002.   inline shape::point getGlyphSizeScaled(){
  1003.     if(scale.x != 1.0f  ||  scale.y != 1.0f){
  1004.       return {MAX((s32)(_glyphSize.x*scale.x), 1),
  1005.               MAX((s32)(_glyphSize.y*scale.y), 1)};
  1006.     } else {
  1007.       return _glyphSize;
  1008.     }
  1009.   }
  1010.  
  1011.   shape::point getTextSize(const char* fmt, bool bordered = false, ...);
  1012.  
  1013.   shape::rect  getRect(Surface& dst_surf, s32 x, s32 y, const char* fmt,
  1014.                        bool bordered = false, ...);
  1015.  
  1016.   shape::rect  getRect(Window& dst_surf, s32 x, s32 y, const char* fmt,
  1017.                        bool bordered = false, ...);
  1018.  
  1019.   //(will calculate the rect of whatever is currently in _fmtBuffer)
  1020.   shape::rect  getRect(Surface& dst_surf, s32 x, s32 y, bool bordered = false);
  1021.  
  1022.   shape::rect  getRect(Window& dst_surf, s32 x, s32 y, bool bordered = false);
  1023.  
  1024.  
  1025.  
  1026.   //aside from KIT_CENTER_TEXT, there's another non-absolute positioning option,
  1027.    //as negative x/y positions will change the text's origin point from the
  1028.    //top-left to the bottom-right (unless neg_pos_margin = false), like so:
  1029.    //'surface_width-text_width+x+1', and 'surface_height-text_height+y+1'
  1030.    //(+x & +y, as in adding by a negative)
  1031.  
  1032.   //if !maxLen, the entire resulting string will be drawn
  1033.  
  1034.   //returns the resulting string's calculated bounding box, in pixels
  1035.  
  1036.   shape::rect draw(Surface& dst_surf, s32 x, s32 y,
  1037.                    const char* fmt, u31 maxLen = 0, ...);
  1038.  
  1039.   shape::rect draw(Window& dst_surf, s32 x, s32 y,
  1040.                    const char* fmt, u31 maxLen = 0, ...);
  1041.  
  1042.   //(will draw whatever is currently in _fmtBuffer)
  1043.   shape::rect draw(Surface& dst_surf, s32 x, s32 y, u31 maxLen = 0);
  1044.  
  1045.   shape::rect draw(Window& dst_surf, s32 x, s32 y, u31 maxLen = 0);
  1046.  
  1047.  
  1048.  
  1049.   //same behavior as draw, but the x & y (and returned bounding box)
  1050.    //are adjusted to match a 2-pixel border around the text
  1051.  
  1052.   shape::rect drawBox(Surface& dst_surf, s32 x, s32 y,
  1053.                       const char* fmt, u31 maxLen = 0, ...);
  1054.  
  1055.   shape::rect drawBox(Window& dst_surf, s32 x, s32 y,
  1056.                       const char* fmt, u31 maxLen = 0, ...);
  1057.  
  1058.   //(will draw whatever is currently in _fmtBuffer)
  1059.   shape::rect drawBox(Surface& dst_surf, s32 x, s32 y, u31 maxLen = 0);
  1060.  
  1061.   shape::rect drawBox(Window& dst_surf, s32 x, s32 y, u31 maxLen = 0);
  1062.  
  1063.  
  1064.  
  1065.   //positions used here are absolute, in that negative values
  1066.    //will always make the box appear past the top-left side
  1067.   //returns the number of pixels to shift the text by
  1068.    //to make it fit inside the box
  1069.   shape::point drawBoxNoText(Surface& dst_surf, s32 x, s32 y, u31 w, u31 h);
  1070.  
  1071.   shape::point drawBoxNoText(Window& dst_surf, s32 x, s32 y, u31 w, u31 h);
  1072.  
  1073.  
  1074.  
  1075.   //returns final rect of char, after negative margin check
  1076.    //(does not update _lastBounds!)
  1077.   shape::rect drawChar(Surface& dst_surf, u8 chr, s32 x, s32 y);
  1078.  
  1079.   shape::rect drawChar(Window& dst_surf, u8 chr, s32 x, s32 y);
  1080.  
  1081.  
  1082.  
  1083.   //formats fmt into _fmtBuffer, before returning _fmtBuffer
  1084.   u8* format(const char* fmt, ...);
  1085.  
  1086.   //same thing but without formatting basically
  1087.   inline u8* copy(const char* str)
  1088.   { return (u8*)strnCpy((char*)_fmtBuffer, str, _fmtBuffer_len.n); }
  1089.  
  1090. };
  1091.  
  1092.  
  1093.  
  1094.  
  1095.  
  1096. }; /* namespace kit */
  1097.  
  1098. #endif /* _INC__VIDEO_BFONT_SURFACE_HPP */
  1099.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement