[Gambas-devel] Fwd: Declaring static array in library methods.

tommyline at ...674... tommyline at ...674...
Mon Feb 6 15:16:26 CET 2012


Hi Gambas Genies :)

Trying to send it again, as i have problem with getting throught.

I'm trying to implement glDrawElements() method to Opengl library, but got few problems.
To use arrays I have to use glVertexPointer() method first and there comes truoble.

gl.h:

glVertexPointer( GLint size, GLenum type, GLsizei stride, const GLvoid *ptr ); <-it sends the pointer to the array to opengl.

Initially I've tried  the code:

EGIN_METHOD(GLVERTEXPOINTER, GB_INTEGER size; GB_INTEGER type; GB_INTEGER stride; GB_OBJECT tab );

    int s=VARG(size);
    if ((s==2) || (s==3) || (s==4)){
	GB_ARRAY arr = (GB_ARRAY) VARG(tab);
	int i;
	int count = GB.Array.Count(arr);
	GLfloat param[count];
	for (i=0; i<count; i++)
		param[i] = *((GLfloat *)GB.Array.Get(arr,i));
	glVertexPointer(s, VARG(type), VARG(stride), param) ;
    else{
    	printf("ERROR - Wrong size of vertex elements! Should be 2,3 or 4!\n");
	glDisableClientState(GL_VERTEX_ARRAY);
     }
	
END_METHOD

but it's not working.

Then I made this:

BEGIN_METHOD(GLVERTEXPOINTER, GB_INTEGER size; GB_INTEGER type; GB_INTEGER stride; GB_OBJECT tab );

    int s=VARG(size);
    if ((s==2) || (s==3) || (s==4)){
	GB_ARRAY arr = (GB_ARRAY) VARG(tab);
	int i;
	int count = GB.Array.Count(arr);
	static GLfloat param[256];           //--> requires static array !!!
	for (i=0; i<count; i++)
		param[i] = *((GLfloat *)GB.Array.Get(arr,i));
	glVertexPointer(s, VARG(type), VARG(stride), param) ;
    else{
    	printf("ERROR - Wrong size of vertex elements! Should be 2,3 or 4!\n");
	glDisableClientState(GL_VERTEX_ARRAY);
     }
	
END_METHOD

and it works, but as the static array size must be constant, it's a waste of resourses to declare array big enough not to be overfilled, and you can't be sure it won't.

Then I tried this:

BEGIN_METHOD(GLVERTEXPOINTER, GB_INTEGER size; GB_INTEGER type; GB_INTEGER stride; GB_OBJECT tab );

    int s=VARG(size);
    if ((s==2) || (s==3) || (s==4)){
	GB_ARRAY arr = (GB_ARRAY) VARG(tab);
	int i;
	int count = GB.Array.Count(arr);
	GLfloat* param = (GLfloat*) malloc(sizeof(GLfloat) * VARG(size));
	for (i=0; i<count; i++)
		param[i] = *((GLfloat *)GB.Array.Get(arr,i));
	glVertexPointer(s, VARG(type), VARG(stride), param) ;
    else{
    	printf("ERROR - Wrong size of vertex elements! Should be 2,3 or 4!\n");
	glDisableClientState(GL_VERTEX_ARRAY);
     }
	
END_METHOD

which works too, but kicks error 11 on exit and kills Valgrind before it can detect problem.

My question is, any idea how to pass the size of an array to static declaration? Is it possible?

Regards
Tomek.




More information about the Devel mailing list