如何绘制虚线?

How to draw a dashed line?

我需要使用 SDL 绘制虚线(用于乒乓球)。 我想在 for 循环中执行此操作,因为制作和渲染 10 个不同的矩形需要太多代码行。 所以为了清楚起见,不是这个:

SDL_Rect pieceOfLine = {x,y,w,h};
SDL_Rect pieceOfLine1 = {x,y,w,h};
SDL_Rect pieceOfLine2 = {x,y,w,h};

等等

但是有点像这样(我试过了但是没用):

SDL_Rect line = {x,y,w,h};
for(int i = 0 ; i < 10 ; i++){
    SDL_RenderFillRect(renderer,&line);
    line.y += 30; //So actually a line with holes in it
}

我希望我已经足够清楚了,提前致谢,抱歉英语不好。 这是我实际需要的示例(在我的程序中):

|

|

|

|

|

|

你的方法看起来不错,但你需要使 h 等于 15,否则你只是在彼此之上绘制十条线,稍微偏移。