Problems with memory on the Gamebuino Meta

Général

josephscade

il y a 6 ans

Hello everyone,

It seems there is a problem in the Gamebuino Meta, or at least in the Meta's Library.

I am currently developing a Rogue-like game. In order to save the terrain I need to create a big, matrix-like array, in which I put every tile's value. On simple tests (for example a map of 10 * 10 tiles), it works as excepted.

But when the map grows (for example with a map of 80 * 64 tiles, there is a very strange behavior.

Consider the following program:

#include <Gamebuino-Meta.h>

const int sizeX = 80;
const int sizeY = 64;

// nothing interesting in this part
void setup() {
  gb.begin();  
}

void loop()
{
  // we artificially create an array of a "big" size...
  int *terrain = new int [sizeX * sizeY];
  // ... and we put zeros inside it
  for (int i = 0; i < sizeX * sizeY; i ++)
  {
   terrain[i] = 0;
  }

  // now we create some colors
  Color white = gb.createColor(255, 255, 255);
  Color red = gb.createColor(255, 0, 0);


  while(!gb.update());
  gb.display.clear();
  // we draw the array on the screen
  for (int y = 0; y < sizeY; y ++)
  {
    for (int x = 0; x < sizeX; x ++)
    {
      // we plot a different color depending on the value of an element of the array
      switch (terrain[y * sizeX + x])
      {
        case 0:
          gb.display.drawPixel(x, y, white);
          break;
        default:
          gb.display.drawPixel(x, y, red);
          break;
      }
    }
  }
  delete [] terrain;
}

So what do you expect from this program ?

If you understand my code, you can expect to see white everywhere, since the array is (or should be) full of zeros.
But here is what I get with the META Emulator:

(I get the same thing with my real Gamebuino Meta

Am I the only one who does not understand ?

In my humble opinion I think it is a memory issue. But I have not enough skills in C++ to solve it. What have I done wrong ? Can someone help me ?

Please help me.

Best regards, 

JosephScade


PS : please forgive my poor English, do not hesitate to ask if you don't understand.

Sorunome

NEW il y a 6 ans

You are probably RAM overflowing

int *terrain = new int [sizeX * sizeY];

As sizeX is 80 and sizeY is 64 that are 5120 entries.....with it being an int array (one int is 4 bytes large) that makes 20k bytes. The internal screen buffer is already 10k.

The META only has 32k RAM

eriban

NEW il y a 6 ans

To follow up on Sorunome's reply, as I also found out, you have to be very efficient in how you use your memory. In your case, do you really need a 4-byte int for each terrain position? I assume a 1-byte int (int8_t) will also suffice. Depending on the "resolution" you need, you may even be able to store two terrain positions in a single byte, using bit shifting and bit masks. Also, you are allocating and releasing the array each iteration of the loop. I recommend declaring it outside the loop instead. 

josephscade

NEW il y a 6 ans

Hello,

First thank you for your incredibly fast answer.

You pointed out the fact my code does not manage RAM efficiently... and you are right. I'm very sorry, I didn't think about it before : I though it was a bug (or something like that) in the Gamebuino library... how stupid I am ^^

Thank you very much for your answer, I'll optimize my code further.

Have a nice day


PS : in the code posted, I create a new array at each loop, I did it because I wanted to post something easy to read for someone who never saw my code before (the code on my computer is much, much uglier).


Thank you again