问题描述
为了快速参考,SDL2_image库中的showimage.c示例代码具有以下代码:
For quick reference, the showimage.c example code in SDL2_image library has the following code:
/* Show the window */
SDL_SetWindowTitle(window, argv[i]);
SDL_SetWindowSize(window, w, h);
SDL_ShowWindow(window);
done = 0;
while ( ! done ) {
while ( SDL_PollEvent(&event) ) {
/* some event handling code... */
}
/* Draw a background pattern in case the image has transparency */
draw_background(renderer, w, h);
/* Display the image */
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
SDL_Delay(100);
}
SDL_DestroyTexture(texture);
}
在while(!done)
块中调用
SDL_RenderCopy()
和SDL_RenderPresent()
.
因为它只加载一张图像,所以我认为应该创建纹理并将其渲染到帧缓冲区一次,然后将其保留在那里.因此,SDL_RenderCopy()
和SDL_RenderPresent()
应该仅被调用一次:
Since it only loads one image, I thought the texture should be created and rendered to frame buffer once and just left it there. So SDL_RenderCopy()
and SDL_RenderPresent()
should be called only once:
/* Show the window */
SDL_SetWindowTitle(window, argv[i]);
SDL_SetWindowSize(window, w, h);
SDL_ShowWindow(window);
/* Draw a background pattern in case the image has transparency */
draw_background(renderer, w, h);
/* Display the image */
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
done = 0;
while ( ! done ) {
while ( SDL_PollEvent(&event) ) {
/* some event handling code... */
}
SDL_Delay(100);
}
SDL_DestroyTexture(texture);
}
在我的ubuntu 12.04上,该图像显示为我的期望值.但是,在我使用OSX 10.9.2的MBA上,一切都变黑了.
On my ubuntu 12.04, the image was shown as my expectation. However, on my MBA with OSX 10.9.2, it was all black.
为什么有区别?
推荐答案
示例代码只是演示了最可移植的呈现到屏幕的方式.您的应用不一定控制操作系统是否使用双(或三)缓冲,因此您不能依赖于始终向用户呈现单个渲染.另外,在某些情况下,默认的帧缓冲区内容可能会丢失/更改.
The example code is simply demonstrating the most portable way to render to the screen. Your app doesn't necessarily control whether the OS uses double (or triple) buffering, so you can't rely on a single render being consistently presented to the user. Also, there are potential situations where the default framebuffer contents can be lost/altered.
换句话说,您应该渲染每一帧.
In other words, you should be rendering every frame.
这篇关于在OS X上,为什么SDL2_image的showimage示例需要在while循环中执行RenderCopy()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!