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"
- //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;
- }
- Uint32 size_diff=nsize-osize;
- if(size_diff>0) SDL_memset(_ptr+osize,0,size_diff);
- *(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) 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<2) return; //y doesn't exist in 1d array
- 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)
- {
- kit_coreVector* Vector=*Vector_p;
- if(Vector->x==x_new) return 0;
- //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;
- Vector->ptr=(void*)Vector;
- Vector->x=x_new;
- 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<2) return 0; //y doesn't exist in 1d array
- if(Vector->y==y_new) return 0;
- 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) return 0; //z doesn't exist in 1d or 2d array
- if(Vector->z==z_new) return 0;
- 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_old,
- Uint32 x_osize, Uint32 x_nsize)
- {
- kit_coreVector* Vector=*Vector_p;
- _kit_coreVectorFreeZinY(Vector, x_new,x_old, 0,y_old);
- _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_old, Uint32 y_new,
- Uint32 y_osize, Uint32 y_nsize)
- {
- _kit_coreVectorFreeZinY(Vector, 0,x_new, y_old,y_new);
- 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)
- {
- 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)
- {
- kit_coreVector* Vector=*Vector_p; int rS=0; //(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_old, Uint32 y_new,
- Uint32 y_osize, Uint32 y_nsize,
- Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
- {
- int rS=0; //(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)
- {
- kit_coreVector* Vector=*Vector_p; int rS=0; //(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)
- {
- kit_coreVector* Vector=*Vector_p; int rS=0; //(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)
- {
- kit_coreVector* Vector=*Vector_p; int rS=0; //(r)eturn(S)tatus
- if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
- 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_nsize, 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)
- {
- int rS=0; //(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)
- {
- 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_osize, Uint32 z_nsize, Uint32 z_new)
- {
- kit_coreVector* Vector=*Vector_p; int rS=0; //(r)eturn(S)tatus
- if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
- 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;
- return _kit_coreVectorResizeZ(Vector, 0,x_new, y_old,y_new, z_osize,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)
- {
- int rS=0; //(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)
- {
- kit_coreVector* Vector=*Vector_p; int rS=0; //(r)eturn(S)tatus
- if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
- if((rS=_kit_coreVectorResizeY(Vector, x_old,x_new, 0,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_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)
- {
- kit_coreVector* Vector=*Vector_p; int rS=0; //(r)eturn(S)tatus
- if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
- if((rS=_kit_coreVectorResizeY(Vector, 0,x_old, y_osize,y_osize, y_new))) return rS;
- if((rS=_kit_coreVectorResizeY(Vector, x_old,x_new, 0,y_osize, 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);
- }
- #define _0X (0x000)
- #define _NX (0x100)
- #define _PX (0x300)
- #define _0Y (0x000)
- #define _NY (0x010)
- #define _PY (0x030)
- #define _0Z (0x000)
- #define _NZ (0x001)
- #define _PZ (0x003)
- #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_add) ,-5,;,"resulting z cannot have its axis status flipped")
- else _IF_SDLERR_I( (!y_old)!=(!y_add) ,-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) ,-7,;,"resulting x cannot be 0")
- else _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 z_unit=unit; //(dims==3)
- 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_new>0)<<1)|(x_new!=0) )<<12; //0x0000 -> 0x3000
- decision|=( ((y_new>0)<<1)|(y_new!=0) )<< 8; //0x0000 -> 0x0300
- decision|=( ((z_new>0)<<1)|(z_new!=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_old,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_old,y_new,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_old,y_new,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_osize,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_old,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_old,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_old,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)
- _VADECISION( _kit_coreVectorSubY( Vector,x_new,y_old,y_new,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_old,y_new,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)
- _VADECISION( _kit_coreVectorSubY( Vector,x_new,y_old,y_new,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)
- _VADECISION( _kit_coreVectorSubZ( Vector,x_new,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)
- _VADECISION( _kit_coreVectorSubZ( Vector,x_new,y_new,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)
- _VADECISION( _kit_coreVectorSubZ( Vector,x_new,y_new,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_osize,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)
- _VADECISION( _kit_coreVectorSubYZ( Vector,x_new,y_old,y_new,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)
- _VADECISION( _kit_coreVectorSubXZ( &Vector,x_new,x_old,y_new,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)
- 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")
- Uint32 x_old =Vector->x, y_old =Vector->y, z_old=Vector->z;
- Sint32 x_diff=(x_new==0) ? 0 : x_new-x_old;
- Sint32 y_diff=(y_new==0) ? 0 : y_new-y_old;
- Sint32 z_diff=(z_new==0) ? 0 : z_new-z_old;
- 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")
- void *p1d, **p2d, ***p3d;
- int x=Vector->x, y=Vector->y; //z=Vector->z;
- Sint32 condition=Vector->_dims&3;
- if(!condition) 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: p3d=Vector->p3d;
- for(Uint32 xi=0; xi<x; ++xi){
- if((p2d=p3d[xi])==NULL) break;
- for(Uint32 yi=0; yi<y; ++yi){
- if((p1d=p2d[yi])==NULL) break;
- else SDL_free(p1d);
- }
- }
- case 2: p2d=Vector->p2d;
- for(Uint32 xi=0; xi<x; ++xi){
- if((p1d=p2d[xi])==NULL) break;
- else SDL_free(p1d);
- }
- case 1: SDL_free(Vector); //frees Vector->p1d too
- default: *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;
- size_t x_size, y_size, z_size;
- void /* *p1d, */ **p2d, ***p3d;
- int returnStatus=0, dimensions=0x80000000;
- _IF_SDLERR_I(x==0,-1,;,"variable x cannot be 0")
- //x axis
- 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
- ++dimensions; //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){ ++dimensions; //vector now contains a y axis
- y_size=(!z) ? unit*y : sizeof(void*)*y;
- p2d=Vector->p2d;
- for(Uint32 xi=0; xi<x; ++xi){
- returnStatus=_kit_coreVectorRealloc(&p2d[xi],0, 0,y_size);
- _IF_SDLERR_I(returnStatus,-4,;,"SDL_malloc() for y axis failed")
- }
- }
- //z axis
- if(z>0){ ++dimensions; //vector now contains a z axis
- z_size=unit*z; p3d=Vector->p3d;
- for(Uint32 xi=0; xi<x; ++xi){ p2d=p3d[xi];
- for(Uint32 yi=0; yi<y; ++yi){
- returnStatus=_kit_coreVectorRealloc(&p2d[yi],0, 0,z_size);
- _IF_SDLERR_I(returnStatus,-5,;,"SDL_malloc() for z axis failed")
- }
- }
- }
- dimensions&=0x7fffffff; //unset sign bit
- Vector->type.n=type_n;
- Vector->x=x, Vector->y=y, Vector->z=z;
- Vector->unit=unit;
- _error_:
- if(Vector!=NULL){
- Vector->_dims=dimensions&3;
- if(dimensions<0) returnStatus+=kit_coreVectorDestroy(&Vector); //-=0 -> 2
- }
- if(returnStatus_p!=NULL) *returnStatus_p=returnStatus;
- return Vector;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement