testmap.c

Example how to use Shmap library.

#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include "map.h"

int main ()
{

        char c;
        struct map_cell *cell;
        struct map *map;
        int i, j ;
        
        // Init Shared Map Memory
        map = ShmapInit(1);
        
        // Create some obstacles
        ShmapSetRectangleFlag(0.0, 0.0, 0.2, 2.1, MAP_FLAG_WALL, 0);
        ShmapSetRectangleFlag(0.0, 0.0, 3.0, 0.2, MAP_FLAG_WALL, 0);
        ShmapSetRectangleFlag(0.0, 2.0, 3.0, 2.1, MAP_FLAG_WALL, 0);
        ShmapSetRectangleFlag(2.9, 0.0, 3.0, 2.1, MAP_FLAG_WALL, 0);
        ShmapSetRectangleFlag(1.3, 0.8, 1.7, 1.2, MAP_FLAG_WALL, 0);
        ShmapSetRectangleFlag(1.8, 1.5, 2.3, 1.8, MAP_FLAG_WALL, 0);
        ShmapSetRectangleFlag(1.4, 0.9, 1.6, 1.1, MAP_FLAG_WALL, 0);
        ShmapSetRectangleFlag(1.9, 1.6, 2.2, 1.7, MAP_FLAG_WALL, 0);
        /* Main boucle */
        do{
                // Draw map up limit
                printf("\n+");
                for(i=0;i<MAP_WIDTH;i++) printf("-");
                printf("+\n");
                
                // Draw map contents
                for (j=0;j<MAP_HEIGHT;j++){
                        printf("|");
                        for(i=0;i<MAP_WIDTH;i++){
                                cell = &map->cells[j][i];
                                if (cell->flags & MAP_FLAG_WALL) c='#'; 
                                else if (cell->flags & MAP_FLAG_DET_OBST) c='O'; 
                                else c='?'; /* TODO: */
/*                                 case MAP_WALL_CSPACE :               c='X'; break; */
/*                                 case MAP_NEW_OBSTACLE :              c='O'; break; */
/*                                 case MAP_NEW_OBSTACLE_CSPACE :       c='o'; break; */
/*                                 case MAP_PATH  :             c='*'; break; */
/*                                 case MAP_FREE  :             c=' '; break; */
/*                                 case MAP_START :             c='S'; break; */
/*                                 case MAP_GOAL  :             c='G'; break; */
/*                                 default :                    c='?'; break; */
/*                              } */
                                printf("%c",c );
                        }
                        printf("|\n");
                }
                // Draw map lower limit
                printf("+");
                for(i=0;i<MAP_WIDTH;i++) printf("-");
                printf("+\n");
                
                // Question
                printf("Enter a modification on the map (-1 to exit)\n");
                printf("x="); scanf("%d",&i);
                printf("y="); scanf("%d",&j);
                printf("(%d,%d)\n", i,j);
                if (ShmapIsCellInMap(i,j)) map->cells[j][i].flags |= MAP_FLAG_DET_OBST;

        } while ((i!= -1)&& (j!= -1));
        
        // Free Memory
        ShmapFree();
        return 0;
}