Search code examples
c++matrixdrawallegroallegro5

How to draw a binary matrix to the display


I'm recreating tetris in c++ for a starter project. I'm using allegro for user input and drawing to the display. Every action, move, rotation is inserted into a 2x2 matrix. Now I got stuck at drawing the actual matrix to the screen. Here's my current code:

void drawGrid(int grid[10][20]) { // Draws the grid to the screen
    for (int y = 0; y < 20; y++) {
        for (int x = 0; x < 10; x++) {
            if (grid[x][y] == 1) {
                al_draw_filled_rectangle(x * 32 + 10, y * 32, x * 32 + 10 + 32, y * 32 + 32,
                                     al_map_rgb(255, 255, 255)); // Draws a white square, 32x32
            }
        }
    }
}

It displays the white squares rotated by 90 degrees and there are some random amounts of spaces in between parts of ones. For instance

The grid I'm trying to print:

0011000000
0001100000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000

The results I'm getting:enter image description here


Solution

  • I could tell you that your matrix picture does not match the 2D array you pass into the function. The function takes an array of 10 red with 20 columns. Your picture is inverted therefore the way your screen is colored is inverted. If you want to draw what you have on the drawing just switch rows and columns so pass in an array of [20][10]