我如何在表面画一条线?

How do I make a line at surface?

我知道有一个函数可以填充矩形 surface.FillRect(&Rect, uint32),但是有没有办法在表面画一条线,就像渲染器的函数 renderer.DrawLine(x1, y1, x2, y2)?

在 SDL2 中,Surface 只是一个结构,表示驱动程序不可知格式的位图(2D 像素矩阵)。在绘图方面 API 仅限于设置像素或容易定义的像素组(矩形)或将一个表面的矩形块传送(复制)到另一个表面。有了那个 API ,您在技术上可以通过计算坐标 x1,y1 和 x2,y2 之间该线上每个像素的坐标并将每个像素设置为所需颜色来绘制一条线。 SDL2库本身没有这样的实用函数。

渲染器要高级得多 API,它负责加速渲染的许多基础方面。除了单个像素和矩形之外,它还提供线基元。它使用 Texture which stores pixels in an optimized format for underlying graphics driver (and thus provides higher performance when drawing). If you are working on something new you should stick with Renderer. If you need to plug code based on Surface to Renderer code, you can create Texture from Surface data using the Renderer.CreateTextureFromSurface() 方法代替 Surface。