Passing 2D arrays as parameters

Started by Geuben, February 01, 2011, 07:55:38 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Geuben

Good evening everyone.

I've just decided to pick up a game I started a few years ago. I originally started it in Fenix (used DIV years before that), and now decided to use Bennu.

My old code is such a mess that I have taken to rewriting most of it. In doing so I have become a bit stuck with trying to pass a 2D array of custom type to a function as a parameter.

Hopefully the following code will explain what I'm trying to do.


const
   GRID_COLUMNS = 33;
   GRID_ROWS = 25;
   GRID_OFFSET = 10;
   GRID_SQUARE_SIZE = 20;

type _square_coordinates
   int x;
   int y;
end

process procA()
private
   _square_coordinates grid[max_c][max_r];
begin
   procB( ? );
   loop
     some more code here...
   end
end

process procB( ? )
begin
int c;
int r;
begin
for ( c=0 ; c <= GRID_COLUMNS ; c += 1 )
for ( r=0 ; r <= GRID_ROWS ; r += 1 )
grid[c][r].x = ( -GRID_OFFSET_SIZE + ( GRID_SQUARE_SIZE*c) )
grid[c][r].y = ( -GRID_OFFSET_SIZE + ( GRID_SQUARE_SIZE*r) )
end
end
end



The bits I'm struggling with are the ? , I've tried what I think the correct things but none are working. I either get a compile error complaining about the function parameters of procB or complaining about the [][] when the grid is used in procB.

Is this a limitation of the language?

Thanks

SplinterGU

Download Lastest BennuGD Release: http://www.bennugd.org/node/2

Geuben

Doh.

Any ideas on an alternative method?

Also, do I need to include a module to use the load_fpg function in Bennu?

Thanks

handsource-dyko

Yes, you need to import a module to load fpg's. This is mod_map. At the very beginning of your program (the main source), put import "mod_map".

On the wiki you can find wich functions belong to wich module. You only need to import the modules of the functions that your program actually uses. This is how bennu differs from fenix, fxi included everything, but bgdi is compact, so for map functions or anything you use, bgdi imports the function from the module. This creates a smaller memory footprint, this eliminates the overhead of stuff you don't use.

You simply cant put a 2d array into a single argument, so I suggest you split that 2d array into two process/function arguments, that should work. For instance, procA(int Coloms, int Rows). Etc. Of course you also need to keep track of the data types, I've had situations where I forgot that I couldn't fit the number 300 in a byte and get very hard to solve (and rather weird) bugs! ;D

Geuben

Thanks.

That's a little disappointing that the 2D array cant be passed, I guess I will have to come up with an alternative solution.

I understand the reasons for loading only the modules required. I had a look a the modules on the wiki, but didn't search through them all and the load_fpg() page doesn't say its included in mod_map.

SplinterGU

#5
try this


const
   GRID_COLUMNS = 33;
   GRID_ROWS = 25;
   GRID_OFFSET = 10;
   GRID_SQUARE_SIZE = 20;
   GRID_OFFSET_SIZE = 10;

end

type _square_coordinates
   int x;
   int y;
end

process procA()
private
   _square_coordinates grid[ GRID_COLUMNS][GRID_ROWS];
begin
   procB( &grid );
   loop
     frame;//some more code here...
   end
end

process procB( _square_coordinates *grid )
private
int c;
int r;
begin
for ( c=0 ; c <= GRID_COLUMNS ; c += 1 )
for ( r=0 ; r <= GRID_ROWS ; r += 1 )
grid[c* GRID_COLUMNS+r].x = ( -GRID_OFFSET_SIZE + ( GRID_SQUARE_SIZE*c) );
grid[c* GRID_COLUMNS+r].y = ( -GRID_OFFSET_SIZE + ( GRID_SQUARE_SIZE*r) );
end
end
end


begin
    procA();
end
Download Lastest BennuGD Release: http://www.bennugd.org/node/2

SplinterGU

I think that you have invert the row and column... this must be: row,col
Download Lastest BennuGD Release: http://www.bennugd.org/node/2

Geuben

Thanks, I will try that now.

Surely I can put columns and rows any way round I want...they're only my interpretations of numbers.

SplinterGU

Ummm... I think that bennugd invert internally the dimensions... this works

const
   GRID_COLUMNS = 33;
   GRID_ROWS = 25;
   GRID_OFFSET = 10;
   GRID_SQUARE_SIZE = 20;
   GRID_OFFSET_SIZE = 10;



    max_c = 10;
    max_r = 10;


end

type _square_coordinates
   int x;
   int y;
end

process procA()
private
   _square_coordinates grid[GRID_COLUMNS][GRID_ROWS];
begin
   procB( &grid );
   loop
     frame;//some more code here...
   end
end

process procB( _square_coordinates *grid )
private
int c;
int r;
begin
for ( c=0 ; c <= GRID_COLUMNS ; c += 1 )
for ( r=0 ; r <= GRID_ROWS ; r += 1 )
grid[r*(GRID_ROWS+1)+c].x = ( -GRID_OFFSET_SIZE + ( GRID_SQUARE_SIZE*c) );
    grid[r*(GRID_ROWS+1)+c].y = ( -GRID_OFFSET_SIZE + ( GRID_SQUARE_SIZE*r) );
end
end
end


begin
    procA();
end


maybe the better option is that you declare this var as global or use a pointer instead of an array
Download Lastest BennuGD Release: http://www.bennugd.org/node/2

Geuben

Thanks,

That appears to do the job perfectly, will have a go at understanding just what is going on there.

I don't really want to make this a global as that's bad practice.

Quoteor use a pointer instead of an array

Can you explain that bit a little more, I thought that was what I was doing?

SplinterGU

I mean


process procA()
private
   _square_coordinates *grid=NULL;
begin
   grid = malloc((GRID_COLUMNS+1)*(GRID_ROWS+1)*sizeof(_square_coordinates));
   procB( grid );
   loop
     frame;//some more code here...
   end
   free(grid);
end
Download Lastest BennuGD Release: http://www.bennugd.org/node/2

Geuben

Ok, I understand what's going on there. What's the benefit of doing it that way over the other way?

SplinterGU

Download Lastest BennuGD Release: http://www.bennugd.org/node/2