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(int x,int y)
{
return(map[x + 80 * y]);
}
int colread(char chr)
{
switch(chr)
{
case 'k':
return(COLOR_BLACK);
break;
case 'r':
return(COLOR_RED);
break;
case 'g':
return(COLOR_GREEN);
break;
case 'y':
return(COLOR_YELLOW);
break;
case 'b':
return(COLOR_BLUE);
break;
case 'm':
return(COLOR_MAGENTA);
break;
case 'c':
return(COLOR_MAGENTA);
break;
case 'w':
return(COLOR_WHITE);
break;
}
}
void colinit()
{
init_pair(0,COLOR_BLACK, COLOR_BLACK);
init_pair(1,COLOR_BLACK,COLOR_WHITE);
init_pair(3,COLOR_BLACK,COLOR_YELLOW);
init_pair(26,COLOR_BLUE,COLOR_BLUE);
}
void colman(char bg,char fg)
{
int bgint = colread(bg);
int fgint = colread(fg);
if(bgint == 0 && fgint == 1)
{
attrset(0);
attron(A_BOLD);
}
else
{
attron(COLOR_PAIR(bgint*8+fgint));
}
}
void pixrender(int x,int y){/*oh god please no*/
addstr("rendered pixel");refresh();
char coord[3];
sprintf(coord,"%d",x);
addstr("(");
addstr(coord);
addstr(",");
sprintf(coord,"%d",y);
addstr(coord);
addstr(")");
/*
b blue
r red
w white
k black
blue for blocks, white for spikes
*/
char fku = 'f';
char spritesheet[144] = {
// 0 1 2 3 4 5 6 7 8 9 0 1
/*empty */' ','x','n','v','d','b',' ',' ','-','_','[',']',
/*block */'*','X',fku,'V','d','b',' ',' ',fku,fku,'[',']',
/*spup */'^','A','M','N','&','&',' ',' ','A',fku,'P','9',
/*spdwn */'Y',fku,'<','<','<','Z',' ',' ',fku,fku,fku,fku,
/*splft */'9','9','<','<','<','Z',' ',' ','<','<','<',fku,
/*sprht */'P','P','>','>','Z','>',' ',' ','>','>',fku,'>',
/*lava */' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
/*ice */' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
/*owup */'~','~',fku,'T','Z','Z',' ',' ','~',fku,'[',']',
/*owdown*/'-','x','7','v','z','z',' ',' ','-','=','[',']',
/*owleft*/'[','[','9','Y','<','<',' ',' ','[','L','[','|',
/*owrht */']','h','R','j','>','>',' ',' ','[',']','|',']'
};
char bgsheet[144] = {
// 0 1 2 3 4 5 6 7 8 9 0 1
/*empty */'k','b','k','k','k','k','k','k','k','k','k','k',
/*block */'b','b',fku,'k','k','k','k','k',fku,fku,'k','k',
/*spup */'k','k','k','k','k','k','k','k','k','k','k','k',
/*spdwn */'k',fku,'k','k','k','k','k','k',fku,fku,fku,fku,
/*splft */'k','k','k','k','k','k','k','k','k','k','k',fku,
/*sprht */'k','k','k','k','k','k','k','k','k','k',fku,'k',
/*lava */'k','k','k','k','k','k','k','k','k','k','k','k',
/*ice */'k','k','k','k','k','k','k','k','k','k','k','k',
/*owup */'k','k',fku,'k','k','k','k','k','k',fku,'k','k',
/*owdown*/'k','k','k','k','k','k','k','k','k','k','k','k',
/*owleft*/'k','k','k','k','k','k','k','k','k','k','k','k',
/*owrht */'k','k','k','k','k','k','k','k','k','k','k','k',
};
char fgsheet[144] = {
// 0 1 2 3 4 5 6 7 8 9 0 1
/*empty */'k','b','w','w','w','w',' ',' ','y','y','y','y',
/*block */'b','b',fku,'w','w','w',' ',' ',fku,fku,'y','y',
/*spup */'w','w','w','w','w','w',' ',' ','y',fku,'y','y',
/*spdwn */'w',fku,'w','w','w','w',' ',' ',fku,fku,fku,fku,
/*splft */'w','w','w','w','w','w',' ',' ','y','y','y',fku,
/*sprht */'w','w','w','w','w','w',' ',' ','y','y',fku,'y',
/*lava */' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
/*ice */' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
/*owup */'y','y',fku,'y','y','y',' ',' ','y',fku,'y','y',
/*owdown*/'y','y','y','y','y','y',' ',' ','-','y','y','y',
/*owleft*/'y','[','y','Y','y','y',' ',' ','y','y','y','y',
/*owrht */'y','h','y','y','y','y',' ',' ','y','y','y','y'
};
//segfault before here
int pos = getblock(x,2*y) + 12 * getblock(x,2*y+1);
colman(bgsheet[pos],fgsheet[pos]);
char cstr[2] = "\0";
cstr[0] = spritesheet[pos];
attron(A_BOLD);//why does this have to be here
addstr(cstr);
}
void render()
{
int y = 24;
int x;
while (y >= 0)
{
x = 0;
while(x < 80)
{
pixrender(x,y);
x++;
}
if (y > 0)
{
addstr("\n");
}
y-=1;
}
clear();
refresh();
}
void game()
{
colinit();
while (true)
{
render();
}
}
int main()
{
initscr();
start_color();
attron(A_BOLD);
game();
endwin();
return(0);
}
并且它可以工作......但仅在gdb中,当在gdb之外运行相同的a.out时,它会由于段错误而崩溃,有谁知道哪种交互可能会导致这种情况(或者是否是某些特定的代码相关问题) ,我能找到的最相关的东西是https://www.reddit.com/r/cprogramming/comments/18k3o01/issue_with_segfault_when_running_code_outside_of/,但似乎不会有任何潜在的内存问题,也没有任何数据库所以不应该有任何恐惧,这可能是某种未定义的行为吗?
cstr[0]=spritesheet[pos] 不应该存在