Advertisement
Kitomas

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

Aug 29th, 2024
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.50 KB | None | 0 0
  1. /******************************************************************************/
  2. /******************************************************************************/
  3. //"ksdl2\src\kit_sdl2\kit_lodepng_custom.cpp":
  4. #include "_kit_common.hpp"
  5.  
  6.  
  7. namespace kit {
  8.  
  9. namespace png {
  10.  
  11.  
  12.  
  13.  
  14.  
  15. void* lodepng_malloc(size_t size){
  16. #ifdef LODEPNG_MAX_ALLOC
  17.   if(size > LODEPNG_MAX_ALLOC) return 0;
  18. #endif
  19.   return memory::alloc(size);
  20.  
  21. }
  22.  
  23.  
  24.  
  25.  
  26.  
  27. // NOTE: when realloc returns NULL, it leaves the original memory untouched
  28. void* lodepng_realloc(void* ptr, size_t new_size){
  29. #ifdef LODEPNG_MAX_ALLOC
  30.   if(new_size > LODEPNG_MAX_ALLOC) return 0;
  31. #endif
  32.   return memory::realloc(&ptr, new_size);
  33.  
  34. }
  35.  
  36.  
  37.  
  38.  
  39.  
  40. void lodepng_free(void* ptr){
  41.   memory::free(&ptr);
  42.  
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49. //i rewrote lodepng's file io functions to only use SDL functions
  50.  
  51. #ifndef LODEPNG_NO_COMPILE_DISK
  52.  
  53.  
  54. #undef LONG_MAX
  55. #define LONG_MAX (2147483647)
  56.  
  57.  
  58. unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* filename) {
  59.   *out = (unsigned char*)SDL_LoadFile(filename, outsize);
  60.  
  61.   if(*out == nullptr){
  62.     return 78;
  63.  
  64.   } else {
  65.     ++numAllocations; //a subsequent call to memory::free should undo this
  66.  
  67.     return 0;
  68.   }
  69.  
  70. }
  71.  
  72.  
  73. // write given buffer to the file, overwriting the file, it doesn't append to it.
  74. unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename) {
  75.   SDL_RWops* file = SDL_RWFromFile(filename, "wb");
  76.   if(file == nullptr) return 79;
  77.  
  78.   size_t bytesWritten = SDL_RWwrite(file, buffer, 1, buffersize);
  79.  
  80.   SDL_RWclose(file);
  81.  
  82.   if(bytesWritten < buffersize) return 79;
  83.   else                          return  0;
  84.  
  85. }
  86.  
  87.  
  88.  
  89. /*
  90. #undef LONG_MAX
  91. #define LONG_MAX (2147483647)
  92. #include <stdio.h>
  93.  
  94. // returns negative value on error. This should be pure C compatible, so no fstat.
  95. static long lodepng_filesize(const char* filename) {
  96.   FILE* file;
  97.   long size;
  98.   file = fopen(filename, "rb");
  99.   if(!file) return -1;
  100.  
  101.   if(fseek(file, 0, SEEK_END) != 0) {
  102.     fclose(file);
  103.     return -1;
  104.   }
  105.  
  106.   size = ftell(file);
  107.   // It may give LONG_MAX as directory size, this is invalid for us.
  108.   if(size == LONG_MAX) size = -1;
  109.  
  110.   fclose(file);
  111.   return size;
  112. }
  113.  
  114. // load file into buffer that already has the correct allocated size. Returns error code.
  115. static unsigned lodepng_buffer_file(unsigned char* out, size_t size, const char* filename) {
  116.   FILE* file;
  117.   size_t readsize;
  118.   file = fopen(filename, "rb");
  119.   if(!file) return 78;
  120.  
  121.   readsize = fread(out, 1, size, file);
  122.   fclose(file);
  123.  
  124.   if(readsize != size) return 78;
  125.   return 0;
  126. }
  127.  
  128. unsigned lodepng_load_file(unsigned char** out, size_t* outsize, const char* filename) {
  129.   long size = lodepng_filesize(filename);
  130.   if(size < 0) return 78;
  131.   *outsize = (size_t)size;
  132.  
  133.   *out = (unsigned char*)lodepng_malloc((size_t)size);
  134.   if(!(*out) && size > 0) return 83; // the above malloc failed
  135.  
  136.   return lodepng_buffer_file(*out, (size_t)size, filename);
  137. }
  138.  
  139. // write given buffer to the file, overwriting the file, it doesn't append to it.
  140. unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const char* filename) {
  141.   FILE* file;
  142.   file = fopen(filename, "wb" );
  143.   if(!file) return 79;
  144.   fwrite(buffer, 1, buffersize, file);
  145.   fclose(file);
  146.   return 0;
  147. }
  148. */
  149.  
  150. #endif /* LODEPNG_NO_COMPILE_DISK */
  151.  
  152. }; /* namespace png */
  153.  
  154. }; /* namespace kit */
  155. /******************************************************************************/
  156. /******************************************************************************/
  157. //"ksdl2\src\kit_sdl2\kit_memory.cpp":
  158. #include "_kit_common.hpp"
  159.  
  160.  
  161. //turns something into a void**
  162.  //(this makes some code here easier for me to read)
  163. #define VPP(_ptr_p) ((void**)(_ptr_p))
  164.  
  165.  
  166. namespace kit {
  167.  
  168.  
  169. size_t numAllocations = 0;
  170.  
  171.  
  172.  
  173.  
  174.  
  175. void* memory::alloc(size_t size){
  176.   void* newHeapMemory = SDL_malloc(size);
  177.   if(newHeapMemory != nullptr) ++numAllocations;
  178.   return newHeapMemory;
  179.  
  180. }
  181.  
  182.  
  183.  
  184.  
  185. void memory::free(void* ptr_p){
  186.   if(VPP(ptr_p) != nullptr  &&  *VPP(ptr_p) != nullptr){
  187.     --numAllocations;
  188.     SDL_free(*VPP(ptr_p));
  189.     *VPP(ptr_p) = nullptr;
  190.  
  191.   }
  192.  
  193. }
  194.  
  195.  
  196.  
  197.  
  198. void* memory::realloc(void* ptr_p, size_t newSize){
  199.   void* ptr_new = nullptr;
  200.  
  201.   if(VPP(ptr_p) != nullptr){
  202.     ptr_new = SDL_realloc(*VPP(ptr_p), newSize);
  203.  
  204.     if(ptr_new != nullptr){
  205.       if(*VPP(ptr_p) == nullptr) ++numAllocations;
  206.       *VPP(ptr_p) = ptr_new;
  207.  
  208.     }
  209.  
  210.   }
  211.  
  212.   return ptr_new;
  213.  
  214. }
  215.  
  216.  
  217.  
  218.  
  219.  
  220. void* memory::alloc2(size_t size){
  221.   void* newHeapMemory = SDL_malloc(size);
  222.   if(newHeapMemory == nullptr)
  223.     THROW_ERROR("memory::alloc2(): failed to allocate memory");
  224.   ++numAllocations;
  225.   return newHeapMemory;
  226.  
  227. }
  228.  
  229.  
  230.  
  231.  
  232. void memory::free2(void* ptr_p){
  233.   if( VPP(ptr_p) == nullptr) THROW_ERROR("memory::free2(): ptr_p == nullptr");
  234.   if(*VPP(ptr_p) == nullptr) THROW_ERROR("memory::free2(): *ptr_p == nullptr");
  235.   --numAllocations;
  236.   SDL_free(*VPP(ptr_p));
  237.   *VPP(ptr_p) = nullptr;
  238.  
  239. }
  240.  
  241.  
  242.  
  243.  
  244. void* memory::realloc2(void* ptr_p, size_t newSize){
  245.   void* ptr_new = nullptr;
  246.  
  247.   if(VPP(ptr_p) != nullptr){
  248.     ptr_new = SDL_realloc(*VPP(ptr_p), newSize);
  249.  
  250.     if(ptr_new != nullptr){
  251.       if(*VPP(ptr_p) == nullptr) ++numAllocations; //if memory is new, not realloc'd
  252.       *VPP(ptr_p) = ptr_new;
  253.  
  254.     }
  255.  
  256.   } else {
  257.     THROW_ERROR("memory::realloc2(): ptr_p = nullptr");
  258.  
  259.   }
  260.  
  261.  
  262.   if(ptr_new == nullptr)
  263.     THROW_ERROR("memory::realloc2(): failed to reallocate memory");
  264.  
  265.  
  266.   return ptr_new;
  267.  
  268. }
  269.  
  270.  
  271.  
  272.  
  273.  
  274. size_t memory::getNumAllocations(){
  275.   return numAllocations;
  276.  
  277. }
  278.  
  279.  
  280.  
  281.  
  282.  
  283. //currently just a wrapper, but now i can make my own implementation
  284.  //whenever i want, without replacing every call to memset with it
  285. void* memory::set(void* ptr, s32 value, size_t size){
  286.   if(ptr == nullptr) return nullptr; //now it's safe to pass nullptr :D
  287.   return memset(ptr, value, size);
  288.  
  289. }
  290.  
  291.  
  292.  
  293.  
  294. void* memory::set2(void* ptr, s32 value, size_t size){
  295.   if(ptr == nullptr) THROW_ERROR("memory::set2(): ptr = nullptr");
  296.   return memset(ptr, value, size);
  297.  
  298. }
  299.  
  300.  
  301.  
  302.  
  303.  
  304. void* memory::copy(void* destination, const void* source, size_t size){
  305.   //tbd: maybe make it so that memcpy is only called if dst&src aren't nullptr
  306.   return memcpy(destination, source, size);
  307.  
  308. }
  309.  
  310.  
  311.  
  312.  
  313. void* memory::copy2(void* destination, const void* source, size_t size){
  314.   if(destination == nullptr) THROW_ERROR("memory::copy2(): destination = nullptr");
  315.   if(source      == nullptr) THROW_ERROR("memory::copy2(): source = nullptr");
  316.   return memcpy(destination, source, size);
  317.  
  318. }
  319.  
  320.  
  321.  
  322.  
  323.  
  324. u32 memory::getSystemRAM_MiB(){
  325.   s32 result = SDL_GetSystemRAM();
  326.  
  327.   if(result < 0) //hopefully this can't happen
  328.     THROW_ERROR("memory::getSystemRAM_MiB(): SDL_GetSystemRAM() < 0");
  329.  
  330.   return (u32)result;
  331.  
  332. }
  333.  
  334.  
  335.  
  336.  
  337. u32 memory::getSIMDAlignment(){
  338.   return SDL_SIMDGetAlignment(); //this shouldn't be any more than 64...
  339.  
  340. }
  341.  
  342.  
  343.  
  344.  
  345.  
  346. void* memory::allocSIMD(size_t size){
  347.   void* newHeapMemory = SDL_SIMDAlloc(size);
  348.   if(newHeapMemory != nullptr) ++numAllocations;
  349.   return newHeapMemory;
  350.  
  351. }
  352.  
  353.  
  354.  
  355.  
  356. void memory::freeSIMD(void* ptr_p){
  357.   if(VPP(ptr_p) != nullptr  &&  *VPP(ptr_p) != nullptr){
  358.     --numAllocations;
  359.     SDL_SIMDFree(*VPP(ptr_p));
  360.     *VPP(ptr_p) = nullptr;
  361.  
  362.   }
  363.  
  364. }
  365.  
  366.  
  367.  
  368.  
  369. void* memory::reallocSIMD(void* ptr_p, size_t newSize){
  370.   void* ptr_new = nullptr;
  371.  
  372.   if(VPP(ptr_p) != nullptr){
  373.     ptr_new = SDL_SIMDRealloc(*VPP(ptr_p), newSize);
  374.  
  375.     if(ptr_new != nullptr){
  376.       if(*VPP(ptr_p) == nullptr) ++numAllocations;
  377.       *VPP(ptr_p) = ptr_new;
  378.  
  379.     }
  380.  
  381.   }
  382.  
  383.   return ptr_new;
  384.  
  385. }
  386.  
  387.  
  388.  
  389.  
  390.  
  391. void* memory::allocSIMD2(size_t size){
  392.   void* newHeapMemory = SDL_SIMDAlloc(size);
  393.   if(newHeapMemory == nullptr)
  394.     THROW_ERROR("memory::allocSIMD2(): failed to allocate memory");
  395.   ++numAllocations;
  396.   return newHeapMemory;
  397.  
  398. }
  399.  
  400.  
  401.  
  402.  
  403. void memory::freeSIMD2(void* ptr_p){
  404.   if( VPP(ptr_p) == nullptr) THROW_ERROR("memory::freeSIMD2(): ptr_p == nullptr");
  405.   if(*VPP(ptr_p) == nullptr) THROW_ERROR("memory::freeSIMD2(): *ptr_p == nullptr");
  406.   --numAllocations;
  407.   SDL_SIMDFree(*VPP(ptr_p));
  408.   *VPP(ptr_p) = nullptr;
  409.  
  410. }
  411.  
  412.  
  413.  
  414.  
  415. void* memory::reallocSIMD2(void* ptr_p, size_t newSize){
  416.   void* ptr_new = nullptr;
  417.  
  418.   if(VPP(ptr_p) != nullptr){
  419.     ptr_new = SDL_SIMDRealloc(*VPP(ptr_p), newSize);
  420.  
  421.     if(ptr_new != nullptr){
  422.       if(*VPP(ptr_p) == nullptr) ++numAllocations; //if memory is new, not realloc'd
  423.       *VPP(ptr_p) = ptr_new;
  424.  
  425.     }
  426.  
  427.   } else {
  428.     THROW_ERROR("memory::reallocSIMD2(): ptr_p = nullptr");
  429.  
  430.   }
  431.  
  432.  
  433.   if(ptr_new == nullptr)
  434.     THROW_ERROR("memory::reallocSIMD2(): failed to reallocate memory");
  435.  
  436.  
  437.   return ptr_new;
  438.  
  439. }
  440.  
  441.  
  442.  
  443.  
  444.  
  445. memory::Wrapper::Wrapper(size_t size){
  446.   ptr = memory::alloc(size);
  447.   if(ptr == nullptr)
  448.     THROW_ERROR("memory::Wrapper::Wrapper() failed to allocate memory");
  449.  
  450. }
  451.  
  452.  
  453.  
  454.  
  455. memory::WrapperSIMD::WrapperSIMD(size_t size){
  456.   ptr = memory::allocSIMD(size);
  457.   if(ptr == nullptr)
  458.     THROW_ERROR("memory::WrapperSIMD::WrapperSIMD() failed to allocate memory");
  459.  
  460. }
  461.  
  462.  
  463.  
  464.  
  465.  
  466. }; /* namespace kit */
  467. /******************************************************************************/
  468. /******************************************************************************/
  469. //"ksdl2\src\kit_sdl2\kit_Surface.cpp":
  470. #include "_kit_common.hpp"
  471.  
  472. #define SURF_IS_INVALID (!_valid)
  473.  
  474. #define SURF_PTR ((SDL_Surface*)_opq)
  475.  
  476.  
  477. namespace kit {
  478.  
  479.  
  480.  
  481.  
  482.  
  483. Surface::Surface(u32 width, u32 height, u32 pixel_format){
  484.   _type = KIT_CLASSTYPE_SURFACE;
  485.  
  486.   if(width > KIT_S32_MAX)
  487.     THROW_ERROR("Surface::Surface(new): width > KIT_S32_MAX");
  488.  
  489.   if(height > KIT_S32_MAX)
  490.     THROW_ERROR("Surface::Surface(new): height > KIT_S32_MAX");
  491.  
  492.  
  493.  
  494.   _opq = SDL_CreateRGBSurfaceWithFormat(0, width, height,
  495.                                         SDL_BITSPERPIXEL(pixel_format),
  496.                                         pixel_format);
  497.  
  498.   if(_opq == nullptr)
  499.     THROW_ERRORF("Surface::Surface(new): \"%s\"", SDL_GetError());
  500.  
  501.  
  502.  
  503.   _valid = true;
  504.   _constructing = false;
  505.  
  506. }
  507.  
  508.  
  509.  
  510.  
  511.  
  512. Surface::Surface(const Surface& src, u32 pixel_format){
  513.   _type = KIT_CLASSTYPE_SURFACE;
  514.  
  515.   if(KIT_GET_CLASS_TYPE(&src) != KIT_CLASSTYPE_SURFACE)
  516.     THROW_ERROR("Surface::Surface(Surface&): src is not a Surface class");
  517.  
  518.  
  519.   SDL_Surface* src_surf = (SDL_Surface*)KIT_GET_CLASS_OPAQUE(&src);
  520.  
  521.   if(src_surf == nullptr)
  522.     THROW_ERROR("Surface::Surface(Surface&): src._opq = nullptr");
  523.  
  524.  
  525.  
  526.   _opq = SDL_ConvertSurfaceFormat(src_surf, pixel_format, 0);
  527.  
  528.   if(_opq == nullptr)
  529.     THROW_ERRORF("Surface::Surface(Surface&): \"%s\"", SDL_GetError());
  530.  
  531.  
  532.  
  533.   _valid = true;
  534.   _constructing = false;
  535.  
  536. }
  537.  
  538.  
  539.  
  540.  
  541.  
  542. Surface::Surface(const Window& src, u32 pixel_format){
  543.   _type = KIT_CLASSTYPE_SURFACE;
  544.  
  545.   if(KIT_GET_CLASS_TYPE(&src) != KIT_CLASSTYPE_WINDOW)
  546.     THROW_ERROR("Surface::Surface(Window&): src is not a Window class");
  547.  
  548.  
  549.  
  550.   SDL_Window*   src_win    = (SDL_Window*)KIT_GET_CLASS_OPAQUE(&src);
  551.   SDL_Surface** src_surf_p = (SDL_Surface**)&KIT_GET_CLASS_OPAQUE2(&src);
  552.  
  553.   bool src_surf_was_nullptr = *src_surf_p == nullptr;
  554.  
  555.  
  556.  
  557.   *src_surf_p = SDL_GetWindowSurface(src_win);
  558.  
  559.   if(*src_surf_p == nullptr){
  560.     _throw_sdl_error:
  561.     THROW_ERRORF("Surface::Surface(Window&): \"%s\"", SDL_GetError());
  562.   }
  563.  
  564.  
  565.  
  566.   _opq = SDL_ConvertSurfaceFormat(*src_surf_p, pixel_format, 0);
  567.  
  568.  
  569.   if(src_surf_was_nullptr){
  570.     if(SDL_DestroyWindowSurface(src_win) < 0){
  571.       if(_opq != nullptr){
  572.         SDL_FreeSurface(SURF_PTR);
  573.         _opq = nullptr;
  574.  
  575.       }
  576.  
  577.       goto _throw_sdl_error;
  578.  
  579.     }
  580.  
  581.     *src_surf_p = nullptr; //set back
  582.  
  583.   }
  584.  
  585.  
  586.   if(_opq == nullptr) goto _throw_sdl_error;
  587.  
  588.  
  589.  
  590.   _valid = true;
  591.   _constructing = false;
  592.  
  593. }
  594.  
  595.  
  596.  
  597.  
  598.  
  599. void Surface::_construct_file(const char* filePath,
  600.                               SurfaceLoaderCallback callback,
  601.                               const char* fname)
  602. {
  603.   _type = KIT_CLASSTYPE_SURFACE;
  604.  
  605.   if(fname == nullptr){
  606.     fname = "Surface::Surface(specific file format)";
  607.  
  608.     if(filePath == nullptr)
  609.       THROW_ERRORF("%s: filePath = nullptr", fname);
  610.  
  611.     if(!fileio::exists(filePath)){
  612.       THROW_ERRORF("%s: \"%s\" doesn't exist", fname, filePath);
  613.  
  614.     }
  615.  
  616.   }
  617.  
  618.  
  619.  
  620.   if(callback == nullptr){ //load .bmp file
  621.     _opq = SDL_LoadBMP(filePath);
  622.  
  623.     if(_opq == nullptr){
  624.       _throw_sdl_error:
  625.       THROW_ERRORF("%s: \"%s\"", fname, SDL_GetError());
  626.  
  627.     }
  628.  
  629.  
  630.   } else { //load some other file format
  631.     u32 format;
  632.     s32 width, height;
  633.  
  634.     void* pixels_src = callback(filePath, format, width, height);
  635.  
  636.     _opq = SDL_CreateRGBSurfaceWithFormat(0, width, height,
  637.                                           SDL_BITSPERPIXEL(format), format);
  638.  
  639.     if(_opq == nullptr){
  640.       _free_src_and_throw_sdl_error:
  641.       memory::free(&pixels_src);
  642.       goto _throw_sdl_error;
  643.  
  644.     }
  645.  
  646.  
  647.     if(SDL_LockSurface(SURF_PTR) < 0)
  648.       goto _free_src_and_throw_sdl_error;
  649.  
  650.  
  651.     void* pixels_dst  = SURF_PTR->pixels;
  652.     s32   bytesPerRow = SURF_PTR->pitch;
  653.   //s32   height      = SURF_PTR->h;
  654.  
  655.     memory::copy(pixels_dst, pixels_src, bytesPerRow*height);
  656.  
  657.     SDL_UnlockSurface(SURF_PTR);
  658.  
  659.  
  660.     memory::free(&pixels_src);
  661.  
  662.  
  663.   }
  664.  
  665.  
  666.  
  667.   _valid = true;
  668.   _constructing = false;
  669.  
  670. }
  671.  
  672.  
  673.  
  674.  
  675.  
  676. //(the last constructor can be found in "kit_Surface_LoadAllTypes.cpp")
  677.  
  678.  
  679.  
  680.  
  681.  
  682. Surface::~Surface(){
  683.   if(!_valid) return;
  684.   _valid = false;
  685.  
  686.   if(SURF_PTR != nullptr){
  687.     SDL_FreeSurface(SURF_PTR);
  688.     _opq = nullptr;
  689.  
  690.   }
  691.  
  692. }
  693.  
  694.  
  695.  
  696.  
  697.  
  698. //this is only used for saveImage, as this exists
  699.  //as a byproduct of both SDL_LockSurface and the
  700.  //SurfaceSaverCallback being able to error.
  701. //basically what this does is ensure that
  702.  //SDL_UnlockSurface is called, even if the
  703.  //SurfaceSaverCallback throws an exception
  704. struct _thing_that_locks_surface {
  705.   SDL_Surface* ptr = nullptr;
  706.   bool wasLocked   = false;
  707.  
  708.   _thing_that_locks_surface(SDL_Surface* _ptr){
  709.     if(SDL_LockSurface((ptr = _ptr)) < 0)
  710.       THROW_ERRORF("Surface::saveImage(): \"%s\"", SDL_GetError());
  711.   }
  712.  
  713.   ~_thing_that_locks_surface(){ SDL_UnlockSurface(ptr); }
  714.  
  715. };
  716.  
  717.  
  718. void Surface::saveImage(const char* filePath, SurfaceSaverCallback callback){
  719.   if(SURF_IS_INVALID)
  720.     THROW_ERROR("Surface::saveImage(): invalid Surface");
  721.  
  722.   if(filePath == nullptr)
  723.     THROW_ERROR("Surface::saveImage(): filePath = nullptr");
  724.  
  725.  
  726.  
  727.   if(callback == nullptr){ //save .bmp file
  728.     //afaik, you don't need to lock a surface before calling SDL_SaveBMP_RW,
  729.      //(hopefully i'm not wrong about that!)
  730.     if(SDL_SaveBMP(SURF_PTR, filePath) < 0)
  731.       THROW_ERRORF("Surface::saveImage(): \"%s\"", SDL_GetError());
  732.  
  733.  
  734.   } else { //save some other file format
  735.     _thing_that_locks_surface surface_locker(SURF_PTR); //LMAOOO
  736.  
  737.     callback(filePath, *this);
  738.  
  739.     //(surface should unlock after this code block,
  740.      //or if callback throws an exception)
  741.  
  742.  
  743.   }
  744.  
  745. }
  746.  
  747.  
  748.  
  749.  
  750.  
  751. /******************************************************************************/
  752.  
  753.  
  754.  
  755.  
  756.  
  757. bool Surface::hasRLE(){
  758.   if(SURF_IS_INVALID)
  759.     THROW_ERROR("Surface::hasRLE(): invalid Surface");
  760.  
  761.   return SDL_HasSurfaceRLE(SURF_PTR);
  762.  
  763. }
  764.  
  765.  
  766.  
  767.  
  768.  
  769. bool Surface::hasLockRequirement(){
  770.   if(SURF_IS_INVALID)
  771.     THROW_ERROR("Surface::hasRLE(): invalid Surface");
  772.  
  773.   return SDL_MUSTLOCK(SURF_PTR);
  774.  
  775. }
  776.  
  777.  
  778.  
  779.  
  780.  
  781. u32 Surface::getPixelFmt(){
  782.   if(SURF_IS_INVALID)
  783.     THROW_ERROR("Surface::getPixelFmt(): invalid Surface");
  784.  
  785.   return SURF_PTR->format->format;
  786.  
  787. }
  788.  
  789.  
  790.  
  791.  
  792.  
  793. shape::point Surface::getSize(){
  794.   if(SURF_IS_INVALID)
  795.     THROW_ERROR("Surface::getSize(): invalid Surface");
  796.  
  797.   return shape::point(SURF_PTR->w, SURF_PTR->h);
  798.  
  799. }
  800.  
  801.  
  802.  
  803.  
  804.  
  805. u32 Surface::getBytesPerRow(){
  806.   if(SURF_IS_INVALID)
  807.     THROW_ERROR("Surface::getBytesPerRow(): invalid Surface");
  808.  
  809.   return (u32)SURF_PTR->pitch;
  810.  
  811. }
  812.  
  813.  
  814.  
  815.  
  816.  
  817. void* Surface::getPixelData(){
  818.   if(SURF_IS_INVALID)
  819.     THROW_ERROR("Surface::getPixelData(): invalid Surface");
  820.  
  821.   return SURF_PTR->pixels;
  822.  
  823. }
  824.  
  825.  
  826.  
  827.  
  828.  
  829. void* Surface::getUserdata(){
  830.   if(SURF_IS_INVALID)
  831.     THROW_ERROR("Surface::getUserdata(): invalid Surface");
  832.  
  833.   return SURF_PTR->userdata;
  834.  
  835. }
  836.  
  837.  
  838.  
  839.  
  840.  
  841. u32 Surface::getPaletteLen(){
  842.   if(SURF_IS_INVALID)
  843.     THROW_ERROR("Surface::getPaletteLen(): invalid Surface");
  844.  
  845.   SDL_Palette* palette = SURF_PTR->format->palette;
  846.  
  847.   //idk if this is even possible, but just in case
  848.   if(palette == nullptr) return 0;
  849.  
  850.   if(!SDL_ISPIXELFORMAT_INDEXED(SURF_PTR->format->format)) return 0;
  851.  
  852.   return (u32)palette->ncolors;
  853.  
  854. }
  855.  
  856.  
  857.  
  858.  
  859.  
  860. const colors::ABGR* Surface::getPaletteColors(){
  861.   if(SURF_IS_INVALID)
  862.     THROW_ERROR("Surface::getPaletteColors(): invalid Surface");
  863.  
  864.   SDL_Palette* palette = SURF_PTR->format->palette;
  865.  
  866.   //idk if this is even possible, but just in case
  867.   if(palette == nullptr) return nullptr;
  868.  
  869.   if(!SDL_ISPIXELFORMAT_INDEXED(SURF_PTR->format->format)) return nullptr;
  870.  
  871.   return (const colors::ABGR*)palette->colors;
  872.  
  873. }
  874.  
  875.  
  876.  
  877.  
  878.  
  879. BlendModesEnum Surface::getBlendMode(){
  880.   if(SURF_IS_INVALID)
  881.     THROW_ERROR("Surface::getBlendMode(): invalid Surface");
  882.  
  883.   SDL_BlendMode blendMode;
  884.  
  885.   if(SDL_GetSurfaceBlendMode(SURF_PTR, &blendMode) < 0)
  886.     THROW_ERRORF("Surface::getBlendMode(): \"%s\"", SDL_GetError());
  887.  
  888.   return (BlendModesEnum)blendMode;
  889.  
  890. }
  891.  
  892.  
  893.  
  894.  
  895.  
  896. u8 Surface::getAlphaMod(){
  897.   if(SURF_IS_INVALID)
  898.     THROW_ERROR("Surface::getAlphaMod(): invalid Surface");
  899.  
  900.   u8 alphaMod;
  901.  
  902.   if(SDL_GetSurfaceAlphaMod(SURF_PTR, & alphaMod) < 0)
  903.     THROW_ERRORF("Surface::getAlphaMod(): \"%s\"", SDL_GetError());
  904.  
  905.   return alphaMod;
  906.  
  907. }
  908.  
  909.  
  910.  
  911.  
  912.  
  913. colors::BGR Surface::getColorMod(){
  914.   if(SURF_IS_INVALID)
  915.     THROW_ERROR("Surface::getColorMod(): invalid Surface");
  916.  
  917.   u8 r, g, b;
  918.  
  919.   if(SDL_GetSurfaceColorMod(SURF_PTR, &r, &g, &b) < 0)
  920.     THROW_ERRORF("Surface::getColorMod(): \"%s\"", SDL_GetError());
  921.  
  922.   return colors::BGR888(r, g, b);
  923.  
  924. }
  925.  
  926.  
  927.  
  928.  
  929.  
  930. u32 Surface::getColorKey(){
  931.   if(SURF_IS_INVALID)
  932.     THROW_ERROR("Surface::getColorKey(): invalid Surface");
  933.  
  934.   u32 key;
  935.  
  936.   if(SDL_GetColorKey(SURF_PTR, &key) < 0)
  937.     THROW_ERRORF("Surface::getColorKey(): \"%s\"", SDL_GetError());
  938.  
  939.   return key;
  940.  
  941. }
  942.  
  943.  
  944.  
  945.  
  946.  
  947. /******************************************************************************/
  948.  
  949.  
  950.  
  951.  
  952.  
  953. void Surface::setRLE(bool enable){
  954.   if(SURF_IS_INVALID)
  955.     THROW_ERROR("Surface::setRLE(): invalid Surface");
  956.  
  957.   if(SDL_SetSurfaceRLE(SURF_PTR, enable) < 0)
  958.     THROW_ERRORF("Surface::setRLE(): \"%s\"", SDL_GetError());
  959.  
  960. }
  961.  
  962.  
  963.  
  964.  
  965.  
  966. void Surface::setUserdata(void* userdata){
  967.   if(SURF_IS_INVALID)
  968.     THROW_ERROR("Surface::setUserdata(): invalid Surface");
  969.  
  970.   SURF_PTR->userdata = userdata;
  971.  
  972. }
  973.  
  974.  
  975.  
  976.  
  977.  
  978. void Surface::setPaletteColors(const colors::ABGR* newColors,
  979.                                u32 numColors, u32 firstColor)
  980. {
  981.   if(SURF_IS_INVALID)
  982.     THROW_ERROR("Surface::setPaletteColors(): invalid Surface");
  983.  
  984.   if(newColors == nullptr)
  985.     THROW_ERROR("Surface::setPaletteColors(): newColors = nullptr");
  986.  
  987.   if(numColors > KIT_S32_MAX)
  988.     THROW_ERROR("Surface::setPaletteColors(): numColors > KIT_S32_MAX");
  989.  
  990.   if(firstColor > KIT_S32_MAX)
  991.     THROW_ERROR("Surface::setPaletteColors(): firstColor > KIT_S32_MAX");
  992.  
  993.  
  994.   SDL_Palette* palette = SURF_PTR->format->palette;
  995.  
  996.   if(!SDL_ISPIXELFORMAT_INDEXED(SURF_PTR->format->format)  ||
  997.      palette == nullptr  ||  !palette->ncolors)
  998.   {
  999.     THROW_ERROR("Surface::setPaletteColors(): Surface doesn't use a palette");
  1000.   }
  1001.  
  1002.  
  1003.   if(SDL_SetPaletteColors(palette, (SDL_Color*)newColors, firstColor, numColors) < 0)
  1004.     THROW_ERRORF("Surface::setPaletteColors(): \"%s\"", SDL_GetError());
  1005.  
  1006. }
  1007.  
  1008.  
  1009.  
  1010.  
  1011.  
  1012. void Surface::setBlendMode(BlendModesEnum blendMode){
  1013.   if(SURF_IS_INVALID)
  1014.     THROW_ERROR("Surface::setBlendMode(): invalid Surface");
  1015.  
  1016.   if(SDL_SetSurfaceBlendMode(SURF_PTR, (SDL_BlendMode)blendMode) < 0)
  1017.     THROW_ERRORF("Surface::setBlendMode(): \"%s\"", SDL_GetError());
  1018.  
  1019. }
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025. void Surface::setAlphaMod(u8 alphaMod){
  1026.   if(SURF_IS_INVALID)
  1027.     THROW_ERROR("Surface::setAlphaMod(): invalid Surface");
  1028.  
  1029.   if(SDL_SetSurfaceAlphaMod(SURF_PTR, alphaMod) < 0)
  1030.     THROW_ERRORF("Surface::setAlphaMod(): \"%s\"", SDL_GetError());
  1031.  
  1032. }
  1033.  
  1034.  
  1035.  
  1036.  
  1037.  
  1038. void Surface::setColorMod(colors::BGR colorMod){
  1039.   if(SURF_IS_INVALID)
  1040.     THROW_ERROR("Surface::setColorMod(): invalid Surface");
  1041.  
  1042.   if(SDL_SetSurfaceColorMod(SURF_PTR, colorMod.r, colorMod.g, colorMod.b) < 0)
  1043.     THROW_ERRORF("Surface::setColorMod(): \"%s\"", SDL_GetError());
  1044.  
  1045. }
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051. void Surface::setColorKey(bool enable, u32 key){
  1052.   if(SURF_IS_INVALID)
  1053.     THROW_ERROR("Surface::setColorKey(): invalid Surface");
  1054.  
  1055.   if(SDL_SetColorKey(SURF_PTR, enable, key) < 0)
  1056.     THROW_ERRORF("Surface::setColorKey(): \"%s\"", SDL_GetError());
  1057.  
  1058. }
  1059.  
  1060.  
  1061.  
  1062.  
  1063.  
  1064. /******************************************************************************/
  1065.  
  1066.  
  1067.  
  1068.  
  1069.  
  1070. u32 Surface::mapRGB(colors::BGR inputColor){
  1071.   if(SURF_IS_INVALID)
  1072.     THROW_ERROR("Surface::mapRGB(): invalid Surface");
  1073.  
  1074.   return SDL_MapRGB(SURF_PTR->format, inputColor.r, inputColor.g, inputColor.b);
  1075.  
  1076. }
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082. u32 Surface::mapRGBA(colors::ABGR inputColor){
  1083.   if(SURF_IS_INVALID)
  1084.     THROW_ERROR("Surface::mapRGBA(): invalid Surface");
  1085.  
  1086.   return SDL_MapRGBA(SURF_PTR->format, inputColor.r, inputColor.g,
  1087.                                        inputColor.b, inputColor.a);
  1088.  
  1089. }
  1090.  
  1091.  
  1092.  
  1093.  
  1094.  
  1095. void Surface::lock(bool locked){
  1096.   if(SURF_IS_INVALID)
  1097.     THROW_ERROR("Surface::lock(): invalid Surface");
  1098.  
  1099.  
  1100.   if(locked){
  1101.     if(SDL_LockSurface(SURF_PTR) < 0)
  1102.       THROW_ERRORF("Surface::lock(): \"%s\"", SDL_GetError());
  1103.  
  1104.   } else {
  1105.     SDL_UnlockSurface(SURF_PTR);
  1106.  
  1107.   }
  1108.  
  1109. }
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115. }; /* namespace kit */
  1116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement