SDL_RenderCopy如何确定绘制位置?

huangapple go评论51阅读模式
英文:

How does the SDL_RenderCopy determine where to draw?

问题

我需要根据地图渲染一些内容。现在SDL_RenderCopy接受它的坐标的负值。如果我使用SDL_RenderCopy并且dstrect={0,-16,32,32},它会将矩形的下半部分渲染到窗口的左上角。在我的情况下,地图比窗口大,会使用滚动。我可以使用下面提到的两种方法之一来渲染地图上但不在窗口上的内容。但我担心渲染所需的时间。如果我使用具有负dstrect的SDL_RenderCopy,SDL_RenderCopy函数会检查并调整dstrect以进行渲染吗,还是底层硬件流水线会处理这个工作?因为如果SDL_RenderCopy这样做,检查并在无法在窗口上进行渲染的情况下快速返回会更快,但如果底层协议检测到它,那么在矩形无法绘制的情况下,返回到执行下一可能渲染所需的时间会更长。嗯,时间差异可能不会很大,可能会在合理的硬件条件下在毫秒内完成,但当我们谈论大量矩形时(比如10000个,尽管在我的情况下甚至这个数字也不可能),我担心它可能会减慢流程。

提到的两种方法如下
滚动只在y轴上进行

  1. 让SDL_RenderCopy函数进行检查

SDL_Rect dstrect={target.x,target.y-Map.y,target.w,target.h};
SDL_RenderCopy(renderer,texture,nullptr,&dstrect);

  1. 由我来进行检查
//如果在地图范围内,则渲染
if(target.y<Map.y+Map.h+target.h && target.y>Map.y-target.h)
{
SDL_Rect dstrect={target.x,target.y-Map.y,target.w,target.h};
SDL_RenderCopy(renderer,texture,nullptr,&dstrect);
}

英文:

I need to render something based on the map. Now SDL_RenderCopy accepts negative value for it's co-ordinate. If I use SDL_RenderCopy with a dstrect={0,-16,32,32}, it would render the bottom half of the rectangle on the Top left of the window. In my case, the map is larger than the window, and scrolling would be used. I can render something that is on map but not on the window using one of the two methods mentioned below. But I am worried about the time required to render then. If I use SDL_RenderCopy with a negative dstrect, would the SDL_RenderCopy function check and adjust the dstrect to render, or the underlying hardware pipeline would do the job? Because if the SDL_RenderCopy does it, it would be faster to check and return in case the rendering can't be done on the window, but if the underlying the protocol detects it, then it would be much later in the scenario that the rectangle can't be drawn, taking longer time to return to do the next possible rendering. Well the time difference may not be that high, probably be done within ms with a reasonable hardware, but when we are talking about a lot of rectangle (say, 10000, though even that number won't be a possible in my case) then I am worried that it may slow down the process.

The mentioned two approaches are below
The scrolling is done only in the y axis

  1. Let the SDL_RenderCopy function do the check

SDL_Rect dstrect={target.x,target.y-Map.y,target.w,target.h};
SDL_RenderCopy(renderer,texture,nullptr,&dstrect);

  1. Let me do the check
//if within the Map, render
if(target.y<Map.y+Map.h+target.h && target.y>Map.y-target.h)
{
SDL_Rect dstrect={target.x,target.y-Map.y,target.w,target.h);
SDL_RenderCopy(renderer,texture,nullptr,&dstrect);
}

答案1

得分: 0

SDL_RenderCopy 执行了一项检查:

SDL_RenderGetViewport(renderer, &real_dstrect);
real_dstrect.x = 0;
real_dstrect.y = 0;
if (dstrect) {
    if (!SDL_HasIntersection(dstrect, &real_dstrect)) {
        return 0;
    }
    real_dstrect = *dstrect;
}

这段代码检查视口和您的矩形是否有交集。如果没有交集,那么您的矩形完全在窗口外部,函数会提前返回。

如果您的矩形既不完全在窗口外部也不完全在窗口内部,那么会根据渲染器的不同而有不同的处理方式。例如,OpenGL 渲染器交由驱动程序处理,而软件渲染器则使用适当的混合函数进行剪切,该函数调用了SDL_UpperBlit

英文:

SDL_RenderCopy does perform a check:

SDL_RenderGetViewport(renderer, &real_dstrect);
real_dstrect.x = 0;
real_dstrect.y = 0;
if (dstrect) {
    if (!SDL_HasIntersection(dstrect, &real_dstrect)) {
        return 0;
    }
    real_dstrect = *dstrect;
}

This checks whether the viewport and your rect have an intersection. If they don't, then your rect is completely outside the window and the function returns early.

If your rect is neither completely outside nor completely inside the window, then what happens depends on the renderer. For example, the OpenGL renderer leaves it up to the driver, whereas the software renderer does the clipping itself using the appropriate blitting function, which calls SDL_UpperBlit.

huangapple
  • 本文由 发表于 2023年5月28日 10:30:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定