我正在尝试创建一个像素/像素的图像,并使用SDL在屏幕上显示它。图像必须刷新并再次显示〜每秒50次(我正在寻找50 FPS游戏)。我试图创建一个简单的程序来说明我想要做的事情:我创建了一个1280 * 720窗口和纹理,我交替用绿色和红色填充。 问题是代码运行速度非常慢(~8 FPS)。我在哪里陷入困境?
这是我的代码
SDL_Texture *display;
SDL_Window *window;
SDL_Renderer *renderer;
int x;
int y;
int a = 255;
int b = 0;
window = SDL_CreateWindow(gl_title, SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, gl_width, gl_height, 0);
renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED |
SDL_RENDERER_TARGETTEXTURE |
SDL_RENDERER_PRESENTVSYNC);
display = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET, gl_width, gl_height);
while (true)
{
SDL_SetRenderTarget(renderer, display);
SDL_SetRenderDrawColor(renderer, a, b, 0, 255);
for (x = 0; x < gl_width; ++x)
{
for (y = 0; y < gl_height; ++y)
{
SDL_RenderDrawPoint(renderer, x, y);
}
}
a = a == 255 ? 0 : 255;
b = b == 255 ? 0 : 255;
SDL_SetRenderTarget(renderer, NULL);
SDL_RenderCopy(renderer, display, NULL, NULL);
SDL_RenderPresent(renderer);
}
答案 0 :(得分:2)
使用每个像素的函数在屏幕上绘图会很慢。您可以在循环中手动编写像素,并避免每次调用函数的成本,或使用SDL提供的函数之一为您执行此操作:
SDL_RenderFillRect
SDL_RenderDrawLines
SDL_RenderDrawPoints
SDL_RenderDrawRect