Arrays of engine object pointers

Posted By: lostclimate

Arrays of engine object pointers - 09/12/13 18:35

I had this figured out a long time ago, but for the life of me i cant figure out the syntax to have a struct contain an array of engine object pointers.

I want to write my own custom menu struct, and in it I have declared an unitialized TEXT* but every time I try to assign txt_create to it I run into various different problems in everyway i can think to fix it. One way I get a "cant convert pointer to struct TEXT" which doesnt make sense to me because txt_create isnt supposed to actually return the object, but a pointer to. I cant remember how but I can get it to work in the [0] (1st place in the array), after that the other text objects dont seem to function. here is my current code, i think its the "cant convert" problem prev. described in its current state:

Code:
........

printm(str_create("blah"));


........

function printm(STRING* msg)
{

	TEXT* msg_txt[2];//=txt_create(msg,6);
	
	printm_menu->menu_text=malloc(sizeof(TEXT*)*2);
	
	(printm_menu->menu_text)[0]=txt_create(1,8);
	//(printm_menu->menu_text)[1]=txt_create(1,9);
	str_cpy(((printm_menu->menu_text)[0]->pstring)[0],str_create("test"));
	//str_cpy(((printm_menu->menu_text)[1]->pstring)[0],str_create("OK"));

	
	
	(printm_menu.menu_text)[0].pos_x=printm_menu.bg.pos_x+(30*(desktop_size_x/1024));   //positioning
	(printm_menu.menu_text)[0].pos_y=printm_menu.bg.pos_y+(15*(desktop_size_y/768));
	
	//(printm_menu.menu_text)[1].pos_x=30;//printm_menu.bg.pos_x+(280*(desktop_size_x/1024));
	//(printm_menu.menu_text)[1].pos_y=30; //printm_menu.bg.pos_y+(120*(desktop_size_y/768));
//	
	set((printm_menu->menu_text)[0],SHOW);
	//set((printm_menu->menu_text)[1],SHOW);
	//set((printm_menu->menu_text)[1],SHOW);
	set(printm_menu.bg,SHOW);
}




It might be a little jumbled, and commented out in weird places because I was trying like 10 diff ways to make it work, but hopefully its still clear what I'm trying to accomplish.

Posted By: Wjbender

Re: Arrays of engine object pointers - 09/12/13 19:30

first the one you commented out could become
TEXT* msgtxt[arraysize];
msgtxt[index]=txt_create(msg,6);

if you wanted it...

or you could use char* mytext and allocate the array size but im not exactly sure if you want to use char ...
Posted By: alibaba

Re: Arrays of engine object pointers - 09/12/13 19:45

How about calling printm without str_create?
printm("blah");
Posted By: lostclimate

Re: Arrays of engine object pointers - 09/12/13 22:04

@wjbender

I had been doing that before, but when I try to copy the array whether its just reassigning the pointer or manually looping through the array and copying them, for some reason only the first pointer will work and the other one will not. it doesnt error, the TEXT doesnt show as visible. I need the array to be contained in the Menu object though so i need it to at the least copy. Here is the struct in current form:
Code:
typedef struct {

	void* load;
	void* unload;
	TEXT* menu_text;
	PANEL* bg;
	

 	
} MENU;

MENU* printm_menu;


@alibaba

I could do that, but it isnt really the issue at hand. I am trying to keep everything wrapped into nice pointer packages that have easily navigable linked lists in them. BTW, offtopic I might have another model for your project, although I'm not sure it really fits the style, I have it laying around from a contest I did.
Posted By: alibaba

Re: Arrays of engine object pointers - 09/13/13 09:52

After looking through you code i think you first have to initialize the struct:
printm_menu=malloc(sizeof(MENU));
Posted By: Wjbender

Re: Arrays of engine object pointers - 09/13/13 09:58

and dont forget that you need to call txt_create per text item with the incrementing index instead of just msgtxt[2] ..

i tried to malloc an array of text objects
but were unable to use use text create on them
txt create passes an object and even after trying to cast it i could not..
Posted By: Arrovs

Re: Arrays of engine object pointers - 09/13/13 13:09

First i dont know if you initialized your struct at all.
Second you only made pointer in struct, but you needed pointer array.
Of course you could play around that, but then you always need extra pointer variable which then says "im saving here array" like this:
TEXT** texts = menu.texts;
Third you dont need make new strings for copying chars. That eat ram and you need to remember remove them at end
Ok cant remember more things at moment, but here you are.

This fixed one works good:

Code:
#include <acknex.h>

//for new struct allocating
#define new(struktura) sys_malloc(sizeof(struktura))
#define newm(struktura, daudzums) sys_malloc(daudzums*sizeof(struktura))

//testing font
FONT* fonts = "Arial#24b";

typedef struct 
{
	void* load;
	void* unload;
	TEXT** texts;//this is pointer array
	var text_count;
	PANEL* bg;
}Menu;

Menu* printm_menu;

//menu initialization function
Menu* Menu_inic()
{
	Menu* lok_menu = new(Menu);
	lok_menu.load = NULL;
	lok_menu.unload = NULL;
	lok_menu.texts = NULL;
	lok_menu.text_count = 0;
	lok_menu.bg = NULL;
	return lok_menu;
}
//menu destructor function
function Menu_del(Menu* menu)
{
	//if they here custom structs or vars
	if(menu.load != NULL)
	{
		sys_free(menu.load);
	}
	if(menu.unload != NULL)
	{
		sys_free(menu.unload);
	}
	//here you remove every menu text
	if(menu.texts != NULL)
	{
		int lok_i;
		for(lok_i = 0; lok_i < menu.text_count; lok_i++)
		{
			ptr_remove((menu.texts)[lok_i]);
		}
		//after then remove pointer array
		sys_free(menu.texts);
	}
	//and next things
	if(menu.bg != NULL)
	{
		sys_free(menu.bg);
	}
	//text_count removes together with this struct as it is member not pointer
	sys_free(menu);
	return NULL;
}
//Just extra for auto counting texts
function Menu_create_texts(Menu* menu, int count)
{
	menu.texts = newm(TEXT*, 2);
	menu.text_count = 2;
	int lok_i = 0;
	//this part can be automatized more, 
	//but its just for so or you can take this out somewhere else.
	for(lok_i = 0; lok_i < menu.text_count; lok_i++)
	{
		(menu.texts)[lok_i] = txt_create(1, 8);
		(menu.texts)[lok_i].flags = CENTER_X | CENTER_Y;
		(menu.texts)[lok_i].font = fonts;
	}
}

//function printm(STRING* msg)
function printm()
{
	//first you forgot about menu initialization
	printm_menu = Menu_inic();
	
	//What is this???
	//TEXT* msg_txt[2];//=txt_create(msg,6);
	
	//then you made your 2 text pointers
	Menu_create_texts(printm_menu, 2);
	
	//Your string values for texts
	str_cpy(((printm_menu.texts)[0].pstring)[0], "test");
	str_cpy(((printm_menu.texts)[1].pstring)[0], "OK");

	//your positioning
	(printm_menu.texts)[0].pos_x = screen_size.x / 2;
	//printm_menu.bg.pos_x+(30*(desktop_size_x/1024));   //positioning
	(printm_menu.texts)[0].pos_y =  50;
	//printm_menu.bg.pos_y+(15*(desktop_size_y/768));
	
	(printm_menu.texts)[1].pos_x = screen_size.x / 2;
	(printm_menu.texts)[1].pos_y = 100;
	
	//set them both visible
	set((printm_menu.texts)[0],SHOW);
	set((printm_menu.texts)[1],SHOW);
	//dont know anything about yur bg
	//set(printm_menu.bg,SHOW);
	
	//and when you dont need your menu anymore delete it
	while(key_space == 0){wait(1);}
	Menu_del(printm_menu);
}

function main()
{
	//Why you need this know only you
	//printm(str_create("blah"));
	printm();
}

Posted By: lostclimate

Re: Arrays of engine object pointers - 09/13/13 16:40

Quote:
TEXT** texts;//this is pointer array



THAT is what I need laugh thank you. my struct was init'd in a diff. function, but the object was global. I hate pointers wink i forgot to set it as a pointer to a pointer laugh

Thanks soooooooo much.
© 2024 lite-C Forums