Who's Online
150 user(s) are online (
136 user(s) are browsing
Forums )
Members: 1
Guests: 149
skynet ,
more...
Topic options
View mode
Newest First
GCC, Dynamic structure creation method ?
Posted on:
2009/2/25 18:29
#1
Quite a regular
Joined: 2008/11/3 12:06Last Login
: 2023/8/2 22:18
From South France
Group:
Registered Users
Hi,
In fact, I want to create dynamically objects that uses a structure.
Imagine a structure like this one :
typedef struct {
BOOL Exist ;
float XPos , YPos , ZPos ;
float XRot , YRot , ZRot ;
float FOV ;
BOOL Backdrop ;
float RangeNear , RangeFar ;
float Left , Top , Right , Bottom ;
} ObjectType ;
I want to have a function in my program that allocate a small memory for an object that'll use this structure and, the function must return the pointer to the freshly created object structure.
I'm not enough skilled in C to imagine how to do this.
Any one got an idea about this ?
Thank you.
Kindest Regards,
Freddix
Edited by freddix on 2009/2/25 19:21:59
All we have to decide is what to do with the time that is given to us.
Re: GCC, Dynamic structure creation method ?
Posted on:
2009/2/25 18:36
#2
Not too shy to talk
Joined: 2006/11/28 14:16Last Login
: 2014/4/18 13:26
From Weston-Super-Mare, Somerset, UK, Europe, Earth, Milky Way, The Universe
Group:
Registered Users
@freddix
ObjectType * AllocateObject ( void )
{
ObjectType newObj = NULL ;
newObj = IExec -> AllocVecTags ( sizeof ( ObjectType ), TAG_DONE );
if( ! newObj ) return NULL ;
// Do any required initialisation
return newObj ;
}
Re: GCC, Dynamic structure creation method ?
Posted on:
2009/2/25 19:14
#3
Quite a regular
Joined: 2008/11/3 12:06Last Login
: 2023/8/2 22:18
From South France
Group:
Registered Users
@xeron
Thank you for this small function.
I imagine that if I want to delete this object from memory, I'll have to do something like this :
* FreeObject ( NewObjectType ){
if ( NewObjectType <> 0 ){
IExec -> FreeVec ( NewObjectType );
}
}
Am I wrong ?
All we have to decide is what to do with the time that is given to us.
Re: GCC, Dynamic structure creation method ?
Posted on:
2009/2/25 19:33
#4
Just can't stay away
Joined: 2006/11/30 11:30Last Login
: 11/18 16:17
From Finland
Group:
Registered Users
@freddix
In C it's "!=" instead of "<>".
void FreeObject ( ObjectType * obj ){
if ( obj != NULL ){
IExec -> FreeVec ( obj );
}
}
Actually as IExec->FreeVec() allows passing in a NULL pointer (in this case it will simply do nothing) you can just write:
void FreeObject ( ObjectType * obj ){
IExec -> FreeVec ( obj );
}
Re: GCC, Dynamic structure creation method ?
Posted on:
2009/2/25 19:36
#5
Just popping in
Joined: 2006/11/27 14:32Last Login
: 1/15 11:23
From Newcastle-upon-Tyne, UK
Group:
Registered Users
@salass00
Quote:
salass00 wrote: Actually as IExec->FreeVec() allows passing in a NULL pointer (in this case it will simply do nothing) you can just write:Does it? You learn something new everyday... I've always checked if the memory pointed is NULL or not before IExec->FreeVec.
Re: GCC, Dynamic structure creation method ?
Posted on:
2009/2/25 19:37
#6
Quite a regular
Joined: 2008/11/3 12:06Last Login
: 2023/8/2 22:18
From South France
Group:
Registered Users
@salass00
It's what I supposed .... but wasn't sure ...
Thank you.
If I'm not wrong, all this should be correct :
typedef struct {
BOOL Exist ;
int Type ;
BOOL Hidden , Culling , Transparency , WireFrame , Fog , Ambient , Lights , Filtering ;
float XPos , YPos , YPos ;
float XRot , YRot , ZRot ;
float XScale , YScale , ZScale ;
float RGBRed , RGBGreen , RGBBlue ;
} Object3DType ;
static Object3DType * Allocate3DObject ( void );
static void Free3DObject ( Object3DType * OldObject );
Object3DType * Allocate3DObject ( void ){
Object3DType * NewObject = NULL ;
NewObject = IExec -> AllocVecTags ( sizeof ( Object3DType ), TAG_DONE );
if ( ! NewObject ){
return NULL ;
}
return NewObject ;
}
void Free3DObject ( Object3DType * OldObject ){
if ( obj != NULL ){
IExec -> FreeVec ( OldObject );
}
}
Right ?
Edited by freddix on 2009/2/25 23:51:35
All we have to decide is what to do with the time that is given to us.
Re: GCC, Dynamic structure creation method ?
Posted on:
2009/2/25 22:12
#7
Just popping in
Joined: 2007/5/15 13:42Last Login
: 2023/3/8 14:54
From Austria
Group:
Registered Users
@freddix There should be an asterisk in the declaration of NewObject in the Allocate3DObject() function: Object3DType *NewObject = NULL; It's a pointer.
Re: GCC, Dynamic structure creation method ?
Posted on:
2009/2/26 0:00
#8
Quite a regular
Joined: 2008/11/3 12:06Last Login
: 2023/8/2 22:18
From South France
Group:
Registered Users
@Gazelle I've modified. is it correct now ?
All we have to decide is what to do with the time that is given to us.
Re: GCC, Dynamic structure creation method ?
Posted on:
2009/2/26 11:16
#9
Not too shy to talk
Joined: 2006/11/28 14:16Last Login
: 2014/4/18 13:26
From Weston-Super-Mare, Somerset, UK, Europe, Earth, Milky Way, The Universe
Group:
Registered Users
@freddix Yes. A good tip is to initialise the structure to sensible defaults as much as possible in your constructor. Can save weird bugs and headaches later.
Re: GCC, Dynamic structure creation method ?
Posted on:
2009/2/26 13:58
#10
Quite a regular
Joined: 2008/11/3 12:06Last Login
: 2023/8/2 22:18
From South France
Group:
Registered Users
@xeron Do you think that handling the list per ranges can be good ? For example : Default is defined with 512 entries, If more than 512, directly re-allocate 1024 entries, if more than 1024, directly re-allocate 1536 entries, if more than 1536, directly re-allocate to 2048 entries, etc ... Good idea ?
All we have to decide is what to do with the time that is given to us.
Re: GCC, Dynamic structure creation method ?
Posted on:
2009/2/26 16:32
#11
Home away from home
Joined: 2006/11/20 16:26Last Login
: 11/3 19:40
From Norway
Group:
Registered Users
@freddix
typedef struct {
void * last ;
void * next ;
BOOL Exist ;
int Type ;
BOOL Hidden , Culling , Transparency , WireFrame , Fog , Ambient , Lights , Filtering ;
float XPos , YPos , YPos ;
float XRot , YRot , ZRot ;
float XScale , YScale , ZScale ;
float RGBRed , RGBGreen , RGBBlue ;
} Object3DType ;
If you add a next and last pointer you can add objects, so it becomes a list, note that there exist a node and list structure in exec already you can use, this enable you use the exec functions for this.
If you are making a multi threaded game or program, you should be thinking about some kind of lock for etch of objects, so that one thread does not modify the same object at the same time, it called writing a thread safe programs.
(NutsAboutAmiga) Basilisk II for AmigaOS4 AmigaInputAnywhere Excalibur and other tools and apps.
Re: GCC, Dynamic structure creation method ?
Posted on:
2009/2/26 17:21
#12
Quite a regular
Joined: 2008/11/3 12:06Last Login
: 2023/8/2 22:18
From South France
Group:
Registered Users
@LiveForIt Thank you. A friend just sent me an e-mail explaining linked-list too. now I understand what I must do to handle objects dynamically. Thank you all. Kindest Regards, Freddix
All we have to decide is what to do with the time that is given to us.
Re: GCC, Dynamic structure creation method ?
Posted on:
2009/2/27 11:37
#13
Just can't stay away
Joined: 2006/11/30 11:30Last Login
: 11/18 16:17
From Finland
Group:
Registered Users
@Slash
Quote:
Does it? You learn something new everyday... I've always checked if the memory pointed is NULL or not before IExec->FreeVec.It does. It's even documented in the autodocs.
Same applies to many other functions, like DisposeObject(), DeleteMsgPort(), FreeSysObject(), Close(), DropInterface(), CloseLibrary(), ...
Re: GCC, Dynamic structure creation method ?
Posted on:
2009/2/27 18:33
#14
Quite a regular
Joined: 2008/11/3 12:06Last Login
: 2023/8/2 22:18
From South France
Group:
Registered Users
@all : Thank you for all these precious informations. When I'll get something to show, I'll post a sample ;)
All we have to decide is what to do with the time that is given to us.
Re: GCC, Dynamic structure creation method ?
Posted on:
2009/2/28 21:33
#15
Quite a regular
Joined: 2008/11/3 12:06Last Login
: 2023/8/2 22:18
From South France
Group:
Registered Users
I encounter another problem with structures.
I've made this with the book help :
struct Basic3DStructType {
int * LastCreatedObject ;
};
struct Basic3DStructType Basic3D ;
struct Object3DType {
int * PreviousObject , * NextObject ;
};
void BASIC3D_Destructor ( void ){
struct ObjectType * NewObject = Basic3D . LastCreatedObject ;
struct Object3DType * NextToDelete = NewObject . PreviousObject ;
}
I've removed other lines to just show what is wrong ...
I get an error on this line :
Quote:
struct Object3DType *NextToDelete = NewObject.PreviousObject; BAsic3D.c:64: error: request for member 'PreviousObject' in something not a structure or union
Why ?
I tried to change struc to these :
Quote:
struct Object3DType{ int PreviousObject, NextObject; }; Same Result ...
Any info ?
All we have to decide is what to do with the time that is given to us.
Re: GCC, Dynamic structure creation method ?
Posted on:
2009/3/1 0:06
#16
Just popping in
Joined: 2007/5/15 13:42Last Login
: 2023/3/8 14:54
From Austria
Group:
Registered Users
@freddix
Quote:
void BASIC3D_Destructor ( void ){
struct ObjectType * NewObject = Basic3D . LastCreatedObject ;
struct Object3DType * NextToDelete = NewObject . PreviousObject ;
}
BAsic3D.c:64: error: request for member 'PreviousObject' in something not a structure or union Why ?That's because NewObject is a
pointer to a structure and not a structure.
To access elements of a structure through a pointer you need the "->" operator.
struct Object3DType * NextToDelete = NewObject -> PreviousObject ;
Would be correct but will cause a DSI if NewObject is NULL.
This should work:
struct Object3DType
{
struct Object3DType * PreviousObject ;
struct Object3DType * NextObject ;
/* rest of the objectdata */
};
struct Basic3DStructType
{
struct Object3DType * LastCreatedObject ;
};
struct Basic3DStructType Basic3D ;
void BASIC3D_Destructor ( void )
{
struct Object3DType * NewObject = Basic3D . LastCreatedObject ;
if ( NewObject != NULL )
{
struct Object3DType * NextToDelete = NewObject -> PreviousObject ;
/* your code */
}
}
Re: GCC, Dynamic structure creation method ?
Posted on:
2009/3/1 0:26
#17
Quite a regular
Joined: 2008/11/3 12:06Last Login
: 2023/8/2 22:18
From South France
Group:
Registered Users
@Gazelle
ok.
Now I understand how the pointer in struc works.
Thank you Gazelle.
When compiling, I also get some errors on things I'read here for example :
definition :
typedef struct Object3DType {
...
};
Object3DType * Allocate3DObject ( int ObjectID ){
...
}
Basic3D.c:48: error: expeted '=', ',', ';', 'asm' or '__attribute__' before '*' token
and
void Free3DObject ( Object3DType * OldObject ){
...
}
Basic3D.c:71: error: expected ')' before '*' token
Why ? I use exactly what was adviced to me here in this post ...
Kindest Regards,
Freddix
Edited by freddix on 2009/3/1 10:55:00
All we have to decide is what to do with the time that is given to us.
Re: GCC, Dynamic structure creation method ?
Posted on:
2009/3/1 22:06
#18
Quite a regular
Joined: 2008/11/3 12:06Last Login
: 2023/8/2 22:18
From South France
Group:
Registered Users
@freddix no one know ?
All we have to decide is what to do with the time that is given to us.
Re: GCC, Dynamic structure creation method ?
Posted on:
2009/3/1 22:12
#19
Just popping in
Joined: 2006/11/27 21:56Last Login
: 2022/2/17 22:11
From Copenhagen
Group:
Registered Users
@freddix
I think this is wrong:
typedef struct Object3DType {
...
};
and should be:
typedef struct {
...
} Object3DType ;
Re: GCC, Dynamic structure creation method ?
Posted on:
2009/3/1 22:24
#20
Quite a regular
Joined: 2008/11/3 12:06Last Login
: 2023/8/2 22:18
From South France
Group:
Registered Users
@Shadow When I make the changes you said, I get several other error ... Like : Basic3D.c:56: error: request for member 'PreviousObject' in something not a structure or union and previous object is a component of the struct defined ...
All we have to decide is what to do with the time that is given to us.
Currently Active Users Viewing This Thread:
2
(
0 members
and 2 Anonymous Users
)