Advertisement
Kitomas

kit_sdl2_coreVector.c (WIP; to be fully tested)

Aug 11th, 2023
938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 23.01 KB | Source Code | 0 0
  1. //my own primitive form of contiguous dynamic arrays
  2. #include "../include/kit_sdl2/kit_core.h"
  3. #include "../_private/include/_kit_privmacro.h"
  4.  
  5.  
  6. //initializes any added memory if difference is positive
  7. int _kit_coreVectorRealloc(void* ptr_p, char axis,
  8.                            Uint32 osize, Uint32 nsize)
  9. {
  10.   if(osize==nsize) return 0;
  11.   void* _ptr=SDL_realloc(*(void**)ptr_p,nsize);
  12.   if(_ptr==NULL){
  13.     if(axis!=0) SDL_SetError("SDL_realloc() of %c axis failed",axis);
  14.     return -1;
  15.   }
  16.   Uint32 size_diff=nsize-osize;
  17.   if(size_diff>0) SDL_memset(_ptr+osize,0,size_diff);
  18.   *(void**)ptr_p=_ptr;
  19.   return 0;
  20. }
  21.  
  22.  
  23. void _kit_coreVectorFreeZinY(kit_coreVector* Vector,
  24.                              Uint32 x_start, Uint32 x_end,
  25.                              Uint32 y_start, Uint32 y_end)
  26. {
  27.   if(Vector->_dims<3) return; //z doesn't exist in 1d or 2d array
  28.   void*** p3d=Vector->p3d;
  29.   for(Uint32 xi=x_start; xi<x_end; ++xi){ void** p2d=p3d[xi];
  30.     if(p2d==NULL) break;
  31.     for(Uint32 yi=y_start; yi<y_end; ++yi){ void* p1d=p2d[yi];
  32.       if(p1d==NULL) break;
  33.       SDL_free(p1d); p2d[yi]=NULL;
  34.     }
  35.   }
  36. }
  37.  
  38. void _kit_coreVectorFreeYinX(kit_coreVector* Vector,
  39.                              Uint32 x_start, Uint32 x_end)
  40. {
  41.   if(Vector->_dims<2) return; //y doesn't exist in 1d array
  42.   void** p2d=Vector->p2d;
  43.   for(Uint32 xi=x_start; xi<x_end; ++xi){ void* p1d=p2d[xi];
  44.     if(p1d==NULL) break;
  45.     SDL_free(p1d); p2d[xi]=NULL;
  46.   }
  47. }
  48.  
  49.  
  50. int _kit_coreVectorResizeX(kit_coreVector** Vector_p,
  51.                            Uint32 x_osize, Uint32 x_nsize, Uint32 x_new)
  52. {
  53.   kit_coreVector* Vector=*Vector_p;
  54.   if(Vector->x==x_new) return 0;
  55.   //x axis is contiguous with the vector struct itself
  56.   x_osize+=sizeof(kit_coreVector); x_nsize+=sizeof(kit_coreVector);
  57.   if(_kit_coreVectorRealloc(Vector_p,'x', x_osize,x_nsize)) return -12;
  58.   Vector->ptr=(void*)Vector;
  59.   Vector->x=x_new;
  60.   return 0;
  61. }
  62.  
  63. int _kit_coreVectorResizeY(kit_coreVector* Vector,
  64.                            Uint32 x_start, Uint32 x_end,
  65.                            Uint32 y_osize, Uint32 y_nsize, Uint32 y_new)
  66. {
  67.   if(Vector->_dims<2) return 0; //y doesn't exist in 1d array
  68.   if(Vector->y==y_new) return 0;
  69.   void** p2d=Vector->p2d;
  70.   for(Uint32 xi=x_start; xi<x_end; ++xi)
  71.     if(_kit_coreVectorRealloc(&p2d[xi],'y', y_osize,y_nsize)) return -13;
  72.   Vector->y=y_new;
  73.   return 0;
  74. }
  75.  
  76. int _kit_coreVectorResizeZ(kit_coreVector* Vector,
  77.                            Uint32 x_start, Uint32 x_end,
  78.                            Uint32 y_start, Uint32 y_end,
  79.                            Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
  80. {
  81.   if(Vector->_dims<3) return 0; //z doesn't exist in 1d or 2d array
  82.   if(Vector->z==z_new) return 0;
  83.   void*** p3d=Vector->p3d;
  84.   for(Uint32 xi=x_start; xi<x_end; ++xi){ void** p2d=p3d[xi];
  85.     for(Uint32 yi=y_start; yi<y_end; ++yi)
  86.       if(_kit_coreVectorRealloc(&p2d[yi],'z', z_osize,z_nsize)) return -14;
  87.   }
  88.   Vector->z=z_new;
  89.   return 0;
  90. }
  91.  
  92. //uninitialize rS where it isn't needed
  93. //un-inline these if binary size becomes unacceptable!
  94. //(remember, <xyz>_old and <xyz>_new are sometimes flipped when used as a parameter when freeing)
  95. static inline int _kit_coreVectorSubX(kit_coreVector** Vector_p,
  96.                                       Uint32 x_new,   Uint32 x_old, Uint32 y_old,
  97.                                       Uint32 x_osize, Uint32 x_nsize)
  98. {
  99.   kit_coreVector* Vector=*Vector_p;
  100.   _kit_coreVectorFreeZinY(Vector, x_new,x_old, 0,y_old);
  101.   _kit_coreVectorFreeYinX(Vector, x_new,x_old);
  102.   return _kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new);
  103. }
  104.  
  105. static inline int _kit_coreVectorSubY(kit_coreVector* Vector,
  106.                                       Uint32 x_new,   Uint32 y_old, Uint32 y_new,
  107.                                       Uint32 y_osize, Uint32 y_nsize)
  108. {
  109.   _kit_coreVectorFreeZinY(Vector, 0,x_new, y_old,y_new);
  110.   return _kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new);
  111. }
  112.  
  113. static inline int _kit_coreVectorSubZ(kit_coreVector* Vector,
  114.                                       Uint32 x_new,   Uint32 y_new, Uint32 z_osize,
  115.                                       Uint32 z_nsize, Uint32 z_new)
  116. {
  117.   return _kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new);
  118. }
  119.  
  120. static inline int _kit_coreVectorSubXY(kit_coreVector** Vector_p,
  121.                                        Uint32 x_new,   Uint32 x_old, Uint32 y_old,
  122.                                        Uint32 x_osize, Uint32 x_nsize,
  123.                                        Uint32 y_new,   Uint32 y_osize, Uint32 y_nsize)
  124. {
  125.   kit_coreVector* Vector=*Vector_p; int rS=0; //(r)eturn(S)tatus
  126.   _kit_coreVectorFreeZinY(Vector, x_new,x_old, 0,y_old);
  127.   _kit_coreVectorFreeYinX(Vector, x_new,x_old);
  128.   if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
  129.   _kit_coreVectorFreeZinY(Vector, 0,x_new, y_new,y_old);
  130.   return _kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new);
  131. }
  132.  
  133. static inline int _kit_coreVectorSubYZ(kit_coreVector* Vector,
  134.                                        Uint32 x_new,   Uint32 y_old,   Uint32 y_new,
  135.                                        Uint32 y_osize, Uint32 y_nsize,
  136.                                        Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
  137. {
  138.   int rS=0; //(r)eturn(S)tatus
  139.   _kit_coreVectorFreeZinY(Vector, 0,x_new, y_new,y_old);
  140.   if((rS=_kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new))) return rS;
  141.   return _kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new);
  142. }
  143.  
  144. static inline int _kit_coreVectorSubXZ(kit_coreVector** Vector_p,
  145.                                        Uint32 x_new,   Uint32 x_old,   Uint32 y_new,
  146.                                        Uint32 x_osize, Uint32 x_nsize,
  147.                                        Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
  148. {
  149.   kit_coreVector* Vector=*Vector_p; int rS=0; //(r)eturn(S)tatus
  150.   _kit_coreVectorFreeZinY(Vector, x_new,x_old, 0,y_new);
  151.   _kit_coreVectorFreeYinX(Vector, x_new,x_old);
  152.   if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
  153.   return _kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new);
  154. }
  155.  
  156. static inline int _kit_coreVectorSubXYZ(kit_coreVector** Vector_p,
  157.                                         Uint32 x_new,   Uint32 x_old,   Uint32 y_old,
  158.                                         Uint32 x_osize, Uint32 x_nsize,
  159.                                         Uint32 y_new,   Uint32 y_osize, Uint32 y_nsize,
  160.                                         Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
  161. {
  162.   kit_coreVector* Vector=*Vector_p; int rS=0; //(r)eturn(S)tatus
  163.   _kit_coreVectorFreeZinY(Vector, x_new,x_old, 0,y_old);
  164.   _kit_coreVectorFreeYinX(Vector, x_new,x_old);
  165.   if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
  166.   _kit_coreVectorFreeZinY(Vector, 0,x_new, y_new,y_old);
  167.   if((rS=_kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new))) return rS;
  168.   return _kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new);
  169. }
  170.  
  171.  
  172. static inline int _kit_coreVectorAddX(kit_coreVector** Vector_p,
  173.                                       Uint32 x_osize, Uint32 x_nsize, Uint32 x_new,
  174.                                       Uint32 x_old,   Uint32 y_nsize, Uint32 y_new,
  175.                                       Uint32 z_nsize, Uint32 z_new)
  176. {
  177.   kit_coreVector* Vector=*Vector_p; int rS=0; //(r)eturn(S)tatus
  178.   if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
  179.   if((rS=_kit_coreVectorResizeY(Vector, x_old,x_new, 0,y_nsize, y_new))) return rS;
  180.   return _kit_coreVectorResizeZ(Vector, x_old,x_new, 0,y_nsize, 0,z_nsize, z_new);
  181. }
  182.  
  183. static inline int _kit_coreVectorAddY(kit_coreVector* Vector,
  184.                                       Uint32 x_new, Uint32 y_osize, Uint32 y_nsize, Uint32 y_new,
  185.                                       Uint32 y_old, Uint32 z_nsize, Uint32 z_new)
  186. {
  187.   int rS=0; //(r)eturn(S)tatus
  188.   if((rS=_kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new))) return rS;
  189.   return _kit_coreVectorResizeZ(Vector, 0,x_new, y_old,y_new, 0,z_nsize, z_new);
  190. }
  191.  
  192. static inline int _kit_coreVectorAddZ(kit_coreVector* Vector,
  193.                                       Uint32 x_new,   Uint32 y_new,
  194.                                       Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
  195. {
  196.   return _kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new);
  197. }
  198.  
  199. static inline int _kit_coreVectorAddXY(kit_coreVector** Vector_p,
  200.                                        Uint32 x_osize, Uint32 x_nsize, Uint32 x_new,
  201.                                        Uint32 x_old,   Uint32 y_osize, Uint32 y_nsize, Uint32 y_new,
  202.                                        Uint32 y_old,   Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
  203. {
  204.   kit_coreVector* Vector=*Vector_p; int rS=0; //(r)eturn(S)tatus
  205.   if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
  206.   if((rS=_kit_coreVectorResizeY(Vector, 0,x_old, y_osize,y_nsize, y_new))) return rS;
  207.   if((rS=_kit_coreVectorResizeY(Vector, x_old,x_new, 0,y_nsize, y_new))) return rS;
  208.   return _kit_coreVectorResizeZ(Vector, 0,x_new, y_old,y_new, z_osize,z_nsize, z_new);
  209. }
  210.  
  211. static inline int _kit_coreVectorAddYZ(kit_coreVector* Vector,
  212.                                        Uint32 x_new, Uint32 y_osize, Uint32 y_nsize, Uint32 y_new,
  213.                                        Uint32 y_old, Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
  214. {
  215.   int rS=0; //(r)eturn(S)tatus
  216.   if((rS=_kit_coreVectorResizeY(Vector, 0,x_new, y_osize,y_nsize, y_new))) return rS;
  217.   if((rS=_kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_old, z_osize,z_nsize, z_new))) return rS;
  218.   return _kit_coreVectorResizeZ(Vector, 0,x_new, y_old,y_new, 0,z_nsize, z_new);
  219. }
  220.  
  221. static inline int _kit_coreVectorAddXZ(kit_coreVector** Vector_p,
  222.                                        Uint32 x_osize, Uint32 x_nsize, Uint32 x_new,
  223.                                        Uint32 x_old,   Uint32 y_nsize, Uint32 y_new,
  224.                                        Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
  225. {
  226.   kit_coreVector* Vector=*Vector_p; int rS=0; //(r)eturn(S)tatus
  227.   if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
  228.   if((rS=_kit_coreVectorResizeY(Vector, x_old,x_new, 0,y_nsize, y_new))) return rS;
  229.   return _kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_new, z_osize,z_nsize, z_new);
  230. }
  231.  
  232. static inline int _kit_coreVectorAddXYZ(kit_coreVector** Vector_p,
  233.                                         Uint32 x_osize, Uint32 x_nsize, Uint32 x_new,
  234.                                         Uint32 x_old,   Uint32 y_osize, Uint32 y_nsize, Uint32 y_new,
  235.                                         Uint32 y_old,   Uint32 z_osize, Uint32 z_nsize, Uint32 z_new)
  236. {
  237.   kit_coreVector* Vector=*Vector_p; int rS=0; //(r)eturn(S)tatus
  238.   if((rS=_kit_coreVectorResizeX(Vector_p, x_osize,x_nsize, x_new))) return rS;
  239.   if((rS=_kit_coreVectorResizeY(Vector, 0,x_old, y_osize,y_osize, y_new))) return rS;
  240.   if((rS=_kit_coreVectorResizeY(Vector, x_old,x_new, 0,y_osize, y_new))) return rS;
  241.   if((rS=_kit_coreVectorResizeZ(Vector, 0,x_new, 0,y_old, z_osize,z_nsize, z_new))) return rS;
  242.   return _kit_coreVectorResizeZ(Vector, 0,x_new, y_old,y_new, 0,z_nsize, z_new);
  243. }
  244.  
  245.  
  246. #define _0X (0x000)
  247. #define _NX (0x100)
  248. #define _PX (0x300)
  249. #define _0Y (0x000)
  250. #define _NY (0x010)
  251. #define _PY (0x030)
  252. #define _0Z (0x000)
  253. #define _NZ (0x001)
  254. #define _PZ (0x003)
  255. #define _VADECISION(_action) \
  256.   if((returnStatus=_action)){ goto _error_; }
  257. #define _VADECISION_B(_action) _VADECISION(_action) break;
  258. //add to or subtract from size
  259. int kit_coreVectorAdd(kit_coreVector** Vector_p, Sint32 x_add, Sint32 y_add, Sint32 z_add){
  260.   if(!x_add && !y_add && !z_add) return 0;
  261.   int returnStatus=0;
  262.   _IF_SDLERR_I(Vector_p==NULL,-1,;,"Vector_p cannot be NULL")
  263.   kit_coreVector* Vector=*Vector_p;
  264.   _IF_SDLERR_I(Vector==NULL,-2,;,"*Vector_p cannot be NULL")
  265.  
  266.   Uint32 unit=Vector->unit;
  267.   Uint32 x_old=Vector->x,   y_old=Vector->y,   z_old=Vector->z;
  268.        _IF_SDLERR_I( (!z_old)&&(z_add!=0) ,-3,;,"cannot change z on non-3d vector")
  269.   else _IF_SDLERR_I( (!y_old)&&(y_add!=0) ,-4,;,"cannot change y on 1d vector")
  270.   Uint32 x_new=x_old+x_add, y_new=y_old+y_add, z_new=z_old+z_add;
  271.        _IF_SDLERR_I( (!z_old)!=(!z_add) ,-5,;,"resulting z cannot have its axis status flipped")
  272.   else _IF_SDLERR_I( (!y_old)!=(!y_add) ,-6,;,"resulting y cannot have its axis status flipped")
  273.  
  274.   //check for any underflows and overflows respectively (and some dimension error checks)
  275.    //x
  276.        _IF_SDLERR_I( x_add<0&&(x_new>x_old) ,-7,;,"resulting x cannot be 0")
  277.   else _IF_SDLERR_I( x_add>0&&(x_new<x_old) ,-8,;,"applying x_add caused an overflow")
  278.    //y
  279.   if(y_add<0 && (y_new>y_old)) y_new=0;
  280.   else _IF_SDLERR_I( y_add>0&&(y_new<y_old) ,-9,;,"applying y_add caused an overflow")
  281.   else _IF_SDLERR_I( y_new==0 && z_new>0 ,-10,;,"resulting y cannot be 0 if z>0")
  282.    //z
  283.   if(z_add<0 && (z_new>z_old)) z_new=0;
  284.   else _IF_SDLERR_I( z_add>0&&(z_new<z_old) ,-11,;,"applying z_add caused an overflow")
  285.  
  286.   Uint32 x_unit, y_unit, z_unit;
  287.   x_unit=y_unit=z_unit = sizeof(void*);
  288.   int decision, dims=(x_new!=0)+(y_new!=0)+(z_new!=0); //dim(ension)s
  289.        if(dims==1) x_unit=unit;
  290.   else if(dims==2) y_unit=unit;
  291.   else             z_unit=unit; //(dims==3)
  292.   Uint32 x_osize=x_unit*x_old, y_osize=y_unit*y_old, z_osize=z_unit*z_old;
  293.   Uint32 x_nsize=x_unit*x_new, y_nsize=y_unit*y_new, z_nsize=z_unit*z_new;
  294.  
  295.   decision =( ((x_new>0)<<1)|(x_new!=0) )<<12; //0x0000 -> 0x3000
  296.   decision|=( ((y_new>0)<<1)|(y_new!=0) )<< 8; //0x0000 -> 0x0300
  297.   decision|=( ((z_new>0)<<1)|(z_new!=0) )<< 4; //0x0000 -> 0x0030
  298.   switch(decision){
  299.   //SUB
  300.   case (_NX|_0Y|_0Z): //_NX_0Y_0Z; technically a superset of: (n/a)
  301.     _VADECISION_B(_kit_coreVectorSubX( &Vector,x_new,x_old,y_old,x_osize,x_nsize ))
  302.   case (_0X|_NY|_0Z): //_0X_NY_0Z; technically a superset of: (n/a)
  303.     _VADECISION_B(_kit_coreVectorSubY( Vector,x_new,y_old,y_new,y_osize,y_nsize ))
  304.   case (_0X|_0Y|_NZ): //_0X_0Y_NZ; technically a superset of: (n/a)
  305.     _VADECISION_B(_kit_coreVectorSubZ( Vector,x_new,y_new,z_osize,z_nsize,z_new ))
  306.   case (_NX|_NY|_0Z): //_NX_NY_0Z; technically a superset of: (_NX_0Y_0Z,_0X_NY_0Z)
  307.     _VADECISION_B(_kit_coreVectorSubXY( &Vector,x_new,x_old,y_old,x_osize,x_nsize,y_new,y_osize,y_nsize ))
  308.   case (_0X|_NY|_NZ): //_0X_NY_NZ; technically a superset of: (_0X_NY_0Z,_0X_0Y_NZ)
  309.     _VADECISION_B(_kit_coreVectorSubYZ( Vector,x_new,y_old,y_new,y_osize,y_nsize,z_osize,z_nsize,z_new ))
  310.   case (_NX|_0Y|_NZ): //_NX_0Y_NZ; technically a superset of: (_NX_0Y_0Z,_0X_0Y_NZ)
  311.     _VADECISION_B(_kit_coreVectorSubXZ( &Vector,x_new,x_old,y_new,x_osize,x_nsize,z_osize,z_nsize,z_new ))
  312.   case (_NX|_NY|_NZ): //_NX_NY_NZ; technically a superset of: (_NX_NY_0Z,_0X_0Y_NZ)
  313.     _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 ))
  314.   //ADD
  315.   case (_PX|_0Y|_0Z): //_PX_0Y_0Z; technically a superset of: (n/a)
  316.     _VADECISION_B(_kit_coreVectorAddX( &Vector,x_osize,x_nsize,x_new,x_old,y_nsize,y_new,z_nsize,z_new ))
  317.   case (_0X|_PY|_0Z): //_0X_PY_0Z; technically a superset of: (n/a)
  318.     _VADECISION_B(_kit_coreVectorAddY( Vector,x_new,y_osize,y_nsize,y_new,y_old,z_nsize,z_new ))
  319.   case (_0X|_0Y|_PZ): //_0X_0Y_PZ; technically a superset of: (n/a)
  320.     _VADECISION_B(_kit_coreVectorAddZ( Vector,x_new,y_new,z_osize,z_nsize,z_new ))
  321.   case (_PX|_PY|_0Z): //_PX_PY_0Z; technically a superset of: (_PX_0Y_0Z,_0X_PY_0Z)
  322.     _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 ))
  323.   case (_0X|_PY|_PZ): //_0X_PY_PZ; technically a superset of: (_0X_PY_0Z,_0X_0Y_PZ)
  324.     _VADECISION_B(_kit_coreVectorAddYZ( Vector,x_new,y_osize,y_nsize,y_new,y_old,z_osize,z_nsize,z_new ))
  325.   case (_PX|_0Y|_PZ): //_PX_0Y_PZ; technically a superset of: (_PX_0Y_0Z,_0X_0Y_PZ)
  326.     _VADECISION_B(_kit_coreVectorAddXZ( &Vector,x_osize,x_nsize,x_new,x_old,y_nsize,y_new,z_osize,z_nsize,z_new ))
  327.   case (_PX|_PY|_PZ): //_PX_PY_PZ; technically a superset of: (_PX_PY_0Z,_0X_0Y_PZ)
  328.     _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 ))
  329.   //SUBX ADDYZ
  330.   case (_NX|_PY|_0Z): //_NX_PY_0Z; technically a superset of: (_NX_0Y_0Z, _0X_PY_0Z)
  331.     _VADECISION(  _kit_coreVectorSubX( &Vector,x_new,x_old,y_old,x_osize,x_nsize ))
  332.     _VADECISION_B(_kit_coreVectorAddY( Vector,x_new,y_osize,y_nsize,y_new,y_old,z_nsize,z_new ))
  333.   case (_NX|_0Y|_PZ): //_NX_0Y_PZ; technically a superset of: (_NX_0Y_0Z, _0X_0Y_PZ)
  334.     _VADECISION(  _kit_coreVectorSubX( &Vector,x_new,x_old,y_old,x_osize,x_nsize ))
  335.     _VADECISION_B(_kit_coreVectorAddZ( Vector,x_new,y_new,z_osize,z_nsize,z_new ))
  336.   case (_NX|_PY|_PZ): //_NX_PY_PZ; technically a superset of: (_NX_0Y_0Z, _0X_PY_PZ)
  337.     _VADECISION(  _kit_coreVectorSubX( &Vector,x_new,x_old,y_old,x_osize,x_nsize ))
  338.     _VADECISION_B(_kit_coreVectorAddYZ( Vector,x_new,y_osize,y_nsize,y_new,y_old,z_osize,z_nsize,z_new ))
  339.   //SUBY ADDXZ
  340.   case (_PX|_NY|_0Z): //_PX_NY_0Z; technically a superset of: (_0X_NY_0Z, _PX_0Y_0Z)
  341.     _VADECISION(  _kit_coreVectorSubY( Vector,x_new,y_old,y_new,y_osize,y_nsize ))
  342.     _VADECISION_B(_kit_coreVectorAddX( &Vector,x_osize,x_nsize,x_new,x_old,y_nsize,y_new,z_nsize,z_new ))
  343.   case (_0X|_NY|_PZ): //_0X_NY_PZ; technically a superset of: (_0X_NY_0Z, _0X_0Y_PZ)
  344.     _VADECISION(  _kit_coreVectorSubY( Vector,x_new,y_old,y_new,y_osize,y_nsize ))
  345.     _VADECISION_B(_kit_coreVectorAddZ( Vector,x_new,y_new,z_osize,z_nsize,z_new ))
  346.   case (_PX|_NY|_PZ): //_PX_NY_PZ; technically a superset of: (_0X_NY_0Z, _PX_0Y_PZ)
  347.     _VADECISION(  _kit_coreVectorSubY( Vector,x_new,y_old,y_new,y_osize,y_nsize ))
  348.     _VADECISION_B(_kit_coreVectorAddXZ( &Vector,x_osize,x_nsize,x_new,x_old,y_nsize,y_new,z_osize,z_nsize,z_new ))
  349.   //SUBZ ADDXY
  350.   case (_PX|_0Y|_NZ): //_PX_0Y_NZ; technically a superset of: (_0X_0Y_NZ, _PX_0Y_0Z)
  351.     _VADECISION(  _kit_coreVectorSubZ( Vector,x_new,y_new,z_osize,z_nsize,z_new ))
  352.     _VADECISION_B(_kit_coreVectorAddX( &Vector,x_osize,x_nsize,x_new,x_old,y_nsize,y_new,z_nsize,z_new ))
  353.   case (_0X|_PY|_NZ): //_0X_PY_NZ; technically a superset of: (_0X_0Y_NZ, _0X_PY_0Z)
  354.     _VADECISION(  _kit_coreVectorSubZ( Vector,x_new,y_new,z_osize,z_nsize,z_new ))
  355.     _VADECISION_B(_kit_coreVectorAddY( Vector,x_new,y_osize,y_nsize,y_new,y_old,z_nsize,z_new ))
  356.   case (_PX|_PY|_NZ): //_PX_PY_NZ; technically a superset of: (_0X_0Y_NZ, _PX_PY_0Z)
  357.     _VADECISION(  _kit_coreVectorSubZ( Vector,x_new,y_new,z_osize,z_nsize,z_new ))
  358.     _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 ))
  359.   //SUBXYZ ADDXYZ
  360.   case (_NX|_NY|_PZ): //_NX_NY_PZ; technically a superset of: (_NX_NY_0Z, _0X_0Y_PZ)
  361.     _VADECISION(  _kit_coreVectorSubXY( &Vector,x_new,x_old,y_old,x_osize,x_nsize,y_new,y_osize,y_nsize ))
  362.     _VADECISION_B(_kit_coreVectorAddZ( Vector,x_new,y_new,z_osize,z_nsize,z_new ))
  363.   case (_PX|_NY|_NZ): //_PX_NY_NZ; technically a superset of: (_0X_NY_NZ, _PX_0Y_0Z)
  364.     _VADECISION(  _kit_coreVectorSubYZ( Vector,x_new,y_old,y_new,y_osize,y_nsize,z_osize,z_nsize,z_new ))
  365.     _VADECISION_B(_kit_coreVectorAddX( &Vector,x_osize,x_nsize,x_new,x_old,y_nsize,y_new,z_nsize,z_new ))
  366.   case (_NX|_PY|_NZ): //_NX_PY_NZ; technically a superset of: (_NX_0Y_NZ, _0X_PY_0Z)
  367.     _VADECISION(  _kit_coreVectorSubXZ( &Vector,x_new,x_old,y_new,x_osize,x_nsize,z_osize,z_nsize,z_new ))
  368.     _VADECISION_B(_kit_coreVectorAddY( Vector,x_new,y_osize,y_nsize,y_new,y_old,z_nsize,z_new ))
  369.   default: _IS_SDLERR_I(-15,;,"decision switch defaulted (this shouldn't be possible); decision=0x%X",decision)
  370.   }
  371.   *Vector_p=Vector;
  372.   _error_:
  373.   return returnStatus;
  374. }
  375. //set new size (if an axis == 0, then leave that axis unchanged)
  376. int kit_coreVectorSet(kit_coreVector** Vector_p, Uint32 x_new, Uint32 y_new, Uint32 z_new){
  377.   int returnStatus=0;
  378.   _IF_SDLERR_I(Vector_p==NULL,-1,;,"Vector_p cannot be NULL")
  379.   kit_coreVector* Vector=*Vector_p;
  380.   _IF_SDLERR_I(Vector==NULL,-2,;,"*Vector_p cannot be NULL")
  381.  
  382.   Uint32 x_old =Vector->x, y_old =Vector->y, z_old=Vector->z;
  383.   Sint32 x_diff=(x_new==0) ? 0 : x_new-x_old;
  384.   Sint32 y_diff=(y_new==0) ? 0 : y_new-y_old;
  385.   Sint32 z_diff=(z_new==0) ? 0 : z_new-z_old;
  386.  
  387.   returnStatus=kit_coreVectorAdd(Vector_p, x_diff,y_diff,z_diff);
  388.  
  389.   _error_:
  390.   return returnStatus;
  391. }
  392.  
  393.  
  394. int kit_coreVectorDestroy(kit_coreVector** Vector_p){
  395.   int returnStatus=0;
  396.   _IF_SDLERR_I(Vector_p==NULL,-1,;,"Vector_p cannot be NULL")
  397.   kit_coreVector* Vector=*Vector_p;
  398.   _IF_SDLERR_I(Vector==NULL,-2,;,"*Vector_p cannot be NULL")
  399.  
  400.   void *p1d, **p2d, ***p3d;
  401.   int x=Vector->x, y=Vector->y; //z=Vector->z;
  402.   Sint32 condition=Vector->_dims&3;
  403.   if(!condition) condition=(x!=0) + (y!=0) + (Vector->z!=0);
  404.  
  405.   //condition must be equal to the number of dimensions (1=1d,2=2d,3=3d)
  406.   switch(condition){
  407.   case 3: p3d=Vector->p3d;
  408.     for(Uint32 xi=0; xi<x; ++xi){
  409.       if((p2d=p3d[xi])==NULL) break;
  410.       for(Uint32 yi=0; yi<y; ++yi){
  411.         if((p1d=p2d[yi])==NULL) break;
  412.         else SDL_free(p1d);
  413.       }
  414.     }
  415.   case 2: p2d=Vector->p2d;
  416.     for(Uint32 xi=0; xi<x; ++xi){
  417.       if((p1d=p2d[xi])==NULL) break;
  418.       else SDL_free(p1d);
  419.     }
  420.   case 1: SDL_free(Vector); //frees Vector->p1d too
  421.   default: *Vector_p=NULL;
  422.   }
  423.   _error_:
  424.   return returnStatus;
  425. }
  426.  
  427.  
  428. kit_coreVector* kit_coreVectorCreate(Uint32 x, Uint32 y, Uint32 z,
  429.                                      Uint32 unit, Uint32 type_n,
  430.                                      int* returnStatus_p)
  431. {
  432.   kit_coreVector* Vector=NULL;
  433.   size_t x_size, y_size, z_size;
  434.   void /* *p1d, */ **p2d, ***p3d;
  435.   int returnStatus=0, dimensions=0x80000000;
  436.   _IF_SDLERR_I(x==0,-1,;,"variable x cannot be 0")
  437.  
  438.   //x axis
  439.   x_size=(!y && !z) ? unit*x : sizeof(void*)*x;
  440.   returnStatus=_kit_coreVectorRealloc(&Vector,0, 0,sizeof(kit_coreVector)+x_size);
  441.   _IF_SDLERR_I(returnStatus,-2,;,"SDL_malloc() for x axis failed")
  442.   Vector->p1d=((void*)Vector) + sizeof(kit_coreVector); //array is ahead by 32B
  443.   ++dimensions; //vector now contains an x axis
  444.  
  445.   //y axis
  446.   _IF_SDLERR_I(y==0 && z>0,-3,;,"variable y cannot be 0 if z>0")
  447.   if(y>0){ ++dimensions; //vector now contains a y axis
  448.     y_size=(!z) ? unit*y : sizeof(void*)*y;
  449.     p2d=Vector->p2d;
  450.     for(Uint32 xi=0; xi<x; ++xi){
  451.       returnStatus=_kit_coreVectorRealloc(&p2d[xi],0, 0,y_size);
  452.       _IF_SDLERR_I(returnStatus,-4,;,"SDL_malloc() for y axis failed")
  453.     }
  454.   }
  455.  
  456.   //z axis
  457.   if(z>0){ ++dimensions; //vector now contains a z axis
  458.     z_size=unit*z; p3d=Vector->p3d;
  459.     for(Uint32 xi=0; xi<x; ++xi){ p2d=p3d[xi];
  460.       for(Uint32 yi=0; yi<y; ++yi){
  461.         returnStatus=_kit_coreVectorRealloc(&p2d[yi],0, 0,z_size);
  462.         _IF_SDLERR_I(returnStatus,-5,;,"SDL_malloc() for z axis failed")
  463.       }
  464.     }
  465.   }
  466.  
  467.   dimensions&=0x7fffffff; //unset sign bit
  468.   Vector->type.n=type_n;
  469.   Vector->x=x, Vector->y=y, Vector->z=z;
  470.   Vector->unit=unit;
  471.   _error_:
  472.   if(Vector!=NULL){
  473.     Vector->_dims=dimensions&3;
  474.     if(dimensions<0) returnStatus+=kit_coreVectorDestroy(&Vector); //-=0 -> 2
  475.   }
  476.   if(returnStatus_p!=NULL) *returnStatus_p=returnStatus;
  477.   return Vector;
  478. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement