Advertisement
Kitomas

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

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