Advertisement
Kitomas

kit_sdl2_core.c as of 2023-10-12

Oct 12th, 2023
1,009
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.68 KB | None | 0 0
  1. #include "../include/kit_sdl2/kit_core.h"
  2. #include "../_private/include/_kit_privmacro.h"
  3. #include "../_private/include/_kit_coreAllPrivate.h"
  4. //todo: make prng that updates every millisecond
  5. //also, kill the prng thread if coreglobals.shutdown is 1 or w/e
  6.  
  7.  
  8.  
  9.  
  10. #if defined(_KIT_CORE_DEBUG) || defined(_KIT_ALL_DEBUG)
  11. const SDL_bool kit_coreIsDebug=SDL_TRUE;
  12. #else
  13. const SDL_bool kit_coreIsDebug=SDL_FALSE;
  14. #endif
  15.  
  16.  
  17.  
  18.  
  19. struct _kit_coreGlobals_t _kit_coreGlobals;
  20.  
  21.  
  22.  
  23.  
  24. const char boolstr[2][6] = {"false\x00","true\x00\x00"};
  25.  
  26.  
  27. //used to multiply an int by the inverse of an int to get a normalized float
  28. const float inv_i_8 = 1.0f/0x7f;       // = 0.007874015748031496062992125984251968503937
  29. const float inv_i16 = 1.0f/0x7fff;     // = 0.000030518509475997192297128208258308664204
  30. const float inv_i32 = 1.0f/0x7fffffff; // = 0.000000000465661287524579692410575082716799
  31.  
  32.  
  33. //same thing, but for pi
  34. const float inv_qpi = 1.0f/QPI ; // = 1.27323954474 (quarter of pi)
  35. const float inv_hpi = 1.0f/HPI ; // = 0.63661977236 (half of pi)
  36. const float inv_pi  = 1.0f/ PI ; // = 0.31830988618
  37. const float inv_pi2 = 1.0f/ PI2; // = 0.15915494309
  38.  
  39.  
  40.  
  41.  
  42. float kit_coreSawf(float x){ //like sinf, but for a sawtooth wave
  43.   return inv_pi*SDL_fmodf(x+PI,PI2) - 1.0f;
  44. }
  45.  
  46.  
  47.  
  48. float kit_coreTrif(float x){ //like sinf, but for a triangle wave
  49.   const float sawtooth = inv_hpi*SDL_fmodf(x+HPI,PI2) - 2.0f;
  50.   if(sawtooth<0) return 1.0f + sawtooth;
  51.   else           return 1.0f - sawtooth;
  52. }
  53.  
  54.  
  55.  
  56.  
  57. /*
  58. //(make memcpy that uses simd intrinsics if its speed becomes an issue)
  59. void* kit_coreMemcpy(void* dst, const void* src, size_t size){
  60.   if(!size) return dst;
  61.   return SDL_memcpy(dst,src,size);
  62. }
  63.  
  64.  
  65.  
  66. //(make memset that uses simd intrinsics if its speed becomes an issue)
  67. void* kit_coreMemset(void* dst, int value, size_t size){
  68.   if(!size) return dst;
  69.   return SDL_memset(dst, value, size);
  70. }*/
  71.  
  72.  
  73.  
  74. //initializes any added memory to 0 (unless size_old = NO_MEMSET)
  75. int kit_coreRealloc(void* ptr_p, size_t size_old, size_t size_new){
  76.   _IF_GOTO(size_old==size_new,_noerr_,;)
  77.   _IF_SDLERR(size_old>NO_MEMSET,;,"size_old>%p",(void*)NO_MEMSET)
  78.   _IF_SDLERR(size_new>NO_MEMSET,;,"size_new>%p",(void*)NO_MEMSET)
  79.  
  80.   void* ptr=SDL_realloc(*(void**)ptr_p,size_new);
  81.   _IF_SDLERR(ptr==NULL,;,"!realloc")
  82.  
  83.   if((size_new>size_old) && (size_old!=NO_MEMSET))
  84.     kit_coreMemset(ptr+size_old, 0, size_new-size_old);
  85.  
  86.   *(void**)ptr_p=ptr;
  87.  
  88.   _noerr_: return  0;
  89.   _error_: return -1;
  90. }
  91.  
  92.  
  93.  
  94.  
  95. int kit_coreInit(Uint32 flags){
  96.   if(_kit_coreGlobals.init){ SDL_SetError("core is already initialized!"); return 1; }
  97.   if(flags && SDL_Init(flags)<0) return -1; //should only call SDL_Init if flags!=0
  98.  
  99.   _kit_coreGlobals.lock=SDL_CreateMutex();
  100.   if(_kit_coreGlobals.lock == NULL) return -2;
  101.   if(SDL_LockMutex(_kit_coreGlobals.lock)<0) return -3;
  102.  
  103.   _kit_coreGlobals.cores = SDL_GetCPUCount();
  104.  
  105.   _kit_coreGlobals.capabilities =SDL_HasSSE()  <<5;
  106.   _kit_coreGlobals.capabilities|=SDL_HasSSE2() <<4;
  107.   _kit_coreGlobals.capabilities|=SDL_HasSSE3() <<3;
  108.   _kit_coreGlobals.capabilities|=SDL_HasSSE41()<<2;
  109.   _kit_coreGlobals.capabilities|=SDL_HasAVX()  <<1;
  110.   _kit_coreGlobals.capabilities|=SDL_HasAVX2()    ;
  111.  
  112.   _kit_coreGlobals.init = 1;
  113.   if(SDL_UnlockMutex(_kit_coreGlobals.lock)<0) return -4;
  114.   return 0;
  115. }
  116.  
  117.  
  118.  
  119. int kit_coreQuit(){
  120.   if(!_kit_coreGlobals.init){ SDL_SetError("core is not initialized!"); return 1; }
  121.   if(SDL_LockMutex(_kit_coreGlobals.lock)<0) return -1;
  122.  
  123.   SDL_Quit(); //this is safe to use, even if SDL was never initialized!
  124.  
  125.   _kit_coreGlobals.init = 0;
  126.   if(SDL_UnlockMutex(_kit_coreGlobals.lock)<0) return -2;
  127.   SDL_DestroyMutex(_kit_coreGlobals.lock);
  128.   return 0;
  129. }
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement