c - why don't my pointers work? -
i trying write old painter game, paint around squares , when perimeter done square changes colour, using ncurses. have written simple level editor lets me draw squares using asterisk. have block structure , array of them , can create blocks brute-force scanning screen asterisks , setting blocks locations. realised each block joined 2 - 4 other blocks, have pointers next up/dn/lt/rt blocks , follow pointers. save time because wouldn't have check boundary conditions or player stayed on line, check if ptr null , if not let player move in direction. can save block array , load painter game , displays correctly. have ptr current block cur_blk have set point array element 5, , works. if gdb, can print out value of *cur_blk , shows everthing right, pointers don't work. think problem in level editor, stuck.
here code. block struct:
struct block{ int id; int row, col; int alive; struct block *next_up, *next_dn, *next_lt, *next_rt; struct square *my_squares;
};
here block array setup:
struct block *bp; int cnt = 0; for(bp = &the_blocks[0]; bp < &the_blocks[1024]; bp++){ bp->id = cnt; bp->row = 0; bp->col = 0; bp->alive = false; bp->next_up = null; bp->next_dn = null; bp->next_lt = null; bp->next_rt = null; bp->my_squares = (struct square *)malloc(sizeof(struct square)*4); cnt++; }
here code give blocks correct co-ords, , link them - linky code in for(d=0;d<blk_cnt;d++)
loop. rescans array comparing co-ords, such if row 1 less , col same set the_blocks[c].next_up
. pretty sure error don't know is.
char cc, dn_ch, rt_ch; // cur/next chars int row, col; //create blocks for(row=0;row<24;row++){ for(col=0;col<61;col++){ cc = mvinch(row,col); if(cc == '*'){ the_blocks[blk_cnt].row = row; the_blocks[blk_cnt].col = col; the_blocks[blk_cnt].alive = true; blk_cnt++; } if(row<24) ; dn_ch = mvinch(row+1, col); if(col<60) ; rt_ch = mvinch(row,col+1); if(cc == '*' && dn_ch == '*' && rt_ch == '*') make_square(row,col); } } //make squares , link blocks int c,d,e, scnt; for(c=0;c<blk_cnt;c++){ int row = the_blocks[c].row; int col = the_blocks[c].col; for(e=0;e<sq_cnt;e++){ // each square, check top/base left side , rigth side. // use tlx, tly etc scnt = 0; int tlx = the_squares[e].tlx; int tly = the_squares[e].tly; int blx = the_squares[e].blx; int bly = the_squares[e].bly; int trx = the_squares[e].trx; int try = the_squares[e].try; int brx = the_squares[e].brx; int bry = the_squares[e].bry; if(row == tly && col >=tlx && col <= trx){ the_blocks[c].my_squares[scnt] = the_squares[e]; scnt++; } if(row == bly && col >=blx && col <= brx){ the_blocks[c].my_squares[scnt] = the_squares[e]; scnt++; } if(col == tlx && row >=tly && row <= bly){ the_blocks[c].my_squares[scnt] = the_squares[e]; scnt++; } if(col == trx && row >=try && row <= bry){ the_blocks[c].my_squares[scnt] = the_squares[e]; scnt++; } } for(d=0;d<blk_cnt;d++){ if((the_blocks[d].row == row-1) && (the_blocks[d].col == col)){ //the_blocks[c].next_up = (struct block *)malloc(sizeof(struct block)); the_blocks[c].next_up = &the_blocks[d]; } if((the_blocks[d].row == row+1) && (the_blocks[d].col == col)){ //the_blocks[c].next_dn = (struct block *)malloc(sizeof(struct block)); the_blocks[c].next_dn = &the_blocks[d]; } if(( the_blocks[d].col == col-1) && (the_blocks[d].row == row)){ //the_blocks[c].next_lt = (struct block )malloc(sizeof(struct block)); the_blocks[c].next_lt = &the_blocks[d]; } if((the_blocks[d].col == col+1) && (the_blocks[d].row == row)){ //the_blocks[c].next_rt = (struct block *)malloc(sizeof(struct block)); the_blocks[c].next_rt = &the_blocks[d]; } } }
here code painter game sets cur_blk pointer
cur_blk = &the_blocks[5]; move(cur_blk->row, cur_blk->col); addch('@'); refresh();
and move code, thought follow pointers:
switch(dir){ case : if(cur_blk->next_up != null){ cur_blk = cur_blk->next_up; moved = true; break; } case down : if(cur_blk->next_dn != null){ cur_blk = cur_blk->next_dn; moved = true; break; } case left : if(cur_blk->next_lt != null){ cur_blk = cur_blk->next_lt; moved = true; break; } case right : if(cur_blk->next_rt != null){ cur_blk = cur_blk->next_rt; moved = true; break; } }
i stuck unreal. in advance help. can post more code if needed. level editor works apart linky bit!
Comments
Post a Comment