嗨,我正在做一个简单的绘画程序来学习SDL和C,当我运行我的代码时出现分割错误,我想这是因为它试图填充窗口外的像素,但我是不知道如何阻止这种情况的发生。 有没有办法阻止递归访问窗口之外? 这是代码:抱歉,它是多么混乱。如何解决递归洪水填充分段错误?
void floodfill(SDL_Surface *canvas,int SCREEN_WIDTH, int SCREEN_HEIGHT, int x, int y, int boundingX, int boundingY, Uint32 src, Uint32 fillcolour)
{
Uint32 clickedColour = getPixel(canvas, x, y);
Uint32 boundingColour = getPixel(canvas, boundingX, boundingY); //make sure you are not exceeding the limits of the window.
printf("floodfill Inside\n");
printf("X&Y co-ords %d,%d\n",x,y);
if (src==fillcolour)
return;
if (x <= 0 || x <= SCREEN_WIDTH || y <= 0 || y <= SCREEN_HEIGHT)
{
printf("saying things!\n");
return;
}
printf("%d,%d\n",x,y);
if (fillcolour != clickedColour)
putPixel(canvas, x, y, fillcolour);
if (clickedColour !=boundingColour)
return;
if ((x>=0 && x<SCREEN_WIDTH) && (y>=0 && y<SCREEN_HEIGHT))
{
putPixel(canvas, x, y, fillcolour);
printf("put Pixel x=%d and y=%d\n", x, y);
}
floodfill(canvas,SCREEN_WIDTH, SCREEN_HEIGHT, x, y+1, x, y+2, src, fillcolour);
floodfill(canvas,SCREEN_WIDTH, SCREEN_HEIGHT, x, y, x+2, y, src, fillcolour);
floodfill(canvas,SCREEN_WIDTH, SCREEN_HEIGHT, x, y, x, y+2, src, fillcolour);
floodfill(canvas,SCREEN_WIDTH, SCREEN_HEIGHT, x+1, y, x+2, y, src, fillcolour);
}
当然,如果您认为自己不在绘图空间之外,您应该在函数的开头部分进行一些检查以确保您位于正确的区域内? –
输出的最后一行打印行是什么? –
条件if(x <= 0 || x <= SCREEN_WIDTH)没有任何意义。 –