Advertisement
Kitomas

ksdl2 work for 2024-08-02 (3/5)

Aug 2nd, 2024
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 22.42 KB | None | 0 0
  1. /******************************************************************************/
  2. /******************************************************************************/
  3. //"ksdl2\src\kit_sdl2\kit_memory.cpp":
  4. #include "_kit_common.hpp"
  5.  
  6.  
  7. //turns something into a void**
  8.  //(this makes some code here easier for me to read)
  9. #define VPP(_ptr_p) ((void**)(_ptr_p))
  10.  
  11.  
  12. namespace kit {
  13.  
  14.  
  15. size_t numAllocations = 0;
  16.  
  17.  
  18.  
  19.  
  20.  
  21. void* memory::alloc(size_t size){
  22.   void* newHeapMemory = SDL_malloc(size);
  23.   if(newHeapMemory != nullptr) ++numAllocations;
  24.   return newHeapMemory;
  25.  
  26. }
  27.  
  28.  
  29.  
  30.  
  31. void memory::free(void* ptr_p){
  32.   if(VPP(ptr_p) != nullptr  &&  *VPP(ptr_p) != nullptr){
  33.     --numAllocations;
  34.     SDL_free(*VPP(ptr_p));
  35.     *VPP(ptr_p) = nullptr;
  36.  
  37.   }
  38.  
  39. }
  40.  
  41.  
  42.  
  43.  
  44. void* memory::realloc(void* ptr_p, size_t newSize){
  45.   void* ptr_new = nullptr;
  46.  
  47.   if(VPP(ptr_p) != nullptr){
  48.     ptr_new = SDL_realloc(*VPP(ptr_p), newSize);
  49.  
  50.     if(ptr_new != nullptr){
  51.       if(*VPP(ptr_p) == nullptr) ++numAllocations;
  52.       *VPP(ptr_p) = ptr_new;
  53.  
  54.     }
  55.  
  56.   }
  57.  
  58.   return ptr_new;
  59.  
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
  66. void* memory::alloc2(size_t size){
  67.   void* newHeapMemory = SDL_malloc(size);
  68.   if(newHeapMemory == nullptr)
  69.     throw PUSH_ERROR("memory::alloc2(): failed to allocate memory");
  70.   ++numAllocations;
  71.   return newHeapMemory;
  72.  
  73. }
  74.  
  75.  
  76.  
  77.  
  78. void memory::free2(void* ptr_p){
  79.   if( VPP(ptr_p) == nullptr) throw PUSH_ERROR("memory::free2(): ptr_p == nullptr");
  80.   if(*VPP(ptr_p) == nullptr) throw PUSH_ERROR("memory::free2(): *ptr_p == nullptr");
  81.   --numAllocations;
  82.   SDL_free(*VPP(ptr_p));
  83.   *VPP(ptr_p) = nullptr;
  84.  
  85. }
  86.  
  87.  
  88.  
  89.  
  90. void* memory::realloc2(void* ptr_p, size_t newSize){
  91.   void* ptr_new = nullptr;
  92.  
  93.   if(VPP(ptr_p) != nullptr){
  94.     ptr_new = SDL_realloc(*VPP(ptr_p), newSize);
  95.  
  96.     if(ptr_new != nullptr){
  97.       if(*VPP(ptr_p) == nullptr) ++numAllocations; //if memory is new, not realloc'd
  98.       *VPP(ptr_p) = ptr_new;
  99.  
  100.     }
  101.  
  102.   } else {
  103.     throw PUSH_ERROR("memory::realloc2(): ptr_p = nullptr");
  104.  
  105.   }
  106.  
  107.  
  108.   if(ptr_new == nullptr)
  109.     throw PUSH_ERROR("memory::realloc2(): failed to reallocate memory");
  110.  
  111.  
  112.   return ptr_new;
  113.  
  114. }
  115.  
  116.  
  117.  
  118.  
  119.  
  120. size_t memory::getNumAllocations(){
  121.   return numAllocations;
  122.  
  123. }
  124.  
  125.  
  126.  
  127.  
  128.  
  129. //currently just a wrapper, but now i can make my own implementation
  130.  //whenever i want, without replacing every call to memset with it
  131. void* memory::set(void* ptr, s32 value, size_t size){
  132.   if(ptr == nullptr) return nullptr; //now it's safe to pass nullptr :D
  133.   return memset(ptr, value, size);
  134.  
  135. }
  136.  
  137.  
  138.  
  139.  
  140. void* memory::set2(void* ptr, s32 value, size_t size){
  141.   if(ptr == nullptr) throw PUSH_ERROR("memory::set2(): ptr = nullptr");
  142.   return memset(ptr, value, size);
  143.  
  144. }
  145.  
  146.  
  147.  
  148.  
  149.  
  150. void* memory::copy(void* destination, const void* source, size_t size){
  151.   //tbd: maybe make it so that memcpy is only called if dst&src aren't nullptr
  152.   return memcpy(destination, source, size);
  153.  
  154. }
  155.  
  156.  
  157.  
  158.  
  159. void* memory::copy2(void* destination, const void* source, size_t size){
  160.   if(destination == nullptr) throw PUSH_ERROR("memory::copy2(): destination = nullptr");
  161.   if(source      == nullptr) throw PUSH_ERROR("memory::copy2(): source = nullptr");
  162.   return memcpy(destination, source, size);
  163.  
  164. }
  165.  
  166.  
  167.  
  168.  
  169.  
  170. }; /* namespace kit */
  171. /******************************************************************************/
  172. /******************************************************************************/
  173. //"ksdl2\src\kit_sdl2\kit_Mutex.cpp":
  174. #include "_kit_common.hpp"
  175.  
  176. #define KIT_MUTEX_NULLPTR "internal mutex = nullptr"
  177. #define KIT_MUTEX_TIMEDOUT "mutex timed out"
  178.  
  179. #define MUTEX_PTR ((SDL_mutex*)_mutex_p)
  180.  
  181.  
  182. using namespace kit;
  183.  
  184.  
  185.  
  186.  
  187.  
  188. Mutex::Mutex(){
  189.   _type = KIT_CLASSTYPE_MUTEX;
  190.  
  191.  
  192.   _mutex_p = SDL_CreateMutex(); //can't use MUTEX_PTR here
  193.   if(MUTEX_PTR == nullptr)
  194.     throw PUSH_ERRORF("Mutex::Mutex(): \"%s\"", SDL_GetError());
  195.  
  196.   _valid = true;
  197.   _constructing = false;
  198.  
  199. }
  200.  
  201.  
  202.  
  203.  
  204.  
  205. Mutex::~Mutex(){
  206.   if(!_valid) return;
  207.   _valid = false;
  208.  
  209.   //(destroying a locked mutex will result in undefined behavior!)
  210.   if(MUTEX_PTR != nullptr) SDL_DestroyMutex(MUTEX_PTR);
  211.   _mutex_p = nullptr; //can't use MUTEX_PTR here
  212.  
  213. }
  214.  
  215.  
  216.  
  217.  
  218.  
  219. void Mutex::lock(bool locked){
  220.   if(MUTEX_PTR == nullptr)
  221.     throw PUSH_ERROR("Mutex::lock(): " KIT_MUTEX_NULLPTR);
  222.  
  223.  
  224.   int result;
  225.   if(locked) result = SDL_LockMutex  (MUTEX_PTR);
  226.   else       result = SDL_UnlockMutex(MUTEX_PTR);
  227.  
  228.  
  229.   if(result < 0){
  230.     throw PUSH_ERRORF("Mutex::lock(): \"%s\"", SDL_GetError());
  231.  
  232.   } else if(result == SDL_MUTEX_TIMEDOUT){
  233.     throw PUSH_ERROR("Mutex::lock(): " KIT_MUTEX_TIMEDOUT);
  234.  
  235.   }
  236.  
  237.  
  238. }
  239.  
  240.  
  241.  
  242.  
  243.  
  244. bool Mutex::tryLock(){
  245.   if(MUTEX_PTR == nullptr)
  246.     throw PUSH_ERROR("Mutex::tryLock(): " KIT_MUTEX_NULLPTR);
  247.  
  248.   int result = SDL_TryLockMutex(MUTEX_PTR);
  249.  
  250.   if(result < 0) throw PUSH_ERRORF("Mutex::tryLock(): \"%s\"", SDL_GetError());
  251.  
  252.   return result == 0;
  253.  
  254. }
  255. /******************************************************************************/
  256. /******************************************************************************/
  257. //"ksdl2\src\kit_sdl2\kit_Thread.cpp":
  258. #include "_kit_common.hpp"
  259.  
  260. #define KIT_THREAD_NAME _kit_thread_name //(default thread name)
  261. #define KIT_INVALID_THREAD "invalid Thread" //this should remain a literal
  262.  
  263. #define KIT_IS_INVALID_THREAD (!_valid && !_constructing)
  264. #define THREAD_PTR ((SDL_Thread*)_thread) //only applicable for Thread objects
  265.  
  266.  
  267. namespace kit {
  268.  
  269. static const char _kit_thread_name[] = "kit_sdl2 thread";
  270.  
  271.  
  272.  
  273.  
  274.  
  275. //returns whether or not priority of current thread was successfully set
  276.  //may fail if setting priority requires elevated privileges (or at least when
  277.  //setting a higher priority), if it's even allowed at all
  278. bool Thread_setPriority(s32 priority, bool throwOnFailure){
  279.   SDL_ThreadPriority priority_real;
  280.  
  281.   switch(priority){
  282.     case THREAD_LOW    : priority_real = SDL_THREAD_PRIORITY_LOW;           break;
  283.     case THREAD_NORMAL : priority_real = SDL_THREAD_PRIORITY_NORMAL;        break;
  284.     case THREAD_HIGH   : priority_real = SDL_THREAD_PRIORITY_HIGH;          break;
  285.     case THREAD_HIGHEST: priority_real = SDL_THREAD_PRIORITY_TIME_CRITICAL; break;
  286.     default: throw PUSH_ERROR("Thread_setPriority(): invalid thread priority");
  287.   }
  288.  
  289.   bool success = !(SDL_SetThreadPriority(priority_real)<0);
  290.   if(!success && throwOnFailure)
  291.     throw PUSH_ERROR("Thread_setPriority(): failed to set thread priority");
  292.   return success;
  293.  
  294. }
  295.  
  296.  
  297.  
  298.  
  299.  
  300. struct _ThreadOpq {
  301.   ThreadFunction func;
  302.   void*      userdata;
  303.   bool  outsideCreate; //set after exiting SDL_CreateThreadWithStackSize()
  304.  
  305.   u8        _padding8;
  306.   u16      _padding16;
  307.   u32      _padding32;
  308. };
  309.  
  310.  
  311. static s32 ThreadFunctionWrapper(void* data){
  312.   _ThreadOpq* _opq = (_ThreadOpq*)data;
  313.   if(_opq == nullptr) return 0; //should be impossible
  314.   while(!_opq->outsideCreate) SDL_Delay(1); //one of the solutions of all time
  315.  
  316.   _ThreadOpq opq = *_opq; //make stack copy
  317.   memory::free(&_opq);    //free original
  318.  
  319.  
  320.   s32 result = 0;
  321.  
  322.   try {
  323.     if(opq.func) result = opq.func(opq.userdata);
  324.  
  325.  
  326.   } catch(const char* errortext){
  327.     SDL_threadID thread_id   = SDL_GetThreadID(nullptr);
  328.     const char*  thread_name = SDL_GetThreadName(nullptr);
  329.     if(thread_name == nullptr) thread_name = "NAMELESS";
  330.  
  331.     SDL_Log("ERROR ON THREAD %lu (%s): %s",
  332.             thread_id, thread_name, errortext);
  333.  
  334.     //i think this outputs to stderr? idk
  335.     SDL_LogError(SDL_LOG_CATEGORY_ERROR,
  336.                  "ERROR ON THREAD %lu (%s): %s",
  337.                  thread_id, thread_name, errortext);
  338.  
  339.     freeThreadError();
  340.  
  341.  
  342.   }
  343.  
  344.  
  345.   return result;
  346.  
  347. }
  348.  
  349.  
  350.  
  351.  
  352.  
  353. Thread::Thread(ThreadFunction func, void* userdata,
  354.                bool waitInDestructor, size_t stackSize,
  355.                const char* threadName)
  356. {
  357.   _type = KIT_CLASSTYPE_THREAD;
  358.  
  359.   //handle parameters
  360.   if(func == nullptr)
  361.     throw PUSH_ERROR("Thread::Thread(): nullptr was given as ThreadFunction");
  362.   _waitInDestructor = waitInDestructor;
  363.   if(threadName == nullptr) threadName = KIT_THREAD_NAME; //set to default if nullptr
  364.  
  365.  
  366.   _ThreadOpq* opq = (_ThreadOpq*)memory::alloc(sizeof(_ThreadOpq));
  367.   if(opq == nullptr)
  368.     throw PUSH_ERROR("Thread::Thread(): failed to allocate memory for Thread's opaque struct");
  369.   memset(opq, 0, sizeof(_ThreadOpq));
  370.  
  371.   opq->func     = func;
  372.   opq->userdata = userdata;
  373.  
  374.  
  375.   //(can't use THREAD_PTR on lvalue)
  376.   _thread = (GenOpqPtr)SDL_CreateThreadWithStackSize(ThreadFunctionWrapper,
  377.                                                      threadName, stackSize, opq);
  378.   opq->outsideCreate = true;
  379.   if(THREAD_PTR == nullptr){
  380.     memory::free(&opq);
  381.     throw PUSH_ERRORF("Thread::Thread(): \"%s\"", SDL_GetError());
  382.  
  383.   }
  384.  
  385.  
  386.   _valid = true;
  387.   _constructing = false;
  388.  
  389. }
  390.  
  391.  
  392.  
  393.  
  394.  
  395. Thread::~Thread(){
  396.   if(!_valid) return;
  397.   _valid = false;
  398.  
  399.  
  400.   if(THREAD_PTR != nullptr){
  401.     if(_waitInDestructor && !_detached)
  402.       SDL_WaitThread(THREAD_PTR, nullptr); //cleans up memory too
  403.  
  404.     else if(!_detached)
  405.       SDL_DetachThread(THREAD_PTR);
  406.  
  407.   }
  408.  
  409. }
  410.  
  411.  
  412.  
  413.  
  414.  
  415. //returns ID of current thread instead if thread is nullptr
  416. u32 Thread::getID(){
  417.   if(KIT_IS_INVALID_THREAD)
  418.     throw PUSH_ERROR("Thread::getID(): " KIT_INVALID_THREAD);
  419.  
  420.   return SDL_GetThreadID((SDL_Thread*)_thread);
  421.  
  422. }
  423.  
  424.  
  425.  
  426. //returns UTF-8 string, or nullptr if thread doesn't have a name
  427. const char* Thread::getName(){
  428.   if(KIT_IS_INVALID_THREAD)
  429.     throw PUSH_ERROR("Thread::getName(): " KIT_INVALID_THREAD);
  430.  
  431.   return SDL_GetThreadName(THREAD_PTR);
  432.  
  433. }
  434.  
  435.  
  436.  
  437.  
  438.  
  439. s32 Thread::waitUntilDone(){
  440.   if(KIT_IS_INVALID_THREAD)
  441.     throw PUSH_ERROR("Thread::waitUntilDone(): " KIT_INVALID_THREAD);
  442.  
  443.   s32 returnStatus = 0;
  444.   if(!_detached) SDL_WaitThread((SDL_Thread*)_thread, &returnStatus);
  445.  
  446.   return returnStatus;
  447.  
  448. }
  449.  
  450.  
  451.  
  452.  
  453.  
  454. void Thread::detach(){
  455.   if(KIT_IS_INVALID_THREAD)
  456.     throw PUSH_ERROR("Thread::detach(): " KIT_INVALID_THREAD);
  457.  
  458.   if(!_detached){
  459.     SDL_DetachThread(THREAD_PTR);
  460.     _detached = true;
  461.  
  462.   }
  463.  
  464. }
  465.  
  466.  
  467.  
  468.  
  469.  
  470. }; /* namespace kit */
  471. /******************************************************************************/
  472. /******************************************************************************/
  473. //"ksdl2\src\kit_sdl2\kit_time.cpp":
  474. #include "_kit_common.hpp"
  475.  
  476.  
  477. namespace kit {
  478.  
  479.  
  480.  
  481.  
  482.  
  483. timerID timerAdd(timerCallback func, u32 interval_ms, void* userdata){
  484.   if(!_gl.init.timer) throw PUSH_ERROR("timerAdd(): timer subsystem is uninitialized");
  485.  
  486.   timerID id_new = SDL_AddTimer(interval_ms, func, userdata);
  487.  
  488.   if(!id_new) throw PUSH_ERRORF("timerAdd(): \"%s\"", SDL_GetError());
  489.   else        return id_new;
  490.  
  491. }
  492.  
  493.  
  494.  
  495.  
  496. void timerRemove(timerID id){
  497.   if(!_gl.init.timer) throw PUSH_ERROR("timerRemove(): timer subsystem is uninitialized");
  498.  
  499.   bool success = SDL_RemoveTimer(id);
  500.  
  501.   if(!success) throw PUSH_ERRORF("timerRemove(): \"%s\"", SDL_GetError());
  502.  
  503. }
  504.  
  505.  
  506.  
  507.  
  508.  
  509. u64 time::getTicks(){
  510.   return SDL_GetPerformanceCounter();
  511.  
  512. }
  513.  
  514.  
  515. u64 time::getTicksPerSecond(){
  516.   if(!_gl.performanceFreq)
  517.     _gl.performanceFreq = SDL_GetPerformanceFrequency();
  518.  
  519.   return _gl.performanceFreq;
  520.  
  521. }
  522.  
  523.  
  524.  
  525.  
  526. u64 time::getMS(){
  527.   return SDL_GetTicks64();
  528.  
  529. }
  530.  
  531.  
  532. u32 time::getMS_32(){
  533.   return SDL_GetTicks();
  534.  
  535. }
  536.  
  537.  
  538.  
  539.  
  540. f64 time::getSeconds(){
  541.   return (f64)SDL_GetTicks64()/1000;
  542.  
  543. }
  544.  
  545.  
  546.  
  547.  
  548.  
  549. void time::sleep(u32 milliseconds){
  550.   SDL_Delay(milliseconds);
  551.  
  552. }
  553.  
  554.  
  555.  
  556.  
  557.  
  558. }; /* namespace kit */
  559. /******************************************************************************/
  560. /******************************************************************************/
  561. //"ksdl2\src\kit_sdl2\kit_Window.cpp":
  562. #include "_kit_common.hpp"
  563.  
  564. #define WIN_PTR ((SDL_Window*)_win)
  565. #define KIT_WIN_IS_INVALID (!_valid && !_constructing)
  566. #define KIT_WIN_CHECK_INVALID(_funcname) \
  567.   if(KIT_WIN_IS_INVALID) throw PUSH_ERROR(_funcname ": window is invalid");
  568.  
  569. #define WIN_DATA_NAME "Window class"
  570.  
  571. namespace kit {
  572.  
  573. /* functions to potentially maybe incorporate:
  574. SDL_GetWindowBordersSize
  575. SDL_GetWindowSurface
  576. SDL_GetWindowGammaRamp
  577.  
  578. SDL_SetWindowGammaRamp
  579. SDL_SetWindowHitTest
  580. SDL_SetWindowShape
  581. SDL_UpdateWindowSurface
  582. SDL_UpdateWindowSurfaceRects
  583. SDL_IsShapedWindow
  584. SDL_SetWindowModalFor
  585. SDL_SetWindowIcon
  586. */
  587.  
  588.  
  589.  
  590.  
  591.  
  592. #define VALID_WINFLAGS       ( \
  593.   WINFLAG_FULLSCREEN         | \
  594.   WINFLAG_OPENGL             | \
  595.   WINFLAG_HIDDEN             | \
  596.   WINFLAG_BORDERLESS         | \
  597.   WINFLAG_RESIZABLE          | \
  598.   WINFLAG_MINIMIZED          | \
  599.   WINFLAG_MAXIMIZED          | \
  600.   WINFLAG_INPUT_GRABBED      | \
  601.   WINFLAG_FULLSCREEN_DESKTOP | \
  602.   WINFLAG_VULKAN               \
  603. )
  604.  
  605. Window::Window(const char* winTitle,
  606.                s32 winWidth, s32 winHeight,
  607.                u32 winFlags,
  608.                s32 winX, s32 winY)
  609. {
  610.   _type = KIT_CLASSTYPE_WINDOW;
  611.  
  612.   if(winWidth  < 0) throw PUSH_ERROR("Window::Window(): winWidth < 0");
  613.   if(winHeight < 0) throw PUSH_ERROR("Window::Window(): winHeight < 0");
  614.  
  615.  
  616.   _win = SDL_CreateWindow(winTitle, winX, winY,
  617.                           winWidth, winHeight,
  618.                           winFlags & VALID_WINFLAGS);
  619.  
  620.   if(_win == nullptr)
  621.     throw PUSH_ERRORF("Window::Window(): \"%s\"", SDL_GetError());
  622.  
  623.  
  624.   SDL_SetWindowData(WIN_PTR, WIN_DATA_NAME, this);
  625.  
  626.  
  627.   _valid = true;
  628.   _constructing = false;
  629.  
  630. }
  631.  
  632.  
  633.  
  634.  
  635. Window::~Window(){
  636.   if(!_valid) return;
  637.   _valid = false;
  638.  
  639.   if(_win != nullptr){
  640.     SDL_DestroyWindow(WIN_PTR);
  641.     _win = nullptr;
  642.  
  643.   }
  644.  
  645. }
  646.  
  647.  
  648.  
  649.  
  650.  
  651. bool Window::hasSurface(){
  652.   KIT_WIN_CHECK_INVALID("Window::hasSurface()");
  653.  
  654.   return SDL_HasWindowSurface(WIN_PTR);
  655.  
  656. }
  657.  
  658.  
  659.  
  660.  
  661. u32 Window::getPixelFormat(){
  662.   KIT_WIN_CHECK_INVALID("Window::getPixelFormat()");
  663.  
  664.   u32 pixelFormat = SDL_GetWindowPixelFormat(WIN_PTR);
  665.   if(pixelFormat == SDL_PIXELFORMAT_UNKNOWN)
  666.     throw PUSH_ERRORF("Window::getPixelFormat(): \"%s\"", SDL_GetError());
  667.  
  668.   return pixelFormat;
  669.  
  670. }
  671.  
  672.  
  673.  
  674.  
  675. u32 Window::getID(){
  676.   KIT_WIN_CHECK_INVALID("Window::getID()");
  677.  
  678.   u32 id = SDL_GetWindowID(WIN_PTR);
  679.   if(!id) throw PUSH_ERRORF("Window::getID(): \"%s\"", SDL_GetError());
  680.  
  681.   return id;
  682.  
  683. }
  684.  
  685.  
  686.  
  687.  
  688. u32 Window::getDisplayIndex(){
  689.   KIT_WIN_CHECK_INVALID("Window::getDisplayIndex()");
  690.  
  691.   s32 index = SDL_GetWindowDisplayIndex(WIN_PTR);
  692.   if(index < 0)
  693.     throw PUSH_ERRORF("Window::getDisplayIndex(): \"%s\"", SDL_GetError());
  694.  
  695.   return (u32)index;
  696.  
  697. }
  698.  
  699.  
  700.  
  701.  
  702. void* Window::getUserdata(const char* name){
  703.   KIT_WIN_CHECK_INVALID("Window::getUserdata()");
  704.  
  705.   if(name == nullptr)
  706.     throw PUSH_ERROR("Window::getUserdata(): name = nullptr");
  707.  
  708.   return SDL_GetWindowData(WIN_PTR, name);
  709.  
  710. }
  711.  
  712.  
  713.  
  714.  
  715. DisplayMode Window::getDisplayMode(){
  716.   KIT_WIN_CHECK_INVALID("Window::getDisplayMode()");
  717.  
  718.   SDL_DisplayMode mode_sdl;
  719.   if(SDL_GetWindowDisplayMode(WIN_PTR, &mode_sdl) < 0)
  720.     throw PUSH_ERRORF("Window::getDisplayMode(): \"%s\"", SDL_GetError());
  721.  
  722.   DisplayMode mode_kit;
  723.   mode_kit.pixelFmt    = mode_sdl.format;
  724.   mode_kit.w           = mode_sdl.w;
  725.   mode_kit.h           = mode_sdl.h;
  726.   mode_kit.refreshRate = mode_sdl.refresh_rate;
  727.  
  728.   return mode_kit;
  729.  
  730. }
  731.  
  732.  
  733.  
  734.  
  735. const char* Window::getTitle(){
  736.   KIT_WIN_CHECK_INVALID("Window::getTitle()");
  737.  
  738.   return SDL_GetWindowTitle(WIN_PTR);
  739.  
  740. }
  741.  
  742.  
  743.  
  744.  
  745. shape::point Window::getSize(){
  746.   KIT_WIN_CHECK_INVALID("Window::getSize()");
  747.  
  748.   shape::point size;
  749.   SDL_GetWindowSize(WIN_PTR, &size.x, &size.y);
  750.  
  751.   return size;
  752.  
  753. }
  754.  
  755.  
  756.  
  757.  
  758. u32 Window::getFlags(){
  759.   KIT_WIN_CHECK_INVALID("Window::getFlags()");
  760.  
  761.   return SDL_GetWindowFlags(WIN_PTR);
  762.  
  763. }
  764.  
  765.  
  766.  
  767.  
  768. f32 Window::getOpacity(){
  769.   KIT_WIN_CHECK_INVALID("Window::getOpacity()");
  770.  
  771.   f32 opacity;
  772.   if(SDL_GetWindowOpacity(WIN_PTR, &opacity) < 0)
  773.     throw PUSH_ERRORF("Window::getOpacity(): \"%s\"", SDL_GetError());
  774.  
  775.   return opacity;
  776.  
  777. }
  778.  
  779.  
  780.  
  781.  
  782. shape::point Window::getMinSize(){
  783.   KIT_WIN_CHECK_INVALID("Window::getMinSize()");
  784.  
  785.   shape::point minSize;
  786.   SDL_GetWindowMinimumSize(WIN_PTR, &minSize.x, &minSize.y);
  787.  
  788.   return minSize;
  789.  
  790. }
  791.  
  792.  
  793.  
  794.  
  795. shape::point Window::getMaxSize(){
  796.   KIT_WIN_CHECK_INVALID("Window::getMaxSize()");
  797.  
  798.   shape::point maxSize;
  799.   SDL_GetWindowMaximumSize(WIN_PTR, &maxSize.x, &maxSize.y);
  800.  
  801.   return maxSize;
  802.  
  803. }
  804.  
  805.  
  806.  
  807.  
  808. shape::point Window::getPosition(){
  809.   KIT_WIN_CHECK_INVALID("Window::getPosition()");
  810.  
  811.   shape::point pos;
  812.   SDL_GetWindowPosition(WIN_PTR, &pos.x, &pos.y);
  813.  
  814.   return pos;
  815.  
  816. }
  817.  
  818.  
  819.  
  820.  
  821. bool Window::getGrab(){
  822.   KIT_WIN_CHECK_INVALID("Window::getGrab()");
  823.  
  824.   return SDL_GetWindowGrab(WIN_PTR);
  825.  
  826. }
  827.  
  828.  
  829.  
  830.  
  831. bool Window::getKeyboardGrab(){
  832.   KIT_WIN_CHECK_INVALID("Window::getKeyboardGrab()");
  833.  
  834.   return SDL_GetWindowKeyboardGrab(WIN_PTR);
  835.  
  836. }
  837.  
  838.  
  839.  
  840.  
  841. bool Window::getMouseGrab(){
  842.   KIT_WIN_CHECK_INVALID("Window::getMouseGrab()");
  843.  
  844.   return SDL_GetWindowMouseGrab(WIN_PTR);
  845.  
  846. }
  847.  
  848.  
  849.  
  850.  
  851. shape::rect Window::getMouseGrabRect(){
  852.   KIT_WIN_CHECK_INVALID("Window::getMouseGrabRect()");
  853.  
  854.   const SDL_Rect* _rect = SDL_GetWindowMouseRect(WIN_PTR);
  855.  
  856.   shape::rect rect;
  857.  
  858.  
  859.   if(_rect != nullptr){
  860.     rect.x = _rect->x;
  861.     rect.y = _rect->y;
  862.     rect.w = _rect->w;
  863.     rect.h = _rect->h;
  864.  
  865.   } else { //no rect set; use full window
  866.     shape::point winSize = getSize();
  867.     rect.x = 0;
  868.     rect.y = 0;
  869.     rect.w = winSize.x;
  870.     rect.h = winSize.y;
  871.  
  872.   }
  873.  
  874.  
  875.   return rect;
  876.  
  877. }
  878.  
  879.  
  880.  
  881.  
  882. f32 Window::getBrightness(){
  883.   KIT_WIN_CHECK_INVALID("Window::getBrightness()");
  884.  
  885.   return SDL_GetWindowBrightness(WIN_PTR);
  886.  
  887. }
  888.  
  889.  
  890.  
  891.  
  892.  
  893. void* Window::setUserdata(const char* name, void* userdata){
  894.   KIT_WIN_CHECK_INVALID("Window::setUserdata()");
  895.  
  896.   if(name == nullptr)
  897.     throw PUSH_ERROR("Window::setUserdata(): name = nullptr");
  898.   if(!strnCmp(WIN_DATA_NAME, name))
  899.     throw PUSH_ERROR("Window::setUserdata(): name = \"" WIN_DATA_NAME "\"");
  900.  
  901.   return SDL_SetWindowData(WIN_PTR, name, userdata);
  902.  
  903. }
  904.  
  905.  
  906.  
  907.  
  908. void Window::setDisplayMode(const DisplayMode* mode){
  909.   KIT_WIN_CHECK_INVALID("Window::setDisplayMode()");
  910.  
  911.   SDL_DisplayMode mode_sdl;
  912.  
  913.   if(mode != nullptr){
  914.     mode_sdl.format       = mode->pixelFmt;
  915.     mode_sdl.w            = mode->w;
  916.     mode_sdl.h            = mode->h;
  917.     mode_sdl.refresh_rate = mode->refreshRate;
  918.   }
  919.  
  920.   if(SDL_SetWindowDisplayMode(WIN_PTR, (mode) ? &mode_sdl : nullptr) < 0)
  921.     throw PUSH_ERRORF("Window::setDisplayMode(): \"%s\"", SDL_GetError());
  922.  
  923. }
  924.  
  925.  
  926.  
  927.  
  928. void Window::setTitle(const char* title){
  929.   KIT_WIN_CHECK_INVALID("Window::setTitle()");
  930.  
  931.   SDL_SetWindowTitle(WIN_PTR, title);
  932.  
  933. }
  934.  
  935.  
  936.  
  937.  
  938. void Window::setSize(s32 width, s32 height){
  939.   KIT_WIN_CHECK_INVALID("Window::setSize()");
  940.  
  941.   if(width  < 0) throw PUSH_ERROR("Window::setSize(): width < 0");
  942.   if(height < 0) throw PUSH_ERROR("Window::setSize(): height < 0");
  943.  
  944.   SDL_SetWindowSize(WIN_PTR, width, height);
  945.  
  946. }
  947.  
  948.  
  949.  
  950.  
  951. void Window::setVisibility(bool visible){
  952.   KIT_WIN_CHECK_INVALID("Window::setVisibility()");
  953.  
  954.   if(visible) SDL_ShowWindow(WIN_PTR);
  955.   else        SDL_HideWindow(WIN_PTR);
  956.  
  957. }
  958.  
  959.  
  960.  
  961.  
  962. void Window::setFullscreen(u32 mode){
  963.   KIT_WIN_CHECK_INVALID("Window::setFullscreen()");
  964.  
  965.   u32 flags = 0;
  966.   if     (mode == 1) flags = SDL_WINDOW_FULLSCREEN;
  967.   else if(mode == 2) flags = SDL_WINDOW_FULLSCREEN_DESKTOP;
  968.  
  969.   if(SDL_SetWindowFullscreen(WIN_PTR, flags) < 0)
  970.     throw PUSH_ERRORF("Window::setFullscreen(): \"%s\"", SDL_GetError());
  971.  
  972. }
  973.  
  974.  
  975.  
  976.  
  977. void Window::setResizable(bool enable){
  978.   KIT_WIN_CHECK_INVALID("Window::setResizable()");
  979.  
  980.   SDL_SetWindowResizable(WIN_PTR, (SDL_bool)enable);
  981.  
  982. }
  983.  
  984.  
  985.  
  986.  
  987. void Window::setBordered(bool enable){
  988.   KIT_WIN_CHECK_INVALID("Window::setBordered()");
  989.  
  990.   SDL_SetWindowBordered(WIN_PTR, (SDL_bool)enable);
  991.  
  992. }
  993.  
  994.  
  995.  
  996.  
  997. void Window::setAlwaysOnTop(bool enable){
  998.   KIT_WIN_CHECK_INVALID("Window::setAlwaysOnTop()");
  999.  
  1000.   SDL_SetWindowAlwaysOnTop(WIN_PTR, (SDL_bool)enable);
  1001.  
  1002. }
  1003.  
  1004.  
  1005.  
  1006.  
  1007. void Window::setOpacity(f32 opacity){
  1008.   KIT_WIN_CHECK_INVALID("Window::setOpacity()");
  1009.  
  1010.   if(SDL_SetWindowOpacity(WIN_PTR, opacity) < 0)
  1011.     throw PUSH_ERRORF("Window::setOpacity(): \"%s\"", SDL_GetError());
  1012.  
  1013. }
  1014.  
  1015.  
  1016.  
  1017.  
  1018. void Window::setMinSize(s32 minWidth, s32 minHeight){
  1019.   KIT_WIN_CHECK_INVALID("Window::setMinSize()");
  1020.  
  1021.   if(minWidth  < 0) throw PUSH_ERROR("Window::setMinSize(): minWidth < 0");
  1022.   if(minHeight < 0) throw PUSH_ERROR("Window::setMinSize(): minHeight < 0");
  1023.  
  1024.   SDL_SetWindowMinimumSize(WIN_PTR, minWidth, minHeight);
  1025.  
  1026. }
  1027.  
  1028.  
  1029. void Window::setMaxSize(s32 maxWidth, s32 maxHeight){
  1030.   KIT_WIN_CHECK_INVALID("Window::setMaxSize()");
  1031.  
  1032.   if(maxWidth  < 0) throw PUSH_ERROR("Window::setMaxSize(): maxWidth < 0");
  1033.   if(maxHeight < 0) throw PUSH_ERROR("Window::setMaxSize(): maxHeight < 0");
  1034.  
  1035.   SDL_SetWindowMaximumSize(WIN_PTR, maxWidth, maxHeight);
  1036.  
  1037. }
  1038.  
  1039.  
  1040.  
  1041.  
  1042. void Window::setPosition(s32 x, s32 y){
  1043.   KIT_WIN_CHECK_INVALID("Window::setPosition()");
  1044.  
  1045.   SDL_SetWindowPosition(WIN_PTR, x, y);
  1046.  
  1047. }
  1048.  
  1049.  
  1050.  
  1051.  
  1052. void Window::setGrab(bool enable){
  1053.   KIT_WIN_CHECK_INVALID("Window::setGrab()");
  1054.  
  1055.   SDL_SetWindowGrab(WIN_PTR, (SDL_bool)enable);
  1056.  
  1057. }
  1058.  
  1059.  
  1060.  
  1061.  
  1062. void Window::setKeyboardGrab(bool enable){
  1063.   KIT_WIN_CHECK_INVALID("Window::setKeyboardGrab()");
  1064.  
  1065.   SDL_SetWindowKeyboardGrab(WIN_PTR, (SDL_bool)enable);
  1066.  
  1067. }
  1068.  
  1069.  
  1070.  
  1071.  
  1072. void Window::setMouseGrab(bool enable){
  1073.   KIT_WIN_CHECK_INVALID("Window::setMouseGrab()");
  1074.  
  1075.   SDL_SetWindowMouseGrab(WIN_PTR, (SDL_bool)enable);
  1076.  
  1077. }
  1078.  
  1079.  
  1080. void Window::setMouseGrabRect(const shape::rect* rect){
  1081.   KIT_WIN_CHECK_INVALID("Window::setMouseGrabRect()");
  1082.  
  1083.   if(SDL_SetWindowMouseRect(WIN_PTR, (const SDL_Rect*)rect) < 0)
  1084.     throw PUSH_ERRORF("Window::setMouseGrabRect(): \"%s\"", SDL_GetError());
  1085.  
  1086. }
  1087.  
  1088.  
  1089.  
  1090.  
  1091. void Window::setBrightness(f32 brightness){
  1092.   KIT_WIN_CHECK_INVALID("Window::setBrightness()");
  1093.  
  1094.   if(SDL_SetWindowBrightness(WIN_PTR, brightness) < 0)
  1095.     throw PUSH_ERRORF("Window::setBrightness(): \"%s\"", SDL_GetError());
  1096.  
  1097. }
  1098.  
  1099.  
  1100.  
  1101.  
  1102.  
  1103. void Window::warpMouse(s32 x, s32 y){
  1104.   KIT_WIN_CHECK_INVALID("Window::warpMouse()");
  1105.  
  1106.   SDL_WarpMouseInWindow(WIN_PTR, x, y);
  1107.  
  1108. }
  1109.  
  1110.  
  1111.  
  1112.  
  1113. void Window::minimize(){
  1114.   KIT_WIN_CHECK_INVALID("Window::minimize()");
  1115.  
  1116.   SDL_MinimizeWindow(WIN_PTR);
  1117.  
  1118. }
  1119.  
  1120.  
  1121. void Window::maximize(){
  1122.   KIT_WIN_CHECK_INVALID("Window::maximize()");
  1123.  
  1124.   SDL_MaximizeWindow(WIN_PTR);
  1125.  
  1126. }
  1127.  
  1128.  
  1129. void Window::restore(){
  1130.   KIT_WIN_CHECK_INVALID("Window::restore()");
  1131.  
  1132.   SDL_RestoreWindow(WIN_PTR);
  1133.  
  1134. }
  1135.  
  1136.  
  1137.  
  1138.  
  1139. void Window::raise(){
  1140.   KIT_WIN_CHECK_INVALID("Window::raise()");
  1141.  
  1142.   SDL_RaiseWindow(WIN_PTR);
  1143.  
  1144. }
  1145.  
  1146.  
  1147.  
  1148.  
  1149.  
  1150. Window* getGrabbedWindow(){ //not a part of Window
  1151.   SDL_Window* win_sdl = SDL_GetGrabbedWindow();
  1152.   if(win_sdl == nullptr) return nullptr;
  1153.  
  1154.   return (Window*)SDL_GetWindowData(win_sdl, WIN_DATA_NAME);
  1155.  
  1156. }
  1157.  
  1158.  
  1159.  
  1160.  
  1161.  
  1162. }; /* namespace kit */
  1163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement