gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > A little help with arrays...

#61739 - The_Perfection - Tue Nov 22, 2005 11:44 pm

I have a slight problem: I am making a Windows shell and I need to create a 'click' routine. I think that making 'buttons' and checking if they are clicked. The problem is that I need to make an array for the buttons and their coordinates and size. I suppose that I should use a 2D array. I just don't know how to make an array. It is EXTREMELY different from a minor programming language called RPGCode.

So my question is how would you create an array, and how would you create a 2D array?
Is it possible change the size of an array without changing the contents?

Sorry if this is a simple problem, but I literally just switched to GBA programming not even a whole week ago.

#61740 - tepples - Tue Nov 22, 2005 11:48 pm

You don't need an array; you need a struct.
Code:
typedef struct GUIElement
{
  int type;  /* 0 for buttons, 1 for texts, etc */
  const char *label;  /* text string for the item */
  int left, top, width, height;  /* in pixels or tiles or whatever */
}

Look up structs in your C tutorial.

And if you aren't comfortable on the GBA, then perhaps you should work through a C tutorial entirely on the PC first.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#61742 - The_Perfection - Wed Nov 23, 2005 12:37 am

I can pretty easily understand C, its not too different from RPGCode. I have two books on C++ programming as well.
Your idea makes sense, I guess I could use structs, but I would rather stick to something that I know more about to first. I am still taking structs into thought, though.

I was thinking of something like this:
Code:
for(loop = 0; loop < noofbuttons; loop++) {
      if(cursorx > xbutton[loop] && cursorx < xbutton[loop] + xSbutton[loop]  && cursory > ybutton[loop] && cursory < ybutton[loop] + ySbutton[loop]) {
            //All of the things it would do if I clicked the button
      }
}

To do this, all I need to know how to do is make an array, and how to modify its size, if possible, to accomodate all of the buttons.

#61797 - ScottLininger - Wed Nov 23, 2005 5:42 pm

Here's a nice little tutorial on C arrays:

http://www.learn-programming.za.net/programming_c_learn06.html

-Scott

#62420 - thegamefreak0134 - Wed Nov 30, 2005 8:24 pm

Hello Friend. To make two dimentional arrays in C++, simply declare them with two dimentions rather than one. Like this:

Code:
u16 myarray[5][10];


would create an array with 5 elements by 10 elements for a total of 50 elements. You access them the same way.

Code:

u16 i = 3;
u16 j = 7;
u16 click = myarray[i][j];


Should be simple enough.

I assume you want to use the first element for x, the second for y, and the third on for actions and such?

The thing that makes C++ so different from RPG code is the fact that you have to be comfortable with types and type declarations. RPGCode, I believe, doesn't have you worry about that at all. It's even more important for GBA programming, as most hardware registers require you to store a specific size to them.
_________________
What if the hokey-pokey really is what it's all about?

[url=http:/www.darknovagames.com/index.php?action=recruit&clanid=1]Support Zeta on DarkNova![/url]