Difficulty setting and testing object attributes

  allegro, allegro4, c++, oop

So I am working on a tile based map game with allegro 4.4 using VS2010

There is going to be pathfinding involved later so I have node structures that represent the tiles on the map. The node structures are contained with a Graph structure that I have called ‘map’ for this program. The game starts up with an empty 16×16 grid. The idea is that the player can create the map layout themselves by selecting a terrain type on the menu, and then clicking on a grid tile to make that grid tile the selected terrain type. Once the player selects a map tile, the ‘set’ boolean for the node object representing that map tile is set to true and the terrain information and node position is written into the node object attributes.

So to draw the selected tile terrain to the screen, I have a drawMap() function that loops through each node in the map and if the boolean attribute ‘set’ is true for a node then the corresponding tile should be drawn. Please see code below.

void getInput(Menu option, Graph map)   {
    float x = 0;
    float y = 0;
    int tile = 0; 
    int enumStore = 0;
    bool inputFlag = false;

    //retrieve mouse position when clicked
    if(mouse_b & 1)
    {
        inputFlag = true;
        x = mouse_x;
        y = mouse_y;
    }
    if(inputFlag)
    {
        //set index node cost attribute to currently selected terrain
        x = floor(x/tileSize);
        y = floor(y/tileSize);
        tile = (16*y)+x;    //16 because grid is 16x16
        map.nodes[tile].terrain = option.current;
        map.nodes[tile].nodex = x*tileSize; //store position of top left corner of tile with node attributes
        map.nodes[tile].nodey = y*tileSize;
        map.nodes[tile].set = true;
        textprintf_ex(screen, font, 0, 15, WHITE, 0, "Current Menu Selection: %i", option.current);
        textprintf_ex(screen, font, 0, 25, WHITE, 0, "tile %i is now set", tile);
    }
}

void drawMap(BITMAP *buffer, BITMAP *tiles[], Graph map)    {
    for(int i=0; i<map.tiles; i++)
    {
        if(map.nodes[i].set)
        {
            blit(tiles[map.nodes[i].terrain], buffer, 0, 0, map.nodes[i].nodex, map.nodes[i].nodey,
                                    tiles[map.nodes[i].terrain]->w, tiles[map.nodes[i].terrain]->h);
            textprintf_ex(screen, font, 0, 35, WHITE, 0, "blit executed");
        }
    }   
}
//main game loop
    while (!key[KEY_ESC])
    {
        //see if mouse needs polled before using mouse position functions
        if(mouse_needs_poll())
        {
            poll_mouse();
        }
        //change menu selection
        option.ChangeSelection(buffer);
        //get and apply player input
        getInput(option, map);

        //draw the menu and the tile grid
        option.DrawMenu(buffer);
        drawMap(buffer, tiles, map);
        drawGrid(buffer);

        show_mouse(buffer);

        //update the screen
        blit(buffer, screen, 0,0,0,0,640,480);

        //game timer 
        rest(10);
    }

My text output statements confirm that the player input is being registered to the proper node index. But for some reason the blit in the drawMap() function never seems to be executed. I don’t see how this is possible since the ‘set’ attribute for the nodes I click are definitely being set to true? I have messed around with a flag to verify that the if statement in my drawMap() function is never entered, not even briefly in a single loop.

I am just getting into the meat of learning OOP and different game design methods so please feel free to constructively point out efficiency issues or other problems in my code.

Thank you in advance!

Source: Windows Questions C++

LEAVE A COMMENT