Advertisement
Kitomas

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

Nov 22nd, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 30.06 KB | None | 0 0
  1. /******************************************************************************/
  2. /******************************************************************************/
  3. //"2024-11-21\include\kit\_video_PixelFmt.hpp":
  4. #ifndef _INC__VIDEO_PIXELFMT_HPP
  5. #define _INC__VIDEO_PIXELFMT_HPP
  6.  
  7. #include "commondef.hpp"
  8.  
  9.  
  10. namespace kit {
  11.  
  12.  
  13.  
  14.  
  15.  
  16. enum BlendModesEnum {
  17.   BLENDMODE_NONE  = 0x00000000, //no blending
  18.                                   //dstRGBA = srcRGBA
  19.   BLENDMODE_BLEND = 0x00000001, //alpha blending
  20.                                   //dstRGB = (srcRGB * srcA) + (dstRGB * (1-srcA))
  21.                                   //dstA = srcA + (dstA * (1-srcA))
  22.   BLENDMODE_ADD   = 0x00000002, //additive blending
  23.                                   //dstRGB = (srcRGB * srcA) + dstRGB
  24.                                   //dstA = dstA
  25.   BLENDMODE_MOD   = 0x00000004, //color modulate
  26.                                   //dstRGB = srcRGB * dstRGB
  27.                                   //dstA = dstA
  28.   BLENDMODE_MUL   = 0x00000008, //color multiply
  29.                                   //dstRGB = (srcRGB * dstRGB) + (dstRGB * (1-srcA))
  30.                                   //dstA = dstA
  31.  
  32.   BLENDMODE_INVALID = 0x7FFFFFFF,
  33.  
  34. };
  35.  
  36.  
  37.  
  38.  
  39.  
  40. enum PixelTypeEnum {
  41.   PIXELTYPE_UNKNOWN,
  42.   PIXELTYPE_INDEX1,
  43.   PIXELTYPE_INDEX4,
  44.   PIXELTYPE_INDEX8,
  45.   PIXELTYPE_PACKED8,
  46.   PIXELTYPE_PACKED16,
  47.   PIXELTYPE_PACKED32,
  48.   PIXELTYPE_ARRAYU8,
  49.   PIXELTYPE_ARRAYU16,
  50.   PIXELTYPE_ARRAYU32,
  51.   PIXELTYPE_ARRAYF16,
  52.   PIXELTYPE_ARRAYF32,
  53. };
  54.  
  55. enum PixelOrderEnum { //bitmap pixel order, high bit -> low bit
  56.   PIXELORDER_NONE,
  57.   PIXELORDER_4321,
  58.   PIXELORDER_1234,
  59. };
  60.  
  61. enum PackedOrderEnum { //packed component order, high bit -> low bit
  62.   PACKEDORDER_NONE,
  63.   PACKEDORDER_XRGB,
  64.   PACKEDORDER_RGBX,
  65.   PACKEDORDER_ARGB,
  66.   PACKEDORDER_RGBA,
  67.   PACKEDORDER_XBGR,
  68.   PACKEDORDER_BGRX,
  69.   PACKEDORDER_ABGR,
  70.   PACKEDORDER_BGRA,
  71. };
  72.  
  73. enum ArrayOrderEnum { //array component order, low byte -> high byte
  74.   ARRAYORDER_NONE,
  75.   ARRAYORDER_RGB,
  76.   ARRAYORDER_RGBA,
  77.   ARRAYORDER_ARGB,
  78.   ARRAYORDER_BGR,
  79.   ARRAYORDER_BGRA,
  80.   ARRAYORDER_ABGR,
  81. };
  82.  
  83. enum PackedLayoutEnum { //packed component layout
  84.   PACKEDLAYOUT_NONE,
  85.   PACKEDLAYOUT_332,
  86.   PACKEDLAYOUT_4444,
  87.   PACKEDLAYOUT_1555,
  88.   PACKEDLAYOUT_5551,
  89.   PACKEDLAYOUT_565,
  90.   PACKEDLAYOUT_8888,
  91.   PACKEDLAYOUT_2101010,
  92.   PACKEDLAYOUT_1010102,
  93. };
  94.  
  95. #define KIT_DEFINE_PIXELFOURCC(A, B, C, D) ( \
  96.   (  ((u32)((u8)(A)))<< 0  )  | \
  97.   (  ((u32)((u8)(B)))<< 8  )  | \
  98.   (  ((u32)((u8)(C)))<<16  )  | \
  99.   (  ((u32)((u8)(D)))<<24  )    )
  100.  
  101. #define KIT_DEF_PIXELFMT(type, order, layout, bits, bytes) \
  102.     ((1 << 28) | ((type) << 24) | ((order) << 20) | ((layout) << 16) | \
  103.      ((bits) << 8) | ((bytes) << 0))
  104.  
  105. #define KIT_PIXELFLAG(X)    (((X) >> 28) & 0x0F)
  106. #define KIT_PIXELTYPE(X)    (((X) >> 24) & 0x0F)
  107. #define KIT_PIXELORDER(X)   (((X) >> 20) & 0x0F)
  108. #define KIT_PIXELLAYOUT(X)  (((X) >> 16) & 0x0F)
  109. #define KIT_BITSPERPIXEL(X) (((X) >> 8) & 0xFF)
  110. #define KIT_BYTESPERPIXEL(X) \
  111.     (KIT_ISPIXELFORMAT_FOURCC(X) ? \
  112.         ((((X) == PIXELFMT_YUY2) || \
  113.           ((X) == PIXELFMT_UYVY) || \
  114.           ((X) == PIXELFMT_YVYU)) ? 2 : 1) : (((X) >> 0) & 0xFF))
  115.  
  116. #define KIT_ISPIXELFORMAT_INDEXED(format)   \
  117.     (!KIT_ISPIXELFORMAT_FOURCC(format) && \
  118.      ((KIT_PIXELTYPE(format) == PIXELTYPE_INDEX1) || \
  119.       (KIT_PIXELTYPE(format) == PIXELTYPE_INDEX4) || \
  120.       (KIT_PIXELTYPE(format) == PIXELTYPE_INDEX8)))
  121.  
  122. #define KIT_ISPIXELFORMAT_PACKED(format) \
  123.     (!KIT_ISPIXELFORMAT_FOURCC(format) && \
  124.      ((KIT_PIXELTYPE(format) == PIXELTYPE_PACKED8) || \
  125.       (KIT_PIXELTYPE(format) == PIXELTYPE_PACKED16) || \
  126.       (KIT_PIXELTYPE(format) == PIXELTYPE_PACKED32)))
  127.  
  128. #define KIT_ISPIXELFORMAT_ARRAY(format) \
  129.     (!KIT_ISPIXELFORMAT_FOURCC(format) && \
  130.      ((KIT_PIXELTYPE(format) == PIXELTYPE_ARRAYU8) || \
  131.       (KIT_PIXELTYPE(format) == PIXELTYPE_ARRAYU16) || \
  132.       (KIT_PIXELTYPE(format) == PIXELTYPE_ARRAYU32) || \
  133.       (KIT_PIXELTYPE(format) == PIXELTYPE_ARRAYF16) || \
  134.       (KIT_PIXELTYPE(format) == PIXELTYPE_ARRAYF32)))
  135.  
  136. #define KIT_ISPIXELFORMAT_ALPHA(format)   \
  137.     ((KIT_ISPIXELFORMAT_PACKED(format) && \
  138.      ((KIT_PIXELORDER(format) == PACKEDORDER_ARGB) || \
  139.       (KIT_PIXELORDER(format) == PACKEDORDER_RGBA) || \
  140.       (KIT_PIXELORDER(format) == PACKEDORDER_ABGR) || \
  141.       (KIT_PIXELORDER(format) == PACKEDORDER_BGRA))) || \
  142.     (KIT_ISPIXELFORMAT_ARRAY(format) && \
  143.      ((KIT_PIXELORDER(format) == ARRAYORDER_ARGB) || \
  144.       (KIT_PIXELORDER(format) == ARRAYORDER_RGBA) || \
  145.       (KIT_PIXELORDER(format) == ARRAYORDER_ABGR) || \
  146.       (KIT_PIXELORDER(format) == ARRAYORDER_BGRA))))
  147.  
  148. //The flag is set to 1 because 0x1? is not in the printable ASCII range
  149. #define KIT_ISPIXELFORMAT_FOURCC(format)    \
  150.     ((format) && (KIT_PIXELFLAG(format) != 1))
  151.  
  152. enum PixelFormatEnum {
  153.     PIXELFMT_UNKNOWN,
  154.     PIXELFMT_INDEX1LSB   = KIT_DEF_PIXELFMT(PIXELTYPE_INDEX1, PIXELORDER_4321, 0, 1, 0),
  155.     PIXELFMT_INDEX1MSB   = KIT_DEF_PIXELFMT(PIXELTYPE_INDEX1, PIXELORDER_1234, 0, 1, 0),
  156.     PIXELFMT_INDEX4LSB   = KIT_DEF_PIXELFMT(PIXELTYPE_INDEX4, PIXELORDER_4321, 0, 4, 0),
  157.     PIXELFMT_INDEX4MSB   = KIT_DEF_PIXELFMT(PIXELTYPE_INDEX4, PIXELORDER_1234, 0, 4, 0),
  158.     PIXELFMT_INDEX8      = KIT_DEF_PIXELFMT(PIXELTYPE_INDEX8,               0, 0, 8, 1),
  159.  
  160.     PIXELFMT_RGB332      = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED8 , PACKEDORDER_XRGB, PACKEDLAYOUT_332 ,  8, 1),
  161.     PIXELFMT_XRGB4444    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_XRGB, PACKEDLAYOUT_4444, 12, 2),
  162.     PIXELFMT_XBGR4444    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_XBGR, PACKEDLAYOUT_4444, 12, 2),
  163.     PIXELFMT_XRGB1555    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_XRGB, PACKEDLAYOUT_1555, 15, 2),
  164.     PIXELFMT_XBGR1555    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_XBGR, PACKEDLAYOUT_1555, 15, 2),
  165.     PIXELFMT_ARGB4444    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_ARGB, PACKEDLAYOUT_4444, 16, 2),
  166.     PIXELFMT_RGBA4444    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_RGBA, PACKEDLAYOUT_4444, 16, 2),
  167.     PIXELFMT_ABGR4444    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_ABGR, PACKEDLAYOUT_4444, 16, 2),
  168.     PIXELFMT_BGRA4444    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_BGRA, PACKEDLAYOUT_4444, 16, 2),
  169.     PIXELFMT_ARGB1555    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_ARGB, PACKEDLAYOUT_1555, 16, 2),
  170.     PIXELFMT_RGBA5551    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_RGBA, PACKEDLAYOUT_5551, 16, 2),
  171.     PIXELFMT_ABGR1555    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_ABGR, PACKEDLAYOUT_1555, 16, 2),
  172.     PIXELFMT_BGRA5551    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_BGRA, PACKEDLAYOUT_5551, 16, 2),
  173.     PIXELFMT_RGB565      = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_XRGB, PACKEDLAYOUT_565 , 16, 2),
  174.     PIXELFMT_BGR565      = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED16, PACKEDORDER_XBGR, PACKEDLAYOUT_565 , 16, 2),
  175.  
  176.     PIXELFMT_RGB24       = KIT_DEF_PIXELFMT(PIXELTYPE_ARRAYU8, ARRAYORDER_RGB, 0, 24, 3),
  177.     PIXELFMT_BGR24       = KIT_DEF_PIXELFMT(PIXELTYPE_ARRAYU8, ARRAYORDER_BGR, 0, 24, 3),
  178.  
  179.     PIXELFMT_XRGB8888    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED32, PACKEDORDER_XRGB, PACKEDLAYOUT_8888, 24, 4),
  180.     PIXELFMT_RGBX8888    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED32, PACKEDORDER_RGBX, PACKEDLAYOUT_8888, 24, 4),
  181.     PIXELFMT_XBGR8888    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED32, PACKEDORDER_XBGR, PACKEDLAYOUT_8888, 24, 4),
  182.     PIXELFMT_BGRX8888    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED32, PACKEDORDER_BGRX, PACKEDLAYOUT_8888, 24, 4),
  183.     PIXELFMT_ARGB8888    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED32, PACKEDORDER_ARGB, PACKEDLAYOUT_8888, 32, 4),
  184.     PIXELFMT_RGBA8888    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED32, PACKEDORDER_RGBA, PACKEDLAYOUT_8888, 32, 4),
  185.     PIXELFMT_ABGR8888    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED32, PACKEDORDER_ABGR, PACKEDLAYOUT_8888, 32, 4),
  186.     PIXELFMT_BGRA8888    = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED32, PACKEDORDER_BGRA, PACKEDLAYOUT_8888, 32, 4),
  187.     PIXELFMT_ARGB2101010 = KIT_DEF_PIXELFMT(PIXELTYPE_PACKED32, PACKEDORDER_ARGB, PACKEDLAYOUT_2101010, 32, 4),
  188.  
  189.  
  190.     //these are actually what is returned by getPixelFmtName(),
  191.      //so instead of "PIXELFMT_XRGB4444", it would return "PIXELFMT_RGB444"
  192.     PIXELFMT_RGB444 = PIXELFMT_XRGB4444,
  193.     PIXELFMT_BGR444 = PIXELFMT_XBGR4444,
  194.     PIXELFMT_RGB555 = PIXELFMT_XRGB1555,
  195.     PIXELFMT_BGR555 = PIXELFMT_XBGR1555,
  196.     PIXELFMT_RGB888 = PIXELFMT_XRGB8888,
  197.     PIXELFMT_BGR888 = PIXELFMT_XBGR8888,
  198.  
  199.     //aliases for convenenience
  200.     PIXELFMT_RGBA32 = PIXELFMT_ABGR8888,
  201.     PIXELFMT_ARGB32 = PIXELFMT_BGRA8888,
  202.     PIXELFMT_BGRA32 = PIXELFMT_ARGB8888,
  203.     PIXELFMT_ABGR32 = PIXELFMT_RGBA8888,
  204.  
  205.  
  206.     PIXELFMT_YV12 = KIT_DEFINE_PIXELFOURCC('Y', 'V', '1', '2'), //Planar mode: Y + V + U            (3 planes)
  207.     PIXELFMT_IYUV = KIT_DEFINE_PIXELFOURCC('I', 'Y', 'U', 'V'), //Planar mode: Y + U + V            (3 planes)
  208.     PIXELFMT_YUY2 = KIT_DEFINE_PIXELFOURCC('Y', 'U', 'Y', '2'), //Packed mode: Y0 + U0 + Y1 + V0    (1 plane )
  209.     PIXELFMT_UYVY = KIT_DEFINE_PIXELFOURCC('U', 'Y', 'V', 'Y'), //Packed mode: U0 + Y0 + V0 + Y1    (1 plane )
  210.     PIXELFMT_YVYU = KIT_DEFINE_PIXELFOURCC('Y', 'V', 'Y', 'U'), //Packed mode: Y0 + V0 + Y1 + U0    (1 plane )
  211.     PIXELFMT_NV12 = KIT_DEFINE_PIXELFOURCC('N', 'V', '1', '2'), //Planar mode: Y + U/V interleaved  (2 planes)
  212.     PIXELFMT_NV21 = KIT_DEFINE_PIXELFOURCC('N', 'V', '2', '1'), //Planar mode: Y + V/U interleaved  (2 planes)
  213. };
  214.  
  215.  
  216.  
  217.  
  218.  
  219. //get a human-readable string for a given pixel format
  220. const char* getPixelFmtName(u32 pixel_format);
  221.  
  222.  
  223.  
  224.  
  225.  
  226. }; /* namespace kit */
  227.  
  228. #endif /* _INC__VIDEO_PIXELFMT_HPP */
  229. /******************************************************************************/
  230. /******************************************************************************/
  231. //"2024-11-21\include\kit\_video_Surface.hpp":
  232. #ifndef _INC__VIDEO_SURFACE_HPP
  233. #define _INC__VIDEO_SURFACE_HPP
  234.  
  235. #include "commondef.hpp"
  236. #include "_video_PixelFmt.hpp"
  237.  
  238.  
  239. namespace kit {
  240.  
  241.  
  242.  
  243.  
  244.  
  245. class Surface; //forward declaration
  246.  
  247. //like AudioData, the pointer this returns must have been allocated with
  248.  //memory:alloc specifically (NOT memory::allocSIMD!)
  249. //(also, this doesn't really account for indexed color formats, as while you can specify
  250.  //the palette indexes in the pixel data, the actual pallete will need to be set manually)
  251. typedef void* (*SurfaceLoaderCallback)(const char* filePath, u32& format_out,
  252.                                        u31& width_out, u31& height_out);
  253.  
  254. typedef void (*SurfaceSaverCallback)(const char* filePath, Surface& surface_in);
  255.  
  256. //(like AudioData, when saving OR loading, filePath will never be nullptr by the
  257.  //time the callback is invoked, so you don't need to worry about checking for it)
  258.  
  259.  
  260.  
  261. //(not an actual callback, but rather the lack of one)
  262. #define SurfaceLoadBMP ((SurfaceLoaderCallback)nullptr)
  263. #define SurfaceSaveBMP ((SurfaceSaverCallback)nullptr)
  264.  
  265. void* SurfaceLoadQOI(const char* fP, u32& fmt_o, u31& w_o, u31& h_o);
  266. void  SurfaceSaveQOI(const char* fP, Surface& s_i);
  267.  
  268. void* SurfaceLoadPNG(const char* fP, u32& fmt_o, u31& w_o, u31& h_o);
  269. void  SurfaceSavePNG(const char* fP, Surface& s_i);
  270.  
  271.  
  272.  
  273.  
  274.  
  275. //(specifically, these are the save/load callbacks that come bundled with kit_sdl2,
  276. // though you can in theory make your own for any file format you want! :D)
  277.  
  278. //currently supported image file formats:
  279.  //.bmp (SurfaceLoadBMP, SurfaceSaveBMP)
  280.  //.qoi (SurfaceLoadQOI, SurfaceSaveQOI)
  281.  //.png (SurfaceLoadPNG, SurfaceSavePNG)
  282.  
  283. class Window; //forward declaration
  284.  
  285. class Surface { //16B
  286.   u32          _type;
  287.   bool        _valid = false;
  288.   bool _constructing = true;
  289.   u16     _padding16;
  290.   GenOpqPtr     _opq = nullptr;
  291.  
  292.  
  293.   //used by both of the 'load from file' constructors
  294.   void _construct_file(const char* filePath, SurfaceLoaderCallback callback,
  295.                        const char* funcName = nullptr);
  296.  
  297.  
  298. public:
  299.  
  300.   //create a new, completely blank surface
  301.   Surface(u32 width, u32 height, u32 pixel_format);
  302.  
  303.   //create from another Surface, converting to a new pixel format in the process
  304.   Surface(const Surface& src, u32 pixel_format);
  305.  
  306.   //create from a Window's surface, converting to a new pixel format in the process
  307.   Surface(const Window& src, u32 pixel_format);
  308.  
  309.   //create from an image file of a specific file format
  310.    //(may reduce binary size if you only plan to use 1 file type or something)
  311.   Surface(const char* filePath, SurfaceLoaderCallback callback)
  312.   { _construct_file(filePath, callback); }
  313.  
  314.   //create from an image file of any supported format (.png, .qoi, .bmp)
  315.   Surface(const char* filePath);
  316.  
  317.   ~Surface();
  318.  
  319.  
  320.  
  321.   //(this will overwrite any file named filePath! make sure to check
  322.    //with fileio::exists() unless you intend to overwrite the previous file)
  323.   void saveImage(const char* filePath, SurfaceSaverCallback callback);
  324.  
  325.  
  326.  
  327.   //"If RLE is enabled, color key and alpha blending blits are much faster,
  328.    //but the surface must be locked before directly accessing the pixels."
  329.   bool                hasRLE();
  330.   bool                hasLockRequirement();
  331.   u32                 getPixelFmt();
  332.   shape::point        getSize(); //{width, height}
  333.   u32                 getBytesPerRow();
  334.   void*               getPixelData();
  335.   void*               getUserdata();
  336.   u32                 getPaletteLen(); //returns 0 if Surface doesn't use a palette
  337.   const colors::ABGR* getPaletteColors(); //returns nullptr if Surface doesn't use a palette
  338.   BlendModesEnum      getBlendMode();
  339.   u8                  getAlphaMod();
  340.   colors::BGR         getColorMod();
  341.   u32                 getColorKey();
  342.  
  343.  
  344.   void setRLE(bool enable);
  345.   void setUserdata(void* userdata);
  346.   void setPaletteColors(const colors::ABGR* newColors, u32 numColors, u32 firstColor = 0);
  347.   void setBlendMode(BlendModesEnum blendMode);
  348.   void setAlphaMod(u8 alphaMod);
  349.   void setColorMod(colors::BGR colorMod);
  350.   void setColorKey(bool enable, u32 key);
  351.  
  352.  
  353.   u32 mapRGB(colors::BGR inputColor);
  354.   u32 mapRGBA(colors::ABGR inputColor);
  355.  
  356.   void lock(bool locked = true);
  357.   inline void unlock(){ lock(false); }
  358.  
  359.  
  360.  
  361.   void fillRects(colors::ABGR color, const shape::rect* rects = nullptr, size_t rects_len = 0);
  362.  
  363.  
  364.   //w & h of dst are ignored by blit, but not blitScaled
  365.    //(also ignored by blitAt unless scale != 1.0f)
  366.   void blit(Surface& dst_surf, const shape::rect* dst = nullptr,
  367.                                const shape::rect* src = nullptr);
  368.  
  369.   void blit(Window&  dst_surf, const shape::rect* dst = nullptr,
  370.                                const shape::rect* src = nullptr);
  371.  
  372.  
  373.   void blitScaled(Surface& dst_surf, const shape::rect* dst = nullptr,
  374.                                      const shape::rect* src = nullptr);
  375.  
  376.   void blitScaled(Window&  dst_surf, const shape::rect* dst = nullptr,
  377.                                      const shape::rect* src = nullptr);
  378.  
  379.  
  380.   //centered blit (as in, x&y is the center of the blit's destination)
  381.   void blitAt(Surface& dst_surf,  s32 x, s32 y, f32 scale = 1.0f);
  382.  
  383.   void blitAt(Window&  dst_surf,  s32 x, s32 y, f32 scale = 1.0f);
  384.  
  385. };
  386.  
  387.  
  388.  
  389.  
  390.  
  391. }; /* namespace kit */
  392.  
  393. #endif /* _INC__VIDEO_SURFACE_HPP */
  394. /******************************************************************************/
  395. /******************************************************************************/
  396. //"2024-11-21\include\kit\_video_Window.hpp":
  397. #ifndef _INC__VIDEO_WINDOW_HPP
  398. #define _INC__VIDEO_WINDOW_HPP
  399.  
  400. #include "commondef.hpp"
  401. #include "_misc_Mutex.hpp"
  402.  
  403.  
  404. namespace kit {
  405.  
  406.  
  407.  
  408.  
  409.  
  410. enum WindowPositionEnum {
  411.   WINPOS_UNDEFINED = 0x1FFF0000u,
  412.   WINPOS_CENTERED  = 0x2FFF0000u,
  413. };
  414.  
  415.  
  416.  
  417. enum WindowFlagEnum {
  418.   WINFLAG_FULLSCREEN         = 0x00000001, //fullscreen window
  419.   WINFLAG_OPENGL             = 0x00000002, //window usable with an opengl context
  420.   WINFLAG_HIDDEN             = 0x00000008, //window is not visible
  421.   WINFLAG_BORDERLESS         = 0x00000010, //no window decoration
  422.   WINFLAG_RESIZABLE          = 0x00000020, //window can be resized
  423.   WINFLAG_MINIMIZED          = 0x00000040, //window is minimized
  424.   WINFLAG_MAXIMIZED          = 0x00000080, //window is maximized
  425.   WINFLAG_INPUT_GRABBED      = 0x00000100, //window has grabbed input focus
  426.   WINFLAG_FULLSCREEN_DESKTOP = 0x00001000|WINFLAG_FULLSCREEN, //fs window at desktop resolution
  427.   WINFLAG_VULKAN             = 0x10000000, //window usable with a vulkan instance
  428. };
  429.  
  430.  
  431.  
  432.  
  433.  
  434. struct DisplayMode { //16B
  435.   u32 pixelFmt;
  436.   s32 w, h;
  437.   s32 refreshRate;
  438. };
  439.  
  440.  
  441.  
  442.  
  443.  
  444. class Surface; //forward declaration
  445.  
  446. class Window { //40B (24B + sizeof(Mutex))
  447.   u32          _type;
  448.   bool        _valid = false;
  449.   bool _constructing = true;
  450.   u16     _padding16;
  451.   GenOpqPtr     _win = nullptr;
  452.   GenOpqPtr    _surf = nullptr; //window's surface (internal)
  453.   Mutex        _lock; //mostly for mutex'ing surface accesses
  454.  
  455.  
  456. public:
  457.  
  458.   Window(const char* winTitle,
  459.          u31 winWidth, u31 winHeight,
  460.          u32 winFlags = 0,
  461.          s32 winX = WINPOS_UNDEFINED,
  462.          s32 winY = WINPOS_UNDEFINED);
  463.  
  464.   ~Window();
  465.  
  466.  
  467.   inline bool isValid()       { return _valid;        }
  468.   inline bool isConstructing(){ return _constructing; }
  469.  
  470.   inline void   lock(bool locked = true){ _lock.lock(locked); }
  471.   inline void unlock()                  { _lock.lock(false ); }
  472.  
  473.  
  474.   bool         hasSurface();
  475.   u32          getPixelFormat(); //returns pixel format used by window's surface
  476.   u32          getID();
  477.   u32          getDisplayIndex();
  478.   void*        getUserdata(const char* name);
  479.   DisplayMode  getDisplayMode(); //get display mode when visible at fullscreen
  480.   const char*  getTitle();
  481.   shape::point getSize();
  482.   u32          getFlags();
  483.   f32          getOpacity();
  484.   shape::point getMinSize();
  485.   shape::point getMaxSize();
  486.   shape::point getPosition();
  487.   bool         getGrab();
  488.   bool         getKeyboardGrab();
  489.   bool         getMouseGrab();
  490.   shape::rect  getMouseGrabRect();
  491.   f32          getBrightness(); //the window's gamma multiplier, specifically
  492.  
  493.  
  494.    //returns previous userdata pointer; name "Window class" is forbidden (as it is used internally)
  495.   void* setUserdata(const char* name, void* userdata);
  496.   void  setDisplayMode(const DisplayMode* mode); //nullptr to use win's dims & desktop's fmt & refresh rate
  497.   void  setTitle(const char* title);
  498.   void  setSize(u31 width, u31 height);
  499.   void  setVisibility(bool visible);
  500.   void  setFullscreen(u32 mode); //0,1,2 = disabled, enabled, enabled @ desktop resolution
  501.   void  setResizable(bool enable);
  502.   void  setBordered(bool enable);
  503.   void  setAlwaysOnTop(bool enable);
  504.   void  setOpacity(f32 opacity); //0.0f -> 1.0f
  505.   void  setMinSize(u31 minWidth, u31 minHeight);
  506.   void  setMaxSize(u31 maxWidth, u31 maxHeight);
  507.   void  setPosition(s32 x, s32 y);
  508.   void  setGrab(bool enable); //also includes keyboard if SDL_HINT_GRAB_KEYBOARD is set (tbd: replace macro name)
  509.   void  setKeyboardGrab(bool enable);
  510.   void  setMouseGrab(bool enable); //confines mouse cursor to window
  511.   void  setMouseGrabRect(const shape::rect* rect = nullptr); //nullptr to use entire window
  512.   void  setBrightness(f32 brightness); //gamma multiplier; 0.0f -> 1.0f
  513.   void  setIcon(Surface& icon);
  514.  
  515.  
  516.   void warpMouse(s32 x, s32 y); //teleport cursor, relative to the window's top-left corner
  517.   void minimize();
  518.   void maximize();
  519.   void restore();
  520.   void raise(); //also makes window gain input focus
  521.  
  522.  
  523.   //window surface stuff specifically
  524.  
  525.   //(renewSurface is basically SDL_GetWindowSurface(),
  526.    //but the returned ptr is handled internally)
  527.   bool renewSurface(); //returns false if the Window is invalid
  528.   void destroySurface(); //(surface is destroyed automatically when the Window is destroyed)
  529.   void updateSurface(const shape::rect* rects = nullptr, u31 rects_len = 0);
  530.   //(^^ VV leave rects as nullptr to update/fill whole surface)
  531.   void fillRects(colors::ABGR color, const shape::rect* rects = nullptr, u31 rects_len = 0);
  532.  
  533. };
  534.  
  535.  
  536.  
  537.  
  538.  
  539. //returns window if input is grabbed; nullptr otherwise
  540. Window* getGrabbedWindow();
  541.  
  542.  
  543.  
  544.  
  545.  
  546. }; /* namespace kit */
  547.  
  548. #endif /* _INC__VIDEO_WINDOW_HPP */
  549. /******************************************************************************/
  550. /******************************************************************************/
  551. //"2024-11-21\src\kit_sdl2\_kit_common.hpp":
  552. #ifndef _SRC__KIT_COMMON_HPP
  553. #define _SRC__KIT_COMMON_HPP
  554.  
  555. /* notes/todo:
  556. throw if trying to create object using an uninitialized subsystem
  557. specify which functions/methods can throw exceptions
  558. when throwing in a constructor, make sure to deallocate any memory
  559.  
  560. SDL_FlushEvent/Events
  561. SDL_HasEvent/Events
  562. SDL_JoystickEventState
  563. SDL_PeepEvents
  564. SDL_PushEvent
  565. SDL_RegisterEvents
  566. SDL_WaitEvent/EventTimeout
  567.  
  568. maybe put a mutex on numAllocations
  569.  
  570. allow user to push their own text errors,
  571.  
  572. add sampleRate conversion for AudioData that are f32 or f64
  573. also, add 'get' functions for SoundEngine
  574.  
  575. validity checks only on debug build
  576.   (only make validity checks when i have to as well)
  577.  
  578. make format conversion helper functions for Surface
  579. make constructor for Surface that doesn't convert formats,
  580.   instead just copying the source format
  581.  
  582. */
  583.  
  584.  
  585.  
  586. /*
  587. this could be added to SoundEngine at some point, but this
  588. has the possibility of introducing phase cancellation, so idk
  589.  
  590.  
  591. #define convertPan(_pan) CLAMP(_pan, -1.0f, 1.0f)
  592. //#define convertPan(_pan) CLAMP( (_pan+1.0f)*0.5f, 0.0f, 1.0f )
  593.  
  594. #define sqrt2_inv ( 0.70710678f )
  595.  
  596. //sort of a work-in-progress
  597. static inline smp_f32s& applyPan(smp_f32s& sample, f32 pan){
  598.  
  599.   if(pan < 0){
  600.     sample.l += sample.r*(-pan);
  601.     sample.r *= 1.0f+pan;
  602.  
  603.   } else if(pan > 0){
  604.     sample.r += sample.l*pan;
  605.     sample.l *= 1.0f-pan;
  606.  
  607.   }
  608.  
  609.   return sample;
  610. }
  611.  
  612. */
  613.  
  614.  
  615.  
  616.  
  617.  
  618. #include <SDL2/SDL.h>
  619.  
  620.  
  621. #ifdef _DEBUG
  622. #define _log(...) kit_LogInfo(__VA_ARGS__)
  623. #define loghere kit_LogInfo("line %4i: (%s)",__LINE__,__FILE__);
  624. #define _getnumallocs kit_LogInfo("%4i: # OF ALLOCATIONS = %u", __LINE__, (u32)memory::getNumAllocations());
  625.  
  626. #else
  627. #define _log(...)
  628. #define loghere
  629. #define _getnumallocs
  630.  
  631. #endif
  632.  
  633. /*
  634. #define DISABLE_WARNING(_name) _Pragma(#_name)
  635. #define DISABLE_WARNING_PUSH(_name) _Pragma("GCC diagnostic push") DISABLE_WARNING(_name)
  636. #define DISABLE_WARNING_POP _Pragma("GCC diagnostic pop")
  637. */
  638.  
  639.  
  640. #include <kit/all.hpp>
  641.  
  642.  
  643.  
  644.  
  645.  
  646. //class type ID macros
  647. #define KIT_OPAQUE_PRESENT (0x80000000)
  648. #define KIT_OPAQUE2_PRESENT (KIT_OPAQUE_PRESENT|0x40000000) //opaque 1 is implied
  649. #define KIT_IS_OPAQUE_PRESENT(_type) ( ((_type)&KIT_OPAQUE_PRESENT) != 0 )
  650. #define KIT_IS_OPAQUE2_PRESENT(_type) ( ((_type)&KIT_OPAQUE2_PRESENT) != 0 )
  651.  
  652. #define KIT_CLASSTYPE_NULL          (0x0000                      )
  653. #define KIT_CLASSTYPE_MUTEX         (0x0001 | KIT_OPAQUE_PRESENT )
  654. #define KIT_CLASSTYPE_THREAD        (0x0002 | KIT_OPAQUE_PRESENT )
  655. #define KIT_CLASSTYPE_BINARYDATA    (0x0003                      )
  656. #define KIT_CLASSTYPE_FILE          (0x0004 | KIT_OPAQUE_PRESENT )
  657. #define KIT_CLASSTYPE_WINDOW        (0x0005 | KIT_OPAQUE2_PRESENT)
  658. #define KIT_CLASSTYPE_SURFACE       (0x0006 | KIT_OPAQUE_PRESENT )
  659. //#define KIT_CLASSTYPE_TEXTURE       (0x0007 | KIT_OPAQUE_PRESENT )
  660. #define KIT_CLASSTYPE_AUDIODEVICE   (0x0008 | KIT_OPAQUE_PRESENT )
  661. #define KIT_CLASSTYPE_AUDIOSTREAM   (0x0009 | KIT_OPAQUE_PRESENT )
  662. #define KIT_CLASSTYPE_AUDIODATA     (0x000A                      )
  663. #define KIT_CLASSTYPE_SOUNDENGINE   (0x000B | KIT_OPAQUE2_PRESENT)
  664. #define KIT_CLASSTYPE_EVENTWATCH    (0x000C                      )
  665. #define KIT_CLASSTYPE_BFONT_SURFACE (0x000D | KIT_OPAQUE2_PRESENT)
  666.  
  667.  
  668.  
  669.  
  670.  
  671. namespace kit {
  672.  
  673.  
  674.  
  675.  
  676.  
  677. #define DISABLE_STRICT_ALIASING_WARNING \
  678.   _Pragma("GCC diagnostic push") \
  679.   _Pragma("GCC diagnostic ignored \"-Wstrict-aliasing\"")
  680. #define POP_IGNORED_WARNING \
  681.   _Pragma("GCC diagnostic pop")
  682.  
  683. #define KIT_GET_CLASS_TYPE(_ptr)         ( ((kit::_commonClassValues*)(_ptr))->type         )
  684. #define KIT_GET_CLASS_VALID(_ptr)        ( ((kit::_commonClassValues*)(_ptr))->valid        )
  685. #define KIT_GET_CLASS_CONSTRUCTING(_ptr) ( ((kit::_commonClassValues*)(_ptr))->constructing )
  686. #define KIT_GET_CLASS_DATA(_ptr)         ( ((kit::_commonClassValues*)(_ptr))->data         )
  687. #define KIT_GET_CLASS_OPAQUE(_ptr)       ( ((kit::_commonClassValues*)(_ptr))->opq          )
  688. #define KIT_GET_CLASS_OPAQUE2(_ptr)      ( ((kit::_commonClassValues*)(_ptr))->opq2         )
  689.  
  690. #define KIT_IS_CLASS_TYPE(_ptr,_type)   ( KIT_GET_CLASS_TYPE(_ptr) == (_type) )
  691. #define KIT_IS_CLASS_VALID(_ptr)        ( KIT_GET_CLASS_VALID(_ptr) != 0 )
  692. #define KIT_IS_CLASS_CONSTRUCTING(_ptr) ( KIT_GET_CLASS_CONSTRUCTING(_ptr) != 0 )
  693.  
  694. struct _commonClassValues { //8-24B (depending on KIT_OPAQUE_PRESENT/KIT_OPAQUE2_PRESENT)
  695.   u32          type;
  696.   bool        valid; //'is object fully and successfully constructed?'
  697.   bool constructing; //'is object currently inside constructor?'
  698.   //^^this is useful for if public class functions are called inside the
  699.    //constructor itself, but the valid flag is not set to true yet
  700.   u16          data; //purpose varies by class type; unused by some (or it's split into 2 u8)
  701.   GenOpqPtr     opq; //nonexistent if type lacks the KIT_OPAQUE_PRESENT flag
  702.   GenOpqPtr    opq2; //nonexistent if type lacks the KIT_OPAQUE2_PRESENT flag
  703. };
  704.  
  705.  
  706.  
  707.  
  708.  
  709. union Error { //16B
  710.   struct {
  711.     const char* _txt;
  712.     u32   _thread_id;
  713.     bool       _heap;
  714.     u8     _padding8;
  715.     u16   _padding16;
  716.   };
  717.  
  718.   struct { u64 _0, _1; };
  719.  
  720.  
  721.   Error() : _txt(nullptr), _thread_id(0), _heap(false) {}
  722.  
  723.   Error(const char* txt, bool heap = false)
  724.     : _txt(txt), _thread_id(SDL_GetThreadID(nullptr)), _heap(heap) {}
  725.  
  726.  
  727.   inline const char* text(){
  728.     return (_txt != nullptr) ? _txt : "(ERROR TEXT IS NULLPTR)"; }
  729.  
  730. };
  731.  
  732.  
  733.  
  734.  
  735.  
  736. //this limit of 8 might only be a problem if 8 errors
  737.  //are thrown at the same time or something (lol)
  738. #define FSTR_COUNT ((size_t)8)
  739. #define FSTR_LEN   ((size_t)512)
  740.  
  741. struct _globalStates {
  742.   char fstr_str[FSTR_COUNT][FSTR_LEN];
  743.   s32 fstr_which = 0;
  744.  
  745.   //normally, the user is warned if the SDL version's patch level
  746.    //is < the one kit_sdl2 was made for, but this can be bypassed
  747.    //(assuming i actually implement something that can enable this flag)
  748.   bool patch_warning_disabled;
  749.  
  750.   char _; //padding
  751.  
  752.   u16 CPUCapabilities; //some combination of CPUCapabilityFlagsEnum values
  753.  
  754.   u64 performanceFreq = 0; //how many performance counter units per second
  755.  
  756.   //(haptic & sensor subsystems are not included)
  757.   union {
  758.     u64 init_value;
  759.     struct {
  760.       bool timer;
  761.       bool audio;
  762.       bool video;          //auto-inits events
  763.       bool gamecontroller; //auto-inits joystick
  764.       bool joystick;       //auto-inits events
  765.       bool events;
  766.       bool joystick_sdl; //same as events_sdl, except with the joystick subsystem
  767.       bool events_sdl;   //'were events EXPLICITLY initialized? (as in, not auto-init)'
  768.     } init;
  769.   };
  770.  
  771.   Error* errors     = nullptr;
  772.   size_t errors_len = 0;
  773.  
  774.   SDL_mutex* lock = nullptr;
  775.  
  776. };
  777.  
  778. //found in "kit_func_init-quit.cpp"
  779. extern _globalStates _gl; //gl as in global, not opengl
  780. extern const char _fstr_failure[]; // = "(FSTR FAILED)"
  781.  
  782. //(will complain about undefined sequencing if you put fstr in a call to fstr,
  783.  //due to the of the "^=" assignment operator)
  784. #define fstr(...) ( \
  785.   (snPrintf(_gl.fstr_str[(_gl.fstr_which=(_gl.fstr_which+1)%FSTR_COUNT)],FSTR_LEN,__VA_ARGS__)>=0) \
  786.     ? (const char*)_gl.fstr_str[_gl.fstr_which] : _fstr_failure \
  787. )
  788.  
  789. #define   LOCK_GLOBALS() { if(_gl.lock != nullptr) SDL_LockMutex(  _gl.lock); }
  790. #define UNLOCK_GLOBALS() { if(_gl.lock != nullptr) SDL_UnlockMutex(_gl.lock); }
  791.  
  792.  
  793.  
  794.  
  795.  
  796. //in _kit_private.cpp:
  797. const char* _pushError(const char* errortext);
  798. bool _freeError(u32 thread_id = 0); //returns true if it found an error to free
  799. void _freeErrors(); //also frees _gl.errors entirely
  800.  
  801. #define THROW_ERROR(_txt) throw _pushError(_txt)
  802. //fstr itself is not thread safe, so let's lock globals until
  803.  //the result of fstr has been copied to the thread errors list
  804. #define THROW_ERRORF(...) { \
  805.   LOCK_GLOBALS(); \
  806.   const char* _T_EF_long_name_dont_name_anything_else_this = _pushError( fstr(__VA_ARGS__) ); \
  807.   UNLOCK_GLOBALS(); \
  808.   throw _T_EF_long_name_dont_name_anything_else_this; \
  809. }
  810.  
  811.  
  812.  
  813.  
  814.  
  815. extern size_t numAllocations;
  816.  
  817.  
  818.  
  819.  
  820.  
  821. //100ms
  822. #define FADETOTAL_SEC (0.100f)
  823.  
  824. //linearly fade over the course of 10ms
  825. #define FADEDELTA_SEC (0.010f)
  826.  
  827. //the most common audio clipping ends at 10-11ms after unpausing,
  828.  //but i've seen clipping as far as ~450ms after unpausing
  829. #define FADEDELAY_SEC (FADETOTAL_SEC - FADEDELTA_SEC)
  830.  
  831.  
  832.  
  833.  
  834.  
  835. #define ERROR_ON_NEGATIVE(_funcname, _var) \
  836.   if((_var) < 0){ THROW_ERROR(_funcname ": " #_var " < 0"); }
  837.  
  838.  
  839.  
  840.  
  841.  
  842. }; /* namespace kit */
  843.  
  844. #include "_kit_opaques.hpp"
  845.  
  846. #endif /* _SRC__KIT_COMMON_HPP */
  847. /******************************************************************************/
  848. /******************************************************************************/
  849. //"2024-11-21\src\kit_sdl2\_kit_opaques.hpp":
  850. #ifndef _SRC__KIT_OPAQUES_HPP
  851. #define _SRC__KIT_OPAQUES_HPP
  852.  
  853. //THIS FILE IS NOT TO BE INCLUDED ON ITS OWN,
  854.  //RATHER AS A PART OF "_kit_common.hpp"!!
  855.  
  856. /*
  857. Opaques are included as follows: (classes without opaques are omitted)
  858. [ ] Mutex
  859. [ ] Thread
  860. [ ] File
  861. [ ] Window
  862. [ ] Surface
  863. [*] AudioDevice
  864. [ ] AudioStream
  865. [*] SoundEngine
  866. */
  867.  
  868. namespace kit {
  869.  
  870.  
  871.  
  872.  
  873.  
  874. struct _AudioDeviceOpaque {
  875.   AudioDeviceInfo* info_p;
  876.  
  877.   void*            buffer;
  878.   u32         buffer_size;
  879.  
  880.   f32           fadeDelta;
  881.   f32          fadeVolume;
  882.   u32           fadeDelay;
  883.  
  884.   bool            fadeOut; //fade-in otherwise
  885.   bool        noFadeDelay;
  886.  
  887.   bool            playing;
  888.  
  889.   u8  _0;
  890.   u32 _1;
  891.  
  892. };
  893.  
  894.  
  895.  
  896.  
  897.  
  898. struct _SoundEngineTrack { //88B
  899.   const AudioDataHeader* audio;
  900.  
  901.   //time at the point of audio being queued, in milliseconds
  902.   u64                timestamp;
  903.  
  904.   f64                 position;
  905.  
  906.   f64                  spd_old;
  907.   f64                  spd_new;
  908.   f64                 spdDelta;
  909.  
  910.   Stereo_f32           vol_old;
  911.   Stereo_f32           vol_new;
  912.   Stereo_f32          volDelta;
  913.   Stereo_f32         volMaster;
  914.  
  915.   u16                    loops;
  916.   bool                stopping;
  917.  
  918.   char             _padding[5];
  919.  
  920. };
  921.  
  922.  
  923.  
  924.  
  925.  
  926. }; /* namespace kit */
  927.  
  928. #endif /* _SRC__KIT_OPAQUES_HPP */
  929.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement