Gamestudio Links
Zorro Links
Newest Posts
Free Live Data for Zorro with Paper Trading?
by dr_panther. 05/18/24 11:01
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (7th_zorro, dr_panther), 724 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 3 of 3 1 2 3
Re: is possible lite-C`struct contain any functions [Re: WretchedSid] #342070
09/23/10 07:37
09/23/10 07:37
Joined: Jan 2004
Posts: 3,023
The Netherlands
Helghast Offline
Expert
Helghast  Offline
Expert

Joined: Jan 2004
Posts: 3,023
The Netherlands
Originally Posted By: JustSid
Write a runtime that gets called wenn calling a function in a struct. In this function set a pointer called this to the struct and then call the actual function.

It's that easy.


Yep exactly as JustSid said, but here's an example with that:

usage of "this"
Code:
typedef struct
{
    var x;
    var y;
    STRING *funcName;    //The function pointer
} SPOT;

void callingFunction(SPOT *this) //your function
{
	STRING *tempStr = str_create("");
	str_cpy(tempStr, "this position x = ");
	str_cat(tempStr, str_for_num(NULL, this.x));
	str_cat(tempStr, ", and this position y = ");
	str_cat(tempStr, str_for_num(NULL, this.y));
	error(tempStr);
}

SPOT* setup_spot()  //a init routine
{
	SPOT* spot = sys_malloc(sizeof(SPOT));
	
	spot->x = random(100);
	spot->y = random(100);
	spot->funcName = str_create("");  //Set fn as func
	str_cpy(spot->funcName, "callingFunction");
	
	return(spot);
}

// empty prototype function
void PrototypeCallFunction(SPOT *this);

void main()
{
        random_seed(0);
	SPOT* spot = setup_spot();  //create SPOT
	
	PrototypeCallFunction = engine_getscript(_chr(spot->funcName));
	if(PrototypeCallFunction) { PrototypeCallFunction(spot); }
}




Formerly known as dennis_fantasy
Portfolio - http://www.designorhea.com/
Project - http://randomchance.cherrygames.org/
Re: is possible lite-C`struct contain any functions [Re: Helghast] #342093
09/23/10 12:22
09/23/10 12:22
Joined: May 2007
Posts: 2,043
Germany
Lukas Offline

Programmer
Lukas  Offline

Programmer

Joined: May 2007
Posts: 2,043
Germany
Why use a string instead of a direct function pointer?

Re: is possible lite-C`struct contain any functions [Re: Lukas] #342094
09/23/10 12:33
09/23/10 12:33
Joined: Jan 2004
Posts: 3,023
The Netherlands
Helghast Offline
Expert
Helghast  Offline
Expert

Joined: Jan 2004
Posts: 3,023
The Netherlands
Originally Posted By: Lukas
Why use a string instead of a direct function pointer?


Was having problems running that, and copying a string name is easier to do from an external setup (think textfiles etc) then a direct function name... You can obviously change it to your own personal liking, but to me this was easiest laugh

regards,


Formerly known as dennis_fantasy
Portfolio - http://www.designorhea.com/
Project - http://randomchance.cherrygames.org/
Re: is possible lite-C`struct contain any functions [Re: WretchedSid] #342115
09/23/10 15:54
09/23/10 15:54
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
engine_getscript is dirty, calling the function from the structure is what we want.

spot->function();

But when using a dummy function inbetween that passes the object as "this", you can't give other parameters.

Last edited by Joozey; 09/23/10 15:57.

Click and join the 3dgs irc community!
Room: #3dgs
Re: is possible lite-C`struct contain any functions [Re: Joozey] #342118
09/23/10 16:46
09/23/10 16:46
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
Well, actually you can. The trick is called variadic functions which are sadly not available in Lite-C. However, you can use a DLL for this stuff and everything is fine.

I tried something like this already with Lite-ObjectiveC but no one was interested in this so there is no Lite-C version anymore.


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: is possible lite-C`struct contain any functions [Re: WretchedSid] #342261
09/25/10 22:01
09/25/10 22:01
Joined: Apr 2009
Posts: 33
Germany
B
Bunsen Offline
Newbie
Bunsen  Offline
Newbie
B

Joined: Apr 2009
Posts: 33
Germany
Interesting thread. This would be my try:

//-------------------------------------------------------------------------
// Some Prototypes
//-------------------------------------------------------------------------
void delegate(void); // Simple void function
#define CALL(this, func) delegate = this.aFunc; delegate()

void delegate1(int x); // Void function with int argument
#define CALL1(this, func, arg) delegate1 = this.aFunc; delegate1(arg)

int delegate2(int x); // Int function with int argument
#define CALL2(retVal, this, func, arg) delegate2 = this.aFunc; retVal = delegate2(arg)

// Expand this here ...

//-------------------------------------------------------------------------
// Some Functions
//-------------------------------------------------------------------------
void func1() // A simple void function
{
printf("Hallo world!");
}

void func2(int x) // A void function which process an argument
{
printf("Result = %d", x);
}

int func3(int x) // A function which process an argument and returns a result
{
return x * 2;
}

//-------------------------------------------------------------------------
// A structure with a function member
//-------------------------------------------------------------------------
typedef struct
{
int a,b;
long aFunc; // Simply use a long integer to store the function address
} AStruct;

//-------------------------------------------------------------------------
// Use it
//-------------------------------------------------------------------------
void main()
{
AStruct obj[3]; // 3 Instances

obj[0].aFunc = func1; // Obj 0 gets a simple void function
CALL(obj[0], aFunc);

obj[1].aFunc = func2; // Obj 1 gets a void function which process an argument
CALL1(obj[1], aFunc, 42);

obj[2].aFunc = func3; // Obj 2 gets a function which process an argument and returns a result
CALL2(int retVal, obj[2], aFunc, 42);
printf("retVal = %d", retVal);
}

Page 3 of 3 1 2 3

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1