how define callback function for 3DGS?

Posted By: frankjiang

how define callback function for 3DGS? - 06/05/17 02:43

how define callback function for 3DGS?
any one know?
Posted By: Ayumi

Re: how define callback function for 3DGS? - 06/05/17 09:37

Just have a look to

https://stackoverflow.com/questions/142789/what-is-a-callback-in-c-and-how-are-they-implemented
Posted By: frankjiang

Re: how define callback function for 3DGS? - 06/05/17 12:05

I say about Lite C,not purl C.
Code:
void populate_array(int *array, size_t arraySize, int (*getNextValue)(void))
{
	    for (size_t i=0; i<arraySize; i++)
	        array[i] = getNextValue();
}

int getNextRandomValue(void)
{
	return 0;
}



this will be show syntax error!
Posted By: Kartoffel

Re: how define callback function for 3DGS? - 06/05/17 16:46

I remember having a similar issue but you can do things like this in lite-c. Give me a minute, I'll give it a try!
Posted By: Kartoffel

Re: how define callback function for 3DGS? - 06/05/17 16:56

have a look:
Code:
#include <acknex.h>

//

void populate_array(int * array, int arraySize, void * getNextValue)
{
	int i;
	for(i = 0; i < arraySize; i++)
	{
		int TempFunction(); // temporary function
		TempFunction = getNextValue; // set function pointer
		
		array[i] = TempFunction(); // execute function
	}
}

int getNextRandomValue(void)
{
	return (int)(random(1000));
}

void main()
{
	fps_max = 60;
	
	random_seed(0); // initialize random seed
	
	//
	
	int MyArray[8]; // create array
	
	populate_array(MyArray, 8, getNextRandomValue); // fill array with return values of function "getNextRandomValue()"
	
	printf("Array Content: (%d, %d, %d, %d, %d, %d, %d, %d)", 
		MyArray[0], MyArray[1], MyArray[2], MyArray[3],
		MyArray[4], MyArray[5], MyArray[6], MyArray[7]);
}

It's a strange work-around but it works. Note that this is a lite-c-only thing, I don't think this would work in C.
Posted By: Ch40zzC0d3r

Re: how define callback function for 3DGS? - 06/05/17 17:24

Its just a simple function pointer, you can pass anything which is 4 bytes in size and cast it to a function to call it...
Posted By: frankjiang

Re: how define callback function for 3DGS? - 06/06/17 01:18

well,can you show any demo code with it? tongue
Posted By: frankjiang

Re: how define callback function for 3DGS? - 06/06/17 01:20

thanks,your example code looks so nice!
Posted By: frankjiang

Re: how define callback function for 3DGS? - 06/06/17 02:11

i want to give this function any parms,how to do it?
Code:
int getNextRandomValue(int a)
{
	return a * a;
}


laugh
Posted By: frankjiang

Re: how define callback function for 3DGS? - 06/06/17 02:15

i try it,but script is crash!
Code:
#include <acknex.h>

//

void populate_array(int * array, int arraySize, void * getNextValue)
{
	int i;
	for(i = 0; i < arraySize; i++)
	{
		int TempFunction(); // temporary function
		TempFunction = getNextValue; // set function pointer
		
		array[i] = TempFunction(); // execute function
	}
}

int getNextRandomValue(int a)
{
	return a*a;
}

void main()
{
	fps_max = 60;
	
	random_seed(0); // initialize random seed
	
	//
	
	int MyArray[8]; // create array
	
	populate_array(MyArray, 2, getNextRandomValue); // fill array with return values of function "getNextRandomValue()"
	
	int TempFunction(int a);
	TempFunction = MyArray[0];
	int res = TempFunction(2);
	printf("%d",res);
}

Posted By: MasterQ32

Re: how define callback function for 3DGS? - 06/06/17 09:20

your argument list must match when calling!

Code:
void populate_array(int * array, int arraySize, void * getNextValue)
{
	int i;
	for(i = 0; i < arraySize; i++)
	{
		int TempFunction(int a); // temporary function
		TempFunction = getNextValue; // set function pointer
		
		array[i] = TempFunction(i); // execute function
	}
}

Posted By: frankjiang

Re: how define callback function for 3DGS? - 06/06/17 12:38

thank you for your answer!
but i want to use this code likes:
Code:
void PrintfText(int value) 
{
    printf("Hello World %d!/n",value);
}

void CallPrintfText(void (*callfuct)(int),int value)
{
    callfuct(value);
}

int main()
{
    CallPrintfText(PrintfText);
    return 0;
}

Posted By: Kartoffel

Re: how define callback function for 3DGS? - 06/06/17 13:19

as you've seen in my example, you have to use that little workaround.
Code:
void PrintfText(int value) 
{
    printf("Hello World %d!/n",value);
}

void CallPrintfText(void * callfuct,int value)
{
    void func(int);
    func = callfunct;
    func(value);
}

int main()
{
    CallPrintfText(PrintfText);
    return 0;
}


I don't think there's another way using lite-c.
Posted By: frankjiang

Re: how define callback function for 3DGS? - 06/07/17 02:09

Code:
#include <acknex.h>
#include <default.c>
void PrintfText(int value) 
{
    printf("Hello World n%d!",value);
}

void CallPrintfText(void * callfuct,int value)
{
    void func(int);
    func = callfunct;
    func(value);
}

int main()
{
    CallPrintfText(PrintfText);
    return 0;
}


this code will be show"callfuct undeclared idenitifier"
Posted By: Superku

Re: how define callback function for 3DGS? - 06/07/17 05:40

Because there's a typo in the first parameter ("callfuct" instead of "callfunct").
Posted By: frankjiang

Re: how define callback function for 3DGS? - 06/07/17 12:04

so do you know how do it?
Posted By: Dico

Re: how define callback function for 3DGS? - 06/08/17 00:51

Superku mean this :



void CallPrintfText(void * callfuct,int value)
{
void func(int);
func = callfunct;
func(value);
}

its not the same name just correct it like this :


Code:
#include <acknex.h>
#include <default.c>
void PrintfText(int value) 
{
    printf("Hello World n%d!",value);
}

void CallPrintfText(void * callfunct,int value)
{
    void func(int);
    func = callfunct;
    func(value);
}

int main()
{
    CallPrintfText(PrintfText,50);
    return 0;
}

Posted By: frankjiang

Re: how define callback function for 3DGS? - 06/08/17 05:29

Thank you very much.
Posted By: Dico

Re: how define callback function for 3DGS? - 06/08/17 16:08

You're welcome laugh
© 2024 lite-C Forums