Advertisement
Kitomas

2024-07-19 (8/13)

Jul 19th, 2024
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 32.31 KB | None | 0 0
  1. /******************************************************************************/
  2. /******************************************************************************/
  3. //"kit_w32\include\kit\misc.hpp":
  4. #ifndef _KIT_INC_MISC_HPP
  5. #define _KIT_INC_MISC_HPP
  6.  
  7. #include "commondef.hpp"
  8. #include "_misc_Mutex.hpp"
  9. #include "_misc_time.hpp"
  10. #include "_misc_fileio.hpp"
  11. #include "_misc_func.hpp"
  12. #include "_misc_memory.hpp"
  13. #include "_misc_FStr.hpp"
  14. #include "_misc_Thread.hpp"
  15. #include "_misc_Timer.hpp"
  16.  
  17.  
  18. #endif /* _KIT_INC_VIDEO_MISC_HPP */
  19. /******************************************************************************/
  20. /******************************************************************************/
  21. //"kit_w32\include\kit\video.hpp":
  22. #ifndef _KIT_INC_VIDEO_HPP
  23. #define _KIT_INC_VIDEO_HPP
  24.  
  25. #include "commondef.hpp"
  26. #include "misc.hpp"
  27. #include "_video_common.hpp"
  28. #include "_video_Window.hpp"
  29. #include "_video_Bitmap.hpp"
  30. #include "_video_BitmapFont.hpp"
  31.  
  32.  
  33. #endif /* _KIT_INC_VIDEO_HPP */
  34. /******************************************************************************/
  35. /******************************************************************************/
  36. //"kit_w32\include\kit\_audio_AudioStream.hpp":
  37. #ifndef _KIT_INC__AUDIO_AUDIOSTREAM_HPP
  38. #define _KIT_INC__AUDIO_AUDIOSTREAM_HPP
  39.  
  40. #include "commondef.hpp"
  41.  
  42.  
  43. namespace kit {
  44.  
  45.  
  46.  
  47.  
  48. //this is *mostly* lifted from SDL's AudioFormat enumeration
  49.  
  50. #define KIT_ASTREAM_FMT_BITSIZE(_fmt) ( (((_fmt)+1)&0xFF) )
  51. #define KIT_ASTREAM_FMT_BYTESIZE(_fmt) ( KIT_ASTREAM_FMT_BITSIZE(_fmt)/8 )
  52.  
  53. enum AudioStreamFormats {
  54.   ASTREAM_FMT_U8  = 0x0007,
  55.   ASTREAM_FMT_S8  = 0x8007,
  56.   ASTREAM_FMT_S16 = 0x800F,
  57.   ASTREAM_FMT_S24 = 0x8018, //(packed signed 24-bit)
  58.   ASTREAM_FMT_S32 = 0x801F,
  59.   ASTREAM_FMT_F32 = 0x811F,
  60. };
  61.  
  62.  
  63.  
  64. enum AudioStreamCallbackReturns {
  65.   ASTREAM_RTN_CONTINUE = 0, //keep invoking the callback, and keep playing
  66.   ASTREAM_RTN_COMPLETE = 1, //stop invoking the callback, and stop once all output samples have played
  67.   ASTREAM_RTN_ABORT    = 2, //stop invoking the callback, and stop the stream asap
  68. };
  69.  
  70.  
  71.  
  72.  
  73. struct AudioStreamInfo; //forward declaration
  74.  
  75. //the callback responsible for reading from input buffers,
  76.  //and/or writing to output buffers.
  77. typedef s32 (*AudioStreamCallback)(const void* inBuffer, void* outBuffer,
  78.                                    const AudioStreamInfo* info, void* userdata);
  79.  
  80.  
  81. //if registered with AudioStream::setEndCallback(),
  82.  //this function is called when a stream stops
  83.  //(unlike the regular callback, this is optional!)
  84. typedef void (*AudioStreamEndCallback)(void* userdata);
  85.  
  86.  
  87.  
  88.  
  89. struct AudioStreamInfo {
  90.   //time of first sample of each callback...
  91.   f64 timeInput;   //...at the point of ADC input capture
  92.   f64 timeCurrent; //...at the actual callback's start
  93.   f64 timeOutput;  //...at the point of DAC output render
  94.  
  95.   u32 inputBufferSize;  //in bytes, not sample frames
  96.   u32 outputBufferSize;  //
  97.  
  98.   s32 inputDeviceID;
  99.   s32 outputDeviceID;
  100.  
  101.   s32 inputChannels;
  102.   s32 outputChannels;
  103.  
  104.   u16 inputFormat;
  105.   u16 outputFormat;
  106.  
  107.   u32 sampleFrames;
  108.  
  109.   f64 sampleRate;
  110.  
  111.   f64 inputLatency;
  112.   f64 outputLatency;
  113. };
  114.  
  115.  
  116.  
  117. // in/output<parameter> fields are ignored if in/outputDeviceID is -1
  118. struct AudioStreamParams {
  119.   AudioStreamCallback callback;
  120.   void*               userdata;
  121.  
  122.   s32 inputDeviceID  = -1; //-1 for no input
  123.   s32 outputDeviceID = -1; //-1 for no output
  124.  
  125.   s32 inputChannels  = -1; //-1 for whatever is default
  126.   s32 outputChannels = -1;  //^^
  127.  
  128.   u16 inputFormat  = ASTREAM_FMT_F32; //float samples by default
  129.   u16 outputFormat = ASTREAM_FMT_F32;  //^^
  130.  
  131.   u32 sampleFrames  =  0; // 0 for undefined (may change between callbacks!)
  132.   f64 sampleRate    = -1; //-1 for whatever is default
  133.  
  134.   f64 inputSuggestedLatency  = -1.0; //-1.0 for whatever is default
  135.   f64 outputSuggestedLatency = -1.0;  //^^
  136. };
  137.  
  138.  
  139.  
  140.  
  141. struct _AudioStreamOpaque;
  142.  
  143. class AudioStream {
  144.   u32                _type;
  145.   bool              _valid = false;
  146.   bool       _constructing = true;
  147.   u16           _padding16;
  148.   _AudioStreamOpaque* _opq = nullptr;
  149.  
  150.  
  151.   void _construct(const AudioStreamParams* params);
  152.  
  153.  
  154. public:
  155.  
  156.   //normal constructor
  157.   AudioStream(AudioStreamParams* params){ _construct(params); }
  158.  
  159.   //attempts to create a default f32 output stream
  160.   AudioStream(AudioStreamCallback callback, void* userdata = nullptr,
  161.               u32 sampleFrames = 0, s32 channels = 2);
  162.  
  163.   ~AudioStream();
  164.  
  165.  
  166.   bool isValid(){ return _valid; }
  167.   bool isConstructing(){ return _constructing; }
  168.   bool isActive();
  169.  
  170.   f64 getTime();
  171.   f64 getCPULoad(); //usually 0.0 to 1.0, but may exceed 1.0 if usage is too high
  172.   AudioStreamInfo getInfo();
  173.  
  174.  
  175.   void setCallback(AudioStreamCallback newCallback);
  176.   void setEndCallback(AudioStreamEndCallback newEndCallback);
  177.  
  178.  
  179.   void lock(bool locked = true);
  180.   void unlock(){ lock(false); }
  181.  
  182.  
  183.   void start();
  184.   void stop();
  185.   void abort();
  186.  
  187. };
  188.  
  189.  
  190.  
  191.  
  192. }; /* namespace kit */
  193.  
  194. #endif /* _KIT_INC__AUDIO_AUDIOSTREAM_HPP */
  195. /******************************************************************************/
  196. /******************************************************************************/
  197. //"kit_w32\include\kit\_audio_func.hpp":
  198. #ifndef _KIT_INC__AUDIO_FUNC_HPP
  199. #define _KIT_INC__AUDIO_FUNC_HPP
  200.  
  201. #include "commondef.hpp"
  202.  
  203.  
  204. namespace kit {
  205.  
  206.  
  207.  
  208.  
  209. struct AudioDeviceInfo {
  210.   const char* name;
  211.  
  212.   struct {
  213.     s32 input;
  214.     s32 output;
  215.   } maxChannels;
  216.  
  217.   //default latency values (in seconds)
  218.    //for programs with low-interactivity
  219.    //(like an audio player, for example)
  220.   struct {
  221.     f64 input;
  222.     f64 output;
  223.   } defLoLatency;
  224.  
  225.   //default latency values (in seconds)
  226.    //for programs with high-interactivity
  227.    //(like a video game, for example)
  228.   struct {
  229.     f64 input;
  230.     f64 output;
  231.   } defHiLatency;
  232.  
  233.   //default sample rate
  234.   f64 defSampleRate;
  235. };
  236.  
  237.  
  238.  
  239.  
  240. //used by audio::areParamsSupported()
  241. struct AudioStreamParams;
  242.  
  243.  
  244.  
  245.  
  246. namespace audio {
  247.  
  248.  
  249.  
  250.  
  251. s32 getDeviceCount();
  252.  
  253.  
  254. s32 getDefInputDevice();
  255.  
  256. s32 getDefOutputDevice();
  257.  
  258.  
  259.  
  260. AudioDeviceInfo getDeviceInfo(s32 deviceID);
  261.  
  262.  
  263.  
  264.  
  265. //*reason_p (assuming reason_p != nullptr) will be filled with the
  266.  //the constant location of the text version of why the paramters
  267.  //aren't supported (assuming false was returned)
  268. bool areParamsSupported(const AudioStreamParams* params,
  269.                         const char** reason_p = nullptr);
  270.  
  271.  
  272.  
  273.  
  274. }; /* namespace audio */
  275.  
  276.  
  277. }; /* namespace kit */
  278.  
  279. #endif /* _KIT_INC__AUDIO_FUNC_HPP */
  280. /******************************************************************************/
  281. /******************************************************************************/
  282. //"kit_w32\include\kit\_misc_fileio.hpp":
  283. #ifndef _KIT_INC__MISC_FILEIO_HPP
  284. #define _KIT_INC__MISC_FILEIO_HPP
  285.  
  286. #include "commondef.hpp"
  287.  
  288.  
  289. namespace kit {
  290.  
  291.  
  292.  
  293.  
  294. class BinaryData {
  295.   u32 _type;
  296.   bool _valid        = false;
  297.   bool _constructing = true;
  298.   u16 _padding16;
  299.   char* _data        = nullptr;
  300.   size_t _data_len   = 0;
  301.  
  302.   union {
  303.     u8  n8;
  304.     u16 n16;
  305.     u32 n32;
  306.     u64 n64;
  307.     char str[8];
  308.   }* _magic; //will be set to _data
  309.  
  310.  
  311. public:
  312.  
  313.   BinaryData(const char* filePath);
  314.  
  315.   //this will allocate dataSize bytes for _data, before copying data to _data
  316.    //(it is safe to pass nullptr to data, though that would make this equivalent to malloc)
  317.   BinaryData(const void* data, size_t dataSize);
  318.  
  319.   ~BinaryData();
  320.  
  321.  
  322.   bool isValid(){ return _valid; }
  323.   bool isConstructing(){ return _constructing; }
  324.  
  325.   char*  getData(){ return _data; }
  326.   size_t getSize(){ return _data_len; }
  327.   u8     getMagic8(){ return _magic->n8; } //never seen an 8-bit magic num, but just in case lol
  328.   u16    getMagic16(){ return _magic->n16; }
  329.   u32    getMagic32(){ return _magic->n32; }
  330.   u64    getMagic64(){ return _magic->n64; }
  331.   const char* getMagicStr(){ return _magic->str; } //(not necessarily null-terminated!)
  332.  
  333. };
  334.  
  335.  
  336.  
  337.  
  338. namespace fileio {
  339.  
  340.  
  341.  
  342.  
  343. bool isFileReadOnly(const char* filePath);
  344.  
  345.  
  346.  
  347. bool fileExists(const char* filePath);
  348.  
  349.  
  350.  
  351. size_t getSize(const char* filePath);
  352.  
  353.  
  354.  
  355. void deleteFile(const char* filePath);
  356.  
  357.  
  358.  
  359. void writeToFile(const char* filePath, const void* data,
  360.                  size_t dataSize, bool append = false);
  361.  
  362.  
  363.  
  364.  
  365. }; /* namespace fileio */
  366.  
  367.  
  368. }; /* namespace kit */
  369.  
  370. #endif /* _KIT_INC__MISC_FILEIO_HPP */
  371. /******************************************************************************/
  372. /******************************************************************************/
  373. //"kit_w32\include\kit\_misc_FStr.hpp":
  374. #ifndef _KIT_INC__MISC_FSTR_HPP
  375. #define _KIT_INC__MISC_FSTR_HPP
  376.  
  377. #include "commondef.hpp"
  378.  
  379.  
  380. namespace kit {
  381.  
  382.  
  383.  
  384.  
  385. class FStr {
  386.   u32 _type;
  387.   bool _valid = false;
  388.   bool _constructing = true;
  389.   u16 _str_size;
  390.   char* _str = nullptr;
  391.  
  392.  
  393. public:
  394.  
  395.   FStr(u16 maxSize = 2048);
  396.   ~FStr();
  397.  
  398.  
  399.   bool isValid(){ return _valid; }
  400.   bool isConstructing(){ return _constructing; }
  401.  
  402.   u16 getSize(){ return _str_size; }
  403.  
  404.   char* ptr(){ return _str; }
  405.   char* fmt(const char* fmt_str, ...);
  406.  
  407. };
  408.  
  409.  
  410.  
  411.  
  412. };
  413.  
  414. #endif /* _KIT_INC__MISC_FSTR_HPP */
  415. /******************************************************************************/
  416. /******************************************************************************/
  417. //"kit_w32\include\kit\_misc_func.hpp":
  418. #ifndef _KIT_INC__MISC_FUNC_HPP
  419. #define _KIT_INC__MISC_FUNC_HPP
  420.  
  421. #include "commondef.hpp"
  422.  
  423.  
  424. namespace kit {
  425.  
  426.  
  427.  
  428.  
  429. enum MessageBoxEnum {
  430.   //return values
  431.   MSGBOX_RTN_NULL     = 0x00000000, //showMessageBox failed
  432.   MSGBOX_RTN_OK       = 0x00000001, //'ok' button was clicked
  433.   MSGBOX_RTN_CANCEL   = 0x00000002, //'cancel' button was clicked
  434.   MSGBOX_RTN_ABORT    = 0x00000003, //'abort' button was clicked
  435.   MSGBOX_RTN_RETRY    = 0x00000004, //'retry' button was clicked
  436.   MSGBOX_RTN_IGNORE   = 0x00000005, //'ignore' button was clicked
  437.   MSGBOX_RTN_YES      = 0x00000006, //'yes' button was clicked
  438.   MSGBOX_RTN_NO       = 0x00000007, //'no' button was clicked
  439.   MSGBOX_RTN_TRYAGAIN = 0x0000000A, //'try again' button was clicked
  440.   MSGBOX_RTN_CONTINUE = 0x0000000B, //'continue' button was clicked
  441.  
  442.  
  443.   //button types
  444.   MSGBOX_BTN_OK                = 0x00000000,
  445.   MSGBOX_BTN_OKCANCEL          = 0x00000001,
  446.   MSGBOX_BTN_ABORTRETRYIGNORE  = 0x00000002,
  447.   MSGBOX_BTN_YESNOCANCEL       = 0x00000003,
  448.   MSGBOX_BTN_YESNO             = 0x00000004,
  449.   MSGBOX_BTN_RETRYCANCEL       = 0x00000005,
  450.   MSGBOX_BTN_CANCELTRYCONTINUE = 0x00000006,
  451.  
  452.   //icon types
  453.   MSGBOX_ICN_ERROR    = 0x000000010,
  454.   MSGBOX_ICN_QUESTION = 0x000000020, //apparently deprecated, but still supported
  455.   MSGBOX_ICN_WARNING  = 0x000000030,
  456.   MSGBOX_ICN_INFO     = 0x000000040,
  457. };
  458.  
  459.  
  460.  
  461. class Window;
  462. u32 showMessageBox(const char* text = nullptr, const char* title = nullptr,
  463.                    u32 type = 0, Window* win = nullptr, u32 defaultButton = 0);
  464.  
  465.  
  466.  
  467.  
  468. u32 getLastSystemError();
  469.  
  470.  
  471.  
  472. u32 getNumLogicalCPUCores();
  473.  
  474.  
  475.  
  476.  
  477. union WindowEvent;
  478. //calling this function exclusively in the main thread is recommended
  479. bool pollWindowEvent(WindowEvent* event_p = nullptr);
  480.  
  481.  
  482.  
  483.  
  484. }; /* namespace kit */
  485.  
  486. #endif /* _KIT_INC__MISC_FUNC_HPP */
  487. /******************************************************************************/
  488. /******************************************************************************/
  489. //"kit_w32\include\kit\_misc_memory.hpp":
  490. #ifndef _KIT_INC__MISC_MEMORY_HPP
  491. #define _KIT_INC__MISC_MEMORY_HPP
  492.  
  493. #include "commondef.hpp"
  494.  
  495.  
  496. namespace kit {
  497.  
  498.  
  499. namespace memory {
  500.  
  501.  
  502.  
  503.  
  504. void* alloc(size_t size);
  505.  
  506. //this version of free is especially useful, because it's safe to pass nullptr to!
  507.  //this rule goes for both ptr_p and *ptr_p
  508. void free(void* ptr_p);
  509.  
  510. void* realloc(void* ptr_p, size_t newSize);
  511.  
  512. //^^(for both free and realloc, a void** declared in function as void*,
  513.  //  so you don't need to explicitly cast it to void**)
  514.  
  515.  
  516. //same as alloc and free, now with exception throwing!
  517. void* alloc2(size_t size);
  518.  
  519. void free2(void* ptr_p); //(unlike free, this will throw if nullptr is given)
  520.  
  521. void* realloc2(void* ptr_p, size_t newSize);
  522.  
  523.  
  524. size_t getNumAllocations();
  525.  
  526.  
  527.  
  528.  
  529. void* set(void* ptr, s32 value, size_t size);
  530.  
  531. //throws exceptions, unlike set
  532. void* set2(void* ptr, s32 value, size_t size);
  533.  
  534.  
  535.  
  536.  
  537. void* copy(void* destination, const void* source, size_t size);
  538.  
  539. //throws exceptions, unlike copy
  540. void* copy2(void* destination, const void* source, size_t size);
  541.  
  542.  
  543.  
  544.  
  545. }; /* namespace memory */
  546.  
  547.  
  548. }; /* namespace kit */
  549.  
  550. #endif /* _KIT_INC__MISC_MEMORY_HPP */
  551. /******************************************************************************/
  552. /******************************************************************************/
  553. //"kit_w32\include\kit\_misc_Mutex.hpp":
  554. #ifndef _KIT_INC__MISC_MUTEX_HPP
  555. #define _KIT_INC__MISC_MUTEX_HPP
  556.  
  557.  
  558. #include "commondef.hpp"
  559.  
  560.  
  561. namespace kit {
  562.  
  563.  
  564.  
  565.  
  566. //this mutex is non-blocking within the same thread!
  567.  //(that means you can lock multiple times in one thread
  568.  // as long as you unlock it the same number of times)
  569. class MutexSimple {
  570.   u32 _type;
  571.   bool _valid        = false;
  572.   bool _constructing = true;
  573.   u16 _padding16;
  574.   _GenericOpaquePtr _mutex_p = nullptr;
  575.   u32 _spinCount;
  576.  
  577.  
  578. public:
  579.  
  580.   MutexSimple(u32 spinCount = -1);
  581.  
  582.   ~MutexSimple();
  583.  
  584.  
  585.   bool isValid(){ return _valid; }
  586.   bool isConstructing(){ return _constructing; }
  587.  
  588.   u32 getSpinCount(){ return _spinCount; }
  589.  
  590.  
  591.   void setSpinCount(u32 spinCount);
  592.  
  593.  
  594.   void lock(bool locked = true);
  595.   void unlock(){ lock(false); }
  596.   bool tryLock(); //returns true if locked successfully
  597.  
  598. };
  599.  
  600.  
  601.  
  602.  
  603. }; /* namespace kit */
  604.  
  605.  
  606. #endif /* _KIT_INC__MISC_MUTEX_HPP */
  607. /******************************************************************************/
  608. /******************************************************************************/
  609. //"kit_w32\include\kit\_misc_Thread.hpp":
  610. #ifndef _KIT_INC__MISC_THREAD_HPP
  611. #define _KIT_INC__MISC_THREAD_HPP
  612.  
  613.  
  614. #include "commondef.hpp"
  615.  
  616.  
  617. namespace kit {
  618.  
  619.  
  620.  
  621.  
  622. enum ThreadPriorityEnum {
  623.   THREAD_NO_CHANGE     = -3,
  624.  
  625.   THREAD_LOWEST        = -2,
  626.   THREAD_BELOW_NORMAL  = -1,
  627.   THREAD_NORMAL        =  0,
  628.   THREAD_ABOVE_NORMAL  =  1,
  629.   THREAD_HIGHEST       =  2,
  630. };
  631.  
  632.  
  633.  
  634.  
  635. typedef s32 (*ThreadFunction)(void* userdata);
  636.  
  637.  
  638.  
  639.  
  640. struct _ThreadOpaque;
  641.  
  642. class Thread {
  643.   u32            _type;
  644.   bool          _valid = false;
  645.   bool   _constructing = true;
  646.   s8         _priority = THREAD_NORMAL;
  647.   bool _backgroundMode = false;
  648.   _ThreadOpaque*  _opq = nullptr;
  649.  
  650.  
  651. public:
  652.  
  653.   //if detached is true, the Thread object will not wait for func
  654.    //to return inside the Thread's destructor
  655.   Thread(ThreadFunction func, void* userdata = nullptr,
  656.          bool detached = false, size_t stackSize = 0);
  657.  
  658.   ~Thread();
  659.  
  660.  
  661.   bool isValid(){ return _valid; }
  662.   bool isConstructing(){ return _constructing; }
  663.   bool isDone();
  664.  
  665.   u32 getID();
  666.  
  667.  
  668.   //priority can be one of the values in ThreadPriorityEnum
  669.   void setPriority(s32 priority, bool backgroundMode = false);
  670.  
  671.  
  672.   //returns result of func (only for non-detached threads!)
  673.    //(timeout is infinite by default)
  674.   s32 waitUntilDone(u32 timeoutMilliseconds = 0xFFFFFFFF);
  675.  
  676. };
  677.  
  678.  
  679.  
  680.  
  681. }; /* namespace kit */
  682.  
  683. #endif /* _KIT_INC__MISC_THREAD_HPP */
  684. /******************************************************************************/
  685. /******************************************************************************/
  686. //"kit_w32\include\kit\_misc_time.hpp":
  687. #ifndef _KIT_INC__MISC_TIME_HPP
  688. #define _KIT_INC__MISC_TIME_HPP
  689.  
  690.  
  691. #include "commondef.hpp"
  692.  
  693.  
  694. namespace kit {
  695.  
  696.  
  697. namespace time {
  698.  
  699.  
  700.  
  701.  
  702. u64 getTicks();
  703.  
  704.  
  705. u64 getTicksPerSecond();
  706.  
  707.  
  708. f64 getUptime();
  709.  
  710.  
  711.  
  712.  
  713. void ticksleep(u64 ticks);
  714.  
  715.  
  716.  
  717. void sleep(u32 milliseconds);
  718.  
  719.  
  720.  
  721.  
  722. }; /* namespace time */
  723.  
  724.  
  725. }; /* namespace kit */
  726.  
  727. #endif /* _KIT_INC__MISC_TIME_HPP */
  728. /******************************************************************************/
  729. /******************************************************************************/
  730. //"kit_w32\include\kit\_misc_Timer.hpp":
  731. #ifndef _KIT_INC__MISC_TIMER_HPP
  732. #define _KIT_INC__MISC_TIMER_HPP
  733.  
  734.  
  735. #include "commondef.hpp"
  736.  
  737.  
  738. namespace kit {
  739.  
  740.  
  741.  
  742.  
  743. class TimerSimple {
  744.   u32                    _type;
  745.   bool                  _valid = false;
  746.   bool           _constructing = true;
  747.   u16               _padding16;
  748.   _GenericOpaquePtr _timer_ptr = nullptr;
  749.  
  750.  
  751. public:
  752.  
  753.   //only manual reset timers are *for sure* supported currently
  754.   TimerSimple(bool manualReset = true);
  755.  
  756.   ~TimerSimple();
  757.  
  758.  
  759.   void setTimer(f64 durationSeconds);
  760.  
  761.  
  762.   void wait(u32 timeoutMilliseconds = 0); //0 for waiting indefinitely
  763.  
  764.  
  765. };
  766.  
  767.  
  768.  
  769.  
  770. }; /* namespace kit */
  771.  
  772. #endif /* _KIT_INC__MISC_TIMER_HPP */
  773. /******************************************************************************/
  774. /******************************************************************************/
  775. //"kit_w32\include\kit\_video_Bitmap.hpp":
  776. #ifndef _KIT_INC__VIDEO_BITMAP_HPP
  777. #define _KIT_INC__VIDEO_BITMAP_HPP
  778.  
  779. #include "commondef.hpp"
  780. #include "_video_common.hpp"
  781.  
  782.  
  783. namespace kit {
  784.  
  785.  
  786.  
  787.  
  788. class Window;
  789. struct _BitmapOpaque;
  790.  
  791. class Bitmap {
  792.   u32 _type;
  793.   bool _valid         = false;
  794.   bool _constructing  = true;
  795.   u16 _padding16;
  796.   _BitmapOpaque* _opq = nullptr;
  797.  
  798.  
  799.   //(_parse<format> functions must free what is returned to prevent leaks)
  800.   color::ARGB* _parseQOI(BinaryData& fileData, shape::point& size);
  801.  
  802.   //separated from first constructor so it can be used by the 2nd constructor too
  803.   void _constructFromMemory(const color::ARGB* pixelData, u32 width, u32 height,
  804.                             Window* blit_dst, bool directAccess);
  805.  
  806.  
  807. public:
  808.  
  809.   //loads from memory (pixel data is copied to bitmap; it isn't just referenced)
  810.   Bitmap(const color::ARGB* pixelData, u32 width, u32 height,
  811.          Window* blit_dst, bool directAccess = false)
  812.   { _constructFromMemory(pixelData, width, height, blit_dst, directAccess); }
  813.  
  814.   //loads from a file
  815.   Bitmap(const char* filePath, Window* blit_dst, bool directAccess = false);
  816.  
  817.   ~Bitmap();
  818.  
  819.  
  820.   bool isValid(){ return _valid; }
  821.   bool isConstructing(){ return _constructing; }
  822.   bool isDirectAccess();
  823.  
  824.   color::ARGB* getPixels(); //get pointer to internal pixel data (must be direct access)
  825.   shape::point getSize();
  826.   //(size of pixelDataOut array must be at least sizeof(color::ARGB)*width*height)
  827.   void         getPixelData(color::ARGB* pixelDataOut);
  828.  
  829.   void setPixelData(const color::ARGB* pixelData, u32 width, u32 height);
  830.   void setBlitDestination(Window* blit_dst); //relatively expensive; don't call this too often
  831.  
  832.  
  833.   void lock(bool enable = true);
  834.   void unlock(){ lock(false); }
  835.  
  836.  
  837.   void blitRect(shape::rect* dst = nullptr,  shape::rect* src = nullptr,
  838.                 color::ARGB transparency = 0x80000000);
  839.  
  840.   void blit(s32 x, s32 y, f32 scale = 1.0f,
  841.             color::ARGB transparency = 0x80000000);
  842.  
  843.   void blitCentered(s32 xCenter, s32 yCenter, f32 scale = 1.0f,
  844.                     color::ARGB transparency = 0x80000000);
  845. };
  846.  
  847.  
  848.  
  849.  
  850. }; /* namespace kit */
  851.  
  852. #endif /* _KIT_INC__VIDEO_BITMAP_HPP */
  853. /******************************************************************************/
  854. /******************************************************************************/
  855. //"kit_w32\include\kit\_video_BitmapFont.hpp":
  856. #ifndef _KIT_INC__VIDEO_BITMAPFONT_HPP
  857. #define _KIT_INC__VIDEO_BITMAPFONT_HPP
  858.  
  859. #include "commondef.hpp"
  860. #include "_video_common.hpp"
  861.  
  862.  
  863. namespace kit {
  864.  
  865.  
  866.  
  867.  
  868. class BitmapFont {
  869.   u32                 _type;
  870.   bool               _valid = false;
  871.   bool        _constructing = true;
  872.   u8           _anchorPoint = 0; //tbd
  873.   bool     _verticalNewline = true;
  874.   Bitmap*        _fontAtlas = nullptr;
  875.   s16 _stepLengths[256]; //how many pixels to step forward for a given char (including glyph size!)
  876.   shape::spoint _offsets[256]; //pixel offsets for a given char
  877.   shape::rect     _glyphSrc;
  878.   shape::rect     _glyphDst;
  879.   shape::fpoint _glyphScale;
  880.   color::ARGB _transparency = 0xFF00FF; //magenta by default (0x80000000 for no transparency)
  881.   s16           _newlineLen; //how many pixels of spacing every newline (including glyph size!)
  882.   bool          _fromMemory = false; //determines whether _fontAtlas should be freed
  883.  
  884.  
  885.   //by default, a font will construct monospaced, behaving like stdout text does
  886.   void _constructFromMemory(Bitmap* fontAtlas,
  887.                             const s16* stepLengths = nullptr, s16 newlineLen = -32768,
  888.                             const shape::spoint* offsets = nullptr, bool verticalNewline = true);
  889.  
  890.  
  891. public:
  892.  
  893.   //(in this version of the constructor, the memory for fontAtlas
  894.    //is managed by the user, and is therefore not freed)
  895.   BitmapFont(Bitmap* fontAtlas,
  896.              const s16* stepLengths   = nullptr,        s16  newlineLen = -32768,
  897.              const shape::spoint* offsets = nullptr, bool verticalNewline = true)
  898.   {
  899.     _fromMemory = true;
  900.     _constructFromMemory(fontAtlas, stepLengths, newlineLen,
  901.                          offsets, verticalNewline);
  902.   }
  903.  
  904.   //loads a bitmap from an image file
  905.   BitmapFont(const char* filePath,  Window* blit_dst,  bool directAccess = false,
  906.              const s16*  stepLengths   = nullptr,        s16  newlineLen = -32768,
  907.              const shape::spoint* offsets = nullptr, bool verticalNewline = true);
  908.  
  909.   ~BitmapFont();
  910.  
  911.  
  912.   bool isValid(){ return _valid; }
  913.   bool isConstructing(){ return _constructing; }
  914.  
  915.   shape::point getStringSize(const char* str, size_t maxLen = 0);
  916.  
  917.  
  918.   shape::point setScale(float x = 1.0f, float y = 1.0f); //returns new char size
  919.  
  920.  
  921.   void putChar(s32 x, s32 y, char chr);
  922.   void print(s32 x, s32 y, const char* str, size_t maxLen = 0);
  923.  
  924. };
  925.  
  926.  
  927.  
  928.  
  929. };
  930.  
  931. #endif /* _KIT_INC__VIDEO_BITMAPFONT_HPP */
  932. /******************************************************************************/
  933. /******************************************************************************/
  934. //"kit_w32\include\kit\_video_common.hpp":
  935. #ifndef _KIT_INC__VIDEO_COMMON_HPP
  936. #define _KIT_INC__VIDEO_COMMON_HPP
  937.  
  938. #include "commondef.hpp"
  939.  
  940.  
  941. #define KIT_VIDEO_COMMON_REMOVE_PADDING
  942.  
  943.  
  944. #ifdef KIT_VIDEO_COMMON_REMOVE_PADDING
  945. #pragma pack(push,1)
  946. #endif /* KIT_VIDEO_COMMON_REMOVE_PADDING */
  947.  
  948.  
  949. namespace kit {
  950.  
  951.  
  952.  
  953.  
  954. namespace color {
  955.  
  956.   //what GDI uses (save for the alpha channel)
  957.   union ARGB { //4B; 0xAARRGGBB
  958.     u32 v; //entire color [v]alue
  959.     struct {
  960.       u8 b;
  961.       u8 g;
  962.       u8 r;
  963.       u8 a;
  964.     };
  965.  
  966.     ARGB() : v(0) {}
  967.     ARGB(u32 _v) : v(_v) {}
  968.     ARGB(u8 _r, u8 _g,
  969.          u8 _b, u8 _a) : r(_r), g(_g), b(_b), a(_a) {}
  970.     inline bool operator==(const ARGB& c){ return (v == c.v); };
  971.     inline bool operator!=(const ARGB& c){ return (v != c.v); };
  972.     inline bool operator==(const u32& cv){ return (v == cv); };
  973.     inline bool operator!=(const u32& cv){ return (v != cv); };
  974.     inline void operator=(const u32 cv){ v = cv; }
  975.   };
  976.  
  977.  
  978.   union ABGR { //4B; 0xAABBGGRR
  979.     u32 v; //entire color [v]alue
  980.     struct {
  981.       u8 r;
  982.       u8 g;
  983.       u8 b;
  984.       u8 a;
  985.     };
  986.  
  987.     ABGR() : v(0) {}
  988.     ABGR(u32 _v) : v(_v) {}
  989.     ABGR(u8 _r, u8 _g,
  990.          u8 _b, u8 _a) : r(_r), g(_g), b(_b), a(_a) {}
  991.     inline bool operator==(const ABGR& c){ return (v == c.v); };
  992.     inline bool operator!=(const ABGR& c){ return (v != c.v); };
  993.     inline bool operator==(const u32& cv){ return (v == cv); };
  994.     inline bool operator!=(const u32& cv){ return (v != cv); };
  995.     inline void operator=(const u32 cv){ v = cv; }
  996.   };
  997.  
  998. }; /* namespace color */
  999.  
  1000.  
  1001.  
  1002.  
  1003. namespace shape {
  1004.  
  1005.   struct spoint {
  1006.     s16 x, y;
  1007.     spoint() : x(0), y(0) {}
  1008.     spoint(s16 _x, s16 _y) : x(_x), y(_y) {}
  1009.     inline bool operator==(const spoint& p){ return (x==p.x && y==p.y); };
  1010.     inline bool operator!=(const spoint& p){ return (x!=p.x && y!=p.y); };
  1011.     inline void operator-=(const spoint& p){ x -= p.x;  y -= p.y; }
  1012.     inline void operator+=(const spoint& p){ x += p.x;  y += p.y; }
  1013.   };
  1014.  
  1015.   struct srect {
  1016.     s16 x, y; //x & y position of the rectangle's top-left corner
  1017.     s16 w, h; //the rectangle's width & height
  1018.     srect() : x(0), y(0), w(0), h(0) {}
  1019.     srect(s16 _x, s16 _y) : x(_x), y(_y) {}
  1020.     srect(s16 _x, s16 _y,
  1021.           s16 _w, s16 _h) : x(_x), y(_y), w(_w), h(_h) {}
  1022.     inline bool operator==(const srect& r){ return (x==r.x && y==r.y && w==r.w && h==r.h); };
  1023.     inline bool operator!=(const srect& r){ return (x!=r.x && y!=r.y && w!=r.w && h!=r.h); };
  1024.     inline void operator-=(const spoint& p){ x -= p.x;  y -= p.y; }
  1025.     inline void operator+=(const spoint& p){ x += p.x;  y += p.y; }
  1026.   };
  1027.  
  1028.   struct sline {
  1029.     s16 x0, y0;
  1030.     s16 x1, y1;
  1031.     sline() : x0(0), y0(0), x1(0), y1(0) {}
  1032.     sline(s16 _x0, s16 _y0,
  1033.           s16 _x1, s16 _y1) : x0(_x0), y0(_y0), x1(_x1), y1(_y1) {}
  1034.     inline bool operator==(const sline& l){ return (x0==l.x0 && y0==l.y0 && x1==l.x1 && y1==l.y1); };
  1035.     inline bool operator!=(const sline& l){ return (x0!=l.x0 && y0!=l.y0 && x1!=l.x1 && y1!=l.y1); };
  1036.   };
  1037.  
  1038.   struct scircle {
  1039.     s16 x, y, r;
  1040.     scircle() : x(0), y(0), r(0) {}
  1041.     scircle(s16 _x, s16 _y, s16 _r) : x(_x), y(_y), r(_r) {}
  1042.     inline bool operator==(const scircle& c){ return (x==c.x && y==c.y && r==c.r); };
  1043.     inline bool operator!=(const scircle& c){ return (x!=c.x && y!=c.y && r!=c.r); };
  1044.     inline void operator-=(const spoint& p){ x -= p.x;  y -= p.y; }
  1045.     inline void operator+=(const spoint& p){ x += p.x;  y += p.y; }
  1046.   };
  1047.  
  1048.   struct spoint3d {
  1049.     s16 x, y, z;
  1050.     spoint3d() : x(0), y(0), z(0) {}
  1051.     spoint3d(s16 _x, s16 _y, s16 _z) : x(_x), y(_y), z(_z) {}
  1052.     inline bool operator==(const spoint3d& p){ return (x==p.x && y==p.y && z==p.z); }
  1053.     inline bool operator!=(const spoint3d& p){ return (x!=p.x && y!=p.y && z!=p.z); }
  1054.   };
  1055.  
  1056.  
  1057.  
  1058.   struct point {
  1059.     s32 x, y;
  1060.     point() : x(0), y(0) {}
  1061.     point(s32 _x, s32 _y) : x(_x), y(_y) {}
  1062.     inline bool operator==(const point& p){ return (x==p.x && y==p.y); };
  1063.     inline bool operator!=(const point& p){ return (x!=p.x && y!=p.y); };
  1064.     inline void operator-=(const point& p){ x -= p.x;  y -= p.y; }
  1065.     inline void operator+=(const point& p){ x += p.x;  y += p.y; }
  1066.   };
  1067.  
  1068.   struct rect {
  1069.     s32 x, y; //x & y position of the rectangle's top-left corner
  1070.     s32 w, h; //the rectangle's width & height
  1071.     rect() : x(0), y(0), w(0), h(0) {}
  1072.     rect(s32 _x, s32 _y) : x(_x), y(_y) {}
  1073.     rect(s32 _x, s32 _y,
  1074.          s32 _w, s32 _h) : x(_x), y(_y), w(_w), h(_h) {}
  1075.     inline bool operator==(const rect& r){ return (x==r.x && y==r.y && w==r.w && h==r.h); };
  1076.     inline bool operator!=(const rect& r){ return (x!=r.x && y!=r.y && w!=r.w && h!=r.h); };
  1077.     inline void operator-=(const point& p){ x -= p.x;  y -= p.y; }
  1078.     inline void operator+=(const point& p){ x += p.x;  y += p.y; }
  1079.   };
  1080.  
  1081.   struct line {
  1082.     s32 x0, y0;
  1083.     s32 x1, y1;
  1084.     line() : x0(0), y0(0), x1(0), y1(0) {}
  1085.     line(s32 _x0, s32 _y0,
  1086.          s32 _x1, s32 _y1) : x0(_x0), y0(_y0), x1(_x1), y1(_y1) {}
  1087.     inline bool operator==(const line& l){ return (x0==l.x0 && y0==l.y0 && x1==l.x1 && y1==l.y1); };
  1088.     inline bool operator!=(const line& l){ return (x0!=l.x0 && y0!=l.y0 && x1!=l.x1 && y1!=l.y1); };
  1089.   };
  1090.  
  1091.   struct circle {
  1092.     s32 x, y, r;
  1093.     circle() : x(0), y(0), r(0) {}
  1094.     circle(s32 _x, s32 _y, s32 _r) : x(_x), y(_y), r(_r) {}
  1095.     inline bool operator==(const circle& c){ return (x==c.x && y==c.y && r==c.r); };
  1096.     inline bool operator!=(const circle& c){ return (x!=c.x && y!=c.y && r!=c.r); };
  1097.     inline void operator-=(const point& p){ x -= p.x;  y -= p.y; }
  1098.     inline void operator+=(const point& p){ x += p.x;  y += p.y; }
  1099.   };
  1100.  
  1101.   struct point3d {
  1102.     s32 x, y, z;
  1103.     point3d() : x(0), y(0), z(0) {}
  1104.     point3d(s32 _x, s32 _y, s32 _z) : x(_x), y(_y), z(_z) {}
  1105.     inline bool operator==(const point3d& p){ return (x==p.x && y==p.y && z==p.z); }
  1106.     inline bool operator!=(const point3d& p){ return (x!=p.x && y!=p.y && z!=p.z); }
  1107.   };
  1108.  
  1109.  
  1110.  
  1111.   struct fpoint {
  1112.     f32 x, y;
  1113.     fpoint() : x(0.0f), y(0.0f) {}
  1114.     fpoint(f32 _x, f32 _y) : x(_x), y(_y) {}
  1115.     inline bool operator==(const fpoint& p){ return (x==p.x && y==p.y); };
  1116.     inline bool operator!=(const fpoint& p){ return (x!=p.x && y!=p.y); };
  1117.     inline void operator-=(const fpoint& p){ x -= p.x;  y -= p.y; }
  1118.     inline void operator+=(const fpoint& p){ x += p.x;  y += p.y; }
  1119.   };
  1120.  
  1121.   struct frect {
  1122.     f32 x, y; //x & y position of rectangle's top-left corner
  1123.     f32 w, h; //the rectangle's width & height
  1124.     frect() : x(0.0f), y(0.0f), w(0.0f), h(0.0f) {}
  1125.     frect(f32 _x, f32 _y) : x(_x), y(_y) {}
  1126.     frect(f32 _x, f32 _y,
  1127.           f32 _w, f32 _h) : x(_x), y(_y), w(_w), h(_h) {}
  1128.     inline bool operator==(const frect& r){ return (x==r.x && y==r.y && w==r.w && h==r.h); };
  1129.     inline bool operator!=(const frect& r){ return (x!=r.x && y!=r.y && w!=r.w && h!=r.h); };
  1130.     inline void operator-=(const fpoint& p){ x -= p.x;  y -= p.y; }
  1131.     inline void operator+=(const fpoint& p){ x += p.x;  y += p.y; }
  1132.   };
  1133.  
  1134.   struct fline {
  1135.     f32 x0, y0;
  1136.     f32 x1, y1;
  1137.     fline() : x0(0.0f), y0(0.0f), x1(0.0f), y1(0.0f) {}
  1138.     fline(f32 _x0, f32 _y0,
  1139.           f32 _x1, f32 _y1) : x0(_x0), y0(_y0), x1(_x1), y1(_y1) {}
  1140.     inline bool operator==(const fline& l){ return (x0==l.x0 && y0==l.y0 && x1==l.x1 && y1==l.y1); };
  1141.     inline bool operator!=(const fline& l){ return (x0!=l.x0 && y0!=l.y0 && x1!=l.x1 && y1!=l.y1); };
  1142.   };
  1143.  
  1144.   struct fcircle {
  1145.     f32 x, y, r;
  1146.     fcircle() : x(0.0f), y(0.0f), r(0.0f) {}
  1147.     fcircle(f32 _x, f32 _y, f32 _r) : x(_x), y(_y), r(_r) {}
  1148.     inline bool operator==(const fcircle& c){ return (x==c.x && y==c.y && r==c.r); };
  1149.     inline bool operator!=(const fcircle& c){ return (x!=c.x && y!=c.y && r!=c.r); };
  1150.     inline void operator-=(const fpoint& p){ x -= p.x;  y -= p.y; }
  1151.     inline void operator+=(const fpoint& p){ x += p.x;  y += p.y; }
  1152.   };
  1153.  
  1154.   struct fpoint3d {
  1155.     f32 x, y, z;
  1156.     fpoint3d() : x(0.0f), y(0.0f), z(0.0f) {}
  1157.     fpoint3d(f32 _x, f32 _y, f32 _z) : x(_x), y(_y), z(_z) {}
  1158.     inline bool operator==(const fpoint3d& p){ return (x==p.x && y==p.y && z==p.z); }
  1159.     inline bool operator!=(const fpoint3d& p){ return (x!=p.x && y!=p.y && z!=p.z); }
  1160.   };
  1161.  
  1162.  
  1163.  
  1164.   struct dpoint {
  1165.     f64 x, y;
  1166.     dpoint() : x(0.0), y(0.0) {}
  1167.     dpoint(f64 _x, f64 _y) : x(_x), y(_y) {}
  1168.     inline bool operator==(const dpoint& p){ return (x==p.x && y==p.y); };
  1169.     inline bool operator!=(const dpoint& p){ return (x!=p.x && y!=p.y); };
  1170.     inline void operator-=(const dpoint& p){ x -= p.x;  y -= p.y; }
  1171.     inline void operator+=(const dpoint& p){ x += p.x;  y += p.y; }
  1172.   };
  1173.  
  1174.   struct drect {
  1175.     f64 x, y; //x & y position of rectangle's top-left corner
  1176.     f64 w, h; //the rectangle's width & height
  1177.     drect() : x(0.0), y(0.0), w(0.0), h(0.0) {}
  1178.     drect(f64 _x, f64 _y) : x(_x), y(_y) {}
  1179.     drect(f64 _x, f64 _y,
  1180.           f64 _w, f64 _h) : x(_x), y(_y), w(_w), h(_h) {}
  1181.     inline bool operator==(const drect& r){ return (x==r.x && y==r.y && w==r.w && h==r.h); };
  1182.     inline bool operator!=(const drect& r){ return (x!=r.x && y!=r.y && w!=r.w && h!=r.h); };
  1183.     inline void operator-=(const dpoint& p){ x -= p.x;  y -= p.y; }
  1184.     inline void operator+=(const dpoint& p){ x += p.x;  y += p.y; }
  1185.   };
  1186.  
  1187.   struct dline {
  1188.     f64 x0, y0;
  1189.     f64 x1, y1;
  1190.     dline() : x0(0.0), y0(0.0), x1(0.0), y1(0.0) {}
  1191.     dline(f64 _x0, f64 _y0,
  1192.           f64 _x1, f64 _y1) : x0(_x0), y0(_y0), x1(_x1), y1(_y1) {}
  1193.     inline bool operator==(const dline& l){ return (x0==l.x0 && y0==l.y0 && x1==l.x1 && y1==l.y1); };
  1194.     inline bool operator!=(const dline& l){ return (x0!=l.x0 && y0!=l.y0 && x1!=l.x1 && y1!=l.y1); };
  1195.   };
  1196.  
  1197.   struct dcircle {
  1198.     f64 x, y;
  1199.     f64 r;
  1200.     dcircle() : x(0.0), y(0.0), r(0.0) {}
  1201.     dcircle(f64 _x, f64 _y, f64 _r) : x(_x), y(_y), r(_r) {}
  1202.     inline bool operator==(const dcircle& c){ return (x==c.x && y==c.y && r==c.r); };
  1203.     inline bool operator!=(const dcircle& c){ return (x!=c.x && y!=c.y && r!=c.r); };
  1204.     inline void operator-=(const dpoint& p){ x -= p.x;  y -= p.y; }
  1205.     inline void operator+=(const dpoint& p){ x += p.x;  y += p.y; }
  1206.   };
  1207.  
  1208.   struct dpoint3d {
  1209.     f64 x, y, z;
  1210.     dpoint3d() : x(0.0), y(0.0), z(0.0) {}
  1211.     dpoint3d(f64 _x, f64 _y, f64 _z) : x(_x), y(_y), z(_z) {}
  1212.     inline bool operator==(const dpoint3d& p){ return (x==p.x && y==p.y && z==p.z); }
  1213.     inline bool operator!=(const dpoint3d& p){ return (x!=p.x && y!=p.y && z!=p.z); }
  1214.   };
  1215.  
  1216. }; /* namespace shape */
  1217.  
  1218.  
  1219.  
  1220.  
  1221. }; /* namespace kit */
  1222.  
  1223. #ifdef KIT_VIDEO_COMMON_REMOVE_PADDING
  1224. #pragma pack(pop)
  1225. #endif /* KIT_VIDEO_COMMON_REMOVE_PADDING */
  1226.  
  1227. #endif /* _KIT_INC__VIDEO_COMMON_HPP */
  1228.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement