   Map Array with Heigh
  Hello. I wish to convert my map array to store a heigh. This is what i have:int mapa[8][8]= {{2,2,1,1,1,1,1,1},    {2,1,2,2,2,2,1,1},    {2,2,2,2,1,1,2,1},    {2,2,1,2,1,1,1,1}};I want to know why is this wrong ? What am i suposed to do, to when i call mapa[x][y][1] it will tell me its height and mapa[x][y][0] it will tell me its tile.int mapa[8][8][1]= {{2,2,1,1,1,1,1,1},    {2,1,2,2,2,2,1,1},    {2,2,2,2,1,1,2,1},    {2,2,1,2,1,1,1,1}}; Its an isometric map, if anyone has a tip for it, please it would be welcome. Other thing ive tryed is creating a struct MAP, so where i would have map.heights[8][8] and map.tiles[8][8] but i cannot find a way to fill those values since i cannot say map.tiles[8][8]= {{2,2,1,1,1,1,1,1},    {2,1,2,2,2,2,1,1},    {2,2,2,2,1,1,2,1},    {2,2,1,2,1,1,1,1}}; Would apreciate any help =][Edited by - MadHippie on May 9, 2010 5:31:24 PM]
  Well, for starters, I wouldn't recommend using raw arrays, ever, unless you found it absolutely necessary. Try using a vector of vectors if you need a 2d array. There is also boost::multi_array, but I've never been a fan of using the boost library. I prefer to stick with things in the standard namespace, especially now that TR1 is included.   ;  Im sorry, but how would i declare that with no loop?Heres what ive tryed, the whole code commented the parts with the map array.#include <stdio.h>#include <stdlib.h>#include <allegro.h>//globaisint p_x = 0, p_y = 0;int m_altura = 8, m_largura = 8;// BEGIN THE MAP ARRAY AND TRANSFORM INTO A STRUCT int mapa[8][8]=   {{2,2,1,1,1,1,1,1},    {2,1,2,2,2,2,1,1},    {2,2,2,2,1,1,2,1},    {2,2,1,2,1,1,1,1}};    struct mapa{   int x;   int y;   int altura;   int obj;int tile;} ; struct mapa *mapao[8][8];// transforma array na struct-arrayvoid passa_mapa_struct(){  for(int x =0;x<8;x++)  { for(int y = 0;y<8;y++)  {    mapao[x][y]->x = x;    mapao[x][y]->y = y;    mapao[x][y]->altura = 1;    mapao[x][y]->obj = 0;    mapao[x][y]->tile = mapa[x][y];  }}   }END_OF_FUNCTION(passa_mapa_struct); //verifica se o prox tile eh passavel  int passavel(int dir){  int prox = p_x, proy = p_y;  if(!dir || dir==0) return 0;  switch(dir)  {   case 2:prox+=1;break;   case 4:proy-=1;break;   case 6:proy+=1;break;   case 8:prox-=1; break; }  if(mapa[prox][proy]==0 || prox>m_altura-1 || proy>m_largura-1 || prox<0 || proy<0) return 0;  return 1;}END_OF_FUNCTION(passavel); // desenha o mapa no bitmap mapaMvoid desenha_mapa(BITMAP **mapaM, BITMAP **mapaL1){   BITMAP *tile, *tile2, *grama2, *grama4, *grama8, *grama6;tile = load_bitmap("tiles/grama.bmp", NULL); tile2 = load_bitmap("tiles/areia.bmp", NULL); grama2 = load_bitmap("tiles/grama2.bmp", NULL);grama6 = load_bitmap("tiles/grama6.bmp", NULL); grama8 = load_bitmap("tiles/grama8.bmp", NULL); grama4 = load_bitmap("tiles/grama4.bmp", NULL); passa_mapa_struct();  int x, y;int tx = 0; int ty = 100;    int teste1, teste2, teste3;for(y=0;y<8;y++) {    for(x=0;x<8;x++)    { ///////////////////////////////////////////////////// // HERE CHECKING THE STRUCT TILE INFO FOR THE DRAW /////////////////////////////////////////////////////// switch(mapao[x][y]->tile) {   case 1: draw_sprite(*mapaM, tile, tx, ty); break;   case 2: draw_sprite(*mapaM, tile2, tx, ty); if(mapao[y+1][x]->tile==1) draw_sprite(*mapaM, grama2, tx, ty); if(mapao[y-1][x]->tile==1) draw_sprite(*mapaM, grama8, tx, ty); if(mapao[y][x+1]->tile==1) draw_sprite(*mapaM, grama6, tx, ty); if(mapao[y][x-1]->tile==1) draw_sprite(*mapaM, grama4, tx, ty);    break;    } tx+=16;  ty-=8;    }    ty = 100+(1+y)*8;    tx = (y+1)*16; }  }END_OF_FUNCTION(desenha_mapa); // velocidade do personagemvolatile long speed_counter = 0; void increment_speed_counter() { speed_counter++;} END_OF_FUNCTION(increment_speed_counter); //contador de frames da animaçãoint frames = 0; int desce = 0;void mais_frame() {if(frames==0) desce = 0;    if(frames==2) desce = 1;    if(desce==1) frames--;    else frames++;} END_OF_FUNCTION(mais_frame); // mainint main(int argc, char *argv[]){int andando = 0;allegro_init();install_keyboard(); install_timer(); LOCK_VARIABLE(speed_counter); LOCK_FUNCTION(increment_speed_counter);install_int_ex(mais_frame, BPS_TO_TIMER(7)); install_int_ex(increment_speed_counter, BPS_TO_TIMER(40)); set_color_depth(16); set_gfx_mode(GFX_AUTODETECT, 640,480,0,0);BITMAP *my_pic;my_pic = load_bitmap("neguim.bmp", NULL); BITMAP *p21, *p22, *p23, *p41, *p42, *p43, *p61, *p62, *p63, *p81, *p82, *p83;    p21 = load_bitmap("Sprites/FFT1/21.bmp", NULL);    p22 = load_bitmap("Sprites/FFT1/22.BMP", NULL);    p23 = load_bitmap("Sprites/FFT1/23.BMP", NULL);p41 = load_bitmap("Sprites/FFT1/41.BMP", NULL);    p42 = load_bitmap("Sprites/FFT1/42.BMP", NULL);    p43 = load_bitmap("Sprites/FFT1/43.BMP", NULL);p61 = load_bitmap("Sprites/FFT1/61.BMP", NULL);    p62 = load_bitmap("Sprites/FFT1/62.BMP", NULL);    p63 = load_bitmap("Sprites/FFT1/63.BMP", NULL);   p81 = load_bitmap("Sprites/FFT1/81.BMP", NULL);    p82 = load_bitmap("Sprites/FFT1/82.BMP", NULL);    p83 = load_bitmap("Sprites/FFT1/83.BMP", NULL);int movimento = 0, direcao = 2;int contagem = 0;BITMAP *buffer; BITMAP *mapaM;BITMAP *mapaL1;mapaL1 = create_bitmap(640, 480);buffer = create_bitmap(640,480); mapaM = create_bitmap(640, 480);int my_pic_x = 0; int my_pic_y = 0;desenha_mapa(&mapaM, &mapaL1);   while(!key[KEY_ESC]) {  if(movimento==0)  {   if(key[KEY_RIGHT]) movimento = 6; else if(key[KEY_LEFT]) movimento = 4; else if(key[KEY_UP]) movimento = 8; else if(key[KEY_DOWN]) movimento = 2;   } if(passavel(movimento)==0) movimento = 0; while(speed_counter > 0){   if(contagem < 8 && movimento!=0)  {if(passavel(movimento)==1)   {  andando = 1; if(movimento!=0) direcao = movimento; switch(movimento) {    case 4: my_pic_x-= 2; my_pic_y++; break;    case 6: my_pic_x+= 2; my_pic_y--; break;case 2: my_pic_x+=2; my_pic_y++; break;    case 8: my_pic_x-=2; my_pic_y--;break;} contagem++; }  }  else if(contagem==8)  {    switch(movimento)    { case 2:p_x++; break; case 4:p_y--;break; case 8:p_x--;break; case 6:p_y++; break;   }  movimento = 0;    contagem = 0;    andando = 0;  }  speed_counter --;}draw_sprite(buffer, mapaM, 0, 0);//draw_sprite(buffer, mapaL1, 0, 0);if(andando==1){ switch(movimento) {  case 2:  if(frames==0) draw_sprite(buffer, p21, my_pic_x+5, my_pic_y+83);  else if(frames==1) draw_sprite(buffer, p22, my_pic_x+5, my_pic_y+83);   else if(frames==2) draw_sprite(buffer, p23, my_pic_x+5, my_pic_y+83);  else draw_sprite(buffer, p22, my_pic_x+5, my_pic_y+83); break;  case 4:  if(frames==0) draw_sprite(buffer, p41, my_pic_x+5, my_pic_y+83);  else if(frames==1) draw_sprite(buffer, p42, my_pic_x+5, my_pic_y+83);   else if(frames==2) draw_sprite(buffer, p43, my_pic_x+5, my_pic_y+83);  else draw_sprite(buffer, p42, my_pic_x+5, my_pic_y+83); break;  case 6:  if(frames==0) draw_sprite(buffer, p61, my_pic_x+5, my_pic_y+83);  else if(frames==1) draw_sprite(buffer, p62, my_pic_x+5, my_pic_y+83);   else if(frames==2) draw_sprite(buffer, p63, my_pic_x+5, my_pic_y+83);  else draw_sprite(buffer, p62, my_pic_x+5, my_pic_y+83); break;  case 8:  if(frames==0) draw_sprite(buffer, p81, my_pic_x+5, my_pic_y+83);  else if(frames==1) draw_sprite(buffer, p82, my_pic_x+5, my_pic_y+83);   else if(frames==2) draw_sprite(buffer, p83, my_pic_x+5, my_pic_y+83);  else draw_sprite(buffer, p82, my_pic_x+5, my_pic_y+83); break; }  }    else if(andando == 0) {   switch(direcao)   {case 2: draw_sprite(buffer, p22, my_pic_x+5, my_pic_y+83);break;  case 4: draw_sprite(buffer, p42, my_pic_x+5, my_pic_y+83);break;case 6: draw_sprite(buffer, p62, my_pic_x+5, my_pic_y+83);break;case 8: draw_sprite(buffer, p82, my_pic_x+5, my_pic_y+83);break;}}    textprintf(buffer,font,0,0,makecol(99,99,99),"X:%d Y:%d Dir: %d Passavel? %d", p_x, p_y , movimento , mapa[p_x+1][p_y]);blit(buffer, screen, 0,0,0,0,640,480); clear_bitmap(buffer);} return 0; }END_OF_MAIN()  Thanx, i would apreciate any help.[First bit of help: See the thread at the top of the forum that says "READ THIS BEFORE POSTING"? Please actually read it. And then use source tags for posting this much code. Thank you. - Zahlman][Edited by - Zahlman on May 11, 2010 5:13:46 PM];Quote:Original post by MadHippieWhat am i suposed to do, to when i call mapa[x][y][1] it will tell me its height and mapa[x][y][0] it will tell me its tile.Notice how you have two possible values for the last dimension? 0 and 1, yeah? So, you would need to have a size of 2 in that dimension, and you need to brace each pair of {tile ID, height} values.Thus:int map[8][8][2] = { { {2, 0}, {2, 1}, // etc. etc.Or,Quote:Other thing ive tryed is creating a struct MAPYes, you can do that.Quote:but i cannot find a way to fill those values since i cannot sayWell, think about the order of the dimensions. It's width, then height, then tile data, so you initialize it the same way. And it looks the same as above:Tile map[8][8] = { { {2, 0}, {2, 1}, // etc. etc.Because now, the innermost braces each surround the values for one instance of the struct.Or, you can make a constructor for the struct:struct Tile {  int appearance, height;  // The constructor:  MAP(int appearance, int height): appearance(appearance), height(height) {}};And now you can make an instance of the struct like this:Tile t(2, 0);And that means you can fill in the map one element at a time if you like:Tile map[8][8];map[0][0] = Tile(2, 0);map[0][1] = Tile(2, 1);// etc.// Or maybe you will read the values from a file, or something.