使用 SDL2 渲染器绘制填充 quad/triangle 的最快方法?
Fastest way to draw filled quad/triangle with the SDL2 renderer?
我有一个使用 SDL2 编写的游戏,以及用于绘图的 SDL2 渲染器(硬件加速)。有绘制填充四边形或三角形的技巧吗?
目前我只是通过画很多线来填充它们 (SDL_Drawlines),但是性能很糟糕。
我不想进入 OpenGL。
不可能。 SDL2 不包含成熟的渲染引擎。
部分选项:
SDL_RenderGeometry()
/SDL_RenderGeometryRaw()
were added in SDL 2.0.18:
- Added
SDL_RenderGeometry()
and SDL_RenderGeometryRaw()
to allow rendering of arbitrary shapes using the SDL 2D render API
示例:
// g++ main.cpp `pkg-config --cflags --libs sdl2`
#include <SDL.h>
#include <vector>
int main( int argc, char** argv )
{
SDL_Init( SDL_INIT_EVERYTHING );
SDL_Window* window = SDL_CreateWindow("SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN );
SDL_Renderer* renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
const std::vector< SDL_Vertex > verts =
{
{ SDL_FPoint{ 400, 150 }, SDL_Color{ 255, 0, 0, 255 }, SDL_FPoint{ 0 }, },
{ SDL_FPoint{ 200, 450 }, SDL_Color{ 0, 0, 255, 255 }, SDL_FPoint{ 0 }, },
{ SDL_FPoint{ 600, 450 }, SDL_Color{ 0, 255, 0, 255 }, SDL_FPoint{ 0 }, },
};
bool running = true;
while( running )
{
SDL_Event ev;
while( SDL_PollEvent( &ev ) )
{
if( ( SDL_QUIT == ev.type ) ||
( SDL_KEYDOWN == ev.type && SDL_SCANCODE_ESCAPE == ev.key.keysym.scancode ) )
{
running = false;
break;
}
}
SDL_SetRenderDrawColor( renderer, 0, 0, 0, SDL_ALPHA_OPAQUE );
SDL_RenderClear( renderer );
SDL_RenderGeometry( renderer, nullptr, verts.data(), verts.size(), nullptr, 0 );
SDL_RenderPresent( renderer );
}
SDL_DestroyRenderer( renderer );
SDL_DestroyWindow( window );
SDL_Quit();
return 0;
}
请注意,由于 API 缺少 Z 坐标的数据通道,因此只能实现 affine texturing。
我有一个使用 SDL2 编写的游戏,以及用于绘图的 SDL2 渲染器(硬件加速)。有绘制填充四边形或三角形的技巧吗?
目前我只是通过画很多线来填充它们 (SDL_Drawlines),但是性能很糟糕。
我不想进入 OpenGL。
不可能。 SDL2 不包含成熟的渲染引擎。
部分选项:
SDL_RenderGeometry()
/SDL_RenderGeometryRaw()
were added in SDL 2.0.18:
- Added
SDL_RenderGeometry()
andSDL_RenderGeometryRaw()
to allow rendering of arbitrary shapes using the SDL 2D render API
示例:
// g++ main.cpp `pkg-config --cflags --libs sdl2`
#include <SDL.h>
#include <vector>
int main( int argc, char** argv )
{
SDL_Init( SDL_INIT_EVERYTHING );
SDL_Window* window = SDL_CreateWindow("SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN );
SDL_Renderer* renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
const std::vector< SDL_Vertex > verts =
{
{ SDL_FPoint{ 400, 150 }, SDL_Color{ 255, 0, 0, 255 }, SDL_FPoint{ 0 }, },
{ SDL_FPoint{ 200, 450 }, SDL_Color{ 0, 0, 255, 255 }, SDL_FPoint{ 0 }, },
{ SDL_FPoint{ 600, 450 }, SDL_Color{ 0, 255, 0, 255 }, SDL_FPoint{ 0 }, },
};
bool running = true;
while( running )
{
SDL_Event ev;
while( SDL_PollEvent( &ev ) )
{
if( ( SDL_QUIT == ev.type ) ||
( SDL_KEYDOWN == ev.type && SDL_SCANCODE_ESCAPE == ev.key.keysym.scancode ) )
{
running = false;
break;
}
}
SDL_SetRenderDrawColor( renderer, 0, 0, 0, SDL_ALPHA_OPAQUE );
SDL_RenderClear( renderer );
SDL_RenderGeometry( renderer, nullptr, verts.data(), verts.size(), nullptr, 0 );
SDL_RenderPresent( renderer );
}
SDL_DestroyRenderer( renderer );
SDL_DestroyWindow( window );
SDL_Quit();
return 0;
}
请注意,由于 API 缺少 Z 坐标的数据通道,因此只能实现 affine texturing。