嗨,我正在做一個簡單的繪畫程序來學習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)沒有任何意義。 –