Gamestudio Links
Zorro Links
Newest Posts
New FXCM FIX Plugin
by flink. 06/04/24 07:30
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (Akow, SBGuy), 1,423 guests, and 7 spiders.
Key: Admin, Global Mod, Mod
Newest Members
AemStones, LucasJoshua, Baklazhan, Hanky27, firatv
19058 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 2 of 3 1 2 3
Re: is possible lite-C`struct contain any functions [Re: Helghast] #340339
09/03/10 10:17
09/03/10 10:17
Joined: Dec 2009
Posts: 128
China
frankjiang Offline OP
Member
frankjiang  Offline OP
Member

Joined: Dec 2009
Posts: 128
China
thanks everybody.


development 3d game is interesting!
Re: is possible lite-C`struct contain any functions [Re: frankjiang] #340343
09/03/10 11:05
09/03/10 11:05
Joined: Feb 2009
Posts: 2,154
Damocles_ Offline
Expert
Damocles_  Offline
Expert

Joined: Feb 2009
Posts: 2,154
This works: (using int)
(the includes are not perfect..)

Code:
#include <default.c>
#include <litec.h>

typedef struct
{
    int x;
    int y;
    void fn(int x);    //The function pointer
} SPOT;
void func(int x)    //your function
{
    error("Hello world!");
}

SPOT* setup_spot()  //a init routine
{
    SPOT* spot = malloc(sizeof(SPOT));
    spot->x = 0;
    spot->y = 0;
    spot->fn = func;  //Set fn as func
    return spot;
}

void main()
{
    SPOT* spot = setup_spot();  //create SPOT
    spot->fn(5);   //call func();
}



Re: is possible lite-C`struct contain any functions [Re: Damocles_] #340346
09/03/10 11:33
09/03/10 11:33
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
@damocles: It does? Is that too for A7? I was under the impression that it was not possible to call structure elements as functions...

I always do this, seems to work just fine, but not sure if it could go wrong with an integer pointer as function pointer storage:

Code:
typedef struct
{
 int *spot_func;
} SPOT;

void spot_func_prototype(SPOT *s);

...

void spot_remove(SPOT s)
{
 error("yay");
}

void main()
{
 SPOT spot;
 spot.func = spot_remove;

 spot_func_prototype = spot.func;
 spot_func_prototype( spot );
}



Last edited by Joozey; 09/03/10 11:35.

Click and join the 3dgs irc community!
Room: #3dgs
Re: is possible lite-C`struct contain any functions [Re: Joozey] #340347
09/03/10 11:38
09/03/10 11:38
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
MasterQ32 Offline
Expert
MasterQ32  Offline
Expert

Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
i think everybody has his own, working ideas and there are more ways than we can count, but we can't get them into our mind


Visit my site: www.masterq32.de
Re: is possible lite-C`struct contain any functions [Re: MasterQ32] #340351
09/03/10 11:56
09/03/10 11:56
Joined: Feb 2009
Posts: 2,154
Damocles_ Offline
Expert
Damocles_  Offline
Expert

Joined: Feb 2009
Posts: 2,154
well it works (did test with a7)

Re: is possible lite-C`struct contain any functions [Re: Damocles_] #340353
09/03/10 12:16
09/03/10 12:16
Joined: Feb 2009
Posts: 2,154
Damocles_ Offline
Expert
Damocles_  Offline
Expert

Joined: Feb 2009
Posts: 2,154
Works also with return values.
Basically you can almost work as in an object oriented approach.

Code:
#include <default.c>


typedef struct spotpoint
{
    int x;
    int y;
    int fn(struct spotpoint * myself, int add_x);    //The function pointer
} SPOT;
int func(SPOT* myself, int add_x)    //your function
{
	int itis=myself->x + add_x;
   return itis;
}

SPOT* setup_spot()  //a init routine
{
    SPOT* spot = malloc(sizeof(SPOT));
    spot->x = 0;
    spot->y = 0;
    spot->fn = func;  //Set fn as func
    return spot;
}

void main()
{
//create the objects
    SPOT* spot = setup_spot();  //create SPOT
    SPOT* spot2 = setup_spot();  //create SPOT
    

//alter attributes in objects    
spot->x=4;		//set values
    spot2->x=3;	//set values
    
//call function in objects, so they work on their personal attributes    
    int whatis= spot->fn(spot,5);   //call func();
    int whatis2= spot2->fn(spot2,5);   //call func();
    
    if(whatis==9) error("Correct value for spot returned!");
    else error("False value spot returned!");
    
    if(whatis2==9) error("Correct value spot2 returned!");
    else error("False value spot2 returned!");
    
}



Last edited by Damocles_; 09/03/10 12:34.
Re: is possible lite-C`struct contain any functions [Re: Damocles_] #340399
09/03/10 20:23
09/03/10 20:23
Joined: Sep 2009
Posts: 993
Budapest
Aku_Aku Offline
User
Aku_Aku  Offline
User

Joined: Sep 2009
Posts: 993
Budapest
I am totally impressed by Damocles_ and Helghast work.

Re: is possible lite-C`struct contain any functions [Re: Aku_Aku] #342045
09/22/10 21:20
09/22/10 21:20
Joined: Sep 2003
Posts: 9,859
F
FBL Offline
Senior Expert
FBL  Offline
Senior Expert
F

Joined: Sep 2003
Posts: 9,859
Now all we need is "this" tongue

Re: is possible lite-C`struct contain any functions [Re: FBL] #342050
09/22/10 22:33
09/22/10 22:33
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
Couldn't figure out a "this" by workarounds. Anyone? tongue


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

Joined: Apr 2007
Posts: 3,751
Canada
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.


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Page 2 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