Creating A Grid

Posted By: nikka08

Creating A Grid - 03/22/10 03:00

Hi all. I'm trying to create a loop to generate a 2D 32*32 grid across the 1024*768 screen. In each cell, I would like a PANEL.

I tried using a couple simple array variables, but I came up with a few problems.

1) Can a panel be created within a while() loop using a name based on the array variable? i.e.-PANEL* Grid(1); PANEL* Grid(2)...

2) I seem to be having a problem using:
PANEL* Grid(1) =
{pos_x = grid_x[1]; pos_y = grid_y[1]}
It keeps giving me a syntax error on the "pos_x = grid_x[1];" line.

Basically, I'm trying to have a loop do the following:

Panel at 0,0
Panel at 0,32
Panel at 0,64....etc.

I seem to be going about it from the wrong direction, could someone please help me?
Posted By: DJBMASTER

Re: Creating A Grid - 03/22/10 03:08

1) When creating panels in a function, you have to use the function 'pan_create'. Panel* definitions can only be declared globally.

2) Like point 1, you have to use 'pan_create' function when building a panel at runtime. Also you can only pass constants to certain members of a panel definition.
Posted By: nikka08

Re: Creating A Grid - 03/22/10 03:13

Originally Posted By: DJBMASTER
1) When creating panels in a function, you have to use the function 'pan_create'. Panel* definitions can only be declared globally.


Ok, so I'm using the pan_create function instead of PANEL*.

Originally Posted By: DJBMASTER
2) Like point 1, you have to use 'pan_create' function when building a panel at runtime. Also you can only pass constants to certain members of a panel definition.


So are you saying that I can't use an array to define the position of the panel?

Thank you very much for your quick response.
Posted By: Elektron

Re: Creating A Grid - 03/22/10 04:07

I think you can use array but it must be global.

I hope it helps
Posted By: DJBMASTER

Re: Creating A Grid - 03/22/10 06:29

Sure you can use an array to define the position. You have 2 choices...

1) If you know the positions already use a global array.
2) If you want to calculate positions and store them in an array, you will need to know how to use 'dynamic memory', with the functions 'malloc/calloc/free'.
Posted By: nikka08

Re: Creating A Grid - 03/23/10 14:56

Originally Posted By: DJBMASTER
Sure you can use an array to define the position. You have 2 choices...

1) If you know the positions already use a global array.
2) If you want to calculate positions and store them in an array, you will need to know how to use 'dynamic memory', with the functions 'malloc/calloc/free'.


I am using a global array that I defined all of the positions for. My problem is the actual syntax of the code. Instead of pos_x = 256;, I used pos_x = Gridx[8];. And it errors. So I guess I'm just asking how you would actually code it. Again, thank you for your responses.
Posted By: TrackingKeks

Re: Creating A Grid - 03/23/10 15:31

You have to set the position in a loop,not in the panel definition to define the position. I think you have to use pan_setpos.
Posted By: DJBMASTER

Re: Creating A Grid - 03/23/10 16:59

Yes, you can't specify variables in the panel definition. They can only be changed within a function. There's no need for 'pan_setpos' because you can directly modify the positon with 'PANEL*.pos_x/pos_y'...
Code:
var gridx[5] = {0,32,64,96,128};
var gridy[5] = {0,32,64,96,128}; // bla bla
BMAP* panel_bmap = "image.bmp";

void CreateGrid()
{
PANEL* p = pan_create("",1);
p.bmap = panel_bmap;
p.pos_x = gridx[0]; p.pos_y = gridy[0];
set(p,SHOW);
}


Of course, this is tedious, so let the computer do the work...
Code:
BMAP* bmp = "image.bmp";

void CreateGrid()
{
var i,j,size = 33; // tested with a 32x32 picture.

for(i=0; i<screen_size.x; i+=size)
{
	for(j=0; j<screen_size.y; j+=size)
	{
	PANEL* p = pan_create("",1);
	p.bmap = bmp;
	p.pos_x = i; p.pos_y = j;
	set(p,SHOW);
	}
}

}


Posted By: nikka08

Re: Creating A Grid - 03/23/10 23:35

Ok, I get it now, thank you so, so much, djbmaster. Your help is very much appreciated.
© 2024 lite-C Forums