Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //my own primitive form of contiguous dynamic arrays
- #include "../include/kit_sdl2/kit_macroconst.h"
- #include "../include/kit_sdl2/kit_core.h"
- #include "../_private/include/_kit_privmacro.h"
- static inline int _getDimensions(Uint32 x, Uint32 y, Uint32 z){
- int dims = 1;
- if(y>1) dims = 2;
- if(z>1) dims = 3;
- return dims;
- }
- static inline void _truncateLens(Uint32* lens, Uint64 lenslen,
- Uint32 x, Uint32 y, Uint32 z)
- {
- Uint32 lens_max=_getLens_max(x,y,z);
- //kit_coreLog("lens_max=%u, lenslen=%u, xyz=%u,%u,%u",
- // lens_max, lenslen, x, y, z);
- for(Uint32 i; i<lenslen; ++i)
- if(lens[i]>lens_max) lens[i]=lens_max;
- }
- static inline Uint64 _getLenslen(Uint32 x, Uint32 y, Uint32 z){
- Uint64 lenslen = 1;
- if(y>1) lenslen *= x;
- if(z>1) lenslen *= y;
- //kit_coreLog("lenslen=%u, xyz=%u,%u,%u", lenslen, x,y,z);
- return lenslen;
- }
- static inline Uint32 _getLens_max(Uint32 x, Uint32 y, Uint32 z){
- Uint32 lens_max;
- if( z>1) lens_max = z;
- else if(y>1) lens_max = y;
- else /*x>1*/lens_max = x;
- return lens_max;
- }
- int _resizeLens(kit_coreVector* Vector){ //also truncates
- Uint32 x=Vector->x;
- Uint32 y=Vector->y;
- Uint32 z=Vector->z;
- Uint32 lenslen_old = Vector->lenslen;
- Uint32 lenslen_new = _getLenslen(x,y,z);
- Uint32 lensize_old = lenslen_old*sizeof(Uint32);
- Uint32 lensize_new = lenslen_new*sizeof(Uint32);
- _IF_SDLERR(kit_coreRealloc(&Vector->lens, lensize_old,lensize_new),;,"!lengths")
- /*!err*/ return 0;
- _error_: return -1;
- }
- //called when SetFast isn't applicable
- //(probably) slower than SetFast, hence the name
- int _kit_coreVectorSetSlow(kit_coreVector** Vector_p, Uint32 x_new, Uint32 y_new, Uint32 z_new){
- kit_coreLog("SetSlow");
- kit_coreVector* Vector=*Vector_p;
- kit_coreVector vin=*Vector; //v(ector) in
- kit_coreVector* VectorOut=kit_coreVectorCreate(x_new,y_new,z_new, vin.unit, vin.type.n);
- _IF_GOTO_ERROR(VectorOut==NULL,;)
- kit_coreVector vout=*VectorOut; //v(ector) out
- //copy input data to output
- //the size of each row, in bytes, to be fed into memcpy
- Uint32 dataRow_size = MIN(vin.unit*vin.x, vout.unit*vout.x);
- Uint32 idata_inc = vin.unit*vin.x; //input data increase
- Uint32 odata_inc = vout.unit*vout.x; //output data increase
- Uint32 yi_max = MIN(vin.y, vout.y);
- Uint32 zi_max = MIN(vin.z, vout.z);
- Sint32 y_difference = vout.y-vin.y;
- for(Uint32 zi=0; zi<zi_max; ++zi){
- for(Uint32 yi=0; yi<yi_max; ++yi){
- SDL_memcpy(vout.data, vin.data, dataRow_size);
- vin.data +=idata_inc;
- vout.data+=odata_inc;
- }
- //skip output forward by any empty space left
- if(y_difference>0) vout.data += odata_inc*y_difference;
- }
- kit_coreVectorDestroy(Vector_p); //destroy original
- *Vector_p=VectorOut; //set vector pointer to the new one
- /*!err*/ return 0; // 0 on success
- _error_: return -1; //-1 on error
- }
- //should be called only when resizing is done on the same axis as its maximum,
- //like when only x is being resized on a currently 1D vector, or only the y on a 2D vector, etc.
- int _kit_coreVectorSetFast(kit_coreVector** Vector_p, Uint32 x_new, Uint32 y_new, Uint32 z_new){
- kit_coreLog("SetFast");
- kit_coreVector* Vector=*Vector_p;
- Uint64 totalSize_old = sizeof(kit_coreVector) + Vector->datasize;
- Uint64 lengthsSize_old = Vector->lensize;
- Uint64 totalSize_new = sizeof(kit_coreVector) + Vector->unit*x_new*y_new*z_new;
- Uint64 lengthsSize_new = _getLensize(x_new,y_new,z_new);
- _IF_SDLERR(kit_coreRealloc(&Vector, totalSize_old, totalSize_new),;,"!Vector")
- Vector->datasize = totalSize_new-sizeof(kit_coreVector); //size of vector struct not counted
- Vector->data = (void*)Vector+sizeof(kit_coreVector);
- /*!err*/ *Vector_p=Vector; return 0;
- _error_: *Vector_p=Vector; return -1;
- }
- //set new size (if <x,y,z>_new == 0, then leave that axis unchanged)
- int kit_coreVectorSet(kit_coreVector** Vector_p, Uint32 x_new, Uint32 y_new, Uint32 z_new){
- _IF_SDLERR(Vector_p==NULL,;,"!Vector_p")
- kit_coreVector* Vector=*Vector_p;
- _IF_SDLERR(Vector==NULL,;,"!*Vector_p")
- if(!x_new && !y_new && !z_new) goto _noerr_;
- _IF_SDLERR(x_new&U32_MSb,;,"x<0")
- _IF_SDLERR(y_new&U32_MSb,;,"y<0")
- _IF_SDLERR(z_new&U32_MSb,;,"z<0")
- Uint32 x_old=Vector->x;
- Uint32 y_old=Vector->y;
- Uint32 z_old=Vector->z;
- if(!x_new) x_new=x_old;
- if(!y_new) y_new=y_old;
- if(!z_new) z_new=z_old;
- int dimensions_old = _getDimensions(x_old,y_old,z_old);
- int decision;
- decision = dimensions_old<<12; //dimensions on 3rd nybble
- decision |= (x_old!=x_new)<< 8; //'did x axis change?' on 2nd nybble
- decision |= (y_old!=y_new)<< 4; //'did y axis change?' on 1st nybble
- decision |= (z_old!=z_new) ; //'did z axis change?' on 0th nybble
- if(!(decision&0xFFF)) goto _noerr_; //return early if everything is the same
- switch(decision){
- case 0x1100: SDL_FALLTHROUGH;
- case 0x2010: SDL_FALLTHROUGH;
- case 0x3001: _IF_GOTO_ERROR(_kit_coreVectorSetFast(Vector_p, x_new,y_new,z_new),;) break;
- default: _IF_GOTO_ERROR(_kit_coreVectorSetSlow(Vector_p, x_new,y_new,z_new),;)
- }
- Vector=*Vector_p;
- Vector->z_block = x_new*y_new;
- Vector->x=x_new;
- Vector->y=y_new;
- Vector->z=z_new;
- _noerr_: return 0; // 0 on success
- _error_: return -1; //-1 on failure
- }
- //add to or subtract from size
- int kit_coreVectorAdd(kit_coreVector** Vector_p, Sint32 x_add, Sint32 y_add, Sint32 z_add){
- _IF_SDLERR(Vector_p==NULL,;,"!Vector_p")
- kit_coreVector* Vector=*Vector_p;
- _IF_SDLERR(Vector==NULL,;,"!*Vector_p")
- Uint32 x_new = Vector->x+x_add;
- Uint32 y_new = Vector->y+y_add;
- Uint32 z_new = Vector->z+z_add;
- //==0 error is done here, since its behavior is different in VectorSet
- _IF_SDLERR(x_new==0,;,"x==0")
- _IF_SDLERR(y_new==0,;,"y==0")
- _IF_SDLERR(z_new==0,;,"z==0")
- _IF_GOTO_ERROR(kit_coreVectorSet(Vector_p, x_new,y_new,z_new),;)
- /*!err*/ return 0; // 0 on success
- _error_: return -1; //-1 on failure
- }
- Uint32 kit_coreVectorAppend(kit_coreVector** Vector_p, void* src, Uint32 x_pos, Uint32 y_pos){
- Uint32 newIndex=-1; //0xffffffff
- _IF_SDLERR(Vector_p==NULL,;,"!Vector_p")
- kit_coreVector* Vector=*Vector_p;
- _IF_SDLERR(Vector==NULL,;,"!*Vector_p")
- //TBD
- _error_:
- return newIndex;
- }
- int kit_coreVectorDestroy(kit_coreVector** Vector_p){
- _IF_SDLERR(Vector_p==NULL,;,"!Vector_p")
- kit_coreVector* Vector=*Vector_p;
- _IF_GOTO(Vector==NULL,_noerr_,;) //exit early if vector is already destroyed
- if(Vector->lens != NULL) SDL_free(Vector->lens);
- SDL_free(Vector);
- *Vector_p=NULL;
- _noerr_: return 0; // 0 on success
- _error_: return -1; //-1 on failure
- }
- kit_coreVector* kit_coreVectorCreate(Uint32 x, Uint32 y, Uint32 z, Uint32 unit, Uint64 type_n){
- kit_coreVector* Vector=NULL;
- _IF_SDLERR(x==0,;,"x==0")
- _IF_SDLERR(y==0,;,"y==0")
- _IF_SDLERR(z==0,;,"z==0")
- _IF_SDLERR(unit==0,;,"unit==0")
- _IF_SDLERR(x&U32_MSb,;,"x<0")
- _IF_SDLERR(y&U32_MSb,;,"y<0")
- _IF_SDLERR(z&U32_MSb,;,"z<0")
- _IF_SDLERR(unit&U32_MSb,;,"unit<0")
- //allocate memory for vector
- Uint64 VectorSize = unit*x*y*z;
- _IF_SDLERR(kit_coreRealloc(&Vector, 0,sizeof(kit_coreVector)+VectorSize) ,;,"!Vector")
- Vector->data = (void*)Vector+sizeof(kit_coreVector);
- Vector->unit=-1;
- //allocate memory for lengths
- //(z is not used here, because even 3D vectors only need a 2D array of Uint32)
- //Uint64 lengthsSize = _getLensize(x,y,z);
- //_IF_SDLERR(kit_coreRealloc(&Vector->lens, 0,lengthsSize),;,"!lengths")
- //set the rest of the struct's values
- //set last byte of type.n to 0 so it can always be interpreted as a string (type.s)
- Vector->type.n = type_n&U64_MSBC;
- Vector->z_block = x*y;
- Vector->lensize=lengthsSize;
- Vector->datasize=VectorSize;
- Vector->x=x, Vector->y=y, Vector->z=z;
- Vector->unit=unit;
- _error_: //Vector->unit will remain -1 on error
- if(Vector!=NULL && Vector->unit==-1) kit_coreVectorDestroy(&Vector);
- return Vector;
- }
- kit_coreVector* kit_coreVectorCopy(kit_coreVector* Vector){
- kit_coreVector* VectorOut=NULL;
- _IF_SDLERR(Vector==NULL,;,"!Vector")
- Uint64 type_n = Vector->type.n;
- Uint64 z_block = Vector->z_block;
- Uint64 lensize = Vector->lensize;
- Uint64 datasize = Vector->datasize;
- Uint32 x = Vector->x;
- Uint32 y = Vector->y;
- Uint32 z = Vector->z;
- Uint32 unit = Vector->unit;
- Uint32* lens = Vector->lens;
- void* data = Vector->data;
- VectorOut=kit_coreVectorCreate(x,y,z, unit, type_n);
- _IF_GOTO_ERROR(VectorOut==NULL,;)
- VectorOut->type.n = type_n;
- VectorOut->z_block = z_block;
- VectorOut->lensize = lensize;
- VectorOut->datasize = datasize;
- VectorOut->x = x;
- VectorOut->y = y;
- VectorOut->z = z;
- VectorOut->unit = unit;
- SDL_memcpy(VectorOut->lens, lens, lensize);
- SDL_memcpy(VectorOut->data, data, datasize);
- _error_:
- return VectorOut;
- }
- #if defined(_KIT_CORE_DEBUG) || defined(_KIT_ALL_DEBUG)
- extern int printf(const char*,...);
- void kit_coreVectorPrintInt(kit_coreVector* Vector,const char* prefix){
- char msg[]="PrintInt";
- const char* _prefix=prefix;
- if(prefix==NULL || prefix==(void*)1) _prefix=msg;
- if(Vector==NULL){ kit_coreLog("%s: Vector cannot be NULL", _prefix); return; }
- if(Vector->unit != sizeof(Uint32)){
- kit_coreLog("%s: Vector->unit != sizeof(Uint32)", _prefix); return;
- }
- Uint32 x_s=Vector->x, y_s=Vector->y, z_s=Vector->z; Sint32* i32=Vector->data;
- Uint32 index=-1;
- if(prefix==(void*)1) goto print_results; //skip test fill if _prefix==1
- //test fill
- for(Uint32 zi=0; zi<z_s; ++zi){
- for(Uint32 yi=0; yi<y_s; ++yi){
- for(Uint32 xi=0; xi<x_s; ++xi) i32[++index] = ((xi+zi)<<4) | (yi+zi);
- }
- }
- //print results
- if(prefix==NULL) return; //now this part is optional!
- print_results:;
- index=-1;
- printf("%s: %u,%u,%u\n", _prefix, x_s,y_s,z_s);
- for(Uint32 zi=0; zi<z_s; ++zi){
- printf("zi=%u:\n",zi);
- for(Uint32 yi=0; yi<y_s; ++yi){
- for(Uint32 xi=0; xi<x_s; ++xi) printf("%02X,",i32[++index]);
- printf("\n");
- }
- printf("\n");
- }
- }
- void kit_coreVectorPrintLens(kit_coreVector* Vector,const char* prefix){
- char msg[]="PrintLens";
- const char* _prefix=prefix;
- if(prefix==NULL || prefix==(void*)1) _prefix=msg;
- if(Vector==NULL){ kit_coreLog("%s: Vector cannot be NULL", _prefix); return; }
- Uint32 x_s=Vector->x, y_s=Vector->y; Uint32* u32=Vector->lens;
- Uint32 index=-1;
- if(prefix==(void*)1) goto print_results; //skip test fill if _prefix==1
- //test fill
- for(Uint32 yi=0; yi<y_s; ++yi){
- for(Uint32 xi=0; xi<x_s; ++xi)
- u32[++index] = (xi<<4) | yi;
- }
- //print results
- if(prefix==NULL) return;
- print_results:;
- index=-1;
- printf("%s: %u,%u\n", _prefix, x_s,y_s);
- for(Uint32 yi=0; yi<y_s; ++yi){
- for(Uint32 xi=0; xi<x_s; ++xi)
- printf("%02X,",u32[++index]);
- printf("\n");
- }
- }
- #define _vectorLogT(...) kit_coreLog(__VA_ARGS__)
- //#define _vectorLogT(...) ;
- int kit_coreVectorTest(){
- int returnStatus=0;
- kit_coreVector* Vector=NULL; SDL_SetError("(no error)");
- ++returnStatus;
- //_error_:
- --returnStatus;
- if(returnStatus){
- _vectorLogT("failed; error = \"%s\"",SDL_GetError());
- } else if(Vector != NULL){
- _vectorLogT("attempting to destroy vector");
- if(kit_coreVectorDestroy(&Vector))
- _vectorLogT("failed; error = \"%s\"",SDL_GetError());
- }
- return returnStatus;
- }
- #else
- int kit_coreVectorTest(){
- kit_coreLog("\"_KIT_CORE_DEBUG\" was not defined during compilation of kit_core");
- SDL_SetError("!defined(_KIT_CORE_DEBUG)");
- return 1;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement