[Help]Multidimensional arrays. Read it if you need help!

Understanding the language, error messages, etc.

[Help]Multidimensional arrays. Read it if you need help!

Postby REGFly » Wed Apr 29, 2015 2:27 pm

In my snake game I'm making I use multidimensional arrays with a set size of the biggest sprite put in the array. It works for now but that's not exactly what I want. I Either would like to use a multidimensional array with different item lenghts per collum or just make a array unto wich I put another with the right lenght per sprite (Wich should work because I remember doing something similar in C++ once). But the thing is the ways I wrote don't work for some reason. Could you help?
Last edited by REGFly on Sun May 03, 2015 11:16 am, edited 2 times in total.
REGFly
 
Posts: 62
Joined: Sat Mar 07, 2015 10:21 pm

Re: Multidimensional arrays

Postby REGFly » Wed Apr 29, 2015 2:34 pm

Or better yet is there a way so see how much elements are there int the array? For example I make a 6 item array, but I only use 2 of the six blocks. How can the program find that out?
REGFly
 
Posts: 62
Joined: Sat Mar 07, 2015 10:21 pm

Re: Multidimensional arrays

Postby clement » Thu Apr 30, 2015 6:44 pm

Hi,

for multidimentinal array you can use a array of pointer :
Code: Select all
const byte background[] PROGMEM = {88,48,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF8,0x0,0x0,0x8,0x0,0x0,0x0,0x0,0x0,0x7F,0xFE,0xDA,0x40,0xFF,0xD0,0x0,0x0,0x0,0x70,0x0,0x0,0xFE,0xA,0xA0,0xFF,0xA0,0x0,0x0,0x0,0xF8,0x0,0x0,0xE,0xF9,0x20,0xFF,0x41,0xFF,0xF0,0x1,0x6C,0x0,0x0,0x1,0xF8,0x0,0xFE,0x82,0x0,0x0,0x1,0x4,0x3F,0x80,0x0,0x79,0x0,0xFD,0x4,0x0,0x0,0x1,0x7C,0xF,0xFF,0x0,0x3A,0x80,0xFA,0x8,0x0,0x0,0x0,0xF8,0x0,0x7F,0xF0,0x3A,0x80,0xF4,0x10,0xFF,0xFC,0x0,0x70,0x0,0x0,0xFC,0x18,0x0,0xE8,0x21,0xFF,0xFE,0x0,0x0,0x0,0x0,0xE,0x19,0x0,0xD0,0x1,0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x8,0x0,0xE8,0x21,0xFF,0xFE,0x0,0xF8,0xF,0xD5,0xC,0x8,0x0,0xF4,0x10,0xFF,0xFC,0x0,0x70,0x7,0xC1,0xE,0x8,0x0,0xFA,0x8,0x0,0x0,0x0,0x0,0x3,0xFF,0xE,0x8,0x0,0xFD,0x4,0x0,0x0,0x0,0x0,0x0,0x0,0xE,0x18,0x0,0xFE,0x82,0x0,0x0,0x0,0x0,0x20,0x0,0xE,0x18,0x0,0xFF,0x41,0xFF,0xF0,0x0,0x0,0x30,0x0,0x1C,0x18,0x0,0xFF,0xA0,0x0,0x0,0x0,0x0,0x3C,0x0,0xF8,0x18,0x0,0xFF,0xD0,0x0,0x0,0x0,0x0,0x3F,0xFF,0xF0,0x38,0x0,0x0,0x8,0x0,0x0,0x0,0x0,0x3F,0xFF,0x0,0x38,0x0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x80,0x0,0x78,0x0,0xFF,0xF8,0x0,0x0,0x0,0x0,0x0,0x0,0x1,0xF8,0x0,0xFF,0xF8,0x0,0x0,0x0,0x0,0x0,0x0,0xF,0xF8,0x0,0xFF,0xF8,0x0,0x0,0x0,0x0,0x0,0x0,0xFF,0xF8,0x0,0xFF,0xF8,0x0,0x0,0x0,0x0,0x0,0x7F,0xFF,0xF8,0x0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF8,0x0,};
const byte ball[] PROGMEM = {8,4,0x60,0xF0,0xD0,0x60,};
const byte flipperBas[] PROGMEM = {8,9,0x60,0x60,0xE0,0xE0,0xE0,0xC0,0xC0,0x80,0x80,};
const byte flipperHaut[] PROGMEM = {8,7,0x40,0xF0,0x38,0x38,0x1E,0xE,0x3,};

#define SIZE_ARRAY 3
byte const * multiDimArray[SIZE_ARRAY]={
  background,
  ball,
  flipperBas
};

for(int i=0;i<SIZE_ARRAY;i++)
{
  //do somthing with the pointer
}



++
clement
 
Posts: 161
Joined: Sat Oct 25, 2014 8:06 am

Re: Multidimensional arrays

Postby REGFly » Thu Apr 30, 2015 7:11 pm

You see people? This is why others (myself included) need to learn pointers. Thanks for the help :D. I didn't want go on with the menu until I got a answer so that I'd wouldn't have to rewrite alot of stuff. I already had a solution but not one I was happy with so thanks yet again :D
REGFly
 
Posts: 62
Joined: Sat Mar 07, 2015 10:21 pm

Re: [Help]Multidimensional arrays. Read it if you need help!

Postby REGFly » Sun May 03, 2015 11:18 am

Now that I'm usin a multidimensional pointer array I'm having problems doing this
Code: Select all
for(int r = 0, y = 0, x = 0; r < 6; r++)
     {
         x = (sizeof(gameMenu[r]) - 2) / 7 * 8;
         x = LCDWIDTH - x;
         gb.display.drawBitmap(x, y, gameMenu[r]);
         y = y + 8;
     }

It calculates where the menu icons need to be and draws them. It worked fine before
REGFly
 
Posts: 62
Joined: Sat Mar 07, 2015 10:21 pm

Re: [Help]Multidimensional arrays. Read it if you need help!

Postby clement » Sun May 03, 2015 3:46 pm

Please add the declarion of the array.

thx.
clement
 
Posts: 161
Joined: Sat Oct 25, 2014 8:06 am

Re: [Help]Multidimensional arrays. Read it if you need help!

Postby REGFly » Sun May 03, 2015 3:57 pm

what do you mean? It was declared, I think...
This is how I did it. Just how you showed it
Code: Select all

byte const * gameMenu[] =
{
 
 single,
 multi,
 classic,
 settings,
 credits,
 exitGame
 
};



before that I declred all the sprites
Code: Select all

const byte single[] PROGMEM =
{

//sprite

}

// so on and so on


REGFly
 
Posts: 62
Joined: Sat Mar 07, 2015 10:21 pm

Re: [Help]Multidimensional arrays. Read it if you need help!

Postby REGFly » Sun May 03, 2015 4:06 pm

The problem is that
Code: Select all

sizeof(gameMenu[r])



allways returns a value of zero now that I've swithced to a multidimensional pointer array
REGFly
 
Posts: 62
Joined: Sat Mar 07, 2015 10:21 pm

Re: [Help]Multidimensional arrays. Read it if you need help!

Postby clement » Sun May 03, 2015 4:22 pm

Sorry my english is bad and my word can be misunderstand :o



I would like , see all the code.

I have tested with a simple example :


Image

Code: Select all
#define SIZE_ARRAY 3
  byte const * multiDimArray[SIZE_ARRAY]={
    background,
    ball,
    flipperBas
  };
   
void loop()
{
 if(gb.update())
 {
   
    for(int i=0;i<SIZE_ARRAY;i++)
    {
      //do somthing with the pointer
      gb.display.drawBitmap(random(0,42),random(0,24), multiDimArray[i]);
    }
}
}


I think , you need for positioning your image the width and the height
it's the two first number in the image

I test this :
Code: Select all
 
 
    for(int i=0;i<SIZE_ARRAY;i++)
    {
       int8_t width = pgm_read_byte(multiDimArray[i]);
       int8_t height = pgm_read_byte((multiDimArray[i] + 1));

      //do somthing with the pointer
      gb.display.drawBitmap(width/2,height/2, multiDimArray[i]);
    }


it's work

I hope help you :)
clement
 
Posts: 161
Joined: Sat Oct 25, 2014 8:06 am

Re: [Help]Multidimensional arrays. Read it if you need help!

Postby REGFly » Sun May 03, 2015 4:30 pm

Thanks. I've never heard of the pgm_read_byte function. That's probably what I needed. I'll try it out a bit later then :D
REGFly
 
Posts: 62
Joined: Sat Mar 07, 2015 10:21 pm

Next

Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 61 guests

cron