Coding: Dynamically Defined Variables April 27, 2010 02:57PM | Registered: 14 years ago Posts: 121 |
Re: Coding: Dynamically Defined Variables April 27, 2010 06:35PM | Registered: 15 years ago Posts: 379 |
// Object Structure enum ObjType { OT_Static, OT_Anim }; typedef struct { int x, y; enum ObjType type; void *image_data; } ObjectType; typedef struct { int width; int height; } StaticImageType; typedef structure { int width, height; int tile_width, tile_height; int min_frame, current_frame, max_frame; } AnimatedImageType; // Create 100 Objects ObjectType Objs[100]; // Initialization Functions void Init_Static_Object(int id, int width, int height) { StaticImageType *tempData = malloc(sizeof(StaticImageType)); Objs[id].width = width; Objs[id].height = height; Objs[id].image_data = tempData; } void Init_Static_Object(int id, int width, int height) { StaticImageType *tempData = malloc(sizeof(StaticImageType)); tempData->width = width; tempData->height = height; Objs[id].type = OT_Static; Objs[id].image_data = (void*)tempData; } void Init_Anim_Object(int id, int width, int height) { AnimatedImageType *tempData = malloc(sizeof(AnimatedImageType)); tempData->width = width; tempData->height = height; Objs[id].type = OT_Anim; Objs[id].image_data = (void*)tempData; } void Draw_Object(int id) { if (Objs[id].type == OT_Static) { StaticImageType *tempData = (StaticImageType*)Objs[id].image_data; //Do drawing static }else if (Objs[id].type == OT_Anim) { AnimatedImageType *tempData = (AnimatedImageType*)Objs[id].image_data; //Do drawing with animation } }
Re: Coding: Dynamically Defined Variables April 28, 2010 05:26AM | Registered: 14 years ago Posts: 121 |
Re: Coding: Dynamically Defined Variables April 28, 2010 05:49AM | Registered: 14 years ago Posts: 121 |
#include#include // Object Structure typedef struct { int x, y; void *image_data; } ObjectType; typedef struct { int width; int height; } StaticImageType; typedef struct { int width, height; int tile_width, tile_height; int min_frame, current_frame, max_frame; } AnimatedImageType; // Create 100 Objects ObjectType Objs[100]; // Initialization Functions void Init_Static_Object(int id, int width, int height) { StaticImageType *tempData = malloc(sizeof(StaticImageType)); tempData->width = width; tempData->height = height; Objs[id].image_data = (void*)tempData; } void Init_Anim_Object(int id, int width, int height) { AnimatedImageType *tempData = malloc(sizeof(AnimatedImageType)); tempData->width = width; tempData->height = height; Objs[id].image_data = (void*)tempData; } int main() { Init_Static_Object(0,64,64); Init_Anim_Object(1,64,64); printf("Object 0: (%d,%d)->(%d,%d)",Objs[0].x,Objs[0].y,Objs[0].image_data->width,Objs[0].image_data->height); printf("Object 1: (%d,%d)->(%d,%d)",Objs[1].x,Objs[1].y,Objs[1].image_data->width,Objs[1].image_data->height); return 0; }
Re: Coding: Dynamically Defined Variables April 29, 2010 10:18PM | Registered: 15 years ago Posts: 703 |
Re: Coding: Dynamically Defined Variables April 30, 2010 05:00AM | Registered: 14 years ago Posts: 121 |
Re: Coding: Dynamically Defined Variables April 30, 2010 05:42AM | Registered: 14 years ago Posts: 121 |
#include#include // Object Structure typedef struct { int x, y; void *image_data; } ObjectType; typedef struct { int width; int height; } StaticImageType; typedef struct { int width, height; int tile_width, tile_height; int min_frame, current_frame, max_frame; } AnimatedImageType; // Create 100 Objects ObjectType Objs[100]; // Initialization Functions void Init_Static_Object(int id, int width, int height) { StaticImageType *tempData = (StaticImageType *) malloc(sizeof(StaticImageType)); tempData->width = width; tempData->height = height; Objs[id].image_data = (StaticImageType *)tempData; } void Init_Anim_Object(int id, int width, int height) { AnimatedImageType *tempData = (AnimatedImageType *) malloc(sizeof(AnimatedImageType)); tempData->width = width; tempData->height = height; Objs[id].image_data = (AnimatedImageType *)tempData; } int main() { Init_Static_Object(0,64,64); Init_Anim_Object(1,64,64); printf("Object 0: (%d,%d)->(%d,%d)",Objs[0].x,Objs[0].y,Objs[0].image_data->width,Objs[0].image_data->height); printf("Object 1: (%d,%d)->(%d,%d)",Objs[1].x,Objs[1].y,Objs[1].image_data->width,Objs[1].image_data->height); return 0; }
Re: Coding: Dynamically Defined Variables April 30, 2010 06:05AM | Registered: 15 years ago Posts: 703 |
Re: Coding: Dynamically Defined Variables April 30, 2010 02:09PM | Registered: 14 years ago Posts: 121 |
Re: Coding: Dynamically Defined Variables April 30, 2010 06:58PM | Registered: 15 years ago Posts: 703 |
typedef struct { int width; int height; } StaticImageType; typedef struct { int x, y; StaticImageType* image_data; } ObjectType;
Re: Coding: Dynamically Defined Variables May 05, 2010 02:27AM | Registered: 14 years ago Posts: 121 |
Re: Coding: Dynamically Defined Variables May 05, 2010 04:43AM | Moderator Registered: 15 years ago Posts: 703 |
Quote
LordAshes
With all due respect, I know you are just trying to help, but I believe I answered this in this thread above.
The reason why this is not possible is because the structure that image_data points to is not known at design time. In the simplified example above, it can point to either the startic or dynamic image structure. In my real code it can point to one of a hundred different structures. The decision as to which structure it points to is made at runtime.
I am fairly sure I have the pointer address correct (i.e. using memory commands I could probably read the structure data) but I would like it to recognize the structure so I can use references like objs[0].image_data->width.
This means the design time type needs to be generic (i.e. void) but at run-time this needs to be changed to one of the structure types.
(typing via PPC)
Re: Coding: Dynamically Defined Variables May 05, 2010 07:14AM | Registered: 15 years ago Posts: 686 |
#includeTakes a bit more memory, but this is probably the simplest way unless you want to move the type field into ObjectType and make image_data a union.#include #define IMAGE_TYPE_STATIC 0 #define IMAGE_TYPE_ANIMATED 1 typedef struct { int width; int height; int type; // these fields are only valid if type==IMAGE_TYPE_ANIMATED int tile_width; int tile_height; int min_frame; int current_frame; int max_frame; } ImageType; // Object Structure typedef struct { int x, y; ImageType *image_data; } ObjectType; // Create 100 Objects ObjectType Objs[100]; // Initialization Functions void Init_Static_Object(int id, int width, int height) { ImageType *tempData = (ImageType *)malloc(sizeof(ImageType)); tempData->width = width; tempData->height = height; tempData->type = IMAGE_TYPE_STATIC; Objs[id].image_data = tempData; } void Init_Anim_Object(int id, int width, int height) { ImageType *tempData = (ImageType *)malloc(sizeof(ImageType)); tempData->width = width; tempData->height = height; tempData->type = IMAGE_TYPE_ANIMATED; // set the other fields? Objs[id].image_data = tempData; } int main() { Init_Static_Object(0,64,64); Init_Anim_Object(1,64,64); printf("Object 0: (%d,%d)->(%d,%d)",Objs[0].x,Objs[0].y,Objs[0].image_data->width,Objs[0].image_data->height); printf("Object 1: (%d,%d)->(%d,%d)",Objs[1].x,Objs[1].y,Objs[1].image_data->width,Objs[1].image_data->height); return 0; }
Re: Coding: Dynamically Defined Variables May 05, 2010 05:04PM | Registered: 14 years ago Posts: 121 |
Quote
scanff
Sorry I missed that part. Sounds like a hard way of doing it. Does you're code need to be strict C, this type of problem is exactly why C++ was created. You can use polymorphism and derive from a base class, much easier than casting voids and adding checks to see what the actual data type is.
Re: Coding: Dynamically Defined Variables May 05, 2010 05:09PM | Registered: 14 years ago Posts: 121 |
Quote
tueidj
Completely off the top of my head, might have typos/stupid mistakes:{Code removed to reduce space}Takes a bit more memory, but this is probably the simplest way unless you want to move the type field into ObjectType and make image_data a union.
Re: Coding: Dynamically Defined Variables May 05, 2010 08:05PM | Registered: 15 years ago Posts: 686 |
#include#include // Object Structure #define OBJECT_TYPE_STATIC_IMAGE 0 #define OBJECT_TYPE_ANIMATED_IMAGE 1 // more defines for other object types go here typedef struct { int x, y; int type; union { StaticImageType *StaticImageData; AnimatedImageType *AnimatedImageData; // other type pointers go here }; } ObjectType; typedef struct { int width; int height; } StaticImageType; typedef struct { int width, height; int tile_width, tile_height; int min_frame, current_frame, max_frame; } AnimatedImageType; // Create 100 Objects ObjectType Objs[100]; // Initialization Functions void Init_Static_Object(int id, int width, int height) { StaticImageType *tempData = (StaticImageType *)malloc(sizeof(StaticImageType)); tempData->width = width; tempData->height = height; Objs[id].type = OBJECT_TYPE_STATIC_IMAGE; Objs[id].StaticImageData = tempData; } void Init_Anim_Object(int id, int width, int height) { AnimatedImageType *tempData = (AnimatedImageType *)malloc(sizeof(AnimatedImageType)); tempData->width = width; tempData->height = height; Objs[id].type = OBJECT_TYPE_ANIMATED_IMAGE; Objs[id].AnimatedImageData = tempData; } int main() { int i; Init_Static_Object(0,64,64); Init_Anim_Object(1,64,64); for (i=0;i<2;i++) { switch (Objs.type) { case OBJECT_TYPE_STATIC_IMAGE: printf("Object %d: (%d,%d)->(%d,%d)\n", i,Objs.x,Objs.y,Objs.StaticImageData->width,Objs.StaticImageData->height); break; case OBJECT_TYPE_ANIMATED_IMAGE: printf("Object %d: (%d,%d)->(%d,%d)\n", i,Objs.x,Objs.y,Objs.AnimatedImageData->width,Objs.AnimatedImageData->height); break; default: printf("I don't know wtf this object is.\n"); } } return 0; }
Re: Coding: Dynamically Defined Variables May 06, 2010 03:03AM | Registered: 14 years ago Posts: 121 |
Quote
tueidj
Well you need to at least give an accurate example if you want people to improve it. Let's try again.
Quote
LordAshes
The reason why this is not possible is because the structure that image_data points to is not known at design time. In the simplified example above, it can point to either the startic or dynamic image structure. In my real code it can point to one of a hundred different structures. The decision as to which structure it points to is made at runtime.
Re: Coding: Dynamically Defined Variables May 06, 2010 09:47AM | Registered: 15 years ago Posts: 686 |
Re: Coding: Dynamically Defined Variables May 06, 2010 01:50PM | Registered: 15 years ago Posts: 858 |
Re: Coding: Dynamically Defined Variables May 10, 2010 04:54PM | Registered: 14 years ago Posts: 121 |
Quote
tueidj
Do you expect C to somehow magically know what your structs will look like without defining them?
Quote
WikiFSX
That's it. If you want C to know about all ~500 types, you are going to have to program support for all of them. This isn't some scripting language where you just automagically throw things in variables.