Looking for help on my project (Walls via drawRect and functions) [Solved]

I am relatively new to C and am trying to write a dungeon crawler for the arduboy.
I try to draw all walls (shown as rectangles) with a function, so i´ve created a “struct”.

struct wall {
  int x;
  int y;
  byte w;
  byte h;
};

All the walls for a level are stored in an array, which I enter into a function that draws the walls. I tried to orient myself as much as possible to examples. (that’s why i use a bool, void get´s me another error)

Wall Wallslvl_1 [4] {
  {-10, -5, 140, 5}, // above
  {130, -5, 5, 70}, // right
  {-10, -5, 5, 70}, // left
  {-10, 65, 140, 5} // below
};

// code before the loop
bool wallsLVL1 (struct wall [], byte howlong);
// in the loop
bool wL1;
wL1 = wallsLVL1 (Wallslvl_1, (sizeof (Wallslvl_1) / sizeof (Wallslvl_1 [0])));

The function:

bool wallsLVL1 (struct wall [], byte howlong) {
  for (byte i = 0; i <howlong; i ++) {
    arduboy.drawRect (Wall [i] .x + movex, Wall [i] .y + movey, Wall [i] .w, Wall [i] .h, WHITE);
  }
}

I get the following errors for each “[”.

Error: expected primary-expression before ‘[’ token:
arduboy.drawRect (Wall [i] .x + movex, Wall [i] .y + movey, Wall [i] .w, Wall [i] .h, WHITE);

What did I overlook? Thank you for any help.

1 Like
bool wallsLVL1 (struct wall [], byte howlong) {
  for (byte i = 0; i <howlong; i ++) {
    arduboy.drawRect (Wall [i] .x + movex, Wall [i] .y + movey, Wall [i] .w, Wall [i] .h, WHITE);
  }
}

Your parameter is with a lower-case ‘w’. However when you iterate through the array, you are using a capital ‘W’ which is actually your structure name, not the array variable.

3 Likes

Did that work? 

2 Likes

Thank you, i´ll try when i´m home.
:slight_smile:

2 Likes

unfortunately no, the new code generates the following errors:

C: \ … \ Dungeon_Explorer.ino: In function ‘bool wallsLVL1 (wall *, byte)’:

Dungeon_Explorer: 319: error: expected primary-expression before ‘[’ token

 arduboy.drawRect (wall [i] .x + movex, wall [i] .y + movey, wall [i] .w, wall [i] .h, WHITE);

                        ^

Dungeon_Explorer: 319: error: expected primary-expression before ‘[’ token

 arduboy.drawRect (wall [i] .x + movex, wall [i] .y + movey, wall [i] .w, wall [i] .h, WHITE);

                                             ^

Dungeon_Explorer: 319: error: expected primary-expression before ‘[’ token

 arduboy.drawRect (wall [i] .x + movex, wall [i] .y + movey, wall [i] .w, wall [i] .h, WHITE);

                                                                  ^

Dungeon_Explorer: 319: error: expected primary-expression before ‘[’ token

 arduboy.drawRect (wall [i] .x + movex, wall [i] .y + movey, wall [i] .w, wall [i] .h, WHITE);

                                                                               ^

(It makes no difference if I write “wall” within the draw function with a big or small “W”.)

I also try to use a modified version of the code to check collisions, so I try to create a RECT object.
Rect wallRect = Rect {Wall [i] .x,
Wall [i] .y,
Wall [i] .w,
Wall [i] .h};
The function also generates an error:

error: expected primary-expression before ‘{’ token

 Rect wallRect = Rect {Wall [i] .x,

Try:

struct Wall {
  int x;
  int y;
  byte w;
  byte h;
};


Wall wallslvl_1 [4] {
  {-10, -5, 140, 5}, // above
  {130, -5, 5, 70}, // right
  {-10, -5, 5, 70}, // left
  {-10, 65, 140, 5} // below
};

uint8_t movex = 2;
uint8_t movey = 5;

bool wallsLVL1 (Wall walls[], byte howlong) {
  Rect r = {22, 33, 22, 88};
  for (byte i = 0; i < howlong; i ++) {
    arduboy.drawRect (walls[i].x + movex, walls[i].y + movey, walls[i].w, walls[i].h, WHITE);
  }
}

I slapped a Rect initialisation in there to show how it was done. You can remove it.

1 Like

It’s C++, not C.

void shouldn’t be erroring, this should work:

void wallsLVL1(Wall wall[], byte howlong)
{
  for (byte i = 0; i < howlong; ++i)
  {
    arduboy.drawRect(wall[i].x + movex, wall[i].y + movey, wall[i].w, wall[i].h, WHITE);
  }
}

@filmote was half right about the casing being your problem,
but your other problem was the placement of the keyword struct.

If this had been C then you would have had to write

void wallsLVL1(struct Wall wall[], byte howlong)

But since it’s C++, you don’t need the word struct there.

1 Like

You don’t seem to pass the name of the ‘wall’ bar.

Should it be…

Wall wall[]

…?

2 Likes

I thought I had.
Must have gotten distracted by the furry creature roaming around.

3 Likes

Thank you both very much, everything works fine now. :slight_smile:
But I still use “bool” because otherwise I get this error.

    Dungeon_Explorer:224: error: variable or field 'wL1' declared void

           void wL1;

                ^

Dungeon_Explorer:225: error: 'wL1' was not declared in this scope

       wL1 = wallsLVL1( wallslvl_1, (sizeof(wallslvl_1) / sizeof(wallslvl_1[0]) ));

       ^

(but the type does not bother me so it´s okay)
Thank you again, I did´t think that people would help me so fast.

Thye wallsLVL1 function does not return anything.

So rather than trying to do the following:

bool wL1
...
wL1 = wallsLVL1( wallslvl_1, (sizeof(wallslvl_1) / sizeof(wallslvl_1[0]) ));

Or even how I imagined you changed the code:

void wL1
...
wL1 = wallsLVL1( wallslvl_1, (sizeof(wallslvl_1) / sizeof(wallslvl_1[0]) ));

you can simply call the func directly and as it does not return a value, there is no need for an assignment.

wallsLVL1( wallslvl_1, (sizeof(wallslvl_1) / sizeof(wallslvl_1[0]) ));
3 Likes

I din´t know that but it works fine,
Thank you very much!

2 Likes