Advertisement
Kitomas

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

Nov 22nd, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 34.46 KB | None | 0 0
  1. /******************************************************************************/
  2. /******************************************************************************/
  3. //"2024-11-21\src\kit_sdl2\kit_Surface_LoadAllTypes.cpp":
  4. //hopefully by putting this into another object file, you can have the choice
  5.  //of only using one image type while only linking code of that specific type
  6.  
  7. #include "_kit_common.hpp"
  8.  
  9.  
  10. namespace kit {
  11.  
  12.  
  13.  
  14.  
  15.  
  16. //file signatures
  17. #define MAGIC_BMP 0x4D42             // = "BM"
  18. #define MAGIC_QOI 0x66696F71         // = "qoif"
  19. #define MAGIC_PNG 0x0A1A0A0D474E5089 // = "\x89PNG\x0D\x0A\x1A\x0A"
  20.  
  21.  
  22.  
  23.  
  24.  
  25. Surface::Surface(const char* filePath){
  26.   if(filePath == nullptr)
  27.     THROW_ERROR("Surface::Surface(any file format): filePath = nullptr");
  28.  
  29.   if(!fileio::exists(filePath)){
  30.     THROW_ERRORF("Surface::Surface(any file format): \"%s\" doesn't exist",
  31.                       filePath);
  32.   }
  33.  
  34.  
  35.  
  36.   u64 magic; //file signature, up to a maximum of 8 bytes
  37.  
  38.   if(  File(filePath,"rb").read(&magic, sizeof(magic))  <  sizeof(magic)  )
  39.   {
  40.     THROW_ERRORF(
  41.             "Surface::Surface(any file format): failed to read 8 bytes of file \"%s\"",
  42.             filePath
  43.           );
  44.   }
  45.  
  46.  
  47.  
  48.   SurfaceLoaderCallback callback;
  49.  
  50.   //test for file signatures in order of their byte count
  51.   if(     (magic&            0xffff) == MAGIC_BMP) callback = nullptr;
  52.   else if((magic&        0xffffffff) == MAGIC_QOI) callback = SurfaceLoadQOI;
  53.   else if((magic&0xffffffffffffffff) == MAGIC_PNG) callback = SurfaceLoadPNG;
  54.   else THROW_ERROR( "Surface::Surface(any file format): unsupported file format");
  55.  
  56.  
  57.   //(auto-increments numAllocations)
  58.   _construct_file(filePath, callback, "Surface::Surface(any file format)");
  59.  
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
  66. }; /* namespace kit */
  67. /******************************************************************************/
  68. /******************************************************************************/
  69. //"2024-11-21\src\kit_sdl2\kit_Surface_LoadPNG.cpp":
  70. #include "_kit_common.hpp"
  71.  
  72.  
  73. #include "../lodepng/lodepng.hpp"
  74.  
  75.  
  76. namespace kit {
  77.  
  78.  
  79. using namespace png;
  80.  
  81.  
  82.  
  83.  
  84.  
  85. void* SurfaceLoadPNG(const char* filePath, u32& fmt_out,
  86.                      u31& width_out, u31& height_out)
  87. {
  88.   if(!fileio::exists(filePath))
  89.     THROW_ERRORF("SurfaceLoadPNG(): file \"%s\" doesn't exist", filePath);
  90.  
  91.  
  92.   colors::ABGR* pixels;
  93.   u32           width_in;
  94.   u32           height_in;
  95.  
  96.   u32 err = lodepng_decode32_file((u8**)&pixels,&width_in,&height_in,filePath);
  97.  
  98.   if(err) THROW_ERRORF("SurfaceLoadPNG(): \"%s\"", lodepng_error_text(err));
  99.  
  100.   if(width_in > KIT_S32_MAX)
  101.     THROW_ERROR("SurfaceLoadPNG(): image width > KIT_S32_MAX");
  102.  
  103.   if(height_in > KIT_S32_MAX)
  104.     THROW_ERROR("SurfaceLoadPNG(): image height > KIT_S32_MAX");
  105.  
  106.  
  107.  
  108.   //analyze the input pixels to determine the pixel format (out of 2)
  109.    //(PIXELFMT_<ABGR8888, BGR888>)
  110.  
  111.   size_t numPixels = width_in * height_in;
  112.  
  113.   bool uses_alpha = false; //will remain false if all pixels are opaque
  114.  
  115.   for(size_t i=0; i<numPixels; ++i)
  116.     uses_alpha |= pixels[i].a < 255;
  117.  
  118.  
  119.  
  120.   fmt_out    = (uses_alpha) ? PIXELFMT_ABGR8888 : PIXELFMT_BGR888;
  121.   width_out  = width_in;
  122.   height_out = height_in;
  123.  
  124.   return pixels;
  125.  
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132. }; /* namespace kit */
  133. /******************************************************************************/
  134. /******************************************************************************/
  135. //"2024-11-21\src\kit_sdl2\kit_Surface_LoadSaveQOI.cpp":
  136. #include "_kit_common.hpp"
  137.  
  138. #define QOI_NO_STDIO
  139. #define QOI_MALLOC(sz) kit::memory::alloc(sz)
  140. #define QOI_FREE(p)    kit::memory::free(&p)
  141. #define QOI_ZEROARR(a) kit::memory::set((a),0,sizeof(a))
  142.  
  143.  
  144. namespace kit {
  145.  
  146.  
  147. namespace qoi {
  148.  
  149. #define QOI_IMPLEMENTATION
  150. #include "../qoi/qoi.h"
  151.  
  152. }; /* namespace qoi */
  153.  
  154. //hopefully putting it in its own namespace will prevent any user naming conflicts
  155.  //(maybe it wouldn't have been a problem anyway, idk)
  156. using namespace qoi;
  157.  
  158.  
  159.  
  160.  
  161.  
  162. //returns pixel data in the form of colors::ABGR, or nullptr on failure
  163.  //(alpha may be unused if format is BGR888)
  164. //if successful, *desc is filled with the image's non-pixel data info
  165. //set channels to 0 to use default value found in image's file data
  166. //result should be freed after use
  167.  
  168. //void *qoi::qoi_decode(const void *data, int size, qoi_desc *desc, int channels);
  169.  
  170.  
  171. //data is in the form of colors::ABGR
  172.  //(alpha may be unused if format is BGR888)
  173. //returns encoded qoi data, or nullptr on failure
  174. //desc is used as an input, unlike qoi_decode
  175. //if successful, out_len is set to the size of the encoded data, in bytes
  176. //result should be freed after use
  177.  
  178. //void *qoi::qoi_encode(const void *data, const qoi_desc *desc, int *out_len);
  179.  
  180.  
  181.  
  182.  
  183.  
  184. #define QOI_SIG 0x66696F71 // = "qoif"
  185.  
  186. void* SurfaceLoadQOI(const char* filePath, u32& format_out,
  187.                      u31& width_out, u31& height_out)
  188. {
  189.   if(!fileio::exists(filePath))
  190.     THROW_ERRORF("SurfaceLoadQOI(): file \"%s\" doesn't exist", filePath);
  191.  
  192.   BinaryData _fileData(filePath);
  193.  
  194.  
  195.  
  196.   s32   fileSize = ( s32 )_fileData.getSize();
  197.   void* fileData = (void*)_fileData.getData(); //the ACTUAL file data
  198.  
  199.   if(_fileData.getSize() > KIT_S32_MAX)
  200.     THROW_ERROR("SurfaceLoadQOI(): file is too large for a QOI (>2GiB)");
  201.  
  202.   if(fileSize < 14)
  203.     THROW_ERROR("SurfaceLoadQOI(): file is too small for a QOI (<14B)");
  204.  
  205.   if((*(u32*)fileData) != QOI_SIG)
  206.     THROW_ERROR("SurfaceLoadQOI(): file is not a QOI image");
  207.  
  208.  
  209.  
  210.   qoi_desc desc;
  211.  
  212.   void* _pixels = (colors::ABGR*)qoi_decode(fileData, fileSize, &desc, 0);
  213.  
  214.   if(_pixels == nullptr)
  215.     THROW_ERROR("SurfaceLoadQOI(): failed to decode QOI");
  216.  
  217.   if(desc.channels < 3  ||  desc.channels > 4){
  218.     memory::free(&_pixels);
  219.     THROW_ERROR("SurfaceLoadQOI(): color channels must be either 3 or 4");
  220.   }
  221.  
  222.   if(desc.width > KIT_S32_MAX){
  223.     memory::free(&_pixels);
  224.     THROW_ERROR("SurfaceLoadQOI(): image width cannot be >= 2^31 pixels");
  225.   }
  226.  
  227.   if(desc.height > KIT_S32_MAX){
  228.     memory::free(&_pixels);
  229.     THROW_ERROR("SurfaceLoadQOI(): image height cannot be >= 2^31 pixels");
  230.   }
  231.  
  232.  
  233.  
  234.   //the only difference between ABGR8888 and BGR888 is that
  235.    //BGR888 ignores the high byte that's usually used for the alpha channel
  236.    //(both have a 32-bit width)
  237.   format_out = (desc.channels == 4) ? PIXELFMT_ABGR8888 : PIXELFMT_BGR888;
  238.   width_out  = desc.width;
  239.   height_out = desc.height;
  240.  
  241.  
  242.  
  243.   if(desc.channels == 4){
  244.     //_pixels can be interpreted as a colors::ABGR*; return as-is
  245.     return _pixels;
  246.  
  247.  
  248.   } else { //desc.channels == 3
  249.     //_pixels is made of 24-bit rgb triplets without padding,
  250.      //so the pixel data needs to be converted to colors::ABGR
  251.      //(where the .a component is always 255, AKA fully opaque)
  252.     colors::BGR* pixels_in = (colors::BGR*)_pixels;
  253.  
  254.     size_t numPixels = desc.width * desc.height;
  255.  
  256.     colors::ABGR* pixels_out = (colors::ABGR*)memory::alloc(numPixels * sizeof(colors::ABGR));
  257.  
  258.     if(pixels_out == nullptr){
  259.       memory::free(&pixels_in);
  260.       THROW_ERROR("SurfaceLoadQOI(): failed to allocate memory for pixel data");
  261.  
  262.     }
  263.  
  264.  
  265.     //copy color data accordingly
  266.     for(size_t i=0; i<numPixels; ++i){
  267.       pixels_out[i].r = pixels_in[i].r;
  268.       pixels_out[i].g = pixels_in[i].g;
  269.       pixels_out[i].b = pixels_in[i].b;
  270.       pixels_out[i].a = 255;
  271.  
  272.     }
  273.  
  274.  
  275.     memory::free(&pixels_in);
  276.  
  277.     return pixels_out;
  278.  
  279.   }
  280.  
  281. }
  282.  
  283.  
  284.  
  285.  
  286.  
  287. void SurfaceSaveQOI(const char* filePath, Surface& surface_in){
  288.   shape::point size_in = surface_in.getSize();
  289.  
  290.   u32 format_in = surface_in.getPixelFmt();
  291.   u32 width_in  = size_in.x;
  292.   u32 height_in = size_in.y;
  293.   bool alpha_in = KIT_ISPIXELFORMAT_ALPHA(format_in);
  294.  
  295.  
  296.   //create temporary surface of format PIXELFMT_ABGR8888
  297.   Surface surface_out(surface_in, PIXELFMT_ABGR8888);
  298.  
  299.   //(doesn't need to be freed, since it's freed when surface_out is destroyed)
  300.   colors::ABGR* pixels_in = (colors::ABGR*)surface_out.getPixelData();
  301.  
  302.   qoi_desc desc_out;
  303.   desc_out.width      = width_in;
  304.   desc_out.height     = height_in;
  305.   desc_out.channels   = (alpha_in) ? 4 : 3;
  306.   desc_out.colorspace = 0;
  307.  
  308.  
  309.  
  310.   //pixels_in can be fed directly to the output if no conversion is needed
  311.   void* pixels_out = pixels_in;
  312.   bool  intermediate_used = false;
  313.  
  314.  
  315.   //only make an intermediate buffer if unpadded rgb triplets are required,
  316.    //since they're 24-bits in length, not 32
  317.   if(!alpha_in){
  318.     size_t numPixels  = width_in * height_in;
  319.     pixels_out        = memory::alloc(numPixels * desc_out.channels);
  320.     intermediate_used = true;
  321.  
  322.     if(pixels_out == nullptr)
  323.       THROW_ERROR("SurfaceSaveQOI(): failed to allocate for intermediate buffer");
  324.  
  325.  
  326.     //convert ABGR to BGR
  327.     colors::BGR* _pixels_out = (colors::BGR*)pixels_out;
  328.  
  329.     for(size_t i=0; i<numPixels; ++i){
  330.       _pixels_out[i].r = pixels_in[i].r;
  331.       _pixels_out[i].g = pixels_in[i].g;
  332.       _pixels_out[i].b = pixels_in[i].b;
  333.  
  334.     }
  335.  
  336.  
  337.   }
  338.  
  339.  
  340.  
  341.   int fileSize;
  342.   //the point of this is that encoded_data should auto-free upon throwing
  343.   memory::Wrapper encoded_data( qoi_encode(pixels_out, &desc_out, &fileSize) );
  344.  
  345.   //pixels_out should be freed (if alloc'd) before checking
  346.    //to see if qoi_encode succeeded
  347.   if(intermediate_used)
  348.     memory::free(&pixels_out);
  349.  
  350.   if(encoded_data.ptr == nullptr)
  351.     THROW_ERROR("SurfaceSaveQOI(): failed to encode QOI");
  352.  
  353.   if(fileSize < 0) //this should be impossible, but just in case
  354.     THROW_ERROR("SurfaceSaveQOI(): encoded QOI data size was negative??");
  355.  
  356.   fileio::writeAll(filePath, encoded_data.ptr, (size_t)fileSize);
  357.  
  358. }
  359.  
  360.  
  361.  
  362.  
  363.  
  364. }; /* namespace kit */
  365. /******************************************************************************/
  366. /******************************************************************************/
  367. //"2024-11-21\src\kit_sdl2\kit_Surface_SavePNG.cpp":
  368. #include "_kit_common.hpp"
  369.  
  370.  
  371. #include "../lodepng/lodepng.hpp"
  372.  
  373.  
  374. namespace kit {
  375.  
  376.  
  377. using namespace png;
  378.  
  379. //make sure to specifically use colors::BGR for this one, since i don't want padding
  380. //u32 lodepng_encode24_file(const char* filename, const u8* img, u32 w, u32 h);
  381.  
  382. //u32 lodepng_encode32_file(const char* filename, const u8* img, u32 w, u32 h);
  383.  
  384.  
  385. void SurfaceSavePNG(const char* filePath, Surface& surface_in){
  386.   shape::point size_in = surface_in.getSize();
  387.  
  388.   u32 format_in = surface_in.getPixelFmt();
  389.   u32 width_in  = size_in.x;
  390.   u32 height_in = size_in.y;
  391.   bool alpha_in = KIT_ISPIXELFORMAT_ALPHA(format_in);
  392.  
  393.  
  394.   //create temporary surface of format PIXELFMT_ABGR8888
  395.   Surface surface_out(surface_in, PIXELFMT_ABGR8888);
  396.  
  397.   //(doesn't need to be freed, since it's freed when surface_out is destroyed)
  398.   colors::ABGR* pixels_in = (colors::ABGR*)surface_out.getPixelData();
  399.  
  400.  
  401.  
  402.   u32 err = 0;
  403.   if(0){ //only entered in the event of an error related to lodepng
  404.     _throw_lodepng_error:
  405.     THROW_ERRORF("SurfaceSavePNG(): \"%s\"", lodepng_error_text(err));
  406.  
  407.   }
  408.  
  409.  
  410.  
  411.   if(alpha_in){
  412.     //pixels_in can be fed directly to the output if no conversion is needed
  413.     err = lodepng_encode32_file(filePath, (u8*)pixels_in, width_in, height_in);
  414.  
  415.     if(err) goto _throw_lodepng_error;
  416.  
  417.  
  418.   } else { //use rgb triplets
  419.     size_t numPixels = width_in * height_in;
  420.     void* pixels_out = memory::alloc(numPixels * sizeof(colors::BGR));
  421.  
  422.     if(pixels_out == nullptr)
  423.       THROW_ERROR("SurfaceSavePNG(): failed to allocate for intermediate buffer");
  424.  
  425.  
  426.     //convert ABGR to BGR
  427.     colors::BGR* _pixels_out = (colors::BGR*)pixels_out;
  428.  
  429.     for(size_t i=0; i<numPixels; ++i){
  430.       _pixels_out[i].r = pixels_in[i].r;
  431.       _pixels_out[i].g = pixels_in[i].g;
  432.       _pixels_out[i].b = pixels_in[i].b;
  433.  
  434.     }
  435.  
  436.  
  437.     err = lodepng_encode24_file(filePath, (u8*)pixels_out, width_in, height_in);
  438.  
  439.     memory::free(&pixels_out); //free BEFORE checking for error
  440.  
  441.     if(err) goto _throw_lodepng_error;
  442.  
  443.  
  444.   }
  445.  
  446. }
  447.  
  448.  
  449.  
  450.  
  451.  
  452. }; /* namespace kit */
  453. /******************************************************************************/
  454. /******************************************************************************/
  455. //"2024-11-21\src\kit_sdl2\kit_Thread.cpp":
  456. #include "_kit_common.hpp"
  457.  
  458. #define KIT_THREAD_NAME _kit_thread_name //(default thread name)
  459. #define KIT_INVALID_THREAD "invalid Thread" //this should remain a literal
  460.  
  461. #define KIT_IS_INVALID_THREAD (!_valid)
  462. #define THREAD_PTR ((SDL_Thread*)_thread) //only applicable for Thread objects
  463.  
  464.  
  465. namespace kit {
  466.  
  467. static const char _kit_thread_name[] = "kit_sdl2 thread";
  468.  
  469.  
  470.  
  471.  
  472.  
  473. //returns whether or not priority of current thread was successfully set
  474.  //may fail if setting priority requires elevated privileges (or at least when
  475.  //setting a higher priority), if it's even allowed at all
  476. bool Thread_setPriority(s32 priority, bool throwOnFailure){
  477.   SDL_ThreadPriority priority_real;
  478.  
  479.   switch(priority){
  480.     case THREAD_LOW    : priority_real = SDL_THREAD_PRIORITY_LOW;           break;
  481.     case THREAD_NORMAL : priority_real = SDL_THREAD_PRIORITY_NORMAL;        break;
  482.     case THREAD_HIGH   : priority_real = SDL_THREAD_PRIORITY_HIGH;          break;
  483.     case THREAD_HIGHEST: priority_real = SDL_THREAD_PRIORITY_TIME_CRITICAL; break;
  484.     default: THROW_ERROR("Thread_setPriority(): invalid thread priority");
  485.   }
  486.  
  487.   bool success = !(SDL_SetThreadPriority(priority_real)<0);
  488.   if(!success && throwOnFailure)
  489.     THROW_ERROR("Thread_setPriority(): failed to set thread priority");
  490.   return success;
  491.  
  492. }
  493.  
  494.  
  495.  
  496.  
  497.  
  498. struct _ThreadOpq { //24B (17 if you don't count the padding)
  499.   ThreadFunction func;
  500.   void*      userdata;
  501.   bool  outsideCreate; //set after exiting SDL_CreateThreadWithStackSize()
  502.  
  503.   u8        _padding8;
  504.   u16      _padding16;
  505.   u32      _padding32;
  506. };
  507.  
  508.  
  509. static s32 ThreadFunctionWrapper(void* data){
  510.   _ThreadOpq* _opq = (_ThreadOpq*)data;
  511.   if(_opq == nullptr) return 0; //should be impossible
  512.   while(!_opq->outsideCreate) SDL_Delay(1); //one of the solutions of all time
  513.  
  514.   _ThreadOpq opq = *_opq; //make stack copy
  515.   memory::free(&_opq);    //free original
  516.  
  517.  
  518.   s32 result = 0;
  519.  
  520.   try {
  521.     if(opq.func) result = opq.func(opq.userdata);
  522.  
  523.  
  524.   } catch(const char* errortext){
  525.     SDL_threadID thread_id   = SDL_GetThreadID(nullptr);
  526.     const char*  thread_name = SDL_GetThreadName(nullptr);
  527.     if(thread_name == nullptr) thread_name = "NAMELESS";
  528.  
  529.     kit_LogError("ON THREAD %lu (%s): %s",
  530.                  thread_id, thread_name, errortext);
  531.  
  532.     freeThreadErrors();
  533.  
  534.  
  535.   }
  536.  
  537.  
  538.   return result;
  539.  
  540. }
  541.  
  542.  
  543.  
  544.  
  545.  
  546. Thread::Thread(ThreadFunction func, void* userdata,
  547.                bool waitInDestructor, size_t stackSize,
  548.                const char* threadName)
  549. {
  550.   if(_valid) return;
  551.   _type = KIT_CLASSTYPE_THREAD;
  552.  
  553.   //handle parameters
  554.   if(func == nullptr)
  555.     THROW_ERROR("Thread::Thread(): nullptr was given as ThreadFunction");
  556.  
  557.   _waitInDestructor = waitInDestructor;
  558.  
  559.   if(threadName == nullptr) threadName = KIT_THREAD_NAME; //set to default if nullptr
  560.  
  561.  
  562.   _ThreadOpq* opq = (_ThreadOpq*)memory::alloc(sizeof(_ThreadOpq));
  563.   if(opq == nullptr)
  564.     THROW_ERROR("Thread::Thread(): failed to allocate memory for Thread's opaque struct");
  565.   memset(opq, 0, sizeof(_ThreadOpq));
  566.  
  567.   opq->func     = func;
  568.   opq->userdata = userdata;
  569.  
  570.  
  571.   //(can't use THREAD_PTR on lvalue)
  572.   _thread = (GenOpqPtr)SDL_CreateThreadWithStackSize(ThreadFunctionWrapper,
  573.                                                      threadName, stackSize, opq);
  574.   opq->outsideCreate = true;
  575.   if(THREAD_PTR == nullptr){
  576.     memory::free(&opq);
  577.     THROW_ERRORF("Thread::Thread(): \"%s\"", SDL_GetError());
  578.  
  579.   }
  580.  
  581.  
  582.   _valid = true;
  583.   _constructing = false;
  584.  
  585. }
  586.  
  587.  
  588.  
  589.  
  590.  
  591. Thread::~Thread(){
  592.   if(!_valid) return;
  593.   _valid = false;
  594.  
  595.  
  596.   if(THREAD_PTR != nullptr){
  597.     if(_waitInDestructor && !_detached)
  598.       SDL_WaitThread(THREAD_PTR, nullptr); //cleans up memory too
  599.  
  600.     else if(!_detached)
  601.       SDL_DetachThread(THREAD_PTR);
  602.  
  603.   }
  604.  
  605. }
  606.  
  607.  
  608.  
  609.  
  610.  
  611. //returns ID of current thread instead if thread is nullptr
  612. u32 Thread::getID(){
  613.   if(KIT_IS_INVALID_THREAD)
  614.     THROW_ERROR("Thread::getID(): " KIT_INVALID_THREAD);
  615.  
  616.   return SDL_GetThreadID((SDL_Thread*)_thread);
  617.  
  618. }
  619.  
  620.  
  621.  
  622. //returns UTF-8 string, or nullptr if thread doesn't have a name
  623. const char* Thread::getName(){
  624.   if(KIT_IS_INVALID_THREAD)
  625.     THROW_ERROR("Thread::getName(): " KIT_INVALID_THREAD);
  626.  
  627.   return SDL_GetThreadName(THREAD_PTR);
  628.  
  629. }
  630.  
  631.  
  632.  
  633.  
  634.  
  635. s32 Thread::waitUntilDone(){
  636.   if(KIT_IS_INVALID_THREAD)
  637.     THROW_ERROR("Thread::waitUntilDone(): " KIT_INVALID_THREAD);
  638.  
  639.   s32 returnStatus = 0;
  640.   if(!_detached) SDL_WaitThread((SDL_Thread*)_thread, &returnStatus);
  641.  
  642.   return returnStatus;
  643.  
  644. }
  645.  
  646.  
  647.  
  648.  
  649.  
  650. void Thread::detach(){
  651.   if(KIT_IS_INVALID_THREAD)
  652.     THROW_ERROR("Thread::detach(): " KIT_INVALID_THREAD);
  653.  
  654.   if(!_detached){
  655.     SDL_DetachThread(THREAD_PTR);
  656.     _detached = true;
  657.  
  658.   }
  659.  
  660. }
  661.  
  662.  
  663.  
  664.  
  665.  
  666. }; /* namespace kit */
  667. /******************************************************************************/
  668. /******************************************************************************/
  669. //"2024-11-21\src\kit_sdl2\kit_time.cpp":
  670. #include "_kit_common.hpp"
  671.  
  672.  
  673. namespace kit {
  674.  
  675.  
  676.  
  677.  
  678.  
  679. timerID timerAdd(timerCallback func, u32 interval_ms, void* userdata){
  680.   if(!_gl.init.timer) THROW_ERROR("timerAdd(): timer subsystem is uninitialized");
  681.  
  682.   timerID id_new = SDL_AddTimer(interval_ms, func, userdata);
  683.  
  684.   if(!id_new) THROW_ERRORF("timerAdd(): \"%s\"", SDL_GetError())//(";" can't be used here)
  685.   else        return id_new;
  686.  
  687. }
  688.  
  689.  
  690.  
  691.  
  692. void timerRemove(timerID id){
  693.   if(!_gl.init.timer) THROW_ERROR("timerRemove(): timer subsystem is uninitialized");
  694.  
  695.   bool success = SDL_RemoveTimer(id);
  696.  
  697.   if(!success) THROW_ERRORF("timerRemove(): \"%s\"", SDL_GetError());
  698.  
  699. }
  700.  
  701.  
  702.  
  703.  
  704.  
  705. u64 time::getTicks(){
  706.   return SDL_GetPerformanceCounter();
  707.  
  708. }
  709.  
  710.  
  711. u64 time::getTicksPerSecond(){
  712.   if(!_gl.performanceFreq)
  713.     _gl.performanceFreq = SDL_GetPerformanceFrequency();
  714.  
  715.   return _gl.performanceFreq;
  716.  
  717. }
  718.  
  719.  
  720.  
  721.  
  722. u64 time::getMS(){
  723.   return SDL_GetTicks64();
  724.  
  725. }
  726.  
  727.  
  728. u32 time::getMS_32(){
  729.   return SDL_GetTicks();
  730.  
  731. }
  732.  
  733.  
  734.  
  735.  
  736. f64 time::getSeconds(){
  737.   return (f64)SDL_GetTicks64()/1000;
  738.  
  739. }
  740.  
  741.  
  742.  
  743.  
  744.  
  745. void time::sleep(u32 milliseconds){
  746.   SDL_Delay(milliseconds);
  747.  
  748. }
  749.  
  750.  
  751.  
  752.  
  753.  
  754. }; /* namespace kit */
  755. /******************************************************************************/
  756. /******************************************************************************/
  757. //"2024-11-21\src\kit_sdl2\kit_video_func.cpp":
  758. #include "_kit_common.hpp"
  759.  
  760.  
  761. namespace kit {
  762.  
  763.  
  764.  
  765.  
  766.  
  767. //len of 21, assumming "PIXELFMT_ARGB2101010\x00" is the longest format name
  768. static char _pixelFmtNames[39][21];
  769. static bool _pixelFmtNamesInit[39];
  770.  
  771.  
  772.  
  773.  
  774.  
  775. const char* getPixelFmtName(u32 pixel_format){
  776.   u32 which = 0;
  777.  
  778.   switch(pixel_format){
  779.     case PIXELFMT_INDEX1LSB  : which =  1; break;
  780.     case PIXELFMT_INDEX1MSB  : which =  2; break;
  781.     case PIXELFMT_INDEX4LSB  : which =  3; break;
  782.     case PIXELFMT_INDEX4MSB  : which =  4; break;
  783.     case PIXELFMT_INDEX8     : which =  5; break;
  784.     case PIXELFMT_RGB332     : which =  6; break;
  785.     case PIXELFMT_RGB444     : which =  7; break;
  786.     case PIXELFMT_BGR444     : which =  8; break;
  787.     case PIXELFMT_RGB555     : which =  9; break;
  788.     case PIXELFMT_BGR555     : which = 10; break;
  789.     case PIXELFMT_ARGB4444   : which = 11; break;
  790.     case PIXELFMT_RGBA4444   : which = 12; break;
  791.     case PIXELFMT_ABGR4444   : which = 13; break;
  792.     case PIXELFMT_BGRA4444   : which = 14; break;
  793.     case PIXELFMT_ARGB1555   : which = 15; break;
  794.     case PIXELFMT_RGBA5551   : which = 16; break;
  795.     case PIXELFMT_ABGR1555   : which = 17; break;
  796.     case PIXELFMT_BGRA5551   : which = 18; break;
  797.     case PIXELFMT_RGB565     : which = 19; break;
  798.     case PIXELFMT_BGR565     : which = 20; break;
  799.     case PIXELFMT_RGB24      : which = 21; break;
  800.     case PIXELFMT_BGR24      : which = 22; break;
  801.     case PIXELFMT_RGB888     : which = 23; break;
  802.     case PIXELFMT_RGBX8888   : which = 24; break;
  803.     case PIXELFMT_BGR888     : which = 25; break;
  804.     case PIXELFMT_BGRX8888   : which = 26; break;
  805.     case PIXELFMT_ARGB8888   : which = 27; break;
  806.     case PIXELFMT_RGBA8888   : which = 28; break;
  807.     case PIXELFMT_ABGR8888   : which = 29; break;
  808.     case PIXELFMT_BGRA8888   : which = 30; break;
  809.     case PIXELFMT_ARGB2101010: which = 31; break;
  810.     case PIXELFMT_YV12       : which = 32; break;
  811.     case PIXELFMT_IYUV       : which = 33; break;
  812.     case PIXELFMT_YUY2       : which = 34; break;
  813.     case PIXELFMT_UYVY       : which = 35; break;
  814.     case PIXELFMT_YVYU       : which = 36; break;
  815.     case PIXELFMT_NV12       : which = 37; break;
  816.     case PIXELFMT_NV21       : which = 38; break;
  817.     default:;
  818.   }
  819.  
  820.  
  821.   char* name_kit = _pixelFmtNames[which];
  822.  
  823.   if(!_pixelFmtNamesInit[which]){
  824.     const char* name_sdl = SDL_GetPixelFormatName(pixel_format);
  825.     name_sdl += sizeof("SDL_PIXELFORMAT")-1; //skip the "SDL_PIXELFORMAT" part
  826.  
  827.     strnCpy(name_kit  , "PIXELFMT",  8);
  828.     strnCpy(name_kit+8,   name_sdl, 12);
  829.     name_kit[20] = 0; //manual null-terminator, just in case
  830.  
  831.     _pixelFmtNamesInit[which] = true;
  832.  
  833.   }
  834.  
  835.  
  836.   return (const char*)name_kit;
  837.  
  838. }
  839.  
  840.  
  841.  
  842.  
  843.  
  844. }; /* namespace kit */
  845. /******************************************************************************/
  846. /******************************************************************************/
  847. //"2024-11-21\src\kit_sdl2\kit_Window.cpp":
  848. #include "_kit_common.hpp"
  849.  
  850. #define WIN_PTR ((SDL_Window*)_win)
  851. #define SURF_PTR ((SDL_Surface*)_surf)
  852. #define KIT_WIN_IS_INVALID (!_valid)
  853. #define KIT_WIN_CHECK_INVALID(_funcname) \
  854.   if(KIT_WIN_IS_INVALID) THROW_ERROR(_funcname ": window is invalid");
  855.  
  856. #define WIN_DATA_NAME "Window class"
  857.  
  858. namespace kit {
  859.  
  860. /* functions to potentially maybe incorporate:
  861. SDL_GetWindowBordersSize
  862. SDL_GetWindowGammaRamp
  863.  
  864. SDL_SetWindowGammaRamp
  865. SDL_SetWindowHitTest
  866. SDL_SetWindowShape
  867. SDL_IsShapedWindow
  868. SDL_SetWindowModalFor
  869. */
  870.  
  871.  
  872.  
  873.  
  874.  
  875. #define VALID_WINFLAGS       ( \
  876.   WINFLAG_FULLSCREEN         | \
  877.   WINFLAG_OPENGL             | \
  878.   WINFLAG_HIDDEN             | \
  879.   WINFLAG_BORDERLESS         | \
  880.   WINFLAG_RESIZABLE          | \
  881.   WINFLAG_MINIMIZED          | \
  882.   WINFLAG_MAXIMIZED          | \
  883.   WINFLAG_INPUT_GRABBED      | \
  884.   WINFLAG_FULLSCREEN_DESKTOP | \
  885.   WINFLAG_VULKAN               \
  886. )
  887.  
  888. Window::Window(const char* winTitle,
  889.                u31 winWidth, u31 winHeight,
  890.                u32 winFlags,
  891.                s32 winX, s32 winY)
  892. {
  893.   if(_valid) return;
  894.   _type = KIT_CLASSTYPE_WINDOW;
  895.  
  896.   if(winWidth.s ) THROW_ERROR("Window::Window(): winWidth > KIT_S32_MAX");
  897.   if(winHeight.s) THROW_ERROR("Window::Window(): winHeight > KIT_S32_MAX");
  898.  
  899.  
  900.   _win = SDL_CreateWindow(winTitle, winX, winY,
  901.                           winWidth.n, winHeight.n,
  902.                           winFlags & VALID_WINFLAGS);
  903.  
  904.   if(_win == nullptr)
  905.     THROW_ERRORF("Window::Window(): \"%s\"", SDL_GetError());
  906.  
  907.  
  908.   SDL_SetWindowData(WIN_PTR, WIN_DATA_NAME, this);
  909.  
  910.  
  911.   _valid = true;
  912.   _constructing = false;
  913.  
  914. }
  915.  
  916.  
  917.  
  918.  
  919. Window::~Window(){
  920.   if(!_valid) return;
  921.   _valid = false;
  922.  
  923.   if(_win != nullptr){
  924.     SDL_DestroyWindow(WIN_PTR);
  925.     _win = nullptr;
  926.  
  927.   }
  928.  
  929. }
  930.  
  931.  
  932.  
  933.  
  934.  
  935. bool Window::hasSurface(){
  936.   KIT_WIN_CHECK_INVALID("Window::hasSurface()");
  937.  
  938.   return SDL_HasWindowSurface(WIN_PTR);
  939.  
  940. }
  941.  
  942.  
  943.  
  944.  
  945. u32 Window::getPixelFormat(){
  946.   KIT_WIN_CHECK_INVALID("Window::getPixelFormat()");
  947.  
  948.   u32 pixelFormat = SDL_GetWindowPixelFormat(WIN_PTR);
  949.   if(pixelFormat == SDL_PIXELFORMAT_UNKNOWN)
  950.     THROW_ERRORF("Window::getPixelFormat(): \"%s\"", SDL_GetError());
  951.  
  952.   return pixelFormat;
  953.  
  954. }
  955.  
  956.  
  957.  
  958.  
  959. u32 Window::getID(){
  960.   KIT_WIN_CHECK_INVALID("Window::getID()");
  961.  
  962.   u32 id = SDL_GetWindowID(WIN_PTR);
  963.   if(!id) THROW_ERRORF("Window::getID(): \"%s\"", SDL_GetError());
  964.  
  965.   return id;
  966.  
  967. }
  968.  
  969.  
  970.  
  971.  
  972. u32 Window::getDisplayIndex(){
  973.   KIT_WIN_CHECK_INVALID("Window::getDisplayIndex()");
  974.  
  975.   s32 index = SDL_GetWindowDisplayIndex(WIN_PTR);
  976.   if(index < 0)
  977.     THROW_ERRORF("Window::getDisplayIndex(): \"%s\"", SDL_GetError());
  978.  
  979.   return (u32)index;
  980.  
  981. }
  982.  
  983.  
  984.  
  985.  
  986. void* Window::getUserdata(const char* name){
  987.   KIT_WIN_CHECK_INVALID("Window::getUserdata()");
  988.  
  989.   if(name == nullptr)
  990.     THROW_ERROR("Window::getUserdata(): name = nullptr");
  991.  
  992.   return SDL_GetWindowData(WIN_PTR, name);
  993.  
  994. }
  995.  
  996.  
  997.  
  998.  
  999. DisplayMode Window::getDisplayMode(){
  1000.   KIT_WIN_CHECK_INVALID("Window::getDisplayMode()");
  1001.  
  1002.   SDL_DisplayMode mode_sdl;
  1003.   if(SDL_GetWindowDisplayMode(WIN_PTR, &mode_sdl) < 0)
  1004.     THROW_ERRORF("Window::getDisplayMode(): \"%s\"", SDL_GetError());
  1005.  
  1006.   DisplayMode mode_kit;
  1007.   mode_kit.pixelFmt    = mode_sdl.format;
  1008.   mode_kit.w           = mode_sdl.w;
  1009.   mode_kit.h           = mode_sdl.h;
  1010.   mode_kit.refreshRate = mode_sdl.refresh_rate;
  1011.  
  1012.   return mode_kit;
  1013.  
  1014. }
  1015.  
  1016.  
  1017.  
  1018.  
  1019. const char* Window::getTitle(){
  1020.   KIT_WIN_CHECK_INVALID("Window::getTitle()");
  1021.  
  1022.   return SDL_GetWindowTitle(WIN_PTR);
  1023.  
  1024. }
  1025.  
  1026.  
  1027.  
  1028.  
  1029. shape::point Window::getSize(){
  1030.   KIT_WIN_CHECK_INVALID("Window::getSize()");
  1031.  
  1032.   shape::point size;
  1033.   SDL_GetWindowSize(WIN_PTR, &size.x, &size.y);
  1034.  
  1035.   return size;
  1036.  
  1037. }
  1038.  
  1039.  
  1040.  
  1041.  
  1042. u32 Window::getFlags(){
  1043.   KIT_WIN_CHECK_INVALID("Window::getFlags()");
  1044.  
  1045.   return SDL_GetWindowFlags(WIN_PTR);
  1046.  
  1047. }
  1048.  
  1049.  
  1050.  
  1051.  
  1052. f32 Window::getOpacity(){
  1053.   KIT_WIN_CHECK_INVALID("Window::getOpacity()");
  1054.  
  1055.   f32 opacity;
  1056.   if(SDL_GetWindowOpacity(WIN_PTR, &opacity) < 0)
  1057.     THROW_ERRORF("Window::getOpacity(): \"%s\"", SDL_GetError());
  1058.  
  1059.   return opacity;
  1060.  
  1061. }
  1062.  
  1063.  
  1064.  
  1065.  
  1066. shape::point Window::getMinSize(){
  1067.   KIT_WIN_CHECK_INVALID("Window::getMinSize()");
  1068.  
  1069.   shape::point minSize;
  1070.   SDL_GetWindowMinimumSize(WIN_PTR, &minSize.x, &minSize.y);
  1071.  
  1072.   return minSize;
  1073.  
  1074. }
  1075.  
  1076.  
  1077.  
  1078.  
  1079. shape::point Window::getMaxSize(){
  1080.   KIT_WIN_CHECK_INVALID("Window::getMaxSize()");
  1081.  
  1082.   shape::point maxSize;
  1083.   SDL_GetWindowMaximumSize(WIN_PTR, &maxSize.x, &maxSize.y);
  1084.  
  1085.   return maxSize;
  1086.  
  1087. }
  1088.  
  1089.  
  1090.  
  1091.  
  1092. shape::point Window::getPosition(){
  1093.   KIT_WIN_CHECK_INVALID("Window::getPosition()");
  1094.  
  1095.   shape::point pos;
  1096.   SDL_GetWindowPosition(WIN_PTR, &pos.x, &pos.y);
  1097.  
  1098.   return pos;
  1099.  
  1100. }
  1101.  
  1102.  
  1103.  
  1104.  
  1105. bool Window::getGrab(){
  1106.   KIT_WIN_CHECK_INVALID("Window::getGrab()");
  1107.  
  1108.   return SDL_GetWindowGrab(WIN_PTR);
  1109.  
  1110. }
  1111.  
  1112.  
  1113.  
  1114.  
  1115. bool Window::getKeyboardGrab(){
  1116.   KIT_WIN_CHECK_INVALID("Window::getKeyboardGrab()");
  1117.  
  1118.   return SDL_GetWindowKeyboardGrab(WIN_PTR);
  1119.  
  1120. }
  1121.  
  1122.  
  1123.  
  1124.  
  1125. bool Window::getMouseGrab(){
  1126.   KIT_WIN_CHECK_INVALID("Window::getMouseGrab()");
  1127.  
  1128.   return SDL_GetWindowMouseGrab(WIN_PTR);
  1129.  
  1130. }
  1131.  
  1132.  
  1133.  
  1134.  
  1135. shape::rect Window::getMouseGrabRect(){
  1136.   KIT_WIN_CHECK_INVALID("Window::getMouseGrabRect()");
  1137.  
  1138.   const SDL_Rect* _rect = SDL_GetWindowMouseRect(WIN_PTR);
  1139.  
  1140.   shape::rect rect;
  1141.  
  1142.  
  1143.   if(_rect != nullptr){
  1144.     rect.x = _rect->x;
  1145.     rect.y = _rect->y;
  1146.     rect.w = _rect->w;
  1147.     rect.h = _rect->h;
  1148.  
  1149.   } else { //no rect set; use full window
  1150.     shape::point winSize = getSize();
  1151.     rect.x = 0;
  1152.     rect.y = 0;
  1153.     rect.w = winSize.x;
  1154.     rect.h = winSize.y;
  1155.  
  1156.   }
  1157.  
  1158.  
  1159.   return rect;
  1160.  
  1161. }
  1162.  
  1163.  
  1164.  
  1165.  
  1166. f32 Window::getBrightness(){
  1167.   KIT_WIN_CHECK_INVALID("Window::getBrightness()");
  1168.  
  1169.   return SDL_GetWindowBrightness(WIN_PTR);
  1170.  
  1171. }
  1172.  
  1173.  
  1174.  
  1175.  
  1176.  
  1177. void* Window::setUserdata(const char* name, void* userdata){
  1178.   KIT_WIN_CHECK_INVALID("Window::setUserdata()");
  1179.  
  1180.   if(name == nullptr)
  1181.     THROW_ERROR("Window::setUserdata(): name = nullptr");
  1182.   if(!strnCmp(WIN_DATA_NAME, name)) //WIN_DATA_NAME is reserved
  1183.     THROW_ERROR("Window::setUserdata(): name = \"" WIN_DATA_NAME "\"");
  1184.  
  1185.   return SDL_SetWindowData(WIN_PTR, name, userdata);
  1186.  
  1187. }
  1188.  
  1189.  
  1190.  
  1191.  
  1192. void Window::setDisplayMode(const DisplayMode* mode){
  1193.   KIT_WIN_CHECK_INVALID("Window::setDisplayMode()");
  1194.  
  1195.   SDL_DisplayMode mode_sdl;
  1196.  
  1197.   if(mode != nullptr){
  1198.     mode_sdl.format       = mode->pixelFmt;
  1199.     mode_sdl.w            = mode->w;
  1200.     mode_sdl.h            = mode->h;
  1201.     mode_sdl.refresh_rate = mode->refreshRate;
  1202.   }
  1203.  
  1204.   if(SDL_SetWindowDisplayMode(WIN_PTR, (mode) ? &mode_sdl : nullptr) < 0)
  1205.     THROW_ERRORF("Window::setDisplayMode(): \"%s\"", SDL_GetError());
  1206.  
  1207. }
  1208.  
  1209.  
  1210.  
  1211.  
  1212. void Window::setTitle(const char* title){
  1213.   KIT_WIN_CHECK_INVALID("Window::setTitle()");
  1214.  
  1215.   if(title == nullptr) title = "";
  1216.  
  1217.   SDL_SetWindowTitle(WIN_PTR, title);
  1218.  
  1219. }
  1220.  
  1221.  
  1222.  
  1223.  
  1224. void Window::setSize(u31 width, u31 height){
  1225.   KIT_WIN_CHECK_INVALID("Window::setSize()");
  1226.  
  1227.   if(width.s ) THROW_ERROR("Window::setSize(): width > KIT_S32_MAX");
  1228.   if(height.s) THROW_ERROR("Window::setSize(): height > KIT_S32_MAX");
  1229.  
  1230.   SDL_SetWindowSize(WIN_PTR, width.n, height.n);
  1231.  
  1232. }
  1233.  
  1234.  
  1235.  
  1236.  
  1237. void Window::setVisibility(bool visible){
  1238.   KIT_WIN_CHECK_INVALID("Window::setVisibility()");
  1239.  
  1240.   if(visible) SDL_ShowWindow(WIN_PTR);
  1241.   else        SDL_HideWindow(WIN_PTR);
  1242.  
  1243. }
  1244.  
  1245.  
  1246.  
  1247.  
  1248. void Window::setFullscreen(u32 mode){
  1249.   KIT_WIN_CHECK_INVALID("Window::setFullscreen()");
  1250.  
  1251.   u32 flags = 0; //normal windowed mode by default
  1252.   if     (mode == 1) flags = SDL_WINDOW_FULLSCREEN;
  1253.   else if(mode == 2) flags = SDL_WINDOW_FULLSCREEN_DESKTOP;
  1254.  
  1255.   if(SDL_SetWindowFullscreen(WIN_PTR, flags) < 0)
  1256.     THROW_ERRORF("Window::setFullscreen(): \"%s\"", SDL_GetError());
  1257.  
  1258. }
  1259.  
  1260.  
  1261.  
  1262.  
  1263. void Window::setResizable(bool enable){
  1264.   KIT_WIN_CHECK_INVALID("Window::setResizable()");
  1265.  
  1266.   SDL_SetWindowResizable(WIN_PTR, (SDL_bool)enable);
  1267.  
  1268. }
  1269.  
  1270.  
  1271.  
  1272.  
  1273. void Window::setBordered(bool enable){
  1274.   KIT_WIN_CHECK_INVALID("Window::setBordered()");
  1275.  
  1276.   SDL_SetWindowBordered(WIN_PTR, (SDL_bool)enable);
  1277.  
  1278. }
  1279.  
  1280.  
  1281.  
  1282.  
  1283. void Window::setAlwaysOnTop(bool enable){
  1284.   KIT_WIN_CHECK_INVALID("Window::setAlwaysOnTop()");
  1285.  
  1286.   SDL_SetWindowAlwaysOnTop(WIN_PTR, (SDL_bool)enable);
  1287.  
  1288. }
  1289.  
  1290.  
  1291.  
  1292.  
  1293. void Window::setOpacity(f32 opacity){
  1294.   KIT_WIN_CHECK_INVALID("Window::setOpacity()");
  1295.  
  1296.   if(SDL_SetWindowOpacity(WIN_PTR, opacity) < 0)
  1297.     THROW_ERRORF("Window::setOpacity(): \"%s\"", SDL_GetError());
  1298.  
  1299. }
  1300.  
  1301.  
  1302.  
  1303.  
  1304. void Window::setMinSize(u31 minWidth, u31 minHeight){
  1305.   KIT_WIN_CHECK_INVALID("Window::setMinSize()");
  1306.  
  1307.   if(minWidth.s ) THROW_ERROR("Window::setMinSize(): minWidth > KIT_S32_MAX");
  1308.   if(minHeight.s) THROW_ERROR("Window::setMinSize(): minHeight > KIT_S32_MAX");
  1309.  
  1310.   SDL_SetWindowMinimumSize(WIN_PTR, minWidth.n, minHeight.n);
  1311.  
  1312. }
  1313.  
  1314.  
  1315. void Window::setMaxSize(u31 maxWidth, u31 maxHeight){
  1316.   KIT_WIN_CHECK_INVALID("Window::setMaxSize()");
  1317.  
  1318.   if(maxWidth.s ) THROW_ERROR("Window::setMaxSize(): maxWidth > KIT_S32_MAX");
  1319.   if(maxHeight.s) THROW_ERROR("Window::setMaxSize(): maxHeight > KIT_S32_MAX");
  1320.  
  1321.   SDL_SetWindowMaximumSize(WIN_PTR, maxWidth.n, maxHeight.n);
  1322.  
  1323. }
  1324.  
  1325.  
  1326.  
  1327.  
  1328. void Window::setPosition(s32 x, s32 y){
  1329.   KIT_WIN_CHECK_INVALID("Window::setPosition()");
  1330.  
  1331.   SDL_SetWindowPosition(WIN_PTR, x, y);
  1332.  
  1333. }
  1334.  
  1335.  
  1336.  
  1337.  
  1338. void Window::setGrab(bool enable){
  1339.   KIT_WIN_CHECK_INVALID("Window::setGrab()");
  1340.  
  1341.   SDL_SetWindowGrab(WIN_PTR, (SDL_bool)enable);
  1342.  
  1343. }
  1344.  
  1345.  
  1346.  
  1347.  
  1348. void Window::setKeyboardGrab(bool enable){
  1349.   KIT_WIN_CHECK_INVALID("Window::setKeyboardGrab()");
  1350.  
  1351.   SDL_SetWindowKeyboardGrab(WIN_PTR, (SDL_bool)enable);
  1352.  
  1353. }
  1354.  
  1355.  
  1356.  
  1357.  
  1358. void Window::setMouseGrab(bool enable){
  1359.   KIT_WIN_CHECK_INVALID("Window::setMouseGrab()");
  1360.  
  1361.   SDL_SetWindowMouseGrab(WIN_PTR, (SDL_bool)enable);
  1362.  
  1363. }
  1364.  
  1365.  
  1366. void Window::setMouseGrabRect(const shape::rect* rect){
  1367.   KIT_WIN_CHECK_INVALID("Window::setMouseGrabRect()");
  1368.  
  1369.   if(SDL_SetWindowMouseRect(WIN_PTR, (const SDL_Rect*)rect) < 0)
  1370.     THROW_ERRORF("Window::setMouseGrabRect(): \"%s\"", SDL_GetError());
  1371.  
  1372. }
  1373.  
  1374.  
  1375.  
  1376.  
  1377. void Window::setBrightness(f32 brightness){
  1378.   KIT_WIN_CHECK_INVALID("Window::setBrightness()");
  1379.  
  1380.   if(SDL_SetWindowBrightness(WIN_PTR, brightness) < 0)
  1381.     THROW_ERRORF("Window::setBrightness(): \"%s\"", SDL_GetError());
  1382.  
  1383. }
  1384.  
  1385.  
  1386.  
  1387.  
  1388. void Window::setIcon(Surface& icon){
  1389.   KIT_WIN_CHECK_INVALID("Window::setIcon()");
  1390.  
  1391.   void* icon_p = &icon;
  1392.  
  1393.   if(!KIT_GET_CLASS_VALID(icon_p))
  1394.     THROW_ERROR("Window::setIcon(): icon is invalid");
  1395.  
  1396.   SDL_Surface* icon_surf = (SDL_Surface*)KIT_GET_CLASS_OPAQUE(icon_p);
  1397.  
  1398.   SDL_SetWindowIcon(WIN_PTR, icon_surf);
  1399.  
  1400. }
  1401.  
  1402.  
  1403.  
  1404.  
  1405. void Window::warpMouse(s32 x, s32 y){
  1406.   KIT_WIN_CHECK_INVALID("Window::warpMouse()");
  1407.  
  1408.   SDL_WarpMouseInWindow(WIN_PTR, x, y);
  1409.  
  1410. }
  1411.  
  1412.  
  1413.  
  1414.  
  1415. void Window::minimize(){
  1416.   KIT_WIN_CHECK_INVALID("Window::minimize()");
  1417.  
  1418.   SDL_MinimizeWindow(WIN_PTR);
  1419.  
  1420. }
  1421.  
  1422.  
  1423. void Window::maximize(){
  1424.   KIT_WIN_CHECK_INVALID("Window::maximize()");
  1425.  
  1426.   SDL_MaximizeWindow(WIN_PTR);
  1427.  
  1428. }
  1429.  
  1430.  
  1431. void Window::restore(){
  1432.   KIT_WIN_CHECK_INVALID("Window::restore()");
  1433.  
  1434.   SDL_RestoreWindow(WIN_PTR);
  1435.  
  1436. }
  1437.  
  1438.  
  1439.  
  1440.  
  1441. void Window::raise(){
  1442.   KIT_WIN_CHECK_INVALID("Window::raise()");
  1443.  
  1444.   SDL_RaiseWindow(WIN_PTR);
  1445.  
  1446. }
  1447.  
  1448.  
  1449.  
  1450.  
  1451.  
  1452. /******************************************************************************/
  1453.  
  1454.  
  1455.  
  1456.  
  1457.  
  1458. bool Window::renewSurface(){
  1459.   if(KIT_WIN_IS_INVALID) return false;
  1460.  
  1461.   _surf = SDL_GetWindowSurface(WIN_PTR);
  1462.  
  1463.   if(_surf == nullptr)
  1464.     THROW_ERRORF("Window::renewSurface(): \"%s\"", SDL_GetError());
  1465.  
  1466.   return true;
  1467.  
  1468. }
  1469.  
  1470.  
  1471.  
  1472.  
  1473.  
  1474. void Window::destroySurface(){
  1475.   KIT_WIN_CHECK_INVALID("Window::destroySurface()");
  1476.  
  1477.   if(_surf == nullptr) return;
  1478.  
  1479.   if(SDL_DestroyWindowSurface(WIN_PTR) < 0)
  1480.     THROW_ERRORF("Window::destroySurface(): \"%s\"", SDL_GetError());
  1481.  
  1482.   _surf = nullptr;
  1483.  
  1484. }
  1485.  
  1486.  
  1487.  
  1488.  
  1489.  
  1490. void Window::updateSurface(const shape::rect* rects, u31 rects_len){
  1491.   KIT_WIN_CHECK_INVALID("Window::updateSurface()");
  1492.  
  1493.   if(rects_len.s)
  1494.     THROW_ERROR("Window::updateSurface(): rects_len > KIT_S32_MAX");
  1495.  
  1496.  
  1497.   int err;
  1498.  
  1499.   if(rects == nullptr) err = SDL_UpdateWindowSurface(WIN_PTR);
  1500.   else err = SDL_UpdateWindowSurfaceRects(WIN_PTR, (SDL_Rect*)rects, rects_len.n);
  1501.  
  1502.   if(err < 0)
  1503.     THROW_ERRORF("Window::updateSurface(): \"%s\"", SDL_GetError());
  1504.  
  1505. }
  1506.  
  1507.  
  1508.  
  1509.  
  1510.  
  1511. void Window::fillRects(colors::ABGR color, const shape::rect* rects,
  1512.                                            u31 rects_len)
  1513. {
  1514.   KIT_WIN_CHECK_INVALID("Window::fillRects()");
  1515.  
  1516.   if(_surf == nullptr)
  1517.     THROW_ERROR("Window::fillRects(): Window doesn't have a surface");
  1518.  
  1519.   if(rects_len.s)
  1520.     THROW_ERROR("Window::fillRects(): rects_len > KIT_S32_MAX");
  1521.  
  1522.  
  1523.   u32 mapped_color = SDL_MapRGBA(SURF_PTR->format, color.r, color.g,
  1524.                                                    color.b, color.a);
  1525.  
  1526.  
  1527.   int err;
  1528.  
  1529.   if(rects == nullptr || !rects_len.n){
  1530.     err = SDL_FillRect(SURF_PTR, (SDL_Rect*)rects, mapped_color);
  1531.  
  1532.   } else {
  1533.     err = SDL_FillRects(SURF_PTR, (SDL_Rect*)rects, rects_len.n, mapped_color);
  1534.  
  1535.   }
  1536.  
  1537.  
  1538.   if(err < 0)
  1539.     THROW_ERRORF("Window::fillRects(): \"%s\"", SDL_GetError());
  1540.  
  1541. }
  1542.  
  1543.  
  1544.  
  1545.  
  1546.  
  1547. /******************************************************************************/
  1548.  
  1549.  
  1550.  
  1551.  
  1552.  
  1553. Window* getGrabbedWindow(){ //not a part of Window
  1554.   SDL_Window* win_sdl = SDL_GetGrabbedWindow();
  1555.   if(win_sdl == nullptr) return nullptr;
  1556.  
  1557.   return (Window*)SDL_GetWindowData(win_sdl, WIN_DATA_NAME);
  1558.  
  1559. }
  1560.  
  1561.  
  1562.  
  1563.  
  1564.  
  1565. }; /* namespace kit */
  1566.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement