Search code examples
c++arraysglut

2D array pointer in C++ GLUT modified using keyboard function


I've been trying to modify a 2d array grid when the user enters a key. All my program does is create a 2d grid, where each cell either is blocked/open. I allocate the 2d array in my main.cpp class, like so:

point** grid = new point*[size];
for (int i = 0; i < size; i++) grid[i] = new point[size];

Then, I route it to my display.cpp through a method called display(). I create a global variable point** g, to store the 2d array i allocated in main.cpp, then, i modify certain cell's blocked/open values when space is pressed.

point** g;
void display(int argc, char** argv, float size, float gridSize, point** _g) {
    //Assign grid, sz and grid_Sz
    _g = g;
    sz = size;
    grid_Sz = gridSize;
    //Initialize the GLUT library and negotiate a session with the window system
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(VWIDTH, VHEIGHT);
    glutCreateWindow("Pathfinding Creator");
    glutDisplayFunc(render_callback);
    glutKeyboardFunc(key_callback);
    glutReshapeFunc(resize);
}

//Key input callback
void key_callback(unsigned char key, int x, int y) {
//Start a point that the player is in at the top left corner of the grid (0,0)
    point position;
    position.x = 0;
    position.y = 0;

    //Constant ASCII codes
    const int ESC = 27;
    const int SPACE = 32;
    switch (key) {
    case ESC:
        exit(0);
        break;
    case SPACE:
        //Toggles the grid cell's 'blocked state' if true, put to false, false put to true...
        g[position.x][position.y].blocked = (g[position.x][position.y].blocked) ? false : true;
    }
}        

The problem that I am having is a way for the 2d grid to go back to main.cpp. I want it back, because main.cpp handles all the writing of the 2d grid to a .txt file.

I've now tried to make display() not return anything and declare _g as a extern point** in my display.h, which i then use inside main.cpp.

This changes nothing, my program still crashes and i get an access violation. It crashes when space is pressed, or when the assignment of _g[position.x][position.y].blocked is set. In display.h:

extern point** _g;

In display.cpp:

void display(int argc, char** argv, float size, float gridSize, point** grid) {
    //Assign grid, sz and grid_Sz
    sz = size;
    grid_Sz = gridSize;
    _g = grid;
    .....
}

Finally, i just assign _g in my main.cpp to the allocated grid:

point** grid = new point*[size];
for (int i = 0; i < size; i++) grid[i] = new point[size];
grid = _g;

I'm not sure if there is a better way to do this, or if my previous method could've worked with some tinkering. Any help will be appreciated.


Solution

  • I think you misunderstand what your _g = g; line is doing. This will take the pointer value at g, and assign that value to the local variable _g. This will not copy the contents of what is pointed to. – 1201ProgramAlarm

    Thanks alot! I realized that _g wasn't actually copying the contents of g, so i moved the memory allocation into the display.cpp file, and everything worked out from there.