Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "../include/kit_sdl2/kit_core.h"
- #include "../_private/include/_kit_privmacro.h"
- #include "../_private/include/_kit_coreAllPrivate.h"
- //todo: make prng that updates every millisecond
- //also, kill the prng thread if coreglobals.shutdown is 1 or w/e
- #if defined(_KIT_CORE_DEBUG) || defined(_KIT_ALL_DEBUG)
- const SDL_bool kit_coreIsDebug=SDL_TRUE;
- #else
- const SDL_bool kit_coreIsDebug=SDL_FALSE;
- #endif
- struct _kit_coreGlobals_t _kit_coreGlobals;
- const char boolstr[2][6] = {"false\x00","true\x00\x00"};
- //used to multiply an int by the inverse of an int to get a normalized float
- const float inv_i_8 = 1.0f/0x7f; // = 0.007874015748031496062992125984251968503937
- const float inv_i16 = 1.0f/0x7fff; // = 0.000030518509475997192297128208258308664204
- const float inv_i32 = 1.0f/0x7fffffff; // = 0.000000000465661287524579692410575082716799
- //same thing, but for pi
- const float inv_qpi = 1.0f/QPI ; // = 1.27323954474 (quarter of pi)
- const float inv_hpi = 1.0f/HPI ; // = 0.63661977236 (half of pi)
- const float inv_pi = 1.0f/ PI ; // = 0.31830988618
- const float inv_pi2 = 1.0f/ PI2; // = 0.15915494309
- float kit_coreSawf(float x){ //like sinf, but for a sawtooth wave
- return inv_pi*SDL_fmodf(x+PI,PI2) - 1.0f;
- }
- float kit_coreTrif(float x){ //like sinf, but for a triangle wave
- const float sawtooth = inv_hpi*SDL_fmodf(x+HPI,PI2) - 2.0f;
- if(sawtooth<0) return 1.0f + sawtooth;
- else return 1.0f - sawtooth;
- }
- /*
- //(make memcpy that uses simd intrinsics if its speed becomes an issue)
- void* kit_coreMemcpy(void* dst, const void* src, size_t size){
- if(!size) return dst;
- return SDL_memcpy(dst,src,size);
- }
- //(make memset that uses simd intrinsics if its speed becomes an issue)
- void* kit_coreMemset(void* dst, int value, size_t size){
- if(!size) return dst;
- return SDL_memset(dst, value, size);
- }*/
- //initializes any added memory to 0 (unless size_old = NO_MEMSET)
- int kit_coreRealloc(void* ptr_p, size_t size_old, size_t size_new){
- _IF_GOTO(size_old==size_new,_noerr_,;)
- _IF_SDLERR(size_old>NO_MEMSET,;,"size_old>%p",(void*)NO_MEMSET)
- _IF_SDLERR(size_new>NO_MEMSET,;,"size_new>%p",(void*)NO_MEMSET)
- void* ptr=SDL_realloc(*(void**)ptr_p,size_new);
- _IF_SDLERR(ptr==NULL,;,"!realloc")
- if((size_new>size_old) && (size_old!=NO_MEMSET))
- kit_coreMemset(ptr+size_old, 0, size_new-size_old);
- *(void**)ptr_p=ptr;
- _noerr_: return 0;
- _error_: return -1;
- }
- int kit_coreInit(Uint32 flags){
- if(_kit_coreGlobals.init){ SDL_SetError("core is already initialized!"); return 1; }
- if(flags && SDL_Init(flags)<0) return -1; //should only call SDL_Init if flags!=0
- _kit_coreGlobals.lock=SDL_CreateMutex();
- if(_kit_coreGlobals.lock == NULL) return -2;
- if(SDL_LockMutex(_kit_coreGlobals.lock)<0) return -3;
- _kit_coreGlobals.cores = SDL_GetCPUCount();
- _kit_coreGlobals.capabilities =SDL_HasSSE() <<5;
- _kit_coreGlobals.capabilities|=SDL_HasSSE2() <<4;
- _kit_coreGlobals.capabilities|=SDL_HasSSE3() <<3;
- _kit_coreGlobals.capabilities|=SDL_HasSSE41()<<2;
- _kit_coreGlobals.capabilities|=SDL_HasAVX() <<1;
- _kit_coreGlobals.capabilities|=SDL_HasAVX2() ;
- _kit_coreGlobals.init = 1;
- if(SDL_UnlockMutex(_kit_coreGlobals.lock)<0) return -4;
- return 0;
- }
- int kit_coreQuit(){
- if(!_kit_coreGlobals.init){ SDL_SetError("core is not initialized!"); return 1; }
- if(SDL_LockMutex(_kit_coreGlobals.lock)<0) return -1;
- SDL_Quit(); //this is safe to use, even if SDL was never initialized!
- _kit_coreGlobals.init = 0;
- if(SDL_UnlockMutex(_kit_coreGlobals.lock)<0) return -2;
- SDL_DestroyMutex(_kit_coreGlobals.lock);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement