Advertisement
Kitomas

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

Aug 29th, 2024
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 30.78 KB | None | 0 0
  1. /******************************************************************************/
  2. /******************************************************************************/
  3. //"ksdl2\src\kit_sdl2\kit_fileio_File.cpp":
  4. #include "_kit_common.hpp"
  5.  
  6.  
  7. #define KIT_IS_INVALID_FILE (!_valid && !_constructing)
  8. #define KIT_FILE_CHECK(_funcName) \
  9.   if(KIT_IS_INVALID_FILE){ THROW_ERROR(_funcName ": invalid File");           } \
  10.   if(_file == nullptr   ){ THROW_ERROR(_funcName ": _file = nullptr");        } \
  11.   if(_closed            ){ THROW_ERROR(_funcName ": file is already closed"); }
  12.  
  13. #define FILE_PTR ((SDL_RWops*)_file)
  14. #define SIZE()                               size(FILE_PTR)
  15. #define READ(_elems, _elemSize, _elems_len)  read(FILE_PTR, _elems, _elemSize, _elems_len)
  16. #define WRITE(_elems, _elemSize, _elems_len) write(FILE_PTR, _elems, _elemSize, _elems_len)
  17. #define SEEK(_off, _whence)                  seek(FILE_PTR, _off, _whence)
  18. #define TELL()                               seek(FILE_PTR, 0, RW_SEEK_CUR)
  19. #define CLOSE()                              close(FILE_PTR)
  20.  
  21.  
  22. namespace kit {
  23.  
  24.  
  25.  
  26.  
  27.  
  28. File::File(const char* filePath, const char* mode){
  29.   _type = KIT_CLASSTYPE_FILE;
  30.  
  31.   _file = (GenOpqPtr)SDL_RWFromFile(filePath, mode);
  32.   if(_file == nullptr) THROW_ERRORF("File::File(): \"%s\"", SDL_GetError());
  33.  
  34.   _valid = true;
  35.   _constructing = false;
  36.  
  37. }
  38.  
  39.  
  40.  
  41.  
  42.  
  43. File::~File(){
  44.   if(!_valid) return;
  45.   _valid = false;
  46.  
  47.   if(_file != nullptr){
  48.     _zombie = FILE_PTR->CLOSE()<0;
  49.     _file = nullptr;
  50.   }
  51.  
  52. }
  53.  
  54.  
  55.  
  56.  
  57.  
  58. size_t File::read(void* elements, size_t elementSize, size_t elements_len){
  59.   KIT_FILE_CHECK("File::read()");
  60.  
  61.   size_t result = FILE_PTR->READ(elements, elementSize, elements_len);
  62.   if(!result && !strnCmp("Error", SDL_GetError()))
  63.     THROW_ERRORF("File::read(): \"%s\"", SDL_GetError());
  64.  
  65.   return result;
  66.  
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73. void File::write(const void* elements, size_t elementSize, size_t elements_len){
  74.   KIT_FILE_CHECK("File::write()");
  75.  
  76.   size_t result = FILE_PTR->WRITE(elements, elementSize, elements_len);
  77.   if(result < elements_len)
  78.     THROW_ERRORF("File::write(): \"%s\"", SDL_GetError());
  79.  
  80. }
  81.  
  82.  
  83.  
  84.  
  85.  
  86. size_t File::seek(s64 offset, u32 whence){
  87.   KIT_FILE_CHECK("File::seek()");
  88.  
  89.   if(whence > 2) THROW_ERROR("File::seek(): whence is invalid");
  90.  
  91.   s64 result = FILE_PTR->SEEK(offset, whence);
  92.   if(result < 0) THROW_ERRORF("File::seek(): \"%s\"", SDL_GetError());
  93.  
  94.   return (size_t)result;
  95.  
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102. void File::close(){
  103.   KIT_FILE_CHECK("File::close()");
  104.  
  105.   if(FILE_PTR->CLOSE() < 0)
  106.     THROW_ERROR("File::close(): write error occurred while flushing data");
  107.  
  108.   _closed = true;
  109.   _file   = nullptr;
  110.  
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  
  117. size_t File::size(){
  118.   KIT_FILE_CHECK("File::size()");
  119.  
  120.   s64 result = FILE_PTR->SIZE();
  121.   if(result < 0)
  122.     THROW_ERROR("File::size(): failed to get file's size");
  123.  
  124.   return (size_t)result;
  125.  
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132. }; /* namespace kit */
  133. /******************************************************************************/
  134. /******************************************************************************/
  135. //"ksdl2\src\kit_sdl2\kit_func.cpp":
  136. #include "_kit_common.hpp"
  137. #include <stdarg.h>
  138.  
  139.  
  140. namespace kit {
  141.  
  142.  
  143.  
  144.  
  145.  
  146. void Log(LogPriorityEnum priority,
  147.          const char* fmt, ...)
  148. {
  149.   //(unnecessary?)
  150.   //if(priority == 0  ||  priority > LOG_PRIORITY_CRITICAL)
  151.   //  THROW_ERROR("Log(): unknown priority");
  152.  
  153.   va_list args;
  154.   va_start(args, fmt);
  155.   SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION, (SDL_LogPriority)priority, fmt, args);
  156.   va_end(args);
  157.  
  158. }
  159.  
  160.  
  161.  
  162.  
  163.  
  164. //(len_max does not include null-terminator!)
  165. //(if !len_max, call is analogous to str(not n)len)
  166. size_t strnLen(const char* str, size_t len_max){
  167.   //if(str == nullptr) THROW_ERROR("strnLen(): str = nullptr");
  168.  
  169.   size_t len = 0;
  170.  
  171.   if(!len_max){
  172.     for(; str[len]; ++len);
  173.  
  174.   } else {
  175.     for(; str[len] && len<len_max; ++len);
  176.  
  177.   }
  178.  
  179.   return len;
  180.  
  181. }
  182.  
  183.  
  184.  
  185.  
  186. //(len_max does not include null-terminator!)
  187. //(if !len_max, call is analogous to str(not n)cmp)
  188. s32 strnCmp(const char* str_a, const char* str_b, size_t len_max){
  189.   //might uncomment these, maybe not though
  190.   //if(str_a == nullptr) THROW_ERROR("strnCmp(): str_a = nullptr");
  191.   //if(str_b == nullptr) THROW_ERROR("strnCmp(): str_b = nullptr");
  192.  
  193.   if(!len_max){
  194.     while(*str_a && (*str_a == *str_b))
  195.       ++str_a, ++str_b;
  196.  
  197.   } else {
  198.     --len_max;
  199.     while(*str_a && (*str_a == *str_b) && len_max)
  200.       ++str_a, ++str_b, --len_max;
  201.  
  202.   }
  203.  
  204.   return (*(const u8*)str_a) - (*(const u8*)str_b);
  205.  
  206. }
  207.  
  208.  
  209.  
  210.  
  211. void strnCpy(char* str_dst, const char* str_src, size_t len_max){
  212.   if(!len_max){
  213.     while((*str_dst++ = *str_src++));
  214.  
  215.   } else {
  216.     size_t i = 0;
  217.     while(i++ != len_max && (*str_dst++ = *str_src++));
  218.  
  219.   }
  220.  
  221.  
  222.   *str_dst = 0; //null-terminator
  223.  
  224.  
  225. }
  226.  
  227.  
  228.  
  229.  
  230.  
  231. const char* getLastSysError(){
  232.   return SDL_GetError();
  233.  
  234. }
  235.  
  236.  
  237.  
  238. void clearSysError(){
  239.   SDL_ClearError();
  240.  
  241. }
  242.  
  243.  
  244.  
  245. void freeThreadErrors(){
  246.   while(_freeError()); //frees errors of *current* thread
  247.  
  248. }
  249.  
  250.  
  251.  
  252.  
  253.  
  254. #define MSGBOX_EWI_FLAGS (MSGBOX_ERROR|MSGBOX_WARNING|MSGBOX_INFO)
  255.  
  256. void showMsgBox(const char* text, const char* title, u32 flags, Window* win){
  257.   flags &= MSGBOX_EWI_FLAGS;
  258.  
  259.   SDL_Window* win_sdl = nullptr;
  260.   if(win != nullptr){
  261.     if(KIT_GET_CLASS_TYPE(win) != KIT_CLASSTYPE_WINDOW)
  262.       THROW_ERROR("showMsgBoxSimple(): win does not point to a Window object");
  263.     win_sdl = (SDL_Window*)KIT_GET_CLASS_OPAQUE(win);
  264.   }
  265.  
  266.   if(SDL_ShowSimpleMessageBox(flags, title, text, win_sdl)<0)
  267.     THROW_ERRORF("showMsgBoxSimple(): \"%s\"", SDL_GetError());
  268.  
  269. }
  270.  
  271.  
  272.  
  273.  
  274.  
  275. bool clipboardHasText(){
  276.   return SDL_HasClipboardText();
  277.  
  278. }
  279.  
  280.  
  281.  
  282. char* clipboardGetText(){
  283.   char* txt = SDL_GetClipboardText();
  284.  
  285.   //txt will be an empty string on error
  286.   if(!txt[0]){
  287.     //according to the SDL2 wiki, you need to
  288.      //free the returned pointer, even on error
  289.     SDL_free(txt);
  290.  
  291.     THROW_ERRORF("clipboardGetText(): \"%s\"", SDL_GetError());
  292.  
  293.   }
  294.  
  295.   ++numAllocations; //a subsequent call to memory::free should undo this
  296.  
  297.   return txt;
  298.  
  299. }
  300.  
  301.  
  302.  
  303. void clipboardSetText(const char* txt){
  304.   if(SDL_SetClipboardText(txt)<0)
  305.     THROW_ERRORF("clipboardSetText(): \"%s\"", SDL_GetError());
  306.  
  307. }
  308.  
  309.  
  310.  
  311.  
  312.  
  313. bool showCursor(s32 toggle){
  314.   s32 result = SDL_ShowCursor((toggle < 0) ? -1 : toggle&1);
  315.  
  316.   if(result < 0)
  317.     THROW_ERRORF("showCursor(): \"%s\"", SDL_GetError());
  318.  
  319.   return (bool)result;
  320.  
  321. }
  322.  
  323.  
  324.  
  325.  
  326.  
  327. void warpMouseGlobal(s32 x, s32 y){
  328.   if(SDL_WarpMouseGlobal(x, y) < 0)
  329.     THROW_ERRORF("warpMouseGlobal(): \"%s\"", SDL_GetError());
  330.  
  331. }
  332.  
  333.  
  334.  
  335.  
  336.  
  337. }; /* namespace kit */
  338. /******************************************************************************/
  339. /******************************************************************************/
  340. //"ksdl2\src\kit_sdl2\kit_func_convertEvent.cpp":
  341. #include "_kit_common.hpp"
  342.  
  343. #define ETYPE(_event_type) e_kit.common.type = _event_type
  344.  
  345.  
  346. namespace kit {
  347.  
  348.  
  349.  
  350.  
  351.  
  352. //returns false if that sdl event is unsupported or is otherwise unknown
  353. bool _convertEvent(SDL_Event& e_sdl, Event& e_kit){
  354.   e_kit.common.timestamp = e_sdl.common.timestamp;
  355.  
  356.  
  357.   switch(e_sdl.type){
  358.     case SDL_QUIT:  ETYPE(KEVENT_QUIT);  break;
  359.  
  360.  
  361.  
  362.     case SDL_DISPLAYEVENT:
  363.     {
  364.       switch(e_sdl.display.event){
  365.         case SDL_DISPLAYEVENT_ORIENTATION :  ETYPE(KEVENT_DISPLAY_ORIENTATION );  break;
  366.         case SDL_DISPLAYEVENT_CONNECTED   :  ETYPE(KEVENT_DISPLAY_CONNECTED   );  break;
  367.         case SDL_DISPLAYEVENT_DISCONNECTED:  ETYPE(KEVENT_DISPLAY_DISCONNECTED);  break;
  368.         case SDL_DISPLAYEVENT_MOVED       :  ETYPE(KEVENT_DISPLAY_MOVED       );  break;
  369.         default                           :  return false; //this shouldn't happen
  370.       }
  371.       e_kit.display.id   = e_sdl.display.display;
  372.       e_kit.display.data = e_sdl.display.data1;
  373.     } break;
  374.  
  375.  
  376.  
  377.     case SDL_WINDOWEVENT:
  378.     {
  379.       switch(e_sdl.window.event){
  380.         case SDL_WINDOWEVENT_SHOWN          :  ETYPE(KEVENT_WIN_SHOWN          );  break;
  381.         case SDL_WINDOWEVENT_HIDDEN         :  ETYPE(KEVENT_WIN_HIDDEN         );  break;
  382.         case SDL_WINDOWEVENT_EXPOSED        :  ETYPE(KEVENT_WIN_EXPOSED        );  break;
  383.         case SDL_WINDOWEVENT_MOVED          :  ETYPE(KEVENT_WIN_MOVED          );  break;
  384.         case SDL_WINDOWEVENT_RESIZED        :  ETYPE(KEVENT_WIN_RESIZED        );  break;
  385.         case SDL_WINDOWEVENT_SIZE_CHANGED   :  ETYPE(KEVENT_WIN_SIZE_CHANGED   );  break;
  386.         case SDL_WINDOWEVENT_MINIMIZED      :  ETYPE(KEVENT_WIN_MINIMIZED      );  break;
  387.         case SDL_WINDOWEVENT_MAXIMIZED      :  ETYPE(KEVENT_WIN_MAXIMIZED      );  break;
  388.         case SDL_WINDOWEVENT_RESTORED       :  ETYPE(KEVENT_WIN_RESTORED       );  break;
  389.         case SDL_WINDOWEVENT_ENTER          :  ETYPE(KEVENT_WIN_MFOCUS_GAINED  );  break;
  390.         case SDL_WINDOWEVENT_LEAVE          :  ETYPE(KEVENT_WIN_MFOCUS_LOST    );  break;
  391.         case SDL_WINDOWEVENT_FOCUS_GAINED   :  ETYPE(KEVENT_WIN_KFOCUS_GAINED  );  break;
  392.         case SDL_WINDOWEVENT_FOCUS_LOST     :  ETYPE(KEVENT_WIN_KFOCUS_LOST    );  break;
  393.         case SDL_WINDOWEVENT_CLOSE          :  ETYPE(KEVENT_WIN_CLOSE          );  break;
  394.         case SDL_WINDOWEVENT_TAKE_FOCUS     :  ETYPE(KEVENT_WIN_TAKE_FOCUS     );  break;
  395.         case SDL_WINDOWEVENT_HIT_TEST       :  ETYPE(KEVENT_WIN_HIT_TEST       );  break;
  396.         case SDL_WINDOWEVENT_ICCPROF_CHANGED:  ETYPE(KEVENT_WIN_ICCPROF_CHANGED);  break;
  397.         case SDL_WINDOWEVENT_DISPLAY_CHANGED:  ETYPE(KEVENT_WIN_DISPLAY_CHANGED);  break;
  398.         default                             :  return false; //this shouldn't happen
  399.       }
  400.       e_kit.win.id    = e_sdl.window.windowID;
  401.       e_kit.win.data1 = e_sdl.window.data1;
  402.       e_kit.win.data2 = e_sdl.window.data2;
  403.     } break;
  404.  
  405.  
  406.  
  407.     case SDL_KEYDOWN:  ETYPE(KEVENT_KEY_DOWN);  goto _event_key;
  408.     case SDL_KEYUP  :  ETYPE(KEVENT_KEY_UP  );       _event_key:
  409.     {
  410.       e_kit.key.window  = e_sdl.key.windowID;
  411.       e_kit.key.pressed = e_sdl.key.state == SDL_PRESSED;
  412.       e_kit.key.repeat  = e_sdl.key.repeat != 0;
  413.       e_kit.key.vkey    = e_sdl.key.keysym.sym;
  414.       e_kit.key.pkey    = e_sdl.key.keysym.scancode;
  415.       e_kit.key.kmods   = e_sdl.key.keysym.mod;
  416.     } break;
  417.  
  418.  
  419.  
  420.     //not to be confused with key events, as they are separate
  421.     case SDL_KEYMAPCHANGED:  ETYPE(KEVENT_KEYMAPCHANGED);  break;
  422.  
  423.  
  424.  
  425.     case SDL_MOUSEMOTION    :  ETYPE(KEVENT_MOUSE_MOVED);
  426.     {
  427.       e_kit.mouse.button  = e_sdl.motion.state;
  428.       e_kit.mouse.pressed = e_kit.mouse.button != 0;
  429.       e_kit.mouse.x       = e_sdl.motion.x;
  430.       e_kit.mouse.y       = e_sdl.motion.y;
  431.       e_kit.mouse.dx      = e_sdl.motion.xrel;
  432.       e_kit.mouse.dy      = e_sdl.motion.yrel;
  433.     } goto _set_mouse_common;
  434.  
  435.     case SDL_MOUSEBUTTONDOWN:  ETYPE(KEVENT_MOUSE_DOWN);  goto _event_mouse_bt;
  436.     case SDL_MOUSEBUTTONUP  :  ETYPE(KEVENT_MOUSE_UP  );       _event_mouse_bt:
  437.     {
  438.       //(turn button index into button flag using SDL_BUTTON)
  439.       e_kit.mouse.button   = SDL_BUTTON(e_sdl.button.button);
  440.       e_kit.mouse.pressed  = e_sdl.button.state == SDL_PRESSED;
  441.       e_kit.mouse.dblClick = ((MAX(1,e_sdl.button.clicks)-1)&1) > 0;
  442.       e_kit.mouse.x        = e_sdl.button.x;
  443.       e_kit.mouse.y        = e_sdl.button.y;
  444.     } goto _set_mouse_common;
  445.  
  446.     case SDL_MOUSEWHEEL     :  ETYPE(KEVENT_MOUSE_WHEEL);
  447.     {
  448.       e_kit.mouse.flipped = e_sdl.wheel.direction == SDL_MOUSEWHEEL_FLIPPED;
  449.       e_kit.mouse.x       = e_sdl.wheel.mouseX;
  450.       e_kit.mouse.y       = e_sdl.wheel.mouseY;
  451.       e_kit.mouse.dx      = e_sdl.wheel.x;
  452.       e_kit.mouse.dy      = e_sdl.wheel.y;
  453.       e_kit.mouse.pdx     = e_sdl.wheel.preciseX;
  454.       e_kit.mouse.pdy     = e_sdl.wheel.preciseY;
  455.     } //goto _set_mouse_common; //(redundant)
  456.  
  457.     _set_mouse_common: //(common to all Mouse events)
  458.     {
  459.       e_kit.mouse.window = e_sdl.motion.windowID;
  460.     } break;
  461.  
  462.  
  463.  
  464.     case SDL_JOYAXISMOTION    :  ETYPE(KEVENT_JOY_AXIS          );
  465.     {
  466.       e_kit.joy.axis.which = e_sdl.jaxis.axis;
  467.       e_kit.joy.axis.value = e_sdl.jaxis.value;
  468.     } goto _set_joy_common;
  469.  
  470.     case SDL_JOYBALLMOTION    :  ETYPE(KEVENT_JOY_TRACKBALL     );
  471.     {
  472.       e_kit.joy.trackball.which = e_sdl.jball.ball;
  473.       e_kit.joy.trackball.dx    = e_sdl.jball.xrel;
  474.       e_kit.joy.trackball.dy    = e_sdl.jball.yrel;
  475.     } goto _set_joy_common;
  476.  
  477.     case SDL_JOYHATMOTION     :  ETYPE(KEVENT_JOY_HAT           );
  478.     {
  479.       e_kit.joy.hat.which = e_sdl.jhat.hat;
  480.       e_kit.joy.hat.value = e_sdl.jhat.value;
  481.     } goto _set_joy_common;
  482.  
  483.     case SDL_JOYBUTTONDOWN    :  ETYPE(KEVENT_JOY_BUTTON_DOWN   );  goto _event_joy_bt;
  484.     case SDL_JOYBUTTONUP      :  ETYPE(KEVENT_JOY_BUTTON_UP     );       _event_joy_bt:
  485.     {
  486.       e_kit.joy.button.which   = e_sdl.jbutton.button;
  487.       e_kit.joy.button.pressed = e_sdl.jbutton.state == SDL_PRESSED;
  488.     } goto _set_joy_common;
  489.  
  490.     case SDL_JOYDEVICEADDED   :  ETYPE(KEVENT_JOY_DEVICE_ADDED  );  goto _event_joy_dv;
  491.     case SDL_JOYDEVICEREMOVED :  ETYPE(KEVENT_JOY_DEVICE_REMOVED);       _event_joy_dv:
  492.     {
  493.       e_kit.joy.device.added = e_sdl.jdevice.type == SDL_JOYDEVICEADDED;
  494.     } goto _set_joy_common;
  495.  
  496.     case SDL_JOYBATTERYUPDATED:  ETYPE(KEVENT_JOY_BATTERY       );
  497.     {
  498.       e_kit.joy.battery.level = e_sdl.jbattery.level;
  499.     } //goto _set_joy_common; //redundant
  500.  
  501.     _set_joy_common: //(common to all Joystick events)
  502.     {
  503.       e_kit.joy.id = e_sdl.jdevice.which;
  504.     } break;
  505.  
  506.  
  507.  
  508.     case SDL_CONTROLLERAXISMOTION    :  ETYPE(KEVENT_CTLR_AXIS           );
  509.     {
  510.       e_kit.ctlr.axis.which = e_sdl.caxis.axis;
  511.       e_kit.ctlr.axis.value = e_sdl.caxis.value;
  512.     } goto _set_ctlr_common;
  513.  
  514.     case SDL_CONTROLLERBUTTONDOWN    :  ETYPE(KEVENT_CTLR_BUTTON_DOWN    );  goto _event_ctlr_bt;
  515.     case SDL_CONTROLLERBUTTONUP      :  ETYPE(KEVENT_CTLR_BUTTON_UP      );       _event_ctlr_bt:
  516.     {
  517.       e_kit.ctlr.button.which   = e_sdl.cbutton.button;
  518.       e_kit.ctlr.button.pressed = e_sdl.cbutton.state == SDL_PRESSED;
  519.     } goto _set_ctlr_common;
  520.  
  521.     case SDL_CONTROLLERDEVICEADDED   :  ETYPE(KEVENT_CTLR_DEVICE_ADDED   );  goto _event_ctlr_dv;
  522.     case SDL_CONTROLLERDEVICEREMOVED :  ETYPE(KEVENT_CTLR_DEVICE_REMOVED );  goto _event_ctlr_dv;
  523.     case SDL_CONTROLLERDEVICEREMAPPED:  ETYPE(KEVENT_CTLR_DEVICE_REMAPPED);       _event_ctlr_dv:
  524.     {
  525.       e_kit.ctlr.device.subtype = KIT_SUBEVENT_ID(e_kit.type);
  526.     } goto _set_ctlr_common;
  527.  
  528.     case SDL_CONTROLLERTOUCHPADDOWN  :  ETYPE(KEVENT_CTLR_TOUCHPAD_DOWN  );  goto _event_ctlr_tp;
  529.     case SDL_CONTROLLERTOUCHPADMOTION:  ETYPE(KEVENT_CTLR_TOUCHPAD_MOVED );  goto _event_ctlr_tp;
  530.     case SDL_CONTROLLERTOUCHPADUP    :  ETYPE(KEVENT_CTLR_TOUCHPAD_UP    );       _event_ctlr_tp:
  531.     {
  532.       e_kit.ctlr.touchpad.which    = e_sdl.ctouchpad.touchpad;
  533.       e_kit.ctlr.touchpad.finger   = e_sdl.ctouchpad.finger;
  534.       e_kit.ctlr.touchpad.x        = e_sdl.ctouchpad.x;
  535.       e_kit.ctlr.touchpad.y        = e_sdl.ctouchpad.y;
  536.       e_kit.ctlr.touchpad.pressure = e_sdl.ctouchpad.pressure;
  537.     } goto _set_ctlr_common;
  538.  
  539.     case SDL_CONTROLLERSENSORUPDATE  :  ETYPE(KEVENT_CTLR_SENSOR         );
  540.     {
  541.       e_kit.ctlr.sensor.which        = e_sdl.csensor.sensor;
  542.       e_kit.ctlr.sensor.data[0]      = e_sdl.csensor.data[0];
  543.       e_kit.ctlr.sensor.data[1]      = e_sdl.csensor.data[1];
  544.       e_kit.ctlr.sensor.data[2]      = e_sdl.csensor.data[2];
  545.       e_kit.ctlr.sensor.timestamp_us = e_sdl.csensor.timestamp_us;
  546.     } //goto _set_ctlr_common; //(redundant)
  547.  
  548.     _set_ctlr_common: //(common to all GameController events)
  549.     {
  550.       e_kit.ctlr.id = e_sdl.cdevice.which;
  551.     } break;
  552.  
  553.  
  554.  
  555.     case SDL_CLIPBOARDUPDATE:  ETYPE(KEVENT_CLIPBOARDUPDATE);  break;
  556.  
  557.  
  558.  
  559.     case SDL_DROPFILE    :  ETYPE(KEVENT_DROP_FILE    );  goto _event_drop;
  560.     case SDL_DROPTEXT    :  ETYPE(KEVENT_DROP_TEXT    );  goto _event_drop;
  561.     case SDL_DROPBEGIN   :  ETYPE(KEVENT_DROP_BEGIN   );  goto _event_drop;
  562.     case SDL_DROPCOMPLETE:  ETYPE(KEVENT_DROP_COMPLETE);       _event_drop:
  563.     {
  564.       e_kit.drop.filePath = e_sdl.drop.file;
  565.       e_kit.drop.window   = e_sdl.drop.windowID;
  566.     } break;
  567.  
  568.  
  569.  
  570.     case SDL_AUDIODEVICEADDED  :  ETYPE(KEVENT_ADEV_ADDED  );  goto _event_adev;
  571.     case SDL_AUDIODEVICEREMOVED:  ETYPE(KEVENT_ADEV_REMOVED);       _event_adev:
  572.     {
  573.       e_kit.adev.id      = e_sdl.adevice.which;
  574.       e_kit.adev.isInput = e_sdl.adevice.iscapture;
  575.     } break;
  576.  
  577.  
  578.  
  579.     case SDL_RENDER_TARGETS_RESET:  ETYPE(KEVENT_RENDER_TARGETS_RESET);  break;
  580.     case SDL_RENDER_DEVICE_RESET :  ETYPE(KEVENT_RENDER_DEVICE_RESET );  break;
  581.  
  582.  
  583.  
  584.     case SDL_USEREVENT:  ETYPE(KEVENT_USER);
  585.     {
  586.       e_kit.user.window = e_sdl.user.windowID;
  587.       e_kit.user.id     = e_sdl.user.code;
  588.       e_kit.user.data1  = e_sdl.user.data1;
  589.       e_kit.user.data2  = e_sdl.user.data2;
  590.     } break;
  591.  
  592.  
  593.  
  594.     //event is not recognized, so return false
  595.     default: return false;
  596.  
  597.   }
  598.  
  599.  
  600.   return true;
  601.  
  602. }
  603.  
  604.  
  605.  
  606.  
  607.  
  608. }; /* namespace kit */
  609. /******************************************************************************/
  610. /******************************************************************************/
  611. //"ksdl2\src\kit_sdl2\kit_func_CPUCapabilities.cpp":
  612. #include "_kit_common.hpp"
  613.  
  614.  
  615. namespace kit {
  616.  
  617.  
  618.  
  619.  
  620.  
  621. u32 getCPUCapabilities(){
  622.   if(!_gl.CPUCapabilities){
  623.     if(SDL_HasRDTSC  ()) _gl.CPUCapabilities |= CPU_HAS_RDTSC;
  624.     if(SDL_HasAltiVec()) _gl.CPUCapabilities |= CPU_HAS_ALTIVEC;
  625.     if(SDL_HasMMX    ()) _gl.CPUCapabilities |= CPU_HAS_MMX;
  626.     if(SDL_Has3DNow  ()) _gl.CPUCapabilities |= CPU_HAS_3DNOW;
  627.     if(SDL_HasSSE    ()) _gl.CPUCapabilities |= CPU_HAS_SSE;
  628.     if(SDL_HasSSE2   ()) _gl.CPUCapabilities |= CPU_HAS_SSE2;
  629.     if(SDL_HasSSE3   ()) _gl.CPUCapabilities |= CPU_HAS_SSE3;
  630.     if(SDL_HasSSE41  ()) _gl.CPUCapabilities |= CPU_HAS_SSE41;
  631.     if(SDL_HasSSE42  ()) _gl.CPUCapabilities |= CPU_HAS_SSE42;
  632.     if(SDL_HasAVX    ()) _gl.CPUCapabilities |= CPU_HAS_AVX;
  633.     if(SDL_HasAVX512F()) _gl.CPUCapabilities |= CPU_HAS_AVX512F;
  634.     if(SDL_HasARMSIMD()) _gl.CPUCapabilities |= CPU_HAS_ARMSIMD;
  635.     if(SDL_HasNEON   ()) _gl.CPUCapabilities |= CPU_HAS_NEON;
  636.     if(SDL_HasLSX    ()) _gl.CPUCapabilities |= CPU_HAS_LSX;
  637.     if(SDL_HasLASX   ()) _gl.CPUCapabilities |= CPU_HAS_LASX;
  638.   }
  639.  
  640.   return _gl.CPUCapabilities;
  641.  
  642. }
  643.  
  644.  
  645.  
  646.  
  647.  
  648. u32 getCPUCacheLineSize(){
  649.   return SDL_GetCPUCacheLineSize();
  650.  
  651. }
  652.  
  653.  
  654.  
  655.  
  656.  
  657. u32 getNumLogicalCPUCores(){
  658.   return SDL_GetCPUCount();
  659.  
  660. }
  661.  
  662.  
  663.  
  664.  
  665.  
  666. }; /* namespace kit */
  667. /******************************************************************************/
  668. /******************************************************************************/
  669. //"ksdl2\src\kit_sdl2\kit_func_init-quit.cpp":
  670. #include "_kit_common.hpp"
  671.  
  672.  
  673. namespace kit {
  674.  
  675.  
  676. _globalStates _gl;
  677. const char _fstr_failure[] = "(FSTR FAILED)";
  678.  
  679. const char _boolStr_false[] = "false";
  680. const char _boolStr_true[]  = "true";
  681.  
  682.  
  683.  
  684.  
  685.  
  686. #define FLAG_SET(_flag) (flags&(_flag))
  687.  
  688.  
  689.  
  690.  
  691.  
  692. #define SHOULD_INIT(_flag, _member) (FLAG_SET(_flag) && !_gl.init._member)
  693.  
  694. #define INIT_CHECK(_sdl_flag) { \
  695.   if(SDL_InitSubSystem(_sdl_flag)<0){ THROW_ERRORF("initSubsystems(): \"%s\"", SDL_GetError()); } }
  696.  
  697. #define KINIT_JOYSTICK_IMPLICIT (0x40)
  698. #define KINIT_EVENTS_IMPLICIT   (0x80)
  699.  
  700. void initSubsystems(u32 flags){
  701.   SDL_version ver;
  702.   SDL_GetVersion(&ver);
  703.  
  704.   if(ver.major != SDL_MAJOR_VERSION){
  705.     THROW_ERRORF("kit_sdl%u requires SDL%u, not SDL%u",
  706.                       SDL_MAJOR_VERSION, SDL_MAJOR_VERSION, ver.major);
  707.   }
  708.  
  709.   if(ver.minor < SDL_MINOR_VERSION) {
  710.     THROW_ERRORF("SDL%u version must be >= %u.%u.x, not %u.%u.x",
  711.                       SDL_MAJOR_VERSION,
  712.                       SDL_MAJOR_VERSION, SDL_MINOR_VERSION,
  713.                       ver.major, ver.minor);
  714.   }
  715.  
  716.   //just warn instead of error if patch level is less than what it should be
  717.   if(ver.patch < SDL_PATCHLEVEL  &&  ver.minor == SDL_MINOR_VERSION  &&
  718.      !_gl.patch_warning_disabled)
  719.   {
  720.     _log("\b\b\b\b\b\bWARN: SDL%u patch version should be >= x.x.%u, not x.x.%u",
  721.          SDL_MAJOR_VERSION, SDL_PATCHLEVEL, ver.patch);
  722.   }
  723.  
  724.  
  725.   //disable these flags just in case the user set them for whatever reason
  726.   flags &= ~(KINIT_JOYSTICK_IMPLICIT|KINIT_EVENTS_IMPLICIT);
  727.  
  728.  
  729.  
  730.   if(SHOULD_INIT(KINIT_TIMER, timer)){
  731.     INIT_CHECK(SDL_INIT_TIMER);
  732.     _gl.init.timer = true;
  733.   }
  734.  
  735.  
  736.  
  737.   if(SHOULD_INIT(KINIT_AUDIO, audio)){
  738.     INIT_CHECK(SDL_INIT_AUDIO);
  739.     _gl.init.audio = true;
  740.   }
  741.  
  742.  
  743.  
  744.   if(SHOULD_INIT(KINIT_VIDEO, video)){
  745.     INIT_CHECK(SDL_INIT_VIDEO);
  746.     _gl.init.video = true;
  747.     flags |= KINIT_EVENTS_IMPLICIT; //indicate that events should auto-init
  748.   }
  749.  
  750.  
  751.  
  752.   if(SHOULD_INIT(KINIT_GAMECONTROLLER, gamecontroller)){
  753.     INIT_CHECK(SDL_INIT_GAMECONTROLLER);
  754.     _gl.init.gamecontroller = true;
  755.     flags |= KINIT_JOYSTICK_IMPLICIT; //indicate that joystick should auto-init
  756.   }
  757.  
  758.  
  759.  
  760.   if(SHOULD_INIT(KINIT_JOYSTICK|KINIT_JOYSTICK_IMPLICIT, joystick)){
  761.     if(FLAG_SET(KINIT_JOYSTICK)){
  762.       INIT_CHECK(SDL_INIT_JOYSTICK);
  763.       _gl.init.joystick_sdl = true;
  764.     }
  765.     _gl.init.joystick = true;
  766.     flags |= KINIT_EVENTS_IMPLICIT; //indicate that events should auto-init
  767.   }
  768.  
  769.  
  770.  
  771.   if(SHOULD_INIT(KINIT_EVENTS|KINIT_EVENTS_IMPLICIT, events)){
  772.     if(FLAG_SET(KINIT_EVENTS)){
  773.       INIT_CHECK(SDL_INIT_EVENTS);
  774.       _gl.init.events_sdl = true;
  775.     }
  776.     _gl.init.events = true;
  777.   }
  778.  
  779.  
  780.  
  781.   bool errors_just_created = false;
  782.   if(flags && _gl.errors == nullptr){
  783.     _gl.errors_len = 1;
  784.     _gl.errors     = (Error*)memory::alloc( sizeof(Error) * _gl.errors_len );
  785.  
  786.     if(_gl.errors == nullptr){
  787.       _gl.errors_len = 0;
  788.       THROW_ERROR("initSubsystems(): failed to allocate internal memory");
  789.  
  790.     }
  791.  
  792.     errors_just_created = true;
  793.  
  794.     memory::set(_gl.errors, 0, sizeof(Error) * _gl.errors_len );
  795.  
  796.   }
  797.  
  798.  
  799.  
  800.   if(_gl.init_value && _gl.lock == nullptr){
  801.     _gl.lock = SDL_CreateMutex();
  802.     if(_gl.lock == nullptr){
  803.       if(errors_just_created) _freeErrors();
  804.       THROW_ERROR("initSubsystems(): failed to create internal mutex");
  805.     }
  806.   }
  807.  
  808. }
  809.  
  810.  
  811.  
  812.  
  813.  
  814. #define SHOULD_QUIT(_flag, _member) (FLAG_SET(_flag) && _gl.init._member)
  815. #define QUIT_SUB(_flag) SDL_QuitSubSystem(_flag)
  816.  
  817. void quitSubsystems(u32 flags){
  818.   flags &= ~(KINIT_JOYSTICK_IMPLICIT|KINIT_EVENTS_IMPLICIT);
  819.  
  820.  
  821.   if(SHOULD_QUIT(KINIT_TIMER, timer)){
  822.     QUIT_SUB(SDL_INIT_TIMER);
  823.     _gl.init.timer = false;
  824.   }
  825.  
  826.  
  827.   if(SHOULD_QUIT(KINIT_AUDIO, audio)){
  828.     QUIT_SUB(SDL_INIT_AUDIO);
  829.     _gl.init.audio = false;
  830.   }
  831.  
  832.  
  833.   if(SHOULD_QUIT(KINIT_VIDEO, video)){
  834.     QUIT_SUB(SDL_INIT_VIDEO);
  835.     _gl.init.video = false;
  836.   }
  837.  
  838.  
  839.   if(SHOULD_QUIT(KINIT_GAMECONTROLLER, gamecontroller)){
  840.     QUIT_SUB(SDL_INIT_GAMECONTROLLER);
  841.     _gl.init.gamecontroller = false;
  842.   }
  843.  
  844.  
  845.   if(SHOULD_QUIT(KINIT_JOYSTICK, joystick)){
  846.     if(_gl.init.joystick_sdl){
  847.       QUIT_SUB(SDL_INIT_JOYSTICK);
  848.       _gl.init.joystick_sdl = false;
  849.     }
  850.     _gl.init.joystick = false;
  851.   }
  852.  
  853.  
  854.   if(SHOULD_QUIT(KINIT_EVENTS, events)){
  855.     if(_gl.init.events_sdl){
  856.       QUIT_SUB(SDL_INIT_EVENTS);
  857.       _gl.init.events_sdl = false;
  858.     }
  859.     _gl.init.events = false;
  860.   }
  861.  
  862.  
  863.  
  864.   if(!_gl.init_value){
  865.     _freeErrors();
  866.  
  867.     if(_gl.lock != nullptr){
  868.       LOCK_GLOBALS();   //to lessen the chance that the mutex is destroyed
  869.       UNLOCK_GLOBALS();  //before all the other threads unlock it
  870.  
  871.       SDL_DestroyMutex(_gl.lock);
  872.       _gl.lock = nullptr;
  873.  
  874.     }
  875.  
  876.     //apparently you still need to call SDL_Quit,
  877.      //(even if you've already closed any open subsystems)
  878.     SDL_Quit();
  879.  
  880.   }
  881.  
  882. }
  883.  
  884.  
  885.  
  886.  
  887.  
  888. }; /* namespace kit */
  889. /******************************************************************************/
  890. /******************************************************************************/
  891. //"ksdl2\src\kit_sdl2\kit_func_pollEvent.cpp":
  892. #include "_kit_common.hpp"
  893.  
  894.  
  895. namespace kit {
  896.  
  897.  
  898.  
  899.  
  900.  
  901. //found in "kit_func_convertEvent.cpp"
  902. bool _convertEvent(SDL_Event& e_sdl, Event& e_kit);
  903.  
  904.  
  905.  
  906.  
  907.  
  908. bool pollEvent(Event* event_p){
  909.   if(!_gl.init.events)
  910.     THROW_ERROR("pollEvent(): events subsystem is uninitialized");
  911.  
  912.   SDL_Event e_sdl;
  913.   Event     e_kit;
  914.   bool queueWasNotEmpty;
  915.  
  916.  
  917.  
  918.   _get_another_event:
  919.  
  920.   queueWasNotEmpty = SDL_PollEvent((event_p != nullptr) ? &e_sdl : nullptr);
  921.  
  922.   if(event_p == nullptr) return queueWasNotEmpty;
  923.   e_kit.common.timestamp = e_sdl.common.timestamp;
  924.  
  925.  
  926.  
  927.   //if queue had an event but its type was not recognized by _convertEvent,
  928.    //poll another event until either a supported one is found, or queue empties
  929.   if(queueWasNotEmpty && !_convertEvent(e_sdl, e_kit))
  930.     goto _get_another_event;
  931.  
  932.  
  933.  
  934.   *event_p = e_kit; //should be impossible for event_p to be null here
  935.  
  936.   return queueWasNotEmpty;
  937.  
  938. }
  939.  
  940.  
  941.  
  942.  
  943.  
  944. const char* getEventText(u32 type){
  945.   const char* eventName;
  946.  
  947.  
  948.   switch(type){
  949.     case KEVENT_NULL                : eventName = "KEVENT_NULL";                 break;
  950.  
  951.     case KEVENT_COMMON              : eventName = "KEVENT_COMMON";               break;
  952.  
  953.     case KEVENT_DISPLAY             : eventName = "KEVENT_DISPLAY";              break;
  954.     case KEVENT_DISPLAY_ORIENTATION : eventName = "KEVENT_DISPLAY_ORIENTATION";  break;
  955.     case KEVENT_DISPLAY_DISCONNECTED: eventName = "KEVENT_DISPLAY_DISCONNECTED"; break;
  956.     case KEVENT_DISPLAY_CONNECTED   : eventName = "KEVENT_DISPLAY_CONNECTED";    break;
  957.     case KEVENT_DISPLAY_MOVED       : eventName = "KEVENT_DISPLAY_MOVED";        break;
  958.  
  959.     case KEVENT_WIN                 : eventName = "KEVENT_WIN";                  break;
  960.     case KEVENT_WIN_EXPOSED         : eventName = "KEVENT_WIN_EXPOSED";          break;
  961.     case KEVENT_WIN_HIDDEN          : eventName = "KEVENT_WIN_HIDDEN";           break;
  962.     case KEVENT_WIN_SHOWN           : eventName = "KEVENT_WIN_SHOWN";            break;
  963.     case KEVENT_WIN_MOVED           : eventName = "KEVENT_WIN_MOVED";            break;
  964.     case KEVENT_WIN_RESIZED         : eventName = "KEVENT_WIN_RESIZED";          break;
  965.     case KEVENT_WIN_SIZE_CHANGED    : eventName = "KEVENT_WIN_SIZE_CHANGED";     break;
  966.     case KEVENT_WIN_RESTORED        : eventName = "KEVENT_WIN_RESTORED";         break;
  967.     case KEVENT_WIN_MINIMIZED       : eventName = "KEVENT_WIN_MINIMIZED";        break;
  968.     case KEVENT_WIN_MAXIMIZED       : eventName = "KEVENT_WIN_MAXIMIZED";        break;
  969.     case KEVENT_WIN_MFOCUS_LOST     : eventName = "KEVENT_WIN_MFOCUS_LOST";      break;
  970.     case KEVENT_WIN_MFOCUS_GAINED   : eventName = "KEVENT_WIN_MFOCUS_GAINED";    break;
  971.     case KEVENT_WIN_KFOCUS_LOST     : eventName = "KEVENT_WIN_KFOCUS_LOST";      break;
  972.     case KEVENT_WIN_KFOCUS_GAINED   : eventName = "KEVENT_WIN_KFOCUS_GAINED";    break;
  973.     case KEVENT_WIN_CLOSE           : eventName = "KEVENT_WIN_CLOSE";            break;
  974.     case KEVENT_WIN_TAKE_FOCUS      : eventName = "KEVENT_WIN_TAKE_FOCUS";       break;
  975.     case KEVENT_WIN_HIT_TEST        : eventName = "KEVENT_WIN_HIT_TEST";         break;
  976.     case KEVENT_WIN_ICCPROF_CHANGED : eventName = "KEVENT_WIN_ICCPROF_CHANGED";  break;
  977.     case KEVENT_WIN_DISPLAY_CHANGED : eventName = "KEVENT_WIN_DISPLAY_CHANGED";  break;
  978.  
  979.     case KEVENT_KEY                 : eventName = "KEVENT_KEY";                  break;
  980.     case KEVENT_KEY_DOWN            : eventName = "KEVENT_KEY_DOWN";             break;
  981.     case KEVENT_KEY_UP              : eventName = "KEVENT_KEY_UP";               break;
  982.  
  983.     case KEVENT_KEYMAPCHANGED       : eventName = "KEVENT_KEYMAPCHANGED";        break;
  984.  
  985.     case KEVENT_MOUSE               : eventName = "KEVENT_MOUSE";                break;
  986.     case KEVENT_MOUSE_MOVED         : eventName = "KEVENT_MOUSE_MOVED";          break;
  987.     case KEVENT_MOUSE_UP            : eventName = "KEVENT_MOUSE_UP";             break;
  988.     case KEVENT_MOUSE_DOWN          : eventName = "KEVENT_MOUSE_DOWN";           break;
  989.     case KEVENT_MOUSE_WHEEL         : eventName = "KEVENT_MOUSE_WHEEL";          break;
  990.  
  991.     case KEVENT_JOY                 : eventName = "KEVENT_JOY";                  break;
  992.     case KEVENT_JOY_AXIS            : eventName = "KEVENT_JOY_AXIS";             break;
  993.     case KEVENT_JOY_TRACKBALL       : eventName = "KEVENT_JOY_TRACKBALL";        break;
  994.     case KEVENT_JOY_HAT             : eventName = "KEVENT_JOY_HAT";              break;
  995.     case KEVENT_JOY_BUTTON_UP       : eventName = "KEVENT_JOY_BUTTON_UP";        break;
  996.     case KEVENT_JOY_BUTTON_DOWN     : eventName = "KEVENT_JOY_BUTTON_DOWN";      break;
  997.     case KEVENT_JOY_DEVICE_REMOVED  : eventName = "KEVENT_JOY_DEVICE_REMOVED";   break;
  998.     case KEVENT_JOY_DEVICE_ADDED    : eventName = "KEVENT_JOY_DEVICE_ADDED";     break;
  999.     case KEVENT_JOY_BATTERY         : eventName = "KEVENT_JOY_BATTERY";          break;
  1000.  
  1001.     case KEVENT_CTLR                : eventName = "KEVENT_CTLR";                 break;
  1002.     case KEVENT_CTLR_AXIS           : eventName = "KEVENT_CTLR_AXIS";            break;
  1003.     case KEVENT_CTLR_BUTTON_UP      : eventName = "KEVENT_CTLR_BUTTON_UP";       break;
  1004.     case KEVENT_CTLR_BUTTON_DOWN    : eventName = "KEVENT_CTLR_BUTTON_DOWN";     break;
  1005.     case KEVENT_CTLR_DEVICE_REMOVED : eventName = "KEVENT_CTLR_DEVICE_REMOVED";  break;
  1006.     case KEVENT_CTLR_DEVICE_ADDED   : eventName = "KEVENT_CTLR_DEVICE_ADDED";    break;
  1007.     case KEVENT_CTLR_DEVICE_REMAPPED: eventName = "KEVENT_CTLR_DEVICE_REMAPPED"; break;
  1008.     case KEVENT_CTLR_TOUCHPAD_UP    : eventName = "KEVENT_CTLR_TOUCHPAD_UP";     break;
  1009.     case KEVENT_CTLR_TOUCHPAD_DOWN  : eventName = "KEVENT_CTLR_TOUCHPAD_DOWN";   break;
  1010.     case KEVENT_CTLR_TOUCHPAD_MOVED : eventName = "KEVENT_CTLR_TOUCHPAD_MOVED";  break;
  1011.     case KEVENT_CTLR_SENSOR         : eventName = "KEVENT_CTLR_SENSOR";          break;
  1012.  
  1013.     case KEVENT_ADEV                : eventName = "KEVENT_ADEV";                 break;
  1014.     case KEVENT_ADEV_ADDED          : eventName = "KEVENT_ADEV_ADDED";           break;
  1015.     case KEVENT_ADEV_REMOVED        : eventName = "KEVENT_ADEV_REMOVED";         break;
  1016.  
  1017.     case KEVENT_DROP                : eventName = "KEVENT_DROP";                 break;
  1018.     case KEVENT_DROP_FILE           : eventName = "KEVENT_DROP_FILE";            break;
  1019.     case KEVENT_DROP_TEXT           : eventName = "KEVENT_DROP_TEXT";            break;
  1020.     case KEVENT_DROP_BEGIN          : eventName = "KEVENT_DROP_BEGIN";           break;
  1021.     case KEVENT_DROP_COMPLETE       : eventName = "KEVENT_DROP_COMPLETE";        break;
  1022.  
  1023.     case KEVENT_QUIT                : eventName = "KEVENT_QUIT";                 break;
  1024.  
  1025.     case KEVENT_USER                : eventName = "KEVENT_USER";                 break;
  1026.  
  1027.     case KEVENT_RENDER              : eventName = "KEVENT_RENDER";               break;
  1028.     case KEVENT_RENDER_TARGETS_RESET: eventName = "KEVENT_RENDER_TARGETS_RESET"; break;
  1029.     case KEVENT_RENDER_DEVICE_RESET : eventName = "KEVENT_RENDER_DEVICE_RESET "; break;
  1030.  
  1031.     case KEVENT_CLIPBOARDUPDATE     : eventName = "KEVENT_CLIPBOARDUPDATE";      break;
  1032.  
  1033.     default                         : eventName = "KEVENT_UNKNOWN";
  1034.   }
  1035.  
  1036.  
  1037.   return eventName;
  1038.  
  1039. }
  1040.  
  1041.  
  1042.  
  1043.  
  1044.  
  1045. }; /* namespace kit */
  1046.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement