Advertisement
Kitomas

ksdl2 work for 2024-08-02 (5/5)

Aug 2nd, 2024
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 24.74 KB | None | 0 0
  1. /******************************************************************************/
  2. /******************************************************************************/
  3. //"ksdl2\include\kit\_misc_fileio.hpp":
  4. #ifndef _INC__MISC_FILEIO_HPP
  5. #define _INC__MISC_FILEIO_HPP
  6.  
  7. #include "commondef.hpp"
  8.  
  9.  
  10. namespace kit {
  11.  
  12.  
  13.  
  14.  
  15.  
  16. union BinaryData_magic {
  17.   u8   n8;
  18.   u16  n16;
  19.   u32  n32;
  20.   u64  n64;
  21.   char str[8];
  22.   //(str may not be null-terminated!)
  23. };
  24.  
  25.  
  26.  
  27. class BinaryData {
  28.   u32          _type;
  29.   bool        _valid = false;
  30.   bool _constructing = true;
  31.   u16     _padding16;
  32.   char*        _data = nullptr;
  33.   size_t   _data_len = 0;
  34.  
  35.  
  36. public:
  37.  
  38.   //wacky
  39.   BinaryData_magic** const magic =
  40.     (BinaryData_magic**)&this->_data;
  41.  
  42.  
  43.   BinaryData(const char* filePath);
  44.  
  45.   //this will allocate dataSize bytes for _data, before copying data to _data
  46.    //(it is safe to pass nullptr to data, though that would make the class
  47.    // instance sorta equivalent to a garbage collected memory::alloc())
  48.   BinaryData(const void* data, size_t data_len);
  49.  
  50.   ~BinaryData();
  51.  
  52.  
  53.   inline bool isValid()       { return _valid;        }
  54.   inline bool isConstructing(){ return _constructing; }
  55.  
  56.   char*  getData(){ return _data;     }
  57.   size_t getSize(){ return _data_len; }
  58.  
  59. };
  60.  
  61.  
  62.  
  63.  
  64.  
  65. #define KIT_FILE_SEEK_SET 0
  66. #define KIT_FILE_SEEK_CUR 1
  67. #define KIT_FILE_SEEK_END 2
  68.  
  69.  
  70.  
  71. class File { //16B
  72.   u32          _type;
  73.   bool        _valid = false;
  74.   bool _constructing = true;
  75.   bool       _closed = false;
  76.   bool       _zombie = false;
  77.   GenOpqPtr    _file = nullptr;
  78.  
  79.  
  80. public:
  81.  
  82.   File(const char* filePath, const char* mode);
  83.  
  84.   ~File();
  85.  
  86.  
  87.   inline bool isValid()       { return _valid;        }
  88.   inline bool isConstructing(){ return _constructing; }
  89.   inline bool isClosed()      { return _closed;       }
  90.  
  91.  
  92.   //WARNING: make sure to call clearSysError() before starting any
  93.    //string of calls to read(), otherwise an error might be thrown
  94.    //if the thread's previously set error was equal to "Error"
  95.  
  96.   //returns number of elements read (or 0 if EOF)
  97.   size_t read(void* elements, size_t elementSize, size_t elements_len);
  98.   //returns number of bytes read (or 0 if EOF)
  99.   inline size_t read(void* data, size_t dataSize){ return read(data, 1, dataSize); }
  100.  
  101.   void write(const void* elements, size_t elementSize, size_t elements_len);
  102.   inline void write(const void* data, size_t dataSize){ write(data, 1, dataSize); }
  103.  
  104.   //seek to offset relative of whence (one of KIT_FILE_SEEK_<SET/CUR/END>)
  105.    //returns new absolute offset of data stream
  106.   size_t seek(s64 offset, u32 whence);
  107.  
  108.   //returns current file offset
  109.   inline size_t tell(){ return seek(0, KIT_FILE_SEEK_CUR); }
  110.  
  111.   //closes file handle
  112.    //(file is considered invalid after this point!)
  113.   void close();
  114.  
  115.   //returns size of file, in bytes
  116.   size_t size();
  117.  
  118. };
  119.  
  120.  
  121.  
  122.  
  123.  
  124. namespace fileio {
  125.  
  126.  
  127.  
  128.  
  129.  
  130. bool exists(const char* filePath);
  131.  
  132.  
  133. size_t size(const char* filePath);
  134.  
  135.  
  136. void remove(const char* filePath);
  137.  
  138.  
  139.  
  140.  
  141.  
  142. //heap memory is allocated here,
  143.  //and should be freed with memory::free()
  144.  //(also, while *dataSize_p won't be populated with resulting
  145.  // filesize if dataSize_p is nullptr, it won't actually error)
  146. void* readAll(const char* filePath, size_t* dataSize_p);
  147.  
  148.  
  149. //writes to a binary file from a buffer
  150. void writeAll(const char* filePath, const void* data,
  151.               size_t dataSize, bool append = false);
  152.  
  153.  
  154.  
  155.  
  156.  
  157. }; /* namespace fileio */
  158.  
  159. }; /* namespace kit */
  160.  
  161. #endif /* _INC__MISC_FILEIO_HPP */
  162. /******************************************************************************/
  163. /******************************************************************************/
  164. //"ksdl2\include\kit\_misc_func.hpp":
  165. #ifndef _INC__MISC_FUNC_HPP
  166. #define _INC__MISC_FUNC_HPP
  167.  
  168. #include "commondef.hpp"
  169.  
  170.  
  171. namespace kit {
  172.  
  173.  
  174.  
  175.  
  176.  
  177. enum InitFlagsEnum {
  178.   KINIT_TIMER          = 0x01,
  179.   KINIT_AUDIO          = 0x02,
  180.   KINIT_VIDEO          = 0x04,
  181.   KINIT_JOYSTICK       = 0x08,
  182.   KINIT_GAMECONTROLLER = 0x10,
  183.   KINIT_EVENTS         = 0x20,
  184.  
  185.   KINIT_EVERYTHING     = 0x3F,
  186. };
  187.  
  188.  
  189.  
  190. #define KIT_INIT_ALL() (kit::initSubsystems(KINIT_EVERYTHING))
  191. #define KIT_QUIT_ALL() (kit::quitSubsystems(KINIT_EVERYTHING))
  192.  
  193. //these should only be called from the main thread, only after
  194.  //guaranteeing that no other threads are currently running
  195. void initSubsystems(u32 flags);
  196. void quitSubsystems(u32 flags);
  197. //(quitting with KINIT_EVERYTHING is allowed even if
  198.  //there are no currently active subsystems)
  199.  
  200.  
  201.  
  202.  
  203.  
  204. //(len_max does not include null-terminator!)
  205. //(if !len_max, call is analogous to str(not n)len)
  206. size_t strnLen(const char* str, size_t len_max = 0);
  207.  
  208.  
  209.  
  210. //(mostly) yoinked from stackoverflow :D
  211.  //(len_max does not include null-terminator!)
  212.  //(if !len_max, call is analogous to str(not n)cmp)
  213. s32 strnCmp(const char* str_a, const char* str_b, size_t len_max = 0);
  214.  
  215.  
  216.  
  217.  
  218.  
  219. const char* getLastSysError();
  220. void clearSysError();
  221.  
  222.  
  223.  
  224. //this must be called whenever a const char* exception is caught,
  225.  //only after that Error has been handled by the user
  226.  //(as in, the pointer should be considered invalid after exiting this function!)
  227. //also, a call of quitSubsystem(KINIT_EVERYTHING)
  228.  //will free all thread errors automatically
  229. //also also, mixups might occur if another thread error is pushed before
  230.  //a call to freeThreadError() has the chance to free the previous one
  231.  //on the same thread, resulting in the wrong reference (not an invalid
  232.  //error string, just not the right one) being accessed
  233. void freeThreadError();
  234.  
  235.  
  236.  
  237.  
  238.  
  239. u32 getNumLogicalCPUCores(); //(might be > core count if hyperthreading is enabled)
  240. u32 getCPUCacheLineSize(); //in bytes (L1 cache specifically)
  241.  
  242.  
  243.  
  244.  
  245.  
  246. enum MessageBoxFlagsEnum {
  247.   MSGBOX_ERROR    = 0x0010,
  248.   MSGBOX_WARNING  = 0x0020,
  249.   MSGBOX_INFO     = 0x0040,
  250.   MSGBOX_BTNS_L2R = 0x0080, //buttons placed left-to-right (showMsgBoxEx only)
  251.   MSGBOX_BTNS_R2L = 0x0100, //buttons placed right-to-left (showMsgBoxEx only)
  252. };
  253.  
  254.  
  255.  
  256. class Window; //forward declaration
  257. void showMsgBox(const char* text = nullptr, const char* title = nullptr,
  258.                 u32 flags = MSGBOX_INFO, Window* win = nullptr);
  259.  
  260.  
  261. //showMsgBoxEx is TBD (i don't want to deal with SDL's message boxes right now)
  262.  
  263.  
  264.  
  265.  
  266.  
  267. union Event; //forward declaration
  268. //returns false if there were no events in queue
  269.  //(if event_p is left as nullptr, the next event will not be dequeued)
  270.  //(also, only call this function in the thread that set the video mode,
  271.  // which is usually the main thread!)
  272. bool pollEvent(Event* event_p = nullptr); //requires events subsystem
  273. const char* getEventText(u32 type); //only useful in debug build
  274.  
  275.  
  276.  
  277.  
  278.  
  279. //(clipboard text uses utf-8 encoding)
  280. bool  clipboardHasText();
  281. char* clipboardGetText(); //(allocates memory; returned ptr must be memory::free'd)
  282. void  clipboardSetText(const char* txt);
  283.  
  284.  
  285.  
  286.  
  287.  
  288. bool showCursor(s32 toggle); //boolean, or -1 to query state without changing
  289.  
  290.  
  291.  
  292.  
  293.  
  294. void warpMouseGlobal(s32 x, s32 y);
  295.  
  296.  
  297.  
  298.  
  299.  
  300. }; /* namespace kit */
  301.  
  302. #endif /* _INC__MISC_FUNC_HPP */
  303. /******************************************************************************/
  304. /******************************************************************************/
  305. //"ksdl2\include\kit\_misc_memory.hpp":
  306. #ifndef _INC__MISC_MEMORY_HPP
  307. #define _INC__MISC_MEMORY_HPP
  308.  
  309. #include "commondef.hpp"
  310.  
  311.  
  312. namespace kit {
  313.  
  314.  
  315. namespace memory {
  316.  
  317.  
  318.  
  319.  
  320.  
  321. void* alloc(size_t size);
  322.  
  323. //this version of free is especially useful, because it's safe to pass nullptr to!
  324.  //this rule goes for both ptr_p and *ptr_p
  325. void free(void* ptr_p);
  326.  
  327. void* realloc(void* ptr_p, size_t newSize);
  328.  
  329. //^^(for both free and realloc, a void** declared in function as void*,
  330.  //  so you don't need to explicitly cast it to void**)
  331.  
  332.  
  333.  
  334. //same as alloc and free, now with exception throwing!
  335. void* alloc2(size_t size);
  336.  
  337. void free2(void* ptr_p); //(unlike free, this will throw if nullptr is given)
  338.  
  339. void* realloc2(void* ptr_p, size_t newSize);
  340.  
  341.  
  342.  
  343. size_t getNumAllocations();
  344.  
  345.  
  346.  
  347.  
  348.  
  349. void* set(void* ptr, s32 value, size_t size);
  350.  
  351. //throws exceptions, unlike set
  352. void* set2(void* ptr, s32 value, size_t size);
  353.  
  354.  
  355.  
  356.  
  357.  
  358. void* copy(void* destination, const void* source, size_t size);
  359.  
  360. //throws exceptions, unlike copy
  361. void* copy2(void* destination, const void* source, size_t size);
  362.  
  363.  
  364.  
  365.  
  366.  
  367. }; /* namespace memory */
  368.  
  369.  
  370. }; /* namespace kit */
  371.  
  372. #endif /* _INC__MISC_MEMORY_HPP */
  373. /******************************************************************************/
  374. /******************************************************************************/
  375. //"ksdl2\include\kit\_misc_Mutex.hpp":
  376. #ifndef _INC__MISC_MUTEX_HPP
  377. #define _INC__MISC_MUTEX_HPP
  378.  
  379. #include "commondef.hpp"
  380.  
  381.  
  382. namespace kit {
  383.  
  384.  
  385.  
  386.  
  387.  
  388. //this mutex is non-blocking within the same thread!
  389.  //(that means you can lock multiple times in one thread
  390.  // as long as you unlock it the same number of times)
  391. class Mutex { //16B; does not require an active subsystem
  392.   u32          _type;
  393.   bool        _valid = false;
  394.   bool _constructing = true;
  395.   u16     _padding16;
  396.   GenOpqPtr _mutex_p = nullptr;
  397.  
  398.  
  399. public:
  400.  
  401.   Mutex();
  402.  
  403.   ~Mutex();
  404.  
  405.  
  406.   inline bool isValid()       { return _valid;        }
  407.   inline bool isConstructing(){ return _constructing; }
  408.  
  409.  
  410.   void lock(bool locked = true);
  411.   inline void unlock(){ lock(false); }
  412.   bool tryLock(); //returns true if locked successfully
  413.  
  414. };
  415.  
  416.  
  417.  
  418.  
  419.  
  420. }; /* namespace kit */
  421.  
  422. #endif /* _INC__MISC_MUTEX_HPP */
  423. /******************************************************************************/
  424. /******************************************************************************/
  425. //"ksdl2\include\kit\_misc_Thread.hpp":
  426. #ifndef _INC__MISC_THREAD_HPP
  427. #define _INC__MISC_THREAD_HPP
  428.  
  429. #include "commondef.hpp"
  430.  
  431.  
  432. namespace kit {
  433.  
  434.  
  435.  
  436.  
  437.  
  438. enum ThreadPriorityEnum {
  439.   THREAD_LOW     = -1,
  440.   THREAD_NORMAL  =  0,
  441.   THREAD_HIGH    =  1,
  442.   THREAD_HIGHEST =  2, //for time critical tasks
  443. };
  444.  
  445.  
  446.  
  447.  
  448.  
  449. //if a thread function throws an unhandled exception,
  450.  //it will be outputted to stdout and/or stderr
  451. typedef s32 (*ThreadFunction)(void* userdata);
  452.  
  453.  
  454.  
  455.  
  456.  
  457. //returns whether or not priority of current thread was successfully set
  458.  //may fail if setting priority requires elevated privileges (or at least when
  459.  //setting a higher priority), if it's even allowed at all
  460. bool Thread_setPriority(s32 priority, bool throwOnFailure = false);
  461.  
  462.  
  463.  
  464.  
  465.  
  466. class Thread { //16B; does not require an active subsystem
  467.   u32              _type;
  468.   bool            _valid = false;
  469.   bool     _constructing = true;
  470.   bool _waitInDestructor = false; //otherwise thread auto-detaches
  471.   bool         _detached = false;
  472.   GenOpqPtr      _thread = nullptr;
  473.   //(real opaque is separate from Thread object and is very temporary)
  474.  
  475.  
  476. public:
  477.  
  478.   //if waitInDestructor is false, the Thread object will not
  479.    //wait for func to return inside the Thread's destructor
  480.    //(stackSize = 0 to use whatever default stack size is)
  481.   Thread(ThreadFunction func, void* userdata = nullptr,
  482.          bool waitInDestructor = false, size_t stackSize = 0,
  483.          const char* threadName = nullptr);
  484.          //(threadName should be UTF-8 string if not nullptr)
  485.  
  486.   ~Thread();
  487.  
  488.  
  489.   inline bool isValid()       { return _valid;        }
  490.   inline bool isConstructing(){ return _constructing; }
  491.   inline bool isDetached()    { return _detached;     }
  492.  
  493.   u32 getID();
  494.   //returns UTF-8 string, or nullptr if thread doesn't have a name
  495.   const char* getName();
  496.  
  497.  
  498.   //returns result of func (if detached, returns 0 immediately)
  499.    //(afaik, SDL_WaitThread doesn't time out, so be careful!)
  500.   s32 waitUntilDone();
  501.  
  502.  
  503.   //make thread automatically clean up after returning
  504.    //(otherwise, you'll need to call waitUntilDone() to do that)
  505.   void detach();
  506.  
  507. };
  508.  
  509.  
  510.  
  511.  
  512.  
  513. }; /* namespace kit */
  514.  
  515. #endif /* _INC__MISC_THREAD_HPP */
  516. /******************************************************************************/
  517. /******************************************************************************/
  518. //"ksdl2\include\kit\_misc_time.hpp":
  519. #ifndef _INC__MISC_TIME_HPP
  520. #define _INC__MISC_TIME_HPP
  521.  
  522. #include "commondef.hpp"
  523.  
  524.  
  525. namespace kit {
  526.  
  527.  
  528.  
  529.  
  530.  
  531. /* from the SDL2 wiki: "
  532. The callback function is passed the current timer interval
  533. and returns the next timer interval. If the returned value
  534. is the same as the one passed in, the periodic alarm continues,
  535. otherwise a new alarm is scheduled. If the callback returns 0,
  536. the periodic alarm is cancelled. "*/
  537. typedef u32 (*timerCallback)(u32 interval_ms, void* userdata);
  538.  
  539. typedef s32 timerID;
  540.  
  541.  
  542.  
  543. //(these 2 functions require the timer subsystem)
  544.  
  545. timerID timerAdd(timerCallback func, u32 interval_ms, void* userdata = nullptr);
  546.  
  547. void timerRemove(timerID id);
  548.  
  549.  
  550.  
  551.  
  552.  
  553. namespace time {
  554.  
  555.  
  556.  
  557.  
  558.  
  559. //returns current tick count, and how
  560.  //many ticks are in a second, respectively
  561. u64 getTicks();
  562. u64 getTicksPerSecond();
  563.  
  564.  
  565. //returns # of milliseconds since init
  566. u64 getMS();
  567. u32 getMS_32();
  568.  
  569.  
  570. //returns # of seconds since init
  571.  //(uses integer milliseconds internally;
  572.  // do "(f64)getTicks()/getTicksPerSecond()"
  573.  // if more accuracy is needed)
  574. f64 getSeconds();
  575.  
  576.  
  577.  
  578.  
  579.  
  580. //will wait AT LEAST the given number
  581.  //of milliseconds (possibly longer)
  582. void sleep(u32 milliseconds);
  583.  
  584.  
  585.  
  586.  
  587.  
  588. }; /* namespace time */
  589.  
  590. }; /* namespace kit */
  591.  
  592. #endif /* _INC__MISC_TIME_HPP */
  593. /******************************************************************************/
  594. /******************************************************************************/
  595. //"ksdl2\include\kit\_video_Window.hpp":
  596. #ifndef _INC__VIDEO_WINDOW_HPP
  597. #define _INC__VIDEO_WINDOW_HPP
  598.  
  599. #include "commondef.hpp"
  600. #include "_misc_Mutex.hpp"
  601.  
  602.  
  603. namespace kit {
  604.  
  605.  
  606.  
  607.  
  608.  
  609. enum WindowPositionEnum {
  610.   WINPOS_UNDEFINED = 0x1FFF0000u,
  611.   WINPOS_CENTERED  = 0x2FFF0000u,
  612. };
  613.  
  614.  
  615.  
  616. enum WindowFlagEnum {
  617.   WINFLAG_FULLSCREEN         = 0x00000001, //fullscreen window
  618.   WINFLAG_OPENGL             = 0x00000002, //window usable with an opengl context
  619.   WINFLAG_HIDDEN             = 0x00000008, //window is not visible
  620.   WINFLAG_BORDERLESS         = 0x00000010, //no window decoration
  621.   WINFLAG_RESIZABLE          = 0x00000020, //window can be resized
  622.   WINFLAG_MINIMIZED          = 0x00000040, //window is minimized
  623.   WINFLAG_MAXIMIZED          = 0x00000080, //window is maximized
  624.   WINFLAG_INPUT_GRABBED      = 0x00000100, //window has grabbed input focus
  625.   WINFLAG_FULLSCREEN_DESKTOP = 0x00001000|WINFLAG_FULLSCREEN, //fs window at desktop resolution
  626.   WINFLAG_VULKAN             = 0x10000000, //window usable with a vulkan instance
  627. };
  628.  
  629.  
  630.  
  631.  
  632.  
  633. struct DisplayMode { //16B
  634.   u32 pixelFmt;
  635.   s32 w, h;
  636.   s32 refreshRate;
  637. };
  638.  
  639.  
  640.  
  641.  
  642.  
  643. class Window { //32B (16B+_lock)
  644.   u32          _type;
  645.   bool        _valid = false;
  646.   bool _constructing = true;
  647.   u16     _padding16;
  648.   GenOpqPtr     _win = nullptr;
  649.   Mutex        _lock;
  650.  
  651.  
  652. public:
  653.  
  654.   Window(const char* winTitle,
  655.          s32 winWidth, s32 winHeight,
  656.          u32 winFlags = 0,
  657.          s32 winX = WINPOS_UNDEFINED,
  658.          s32 winY = WINPOS_UNDEFINED);
  659.  
  660.   ~Window();
  661.  
  662.  
  663.   inline bool isValid()       { return _valid;        }
  664.   inline bool isConstructing(){ return _constructing; }
  665.  
  666.   inline void   lock(bool locked = true){ _lock.lock(locked); }
  667.   inline void unlock()                  { _lock.lock(false ); }
  668.  
  669.  
  670.   bool         hasSurface();
  671.   u32          getPixelFormat(); //returns pixel format used by window's surface
  672.   u32          getID();
  673.   u32          getDisplayIndex();
  674.   void*        getUserdata(const char* name);
  675.   DisplayMode  getDisplayMode(); //get display mode when visible at fullscreen
  676.   const char*  getTitle();
  677.   shape::point getSize();
  678.   u32          getFlags();
  679.   f32          getOpacity();
  680.   shape::point getMinSize();
  681.   shape::point getMaxSize();
  682.   shape::point getPosition();
  683.   bool         getGrab();
  684.   bool         getKeyboardGrab();
  685.   bool         getMouseGrab();
  686.   shape::rect  getMouseGrabRect();
  687.   f32          getBrightness(); //the window's gamma multiplier, specifically
  688.  
  689.  
  690.   //returns previous userdata pointer; name "Window class" is forbidden (used internally)
  691.   void* setUserdata(const char* name, void* userdata);
  692.   void  setDisplayMode(const DisplayMode* mode); //nullptr to use win's dims & desktop's fmt & refresh rate
  693.   void  setTitle(const char* title);
  694.   void  setSize(s32 width, s32 height);
  695.   void  setVisibility(bool visible);
  696.   void  setFullscreen(u32 mode); //0,1,2 = disabled, enabled, enabled @ desktop resolution
  697.   void  setResizable(bool enable);
  698.   void  setBordered(bool enable);
  699.   void  setAlwaysOnTop(bool enable);
  700.   void  setOpacity(f32 opacity); //0.0f -> 1.0f
  701.   void  setMinSize(s32 minWidth, s32 minHeight);
  702.   void  setMaxSize(s32 maxWidth, s32 maxHeight);
  703.   void  setPosition(s32 x, s32 y);
  704.   void  setGrab(bool enable); //also includes keyboard if SDL_HINT_GRAB_KEYBOARD is set (tbd: replace macro)
  705.   void  setKeyboardGrab(bool enable);
  706.   void  setMouseGrab(bool enable); //confines mouse cursor to window
  707.   void  setMouseGrabRect(const shape::rect* rect = nullptr); //nullptr to use entire window
  708.   void  setBrightness(f32 brightness); //gamma multiplier; 0.0f -> 1.0f
  709.  
  710.  
  711.   void warpMouse(s32 x, s32 y); //relative to and inside the window's bounds
  712.   void minimize();
  713.   void maximize();
  714.   void restore();
  715.   void raise(); //also makes window have input focus
  716.  
  717. };
  718.  
  719.  
  720.  
  721.  
  722.  
  723. //returns window if input is grabbed; nullptr otherwise
  724. Window* getGrabbedWindow();
  725.  
  726.  
  727.  
  728.  
  729.  
  730. }; /* namespace kit */
  731.  
  732. #endif /* _INC__VIDEO_WINDOW_HPP */
  733. /******************************************************************************/
  734. /******************************************************************************/
  735. //"ksdl2\src\kit_sdl2\_kit_common.hpp":
  736. #ifndef _SRC__KIT_COMMON_HPP
  737. #define _SRC__KIT_COMMON_HPP
  738.  
  739. /* notes/todo:
  740. use fstr to give more coherent error throws
  741. throw if trying to create object using an uninitialized subsystem
  742. specify which functions/methods can throw exceptions
  743. when throwing in a constructor, make sure to deallocate any memory
  744.  
  745. SDL_AddEventWatch
  746. SDL_DelEventWatch
  747. SDL_FlushEvent/Events
  748. SDL_HasEvent/Events
  749. SDL_JoystickEventState
  750. SDL_PeepEvents
  751. SDL_PushEvent
  752. SDL_RegisterEvents
  753. SDL_WaitEvent/EventTimeout
  754. SDL_StartTextInput (??)
  755.  
  756. what's going on with lodepng's encoder's file stuff?
  757. maybe put a mutex on numAllocations
  758. allow people to disable zeroing out audio buffers
  759.  
  760. */
  761.  
  762.  
  763. #include <SDL2/SDL.h>
  764.  
  765.  
  766. #ifdef _DEBUG
  767. #define _log(...) SDL_Log(__VA_ARGS__)
  768. #define loghere SDL_Log("line %4i: (%s)",__LINE__,__FILE__);
  769. #define _getnumallocs SDL_Log("%4i: # OF ALLOCATIONS = %u", __LINE__, (u32)memory::getNumAllocations());
  770.  
  771. #else
  772. #define _log(...)
  773. #define loghere
  774. #define _getnumallocs
  775.  
  776. #endif
  777.  
  778. /*
  779. #define DISABLE_WARNING(_name) _Pragma(#_name)
  780. #define DISABLE_WARNING_PUSH(_name) _Pragma("GCC diagnostic push") DISABLE_WARNING(_name)
  781. #define DISABLE_WARNING_POP _Pragma("GCC diagnostic pop")
  782. */
  783.  
  784.  
  785. #include <kit/all.hpp>
  786.  
  787. int snprintf(char*, size_t, const char*, ...); //only used by fstr
  788. int remove(const char *);                      //only used by fileio::remove
  789.  
  790.  
  791.  
  792.  
  793.  
  794. //class type ID macros
  795. #define KIT_OPAQUE_PRESENT (0x80000000)
  796. #define KIT_IS_OPAQUE_PRESENT(_type) ( ((_type)&KIT_OPAQUE_PRESENT) != 0 )
  797.  
  798. #define KIT_CLASSTYPE_NULL        (0x0000                     )
  799. #define KIT_CLASSTYPE_MUTEX       (0x0001 | KIT_OPAQUE_PRESENT)
  800. #define KIT_CLASSTYPE_THREAD      (0x0002 | KIT_OPAQUE_PRESENT)
  801. #define KIT_CLASSTYPE_BINARYDATA  (0x0003                     )
  802. #define KIT_CLASSTYPE_FILE        (0x0004 | KIT_OPAQUE_PRESENT)
  803. #define KIT_CLASSTYPE_WINDOW      (0x0005 | KIT_OPAQUE_PRESENT)
  804. //#define KIT_CLASSTYPE_SURFACE     (0x0006 | KIT_OPAQUE_PRESENT)
  805. //#define KIT_CLASSTYPE_TEXTURE     (0x0007 | KIT_OPAQUE_PRESENT)
  806. #define KIT_CLASSTYPE_AUDIODEVICE (0x0008 | KIT_OPAQUE_PRESENT)
  807. #define KIT_CLASSTYPE_AUDIOSTREAM (0x0009 | KIT_OPAQUE_PRESENT)
  808.  
  809.  
  810.  
  811.  
  812.  
  813. namespace kit {
  814.  
  815.  
  816.  
  817.  
  818.  
  819. #define KIT_GET_CLASS_TYPE(_ptr)         ( ((kit::_commonClassValues*)(_ptr))->type         )
  820. #define KIT_GET_CLASS_VALID(_ptr)        ( ((kit::_commonClassValues*)(_ptr))->valid        )
  821. #define KIT_GET_CLASS_CONSTRUCTING(_ptr) ( ((kit::_commonClassValues*)(_ptr))->constructing )
  822. #define KIT_GET_CLASS_DATA(_ptr)         ( ((kit::_commonClassValues*)(_ptr))->data         )
  823. #define KIT_GET_CLASS_OPAQUE(_ptr)       ( ((kit::_commonClassValues*)(_ptr))->opq          )
  824.  
  825. #define KIT_IS_CLASS_TYPE(_ptr,_type)   ( KIT_GET_CLASS_TYPE(_ptr) == (_type) )
  826. #define KIT_IS_CLASS_VALID(_ptr)        ( KIT_GET_CLASS_VALID(_ptr) != 0 )
  827. #define KIT_IS_CLASS_CONSTRUCTING(_ptr) ( KIT_GET_CLASS_CONSTRUCTING(_ptr) != 0 )
  828.  
  829. struct _commonClassValues { //8-16B (depending on KIT_OPAQUE_PRESENT)
  830.   u32          type;
  831.   bool        valid; //'is object fully and successfully constructed?'
  832.   bool constructing; //'is object currently inside constructor?'
  833.   //^^this is useful for if public class functions are called inside the
  834.    //constructor itself, but the valid flag is not set to true yet
  835.   u16          data; //purpose varies by class type; unused by some (or it's split into 2 u8)
  836.   GenOpqPtr     opq; //nonexistent if type lacks the KIT_OPAQUE_PRESENT flag
  837. };
  838.  
  839.  
  840.  
  841.  
  842.  
  843. union Error { //16B
  844.   struct {
  845.     const char* _txt;
  846.     u32   _thread_id;
  847.     bool       _heap;
  848.     u8     _padding8;
  849.     u16   _padding16;
  850.   };
  851.  
  852.   struct { u64 _0, _1; };
  853.  
  854.  
  855.   Error() : _txt(nullptr), _thread_id(0), _heap(false) {}
  856.  
  857.   Error(const char* txt, bool heap = false)
  858.     : _txt(txt), _thread_id(SDL_GetThreadID(nullptr)), _heap(heap) {}
  859.  
  860.  
  861.   inline const char* text(){
  862.     return (_txt != nullptr) ? _txt : "(ERROR TEXT IS NULLPTR)"; }
  863.  
  864. };
  865.  
  866.  
  867.  
  868.  
  869.  
  870. #define FSTR_LEN ((size_t)1024)
  871.  
  872. struct _globalStates {
  873.   //(make sure to not call fstr from more than 2 threads at once!)
  874.   char fstr_str[2][FSTR_LEN];
  875.   s32 fstr_which = 0;
  876.  
  877.   //normally, the user is warned if the SDL version's patch level
  878.    //is <= the one kit_sdl2 was made for, but this can be disabled
  879.   bool patch_warning_disabled;
  880.   char _[3];
  881.  
  882.   u64 performanceFreq = 0; //how many performance counter units per second
  883.  
  884.   //(haptic & sensor subsystems are not included)
  885.   union {
  886.     u64 init_value;
  887.     struct {
  888.       bool timer;
  889.       bool audio;
  890.       bool video;          //auto-inits events
  891.       bool gamecontroller; //auto-inits joystick
  892.       bool joystick;       //auto-inits events
  893.       bool events;
  894.       bool joystick_sdl; //same as events_sdl, except with the joystick subsystem
  895.       bool events_sdl;   //'were events EXPLICITLY initialized? (as in, not auto-init)'
  896.     } init;
  897.   };
  898.  
  899.   Error* errors     = nullptr;
  900.   size_t errors_len = 0;
  901.  
  902.   SDL_mutex* lock = nullptr;
  903.  
  904. };
  905.  
  906. //found in "kit_func_init-quit.cpp"
  907. extern _globalStates _gl; //gl as in global, not opengl
  908. extern const char _fstr_failure[]; // = "(FSTR FAILED)"
  909.  
  910. //(will complain about undefined sequencing if you put fstr in a call to fstr,
  911.  //due to the of the "^=" assignment operator)
  912. #define fstr(...) ( \
  913.   (snprintf(_gl.fstr_str[_gl.fstr_which^=1],FSTR_LEN,__VA_ARGS__)>=0) \
  914.     ? (const char*)_gl.fstr_str[_gl.fstr_which] : _fstr_failure \
  915. )
  916.  
  917. #define   LOCK_GLOBALS() { if(_gl.lock != nullptr) SDL_LockMutex(  _gl.lock); }
  918. #define UNLOCK_GLOBALS() { if(_gl.lock != nullptr) SDL_UnlockMutex(_gl.lock); }
  919.  
  920.  
  921.  
  922.  
  923.  
  924. //in _kit_private.cpp:
  925. const char* _pushError(const char* errortext);
  926. void _freeError(u32 thread_id = 0);
  927. void _freeErrors(); //also frees _gl.errors entirely
  928.  
  929. #define PUSH_ERROR(_txt) _pushError(_txt)
  930. #define PUSH_ERRORF(...) _pushError( fstr(__VA_ARGS__) )
  931.  
  932.  
  933.  
  934.  
  935.  
  936. extern size_t numAllocations;
  937.  
  938.  
  939.  
  940.  
  941.  
  942. //100ms
  943. #define FADETOTAL_SEC (0.100f)
  944.  
  945. //linearly fade over the course of 10ms
  946. #define FADEDELTA_SEC (0.010f)
  947.  
  948. //the most common audio clipping ends at 10-11ms after unpausing,
  949.  //but i've seen clipping as far as ~450ms after unpausing
  950. #define FADEDELAY_SEC (FADETOTAL_SEC - FADEDELTA_SEC)
  951.  
  952.  
  953.  
  954.  
  955.  
  956. }; /* namespace kit */
  957.  
  958. #include "_kit_opaques.hpp"
  959.  
  960. #endif /* _SRC__KIT_COMMON_HPP */
  961. /******************************************************************************/
  962. /******************************************************************************/
  963. //"ksdl2\src\kit_sdl2\_kit_opaques.hpp":
  964. #ifndef _SRC__KIT_OPAQUES_HPP
  965. #define _SRC__KIT_OPAQUES_HPP
  966.  
  967. //THIS FILE IS NOT TO BE INCLUDED ON ITS OWN,
  968.  //RATHER AS A PART OF "_kit_common.hpp"!!
  969.  
  970. /*
  971. Opaques are included as follows: (classes without opaques are omitted)
  972. [ ] Mutex
  973. [ ] Thread
  974. [ ] File
  975. [ ] Window
  976. [*] AudioDevice
  977. */
  978.  
  979. namespace kit {
  980.  
  981.  
  982.  
  983.  
  984.  
  985. struct _AudioDeviceOpaque {
  986.   AudioDeviceInfo* info_p;
  987.  
  988.   void*            buffer;
  989.   u32         buffer_size;
  990.  
  991.   f32           fadeDelta;
  992.   f32          fadeVolume;
  993.   u32           fadeDelay;
  994.  
  995.   bool            fadeOut; //fade-in otherwise
  996.   bool        noFadeDelay;
  997.  
  998.   bool            playing;
  999.  
  1000.   u8  _0;
  1001.   u32 _1;
  1002. };
  1003.  
  1004.  
  1005.  
  1006.  
  1007.  
  1008. }; /* namespace kit */
  1009.  
  1010. #endif /* _SRC__KIT_OPAQUES_HPP */
  1011.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement