我是一名C++和SDL的初学者,正在按照一些教程编写小游戏以学习一些概念。
然而,我遇到了一个问题:我的渲染速度非常慢。我使用PerformanceCounters来计算我的循环次数,有和没有渲染函数的情况下都能得到0~2毫秒每帧的结果;但当我添加渲染函数后,每帧的时间却增加到了65毫秒左右。
请问有谁能告诉我我的渲染函数出了什么问题吗?
然而,我遇到了一个问题:我的渲染速度非常慢。我使用PerformanceCounters来计算我的循环次数,有和没有渲染函数的情况下都能得到0~2毫秒每帧的结果;但当我添加渲染函数后,每帧的时间却增加到了65毫秒左右。
请问有谁能告诉我我的渲染函数出了什么问题吗?
SDL_Texture *texture;
...
// gets called by the main loop
void render(int x_offset, int y_offset)
{
if (texture)
{
SDL_DestroyTexture(texture);
}
texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
texture_w,
texture_h);
if (SDL_LockTexture(texture, NULL, &pixel_memory, &pitch) < 0) {
printf("Oops! %s\n", SDL_GetError());
}
Uint32 *pixel;
Uint8 *row = (Uint8 *) pixel_memory;
for (int j = 0; j < texture_h; ++j) {
pixel = (Uint32 *)((Uint8 *) pixel_memory + j * pitch);
for (int i = 0; i < texture_w; ++i) {
Uint8 alpha = 255;
Uint8 red = 172;
Uint8 green = 0;
Uint8 blue = 255;
*pixel++ = ((alpha << 24) | (red << 16) | (green << 8) | (blue));
}
}
SDL_UnlockTexture(texture);
}
sdl2-config --cflags --libs
进行编译,所以是的,-g 参数在那里。我尝试删除 -g 参数并且移除了 SDL_DestroyTexture,但是没有得到更好的结果。 - Thyago B. Rodrigues