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_core.h"
- #include "../_private/include/_kit_privmacro.h"
- //(MAYBE at some point replace <x/y/z>_new with <x/y/z>_old when axes aren't updated)
- //for debug
- extern int printf(const char*,...);
- void _kit_coreVectorPrintInt(kit_coreVector* Vector,const char* prefix){
- if(Vector==NULL){ printf("%s: Vector cannot be NULL\n", prefix); return; }
- Uint32 x_s=Vector->x, y_s=Vector->y, z_s=Vector->z; void* ptr=Vector->ptr;
- Sint32 *i1d=ptr, **i2d=ptr, ***i3d=ptr, dims=Vector->_dims;
- Uint32 xi=0, yi=0, zi=0;
- //test fill
- if(dims!=3) goto _n3d_fs;
- for(zi=0; zi<z_s; ++zi){ _n3d_fs: if(dims==1) goto _n2d_fs;
- for(yi=0; yi<y_s; ++yi){ _n2d_fs:
- for(xi=0; xi<x_s; ++xi){
- switch(dims){
- case 1: i1d[xi] =xi; break;
- case 2: i2d[xi][yi] =(xi<<4)|yi; break;
- case 3: i3d[xi][yi][zi]=((xi+zi)<<4)|(yi+zi); }
- } if(dims==1) goto _n2d_fe;
- } _n2d_fe: if(dims!=3) goto _n3d_fe;
- } _n3d_fe:
- //print
- printf("%s: %u,%u,%u (%uD)\n", prefix, x_s,y_s,z_s, dims);
- if(dims!=3) goto _n3d_ps;
- for(zi=0; zi<z_s; ++zi){ _n3d_ps: if(dims==1) goto _n2d_ps;
- for(yi=0; yi<y_s; ++yi){ _n2d_ps: if(!zi) printf("0");
- for(xi=0; xi<x_s; ++xi){
- switch(dims){
- case 1: printf("%X,",i1d[xi]); break;
- case 2: printf("%X,",i2d[xi][yi]); break;
- case 3: printf("%X,",i3d[xi][yi][zi]); }
- } if(dims==1) goto _n2d_pe;
- printf("\n");
- } _n2d_pe: if(dims!=3) goto _n3d_pe;
- printf("\n");
- } _n3d_pe: printf("\n");
- }
- //initializes any added memory if difference is positive
- int _kit_coreVectorRealloc(void* ptr_p, char axis,
- Uint32 osize, Uint32 nsize)
- {
- if(osize==nsize) return 0;
- void* _ptr=SDL_realloc(*(void**)ptr_p,nsize);
- if(_ptr==NULL){
- if(axis!=0) SDL_SetError("SDL_realloc() of %c axis failed",axis);
- return -1;
- }
- if(nsize>osize) SDL_memset(_ptr+osize, 0, /*size_difference=*/nsize-osize);
- *(void**)ptr_p=_ptr;
- return 0;
- }
- void _kit_coreVectorFreeZinY(kit_coreVector* Vector,
- Uint32 x_start, Uint32 x_end,
- Uint32 y_start, Uint32 y_end)
- {
- if((Vector->_dims&3)<3) return; //z doesn't exist in 1d or 2d array
- void*** p3d=Vector->p3d;
- for(Uint32 xi=x_start; xi<x_end; ++xi){ void** p2d=p3d[xi];
- if(p2d==NULL) break;
- for(Uint32 yi=y_start; yi<y_end; ++yi){ void* p1d=p2d[yi];
- if(p1d==NULL) break;
- SDL_free(p1d); p2d[yi]=NULL;
- }
- }
- }
- void _kit_coreVectorFreeYinX(kit_coreVector* Vector,
- Uint32 x_start, Uint32 x_end)
- {
- if((Vector->_dims&3)<2) return; //y doesn't exist in 1d array (nor does z)
- void** p2d=Vector->p2d;
- for(Uint32 xi=x_start; xi<x_end; ++xi){ void* p1d=p2d[xi];
- if(p1d==NULL) break;
- SDL_free(p1d); p2d[xi]=NULL;
- }
- }
- int _kit_coreVectorResizeX(kit_coreVector** Vector_p,
- Uint32 x_osize, Uint32 x_nsize, Uint32 x_new)
- {
- //x axis is contiguous with the vector struct itself
- x_osize+=sizeof(kit_coreVector); x_nsize+=sizeof(kit_coreVector);
- if(_kit_coreVectorRealloc(Vector_p,'x', x_osize,x_nsize)) return -12;
- kit_coreVector* Vector=*Vector_p;
- Vector->ptr=(void*)Vector+sizeof(kit_coreVector);
- Vector->x=x_new; *Vector_p=Vector;
- return 0;
- }
- int _kit_coreVectorResizeY(kit_coreVector* Vector,
- Uint32 x_start, Uint32 x_end,
- Uint32 y_osize, Uint32 y_nsize, Uint32 y_new)
- {
- if((Vector->_dims&3)<2) return 0; //y doesn't exist in 1d array (nor does z)
- void** p2d=Vector->p2d;
- for(Uint32 xi=x_start; xi<x_end; ++xi)
- if(_kit_coreVectorRealloc(&p2d[xi],'y', y_osize,y_nsize)) return -13;
- Vector->y=y_new;
- return 0;
- }
- int _kit_coreVectorResizeZ(kit_coreVector* Vector,
- Uint32 x_start, Uint32 x_end,
- Uint32 y_start, Uint32 y_end,
- Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
- {
- if((Vector->_dims&3)<3) return 0; //z doesn't exist in 1d or 2d array
- void*** p3d=Vector->p3d;
- for(Uint32 xi=x_start; xi<x_end; ++xi){ void** p2d=p3d[xi];
- for(Uint32 yi=y_start; yi<y_end; ++yi)
- if(_kit_coreVectorRealloc(&p2d[yi],'z', z_osize,z_nsize)) return -14;
- }
- Vector->z=z_new;
- return 0;
- }
- //uninitialize rS where it isn't needed
- //un-inline these if binary size becomes unacceptable!
- //(remember, <xyz>_old and <xyz>_new are sometimes flipped when used as a parameter when freeing)
- static inline int _kit_coreVectorSubX(kit_coreVector** Vector_p,
- Uint32 x_new, Uint32 x_old, Uint32 y_new,
- Uint32 x_osize, Uint32 x_nsize)
- { //printf("SubX\n");
- kit_coreVector* Vector=*Vector_p;
- _kit_coreVectorFreeZinY(Vector, x_new,x_old, 0,y_new);
- _kit_coreVectorFreeYinX(Vector, x_new,x_old);
- return _kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new);
- }
- static inline int _kit_coreVectorSubY(kit_coreVector* Vector,
- Uint32 x_new, Uint32 y_new, Uint32 y_old,
- Uint32 y_osize, Uint32 y_nsize)
- { //printf("SubY\n");
- _kit_coreVectorFreeZinY(Vector, 0,x_new, y_new,y_old);
- return _kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new);
- }
- static inline int _kit_coreVectorSubZ(kit_coreVector* Vector,
- Uint32 x_new, Uint32 y_new, Uint32 z_osize,
- Uint32 z_nsize, Uint32 z_new)
- { //printf("SubZ\n");
- return _kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new);
- }
- static inline int _kit_coreVectorSubXY(kit_coreVector** Vector_p,
- Uint32 x_new, Uint32 x_old, Uint32 y_old,
- Uint32 x_osize, Uint32 x_nsize,
- Uint32 y_new, Uint32 y_osize, Uint32 y_nsize)
- { //printf("SubXY\n");
- kit_coreVector* Vector=*Vector_p; int rS; //(r)eturn(S)tatus
- _kit_coreVectorFreeZinY(Vector, x_new,x_old, 0,y_old);
- _kit_coreVectorFreeYinX(Vector, x_new,x_old);
- if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
- _kit_coreVectorFreeZinY(Vector, 0,x_new, y_new,y_old);
- return _kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new);
- }
- static inline int _kit_coreVectorSubYZ(kit_coreVector* Vector,
- Uint32 x_new, Uint32 y_new, Uint32 y_old,
- Uint32 y_osize, Uint32 y_nsize,
- Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
- { //printf("SubYZ\n");
- int rS; //(r)eturn(S)tatus
- _kit_coreVectorFreeZinY(Vector, 0,x_new, y_new,y_old);
- if((rS=_kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new))) return rS;
- return _kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new);
- }
- static inline int _kit_coreVectorSubXZ(kit_coreVector** Vector_p,
- Uint32 x_new, Uint32 x_old, Uint32 y_new,
- Uint32 x_osize, Uint32 x_nsize,
- Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
- { //printf("SubXZ\n");
- kit_coreVector* Vector=*Vector_p; int rS; //(r)eturn(S)tatus
- _kit_coreVectorFreeZinY(Vector, x_new,x_old, 0,y_new);
- _kit_coreVectorFreeYinX(Vector, x_new,x_old);
- if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
- return _kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new);
- }
- static inline int _kit_coreVectorSubXYZ(kit_coreVector** Vector_p,
- Uint32 x_new, Uint32 x_old, Uint32 y_old,
- Uint32 x_osize, Uint32 x_nsize,
- Uint32 y_new, Uint32 y_osize, Uint32 y_nsize,
- Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
- { //printf("SubXYZ\n");
- kit_coreVector* Vector=*Vector_p; int rS; //(r)eturn(S)tatus
- _kit_coreVectorFreeZinY(Vector, x_new,x_old, 0,y_old);
- _kit_coreVectorFreeYinX(Vector, x_new,x_old);
- if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
- _kit_coreVectorFreeZinY(Vector, 0,x_new, y_new,y_old);
- if((rS=_kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new))) return rS;
- return _kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new);
- }
- static inline int _kit_coreVectorAddX(kit_coreVector** Vector_p,
- Uint32 x_osize, Uint32 x_nsize, Uint32 x_new,
- Uint32 x_old, Uint32 y_nsize, Uint32 y_new,
- Uint32 z_nsize, Uint32 z_new)
- { //printf("AddX\n");
- int rS; //(r)eturn(S)tatus
- if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
- kit_coreVector* Vector=*Vector_p;
- if((rS=_kit_coreVectorResizeY(Vector, x_old,x_new, 0,y_nsize, y_new))) return rS;
- return _kit_coreVectorResizeZ(Vector, x_old,x_new, 0,y_new, 0,z_nsize, z_new);
- }
- static inline int _kit_coreVectorAddY(kit_coreVector* Vector,
- Uint32 x_new, Uint32 y_osize, Uint32 y_nsize, Uint32 y_new,
- Uint32 y_old, Uint32 z_nsize, Uint32 z_new)
- { //printf("AddY\n");
- int rS; //(r)eturn(S)tatus
- if((rS=_kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new))) return rS;
- return _kit_coreVectorResizeZ(Vector, 0,x_new, y_old,y_new, 0,z_nsize, z_new);
- }
- static inline int _kit_coreVectorAddZ(kit_coreVector* Vector,
- Uint32 x_new, Uint32 y_new,
- Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
- { //printf("AddZ\n");
- return _kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new);
- }
- static inline int _kit_coreVectorAddXY(kit_coreVector** Vector_p,
- Uint32 x_osize, Uint32 x_nsize, Uint32 x_new,
- Uint32 x_old, Uint32 y_osize, Uint32 y_nsize, Uint32 y_new,
- Uint32 y_old, Uint32 z_nsize, Uint32 z_new)
- { //printf("AddXY\n");
- int rS; //(r)eturn(S)tatus
- if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
- kit_coreVector* Vector=*Vector_p;
- if((rS=_kit_coreVectorResizeY(Vector, 0,x_old, y_osize,y_nsize, y_new))) return rS;
- if((rS=_kit_coreVectorResizeY(Vector, x_old,x_new, 0,y_nsize, y_new))) return rS;
- if((rS=_kit_coreVectorResizeZ(Vector, 0,x_old, y_old,y_new, 0,z_nsize, z_new))) return rS;
- return _kit_coreVectorResizeZ(Vector, x_old,x_new, 0,y_new, 0,z_nsize, z_new);
- }
- static inline int _kit_coreVectorAddYZ(kit_coreVector* Vector,
- Uint32 x_new, Uint32 y_osize, Uint32 y_nsize, Uint32 y_new,
- Uint32 y_old, Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
- { //printf("AddYZ\n");
- int rS; //(r)eturn(S)tatus
- if((rS=_kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new))) return rS;
- if((rS=_kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_old, z_osize,z_nsize, z_new))) return rS;
- return _kit_coreVectorResizeZ(Vector, 0,x_new, y_old,y_new, 0,z_nsize, z_new);
- }
- static inline int _kit_coreVectorAddXZ(kit_coreVector** Vector_p,
- Uint32 x_osize, Uint32 x_nsize, Uint32 x_new,
- Uint32 x_old, Uint32 y_nsize, Uint32 y_new,
- Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
- { //printf("AddXZ\n");
- int rS; //(r)eturn(S)tatus
- if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
- kit_coreVector* Vector=*Vector_p;
- if((rS=_kit_coreVectorResizeY(Vector, x_old,x_new, 0,y_nsize, y_new))) return rS;
- if((rS=_kit_coreVectorResizeZ(Vector, 0,x_old, 0,y_new, z_osize,z_nsize, z_new))) return rS;
- return _kit_coreVectorResizeZ(Vector, x_old,x_new, 0,y_new, 0,z_nsize, z_new);
- }
- static inline int _kit_coreVectorAddXYZ(kit_coreVector** Vector_p,
- Uint32 x_osize, Uint32 x_nsize, Uint32 x_new,
- Uint32 x_old, Uint32 y_osize, Uint32 y_nsize, Uint32 y_new,
- Uint32 y_old, Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
- { //printf("AddXYZ\n");
- int rS; //(r)eturn(S)tatus
- if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
- kit_coreVector* Vector=*Vector_p;
- if((rS=_kit_coreVectorResizeY(Vector, 0,x_old, y_osize,y_nsize, y_new))) return rS;
- if((rS=_kit_coreVectorResizeY(Vector, x_old,x_new, 0,y_nsize, y_new))) return rS;
- if((rS=_kit_coreVectorResizeZ(Vector, 0,x_old, 0,y_old, z_osize,z_nsize, z_new))) return rS;
- if((rS=_kit_coreVectorResizeZ(Vector, 0,x_old, y_old,y_new, 0,z_nsize, z_new))) return rS;
- return _kit_coreVectorResizeZ(Vector, x_old,x_new, 0,y_new, 0,z_nsize, z_new);
- }
- //_<0(unchanged)/N(egative)/P(ositive)><X/Y/Z>
- #define _0X (0x0000)
- #define _NX (0x1000)
- #define _PX (0x3000)
- #define _0Y (0x0000)
- #define _NY (0x0100)
- #define _PY (0x0300)
- #define _0Z (0x0000)
- #define _NZ (0x0010)
- #define _PZ (0x0030)
- #define _VADECISION(_action) \
- if((returnStatus=_action)){ goto _error_; }
- #define _VADECISION_B(_action) _VADECISION(_action) break;
- //add to or subtract from size
- int kit_coreVectorAdd(kit_coreVector** Vector_p, Sint32 x_add, Sint32 y_add, Sint32 z_add){
- if(!x_add && !y_add && !z_add) return 0;
- int returnStatus=0;
- _IF_SDLERR_I(Vector_p==NULL,-1,;,"Vector_p cannot be NULL")
- kit_coreVector* Vector=*Vector_p;
- _IF_SDLERR_I(Vector==NULL,-2,;,"*Vector_p cannot be NULL")
- Uint32 unit=Vector->unit;
- Uint32 x_old=Vector->x, y_old=Vector->y, z_old=Vector->z;
- _IF_SDLERR_I( (!z_old)&&(z_add!=0) ,-3,;,"cannot change z on non-3d vector")
- else _IF_SDLERR_I( (!y_old)&&(y_add!=0) ,-4,;,"cannot change y on 1d vector")
- Uint32 x_new=x_old+x_add, y_new=y_old+y_add, z_new=z_old+z_add;
- _IF_SDLERR_I( (!z_old)!=(!z_new) ,-5,;,"resulting z cannot have its axis status flipped")
- else _IF_SDLERR_I( (!y_old)!=(!y_new) ,-6,;,"resulting y cannot have its axis status flipped")
- //check for any underflows and overflows respectively (and some dimension error checks)
- //x
- _IF_SDLERR_I( ((x_add<0)&&(x_new>x_old)) || !x_new ,-7,;,"resulting x cannot be 0")
- _IF_SDLERR_I( (x_add>0)&&(x_new<x_old) ,-8,;,"applying x_add caused an overflow")
- //y
- if(y_add<0 && (y_new>y_old)) y_new=0;
- else _IF_SDLERR_I( (y_add>0)&&(y_new<y_old) ,-9,;,"applying y_add caused an overflow")
- else _IF_SDLERR_I( (y_new==0)&&(z_new>0) ,-10,;,"resulting y cannot be 0 if z>0")
- //z
- if((z_add<0) && (z_new>z_old)) z_new=0;
- else _IF_SDLERR_I( (z_add>0)&&(z_new<z_old) ,-11,;,"applying z_add caused an overflow")
- Uint32 x_unit, y_unit, z_unit;
- x_unit=y_unit=z_unit = sizeof(void*);
- int decision, dims=(x_new!=0)+(y_new!=0)+(z_new!=0); //dim(ension)s
- if(dims==1) x_unit=unit;
- else if(dims==2) y_unit=unit;
- else /*(dims==3)*/ z_unit=unit;
- Uint32 x_osize=x_unit*x_old, y_osize=y_unit*y_old, z_osize=z_unit*z_old;
- Uint32 x_nsize=x_unit*x_new, y_nsize=y_unit*y_new, z_nsize=z_unit*z_new;
- decision =( ((x_add>0)<<1)|(x_add!=0) )<<12; //0x0000 -> 0x3000
- decision|=( ((y_add>0)<<1)|(y_add!=0) )<< 8; //0x0000 -> 0x0300
- decision|=( ((z_add>0)<<1)|(z_add!=0) )<< 4; //0x0000 -> 0x0030
- switch(decision){
- //SUB
- case (_NX|_0Y|_0Z): //_NX_0Y_0Z; technically a superset of: (n/a)
- _VADECISION_B(_kit_coreVectorSubX(&Vector,x_new,x_old,y_new,x_osize,x_nsize))
- case (_0X|_NY|_0Z): //_0X_NY_0Z; technically a superset of: (n/a)
- _VADECISION_B(_kit_coreVectorSubY(Vector,x_new,y_new,y_old,y_osize,y_nsize))
- case (_0X|_0Y|_NZ): //_0X_0Y_NZ; technically a superset of: (n/a)
- _VADECISION_B(_kit_coreVectorSubZ(Vector,x_new,y_new,z_osize,z_nsize,z_new))
- case (_NX|_NY|_0Z): //_NX_NY_0Z; technically a superset of: (_NX_0Y_0Z,_0X_NY_0Z)
- _VADECISION_B(_kit_coreVectorSubXY(&Vector,x_new,x_old,y_old,x_osize,x_nsize,y_new,y_osize,y_nsize))
- case (_0X|_NY|_NZ): //_0X_NY_NZ; technically a superset of: (_0X_NY_0Z,_0X_0Y_NZ)
- _VADECISION_B(_kit_coreVectorSubYZ(Vector,x_new,y_new,y_old,y_osize,y_nsize,z_osize,z_nsize,z_new))
- case (_NX|_0Y|_NZ): //_NX_0Y_NZ; technically a superset of: (_NX_0Y_0Z,_0X_0Y_NZ)
- _VADECISION_B(_kit_coreVectorSubXZ(&Vector,x_new,x_old,y_new,x_osize,x_nsize,z_osize,z_nsize,z_new))
- case (_NX|_NY|_NZ): //_NX_NY_NZ; technically a superset of: (_NX_NY_0Z,_0X_0Y_NZ)
- _VADECISION_B(_kit_coreVectorSubXYZ(&Vector,x_new,x_old,y_old,x_osize,x_nsize,y_new,y_osize,y_nsize,z_osize,z_nsize,z_new))
- //ADD
- case (_PX|_0Y|_0Z): //_PX_0Y_0Z; technically a superset of: (n/a)
- _VADECISION_B(_kit_coreVectorAddX(&Vector,x_osize,x_nsize,x_new,x_old,y_nsize,y_new,z_nsize,z_new))
- case (_0X|_PY|_0Z): //_0X_PY_0Z; technically a superset of: (n/a)
- _VADECISION_B(_kit_coreVectorAddY(Vector,x_new,y_osize,y_nsize,y_new,y_old,z_nsize,z_new))
- case (_0X|_0Y|_PZ): //_0X_0Y_PZ; technically a superset of: (n/a)
- _VADECISION_B(_kit_coreVectorAddZ(Vector,x_new,y_new,z_osize,z_nsize,z_new))
- case (_PX|_PY|_0Z): //_PX_PY_0Z; technically a superset of: (_PX_0Y_0Z,_0X_PY_0Z)
- _VADECISION_B(_kit_coreVectorAddXY(&Vector,x_osize,x_nsize,x_new,x_old,y_osize,y_nsize,y_new,y_old,z_nsize,z_new))
- case (_0X|_PY|_PZ): //_0X_PY_PZ; technically a superset of: (_0X_PY_0Z,_0X_0Y_PZ)
- _VADECISION_B(_kit_coreVectorAddYZ(Vector,x_new,y_osize,y_nsize,y_new,y_old,z_osize,z_nsize,z_new))
- case (_PX|_0Y|_PZ): //_PX_0Y_PZ; technically a superset of: (_PX_0Y_0Z,_0X_0Y_PZ)
- _VADECISION_B(_kit_coreVectorAddXZ(&Vector,x_osize,x_nsize,x_new,x_old,y_nsize,y_new,z_osize,z_nsize,z_new))
- case (_PX|_PY|_PZ): //_PX_PY_PZ; technically a superset of: (_PX_PY_0Z,_0X_0Y_PZ)
- _VADECISION_B(_kit_coreVectorAddXYZ(&Vector,x_osize,x_nsize,x_new,x_old,y_osize,y_nsize,y_new,y_old,z_osize,z_nsize,z_new))
- //SUBX ADDYZ
- case (_NX|_PY|_0Z): //_NX_PY_0Z; technically a superset of: (_NX_0Y_0Z, _0X_PY_0Z)
- _VADECISION( _kit_coreVectorSubX(&Vector,x_new,x_old,y_new,x_osize,x_nsize))
- _VADECISION_B(_kit_coreVectorAddY(Vector,x_new,y_osize,y_nsize,y_new,y_old,z_nsize,z_new))
- case (_NX|_0Y|_PZ): //_NX_0Y_PZ; technically a superset of: (_NX_0Y_0Z, _0X_0Y_PZ)
- _VADECISION( _kit_coreVectorSubX(&Vector,x_new,x_old,y_new,x_osize,x_nsize))
- _VADECISION_B(_kit_coreVectorAddZ(Vector,x_new,y_new,z_osize,z_nsize,z_new))
- case (_NX|_PY|_PZ): //_NX_PY_PZ; technically a superset of: (_NX_0Y_0Z, _0X_PY_PZ)
- _VADECISION( _kit_coreVectorSubX(&Vector,x_new,x_old,y_new,x_osize,x_nsize))
- _VADECISION_B(_kit_coreVectorAddYZ(Vector,x_new,y_osize,y_nsize,y_new,y_old,z_osize,z_nsize,z_new))
- //SUBY ADDXZ
- case (_PX|_NY|_0Z): //_PX_NY_0Z; technically a superset of: (_0X_NY_0Z, _PX_0Y_0Z)
- //x_new is swapped for x_old here (see line 5)
- _VADECISION( _kit_coreVectorSubY(Vector,x_old,y_new,y_old,y_osize,y_nsize))
- _VADECISION_B(_kit_coreVectorAddX(&Vector,x_osize,x_nsize,x_new,x_old,y_nsize,y_new,z_nsize,z_new))
- case (_0X|_NY|_PZ): //_0X_NY_PZ; technically a superset of: (_0X_NY_0Z, _0X_0Y_PZ)
- _VADECISION( _kit_coreVectorSubY(Vector,x_new,y_new,y_old,y_osize,y_nsize))
- _VADECISION_B(_kit_coreVectorAddZ(Vector,x_new,y_new,z_osize,z_nsize,z_new))
- case (_PX|_NY|_PZ): //_PX_NY_PZ; technically a superset of: (_0X_NY_0Z, _PX_0Y_PZ)
- //x_new is swapped for x_old here (see line 5)
- _VADECISION( _kit_coreVectorSubY(Vector,x_old,y_new,y_old,y_osize,y_nsize))
- _VADECISION_B(_kit_coreVectorAddXZ(&Vector,x_osize,x_nsize,x_new,x_old,y_nsize,y_new,z_osize,z_nsize,z_new))
- //SUBZ ADDXY
- case (_PX|_0Y|_NZ): //_PX_0Y_NZ; technically a superset of: (_0X_0Y_NZ, _PX_0Y_0Z)
- //x_new is swapped for x_old here (see line 5)
- _VADECISION( _kit_coreVectorSubZ(Vector,x_old,y_new,z_osize,z_nsize,z_new))
- _VADECISION_B(_kit_coreVectorAddX(&Vector,x_osize,x_nsize,x_new,x_old,y_nsize,y_new,z_nsize,z_new))
- case (_0X|_PY|_NZ): //_0X_PY_NZ; technically a superset of: (_0X_0Y_NZ, _0X_PY_0Z)
- //y_new is swapped for y_old here (see line 5)
- _VADECISION( _kit_coreVectorSubZ(Vector,x_new,y_old,z_osize,z_nsize,z_new))
- _VADECISION_B(_kit_coreVectorAddY(Vector,x_new,y_osize,y_nsize,y_new,y_old,z_nsize,z_new))
- case (_PX|_PY|_NZ): //_PX_PY_NZ; technically a superset of: (_0X_0Y_NZ, _PX_PY_0Z)
- //x_new,y_new is swapped for x_old,y_old here respectively (see line 5)
- _VADECISION( _kit_coreVectorSubZ(Vector,x_old,y_old,z_osize,z_nsize,z_new))
- _VADECISION_B(_kit_coreVectorAddXY(&Vector,x_osize,x_nsize,x_new,x_old,y_osize,y_nsize,y_new,y_old,z_nsize,z_new))
- //SUBXYZ ADDXYZ
- case (_NX|_NY|_PZ): //_NX_NY_PZ; technically a superset of: (_NX_NY_0Z, _0X_0Y_PZ)
- _VADECISION( _kit_coreVectorSubXY(&Vector,x_new,x_old,y_old,x_osize,x_nsize,y_new,y_osize,y_nsize))
- _VADECISION_B(_kit_coreVectorAddZ(Vector,x_new,y_new,z_osize,z_nsize,z_new))
- case (_PX|_NY|_NZ): //_PX_NY_NZ; technically a superset of: (_0X_NY_NZ, _PX_0Y_0Z)
- //x_new is swapped for x_old here (see line 5)
- _VADECISION( _kit_coreVectorSubYZ(Vector,x_old,y_new,y_old,y_osize,y_nsize,z_osize,z_nsize,z_new))
- _VADECISION_B(_kit_coreVectorAddX(&Vector,x_osize,x_nsize,x_new,x_old,y_nsize,y_new,z_nsize,z_new))
- case (_NX|_PY|_NZ): //_NX_PY_NZ; technically a superset of: (_NX_0Y_NZ, _0X_PY_0Z)
- //y_new is swapped for y_old here (see line 5)
- _VADECISION( _kit_coreVectorSubXZ(&Vector,x_new,x_old,y_old,x_osize,x_nsize,z_osize,z_nsize,z_new))
- _VADECISION_B(_kit_coreVectorAddY(Vector,x_new,y_osize,y_nsize,y_new,y_old,z_nsize,z_new))
- default: _IS_SDLERR_I(-15,;,"decision switch defaulted (this shouldn't be possible); decision=0x%X",decision)
- } *Vector_p=Vector;
- _error_:
- return returnStatus;
- }
- //set new size (if an axis == 0, then leave that axis unchanged)
- //also, if the new axis length is the same as the old one, leave it unchanged also
- int kit_coreVectorSet(kit_coreVector** Vector_p, Uint32 x_new, Uint32 y_new, Uint32 z_new){
- int returnStatus=0;
- _IF_SDLERR_I(Vector_p==NULL,-1,;,"Vector_p cannot be NULL")
- kit_coreVector* Vector=*Vector_p;
- _IF_SDLERR_I(Vector==NULL,-2,;,"*Vector_p cannot be NULL")
- Sint32 x_diff=(x_new==0) ? 0 : x_new-Vector->x;
- Sint32 y_diff=(y_new==0) ? 0 : y_new-Vector->y;
- Sint32 z_diff=(z_new==0) ? 0 : z_new-Vector->z;
- returnStatus=kit_coreVectorAdd(Vector_p, x_diff,y_diff,z_diff);
- _error_:
- return returnStatus;
- }
- int kit_coreVectorDestroy(kit_coreVector** Vector_p){
- int returnStatus=0;
- _IF_SDLERR_I(Vector_p==NULL,-1,;,"Vector_p cannot be NULL")
- kit_coreVector* Vector=*Vector_p;
- _IF_SDLERR_I(Vector==NULL,-2,;,"*Vector_p cannot be NULL")
- int x=Vector->x, y=Vector->y; //z=Vector->z;
- Sint32 condition=Vector->_dims&3;
- if(!condition) Vector->_dims=condition = (x!=0)+(y!=0)+(Vector->z!=0);
- //condition must be equal to the number of dimensions (1=1d,2=2d,3=3d)
- switch(condition){
- case 3: _kit_coreVectorFreeZinY(Vector, 0,x, 0,y); SDL_FALLTHROUGH;
- case 2: _kit_coreVectorFreeYinX(Vector, 0,x); SDL_FALLTHROUGH;
- case 1: SDL_free(Vector); //frees Vector->p1d too
- } *Vector_p=NULL;
- _error_:
- return returnStatus;
- }
- kit_coreVector* kit_coreVectorCreate(Uint32 x, Uint32 y, Uint32 z,
- Uint32 unit, Uint32 type_n,
- int* returnStatus_p)
- {
- kit_coreVector* Vector=NULL;
- int returnStatus=0;
- _IF_SDLERR_I(x==0,-1,;,"variable x cannot be 0")
- //x axis
- size_t x_size=(!y && !z) ? unit*x : sizeof(void*)*x;
- returnStatus=_kit_coreVectorRealloc(&Vector,0, 0,sizeof(kit_coreVector)+x_size);
- _IF_SDLERR_I(returnStatus,-2,;,"SDL_malloc() for x axis failed")
- Vector->p1d=(void*)Vector + sizeof(kit_coreVector); //array is ahead by 32B
- Vector->_dims=0x80000000; //sign bit will be unset on success
- ++Vector->_dims; //vector now contains an x axis
- //y axis
- _IF_SDLERR_I(y==0 && z>0,-3,;,"variable y cannot be 0 if z>0")
- if(y>0){ ++Vector->_dims; //vector now contains a y axis
- size_t y_size=(!z) ? unit*y : sizeof(void*)*y;
- returnStatus=_kit_coreVectorResizeY(Vector, 0,x, 0,y_size, y);
- _IF_SDLERR_I(returnStatus,-4,;,"SDL_malloc() for y axis failed")
- }
- //z axis
- if(z>0){ ++Vector->_dims; //vector now contains a z axis
- returnStatus=_kit_coreVectorResizeZ(Vector, 0,x, 0,y, 0,unit*z, z);
- _IF_SDLERR_I(returnStatus,-5,;,"SDL_malloc() for z axis failed")
- }
- Vector->_dims&=0x7fffffff; //unset sign bit to indicate success
- Vector->type.n=type_n;
- Vector->x=x, Vector->y=y, Vector->z=z;
- Vector->unit=unit;
- _error_:
- if((Vector!=NULL) && (Vector->_dims<0))
- returnStatus+=kit_coreVectorDestroy(&Vector); //-=0 -> 2
- if(returnStatus_p!=NULL) *returnStatus_p=returnStatus;
- return Vector;
- }
- /* notes
- (in practice, redundant operations are supposed to be trimmed)
- sub z:
- if !3d: error "cannot change z on non-3d vector"
- -realloc z
- sub y:
- if 1d: error "cannot change y on 1d vector"
- if 3d: free z in old y
- -realloc y
- sub x:
- if 3d: free z in y in old x
- if >=2d: free y in old x
- -realloc x
- add x:
- +realloc x
- if >=2d: malloc y in new x
- if 3d: malloc z in new y in new x
- add y:
- if 1d: error "cannot change y on 1d vector"
- +realloc y
- if 3d: malloc z in new y
- add z:
- if !3d: error "cannot change z on non-3d vector"
- +realloc z
- (priority is sub x,y,z then add x,y,z)
- _0x_0y_0z: (RETURN W/O ACTION)
- SUB
- _Nx_0y_0z: (atomic)
- sub x
- _0x_Ny_0z: (atomic)
- sub y
- _0x_0y_Nz: (atomic)
- sub z
- _Nx_Ny_0z: (_Nx_0y_0z,_0x_Ny_0z)
- sub x
- sub y
- _0x_Ny_Nz: (_0x_Ny_0z,_0x_0y_Nz)
- sub y
- sub z
- _Nx_0y_Nz: (_Nx_0y_0z,_0x_0y_Nz)
- sub x
- sub z
- _Nx_Ny_Nz: (_Nx_Ny_0z,_0x_0y_Nz)
- sub x
- sub y
- sub z
- ADD
- _Px_0y_0z: (atomic)
- add x
- _0x_Py_0z: (atomic)
- add y
- _0x_0y_Pz: (atomic)
- add z
- _Px_Py_0z: (_Px_0y_0z,_0x_Py_0z)
- add x
- add y
- _0x_Py_Pz: (_0x_Py_0z,_0x_0y_Pz)
- add y
- add z
- _Px_0y_Pz: (_Px_0y_0z,_0x_0y_Pz)
- add x
- add z
- _Px_Py_Pz: (_Px_Py_0z,_0x_0y_Pz)
- add x
- add y
- add z
- SUBX ADDYZ
- _Nx_Py_0z: (_Nx_0y_0z, _0x_Py_0z)
- sub x
- add y
- _Nx_0y_Pz: (_Nx_0y_0z, _0x_0y_Pz)
- sub x
- add z
- _Nx_Py_Pz: (_Nx_0y_0z, _0x_Py_Pz)
- sub x
- add y
- add z
- SUBY ADDXZ
- _Px_Ny_0z: (_0x_Ny_0z, _Px_0y_0z)
- sub y
- add x
- _0x_Ny_Pz: (_0x_Ny_0z, _0x_0y_Pz)
- sub y
- add z
- _Px_Ny_Pz: (_0x_Ny_0z, _Px_0y_Pz)
- sub y
- add x
- add z
- SUBZ ADDXY
- _Px_0y_Nz: (_0x_0y_Nz, _Px_0y_0z)
- sub z
- add x
- _0x_Py_Nz: (_0x_0y_Nz, _0x_Py_0z)
- sub z
- add y
- _Px_Py_Nz: (_0x_0y_Nz, _Px_Py_0z)
- sub z
- add x
- add y
- SUBXYZ ADDXYZ
- _Nx_Ny_Pz: (_Nx_Ny_0z, _0x_0y_Pz)
- sub x
- sub y
- add z
- _Px_Ny_Nz: (_0x_Ny_Nz, _Px_0y_0z)
- sub y
- sub z
- add x
- _Nx_Py_Nz: (_Nx_0y_Nz, _0x_Py_0z)
- sub x
- sub z
- add y
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement