Need some help with structs...

Hi,
I am trying to create an array that divides a 128*64 pixel display in a grid. That way I can use the pixel coordinates to print rectangles. I am basing this project of JavaScript code so some parts of my code are JS code, and should be changed. But they are indicators of what I want my program to do. If someone has a solution on how to solve this with a struct or in a different way that would be great.
I thank you in advance.

Michiel de Jong

struct Coordinates {
  int x;
  int y;
}

void CreateCoordArray() {
  struct Coordinates Coordinates = (x,y);
  int i = 0;
  int j = 0;
  for (int y = 0; y <= 128; y + 8) {
    for (int x = 0; x <= 64; x + 8) {
      coordinateArray[i][j] = new Coordinates(x, y); //!!!
      i++;
    }
    j++;
    i = 0;
  }
}

void DrawTetromino() {
  for (int i = 0; i < curTetrominoLength; i++) {
    int x = curTetromino[i][0] + startX;
    int y = curTetromino[i][1] + startY;
    gameBoardArray[x][y] = 1;
    let coorX = coordinateArray[x][y].x;
    let coorY = coordinateArray[x][y].y;
    u8g2.setDrawColor(1);
    u8g2.drawBox(coorX, coorY, blokjeDimentie, blokjeDimentie);
  }
}

Make your best attempt to create a sketch and post.

I am trying to create an array that divides a 128*64 pixel display in a grid

Your incorrect nested for loops suggest otherwise.

The new keyword in C++ allocates an object on the heap. That’s not what you want in this case, especially on a microcontroller with 2KiB of RAM.

The easiest way to create an object in C++ is to just use aggregate initialization.

[color=#00979c]struct[/color] [color=#000000]Coordinates[/color] [color=#000000]{[/color]
  [color=#00979c]int[/color] [color=#000000]x[/color][color=#000000];[/color]
  [color=#00979c]int[/color] [color=#000000]y[/color][color=#000000];[/color]
[color=#000000]}[/color][color=#000000];[/color]

[color=#000000]Coordinates[/color] [color=#000000]coordinateArray[/color][color=#000000][[/color][color=#000000]9[/color][color=#000000]][/color][color=#000000][[/color][color=#000000]17[/color][color=#000000]][/color][color=#000000];[/color]

[color=#00979c]void[/color] [color=#000000]CreateCoordArray[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
  [color=#5e6d03]for[/color] [color=#000000]([/color][color=#00979c]int[/color] [color=#000000]y[/color] [color=#434f54]=[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]j[/color] [color=#434f54]=[/color] [color=#000000]0[/color][color=#000000];[/color] [color=#000000]y[/color] [color=#434f54]<=[/color] [color=#000000]128[/color][color=#000000];[/color] [color=#000000]y[/color] [color=#434f54]+=[/color] [color=#000000]8[/color][color=#434f54],[/color] [color=#000000]j[/color][color=#434f54]++[/color][color=#000000])[/color] [color=#000000]{[/color]
    [color=#434f54]// Are you sure you don't mean < 128 instead of <= 128?[/color]
    [color=#5e6d03]for[/color] [color=#000000]([/color][color=#00979c]int[/color] [color=#000000]x[/color] [color=#434f54]=[/color] [color=#000000]0[/color][color=#434f54],[/color] [color=#000000]i[/color] [color=#434f54]=[/color] [color=#000000]0[/color][color=#000000];[/color] [color=#000000]x[/color] [color=#434f54]<=[/color] [color=#000000]64[/color][color=#000000];[/color] [color=#000000]x[/color] [color=#434f54]+=[/color] [color=#000000]8[/color][color=#434f54],[/color] [color=#000000]i[/color][color=#434f54]++[/color][color=#000000])[/color] [color=#000000]{[/color]
      [color=#000000]coordinateArray[/color][color=#000000][[/color][color=#000000]i[/color][color=#000000]][/color][color=#000000][[/color][color=#000000]j[/color][color=#000000]][/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#000000]x[/color][color=#434f54],[/color] [color=#000000]y[/color][color=#000000]}[/color][color=#000000];[/color]
      [color=#434f54]// watch out, it's very easy to go out of bounds on the array![/color]
    [color=#000000]}[/color]
  [color=#000000]}[/color]
[color=#000000]}[/color]

You have to watch out for index out of bounds errors in your code. The pixel in the bottom right corner of the display is (127, 63), not (128, 64).
Also be careful when using one index for the loop exit condition, and another for array access, that’s just asking for trouble …

Pieter

Edit: Good catch @TheMemberFormerlyKnownAsAWOL, fixed the x and y increments in my reply.

You also need to fix the X and Y increments

How do you get the code to automatically color keywords like that?

Power_Broker:
How do you get the code to automatically color keywords like that?

I wrote a little JavaScript tool for it. It simply converts the HTML output from the IDE to BB codes. It’s a pretty dumb RegEx matcher, but it gets the job done.

There’s also a version that converts it to PDF.

Cool!

PieterP:
I wrote a little JavaScript tool for it. It simply converts the HTML output from the IDE to BB codes. It's a pretty dumb RegEx matcher, but it gets the job done.

There's also a version that converts it to PDF.

Just put that in my bookmarks. Thanks