Advertisement
Kitomas

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

Aug 29th, 2024
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 40.59 KB | None | 0 0
  1. /******************************************************************************/
  2. /******************************************************************************/
  3. //"ksdl2\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. //"ksdl2\include\kit\_misc_func.hpp":
  59. #ifndef _INC__MISC_FUNC_HPP
  60. #define _INC__MISC_FUNC_HPP
  61.  
  62. #include "commondef.hpp"
  63.  
  64.  
  65. namespace kit {
  66.  
  67.  
  68.  
  69.  
  70.  
  71. enum InitFlagsEnum {
  72.   KINIT_TIMER          = 0x01,
  73.   KINIT_AUDIO          = 0x02,
  74.   KINIT_VIDEO          = 0x04,
  75.   KINIT_JOYSTICK       = 0x08,
  76.   KINIT_GAMECONTROLLER = 0x10,
  77.   KINIT_EVENTS         = 0x20,
  78.  
  79.   KINIT_EVERYTHING     = 0x3F,
  80. };
  81.  
  82.  
  83.  
  84. #define KIT_INIT_ALL() (kit::initSubsystems(KINIT_EVERYTHING))
  85. #define KIT_QUIT_ALL() (kit::quitSubsystems(KINIT_EVERYTHING))
  86.  
  87. //these should only be called from the main thread, only after
  88.  //guaranteeing that no other threads are currently running
  89. void initSubsystems(u32 flags);
  90. void quitSubsystems(u32 flags);
  91. //(quitting with KINIT_EVERYTHING is allowed even if
  92.  //there are no currently active subsystems)
  93.  
  94.  
  95.  
  96.  
  97.  
  98. enum LogPriorityEnum {
  99.   LOG_PRIORITY_VERBOSE = 1,
  100.   LOG_PRIORITY_DEBUG,
  101.   LOG_PRIORITY_INFO,
  102.   LOG_PRIORITY_WARN,
  103.   LOG_PRIORITY_ERROR,
  104.   LOG_PRIORITY_CRITICAL,
  105. };
  106.  
  107.  
  108.  
  109. //Log, except it suppresses some format string stuff
  110. //this macro is especially useful if you're using %llu, (long long unsigned)
  111.  //and gcc would otherwise complain about an unknown format specifier
  112. //(at least, that's what happened when i used plain ol' SDL_Log,
  113.  //even though it prints 18446744073709551615 with %llu just fine)
  114. #define kit_LogS(...) \
  115.   _Pragma("GCC diagnostic push") \
  116.   _Pragma("GCC diagnostic ignored \"-Wformat=\"") \
  117.   _Pragma("GCC diagnostic push") \
  118.   _Pragma("GCC diagnostic ignored \"-Wformat-extra-args\"") \
  119.   kit::Log(__VA_ARGS__); \
  120.   _Pragma("GCC diagnostic pop") \
  121.   _Pragma("GCC diagnostic pop")
  122.  
  123. #define kit_LogVerbose(...)  kit::Log(LOG_PRIORITY_VERBOSE , __VA_ARGS__)
  124. #define kit_LogDebug(...)    kit::Log(LOG_PRIORITY_DEBUG   , __VA_ARGS__)
  125. #define kit_LogInfo(...)     kit::Log(LOG_PRIORITY_INFO    , __VA_ARGS__)
  126. #define kit_LogWarn(...)     kit::Log(LOG_PRIORITY_WARN    , __VA_ARGS__)
  127. #define kit_LogError(...)    kit::Log(LOG_PRIORITY_ERROR   , __VA_ARGS__)
  128. #define kit_LogCritical(...) kit::Log(LOG_PRIORITY_CRITICAL, __VA_ARGS__)
  129.  
  130. #define kit_LogVerboseS(...)  kit_LogS(LOG_PRIORITY_VERBOSE , __VA_ARGS__)
  131. #define kit_LogDebugS(...)    kit_LogS(LOG_PRIORITY_DEBUG   , __VA_ARGS__)
  132. #define kit_LogInfoS(...)     kit_LogS(LOG_PRIORITY_INFO    , __VA_ARGS__)
  133. #define kit_LogWarnS(...)     kit_LogS(LOG_PRIORITY_WARN    , __VA_ARGS__)
  134. #define kit_LogErrorS(...)    kit_LogS(LOG_PRIORITY_ERROR   , __VA_ARGS__)
  135. #define kit_LogCriticalS(...) kit_LogS(LOG_PRIORITY_CRITICAL, __VA_ARGS__)
  136.  
  137. //basically SDL_LogMessage, except with the category always set to application
  138. void Log(LogPriorityEnum priority, const char* fmt, ...);
  139.  
  140.  
  141.  
  142.  
  143.  
  144. //len_max does not include null-terminator!
  145. //if !len_max, call is analogous to str(not n)len
  146. size_t strnLen(const char* str, size_t len_max = 0);
  147.  
  148.  
  149.  
  150. //(mostly) yoinked from stackoverflow :D
  151. //len_max does not include null-terminator!
  152. //if !len_max, call is analogous to str(not n)cmp
  153. s32 strnCmp(const char* str_a, const char* str_b, size_t len_max = 0);
  154.  
  155.  
  156.  
  157. //null-terminator is added at the end of the copy destination!
  158. //if !len_max, call is analogous to str(not n)cpy
  159. void strnCpy(char* str_dst, const char* str_src, size_t len_max);
  160.  
  161.  
  162.  
  163.  
  164.  
  165. const char* getLastSysError();
  166. void clearSysError();
  167.  
  168.  
  169.  
  170.  
  171.  
  172. //frees any error strings managed by kit_sdl2 on the calling thread
  173. //this must be called whenever a const char* exception is caught,
  174.  //only after that Error has been handled by the user.
  175.  //(as in, the pointer should be considered invalid after exiting this function!)
  176. //since this will work even if there is no kit_sdl2-managed error message,
  177.  //it is best practice to call this even if you're mostly sure that
  178.  //the const char* was thrown by the user!
  179. //also, a call of quitSubsystem(KINIT_EVERYTHING)
  180.  //will free all thread errors automatically
  181. //also also, mixups might occur if another thread error is pushed before
  182.  //a call to freeThreadErrors() has the chance to free the previous one(s)
  183.  //on the same thread, resulting in the wrong reference (not an invalid
  184.  //error string, just not the right one) being accessed
  185. void freeThreadErrors();
  186. //TL;DR: best practice is to call this during any catch(const char*) exceptions!
  187.  
  188.  
  189.  
  190.  
  191.  
  192. enum CPUCapabilityFlagsEnum {
  193.   //(always false on CPUs that aren't using Intel instruction sets)
  194.   CPU_HAS_RDTSC   = 0x0001, //'does CPU have RDTSC instruction?'
  195.  
  196.   //(always false on CPUs that aren't using PowerPC instruction sets)
  197.   CPU_HAS_ALTIVEC = 0x0002, //'does CPU have AltiVec features?'
  198.  
  199.   //(always false on CPUs that aren't using Intel instruction sets)
  200.   CPU_HAS_MMX     = 0x0004, //'does CPU have MMX features?'
  201.  
  202.   //(always false on CPUs that aren't using AMD instruction sets)
  203.   CPU_HAS_3DNOW   = 0x0008, //'does CPU have 3DNow! features?'
  204.  
  205.   //(always false on CPUs that aren't using Intel instruction sets)
  206.   CPU_HAS_SSE     = 0x0010, //'does CPU have SSE features?'
  207.  
  208.   //(always false on CPUs that aren't using Intel instruction sets)
  209.   CPU_HAS_SSE2    = 0x0020, //'does CPU have SSE2 features?'
  210.  
  211.   //(always false on CPUs that aren't using Intel instruction sets)
  212.   CPU_HAS_SSE3    = 0x0040, //'does CPU have SSE3 features?'
  213.  
  214.   //(always false on CPUs that aren't using Intel instruction sets)
  215.   CPU_HAS_SSE41   = 0x0080, //'does CPU have SSE4.1 features?'
  216.  
  217.   //(always false on CPUs that aren't using Intel instruction sets)
  218.   CPU_HAS_SSE42   = 0x0100, //'does CPU have SSE4.2 features?'
  219.  
  220.   //(always false on CPUs that aren't using Intel instruction sets)
  221.   CPU_HAS_AVX     = 0x0200, //'does CPU have AVX features?'
  222.  
  223.   //(always false on CPUs that aren't using Intel instruction sets)
  224.   CPU_HAS_AVX2    = 0x0400, //'does CPU have AVX2 features?'
  225.  
  226.   //(always false on CPUs that aren't using Intel instruction sets)
  227.   CPU_HAS_AVX512F = 0x0800, //'does CPU have AVX-512F (foundation) features?'
  228.  
  229.   //(always false on CPUs that aren't using ARM instruction sets)
  230.   CPU_HAS_ARMSIMD = 0x1000, //'does CPU have ARM SIMD (ARMv6) features?'
  231.  
  232.   //(always false on CPUs that aren't using ARM instruction sets)
  233.   CPU_HAS_NEON    = 0x2000, //'does CPU have NEON (ARM SIMD) features?'
  234.  
  235.   //(always false on CPUs that aren't using LOONGARCH instruction sets)
  236.   CPU_HAS_LSX     = 0x4000, //'does CPU have LSX (LOONGARCH SIMD) features?'
  237.  
  238.   //(always false on CPUs that aren't using LOONGARCH instruction sets)
  239.   CPU_HAS_LASX    = 0x8000, //'does CPU have LASX (LOONGARCH SIMD) features?'
  240. };
  241.  
  242.  
  243.  
  244. u32 getCPUCapabilities(); //returns a combination of CPUCapabilityFlagsEnum flags
  245.  
  246. u32 getCPUCacheLineSize(); //in bytes (L1 cache specifically)
  247. u32 getNumLogicalCPUCores(); //(might be > core count if hyperthreading is enabled)
  248.  
  249.  
  250.  
  251.  
  252.  
  253. enum MessageBoxFlagsEnum {
  254.   MSGBOX_ERROR    = 0x0010,
  255.   MSGBOX_WARNING  = 0x0020,
  256.   MSGBOX_INFO     = 0x0040,
  257.   MSGBOX_BTNS_L2R = 0x0080, //buttons placed left-to-right (showMsgBoxEx only)
  258.   MSGBOX_BTNS_R2L = 0x0100, //buttons placed right-to-left (showMsgBoxEx only)
  259. };
  260.  
  261.  
  262.  
  263. class Window; //forward declaration
  264. void showMsgBox(const char* text = nullptr, const char* title = nullptr,
  265.                 u32 flags = MSGBOX_INFO, Window* win = nullptr);
  266.  
  267.  
  268. //showMsgBoxEx is TBD (i don't want to deal with SDL's message boxes right now)
  269.  
  270.  
  271.  
  272.  
  273.  
  274. union Event; //forward declaration
  275. //returns false if there were no events in queue
  276.  //(if event_p is left as nullptr, the next event will not be dequeued)
  277.  //(also, only call this function in the thread that set the video mode,
  278.  // which is usually the main thread!)
  279. bool pollEvent(Event* event_p = nullptr); //requires events subsystem
  280. const char* getEventText(u32 type);
  281.  
  282.  
  283.  
  284.  
  285.  
  286. //(clipboard text uses utf-8 encoding)
  287. bool  clipboardHasText();
  288. char* clipboardGetText(); //(allocates memory; returned ptr must be memory::free'd)
  289. void  clipboardSetText(const char* txt);
  290.  
  291.  
  292.  
  293.  
  294.  
  295. bool showCursor(s32 toggle); //boolean, or -1 to query state without changing
  296.  
  297.  
  298.  
  299.  
  300.  
  301. void warpMouseGlobal(s32 x, s32 y);
  302.  
  303.  
  304.  
  305.  
  306.  
  307. }; /* namespace kit */
  308.  
  309. #endif /* _INC__MISC_FUNC_HPP */
  310. /******************************************************************************/
  311. /******************************************************************************/
  312. //"ksdl2\include\kit\_misc_memory.hpp":
  313. #ifndef _INC__MISC_MEMORY_HPP
  314. #define _INC__MISC_MEMORY_HPP
  315.  
  316. #include "commondef.hpp"
  317.  
  318.  
  319. namespace kit {
  320.  
  321.  
  322. namespace memory {
  323.  
  324.  
  325.  
  326.  
  327.  
  328. void* alloc(size_t size);
  329.  
  330. //this version of free is especially useful, because it's safe to pass nullptr to!
  331.  //this rule goes for both ptr_p and *ptr_p
  332. void free(void* ptr_p);
  333.  
  334. void* realloc(void* ptr_p, size_t newSize);
  335.  
  336. //^^(for both free and realloc, a void** declared in function as void*,
  337.  //  so you don't need to explicitly cast it to void**)
  338.  
  339.  
  340.  
  341. //same as alloc and free, now with exception throwing!
  342. void* alloc2(size_t size);
  343.  
  344. void free2(void* ptr_p); //(unlike free, this will throw if nullptr is given)
  345.  
  346. void* realloc2(void* ptr_p, size_t newSize);
  347.  
  348.  
  349.  
  350. size_t getNumAllocations();
  351.  
  352.  
  353.  
  354.  
  355.  
  356. void* set(void* ptr, s32 value, size_t size);
  357.  
  358. //throws exceptions, unlike set
  359. void* set2(void* ptr, s32 value, size_t size);
  360.  
  361.  
  362.  
  363.  
  364.  
  365. void* copy(void* destination, const void* source, size_t size);
  366.  
  367. //throws exceptions, unlike copy
  368. void* copy2(void* destination, const void* source, size_t size);
  369.  
  370.  
  371.  
  372.  
  373.  
  374. u32 getSystemRAM_MiB(); //returns amount of RAM configured in the system, in MiB
  375.  
  376. u32 getSIMDAlignment(); //returns required alignment for SIMD instructions, in bytes
  377.  
  378.  
  379.  
  380.  
  381.  
  382. //xSIMD are basically the same as their non-SIMD counterparts,
  383.  //except these are aligned and padded for use with SIMD instructions.
  384. //by padded, i mean that it's safe to read/write an incomplete
  385.  //vector at the end of a memory block
  386.  
  387. //(allocSIMD pointers must be freed using freeSIMD,
  388.  //not free, and vice-versa)
  389.  
  390. void* allocSIMD(size_t size);
  391.  
  392. void freeSIMD(void* ptr_p);
  393.  
  394. void* reallocSIMD(void* ptr_p, size_t newSize);
  395.  
  396.  
  397.  
  398. void* allocSIMD2(size_t size);
  399.  
  400. void freeSIMD2(void* ptr_p);
  401.  
  402. void* reallocSIMD2(void* ptr_p, size_t newSize);
  403.  
  404.  
  405.  
  406.  
  407.  
  408. struct Wrapper {
  409.   void* ptr = nullptr;
  410.  
  411.   Wrapper(size_t size); //new memory::alloc
  412.   Wrapper(void* _ptr) : ptr(_ptr) {} //existing memory::alloc
  413.   ~Wrapper(){ memory::free(&ptr); };
  414.  
  415.   //capital A in the middle to avoid name conflict with actual memory::realloc
  416.   inline void* reAlloc(size_t newSize){ return memory::realloc(&ptr, newSize); }
  417.  
  418. };
  419.  
  420.  
  421.  
  422. struct WrapperSIMD {
  423.   void* ptr = nullptr;
  424.  
  425.   WrapperSIMD(size_t size); //new memory::allocSIMD
  426.   WrapperSIMD(void* _ptr) : ptr(_ptr) {} //existing memory::allocSIMD
  427.   ~WrapperSIMD(){ memory::freeSIMD(&ptr); };
  428.  
  429.   //capital A in the middle to remain consistent with Wrapper::reAlloc()
  430.   inline void* reAlloc(size_t newSize){ return memory::reallocSIMD(&ptr, newSize); }
  431.  
  432. };
  433.  
  434.  
  435.  
  436.  
  437.  
  438. }; /* namespace memory */
  439.  
  440.  
  441. }; /* namespace kit */
  442.  
  443. #endif /* _INC__MISC_MEMORY_HPP */
  444. /******************************************************************************/
  445. /******************************************************************************/
  446. //"ksdl2\include\kit\_video_PixelFmt.hpp":
  447. #ifndef _INC__VIDEO_PIXELFMT_HPP
  448. #define _INC__VIDEO_PIXELFMT_HPP
  449.  
  450. #include "commondef.hpp"
  451.  
  452.  
  453. namespace kit {
  454.  
  455.  
  456.  
  457.  
  458.  
  459. enum BlendModesEnum {
  460.   BLENDMODE_NONE  = 0x00000000, //no blending
  461.                                   //dstRGBA = srcRGBA
  462.   BLENDMODE_BLEND = 0x00000001, //alpha blending
  463.                                   //dstRGB = (srcRGB * srcA) + (dstRGB * (1-srcA))
  464.                                   //dstA = srcA + (dstA * (1-srcA))
  465.   BLENDMODE_ADD   = 0x00000002, //additive blending
  466.                                   //dstRGB = (srcRGB * srcA) + dstRGB
  467.                                   //dstA = dstA
  468.   BLENDMODE_MOD   = 0x00000004, //color modulate
  469.                                   //dstRGB = srcRGB * dstRGB
  470.                                   //dstA = dstA
  471.   BLENDMODE_MUL   = 0x00000008, //color multiply
  472.                                   //dstRGB = (srcRGB * dstRGB) + (dstRGB * (1-srcA))
  473.                                   //dstA = dstA
  474.  
  475.   BLENDMODE_INVALID = 0x7FFFFFFF,
  476.  
  477. };
  478.  
  479.  
  480.  
  481.  
  482.  
  483. enum PixelTypeEnum {
  484.   PIXELTYPE_UNKNOWN,
  485.   PIXELTYPE_INDEX1,
  486.   PIXELTYPE_INDEX4,
  487.   PIXELTYPE_INDEX8,
  488.   PIXELTYPE_PACKED8,
  489.   PIXELTYPE_PACKED16,
  490.   PIXELTYPE_PACKED32,
  491.   PIXELTYPE_ARRAYU8,
  492.   PIXELTYPE_ARRAYU16,
  493.   PIXELTYPE_ARRAYU32,
  494.   PIXELTYPE_ARRAYF16,
  495.   PIXELTYPE_ARRAYF32,
  496. };
  497.  
  498. enum PixelOrderEnum { //bitmap pixel order, high bit -> low bit
  499.   PIXELORDER_NONE,
  500.   PIXELORDER_4321,
  501.   PIXELORDER_1234,
  502. };
  503.  
  504. enum PackedOrderEnum { //packed component order, high bit -> low bit
  505.   PACKEDORDER_NONE,
  506.   PACKEDORDER_XRGB,
  507.   PACKEDORDER_RGBX,
  508.   PACKEDORDER_ARGB,
  509.   PACKEDORDER_RGBA,
  510.   PACKEDORDER_XBGR,
  511.   PACKEDORDER_BGRX,
  512.   PACKEDORDER_ABGR,
  513.   PACKEDORDER_BGRA,
  514. };
  515.  
  516. enum ArrayOrderEnum { //array component order, low byte -> high byte
  517.   ARRAYORDER_NONE,
  518.   ARRAYORDER_RGB,
  519.   ARRAYORDER_RGBA,
  520.   ARRAYORDER_ARGB,
  521.   ARRAYORDER_BGR,
  522.   ARRAYORDER_BGRA,
  523.   ARRAYORDER_ABGR,
  524. };
  525.  
  526. enum PackedLayoutEnum { //packed component layout
  527.   PACKEDLAYOUT_NONE,
  528.   PACKEDLAYOUT_332,
  529.   PACKEDLAYOUT_4444,
  530.   PACKEDLAYOUT_1555,
  531.   PACKEDLAYOUT_5551,
  532.   PACKEDLAYOUT_565,
  533.   PACKEDLAYOUT_8888,
  534.   PACKEDLAYOUT_2101010,
  535.   PACKEDLAYOUT_1010102,
  536. };
  537.  
  538. #define KIT_DEFINE_PIXELFOURCC(A, B, C, D) ( \
  539.   (  ((u32)((u8)(A)))<< 0  )  | \
  540.   (  ((u32)((u8)(B)))<< 8  )  | \
  541.   (  ((u32)((u8)(C)))<<16  )  | \
  542.   (  ((u32)((u8)(D)))<<24  )    )
  543.  
  544. #define KIT_DEF_PIXELFMT(type, order, layout, bits, bytes) \
  545.     ((1 << 28) | ((type) << 24) | ((order) << 20) | ((layout) << 16) | \
  546.      ((bits) << 8) | ((bytes) << 0))
  547.  
  548. #define KIT_PIXELFLAG(X)    (((X) >> 28) & 0x0F)
  549. #define KIT_PIXELTYPE(X)    (((X) >> 24) & 0x0F)
  550. #define KIT_PIXELORDER(X)   (((X) >> 20) & 0x0F)
  551. #define KIT_PIXELLAYOUT(X)  (((X) >> 16) & 0x0F)
  552. #define KIT_BITSPERPIXEL(X) (((X) >> 8) & 0xFF)
  553. #define KIT_BYTESPERPIXEL(X) \
  554.     (KIT_ISPIXELFORMAT_FOURCC(X) ? \
  555.         ((((X) == PIXELFMT_YUY2) || \
  556.           ((X) == PIXELFMT_UYVY) || \
  557.           ((X) == PIXELFMT_YVYU)) ? 2 : 1) : (((X) >> 0) & 0xFF))
  558.  
  559. #define KIT_ISPIXELFORMAT_INDEXED(format)   \
  560.     (!KIT_ISPIXELFORMAT_FOURCC(format) && \
  561.      ((KIT_PIXELTYPE(format) == PIXELTYPE_INDEX1) || \
  562.       (KIT_PIXELTYPE(format) == PIXELTYPE_INDEX4) || \
  563.       (KIT_PIXELTYPE(format) == PIXELTYPE_INDEX8)))
  564.  
  565. #define KIT_ISPIXELFORMAT_PACKED(format) \
  566.     (!KIT_ISPIXELFORMAT_FOURCC(format) && \
  567.      ((KIT_PIXELTYPE(format) == PIXELTYPE_PACKED8) || \
  568.       (KIT_PIXELTYPE(format) == PIXELTYPE_PACKED16) || \
  569.       (KIT_PIXELTYPE(format) == PIXELTYPE_PACKED32)))
  570.  
  571. #define KIT_ISPIXELFORMAT_ARRAY(format) \
  572.     (!KIT_ISPIXELFORMAT_FOURCC(format) && \
  573.      ((KIT_PIXELTYPE(format) == PIXELTYPE_ARRAYU8) || \
  574.       (KIT_PIXELTYPE(format) == PIXELTYPE_ARRAYU16) || \
  575.       (KIT_PIXELTYPE(format) == PIXELTYPE_ARRAYU32) || \
  576.       (KIT_PIXELTYPE(format) == PIXELTYPE_ARRAYF16) || \
  577.       (KIT_PIXELTYPE(format) == PIXELTYPE_ARRAYF32)))
  578.  
  579. #define KIT_ISPIXELFORMAT_ALPHA(format)   \
  580.     ((KIT_ISPIXELFORMAT_PACKED(format) && \
  581.      ((KIT_PIXELORDER(format) == PACKEDORDER_ARGB) || \
  582.       (KIT_PIXELORDER(format) == PACKEDORDER_RGBA) || \
  583.       (KIT_PIXELORDER(format) == PACKEDORDER_ABGR) || \
  584.       (KIT_PIXELORDER(format) == PACKEDORDER_BGRA))) || \
  585.     (KIT_ISPIXELFORMAT_ARRAY(format) && \
  586.      ((KIT_PIXELORDER(format) == ARRAYORDER_ARGB) || \
  587.       (KIT_PIXELORDER(format) == ARRAYORDER_RGBA) || \
  588.       (KIT_PIXELORDER(format) == ARRAYORDER_ABGR) || \
  589.       (KIT_PIXELORDER(format) == ARRAYORDER_BGRA))))
  590.  
  591. //The flag is set to 1 because 0x1? is not in the printable ASCII range
  592. #define KIT_ISPIXELFORMAT_FOURCC(format)    \
  593.     ((format) && (KIT_PIXELFLAG(format) != 1))
  594.  
  595. enum PixelFormatEnum {
  596.     PIXELFMT_UNKNOWN,
  597.     PIXELFMT_INDEX1LSB   = KIT_DEF_PIXELFMT(PIXELTYPE_INDEX1, PIXELORDER_4321, 0, 1, 0),
  598.     PIXELFMT_INDEX1MSB   = KIT_DEF_PIXELFMT(PIXELTYPE_INDEX1, PIXELORDER_1234, 0, 1, 0),
  599.     PIXELFMT_INDEX4LSB   = KIT_DEF_PIXELFMT(PIXELTYPE_INDEX4, PIXELORDER_4321, 0, 4, 0),
  600.     PIXELFMT_INDEX4MSB   = KIT_DEF_PIXELFMT(PIXELTYPE_INDEX4, PIXELORDER_1234, 0, 4, 0),
  601.     PIXELFMT_INDEX8      = KIT_DEF_PIXELFMT(PIXELTYPE_INDEX8,               0, 0, 8, 1),
  602.  
  603.     PIXELFMT_RGB332      = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED8 , PACKEDORDER_XRGB, PACKEDLAYOUT_332 ,  8, 1),
  604.     PIXELFMT_XRGB4444    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_XRGB, PACKEDLAYOUT_4444, 12, 2),
  605.     PIXELFMT_XBGR4444    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_XBGR, PACKEDLAYOUT_4444, 12, 2),
  606.     PIXELFMT_XRGB1555    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_XRGB, PACKEDLAYOUT_1555, 15, 2),
  607.     PIXELFMT_XBGR1555    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_XBGR, PACKEDLAYOUT_1555, 15, 2),
  608.     PIXELFMT_ARGB4444    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_ARGB, PACKEDLAYOUT_4444, 16, 2),
  609.     PIXELFMT_RGBA4444    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_RGBA, PACKEDLAYOUT_4444, 16, 2),
  610.     PIXELFMT_ABGR4444    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_ABGR, PACKEDLAYOUT_4444, 16, 2),
  611.     PIXELFMT_BGRA4444    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_BGRA, PACKEDLAYOUT_4444, 16, 2),
  612.     PIXELFMT_ARGB1555    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_ARGB, PACKEDLAYOUT_1555, 16, 2),
  613.     PIXELFMT_RGBA5551    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_RGBA, PACKEDLAYOUT_5551, 16, 2),
  614.     PIXELFMT_ABGR1555    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_ABGR, PACKEDLAYOUT_1555, 16, 2),
  615.     PIXELFMT_BGRA5551    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_BGRA, PACKEDLAYOUT_5551, 16, 2),
  616.     PIXELFMT_RGB565      = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_XRGB, PACKEDLAYOUT_565 , 16, 2),
  617.     PIXELFMT_BGR565      = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_XBGR, PACKEDLAYOUT_565 , 16, 2),
  618.  
  619.     PIXELFMT_RGB24       = KIT_DEF_PIXELFMT(PIXELTYPE_ARRAYU8, ARRAYORDER_RGB, 0, 24, 3),
  620.     PIXELFMT_BGR24       = KIT_DEF_PIXELFMT(PIXELTYPE_ARRAYU8, ARRAYORDER_BGR, 0, 24, 3),
  621.  
  622.     PIXELFMT_XRGB8888    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED32, PACKEDORDER_XRGB, PACKEDLAYOUT_8888, 24, 4),
  623.     PIXELFMT_RGBX8888    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED32, PACKEDORDER_RGBX, PACKEDLAYOUT_8888, 24, 4),
  624.     PIXELFMT_XBGR8888    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED32, PACKEDORDER_XBGR, PACKEDLAYOUT_8888, 24, 4),
  625.     PIXELFMT_BGRX8888    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED32, PACKEDORDER_BGRX, PACKEDLAYOUT_8888, 24, 4),
  626.     PIXELFMT_ARGB8888    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED32, PACKEDORDER_ARGB, PACKEDLAYOUT_8888, 32, 4),
  627.     PIXELFMT_RGBA8888    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED32, PACKEDORDER_RGBA, PACKEDLAYOUT_8888, 32, 4),
  628.     PIXELFMT_ABGR8888    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED32, PACKEDORDER_ABGR, PACKEDLAYOUT_8888, 32, 4),
  629.     PIXELFMT_BGRA8888    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED32, PACKEDORDER_BGRA, PACKEDLAYOUT_8888, 32, 4),
  630.     PIXELFMT_ARGB2101010 = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED32, PACKEDORDER_ARGB, PACKEDLAYOUT_2101010, 32, 4),
  631.  
  632.  
  633.     //these are actually what is returned by getPixelFmtName(),
  634.      //so instead of "PIXELFMT_XRGB4444", it would return "PIXELFMT_RGB444"
  635.     PIXELFMT_RGB444 = PIXELFMT_XRGB4444,
  636.     PIXELFMT_BGR444 = PIXELFMT_XBGR4444,
  637.     PIXELFMT_RGB555 = PIXELFMT_XRGB1555,
  638.     PIXELFMT_BGR555 = PIXELFMT_XBGR1555,
  639.     PIXELFMT_RGB888 = PIXELFMT_XRGB8888,
  640.     PIXELFMT_BGR888 = PIXELFMT_XBGR8888,
  641.  
  642.     //aliases for convenenience
  643.     PIXELFMT_RGBA32 = PIXELFMT_ABGR8888,
  644.     PIXELFMT_ARGB32 = PIXELFMT_BGRA8888,
  645.     PIXELFMT_BGRA32 = PIXELFMT_ARGB8888,
  646.     PIXELFMT_ABGR32 = PIXELFMT_RGBA8888,
  647.  
  648.  
  649.     PIXELFMT_YV12 = KIT_DEFINE_PIXELFOURCC('Y', 'V', '1', '2'), //Planar mode: Y + V + U            (3 planes)
  650.     PIXELFMT_IYUV = KIT_DEFINE_PIXELFOURCC('I', 'Y', 'U', 'V'), //Planar mode: Y + U + V            (3 planes)
  651.     PIXELFMT_YUY2 = KIT_DEFINE_PIXELFOURCC('Y', 'U', 'Y', '2'), //Packed mode: Y0 + U0 + Y1 + V0    (1 plane )
  652.     PIXELFMT_UYVY = KIT_DEFINE_PIXELFOURCC('U', 'Y', 'V', 'Y'), //Packed mode: U0 + Y0 + V0 + Y1    (1 plane )
  653.     PIXELFMT_YVYU = KIT_DEFINE_PIXELFOURCC('Y', 'V', 'Y', 'U'), //Packed mode: Y0 + V0 + Y1 + U0    (1 plane )
  654.     PIXELFMT_NV12 = KIT_DEFINE_PIXELFOURCC('N', 'V', '1', '2'), //Planar mode: Y + U/V interleaved  (2 planes)
  655.     PIXELFMT_NV21 = KIT_DEFINE_PIXELFOURCC('N', 'V', '2', '1'), //Planar mode: Y + V/U interleaved  (2 planes)
  656. };
  657.  
  658.  
  659.  
  660.  
  661.  
  662. //get a human-readable string for a given pixel format
  663. const char* getPixelFmtName(u32 pixel_format);
  664.  
  665.  
  666.  
  667.  
  668.  
  669. }; /* namespace kit */
  670.  
  671. #endif /* _INC__VIDEO_PIXELFMT_HPP */
  672. /******************************************************************************/
  673. /******************************************************************************/
  674. //"ksdl2\include\kit\_video_Surface.hpp":
  675. #ifndef _INC__VIDEO_SURFACE_HPP
  676. #define _INC__VIDEO_SURFACE_HPP
  677.  
  678. #include "commondef.hpp"
  679. #include "_video_PixelFmt.hpp"
  680.  
  681.  
  682. namespace kit {
  683.  
  684.  
  685.  
  686.  
  687.  
  688. class Surface; //forward declaration
  689.  
  690. //the pointer this returns (pixel data) must be memory::free'd after it's done being used
  691.  //(which means that, when used in a Surface constructor, it must be memory that
  692.  // was allocated using memory::alloc, as it WILL attempt to free that pointer)
  693. //also, this doesn't really account for indexed color formats, as while you can specify
  694.  //the palette indexes in the pixel data, the actual pallete will need to be set manually
  695. typedef void* (*SurfaceLoaderCallback)(const char* filePath, u32& format_out,
  696.                                        s32& width_out, s32& height_out);
  697.  
  698. typedef void (*SurfaceSaverCallback)(const char* filePath, Surface& surface_in);
  699.  
  700. //(when saving OR loading, filePath should never be nullptr by the time the
  701.  //callback is invoked, so you don't need to worry about checking for it)
  702.  
  703.  
  704.  
  705. //(not an actual callback, but rather the lack of one)
  706. #define SurfaceLoadBMP ((SurfaceLoaderCallback)nullptr)
  707. #define SurfaceSaveBMP ((SurfaceSaverCallback)nullptr)
  708.  
  709. void* SurfaceLoadQOI(const char* fP, u32& fmt_o, s32& w_o, s32& h_o);
  710. void  SurfaceSaveQOI(const char* fP, Surface& s_i);
  711.  
  712. void* SurfaceLoadPNG(const char* fP, u32& fmt_o, s32& w_o, s32& h_o);
  713. void  SurfaceSavePNG(const char* fP, Surface& s_i);
  714.  
  715.  
  716.  
  717.  
  718.  
  719. //(specifically, these are the save/load callbacks that come bundled with kit_sdl2,
  720. // though you can in theory make your own for any file format you want! :D)
  721.  
  722. //currently supported image file formats:
  723.  //.bmp (SurfaceLoadBMP, SurfaceSaveBMP)
  724.  //.qoi (SurfaceLoadQOI, SurfaceSaveQOI)
  725.  //.png (SurfaceLoadPNG, SurfaceSavePNG)
  726.  
  727. class Window; //forward declaration
  728.  
  729. class Surface { //16B
  730.   u32          _type;
  731.   bool        _valid = false;
  732.   bool _constructing = true;
  733.   u16     _padding16;
  734.   GenOpqPtr     _opq = nullptr;
  735.  
  736.  
  737.   //used by both of the 'load from file' constructors
  738.   void _construct_file(const char* filePath, SurfaceLoaderCallback callback,
  739.                        const char* fname = nullptr);
  740.  
  741.  
  742. public:
  743.  
  744.   //create a new, completely blank surface
  745.   Surface(u32 width, u32 height, u32 pixel_format);
  746.  
  747.   //create from another Surface, converting to a new pixel format in the process
  748.   Surface(const Surface& src, u32 pixel_format);
  749.  
  750.   //create from a Window's surface, converting to a new pixel format in the process
  751.   Surface(const Window& src, u32 pixel_format);
  752.  
  753.   //create from an image file of a specific file format
  754.    //(may reduce binary size if you only plan to use 1 image type or something)
  755.   Surface(const char* filePath, SurfaceLoaderCallback callback)
  756.   { _construct_file(filePath, callback); }
  757.  
  758.   //create from an image file of any supported file format (.png, .qoi, .bmp)
  759.   Surface(const char* filePath);
  760.  
  761.   ~Surface();
  762.  
  763.  
  764.  
  765.   //(this will overwrite any file named filePath! make sure to check
  766.    //with fileio::exists() unless you intend to overwrite the previous file)
  767.   void saveImage(const char* filePath, SurfaceSaverCallback callback);
  768.  
  769.  
  770.  
  771.   //"If RLE is enabled, color key and alpha blending blits are much faster,
  772.    //but the surface must be locked before directly accessing the pixels."
  773.   bool                hasRLE();
  774.   bool                hasLockRequirement();
  775.   u32                 getPixelFmt();
  776.   shape::point        getSize(); //{width, height}
  777.   u32                 getBytesPerRow();
  778.   void*               getPixelData();
  779.   void*               getUserdata();
  780.   u32                 getPaletteLen(); //returns 0 if Surface doesn't use a palette
  781.   const colors::ABGR* getPaletteColors(); //returns nullptr if Surface doesn't use a palette
  782.   BlendModesEnum      getBlendMode();
  783.   u8                  getAlphaMod();
  784.   colors::BGR         getColorMod();
  785.   u32                 getColorKey();
  786.  
  787.  
  788.   void setRLE(bool enable);
  789.   void setUserdata(void* userdata);
  790.   void setPaletteColors(const colors::ABGR* newColors, u32 numColors, u32 firstColor = 0);
  791.   void setBlendMode(BlendModesEnum blendMode);
  792.   void setAlphaMod(u8 alphaMod);
  793.   void setColorMod(colors::BGR colorMod);
  794.   void setColorKey(bool enable, u32 key);
  795.  
  796.  
  797.   u32 mapRGB(colors::BGR inputColor);
  798.   u32 mapRGBA(colors::ABGR inputColor);
  799.  
  800.   void lock(bool locked = true);
  801.   inline void unlock(){ lock(false); }
  802.  
  803.  
  804.  
  805.   void fillRects(colors::ABGR color, const shape::rect* rects = nullptr, size_t rects_len = 0);
  806.  
  807.  
  808.   void blit(Surface& dst_surf, const shape::point* dst,
  809.                                const shape::rect*  src = nullptr);
  810.  
  811.   void blit(Window&  dst_surf, const shape::point* dst,
  812.                                const shape::rect*  src = nullptr);
  813.  
  814.  
  815.   void blitScaled(Surface& dst_surf, const shape::rect* dst,
  816.                                      const shape::rect* src = nullptr);
  817.  
  818.   void blitScaled(Window&  dst_surf, const shape::rect* dst,
  819.                                      const shape::rect* src = nullptr);
  820.  
  821.  
  822.   //centered blit (as in, x&y is the center of the blit's destination)
  823.   void blitAt(Surface& dst_surf,  s32 x, s32 y, f32 scale = 1.0f);
  824.  
  825.   void blitAt(Window&  dst_surf,  s32 x, s32 y, f32 scale = 1.0f);
  826.  
  827. };
  828.  
  829.  
  830.  
  831.  
  832.  
  833. }; /* namespace kit */
  834.  
  835. #endif /* _INC__VIDEO_SURFACE_HPP */
  836. /******************************************************************************/
  837. /******************************************************************************/
  838. //"ksdl2\include\kit\_video_Window.hpp":
  839. #ifndef _INC__VIDEO_WINDOW_HPP
  840. #define _INC__VIDEO_WINDOW_HPP
  841.  
  842. #include "commondef.hpp"
  843. #include "_misc_Mutex.hpp"
  844.  
  845.  
  846. namespace kit {
  847.  
  848.  
  849.  
  850.  
  851.  
  852. enum WindowPositionEnum {
  853.   WINPOS_UNDEFINED = 0x1FFF0000u,
  854.   WINPOS_CENTERED  = 0x2FFF0000u,
  855. };
  856.  
  857.  
  858.  
  859. enum WindowFlagEnum {
  860.   WINFLAG_FULLSCREEN         = 0x00000001, //fullscreen window
  861.   WINFLAG_OPENGL             = 0x00000002, //window usable with an opengl context
  862.   WINFLAG_HIDDEN             = 0x00000008, //window is not visible
  863.   WINFLAG_BORDERLESS         = 0x00000010, //no window decoration
  864.   WINFLAG_RESIZABLE          = 0x00000020, //window can be resized
  865.   WINFLAG_MINIMIZED          = 0x00000040, //window is minimized
  866.   WINFLAG_MAXIMIZED          = 0x00000080, //window is maximized
  867.   WINFLAG_INPUT_GRABBED      = 0x00000100, //window has grabbed input focus
  868.   WINFLAG_FULLSCREEN_DESKTOP = 0x00001000|WINFLAG_FULLSCREEN, //fs window at desktop resolution
  869.   WINFLAG_VULKAN             = 0x10000000, //window usable with a vulkan instance
  870. };
  871.  
  872.  
  873.  
  874.  
  875.  
  876. struct DisplayMode { //16B
  877.   u32 pixelFmt;
  878.   s32 w, h;
  879.   s32 refreshRate;
  880. };
  881.  
  882.  
  883.  
  884.  
  885.  
  886. class Window { //40B (24B+_lock)
  887.   u32          _type;
  888.   bool        _valid = false;
  889.   bool _constructing = true;
  890.   u16     _padding16;
  891.   GenOpqPtr     _win = nullptr;
  892.   GenOpqPtr    _surf = nullptr; //window's surface (internal)
  893.   Mutex        _lock; //mostly for mutex'ing surface accesses
  894.  
  895.  
  896. public:
  897.  
  898.   Window(const char* winTitle,
  899.          s32 winWidth, s32 winHeight,
  900.          u32 winFlags = 0,
  901.          s32 winX = WINPOS_UNDEFINED,
  902.          s32 winY = WINPOS_UNDEFINED);
  903.  
  904.   ~Window();
  905.  
  906.  
  907.   inline bool isValid()       { return _valid;        }
  908.   inline bool isConstructing(){ return _constructing; }
  909.  
  910.   inline void   lock(bool locked = true){ _lock.lock(locked); }
  911.   inline void unlock()                  { _lock.lock(false ); }
  912.  
  913.  
  914.   bool         hasSurface();
  915.   u32          getPixelFormat(); //returns pixel format used by window's surface
  916.   u32          getID();
  917.   u32          getDisplayIndex();
  918.   void*        getUserdata(const char* name);
  919.   DisplayMode  getDisplayMode(); //get display mode when visible at fullscreen
  920.   const char*  getTitle();
  921.   shape::point getSize();
  922.   u32          getFlags();
  923.   f32          getOpacity();
  924.   shape::point getMinSize();
  925.   shape::point getMaxSize();
  926.   shape::point getPosition();
  927.   bool         getGrab();
  928.   bool         getKeyboardGrab();
  929.   bool         getMouseGrab();
  930.   shape::rect  getMouseGrabRect();
  931.   f32          getBrightness(); //the window's gamma multiplier, specifically
  932.  
  933.  
  934.    //returns previous userdata pointer; name "Window class" is forbidden (as it is used internally)
  935.   void* setUserdata(const char* name, void* userdata);
  936.   void  setDisplayMode(const DisplayMode* mode); //nullptr to use win's dims & desktop's fmt & refresh rate
  937.   void  setTitle(const char* title);
  938.   void  setSize(s32 width, s32 height);
  939.   void  setVisibility(bool visible);
  940.   void  setFullscreen(u32 mode); //0,1,2 = disabled, enabled, enabled @ desktop resolution
  941.   void  setResizable(bool enable);
  942.   void  setBordered(bool enable);
  943.   void  setAlwaysOnTop(bool enable);
  944.   void  setOpacity(f32 opacity); //0.0f -> 1.0f
  945.   void  setMinSize(s32 minWidth, s32 minHeight);
  946.   void  setMaxSize(s32 maxWidth, s32 maxHeight);
  947.   void  setPosition(s32 x, s32 y);
  948.   void  setGrab(bool enable); //also includes keyboard if SDL_HINT_GRAB_KEYBOARD is set (tbd: replace macro name)
  949.   void  setKeyboardGrab(bool enable);
  950.   void  setMouseGrab(bool enable); //confines mouse cursor to window
  951.   void  setMouseGrabRect(const shape::rect* rect = nullptr); //nullptr to use entire window
  952.   void  setBrightness(f32 brightness); //gamma multiplier; 0.0f -> 1.0f
  953.  
  954.  
  955.   void warpMouse(s32 x, s32 y); //teleport cursor, relative to the window's top-left corner
  956.   void minimize();
  957.   void maximize();
  958.   void restore();
  959.   void raise(); //also makes window gain input focus
  960.  
  961.  
  962.  
  963.   //window surface stuff specifically
  964.  
  965.   //(renewSurface is basically SDL_GetWindowSurface(),
  966.    //but the returned ptr is handled internally)
  967.   bool renewSurface(); //returns false if the Window is invalid
  968.   void destroySurface(); //(surface is destroyed automatically when the Window is destroyed)
  969.   void updateSurface(const shape::rect* rects = nullptr, u32 rects_len = 0);
  970.   //(^^ VV leave rects as nullptr to update/fill whole surface)
  971.   void fillRects(colors::ABGR color, const shape::rect* rects = nullptr, size_t rects_len = 0);
  972.  
  973. };
  974.  
  975.  
  976.  
  977.  
  978.  
  979. //returns window if input is grabbed; nullptr otherwise
  980. Window* getGrabbedWindow();
  981.  
  982.  
  983.  
  984.  
  985.  
  986. }; /* namespace kit */
  987.  
  988. #endif /* _INC__VIDEO_WINDOW_HPP */
  989. /******************************************************************************/
  990. /******************************************************************************/
  991. //"ksdl2\src\kit_sdl2\_kit_common.hpp":
  992. #ifndef _SRC__KIT_COMMON_HPP
  993. #define _SRC__KIT_COMMON_HPP
  994.  
  995. /* notes/todo:
  996. use fstr to give more coherent error throws
  997. throw if trying to create object using an uninitialized subsystem
  998. specify which functions/methods can throw exceptions
  999. when throwing in a constructor, make sure to deallocate any memory
  1000.  
  1001. SDL_FlushEvent/Events
  1002. SDL_HasEvent/Events
  1003. SDL_JoystickEventState
  1004. SDL_PeepEvents
  1005. SDL_PushEvent
  1006. SDL_RegisterEvents
  1007. SDL_WaitEvent/EventTimeout
  1008.  
  1009. what's going on with lodepng's encoder's file stuff?
  1010. maybe put a mutex on numAllocations
  1011.  
  1012. allow user to push their own text errors,
  1013.  
  1014. replace relevant unsigned stuff with signed, while making <0 checks
  1015. only make validity checks when i have to
  1016.  
  1017. */
  1018.  
  1019.  
  1020. #include <SDL2/SDL.h>
  1021.  
  1022.  
  1023. #ifdef _DEBUG
  1024. #define _log(...) kit_LogInfo(__VA_ARGS__)
  1025. #define loghere kit_LogInfo("line %4i: (%s)",__LINE__,__FILE__);
  1026. #define _getnumallocs kit_LogInfo("%4i: # OF ALLOCATIONS = %u", __LINE__, (u32)memory::getNumAllocations());
  1027.  
  1028. #else
  1029. #define _log(...)
  1030. #define loghere
  1031. #define _getnumallocs
  1032.  
  1033. #endif
  1034.  
  1035. /*
  1036. #define DISABLE_WARNING(_name) _Pragma(#_name)
  1037. #define DISABLE_WARNING_PUSH(_name) _Pragma("GCC diagnostic push") DISABLE_WARNING(_name)
  1038. #define DISABLE_WARNING_POP _Pragma("GCC diagnostic pop")
  1039. */
  1040.  
  1041.  
  1042. #include <kit/all.hpp>
  1043.  
  1044. int snprintf(char*, size_t, const char*, ...); //only used by fstr
  1045. int remove(const char *);                      //only used by fileio::remove
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051. //class type ID macros
  1052. #define KIT_OPAQUE_PRESENT (0x80000000)
  1053. #define KIT_OPAQUE2_PRESENT (KIT_OPAQUE_PRESENT|0x40000000) //opaque 1 is implied
  1054. #define KIT_IS_OPAQUE_PRESENT(_type) ( ((_type)&KIT_OPAQUE_PRESENT) != 0 )
  1055. #define KIT_IS_OPAQUE2_PRESENT(_type) ( ((_type)&KIT_OPAQUE2_PRESENT) != 0 )
  1056.  
  1057. #define KIT_CLASSTYPE_NULL        (0x0000                      )
  1058. #define KIT_CLASSTYPE_MUTEX       (0x0001 | KIT_OPAQUE_PRESENT )
  1059. #define KIT_CLASSTYPE_THREAD      (0x0002 | KIT_OPAQUE_PRESENT )
  1060. #define KIT_CLASSTYPE_BINARYDATA  (0x0003                      )
  1061. #define KIT_CLASSTYPE_FILE        (0x0004 | KIT_OPAQUE_PRESENT )
  1062. #define KIT_CLASSTYPE_WINDOW      (0x0005 | KIT_OPAQUE2_PRESENT)
  1063. #define KIT_CLASSTYPE_SURFACE     (0x0006 | KIT_OPAQUE_PRESENT )
  1064. //#define KIT_CLASSTYPE_TEXTURE     (0x0007 | KIT_OPAQUE_PRESENT )
  1065. #define KIT_CLASSTYPE_AUDIODEVICE (0x0008 | KIT_OPAQUE_PRESENT )
  1066. #define KIT_CLASSTYPE_AUDIOSTREAM (0x0009 | KIT_OPAQUE_PRESENT )
  1067. #define KIT_CLASSTYPE_AUDIODATA   (0x000A                      )
  1068. #define KIT_CLASSTYPE_EVENTWATCH  (0x000B                      )
  1069.  
  1070.  
  1071.  
  1072.  
  1073.  
  1074. namespace kit {
  1075.  
  1076.  
  1077.  
  1078.  
  1079.  
  1080. #define DISABLE_STRICT_ALIASING_WARNING \
  1081.   _Pragma("GCC diagnostic push") \
  1082.   _Pragma("GCC diagnostic ignored \"-Wstrict-aliasing\"")
  1083. #define POP_IGNORED_WARNING \
  1084.   _Pragma("GCC diagnostic pop")
  1085.  
  1086. #define KIT_GET_CLASS_TYPE(_ptr)         ( ((kit::_commonClassValues*)(_ptr))->type         )
  1087. #define KIT_GET_CLASS_VALID(_ptr)        ( ((kit::_commonClassValues*)(_ptr))->valid        )
  1088. #define KIT_GET_CLASS_CONSTRUCTING(_ptr) ( ((kit::_commonClassValues*)(_ptr))->constructing )
  1089. #define KIT_GET_CLASS_DATA(_ptr)         ( ((kit::_commonClassValues*)(_ptr))->data         )
  1090. #define KIT_GET_CLASS_OPAQUE(_ptr)       ( ((kit::_commonClassValues*)(_ptr))->opq          )
  1091. #define KIT_GET_CLASS_OPAQUE2(_ptr)      ( ((kit::_commonClassValues*)(_ptr))->opq2         )
  1092.  
  1093. #define KIT_IS_CLASS_TYPE(_ptr,_type)   ( KIT_GET_CLASS_TYPE(_ptr) == (_type) )
  1094. #define KIT_IS_CLASS_VALID(_ptr)        ( KIT_GET_CLASS_VALID(_ptr) != 0 )
  1095. #define KIT_IS_CLASS_CONSTRUCTING(_ptr) ( KIT_GET_CLASS_CONSTRUCTING(_ptr) != 0 )
  1096.  
  1097. struct _commonClassValues { //8-24B (depending on KIT_OPAQUE_PRESENT/KIT_OPAQUE2_PRESENT)
  1098.   u32          type;
  1099.   bool        valid; //'is object fully and successfully constructed?'
  1100.   bool constructing; //'is object currently inside constructor?'
  1101.   //^^this is useful for if public class functions are called inside the
  1102.    //constructor itself, but the valid flag is not set to true yet
  1103.   u16          data; //purpose varies by class type; unused by some (or it's split into 2 u8)
  1104.   GenOpqPtr     opq; //nonexistent if type lacks the KIT_OPAQUE_PRESENT flag
  1105.   GenOpqPtr    opq2; //nonexistent if type lacks the KIT_OPAQUE2_PRESENT flag
  1106. };
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112. union Error { //16B
  1113.   struct {
  1114.     const char* _txt;
  1115.     u32   _thread_id;
  1116.     bool       _heap;
  1117.     u8     _padding8;
  1118.     u16   _padding16;
  1119.   };
  1120.  
  1121.   struct { u64 _0, _1; };
  1122.  
  1123.  
  1124.   Error() : _txt(nullptr), _thread_id(0), _heap(false) {}
  1125.  
  1126.   Error(const char* txt, bool heap = false)
  1127.     : _txt(txt), _thread_id(SDL_GetThreadID(nullptr)), _heap(heap) {}
  1128.  
  1129.  
  1130.   inline const char* text(){
  1131.     return (_txt != nullptr) ? _txt : "(ERROR TEXT IS NULLPTR)"; }
  1132.  
  1133. };
  1134.  
  1135.  
  1136.  
  1137.  
  1138.  
  1139. #define FSTR_LEN ((size_t)1024)
  1140.  
  1141. struct _globalStates {
  1142.   //(make sure to not call fstr from more than 2 threads at once!)
  1143.   char fstr_str[2][FSTR_LEN];
  1144.   s32 fstr_which = 0;
  1145.  
  1146.   //normally, the user is warned if the SDL version's patch level
  1147.    //is <= the one kit_sdl2 was made for, but this can be disabled
  1148.   bool patch_warning_disabled;
  1149.  
  1150.   char _; //padding
  1151.  
  1152.   u16 CPUCapabilities; //some combination of CPUCapabilityFlagsEnum values
  1153.  
  1154.   u64 performanceFreq = 0; //how many performance counter units per second
  1155.  
  1156.   //(haptic & sensor subsystems are not included)
  1157.   union {
  1158.     u64 init_value;
  1159.     struct {
  1160.       bool timer;
  1161.       bool audio;
  1162.       bool video;          //auto-inits events
  1163.       bool gamecontroller; //auto-inits joystick
  1164.       bool joystick;       //auto-inits events
  1165.       bool events;
  1166.       bool joystick_sdl; //same as events_sdl, except with the joystick subsystem
  1167.       bool events_sdl;   //'were events EXPLICITLY initialized? (as in, not auto-init)'
  1168.     } init;
  1169.   };
  1170.  
  1171.   Error* errors     = nullptr;
  1172.   size_t errors_len = 0;
  1173.  
  1174.   SDL_mutex* lock = nullptr;
  1175.  
  1176. };
  1177.  
  1178. //found in "kit_func_init-quit.cpp"
  1179. extern _globalStates _gl; //gl as in global, not opengl
  1180. extern const char _fstr_failure[]; // = "(FSTR FAILED)"
  1181.  
  1182. //(will complain about undefined sequencing if you put fstr in a call to fstr,
  1183.  //due to the of the "^=" assignment operator)
  1184. #define fstr(...) ( \
  1185.   (snprintf(_gl.fstr_str[_gl.fstr_which^=1],FSTR_LEN,__VA_ARGS__)>=0) \
  1186.     ? (const char*)_gl.fstr_str[_gl.fstr_which] : _fstr_failure \
  1187. )
  1188.  
  1189. #define   LOCK_GLOBALS() { if(_gl.lock != nullptr) SDL_LockMutex(  _gl.lock); }
  1190. #define UNLOCK_GLOBALS() { if(_gl.lock != nullptr) SDL_UnlockMutex(_gl.lock); }
  1191.  
  1192.  
  1193.  
  1194.  
  1195.  
  1196. //in _kit_private.cpp:
  1197. const char* _pushError(const char* errortext);
  1198. bool _freeError(u32 thread_id = 0); //returns true if it found an error to free
  1199. void _freeErrors(); //also frees _gl.errors entirely
  1200.  
  1201. #define THROW_ERROR(_txt) throw _pushError(_txt)
  1202. //fstr itself is not thread safe, so let's lock globals until
  1203.  //the result of fstr has been copied to the thread errors list
  1204. #define THROW_ERRORF(...) { \
  1205.   LOCK_GLOBALS(); \
  1206.   const char* _T_EF_long_name_dont_name_anything_else_this = _pushError( fstr(__VA_ARGS__) ); \
  1207.   UNLOCK_GLOBALS(); \
  1208.   throw _T_EF_long_name_dont_name_anything_else_this; \
  1209. }
  1210.  
  1211.  
  1212.  
  1213.  
  1214.  
  1215. extern size_t numAllocations;
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221. //100ms
  1222. #define FADETOTAL_SEC (0.100f)
  1223.  
  1224. //linearly fade over the course of 10ms
  1225. #define FADEDELTA_SEC (0.010f)
  1226.  
  1227. //the most common audio clipping ends at 10-11ms after unpausing,
  1228.  //but i've seen clipping as far as ~450ms after unpausing
  1229. #define FADEDELAY_SEC (FADETOTAL_SEC - FADEDELTA_SEC)
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235. #define ERROR_ON_NEGATIVE(_funcname, _var) \
  1236.   if((_var) < 0){ THROW_ERROR(_funcname ": " #_var " < 0") }
  1237.  
  1238.  
  1239.  
  1240.  
  1241.  
  1242. }; /* namespace kit */
  1243.  
  1244. #include "_kit_opaques.hpp"
  1245.  
  1246. #endif /* _SRC__KIT_COMMON_HPP */
  1247. /******************************************************************************/
  1248. /******************************************************************************/
  1249. //"ksdl2\src\kit_sdl2\_kit_opaques.hpp":
  1250. #ifndef _SRC__KIT_OPAQUES_HPP
  1251. #define _SRC__KIT_OPAQUES_HPP
  1252.  
  1253. //THIS FILE IS NOT TO BE INCLUDED ON ITS OWN,
  1254.  //RATHER AS A PART OF "_kit_common.hpp"!!
  1255.  
  1256. /*
  1257. Opaques are included as follows: (classes without opaques are omitted)
  1258. [ ] Mutex
  1259. [ ] Thread
  1260. [ ] File
  1261. [ ] Window
  1262. [*] AudioDevice
  1263. [ ] AudioStream
  1264. */
  1265.  
  1266. namespace kit {
  1267.  
  1268.  
  1269.  
  1270.  
  1271.  
  1272. struct _AudioDeviceOpaque {
  1273.   AudioDeviceInfo* info_p;
  1274.  
  1275.   void*            buffer;
  1276.   u32         buffer_size;
  1277.  
  1278.   f32           fadeDelta;
  1279.   f32          fadeVolume;
  1280.   u32           fadeDelay;
  1281.  
  1282.   bool            fadeOut; //fade-in otherwise
  1283.   bool        noFadeDelay;
  1284.  
  1285.   bool            playing;
  1286.  
  1287.   u8  _0;
  1288.   u32 _1;
  1289. };
  1290.  
  1291.  
  1292.  
  1293.  
  1294.  
  1295. }; /* namespace kit */
  1296.  
  1297. #endif /* _SRC__KIT_OPAQUES_HPP */
  1298.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement