clear vector()'s

Posted By: Joozey

clear vector()'s - 02/03/12 22:23

Hi,

Is there a way to clear all vectors made by vector() without wait(1)? I exceed 64 vector() calls easily when using draw_line3d for drawing something. Before throwing my complete code over to an alternative approach, I was hoping to just clear the vector()s so I can call them again.
Posted By: Superku

Re: clear vector()'s - 02/04/12 00:03

vector() does not create VECTORs, it simply uses one of 64 pre-allocated VECTORs. I assume it works approximately as follows:

VECTOR _vector[64];
var _vector_current = 0;

VECTOR* vector(var x, var y, var z)
{
_vector_current = (_vector_current+1)%64;
_vector[_vector_current].x = x;
_vector[_vector_current].y = y;
_vector[_vector_current].z = z;
return &_vector[_vector_current];
}
Posted By: HeelX

Re: clear vector()'s - 02/04/12 11:11

If you use a function like this:

Code:
VECTOR* vec_setxyz (VECTOR* v, float x, float y, float z)
{
    if (v)
    {
        v->x = x;
        v->y = y;
        v->z = z;
        
        return(v);
    }
    else
        return(NULL);
}



You can simply do this and don't have to rely on vector(..):

Code:
int myLittleFunction ()
{
	...
	
	VECTOR v;
	vec_setxyz(&v, 1, 2, 3);
	
	draw_point3d(&v, COLOR_RED, 100, 23);
	
	...
}



Since this is an engine-dll function, I don't understand why there is no std::vector<VECTOR*> used or the like and if in one frame the number of vector(...) calls exceeds the current used VECTOR*'s, new VECTOR*'s are generated and thrown into the std::vector... just my oppinion.
Posted By: Joozey

Re: clear vector()'s - 02/04/12 12:31

Quote:
_vector_current = (_vector_current+1)%64;

That's what I assumed too, but after 64 draw calls (using vector()) the draw function receives odd values.

Quote:
VECTOR* vec_setxyz (VECTOR* v, float x, float y, float z)

Hm yeah splitting stuff to a separate function at least solves the problem, although now I'm not entirely sure why.

Code:
draw_line3d( vec_add( vec_rotate( vec_create( vecTemp,  -4, -4, 0 ), vec_create( vecTemp, angle, 0, 0) ), pos ), vec_create( vecTemp, 100,100,100), 100 );


After 64 draw_lin3d calls, the position is screwed up here.
But why?

Quote:
VECTOR* rotate_point( VECTOR* relativePosition, VECTOR* absolutePosition, VECTOR* angle )
{
vec_rotate( relativePosition, angle );
vec_add( relativePosition, absolutePosition );
return relativePosition;
}

draw_line3d( rotate_point( vector(-4, -4, 0 ), vPos, vAngle ), vector( 100,100,100), 100 );

Now it works also above 64 draw calls.
So why didn't the former approach work?
Posted By: jcl

Re: clear vector()'s - 02/06/12 11:17

Yes, vector() works indeed similar as described by Superku. It is for passing vector pointers to functions, not for generating vectors. To generate vectors or any other structs in a C program, use either sys_malloc or a static array.

vector() does not "clear" its vectors because there is nothing to clear. Only vectors that you have dynamically generated must be cleared.

© 2024 lite-C Forums